fbe8639c9c558a4ea8ea74ebccc36be64ecc1a0d
[openwrt/openwrt.git] / scripts / ext-toolchain.sh
1 #!/usr/bin/env bash
2 #
3 # Script for various external toolchain tasks, refer to
4 # the --help output for more information.
5 #
6 # Copyright (C) 2012 Jo-Philipp Wich <jo@mein.io>
7 #
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.
12 #
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.
17 #
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
21
22 CC=""
23 CXX=""
24 CPP=""
25
26 CFLAGS=""
27 TOOLCHAIN="."
28
29 LIBC_TYPE=""
30
31
32 # Library specs
33 LIB_SPECS="
34 c: ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
35 rt: librt-* librt
36 pthread: libpthread-* libpthread
37 stdcpp: libstdc++
38 thread_db: libthread-db
39 gcc: libgcc_s
40 ssp: libssp
41 gfortran: libgfortran
42 "
43
44 # Binary specs
45 BIN_SPECS="
46 ldd: ldd
47 ldconfig: ldconfig
48 gdb: gdb
49 gdbserver: gdbserver
50 "
51
52
53 test_c() {
54 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
55 #include <stdio.h>
56
57 int main(int argc, char **argv)
58 {
59 printf("Hello, world!\n");
60 return 0;
61 }
62 EOT
63 }
64
65 test_cxx() {
66 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
67 #include <iostream>
68
69 using namespace std;
70
71 int main()
72 {
73 cout << "Hello, world!" << endl;
74 return 0;
75 }
76 EOT
77 }
78
79 test_softfloat() {
80 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
81 int main(int argc, char **argv)
82 {
83 double a = 0.1;
84 double b = 0.2;
85 double c = (a + b) / (a * b);
86 return 1;
87 }
88 EOT
89 }
90
91 test_uclibc() {
92 local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
93 if [ -d "${sysroot:-$TOOLCHAIN}" ]; then
94 local lib
95 for lib in "${sysroot:-$TOOLCHAIN}"/{lib,usr/lib,usr/local/lib}/ld*-uClibc*.so*; do
96 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
97 return 0
98 fi
99 done
100 fi
101 return 1
102 }
103
104 test_feature() {
105 local feature="$1"; shift
106
107 # find compilers, libc type
108 probe_cc
109 probe_cxx
110 probe_libc
111
112 # common toolchain feature tests
113 case "$feature" in
114 c) test_c; return $? ;;
115 c++) test_cxx; return $? ;;
116 soft*) test_softfloat; return $? ;;
117 esac
118
119 # assume eglibc/glibc supports all libc features
120 if [ "$LIBC_TYPE" != "uclibc" ]; then
121 return 0
122 fi
123
124 # uclibc feature tests
125 local inc
126 local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
127 for inc in "include" "usr/include" "usr/local/include"; do
128 local conf="${sysroot:-$TOOLCHAIN}/$inc/bits/uClibc_config.h"
129 if [ -f "$conf" ]; then
130 case "$feature" in
131 lfs) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?;;
132 ipv6) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?;;
133 rpc) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?;;
134 locale) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?;;
135 wchar) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?;;
136 threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
137 esac
138 fi
139 done
140
141 return 1
142 }
143
144
145 find_libs() {
146 local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
147
148 if [ -n "$spec" ] && probe_cpp; then
149 local libdir libdirs
150 for libdir in $(
151 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
152 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
153 ); do
154 if [ -d "$libdir" ]; then
155 libdirs="$libdirs $(cd "$libdir"; pwd)/"
156 fi
157 done
158
159 local pattern
160 for pattern in $(eval echo $spec); do
161 find $libdirs -name "$pattern.so*" | sort -u
162 done
163
164 return 0
165 fi
166
167 return 1
168 }
169
170 find_bins() {
171 local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##ip")"
172
173 if [ -n "$spec" ] && probe_cpp; then
174 local sysroot="$("$CPP" -print-sysroot)"
175
176 local bindir bindirs
177 for bindir in $(
178 echo "${sysroot:-$TOOLCHAIN}/bin";
179 echo "${sysroot:-$TOOLCHAIN}/usr/bin";
180 echo "${sysroot:-$TOOLCHAIN}/usr/local/bin";
181 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
182 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
183 ); do
184 if [ -d "$bindir" ]; then
185 bindirs="$bindirs $(cd "$bindir"; pwd)/"
186 fi
187 done
188
189 local pattern
190 for pattern in $(eval echo $spec); do
191 find $bindirs -name "$pattern" | sort -u
192 done
193
194 return 0
195 fi
196
197 return 1
198 }
199
200
201 wrap_bin_cc() {
202 local out="$1"
203 local bin="$2"
204
205 echo '#!/bin/sh' > "$out"
206 echo 'for arg in "$@"; do' >> "$out"
207 echo ' case "$arg" in -l*|-L*|-shared|-static)' >> "$out"
208 echo -n ' exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
209 echo -n '-idirafter "$STAGING_DIR/usr/include" ' >> "$out"
210 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
211 echo '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;' >> "$out"
212 echo ' esac' >> "$out"
213 echo 'done' >> "$out"
214 echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+' >> "$out"
215 echo '-idirafter "$STAGING_DIR/usr/include"} "$@"' >> "$out"
216
217 chmod +x "$out"
218 }
219
220 wrap_bin_ld() {
221 local out="$1"
222 local bin="$2"
223
224 echo '#!/bin/sh' > "$out"
225 echo -n 'exec "'"$bin"'" ${STAGING_DIR:+' >> "$out"
226 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
227 echo '-rpath-link "$STAGING_DIR/usr/lib"} "$@"' >> "$out"
228
229 chmod +x "$out"
230 }
231
232 wrap_bin_other() {
233 local out="$1"
234 local bin="$2"
235
236 echo '#!/bin/sh' > "$out"
237 echo 'exec "'"$bin"'" "$@"' >> "$out"
238
239 chmod +x "$out"
240 }
241
242 wrap_bins() {
243 if probe_cc; then
244 mkdir -p "$1" || return 1
245
246 local cmd
247 for cmd in "${CC%-*}-"*; do
248 if [ -x "$cmd" ]; then
249 local out="$1/${cmd##*/}"
250 local bin="$cmd"
251
252 if [ -x "$out" ] && ! grep -q STAGING_DIR "$out"; then
253 mv "$out" "$out.bin"
254 bin='$(dirname "$0")/'"${out##*/}"'.bin'
255 fi
256
257 case "${cmd##*/}" in
258 *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
259 wrap_bin_cc "$out" "$bin"
260 ;;
261 *-ld)
262 wrap_bin_ld "$out" "$bin"
263 ;;
264 *)
265 wrap_bin_other "$out" "$bin"
266 ;;
267 esac
268 fi
269 done
270
271 return 0
272 fi
273
274 return 1
275 }
276
277
278 print_config() {
279 local mktarget="$1"
280 local mksubtarget
281
282 local target="$("$CC" $CFLAGS -dumpmachine)"
283 local cpuarch="${target%%-*}"
284 local prefix="${CC##*/}"; prefix="${prefix%-*}-"
285 local config="${0%/scripts/*}/.config"
286
287 # if no target specified, print choice list and exit
288 if [ -z "$mktarget" ]; then
289 # prepare metadata
290 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
291 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
292 fi
293
294 local mktargets=$(
295 sed -ne "
296 /^Target: / { h };
297 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
298 " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
299 )
300
301 for mktarget in $mktargets; do
302 case "$mktarget" in */*)
303 mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
304 esac
305 done
306
307 if [ -n "$mktargets" ]; then
308 echo "Available targets:" >&2
309 echo $mktargets >&2
310 else
311 echo -e "Could not find a suitable OpenWrt target for " >&2
312 echo -e "CPU architecture '$cpuarch' - you need to " >&2
313 echo -e "define one first!" >&2
314 fi
315 return 1
316 fi
317
318 # bail out if there is a .config already
319 if [ -f "${0%/scripts/*}/.config" ]; then
320 echo "There already is a .config file, refusing to overwrite!" >&2
321 return 1
322 fi
323
324 case "$mktarget" in */*)
325 mksubtarget="${mktarget#*/}"
326 mktarget="${mktarget%/*}"
327 ;; esac
328
329
330 echo "CONFIG_TARGET_${mktarget}=y" > "$config"
331
332 if [ -n "$mksubtarget" ]; then
333 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
334 fi
335
336 if test_feature "softfloat"; then
337 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
338 else
339 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
340 fi
341
342 if test_feature "ipv6"; then
343 echo "CONFIG_IPV6=y" >> "$config"
344 else
345 echo "# CONFIG_IPV6 is not set" >> "$config"
346 fi
347
348 if test_feature "locale"; then
349 echo "CONFIG_BUILD_NLS=y" >> "$config"
350 else
351 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
352 fi
353
354 echo "CONFIG_DEVEL=y" >> "$config"
355 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
356 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
357 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
358 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
359
360 if [ "$LIBC_TYPE" != glibc ]; then
361 echo "CONFIG_TOOLCHAIN_LIBC=\"$LIBC_TYPE\"" >> "$config"
362 fi
363
364 local lib
365 for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN; do
366 local file
367 local spec=""
368 local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
369 for file in $(find_libs "$lib"); do
370 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
371 done
372 if [ -n "$spec" ]; then
373 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
374 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
375 else
376 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
377 fi
378 done
379
380 local bin
381 for bin in LDD LDCONFIG; do
382 local file
383 local spec=""
384 local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
385 for file in $(find_bins "$bin"); do
386 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
387 done
388 if [ -n "$spec" ]; then
389 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
390 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
391 else
392 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
393 fi
394 done
395
396 # inflate
397 make -C "${0%/scripts/*}" defconfig
398 return 0
399 }
400
401
402 probe_cc() {
403 if [ -z "$CC" ]; then
404 local bin
405 for bin in "bin" "usr/bin" "usr/local/bin"; do
406 local cmd
407 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
408 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
409 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
410 return 0
411 fi
412 done
413 done
414 return 1
415 fi
416 return 0
417 }
418
419 probe_cxx() {
420 if [ -z "$CXX" ]; then
421 local bin
422 for bin in "bin" "usr/bin" "usr/local/bin"; do
423 local cmd
424 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
425 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
426 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
427 return 0
428 fi
429 done
430 done
431 return 1
432 fi
433 return 0
434 }
435
436 probe_cpp() {
437 if [ -z "$CPP" ]; then
438 local bin
439 for bin in "bin" "usr/bin" "usr/local/bin"; do
440 local cmd
441 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
442 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
443 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
444 return 0
445 fi
446 done
447 done
448 return 1
449 fi
450 return 0
451 }
452
453 probe_libc() {
454 if [ -z "$LIBC_TYPE" ]; then
455 if test_uclibc; then
456 LIBC_TYPE="uclibc"
457 else
458 LIBC_TYPE="glibc"
459 fi
460 fi
461 return 0
462 }
463
464
465 while [ -n "$1" ]; do
466 arg="$1"; shift
467 case "$arg" in
468 --toolchain)
469 [ -d "$1" ] || {
470 echo "Toolchain directory '$1' does not exist." >&2
471 exit 1
472 }
473 TOOLCHAIN="$(cd "$1"; pwd)"; shift
474 ;;
475
476 --cflags)
477 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
478 ;;
479
480 --print-libc)
481 if probe_cc; then
482 probe_libc
483 echo "$LIBC_TYPE"
484 exit 0
485 fi
486 echo "No C compiler found in '$TOOLCHAIN'." >&2
487 exit 1
488 ;;
489
490 --print-target)
491 if probe_cc; then
492 exec "$CC" $CFLAGS -dumpmachine
493 fi
494 echo "No C compiler found in '$TOOLCHAIN'." >&2
495 exit 1
496 ;;
497
498 --print-bin)
499 if [ -z "$1" ]; then
500 echo "Available programs:" >&2
501 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
502 exit 1
503 fi
504
505 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
506 exit 0
507 ;;
508
509 --print-libs)
510 if [ -z "$1" ]; then
511 echo "Available libraries:" >&2
512 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
513 exit 1
514 fi
515
516 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
517 exit 0
518 ;;
519
520 --test)
521 test_feature "$1"
522 exit $?
523 ;;
524
525 --wrap)
526 [ -n "$1" ] || exec "$0" --help
527 wrap_bins "$1"
528 exit $?
529 ;;
530
531 --config)
532 if probe_cc; then
533 print_config "$1"
534 exit $?
535 fi
536 echo "No C compiler found in '$TOOLCHAIN'." >&2
537 exit 1
538 ;;
539
540 -h|--help)
541 me="$(basename "$0")"
542 echo -e "\nUsage:\n" >&2
543 echo -e " $me --toolchain {directory} --print-libc" >&2
544 echo -e " Print the libc implementation and exit.\n" >&2
545 echo -e " $me --toolchain {directory} --print-target" >&2
546 echo -e " Print the GNU target name and exit.\n" >&2
547 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
548 echo -e " Print executables belonging to given program," >&2
549 echo -e " omit program argument to get a list of names.\n" >&2
550 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
551 echo -e " Print shared objects belonging to given library," >&2
552 echo -e " omit library argument to get a list of names.\n" >&2
553 echo -e " $me --toolchain {directory} --test {feature}" >&2
554 echo -e " Test given feature, exit code indicates success." >&2
555 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
556 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
557 echo -e " 'threads'.\n" >&2
558 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
559 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
560 echo -e " linker, assembler and other key executables in " >&2
561 echo -e " the directory given with --wrap.\n" >&2
562 echo -e " $me --toolchain {directory} --config {target}" >&2
563 echo -e " Analyze the given toolchain and print a suitable" >&2
564 echo -e " .config for the given target. Omit target " >&2
565 echo -e " argument to get a list of names.\n" >&2
566 echo -e " $me --help" >&2
567 echo -e " Display this help text and exit.\n\n" >&2
568 echo -e " Most commands also take a --cflags parameter which " >&2
569 echo -e " is used to specify C flags to be passed to the " >&2
570 echo -e " cross compiler when performing tests." >&2
571 echo -e " This paremter may be repeated multiple times." >&2
572 exit 1
573 ;;
574
575 *)
576 echo "Unknown argument '$arg'" >&2
577 exec $0 --help
578 ;;
579 esac
580 done
581
582 exec $0 --help