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