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
55 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
58 int main(int argc, char **argv)
60 printf("Hello, world!\n");
67 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
74 cout << "Hello, world!" << endl;
81 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
82 int main(int argc, char **argv)
86 double c = (a + b) / (a * b);
93 local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
94 if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
96 for lib in "${sysroot:-$TOOLCHAIN}"/{lib,usr/lib,usr/local/lib}/ld*-uClibc*.so*; do
97 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
106 local feature="$1"; shift
108 # find compilers, libc type
113 # common toolchain feature tests
115 c) test_c; return $? ;;
116 c++) test_cxx; return $? ;;
117 soft*) test_softfloat; return $? ;;
120 # assume eglibc/glibc supports all libc features
121 if [ "$LIBC_TYPE" != "uclibc" ]; then
125 # uclibc feature tests
127 local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
128 for inc in "include" "usr/include" "usr/local/include"; do
129 local conf="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
130 if [ -f "$conf" ]; then
132 lfs) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?;;
133 ipv6) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?;;
134 rpc) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?;;
135 locale) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?;;
136 wchar) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?;;
137 threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
147 local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
149 if [ -n "$spec" ] && probe_cpp; then
152 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
153 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
155 if [ -d "$libdir" ]; then
156 libdirs="$libdirs $(cd "$libdir"; pwd)/"
161 for pattern in $(eval echo $spec); do
162 find $libdirs -name "$pattern.so*" | sort -u
172 local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
174 if [ -n "$spec" ] && probe_cpp; then
175 local sysroot="$("$CPP" -print-sysroot)"
179 echo "${sysroot:-$TOOLCHAIN}/bin";
180 echo "${sysroot:-$TOOLCHAIN}/usr/bin";
181 echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
182 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
183 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
185 if [ -d "$bindir" ]; then
186 bindirs="$bindirs $(cd "$bindir"; pwd)/"
191 for pattern in $(eval echo $spec); do
192 find $bindirs -name "$pattern" | sort -u
206 echo '#!/bin/sh' > "$out"
207 echo 'for arg in "$@"; do' >> "$out"
208 echo ' case "$arg" in -l*|-L*|-shared|-static)' >> "$out"
209 echo -n ' exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
210 echo -n '-idirafter "$STAGING_DIR/usr/include" ' >> "$out"
211 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
212 echo '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;' >> "$out"
213 echo ' esac' >> "$out"
214 echo 'done' >> "$out"
215 echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
216 echo '-idirafter "$STAGING_DIR/usr/include"} "$@"' >> "$out"
225 echo '#!/bin/sh' > "$out"
226 echo -n 'exec "'"$bin"'" ${STAGING_DIR:+' >> "$out"
227 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
228 echo '-rpath-link "$STAGING_DIR/usr/lib"} "$@"' >> "$out"
237 echo '#!/bin/sh' > "$out"
238 echo 'exec "'"$bin"'" "$@"' >> "$out"
245 mkdir -p "$1" || return 1
248 for cmd in "${CC%-*}-"*; do
249 if [ -x "$cmd" ]; then
250 local out="$1/${cmd##*/}"
253 if [ -x "$out" ] && ! grep -q STAGING_DIR "$out"; then
255 bin='$(dirname "$0")/'"${out##*/}"'.bin'
259 *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
260 wrap_bin_cc "$out" "$bin"
263 wrap_bin_ld "$out" "$bin"
266 wrap_bin_other "$out" "$bin"
283 local target="$("$CC" $CFLAGS -dumpmachine)"
284 local cpuarch="${target%%-*}"
285 local prefix="${CC##*/}"; prefix="${prefix%-*}-"
286 local config="${0%/scripts/*}/.config"
288 # if no target specified, print choice list and exit
289 if [ -z "$mktarget" ]; then
291 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
292 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
298 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
299 " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
302 for mktarget in $mktargets; do
303 case "$mktarget" in */*)
304 mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
308 if [ -n "$mktargets" ]; then
309 echo "Available targets:" >&2
312 echo -e "Could not find a suitable OpenWrt target for " >&2
313 echo -e "CPU architecture '$cpuarch' - you need to " >&2
314 echo -e "define one first!" >&2
319 # bail out if there is a .config already
320 if [ -f "${0%/scripts/*}/.config" ]; then
321 echo "There already is a .config file, refusing to overwrite!" >&2
325 case "$mktarget" in */*)
326 mksubtarget="${mktarget#*/}"
327 mktarget="${mktarget%/*}"
331 echo "CONFIG_TARGET_${mktarget}=y" > "$config"
333 if [ -n "$mksubtarget" ]; then
334 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
337 if test_feature "softfloat"; then
338 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
340 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
343 if test_feature "ipv6"; then
344 echo "CONFIG_IPV6=y" >> "$config"
346 echo "# CONFIG_IPV6 is not set" >> "$config"
349 if test_feature "locale"; then
350 echo "CONFIG_BUILD_NLS=y" >> "$config"
352 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
355 echo "CONFIG_DEVEL=y" >> "$config"
356 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
357 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
358 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
359 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
361 if [ "$LIBC_TYPE" != glibc ]; then
362 echo "CONFIG_TOOLCHAIN_LIBC=\"$LIBC_TYPE\"" >> "$config"
366 for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP; do
369 local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
370 for file in $(find_libs "$lib"); do
371 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
373 if [ -n "$spec" ]; then
374 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
375 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
377 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
382 for bin in LDD LDCONFIG; do
385 local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
386 for file in $(find_bins "$bin"); do
387 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
389 if [ -n "$spec" ]; then
390 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
391 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
393 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
398 make -C "${0%/scripts/*}" defconfig
404 if [ -z "$CC" ]; then
406 for bin in "bin" "usr/bin" "usr/local/bin"; do
408 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
409 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
410 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
421 if [ -z "$CXX" ]; then
423 for bin in "bin" "usr/bin" "usr/local/bin"; do
425 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
426 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
427 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
438 if [ -z "$CPP" ]; then
440 for bin in "bin" "usr/bin" "usr/local/bin"; do
442 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
443 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
444 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
455 if [ -z "$LIBC_TYPE" ]; then
466 while [ -n "$1" ]; do
471 echo "Toolchain directory '$1' does not exist." >&2
474 TOOLCHAIN="$(cd "$1"; pwd)"; shift
478 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
487 echo "No C compiler found in '$TOOLCHAIN'." >&2
493 exec "$CC" $CFLAGS -dumpmachine
495 echo "No C compiler found in '$TOOLCHAIN'." >&2
501 echo "Available programs:" >&2
502 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
506 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
512 echo "Available libraries:" >&2
513 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
517 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
527 [ -n "$1" ] || exec "$0" --help
537 echo "No C compiler found in '$TOOLCHAIN'." >&2
542 me="$(basename "$0")"
543 echo -e "\nUsage:\n" >&2
544 echo -e " $me --toolchain {directory} --print-libc" >&2
545 echo -e " Print the libc implementation and exit.\n" >&2
546 echo -e " $me --toolchain {directory} --print-target" >&2
547 echo -e " Print the GNU target name and exit.\n" >&2
548 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
549 echo -e " Print executables belonging to given program," >&2
550 echo -e " omit program argument to get a list of names.\n" >&2
551 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
552 echo -e " Print shared objects belonging to given library," >&2
553 echo -e " omit library argument to get a list of names.\n" >&2
554 echo -e " $me --toolchain {directory} --test {feature}" >&2
555 echo -e " Test given feature, exit code indicates success." >&2
556 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
557 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
558 echo -e " 'threads'.\n" >&2
559 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
560 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
561 echo -e " linker, assembler and other key executables in " >&2
562 echo -e " the directory given with --wrap.\n" >&2
563 echo -e " $me --toolchain {directory} --config {target}" >&2
564 echo -e " Analyze the given toolchain and print a suitable" >&2
565 echo -e " .config for the given target. Omit target " >&2
566 echo -e " argument to get a list of names.\n" >&2
567 echo -e " $me --help" >&2
568 echo -e " Display this help text and exit.\n\n" >&2
569 echo -e " Most commands also take a --cflags parameter which " >&2
570 echo -e " is used to specify C flags to be passed to the " >&2
571 echo -e " cross compiler when performing tests." >&2
572 echo -e " This paremter may be repeated multiple times." >&2
577 echo "Unknown argument '$arg'" >&2