3 # Script for various external toolchain tasks, refer to
4 # the --help output for more information.
6 # Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
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
53 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
56 int main(int argc, char **argv)
58 printf("Hello, world!\n");
65 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
72 cout << "Hello, world!" << endl;
79 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
80 int main(int argc, char **argv)
84 double c = (a + b) / (a * b);
91 local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
92 if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
94 for lib in "${sysroot:-$TOOLCHAIN}"/{lib,usr/lib,usr/local/lib}/ld-uClibc*.so*; do
95 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
104 local feature="$1"; shift
106 # find compilers, libc type
111 # common toolchain feature tests
113 c) test_c; return $? ;;
114 c++) test_cxx; return $? ;;
115 soft*) test_softfloat; return $? ;;
118 # assume eglibc/glibc supports all libc features
119 if [ "$LIBC_TYPE" != "uclibc" ]; then
123 # uclibc feature tests
125 local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
126 for inc in "include" "usr/include" "usr/local/include"; do
127 local conf="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
128 if [ -f "$conf" ]; then
130 lfs) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?;;
131 ipv6) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?;;
132 rpc) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?;;
133 locale) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?;;
134 wchar) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?;;
135 threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
145 local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
147 if [ -n "$spec" ] && probe_cpp; then
150 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
151 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
153 if [ -d "$libdir" ]; then
154 libdirs="$libdirs $(cd "$libdir"; pwd)/"
159 for pattern in $(eval echo $spec); do
160 find $libdirs -name "$pattern.so*" | sort -u
170 local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
172 if [ -n "$spec" ] && probe_cpp; then
173 local sysroot="$("$CPP" -print-sysroot)"
177 echo "${sysroot:-$TOOLCHAIN}/bin";
178 echo "${sysroot:-$TOOLCHAIN}/usr/bin";
179 echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
180 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
181 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
183 if [ -d "$bindir" ]; then
184 bindirs="$bindirs $(cd "$bindir"; pwd)/"
189 for pattern in $(eval echo $spec); do
190 find $bindirs -name "$pattern" | sort -u
204 echo '#!/bin/sh' > "$out"
205 echo 'for arg in "$@"; do' >> "$out"
206 echo ' case "$arg" in -l*|-L*|-shared|-static)' >> "$out"
207 echo -n ' exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
208 echo -n '-idirafter "$STAGING_DIR/usr/include" ' >> "$out"
209 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
210 echo '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;' >> "$out"
211 echo ' esac' >> "$out"
212 echo 'done' >> "$out"
213 echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
214 echo '-idirafter "$STAGING_DIR/usr/include"} "$@"' >> "$out"
223 echo '#!/bin/sh' > "$out"
224 echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
225 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
226 echo '-rpath-link "$STAGING_DIR/usr/lib"} "$@"' >> "$out"
235 echo '#!/bin/sh' > "$out"
236 echo 'exec "'"$bin"'" "$@"' >> "$out"
243 mkdir -p "$1" || return 1
246 for cmd in "${CC%-*}-"*; do
247 if [ -x "$cmd" ]; then
248 local out="$1/${cmd##*/}"
251 if [ -x "$out" ] && ! grep -q STAGING_DIR "$out"; then
253 bin='$(dirname "$0")/'"${out##*/}"'.bin'
257 *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
258 wrap_bin_cc "$out" "$bin"
261 wrap_bin_ld "$out" "$bin"
264 wrap_bin_other "$out" "$bin"
281 local target="$("$CC" $CFLAGS -dumpmachine)"
282 local cpuarch="${target%%-*}"
283 local prefix="${CC##*/}"; prefix="${prefix%-*}-"
284 local config="${0%/scripts/*}/.config"
286 # if no target specified, print choice list and exit
287 if [ -z "$mktarget" ]; then
289 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
290 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
296 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
297 " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
300 for mktarget in $mktargets; do
301 case "$mktarget" in */*)
302 mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
306 if [ -n "$mktargets" ]; then
307 echo "Available targets:" >&2
310 echo -e "Could not find a suitable OpenWrt target for " >&2
311 echo -e "CPU architecture '$cpuarch' - you need to " >&2
312 echo -e "define one first!" >&2
317 # bail out if there is a .config already
318 if [ -f "${0%/scripts/*}/.config" ]; then
319 echo "There already is a .config file, refusing to overwrite!" >&2
323 case "$mktarget" in */*)
324 mksubtarget="${mktarget#*/}"
325 mktarget="${mktarget%/*}"
329 echo "CONFIG_TARGET_${mktarget}=y" > "$config"
331 if [ -n "$mksubtarget" ]; then
332 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
335 if test_feature "softfloat"; then
336 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
338 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
341 if test_feature "ipv6"; then
342 echo "CONFIG_IPV6=y" >> "$config"
344 echo "# CONFIG_IPV6 is not set" >> "$config"
347 if test_feature "locale"; then
348 echo "CONFIG_BUILD_NLS=y" >> "$config"
350 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
353 echo "CONFIG_DEVEL=y" >> "$config"
354 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
355 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
356 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
357 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
360 for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN; do
363 local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
364 for file in $(find_libs "$lib"); do
365 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
367 if [ -n "$spec" ]; then
368 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
369 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
371 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
376 for bin in LDD LDCONFIG; do
379 local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
380 for file in $(find_bins "$bin"); do
381 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
383 if [ -n "$spec" ]; then
384 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
385 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
387 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
392 make -C "${0%/scripts/*}" defconfig
398 if [ -z "$CC" ]; then
400 for bin in "bin" "usr/bin" "usr/local/bin"; do
402 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
403 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
404 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
415 if [ -z "$CXX" ]; then
417 for bin in "bin" "usr/bin" "usr/local/bin"; do
419 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
420 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
421 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
432 if [ -z "$CPP" ]; then
434 for bin in "bin" "usr/bin" "usr/local/bin"; do
436 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
437 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
438 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
449 if [ -z "$LIBC_TYPE" ]; then
460 while [ -n "$1" ]; do
465 echo "Toolchain directory '$1' does not exist." >&2
468 TOOLCHAIN="$(cd "$1"; pwd)"; shift
472 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
481 echo "No C compiler found in '$TOOLCHAIN'." >&2
487 exec "$CC" $CFLAGS -dumpmachine
489 echo "No C compiler found in '$TOOLCHAIN'." >&2
495 echo "Available programs:" >&2
496 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
500 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
506 echo "Available libraries:" >&2
507 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
511 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
521 [ -n "$1" ] || exec "$0" --help
531 echo "No C compiler found in '$TOOLCHAIN'." >&2
536 me="$(basename "$0")"
537 echo -e "\nUsage:\n" >&2
538 echo -e " $me --toolchain {directory} --print-libc" >&2
539 echo -e " Print the libc implementation and exit.\n" >&2
540 echo -e " $me --toolchain {directory} --print-target" >&2
541 echo -e " Print the GNU target name and exit.\n" >&2
542 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
543 echo -e " Print executables belonging to given program," >&2
544 echo -e " omit program argument to get a list of names.\n" >&2
545 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
546 echo -e " Print shared objects belonging to given library," >&2
547 echo -e " omit library argument to get a list of names.\n" >&2
548 echo -e " $me --toolchain {directory} --test {feature}" >&2
549 echo -e " Test given feature, exit code indicates success." >&2
550 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
551 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
552 echo -e " 'threads'.\n" >&2
553 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
554 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
555 echo -e " linker, assembler and other key executables in " >&2
556 echo -e " the directory given with --wrap.\n" >&2
557 echo -e " $me --toolchain {directory} --config {target}" >&2
558 echo -e " Analyze the given toolchain and print a suitable" >&2
559 echo -e " .config for the given target. Omit target " >&2
560 echo -e " argument to get a list of names.\n" >&2
561 echo -e " $me --help" >&2
562 echo -e " Display this help text and exit.\n\n" >&2
563 echo -e " Most commands also take a --cflags parameter which " >&2
564 echo -e " is used to specify C flags to be passed to the " >&2
565 echo -e " cross compiler when performing tests." >&2
566 echo -e " This paremter may be repeated multiple times." >&2
571 echo "Unknown argument '$arg'" >&2