3 # Script for various external toolchain tasks, refer to
4 # the --help output for more information.
6 # Copyright (C) 2012 Jo-Philipp Wich <jo@mein.io>
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.
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.
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
35 c: ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
37 pthread: libpthread-* libpthread
39 thread_db: libthread-db
63 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
66 int main(int argc, char **argv)
68 printf("Hello, world!\n");
75 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
82 cout << "Hello, world!" << endl;
89 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
90 int main(int argc, char **argv)
94 double c = (a + b) / (a * b);
101 local sysroot
="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
102 if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
104 for lib
in "${sysroot:-$TOOLCHAIN}"/{lib
,usr
/lib
,usr
/local
/lib
}/ld
*-uClibc*.so
*; do
105 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
114 local feature
="$1"; shift
116 # find compilers, libc type
121 # common toolchain feature tests
123 c
) test_c
; return $?
;;
124 c
++) test_cxx
; return $?
;;
125 soft
*) test_softfloat
; return $?
;;
128 # assume eglibc/glibc supports all libc features
129 if [ "$LIBC_TYPE" != "uclibc" ]; then
133 # uclibc feature tests
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
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 $?
;;
155 local spec
="$(echo "$LIB_SPECS" | sed -ne "s
#^[[:space:]]*$1:##ip")"
157 if [ -n "$spec" ] && probe_cpp
; then
160 "$CPP" $CFLAGS -v -x c
/dev
/null
2>&1 | \
161 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
163 if [ -d "$libdir" ]; then
164 libdirs
="$libdirs $(cd "$libdir"; pwd)/"
169 for pattern
in $
(eval echo $spec); do
170 find $libdirs -name "$pattern.so*" |
sort -u
180 local spec
="$(echo "$BIN_SPECS" | sed -ne "s
#^[[:space:]]*$1:##ip")"
182 if [ -n "$spec" ] && probe_cpp
; then
183 local sysroot
="$("$CPP" -print-sysroot)"
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'
193 if [ -d "$bindir" ]; then
194 bindirs
="$bindirs $(cd "$bindir"; pwd)/"
199 for pattern
in $
(eval echo $spec); do
200 find $bindirs -name "$pattern" |
sort -u
210 if [ -f $TOOLCHAIN/info.mk
]; then
211 GCC_VERSION
=$
(grep GCC_VERSION
$TOOLCHAIN/info.mk |
sed 's/GCC_VERSION=//')
215 echo "Warning! Can't find info.mk, trying to detect with alternative way."
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-//')
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"
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"
258 echo '#!/bin/sh' > "$out"
259 echo 'exec "'"$bin"'" "$@"' >> "$out"
266 mkdir
-p "$1" ||
return 1
269 for cmd
in "${CC%-*}-"*; do
270 if [ -x "$cmd" ]; then
271 local out
="$1/${cmd##*/}"
274 if [ -x "$out" ] && ! grep -q STAGING_DIR
"$out"; then
276 bin
='$(dirname "$0")/'"${out##*/}"'.bin'
280 *-*cc|
*-*cc-
*|
*-*++|
*-*++-*|
*-cpp)
281 wrap_bin_cc
"$out" "$bin"
284 wrap_bin_ld
"$out" "$bin"
287 wrap_bin_other
"$out" "$bin"
304 local target
="$("$CC" $CFLAGS -dumpmachine)"
305 local version
="$("$CC" $CFLAGS -dumpversion)"
306 local cpuarch
="${target%%-*}"
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"
312 # if no target specified, print choice list and exit
313 if [ -z "$mktarget" ]; then
315 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
316 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
322 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
323 " "${0%/scripts/*}/tmp/.targetinfo" |
sort -u
326 for mktarget
in $mktargets; do
327 case "$mktarget" in */*)
328 mktargets
=$
(echo "$mktargets" |
sed -e "/^${mktarget%/*}\$/d")
332 if [ -n "$mktargets" ]; then
333 echo "Available targets:" >&2
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
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
349 echo "There already is a .config file, trying to overwrite!"
353 case "$mktarget" in */*)
354 mksubtarget
="${mktarget#*/}"
355 mktarget
="${mktarget%/*}"
358 if [ ! -f "$config" ]; then
362 echo "CONFIG_TARGET_${mktarget}=y" >> "$config"
364 if [ -n "$mksubtarget" ]; then
365 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
368 if test_feature
"softfloat"; then
369 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
371 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
374 if test_feature
"ipv6"; then
375 echo "CONFIG_IPV6=y" >> "$config"
377 echo "# CONFIG_IPV6 is not set" >> "$config"
380 if test_feature
"locale"; then
381 echo "CONFIG_BUILD_NLS=y" >> "$config"
383 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
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"
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"
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"
402 echo "Can't detect LIBC type. Aborting!" >&2
406 if [ -n "$GCC_VERSION" ]; then
407 echo "CONFIG_EXTERNAL_GCC_VERSION=\"$GCC_VERSION\"" >> "$config"
409 echo "Can't detect GCC version. Aborting!" >&2
414 for lib
in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP ATOMIC QUADMATH ASAN TSAN LSAN UBSAN
; do
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#.#")"
421 if [ -n "$spec" ]; then
422 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
423 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
425 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
430 for bin
in LDD LDCONFIG
; do
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#.#")"
437 if [ -n "$spec" ]; then
438 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
439 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
441 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
446 make -C "${0%/scripts/*}" defconfig
452 if [ -z "$CC" ]; then
454 for bin
in "bin" "usr/bin" "usr/local/bin"; do
456 for cmd
in "$TOOLCHAIN/$bin/"*-*cc
*; do
457 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
458 CC
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
469 if [ -z "$CXX" ]; then
471 for bin
in "bin" "usr/bin" "usr/local/bin"; do
473 for cmd
in "$TOOLCHAIN/$bin/"*-*++*; do
474 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
475 CXX
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
486 if [ -z "$CPP" ]; then
488 for bin
in "bin" "usr/bin" "usr/local/bin"; do
490 for cmd
in "$TOOLCHAIN/$bin/"*-cpp*; do
491 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
492 CPP
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
503 if [ -f $TOOLCHAIN/info.mk
]; then
504 LIBC_TYPE
=$
(grep LIBC_TYPE
$TOOLCHAIN/info.mk |
sed 's/LIBC_TYPE=//')
508 echo "Warning! Can't find info.mk, trying to detect with alternative way."
510 if [ -z "$LIBC_TYPE" ]; then
521 while [ -n "$1" ]; do
526 echo "Toolchain directory '$1' does not exist." >&2
529 TOOLCHAIN
="$(cd "$1"; pwd)"; shift
533 CFLAGS
="${CFLAGS:+$CFLAGS }$1"; shift
542 echo "No C compiler found in '$TOOLCHAIN'." >&2
548 exec "$CC" $CFLAGS -dumpmachine
550 echo "No C compiler found in '$TOOLCHAIN'." >&2
556 echo "Available programs:" >&2
557 echo $
(echo "$BIN_SPECS" |
sed -ne 's#:.*$##p') >&2
561 find_bins
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-bin
567 echo "Available libraries:" >&2
568 echo $
(echo "$LIB_SPECS" |
sed -ne 's#:.*$##p') >&2
572 find_libs
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-libs
582 [ -n "$1" ] ||
exec "$0" --help
598 echo "No C compiler found in '$TOOLCHAIN'." >&2
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
640 echo "Unknown argument '$arg'" >&2