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