netifd: update to Git HEAD (2024-09-03)
[openwrt/staging/stintel.git] / scripts / ext-toolchain.sh
1 #!/usr/bin/env bash
2 #
3 # Script for various external toolchain tasks, refer to
4 # the --help output for more information.
5 #
6 # Copyright (C) 2012 Jo-Philipp Wich <jo@mein.io>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 CC=""
23 CXX=""
24 CPP=""
25
26 CFLAGS=""
27 TOOLCHAIN="."
28
29 LIBC_TYPE=""
30 GCC_VERSION=""
31
32
33 # Library specs
34 LIB_SPECS="
35 c: ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
36 rt: librt-* librt
37 pthread: libpthread-* libpthread
38 stdcpp: libstdc++
39 thread_db: libthread-db
40 gcc: libgcc_s
41 ssp: libssp
42 gfortran: libgfortran
43 gomp: libgomp
44 atomic: libatomic
45 quadmath: libquadmath
46 asan: libasan
47 tasan: libtsan
48 lasan: liblsan
49 ubasan: libubsan
50 "
51
52 # Binary specs
53 BIN_SPECS="
54 ldd: ldd
55 ldconfig: ldconfig
56 gdb: gdb
57 gdbserver: gdbserver
58 "
59
60 OVERWRITE_CONFIG=""
61
62 test_c() {
63 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
64 #include <stdio.h>
65
66 int main(int argc, char **argv)
67 {
68 printf("Hello, world!\n");
69 return 0;
70 }
71 EOT
72 }
73
74 test_cxx() {
75 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
76 #include <iostream>
77
78 using namespace std;
79
80 int main()
81 {
82 cout << "Hello, world!" << endl;
83 return 0;
84 }
85 EOT
86 }
87
88 test_softfloat() {
89 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
90 int main(int argc, char **argv)
91 {
92 double a = 0.1;
93 double b = 0.2;
94 double c = (a + b) / (a * b);
95 return 1;
96 }
97 EOT
98 }
99
100 test_uclibc() {
101 local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
102 if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
103 local lib
104 for lib in "${sysroot:-$TOOLCHAIN}"/{lib,usr/lib,usr/local/lib}/ld*-uClibc*.so*; do
105 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
106 return 0
107 fi
108 done
109 fi
110 return 1
111 }
112
113 test_feature() {
114 local feature="$1"; shift
115
116 # find compilers, libc type
117 probe_cc
118 probe_cxx
119 probe_libc
120
121 # common toolchain feature tests
122 case "$feature" in
123 c) test_c; return $? ;;
124 c++) test_cxx; return $? ;;
125 soft*) test_softfloat; return $? ;;
126 esac
127
128 # assume eglibc/glibc supports all libc features
129 if [ "$LIBC_TYPE" != "uclibc" ]; then
130 return 0
131 fi
132
133 # uclibc feature tests
134 local inc
135 local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
136 for inc in "include" "usr/include" "usr/local/include"; do
137 local conf="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
138 if [ -f "$conf" ]; then
139 case "$feature" in
140 lfs) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?;;
141 ipv6) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?;;
142 rpc) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?;;
143 locale) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?;;
144 wchar) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?;;
145 threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
146 esac
147 fi
148 done
149
150 return 1
151 }
152
153
154 find_libs() {
155 local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
156
157 if [ -n "$spec" ] && probe_cpp; then
158 local libdir libdirs
159 for libdir in $(
160 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
161 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
162 ); do
163 if [ -d "$libdir" ]; then
164 libdirs="$libdirs $(cd "$libdir"; pwd)/"
165 fi
166 done
167
168 local pattern
169 for pattern in $(eval echo $spec); do
170 find $libdirs -name "$pattern.so*" | sort -u
171 done
172
173 return 0
174 fi
175
176 return 1
177 }
178
179 find_bins() {
180 local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
181
182 if [ -n "$spec" ] && probe_cpp; then
183 local sysroot="$("$CPP" -print-sysroot)"
184
185 local bindir bindirs
186 for bindir in $(
187 echo "${sysroot:-$TOOLCHAIN}/bin";
188 echo "${sysroot:-$TOOLCHAIN}/usr/bin";
189 echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
190 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
191 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
192 ); do
193 if [ -d "$bindir" ]; then
194 bindirs="$bindirs $(cd "$bindir"; pwd)/"
195 fi
196 done
197
198 local pattern
199 for pattern in $(eval echo $spec); do
200 find $bindirs -name "$pattern" | sort -u
201 done
202
203 return 0
204 fi
205
206 return 1
207 }
208
209 find_gcc_version() {
210 if [ -f $TOOLCHAIN/info.mk ]; then
211 GCC_VERSION=$(grep GCC_VERSION $TOOLCHAIN/info.mk | sed 's/GCC_VERSION=//')
212 return 0
213 fi
214
215 echo "Warning! Can't find info.mk, trying to detect with alternative way."
216
217 # Very fragile detection
218 GCC_VERSION=$(find $TOOLCHAIN/bin | grep -oE "gcc-[0-9]+\.[0-9]+\.[0-9]+$" | \
219 head -1 | sed 's/gcc-//')
220 }
221
222
223 wrap_bin_cc() {
224 local out="$1"
225 local bin="$2"
226
227 echo '#!/bin/sh' > "$out"
228 echo 'for arg in "$@"; do' >> "$out"
229 echo ' case "$arg" in -l*|-L*|-shared|-static)' >> "$out"
230 echo -n ' exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
231 echo -n '-idirafter "$STAGING_DIR/usr/include" ' >> "$out"
232 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
233 echo '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;' >> "$out"
234 echo ' esac' >> "$out"
235 echo 'done' >> "$out"
236 echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
237 echo '-idirafter "$STAGING_DIR/usr/include"} "$@"' >> "$out"
238
239 chmod +x "$out"
240 }
241
242 wrap_bin_ld() {
243 local out="$1"
244 local bin="$2"
245
246 echo '#!/bin/sh' > "$out"
247 echo -n 'exec "'"$bin"'" ${STAGING_DIR:+' >> "$out"
248 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
249 echo '-rpath-link "$STAGING_DIR/usr/lib"} "$@"' >> "$out"
250
251 chmod +x "$out"
252 }
253
254 wrap_bin_other() {
255 local out="$1"
256 local bin="$2"
257
258 echo '#!/bin/sh' > "$out"
259 echo 'exec "'"$bin"'" "$@"' >> "$out"
260
261 chmod +x "$out"
262 }
263
264 wrap_bins() {
265 if probe_cc; then
266 mkdir -p "$1" || return 1
267
268 local cmd
269 for cmd in "${CC%-*}-"*; do
270 if [ -x "$cmd" ]; then
271 local out="$1/${cmd##*/}"
272 local bin="$cmd"
273
274 if [ -x "$out" ] && ! grep -q STAGING_DIR "$out"; then
275 mv "$out" "$out.bin"
276 bin='$(dirname "$0")/'"${out##*/}"'.bin'
277 fi
278
279 case "${cmd##*/}" in
280 *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
281 wrap_bin_cc "$out" "$bin"
282 ;;
283 *-ld)
284 wrap_bin_ld "$out" "$bin"
285 ;;
286 *)
287 wrap_bin_other "$out" "$bin"
288 ;;
289 esac
290 fi
291 done
292
293 return 0
294 fi
295
296 return 1
297 }
298
299
300 print_config() {
301 local mktarget="$1"
302 local mksubtarget
303
304 local target="$("$CC" $CFLAGS -dumpmachine)"
305 local version="$("$CC" $CFLAGS -dumpversion)"
306 local cpuarch="${target%%-*}"
307
308 # get CC; strip version; strip gcc and add - suffix
309 local prefix="${CC##*/}"; prefix="${prefix%-$version}"; prefix="${prefix%-*}-"
310 local config="${0%/scripts/*}/.config"
311
312 # if no target specified, print choice list and exit
313 if [ -z "$mktarget" ]; then
314 # prepare metadata
315 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
316 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
317 fi
318
319 local mktargets=$(
320 sed -ne "
321 /^Target: / { h };
322 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
323 " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
324 )
325
326 for mktarget in $mktargets; do
327 case "$mktarget" in */*)
328 mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
329 esac
330 done
331
332 if [ -n "$mktargets" ]; then
333 echo "Available targets:" >&2
334 echo $mktargets >&2
335 else
336 echo -e "Could not find a suitable OpenWrt target for " >&2
337 echo -e "CPU architecture '$cpuarch' - you need to " >&2
338 echo -e "define one first!" >&2
339 fi
340 return 1
341 fi
342
343 # bail out if there is a .config already
344 if [ -f "$config" ]; then
345 if [ "$OVERWRITE_CONFIG" == "" ]; then
346 echo "There already is a .config file, refusing to overwrite!" >&2
347 return 1
348 else
349 echo "There already is a .config file, trying to overwrite!"
350 fi
351 fi
352
353 case "$mktarget" in */*)
354 mksubtarget="${mktarget#*/}"
355 mktarget="${mktarget%/*}"
356 ;; esac
357
358 if [ ! -f "$config" ]; then
359 touch "$config"
360 fi
361
362 echo "CONFIG_TARGET_${mktarget}=y" >> "$config"
363
364 if [ -n "$mksubtarget" ]; then
365 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
366 fi
367
368 if test_feature "softfloat"; then
369 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
370 else
371 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
372 fi
373
374 if test_feature "ipv6"; then
375 echo "CONFIG_IPV6=y" >> "$config"
376 else
377 echo "# CONFIG_IPV6 is not set" >> "$config"
378 fi
379
380 if test_feature "locale"; then
381 echo "CONFIG_BUILD_NLS=y" >> "$config"
382 else
383 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
384 fi
385
386 echo "CONFIG_DEVEL=y" >> "$config"
387 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
388 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
389 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
390 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
391
392 if [ -f "$config" ]; then
393 sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL/d' "$config"
394 sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC/d' "$config"
395 fi
396
397 if [ "$LIBC_TYPE" == glibc ]; then
398 echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC=y" >> "$config"
399 elif [ "$LIBC_TYPE" == musl ]; then
400 echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL=y" >> "$config"
401 else
402 echo "Can't detect LIBC type. Aborting!" >&2
403 return 1
404 fi
405
406 if [ -n "$GCC_VERSION" ]; then
407 echo "CONFIG_EXTERNAL_GCC_VERSION=\"$GCC_VERSION\"" >> "$config"
408 else
409 echo "Can't detect GCC version. Aborting!" >&2
410 return 1
411 fi
412
413 local lib
414 for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP ATOMIC QUADMATH ASAN TSAN LSAN UBSAN; do
415 local file
416 local spec=""
417 local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
418 for file in $(find_libs "$lib"); do
419 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
420 done
421 if [ -n "$spec" ]; then
422 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
423 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
424 else
425 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
426 fi
427 done
428
429 local bin
430 for bin in LDD LDCONFIG; do
431 local file
432 local spec=""
433 local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
434 for file in $(find_bins "$bin"); do
435 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
436 done
437 if [ -n "$spec" ]; then
438 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
439 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
440 else
441 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
442 fi
443 done
444
445 # inflate
446 make -C "${0%/scripts/*}" defconfig
447 return 0
448 }
449
450
451 probe_cc() {
452 if [ -z "$CC" ]; then
453 local bin
454 for bin in "bin" "usr/bin" "usr/local/bin"; do
455 local cmd
456 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
457 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
458 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
459 return 0
460 fi
461 done
462 done
463 return 1
464 fi
465 return 0
466 }
467
468 probe_cxx() {
469 if [ -z "$CXX" ]; then
470 local bin
471 for bin in "bin" "usr/bin" "usr/local/bin"; do
472 local cmd
473 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
474 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
475 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
476 return 0
477 fi
478 done
479 done
480 return 1
481 fi
482 return 0
483 }
484
485 probe_cpp() {
486 if [ -z "$CPP" ]; then
487 local bin
488 for bin in "bin" "usr/bin" "usr/local/bin"; do
489 local cmd
490 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
491 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
492 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
493 return 0
494 fi
495 done
496 done
497 return 1
498 fi
499 return 0
500 }
501
502 probe_libc() {
503 if [ -f $TOOLCHAIN/info.mk ]; then
504 LIBC_TYPE=$(grep LIBC_TYPE $TOOLCHAIN/info.mk | sed 's/LIBC_TYPE=//')
505 return 0
506 fi
507
508 echo "Warning! Can't find info.mk, trying to detect with alternative way."
509
510 if [ -z "$LIBC_TYPE" ]; then
511 if test_uclibc; then
512 LIBC_TYPE="uclibc"
513 else
514 LIBC_TYPE="glibc"
515 fi
516 fi
517 return 0
518 }
519
520
521 while [ -n "$1" ]; do
522 arg="$1"; shift
523 case "$arg" in
524 --toolchain)
525 [ -d "$1" ] || {
526 echo "Toolchain directory '$1' does not exist." >&2
527 exit 1
528 }
529 TOOLCHAIN="$(cd "$1"; pwd)"; shift
530 ;;
531
532 --cflags)
533 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
534 ;;
535
536 --print-libc)
537 if probe_cc; then
538 probe_libc
539 echo "$LIBC_TYPE"
540 exit 0
541 fi
542 echo "No C compiler found in '$TOOLCHAIN'." >&2
543 exit 1
544 ;;
545
546 --print-target)
547 if probe_cc; then
548 exec "$CC" $CFLAGS -dumpmachine
549 fi
550 echo "No C compiler found in '$TOOLCHAIN'." >&2
551 exit 1
552 ;;
553
554 --print-bin)
555 if [ -z "$1" ]; then
556 echo "Available programs:" >&2
557 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
558 exit 1
559 fi
560
561 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
562 exit 0
563 ;;
564
565 --print-libs)
566 if [ -z "$1" ]; then
567 echo "Available libraries:" >&2
568 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
569 exit 1
570 fi
571
572 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
573 exit 0
574 ;;
575
576 --test)
577 test_feature "$1"
578 exit $?
579 ;;
580
581 --wrap)
582 [ -n "$1" ] || exec "$0" --help
583 wrap_bins "$1"
584 exit $?
585 ;;
586
587 --overwrite-config)
588 OVERWRITE_CONFIG=y
589 ;;
590
591 --config)
592 if probe_cc; then
593 probe_libc
594 find_gcc_version
595 print_config "$1"
596 exit $?
597 fi
598 echo "No C compiler found in '$TOOLCHAIN'." >&2
599 exit 1
600 ;;
601
602 -h|--help)
603 me="$(basename "$0")"
604 echo -e "\nUsage:\n" >&2
605 echo -e " $me --toolchain {directory} --print-libc" >&2
606 echo -e " Print the libc implementation and exit.\n" >&2
607 echo -e " $me --toolchain {directory} --print-target" >&2
608 echo -e " Print the GNU target name and exit.\n" >&2
609 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
610 echo -e " Print executables belonging to given program," >&2
611 echo -e " omit program argument to get a list of names.\n" >&2
612 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
613 echo -e " Print shared objects belonging to given library," >&2
614 echo -e " omit library argument to get a list of names.\n" >&2
615 echo -e " $me --toolchain {directory} --test {feature}" >&2
616 echo -e " Test given feature, exit code indicates success." >&2
617 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
618 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
619 echo -e " 'threads'.\n" >&2
620 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
621 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
622 echo -e " linker, assembler and other key executables in " >&2
623 echo -e " the directory given with --wrap.\n" >&2
624 echo -e " $me --toolchain {directory} --config {target}" >&2
625 echo -e " Analyze the given toolchain and print a suitable" >&2
626 echo -e " .config for the given target. Omit target " >&2
627 echo -e " argument to get a list of names.\n" >&2
628 echo -e " $me --help" >&2
629 echo -e " Display this help text and exit.\n\n" >&2
630 echo -e " Most commands also take a --cflags parameter which " >&2
631 echo -e " is used to specify C flags to be passed to the " >&2
632 echo -e " cross compiler when performing tests." >&2
633 echo -e " This parameter may be repeated multiple times." >&2
634 echo -e " Use --overwrite-config before --config to overwrite" >&2
635 echo -e " an already present config with the required changes.">&2
636 exit 1
637 ;;
638
639 *)
640 echo "Unknown argument '$arg'" >&2
641 exec $0 --help
642 ;;
643 esac
644 done
645
646 exec $0 --help