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
57 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
60 int main(int argc, char **argv)
62 printf("Hello, world!\n");
69 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
76 cout << "Hello, world!" << endl;
83 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
84 int main(int argc, char **argv)
88 double c = (a + b) / (a * b);
95 local sysroot
="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
96 if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
98 for lib
in "${sysroot:-$TOOLCHAIN}"/{lib
,usr
/lib
,usr
/local
/lib
}/ld
*-uClibc*.so
*; do
99 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
108 local feature
="$1"; shift
110 # find compilers, libc type
115 # common toolchain feature tests
117 c
) test_c
; return $?
;;
118 c
++) test_cxx
; return $?
;;
119 soft
*) test_softfloat
; return $?
;;
122 # assume eglibc/glibc supports all libc features
123 if [ "$LIBC_TYPE" != "uclibc" ]; then
127 # uclibc feature tests
129 local sysroot
="$("$CC" "$@
" -muclibc -print-sysroot 2>/dev/null)"
130 for inc
in "include" "usr/include" "usr/local/include"; do
131 local conf
="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
132 if [ -f "$conf" ]; then
134 lfs
) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?
;;
135 ipv6
) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?
;;
136 rpc
) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?
;;
137 locale
) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?
;;
138 wchar
) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?
;;
139 threads
) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?
;;
149 local spec
="$(echo "$LIB_SPECS" | sed -ne "s
#^[[:space:]]*$1:##ip")"
151 if [ -n "$spec" ] && probe_cpp
; then
154 "$CPP" $CFLAGS -v -x c
/dev
/null
2>&1 | \
155 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
157 if [ -d "$libdir" ]; then
158 libdirs
="$libdirs $(cd "$libdir"; pwd)/"
163 for pattern
in $
(eval echo $spec); do
164 find $libdirs -name "$pattern.so*" |
sort -u
174 local spec
="$(echo "$BIN_SPECS" | sed -ne "s
#^[[:space:]]*$1:##ip")"
176 if [ -n "$spec" ] && probe_cpp
; then
177 local sysroot
="$("$CPP" -print-sysroot)"
181 echo "${sysroot:-$TOOLCHAIN}/bin";
182 echo "${sysroot:-$TOOLCHAIN}/usr/bin";
183 echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
184 "$CPP" $CFLAGS -v -x c
/dev
/null
2>&1 | \
185 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
187 if [ -d "$bindir" ]; then
188 bindirs
="$bindirs $(cd "$bindir"; pwd)/"
193 for pattern
in $
(eval echo $spec); do
194 find $bindirs -name "$pattern" |
sort -u
204 if [ -f $TOOLCHAIN/info.mk
]; then
205 GCC_VERSION
=$
(grep GCC_VERSION
$TOOLCHAIN/info.mk |
sed 's/GCC_VERSION=//')
209 echo "Warning! Can't find info.mk, trying to detect with alternative way."
211 # Very fragile detection
212 GCC_VERSION
=$
(find $TOOLCHAIN/bin |
grep -oE "gcc-[0-9]+\.[0-9]+\.[0-9]+$" | \
213 head -1 |
sed 's/gcc-//')
221 echo '#!/bin/sh' > "$out"
222 echo 'for arg in "$@"; do' >> "$out"
223 echo ' case "$arg" in -l*|-L*|-shared|-static)' >> "$out"
224 echo -n ' exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
225 echo -n '-idirafter "$STAGING_DIR/usr/include" ' >> "$out"
226 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
227 echo '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;' >> "$out"
228 echo ' esac' >> "$out"
229 echo 'done' >> "$out"
230 echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
231 echo '-idirafter "$STAGING_DIR/usr/include"} "$@"' >> "$out"
240 echo '#!/bin/sh' > "$out"
241 echo -n 'exec "'"$bin"'" ${STAGING_DIR:+' >> "$out"
242 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
243 echo '-rpath-link "$STAGING_DIR/usr/lib"} "$@"' >> "$out"
252 echo '#!/bin/sh' > "$out"
253 echo 'exec "'"$bin"'" "$@"' >> "$out"
260 mkdir
-p "$1" ||
return 1
263 for cmd
in "${CC%-*}-"*; do
264 if [ -x "$cmd" ]; then
265 local out
="$1/${cmd##*/}"
268 if [ -x "$out" ] && ! grep -q STAGING_DIR
"$out"; then
270 bin
='$(dirname "$0")/'"${out##*/}"'.bin'
274 *-*cc|
*-*cc-
*|
*-*++|
*-*++-*|
*-cpp)
275 wrap_bin_cc
"$out" "$bin"
278 wrap_bin_ld
"$out" "$bin"
281 wrap_bin_other
"$out" "$bin"
298 local target
="$("$CC" $CFLAGS -dumpmachine)"
299 local version
="$("$CC" $CFLAGS -dumpversion)"
300 local cpuarch
="${target%%-*}"
302 # get CC; strip version; strip gcc and add - suffix
303 local prefix
="${CC##*/}"; prefix="${prefix%-$version}"; prefix="${prefix%-*}-"
304 local config
="${0%/scripts/*}/.config"
306 # if no target specified, print choice list and exit
307 if [ -z "$mktarget" ]; then
309 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
310 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
316 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
317 " "${0%/scripts/*}/tmp/.targetinfo" |
sort -u
320 for mktarget
in $mktargets; do
321 case "$mktarget" in */*)
322 mktargets
=$
(echo "$mktargets" |
sed -e "/^${mktarget%/*}\$/d")
326 if [ -n "$mktargets" ]; then
327 echo "Available targets:" >&2
330 echo -e "Could not find a suitable OpenWrt target for " >&2
331 echo -e "CPU architecture '$cpuarch' - you need to " >&2
332 echo -e "define one first!" >&2
337 # bail out if there is a .config already
338 if [ -f "$config" ]; then
339 if [ "$OVERWRITE_CONFIG" == "" ]; then
340 echo "There already is a .config file, refusing to overwrite!" >&2
343 echo "There already is a .config file, trying to overwrite!"
347 case "$mktarget" in */*)
348 mksubtarget
="${mktarget#*/}"
349 mktarget
="${mktarget%/*}"
352 if [ ! -f "$config" ]; then
356 echo "CONFIG_TARGET_${mktarget}=y" >> "$config"
358 if [ -n "$mksubtarget" ]; then
359 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
362 if test_feature
"softfloat"; then
363 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
365 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
368 if test_feature
"ipv6"; then
369 echo "CONFIG_IPV6=y" >> "$config"
371 echo "# CONFIG_IPV6 is not set" >> "$config"
374 if test_feature
"locale"; then
375 echo "CONFIG_BUILD_NLS=y" >> "$config"
377 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
380 echo "CONFIG_DEVEL=y" >> "$config"
381 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
382 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
383 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
384 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
386 if [ -f "$config" ]; then
387 sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL/d' "$config"
388 sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC/d' "$config"
391 if [ "$LIBC_TYPE" == glibc
]; then
392 echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC=y" >> "$config"
393 elif [ "$LIBC_TYPE" == musl
]; then
394 echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL=y" >> "$config"
396 echo "Can't detect LIBC type. Aborting!" >&2
400 if [ -n "$GCC_VERSION" ]; then
401 echo "CONFIG_EXTERNAL_GCC_VERSION=\"$GCC_VERSION\"" >> "$config"
403 echo "Can't detect GCC version. Aborting!" >&2
408 for lib
in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP
; do
411 local llib
="$(echo "$lib" | sed -e 's#.*#\L&#')"
412 for file in $
(find_libs
"$lib"); do
413 spec
="${spec:+$spec }$(echo "$file" | sed -e "s
#^$TOOLCHAIN#.#")"
415 if [ -n "$spec" ]; then
416 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
417 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
419 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
424 for bin
in LDD LDCONFIG
; do
427 local lbin
="$(echo "$bin" | sed -e 's#.*#\L&#')"
428 for file in $
(find_bins
"$bin"); do
429 spec
="${spec:+$spec }$(echo "$file" | sed -e "s
#^$TOOLCHAIN#.#")"
431 if [ -n "$spec" ]; then
432 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
433 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
435 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
440 make -C "${0%/scripts/*}" defconfig
446 if [ -z "$CC" ]; then
448 for bin
in "bin" "usr/bin" "usr/local/bin"; do
450 for cmd
in "$TOOLCHAIN/$bin/"*-*cc
*; do
451 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
452 CC
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
463 if [ -z "$CXX" ]; then
465 for bin
in "bin" "usr/bin" "usr/local/bin"; do
467 for cmd
in "$TOOLCHAIN/$bin/"*-*++*; do
468 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
469 CXX
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
480 if [ -z "$CPP" ]; then
482 for bin
in "bin" "usr/bin" "usr/local/bin"; do
484 for cmd
in "$TOOLCHAIN/$bin/"*-cpp*; do
485 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
486 CPP
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
497 if [ -f $TOOLCHAIN/info.mk
]; then
498 LIBC_TYPE
=$
(grep LIBC_TYPE
$TOOLCHAIN/info.mk |
sed 's/LIBC_TYPE=//')
502 echo "Warning! Can't find info.mk, trying to detect with alternative way."
504 if [ -z "$LIBC_TYPE" ]; then
515 while [ -n "$1" ]; do
520 echo "Toolchain directory '$1' does not exist." >&2
523 TOOLCHAIN
="$(cd "$1"; pwd)"; shift
527 CFLAGS
="${CFLAGS:+$CFLAGS }$1"; shift
536 echo "No C compiler found in '$TOOLCHAIN'." >&2
542 exec "$CC" $CFLAGS -dumpmachine
544 echo "No C compiler found in '$TOOLCHAIN'." >&2
550 echo "Available programs:" >&2
551 echo $
(echo "$BIN_SPECS" |
sed -ne 's#:.*$##p') >&2
555 find_bins
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-bin
561 echo "Available libraries:" >&2
562 echo $
(echo "$LIB_SPECS" |
sed -ne 's#:.*$##p') >&2
566 find_libs
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-libs
576 [ -n "$1" ] ||
exec "$0" --help
592 echo "No C compiler found in '$TOOLCHAIN'." >&2
597 me
="$(basename "$0")"
598 echo -e "\nUsage:\n" >&2
599 echo -e " $me --toolchain {directory} --print-libc" >&2
600 echo -e " Print the libc implementation and exit.\n" >&2
601 echo -e " $me --toolchain {directory} --print-target" >&2
602 echo -e " Print the GNU target name and exit.\n" >&2
603 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
604 echo -e " Print executables belonging to given program," >&2
605 echo -e " omit program argument to get a list of names.\n" >&2
606 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
607 echo -e " Print shared objects belonging to given library," >&2
608 echo -e " omit library argument to get a list of names.\n" >&2
609 echo -e " $me --toolchain {directory} --test {feature}" >&2
610 echo -e " Test given feature, exit code indicates success." >&2
611 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
612 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
613 echo -e " 'threads'.\n" >&2
614 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
615 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
616 echo -e " linker, assembler and other key executables in " >&2
617 echo -e " the directory given with --wrap.\n" >&2
618 echo -e " $me --toolchain {directory} --config {target}" >&2
619 echo -e " Analyze the given toolchain and print a suitable" >&2
620 echo -e " .config for the given target. Omit target " >&2
621 echo -e " argument to get a list of names.\n" >&2
622 echo -e " $me --help" >&2
623 echo -e " Display this help text and exit.\n\n" >&2
624 echo -e " Most commands also take a --cflags parameter which " >&2
625 echo -e " is used to specify C flags to be passed to the " >&2
626 echo -e " cross compiler when performing tests." >&2
627 echo -e " This parameter may be repeated multiple times." >&2
628 echo -e " Use --overwrite-config before --config to overwrite" >&2
629 echo -e " an already present config with the required changes.">&2
634 echo "Unknown argument '$arg'" >&2