scripts/download.pl: fix downloads with wget
[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 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 [ -f "$config" ]; then
373 sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL/d' "$config"
374 sed -i '/CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC/d' "$config"
375 fi
376
377 if [ "$LIBC_TYPE" == glibc ]; then
378 echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_GLIBC=y" >> "$config"
379 elif [ "$LIBC_TYPE" == musl ]; then
380 echo "CONFIG_EXTERNAL_TOOLCHAIN_LIBC_USE_MUSL=y" >> "$config"
381 else
382 echo "Can't detect LIBC type. Aborting!" >&2
383 return 1
384 fi
385
386 local lib
387 for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN GOMP; do
388 local file
389 local spec=""
390 local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
391 for file in $(find_libs "$lib"); do
392 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
393 done
394 if [ -n "$spec" ]; then
395 echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
396 echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
397 else
398 echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
399 fi
400 done
401
402 local bin
403 for bin in LDD LDCONFIG; do
404 local file
405 local spec=""
406 local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
407 for file in $(find_bins "$bin"); do
408 spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
409 done
410 if [ -n "$spec" ]; then
411 echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
412 echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
413 else
414 echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
415 fi
416 done
417
418 # inflate
419 make -C "${0%/scripts/*}" defconfig
420 return 0
421 }
422
423
424 probe_cc() {
425 if [ -z "$CC" ]; then
426 local bin
427 for bin in "bin" "usr/bin" "usr/local/bin"; do
428 local cmd
429 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
430 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
431 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
432 return 0
433 fi
434 done
435 done
436 return 1
437 fi
438 return 0
439 }
440
441 probe_cxx() {
442 if [ -z "$CXX" ]; then
443 local bin
444 for bin in "bin" "usr/bin" "usr/local/bin"; do
445 local cmd
446 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
447 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
448 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
449 return 0
450 fi
451 done
452 done
453 return 1
454 fi
455 return 0
456 }
457
458 probe_cpp() {
459 if [ -z "$CPP" ]; then
460 local bin
461 for bin in "bin" "usr/bin" "usr/local/bin"; do
462 local cmd
463 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
464 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
465 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
466 return 0
467 fi
468 done
469 done
470 return 1
471 fi
472 return 0
473 }
474
475 probe_libc() {
476 if [ -f $TOOLCHAIN/info.mk ]; then
477 LIBC_TYPE=$(grep LIBC_TYPE $TOOLCHAIN/info.mk | sed 's/LIBC_TYPE=//')
478 return 0
479 fi
480
481 echo "Warning! Can't find info.mk, trying to detect with alternative way."
482
483 if [ -z "$LIBC_TYPE" ]; then
484 if test_uclibc; then
485 LIBC_TYPE="uclibc"
486 else
487 LIBC_TYPE="glibc"
488 fi
489 fi
490 return 0
491 }
492
493
494 while [ -n "$1" ]; do
495 arg="$1"; shift
496 case "$arg" in
497 --toolchain)
498 [ -d "$1" ] || {
499 echo "Toolchain directory '$1' does not exist." >&2
500 exit 1
501 }
502 TOOLCHAIN="$(cd "$1"; pwd)"; shift
503 ;;
504
505 --cflags)
506 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
507 ;;
508
509 --print-libc)
510 if probe_cc; then
511 probe_libc
512 echo "$LIBC_TYPE"
513 exit 0
514 fi
515 echo "No C compiler found in '$TOOLCHAIN'." >&2
516 exit 1
517 ;;
518
519 --print-target)
520 if probe_cc; then
521 exec "$CC" $CFLAGS -dumpmachine
522 fi
523 echo "No C compiler found in '$TOOLCHAIN'." >&2
524 exit 1
525 ;;
526
527 --print-bin)
528 if [ -z "$1" ]; then
529 echo "Available programs:" >&2
530 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
531 exit 1
532 fi
533
534 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
535 exit 0
536 ;;
537
538 --print-libs)
539 if [ -z "$1" ]; then
540 echo "Available libraries:" >&2
541 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
542 exit 1
543 fi
544
545 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
546 exit 0
547 ;;
548
549 --test)
550 test_feature "$1"
551 exit $?
552 ;;
553
554 --wrap)
555 [ -n "$1" ] || exec "$0" --help
556 wrap_bins "$1"
557 exit $?
558 ;;
559
560 --overwrite-config)
561 OVERWRITE_CONFIG=y
562 ;;
563
564 --config)
565 if probe_cc; then
566 probe_libc
567 print_config "$1"
568 exit $?
569 fi
570 echo "No C compiler found in '$TOOLCHAIN'." >&2
571 exit 1
572 ;;
573
574 -h|--help)
575 me="$(basename "$0")"
576 echo -e "\nUsage:\n" >&2
577 echo -e " $me --toolchain {directory} --print-libc" >&2
578 echo -e " Print the libc implementation and exit.\n" >&2
579 echo -e " $me --toolchain {directory} --print-target" >&2
580 echo -e " Print the GNU target name and exit.\n" >&2
581 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
582 echo -e " Print executables belonging to given program," >&2
583 echo -e " omit program argument to get a list of names.\n" >&2
584 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
585 echo -e " Print shared objects belonging to given library," >&2
586 echo -e " omit library argument to get a list of names.\n" >&2
587 echo -e " $me --toolchain {directory} --test {feature}" >&2
588 echo -e " Test given feature, exit code indicates success." >&2
589 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
590 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
591 echo -e " 'threads'.\n" >&2
592 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
593 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
594 echo -e " linker, assembler and other key executables in " >&2
595 echo -e " the directory given with --wrap.\n" >&2
596 echo -e " $me --toolchain {directory} --config {target}" >&2
597 echo -e " Analyze the given toolchain and print a suitable" >&2
598 echo -e " .config for the given target. Omit target " >&2
599 echo -e " argument to get a list of names.\n" >&2
600 echo -e " $me --help" >&2
601 echo -e " Display this help text and exit.\n\n" >&2
602 echo -e " Most commands also take a --cflags parameter which " >&2
603 echo -e " is used to specify C flags to be passed to the " >&2
604 echo -e " cross compiler when performing tests." >&2
605 echo -e " This parameter may be repeated multiple times." >&2
606 echo -e " Use --overwrite-config before --config to overwrite" >&2
607 echo -e " an already present config with the required changes.">&2
608 exit 1
609 ;;
610
611 *)
612 echo "Unknown argument '$arg'" >&2
613 exec $0 --help
614 ;;
615 esac
616 done
617
618 exec $0 --help