[scripts] add ext-toolchain.sh, a utility for external toolchains
[openwrt/svn-archive/archive.git] / scripts / ext-toolchain.sh
1 #!/usr/bin/env bash
2 # Script to copy a toolchain from given source to given
3 # destination directory.
4
5 CC=""
6 CXX=""
7 CPP=""
8
9 CFLAGS=""
10 TOOLCHAIN="."
11
12 LIBC_TYPE=""
13
14
15 # Library specs
16 LIB_SPECS="
17 c: ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
18 rt: librt-* librt
19 pthread: libpthread-* libpthread
20 cpp: libstdc++
21 gcc: libgcc_s
22 ssp: libssp
23 gfortran: libgfortran
24 "
25
26 # Binary specs
27 BIN_SPECS="
28 ldd: ldd
29 ldconfig: ldconfig
30 gdb: gdb
31 gdbserver: gdbserver
32 "
33
34
35 test_c() {
36 cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
37 #include <stdio.h>
38
39 int main(int argc, char **argv)
40 {
41 printf("Hello, world!\n");
42 return 0;
43 }
44 EOT
45 }
46
47 test_cxx() {
48 cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
49 #include <iostream>
50
51 using namespace std;
52
53 int main()
54 {
55 cout << "Hello, world!" << endl;
56 return 0;
57 }
58 EOT
59 }
60
61 test_softfloat() {
62 cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
63 int main(int argc, char **argv)
64 {
65 double a = 0.1;
66 double b = 0.2;
67 double c = (a + b) / (a * b);
68 return 1;
69 }
70 EOT
71 }
72
73 test_uclibc() {
74 local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
75 if [ -d "$sysroot" ]; then
76 local lib
77 for lib in "$sysroot"/{lib,usr/lib,usr/local/lib}/ld-uClibc*.so*; do
78 if [ -f "$lib" ] && [ ! -h "$lib" ]; then
79 return 0
80 fi
81 done
82 fi
83 return 1
84 }
85
86 test_feature() {
87 local feature="$1"; shift
88
89 # find compilers, libc type
90 probe_cc
91 probe_cxx
92 probe_libc
93
94 # common toolchain feature tests
95 case "$feature" in
96 c) test_c; return $? ;;
97 c++) test_cxx; return $? ;;
98 soft*) test_softfloat; return $? ;;
99 esac
100
101 # assume eglibc/glibc supports all libc features
102 if [ "$LIBC_TYPE" != "uclibc" ]; then
103 return 0
104 fi
105
106 # uclibc feature tests
107 local inc
108 local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
109 for inc in "include" "usr/include" "usr/local/include"; do
110 local conf="$sysroot/$inc/bits/uClibc_config.h"
111 if [ -f "$conf" ]; then
112 case "$feature" in
113 lfs) grep -q '__UCLIBC_HAS_LFS__ 1' "$conf"; return $?;;
114 ipv6) grep -q '__UCLIBC_HAS_IPV6__ 1' "$conf"; return $?;;
115 rpc) grep -q '__UCLIBC_HAS_RPC__ 1' "$conf"; return $?;;
116 locale) grep -q '__UCLIBC_HAS_LOCALE__ 1' "$conf"; return $?;;
117 wchar) grep -q '__UCLIBC_HAS_WCHAR__ 1' "$conf"; return $?;;
118 threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
119 esac
120 fi
121 done
122
123 return 1
124 }
125
126
127 find_libs() {
128 local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##p")"
129
130 if [ -n "$spec" ] && probe_cpp; then
131 local libdir libdirs
132 for libdir in $(
133 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
134 sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
135 ); do
136 if [ -d "$libdir" ]; then
137 libdirs="$libdirs $(cd "$libdir"; pwd)/"
138 fi
139 done
140
141 local pattern
142 for pattern in $(eval echo $spec); do
143 find $libdirs -name "$pattern.so*" | sort -u
144 done
145
146 return 0
147 fi
148
149 return 1
150 }
151
152 find_bins() {
153 local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##p")"
154
155 if [ -n "$spec" ] && probe_cpp; then
156 local sysroot="$("$CPP" -print-sysroot)"
157
158 local bindir bindirs
159 for bindir in $(
160 echo "$sysroot/bin";
161 echo "$sysroot/usr/bin";
162 echo "$sysroot/usr/local/bin";
163 echo "$TOOLCHAIN/bin";
164 echo "$TOOLCHAIN/usr/bin";
165 echo "$TOOLCHAIN/usr/local/bin";
166 "$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
167 sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
168 ); do
169 if [ -d "$bindir" ]; then
170 bindirs="$bindirs $(cd "$bindir"; pwd)/"
171 fi
172 done
173
174 local pattern
175 for pattern in $(eval echo $spec); do
176 find $bindirs -name "$pattern" | sort -u
177 done
178
179 return 0
180 fi
181
182 return 1
183 }
184
185
186 wrap_bins() {
187 if probe_cc; then
188 mkdir -p "$1" || return 1
189
190 local cmd
191 for cmd in "${CC%-*}-"*; do
192 if [ -x "$cmd" ]; then
193 local out="$1/${cmd##*/}"
194
195 echo '#!/bin/sh' > "$out"
196 case "${cmd##*/}" in
197 *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
198 echo -n 'exec "'"$cmd"'" '"$CFLAGS"' ' >> "$out"
199 echo -n '${STAGING_DIR:+-idirafter ' >> "$out"
200 echo -n '"$STAGING_DIR/usr/include" ' >> "$out"
201 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
202 echo -n '-Wl,-rpath-link,' >> "$out"
203 echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
204 ;;
205 *-ld)
206 echo -n 'exec "'"$cmd"'" ${STAGING_DIR:+' >> "$out"
207 echo -n '-L "$STAGING_DIR/usr/lib" ' >> "$out"
208 echo -n '-rpath-link ' >> "$out"
209 echo '"$STAGING_DIR/usr/lib"} "$@"' >> "$out"
210 ;;
211 *)
212 echo "exec '$cmd' \"\$@\"" >> "$out"
213 ;;
214 esac
215 chmod +x "$out"
216 fi
217 done
218
219 return 0
220 fi
221
222 return 1
223 }
224
225
226 probe_cc() {
227 if [ -z "$CC" ]; then
228 local bin
229 for bin in "bin" "usr/bin" "usr/local/bin"; do
230 local cmd
231 for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
232 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
233 CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
234 return 0
235 fi
236 done
237 done
238 return 1
239 fi
240 return 0
241 }
242
243 probe_cxx() {
244 if [ -z "$CXX" ]; then
245 local bin
246 for bin in "bin" "usr/bin" "usr/local/bin"; do
247 local cmd
248 for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
249 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
250 CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
251 return 0
252 fi
253 done
254 done
255 return 1
256 fi
257 return 0
258 }
259
260 probe_cpp() {
261 if [ -z "$CPP" ]; then
262 local bin
263 for bin in "bin" "usr/bin" "usr/local/bin"; do
264 local cmd
265 for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
266 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
267 CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
268 return 0
269 fi
270 done
271 done
272 return 1
273 fi
274 return 0
275 }
276
277 probe_libc() {
278 if [ -z "$LIBC_TYPE" ]; then
279 if test_uclibc; then
280 LIBC_TYPE="uclibc"
281 else
282 LIBC_TYPE="glibc"
283 fi
284 fi
285 return 0
286 }
287
288
289 while [ -n "$1" ]; do
290 arg="$1"; shift
291 case "$arg" in
292 -l|--libs)
293 [ -n "$1" ] || {
294 echo "No library given, specify one of:"$(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&1
295 exit 1
296 }
297 FINDLIB="$1"; shift
298 ;;
299 -b|--bins)
300 [ -n "$1" ] || {
301 echo "No binary given, specify one of:"$(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&1
302 exit 1
303 }
304 FINDBIN="$1"; shift
305 ;;
306
307 --toolchain)
308 [ -d "$1" ] || {
309 echo "Toolchain directory '$1' does not exist." >&2
310 exit 1
311 }
312 TOOLCHAIN="$(cd "$1"; pwd)"; shift
313 ;;
314
315 --cflags)
316 CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
317 ;;
318
319 --print-libc)
320 if probe_cc; then
321 probe_libc
322 echo "$LIBC_TYPE"
323 exit 0
324 fi
325 echo "No C compiler found in '$TOOLCHAIN'." >&2
326 exit 1
327 ;;
328
329 --print-target)
330 if probe_cc; then
331 CC="${CC##*/}"
332 echo "${CC%-*}"
333 exit 0
334 fi
335 echo "No C compiler found in '$TOOLCHAIN'." >&2
336 exit 1
337 ;;
338
339 --print-bin)
340 if [ -z "$1" ]; then
341 echo "Available programs:" >&2
342 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
343 exit 1
344 fi
345
346 find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
347 exit 0
348 ;;
349
350 --print-libs)
351 if [ -z "$1" ]; then
352 echo "Available libraries:" >&2
353 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
354 exit 1
355 fi
356
357 find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
358 exit 0
359 ;;
360
361 --test)
362 test_feature "$1"
363 exit $?
364 ;;
365
366 --wrap)
367 [ -n "$1" ] || exec "$0" --help
368 wrap_bins "$1"
369 exit $?
370 ;;
371
372 -h|--help)
373 me="$(basename "$0")"
374 echo -e "\nUsage:\n" >&2
375 echo -e " $me --toolchain {directory} --print-libc" >&2
376 echo -e " Print the libc implementation and exit.\n" >&2
377 echo -e " $me --toolchain {directory} --print-target" >&2
378 echo -e " Print the GNU target name and exit.\n" >&2
379 echo -e " $me --toolchain {directory} --print-bin {program}" >&2
380 echo -e " Print executables belonging to given program," >&2
381 echo -e " omit program argument to get a list of names.\n" >&2
382 echo -e " $me --toolchain {directory} --print-libs {library}" >&2
383 echo -e " Print shared objects belonging to given library," >&2
384 echo -e " omit library argument to get a list of names.\n" >&2
385 echo -e " $me --toolchain {directory} --test {feature}" >&2
386 echo -e " Test given feature, exit code indicates success." >&2
387 echo -e " Possible features are 'c', 'c++', 'softfloat'," >&2
388 echo -e " 'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and " >&2
389 echo -e " 'threads'.\n" >&2
390 echo -e " $me --toolchain {directory} --wrap {directory}" >&2
391 echo -e " Create wrapper scripts for C and C++ compiler, " >&2
392 echo -e " linker, assembler and other key executables in " >&2
393 echo -e " the directory given with --wrap.\n" >&2
394 echo -e " $me --help" >&2
395 echo -e " Display this help text and exit.\n\n" >&2
396 echo -e " Most commands also take a --cflags parameter which " >&2
397 echo -e " is used to specify C flags to be passed to the " >&2
398 echo -e " cross compiler when performing tests." >&2
399 echo -e " This paremter may be repeated multiple times." >&2
400 exit 1
401 ;;
402
403 *)
404 echo "Unknown argument '$arg'" >&2
405 exec $0 --help
406 ;;
407 esac
408 done
409
410 exit 0