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