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
34 c: ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
36 pthread: libpthread-* libpthread
38 thread_db: libthread-db
56 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
59 int main(int argc, char **argv)
61 printf("Hello, world!\n");
68 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
75 cout << "Hello, world!" << endl;
82 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
83 int main(int argc, char **argv)
87 double c = (a + b) / (a * b);
94 local sysroot
="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
95 if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
97 for lib
in "${sysroot:-$TOOLCHAIN}"/{lib
,usr
/lib
,usr
/local
/lib
}/ld
*-uClibc*.so
*; do
98 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
107 local feature
="$1"; shift
109 # find compilers, libc type
114 # common toolchain feature tests
116 c
) test_c
; return $?
;;
117 c
++) test_cxx
; return $?
;;
118 soft
*) test_softfloat
; return $?
;;
121 # assume eglibc/glibc supports all libc features
122 if [ "$LIBC_TYPE" != "uclibc" ]; then
126 # uclibc feature tests
128 local sysroot
="$("$CC" "$@
" -muclibc -print-sysroot 2>/dev/null)"
129 for inc
in "include" "usr/include" "usr/local/include"; do
130 local conf
="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
131 if [ -f "$conf" ]; then
133 lfs
) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?
;;
134 ipv6
) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?
;;
135 rpc
) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?
;;
136 locale
) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?
;;
137 wchar
) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?
;;
138 threads
) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?
;;
148 local spec
="$(echo "$LIB_SPECS" | sed -ne "s
#^[[:space:]]*$1:##ip")"
150 if [ -n "$spec" ] && probe_cpp
; then
153 "$CPP" $CFLAGS -v -x c
/dev
/null
2>&1 | \
154 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
156 if [ -d "$libdir" ]; then
157 libdirs
="$libdirs $(cd "$libdir"; pwd)/"
162 for pattern
in $
(eval echo $spec); do
163 find $libdirs -name "$pattern.so*" |
sort -u
173 local spec
="$(echo "$BIN_SPECS" | sed -ne "s
#^[[:space:]]*$1:##ip")"
175 if [ -n "$spec" ] && probe_cpp
; then
176 local sysroot
="$("$CPP" -print-sysroot)"
180 echo "${sysroot:-$TOOLCHAIN}/bin";
181 echo "${sysroot:-$TOOLCHAIN}/usr/bin";
182 echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
183 "$CPP" $CFLAGS -v -x c
/dev
/null
2>&1 | \
184 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
186 if [ -d "$bindir" ]; then
187 bindirs
="$bindirs $(cd "$bindir"; pwd)/"
192 for pattern
in $
(eval echo $spec); do
193 find $bindirs -name "$pattern" |
sort -u
207 echo '#!/bin/sh' > "$out"
208 echo 'for arg in "$@"; do' >> "$out"
209 echo ' case "$arg" in -l*|-L*|-shared|-static)' >> "$out"
210 echo -n ' exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
211 echo -n '-idirafter "$STAGING_DIR/usr/include" ' >> "$out"
212 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
213 echo '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;' >> "$out"
214 echo ' esac' >> "$out"
215 echo 'done' >> "$out"
216 echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
217 echo '-idirafter "$STAGING_DIR/usr/include"} "$@"' >> "$out"
226 echo '#!/bin/sh' > "$out"
227 echo -n 'exec "'"$bin"'" ${STAGING_DIR:+' >> "$out"
228 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
229 echo '-rpath-link "$STAGING_DIR/usr/lib"} "$@"' >> "$out"
238 echo '#!/bin/sh' > "$out"
239 echo 'exec "'"$bin"'" "$@"' >> "$out"
246 mkdir
-p "$1" ||
return 1
249 for cmd
in "${CC%-*}-"*; do
250 if [ -x "$cmd" ]; then
251 local out
="$1/${cmd##*/}"
254 if [ -x "$out" ] && ! grep -q STAGING_DIR
"$out"; then
256 bin
='$(dirname "$0")/'"${out##*/}"'.bin'
260 *-*cc|
*-*cc-
*|
*-*++|
*-*++-*|
*-cpp)
261 wrap_bin_cc
"$out" "$bin"
264 wrap_bin_ld
"$out" "$bin"
267 wrap_bin_other
"$out" "$bin"
284 local target
="$("$CC" $CFLAGS -dumpmachine)"
285 local version
="$("$CC" $CFLAGS -dumpversion)"
286 local cpuarch
="${target%%-*}"
288 # get CC; strip version; strip gcc and add - suffix
289 local prefix
="${CC##*/}"; prefix="${prefix%-$version}"; prefix="${prefix%-*}-"
290 local config
="${0%/scripts/*}/.config"
292 # if no target specified, print choice list and exit
293 if [ -z "$mktarget" ]; then
295 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
296 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
302 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
303 " "${0%/scripts/*}/tmp/.targetinfo" |
sort -u
306 for mktarget
in $mktargets; do
307 case "$mktarget" in */*)
308 mktargets
=$
(echo "$mktargets" |
sed -e "/^${mktarget%/*}\$/d")
312 if [ -n "$mktargets" ]; then
313 echo "Available targets:" >&2
316 echo -e "Could not find a suitable OpenWrt target for " >&2
317 echo -e "CPU architecture '$cpuarch' - you need to " >&2
318 echo -e "define one first!" >&2
323 # bail out if there is a .config already
324 if [ -f "$config" ]; then
325 if [ "$OVERWRITE_CONFIG" == "" ]; then
326 echo "There already is a .config file, refusing to overwrite!" >&2
329 echo "There already is a .config file, trying to overwrite!"
333 case "$mktarget" in */*)
334 mksubtarget
="${mktarget#*/}"
335 mktarget
="${mktarget%/*}"
338 if [ ! -f "$config" ]; then
342 echo "CONFIG_TARGET_${mktarget}=y" >> "$config"
344 if [ -n "$mksubtarget" ]; then
345 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
348 if test_feature
"softfloat"; then
349 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
351 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
354 if test_feature
"ipv6"; then
355 echo "CONFIG_IPV6=y" >> "$config"
357 echo "# CONFIG_IPV6 is not set" >> "$config"
360 if test_feature
"locale"; then
361 echo "CONFIG_BUILD_NLS=y" >> "$config"
363 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
366 echo "CONFIG_DEVEL=y" >> "$config"
367 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
368 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
369 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
370 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
372 if [ "$LIBC_TYPE" != glibc
]; then
373 echo "CONFIG_TOOLCHAIN_LIBC=\"$LIBC_TYPE\"" >> "$config"
377 for lib
in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP
; do
380 local llib
="$(echo "$lib" | sed -e 's#.*#\L&#')"
381 for file in $
(find_libs
"$lib"); do
382 spec
="${spec:+$spec }$(echo "$file" | sed -e "s
#^$TOOLCHAIN#.#")"
384 if [ -n "$spec" ]; then
385 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
386 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
388 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
393 for bin
in LDD LDCONFIG
; do
396 local lbin
="$(echo "$bin" | sed -e 's#.*#\L&#')"
397 for file in $
(find_bins
"$bin"); do
398 spec
="${spec:+$spec }$(echo "$file" | sed -e "s
#^$TOOLCHAIN#.#")"
400 if [ -n "$spec" ]; then
401 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
402 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
404 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
409 make -C "${0%/scripts/*}" defconfig
415 if [ -z "$CC" ]; then
417 for bin
in "bin" "usr/bin" "usr/local/bin"; do
419 for cmd
in "$TOOLCHAIN/$bin/"*-*cc
*; do
420 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
421 CC
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
432 if [ -z "$CXX" ]; then
434 for bin
in "bin" "usr/bin" "usr/local/bin"; do
436 for cmd
in "$TOOLCHAIN/$bin/"*-*++*; do
437 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
438 CXX
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
449 if [ -z "$CPP" ]; then
451 for bin
in "bin" "usr/bin" "usr/local/bin"; do
453 for cmd
in "$TOOLCHAIN/$bin/"*-cpp*; do
454 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
455 CPP
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
466 if [ -z "$LIBC_TYPE" ]; then
477 while [ -n "$1" ]; do
482 echo "Toolchain directory '$1' does not exist." >&2
485 TOOLCHAIN
="$(cd "$1"; pwd)"; shift
489 CFLAGS
="${CFLAGS:+$CFLAGS }$1"; shift
498 echo "No C compiler found in '$TOOLCHAIN'." >&2
504 exec "$CC" $CFLAGS -dumpmachine
506 echo "No C compiler found in '$TOOLCHAIN'." >&2
512 echo "Available programs:" >&2
513 echo $
(echo "$BIN_SPECS" |
sed -ne 's#:.*$##p') >&2
517 find_bins
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-bin
523 echo "Available libraries:" >&2
524 echo $
(echo "$LIB_SPECS" |
sed -ne 's#:.*$##p') >&2
528 find_libs
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-libs
538 [ -n "$1" ] ||
exec "$0" --help
552 echo "No C compiler found in '$TOOLCHAIN'." >&2
557 me
="$(basename "$0")"
558 echo -e "\nUsage:\n" >&2
559 echo -e " $me --toolchain {directory} --print-libc" >&2
560 echo -e " Print the libc implementation and exit.\n" >&2
561 echo -e " $me --toolchain {directory} --print-target" >&2
562 echo -e " Print the GNU target name and exit.\n" >&2
563 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
564 echo -e " Print executables belonging to given program," >&2
565 echo -e " omit program argument to get a list of names.\n" >&2
566 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
567 echo -e " Print shared objects belonging to given library," >&2
568 echo -e " omit library argument to get a list of names.\n" >&2
569 echo -e " $me --toolchain {directory} --test {feature}" >&2
570 echo -e " Test given feature, exit code indicates success." >&2
571 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
572 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
573 echo -e " 'threads'.\n" >&2
574 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
575 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
576 echo -e " linker, assembler and other key executables in " >&2
577 echo -e " the directory given with --wrap.\n" >&2
578 echo -e " $me --toolchain {directory} --config {target}" >&2
579 echo -e " Analyze the given toolchain and print a suitable" >&2
580 echo -e " .config for the given target. Omit target " >&2
581 echo -e " argument to get a list of names.\n" >&2
582 echo -e " $me --help" >&2
583 echo -e " Display this help text and exit.\n\n" >&2
584 echo -e " Most commands also take a --cflags parameter which " >&2
585 echo -e " is used to specify C flags to be passed to the " >&2
586 echo -e " cross compiler when performing tests." >&2
587 echo -e " This parameter may be repeated multiple times." >&2
588 echo -e " Use --overwrite-config before --config to overwrite" >&2
589 echo -e " an already present config with the required changes.">&2
594 echo "Unknown argument '$arg'" >&2