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
202 mkdir
-p "$1" ||
return 1
205 for cmd
in "${CC%-*}-"*; do
206 if [ -x "$cmd" ]; then
207 local out
="$1/${cmd##*/}"
209 echo '#!/bin/sh' > "$out"
211 *-*cc|
*-*cc-
*|
*-*++|
*-*++-*|
*-cpp)
212 echo -n 'exec "'"$cmd"'" '"$CFLAGS"' ' >> "$out"
213 echo -n '${STAGING_DIR:+-idirafter ' >> "$out"
214 echo -n '"$STAGING_DIR/usr/include" ' >> "$out"
215 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
216 echo -n '-Wl,-rpath-link,' >> "$out"
217 echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
220 echo -n 'exec "'"$cmd"'" ${STAGING_DIR:+' >> "$out"
221 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
222 echo -n '-rpath-link ' >> "$out"
223 echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
226 echo "exec '$cmd' \"\$@\"" >> "$out"
244 local target
="$("$CC" $CFLAGS -dumpmachine)"
245 local cpuarch
="${target%%-*}"
246 local prefix
="${CC##*/}"; prefix
="${prefix%-*}-"
247 local config
="${0%/scripts/*}/.config"
249 # if no target specified, print choice list and exit
250 if [ -z "$mktarget" ]; then
252 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
253 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
259 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
260 " "${0%/scripts/*}/tmp/.targetinfo" |
sort -u
263 for mktarget
in $mktargets; do
264 case "$mktarget" in */*)
265 mktargets
=$
(echo "$mktargets" |
sed -e "/^${mktarget%/*}\$/d")
269 if [ -n "$mktargets" ]; then
270 echo "Available targets:" >&2
273 echo -e "Could not find a suitable OpenWrt target for " >&2
274 echo -e "CPU architecture '$cpuarch' - you need to " >&2
275 echo -e "define one first!" >&2
280 # bail out if there is a .config already
281 if [ -f "${0%/scripts/*}/.config" ]; then
282 echo "There already is a .config file, refusing to overwrite!" >&2
286 case "$mktarget" in */*)
287 mksubtarget
="${mktarget#*/}"
288 mktarget
="${mktarget%/*}"
292 echo "CONFIG_TARGET_${mktarget}=y" > "$config"
294 if [ -n "$mksubtarget" ]; then
295 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
298 if test_feature
"softfloat"; then
299 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
301 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
304 if test_feature
"ipv6"; then
305 echo "CONFIG_IPV6=y" >> "$config"
307 echo "# CONFIG_IPV6 is not set" >> "$config"
310 if test_feature
"locale"; then
311 echo "CONFIG_BUILD_NLS=y" >> "$config"
313 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
316 echo "CONFIG_DEVEL=y" >> "$config"
317 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
318 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
319 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
320 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
323 for lib
in C RT PTHREAD GCC STDCPP SSP GFORTRAN
; do
326 local llib
="$(echo "$lib" | sed -e 's#.*#\L&#')"
327 for file in $
(find_libs
"$lib"); do
328 spec
="${spec:+$spec }$(echo "$file" | sed -e "s
#^$TOOLCHAIN#.#")"
330 if [ -n "$spec" ]; then
331 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
332 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
334 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
339 for bin
in LDD LDCONFIG
; do
342 local lbin
="$(echo "$bin" | sed -e 's#.*#\L&#')"
343 for file in $
(find_bins
"$bin"); do
344 spec
="${spec:+$spec }$(echo "$file" | sed -e "s
#^$TOOLCHAIN#.#")"
346 if [ -n "$spec" ]; then
347 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
348 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
350 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
355 make -C "${0%/scripts/*}" defconfig
361 if [ -z "$CC" ]; then
363 for bin
in "bin" "usr/bin" "usr/local/bin"; do
365 for cmd
in "$TOOLCHAIN/$bin/"*-*cc
*; do
366 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
367 CC
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
378 if [ -z "$CXX" ]; then
380 for bin
in "bin" "usr/bin" "usr/local/bin"; do
382 for cmd
in "$TOOLCHAIN/$bin/"*-*++*; do
383 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
384 CXX
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
395 if [ -z "$CPP" ]; then
397 for bin
in "bin" "usr/bin" "usr/local/bin"; do
399 for cmd
in "$TOOLCHAIN/$bin/"*-cpp*; do
400 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
401 CPP
="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
412 if [ -z "$LIBC_TYPE" ]; then
423 while [ -n "$1" ]; do
428 echo "Toolchain directory '$1' does not exist." >&2
431 TOOLCHAIN
="$(cd "$1"; pwd)"; shift
435 CFLAGS
="${CFLAGS:+$CFLAGS }$1"; shift
444 echo "No C compiler found in '$TOOLCHAIN'." >&2
450 exec "$CC" $CFLAGS -dumpmachine
452 echo "No C compiler found in '$TOOLCHAIN'." >&2
458 echo "Available programs:" >&2
459 echo $
(echo "$BIN_SPECS" |
sed -ne 's#:.*$##p') >&2
463 find_bins
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-bin
469 echo "Available libraries:" >&2
470 echo $
(echo "$LIB_SPECS" |
sed -ne 's#:.*$##p') >&2
474 find_libs
"$1" ||
exec "$0" --toolchain "$TOOLCHAIN" --print-libs
484 [ -n "$1" ] ||
exec "$0" --help
494 echo "No C compiler found in '$TOOLCHAIN'." >&2
499 me
="$(basename "$0")"
500 echo -e "\nUsage:\n" >&2
501 echo -e " $me --toolchain {directory} --print-libc" >&2
502 echo -e " Print the libc implementation and exit.\n" >&2
503 echo -e " $me --toolchain {directory} --print-target" >&2
504 echo -e " Print the GNU target name and exit.\n" >&2
505 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
506 echo -e " Print executables belonging to given program," >&2
507 echo -e " omit program argument to get a list of names.\n" >&2
508 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
509 echo -e " Print shared objects belonging to given library," >&2
510 echo -e " omit library argument to get a list of names.\n" >&2
511 echo -e " $me --toolchain {directory} --test {feature}" >&2
512 echo -e " Test given feature, exit code indicates success." >&2
513 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
514 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
515 echo -e " 'threads'.\n" >&2
516 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
517 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
518 echo -e " linker, assembler and other key executables in " >&2
519 echo -e " the directory given with --wrap.\n" >&2
520 echo -e " $me --toolchain {directory} --config {target}" >&2
521 echo -e " Analyze the given toolchain and print a suitable" >&2
522 echo -e " .config for the given target. Omit target " >&2
523 echo -e " argument to get a list of names.\n" >&2
524 echo -e " $me --help" >&2
525 echo -e " Display this help text and exit.\n\n" >&2
526 echo -e " Most commands also take a --cflags parameter which " >&2
527 echo -e " is used to specify C flags to be passed to the " >&2
528 echo -e " cross compiler when performing tests." >&2
529 echo -e " This paremter may be repeated multiple times." >&2
534 echo "Unknown argument '$arg'" >&2