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