package/kernel/leds-apu2: fix whitespaces
[openwrt/staging/chunkeey.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 cpuarch="${target%%-*}"
285 local prefix="${CC##*/}"; prefix="${prefix%-*}-"
286 local config="${0%/scripts/*}/.config"
287
288 # if no target specified, print choice list and exit
289 if [ -z "$mktarget" ]; then
290 # prepare metadata
291 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
292 "${0%/*}/scripts/config/mconf" prepare-tmpinfo
293 fi
294
295 local mktargets=$(
296 sed -ne "
297 /^Target: / { h };
298 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
299 " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
300 )
301
302 for mktarget in $mktargets; do
303 case "$mktarget" in */*)
304 mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
305 esac
306 done
307
308 if [ -n "$mktargets" ]; then
309 echo "Available targets:" >&2
310 echo $mktargets >&2
311 else
312 echo -e "Could not find a suitable OpenWrt target for " >&2
313 echo -e "CPU architecture '$cpuarch' - you need to " >&2
314 echo -e "define one first!" >&2
315 fi
316 return 1
317 fi
318
319 # bail out if there is a .config already
320 if [ -f "${0%/scripts/*}/.config" ]; then
321 echo "There already is a .config file, refusing to overwrite!" >&2
322 return 1
323 fi
324
325 case "$mktarget" in */*)
326 mksubtarget="${mktarget#*/}"
327 mktarget="${mktarget%/*}"
328 ;; esac
329
330
331 echo "CONFIG_TARGET_${mktarget}=y" > "$config"
332
333 if [ -n "$mksubtarget" ]; then
334 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
335 fi
336
337 if test_feature "softfloat"; then
338 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
339 else
340 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
341 fi
342
343 if test_feature "ipv6"; then
344 echo "CONFIG_IPV6=y" >> "$config"
345 else
346 echo "# CONFIG_IPV6 is not set" >> "$config"
347 fi
348
349 if test_feature "locale"; then
350 echo "CONFIG_BUILD_NLS=y" >> "$config"
351 else
352 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
353 fi
354
355 echo "CONFIG_DEVEL=y" >> "$config"
356 echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
357 echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
358 echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
359 echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
360
361 if [ "$LIBC_TYPE" != glibc ]; then
362 echo "CONFIG_TOOLCHAIN_LIBC=\"$LIBC_TYPE\"" >> "$config"
363 fi
364
365 local lib
366 for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP; do
367 local file
368 local spec=""
369 local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
370 for file in $(find_libs "$lib"); do
371 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
372 done
373 if [ -n "$spec" ]; then
374 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
375 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
376 else
377 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
378 fi
379 done
380
381 local bin
382 for bin in LDD LDCONFIG; do
383 local file
384 local spec=""
385 local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
386 for file in $(find_bins "$bin"); do
387 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
388 done
389 if [ -n "$spec" ]; then
390 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
391 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
392 else
393 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
394 fi
395 done
396
397 # inflate
398 make -C "${0%/scripts/*}" defconfig
399 return 0
400 }
401
402
403 probe_cc() {
404 if [ -z "$CC" ]; then
405 local bin
406 for bin in "bin" "usr/bin" "usr/local/bin"; do
407 local cmd
408 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
409 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
410 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
411 return 0
412 fi
413 done
414 done
415 return 1
416 fi
417 return 0
418 }
419
420 probe_cxx() {
421 if [ -z "$CXX" ]; then
422 local bin
423 for bin in "bin" "usr/bin" "usr/local/bin"; do
424 local cmd
425 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
426 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
427 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
428 return 0
429 fi
430 done
431 done
432 return 1
433 fi
434 return 0
435 }
436
437 probe_cpp() {
438 if [ -z "$CPP" ]; then
439 local bin
440 for bin in "bin" "usr/bin" "usr/local/bin"; do
441 local cmd
442 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
443 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
444 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
445 return 0
446 fi
447 done
448 done
449 return 1
450 fi
451 return 0
452 }
453
454 probe_libc() {
455 if [ -z "$LIBC_TYPE" ]; then
456 if test_uclibc; then
457 LIBC_TYPE="uclibc"
458 else
459 LIBC_TYPE="glibc"
460 fi
461 fi
462 return 0
463 }
464
465
466 while [ -n "$1" ]; do
467 arg="$1"; shift
468 case "$arg" in
469 --toolchain)
470 [ -d "$1" ] || {
471 echo "Toolchain directory '$1' does not exist." >&2
472 exit 1
473 }
474 TOOLCHAIN="$(cd "$1"; pwd)"; shift
475 ;;
476
477 --cflags)
478 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
479 ;;
480
481 --print-libc)
482 if probe_cc; then
483 probe_libc
484 echo "$LIBC_TYPE"
485 exit 0
486 fi
487 echo "No C compiler found in '$TOOLCHAIN'." >&2
488 exit 1
489 ;;
490
491 --print-target)
492 if probe_cc; then
493 exec "$CC" $CFLAGS -dumpmachine
494 fi
495 echo "No C compiler found in '$TOOLCHAIN'." >&2
496 exit 1
497 ;;
498
499 --print-bin)
500 if [ -z "$1" ]; then
501 echo "Available programs:" >&2
502 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
503 exit 1
504 fi
505
506 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
507 exit 0
508 ;;
509
510 --print-libs)
511 if [ -z "$1" ]; then
512 echo "Available libraries:" >&2
513 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
514 exit 1
515 fi
516
517 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
518 exit 0
519 ;;
520
521 --test)
522 test_feature "$1"
523 exit $?
524 ;;
525
526 --wrap)
527 [ -n "$1" ] || exec "$0" --help
528 wrap_bins "$1"
529 exit $?
530 ;;
531
532 --config)
533 if probe_cc; then
534 print_config "$1"
535 exit $?
536 fi
537 echo "No C compiler found in '$TOOLCHAIN'." >&2
538 exit 1
539 ;;
540
541 -h|--help)
542 me="$(basename "$0")"
543 echo -e "\nUsage:\n" >&2
544 echo -e " $me --toolchain {directory} --print-libc" >&2
545 echo -e " Print the libc implementation and exit.\n" >&2
546 echo -e " $me --toolchain {directory} --print-target" >&2
547 echo -e " Print the GNU target name and exit.\n" >&2
548 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
549 echo -e " Print executables belonging to given program," >&2
550 echo -e " omit program argument to get a list of names.\n" >&2
551 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
552 echo -e " Print shared objects belonging to given library," >&2
553 echo -e " omit library argument to get a list of names.\n" >&2
554 echo -e " $me --toolchain {directory} --test {feature}" >&2
555 echo -e " Test given feature, exit code indicates success." >&2
556 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
557 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
558 echo -e " 'threads'.\n" >&2
559 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
560 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
561 echo -e " linker, assembler and other key executables in " >&2
562 echo -e " the directory given with --wrap.\n" >&2
563 echo -e " $me --toolchain {directory} --config {target}" >&2
564 echo -e " Analyze the given toolchain and print a suitable" >&2
565 echo -e " .config for the given target. Omit target " >&2
566 echo -e " argument to get a list of names.\n" >&2
567 echo -e " $me --help" >&2
568 echo -e " Display this help text and exit.\n\n" >&2
569 echo -e " Most commands also take a --cflags parameter which " >&2
570 echo -e " is used to specify C flags to be passed to the " >&2
571 echo -e " cross compiler when performing tests." >&2
572 echo -e " This paremter may be repeated multiple times." >&2
573 exit 1
574 ;;
575
576 *)
577 echo "Unknown argument '$arg'" >&2
578 exec $0 --help
579 ;;
580 esac
581 done
582
583 exec $0 --help