2 # Script to copy a toolchain from given source to given
3 # destination directory.
17 c: ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
19 pthread: libpthread-* libpthread
36 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
39 int main(int argc, char **argv)
41 printf("Hello, world!\n");
48 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
55 cout << "Hello, world!" << endl;
62 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
63 int main(int argc, char **argv)
67 double c = (a + b) / (a * b);
74 local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
75 if [ -d "$sysroot" ]; then
77 for lib in "$sysroot"/{lib,usr/lib,usr/local/lib}/ld-uClibc*.so*; do
78 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
87 local feature="$1"; shift
89 # find compilers, libc type
94 # common toolchain feature tests
96 c) test_c; return $? ;;
97 c++) test_cxx; return $? ;;
98 soft*) test_softfloat; return $? ;;
101 # assume eglibc/glibc supports all libc features
102 if [ "$LIBC_TYPE" != "uclibc" ]; then
106 # uclibc feature tests
108 local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
109 for inc in "include" "usr/include" "usr/local/include"; do
110 local conf="$sysroot/$inc/bits/uClibc_config.h"
111 if [ -f "$conf" ]; then
113 lfs) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?;;
114 ipv6) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?;;
115 rpc) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?;;
116 locale) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?;;
117 wchar) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?;;
118 threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
128 local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##p")"
130 if [ -n "$spec" ] && probe_cpp; then
133 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
134 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
136 if [ -d "$libdir" ]; then
137 libdirs="$libdirs $(cd "$libdir"; pwd)/"
142 for pattern in $(eval echo $spec); do
143 find $libdirs -name "$pattern.so*" | sort -u
153 local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##p")"
155 if [ -n "$spec" ] && probe_cpp; then
156 local sysroot="$("$CPP" -print-sysroot)"
161 echo "$sysroot/usr/bin";
162 echo "$sysroot/usr/local/bin";
163 echo "$TOOLCHAIN/bin";
164 echo "$TOOLCHAIN/usr/bin";
165 echo "$TOOLCHAIN/usr/local/bin";
166 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
167 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
169 if [ -d "$bindir" ]; then
170 bindirs="$bindirs $(cd "$bindir"; pwd)/"
175 for pattern in $(eval echo $spec); do
176 find $bindirs -name "$pattern" | sort -u
188 mkdir -p "$1" || return 1
191 for cmd in "${CC%-*}-"*; do
192 if [ -x "$cmd" ]; then
193 local out="$1/${cmd##*/}"
195 echo '#!/bin/sh' > "$out"
197 *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
198 echo -n 'exec "'"$cmd"'" '"$CFLAGS"' ' >> "$out"
199 echo -n '${STAGING_DIR:+-idirafter ' >> "$out"
200 echo -n '"$STAGING_DIR/usr/include" ' >> "$out"
201 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
202 echo -n '-Wl,-rpath-link,' >> "$out"
203 echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
206 echo -n 'exec "'"$cmd"'" ${STAGING_DIR:+' >> "$out"
207 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
208 echo -n '-rpath-link ' >> "$out"
209 echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
212 echo "exec '$cmd' \"\$@\"" >> "$out"
227 if [ -z "$CC" ]; then
229 for bin in "bin" "usr/bin" "usr/local/bin"; do
231 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
232 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
233 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
244 if [ -z "$CXX" ]; then
246 for bin in "bin" "usr/bin" "usr/local/bin"; do
248 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
249 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
250 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
261 if [ -z "$CPP" ]; then
263 for bin in "bin" "usr/bin" "usr/local/bin"; do
265 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
266 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
267 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
278 if [ -z "$LIBC_TYPE" ]; then
289 while [ -n "$1" ]; do
294 echo "No library given, specify one of:"$(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&1
301 echo "No binary given, specify one of:"$(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&1
309 echo "Toolchain directory '$1' does not exist." >&2
312 TOOLCHAIN="$(cd "$1"; pwd)"; shift
316 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
325 echo "No C compiler found in '$TOOLCHAIN'." >&2
335 echo "No C compiler found in '$TOOLCHAIN'." >&2
341 echo "Available programs:" >&2
342 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
346 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
352 echo "Available libraries:" >&2
353 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
357 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
367 [ -n "$1" ] || exec "$0" --help
373 me="$(basename "$0")"
374 echo -e "\nUsage:\n" >&2
375 echo -e " $me --toolchain {directory} --print-libc" >&2
376 echo -e " Print the libc implementation and exit.\n" >&2
377 echo -e " $me --toolchain {directory} --print-target" >&2
378 echo -e " Print the GNU target name and exit.\n" >&2
379 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
380 echo -e " Print executables belonging to given program," >&2
381 echo -e " omit program argument to get a list of names.\n" >&2
382 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
383 echo -e " Print shared objects belonging to given library," >&2
384 echo -e " omit library argument to get a list of names.\n" >&2
385 echo -e " $me --toolchain {directory} --test {feature}" >&2
386 echo -e " Test given feature, exit code indicates success." >&2
387 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
388 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
389 echo -e " 'threads'.\n" >&2
390 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
391 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
392 echo -e " linker, assembler and other key executables in " >&2
393 echo -e " the directory given with --wrap.\n" >&2
394 echo -e " $me --help" >&2
395 echo -e " Display this help text and exit.\n\n" >&2
396 echo -e " Most commands also take a --cflags parameter which " >&2
397 echo -e " is used to specify C flags to be passed to the " >&2
398 echo -e " cross compiler when performing tests." >&2
399 echo -e " This paremter may be repeated multiple times." >&2
404 echo "Unknown argument '$arg'" >&2