[scripts] ext-toolchain.sh:
[openwrt.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         cpp:      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" ]; then
93                 local lib
94                 for lib in "$sysroot"/{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/$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:##p")"
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:##p")"
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/bin";
178                         echo "$sysroot/usr/bin";
179                         echo "$sysroot/usr/local/bin";
180                         echo "$TOOLCHAIN/bin";
181                         echo "$TOOLCHAIN/usr/bin";
182                         echo "$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_bins() {
204         if probe_cc; then
205                 mkdir -p "$1" || return 1
206
207                 local cmd
208                 for cmd in "${CC%-*}-"*; do
209                         if [ -x "$cmd" ]; then
210                                 local out="$1/${cmd##*/}"
211
212                                 echo '#!/bin/sh' > "$out"
213                                 case "${cmd##*/}" in
214                                         *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
215                                                 echo -n 'exec "'"$cmd"'" '"$CFLAGS"' '         >> "$out"
216                                                 echo -n '${STAGING_DIR:+-idirafter '           >> "$out"
217                                                 echo -n '"$STAGING_DIR/usr/include" '          >> "$out"
218                                                 echo -n '-L "$STAGING_DIR/usr/lib" '           >> "$out"
219                                                 echo -n '-Wl,-rpath-link,'                     >> "$out"
220                                                 echo    '"$STAGING_DIR/usr/lib"} "$@"'         >> "$out"
221                                         ;;
222                                         *-ld)
223                                                 echo -n 'exec "'"$cmd"'" ${STAGING_DIR:+'      >> "$out"
224                                                 echo -n '-L "$STAGING_DIR/usr/lib" '           >> "$out"
225                                                 echo -n '-rpath-link '                         >> "$out"
226                                                 echo    '"$STAGING_DIR/usr/lib"} "$@"'         >> "$out"
227                                         ;;
228                                         *)
229                                                 echo "exec '$cmd' \"\$@\"" >> "$out"
230                                         ;;
231                                 esac
232                                 chmod +x "$out"
233                         fi
234                 done
235
236                 return 0
237         fi
238
239         return 1
240 }
241
242
243 probe_cc() {
244         if [ -z "$CC" ]; then
245                 local bin
246                 for bin in "bin" "usr/bin" "usr/local/bin"; do
247                         local cmd
248                         for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
249                                 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
250                                         CC="$(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_cxx() {
261         if [ -z "$CXX" ]; then
262                 local bin
263                 for bin in "bin" "usr/bin" "usr/local/bin"; do
264                         local cmd
265                         for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
266                                 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
267                                         CXX="$(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_cpp() {
278         if [ -z "$CPP" ]; then
279                 local bin
280                 for bin in "bin" "usr/bin" "usr/local/bin"; do
281                         local cmd
282                         for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
283                                 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
284                                         CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
285                                         return 0
286                                 fi
287                         done
288                 done
289                 return 1
290         fi
291         return 0
292 }
293
294 probe_libc() {
295         if [ -z "$LIBC_TYPE" ]; then
296                 if test_uclibc; then
297                         LIBC_TYPE="uclibc"
298                 else
299                         LIBC_TYPE="glibc"
300                 fi
301         fi
302         return 0
303 }
304
305
306 while [ -n "$1" ]; do
307         arg="$1"; shift
308         case "$arg" in
309                 --toolchain)
310                         [ -d "$1" ] || {
311                                 echo "Toolchain directory '$1' does not exist." >&2
312                                 exit 1
313                         }
314                         TOOLCHAIN="$(cd "$1"; pwd)"; shift
315                 ;;
316
317                 --cflags)
318                         CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
319                 ;;
320
321                 --print-libc)
322                         if probe_cc; then
323                                 probe_libc
324                                 echo "$LIBC_TYPE"
325                                 exit 0
326                         fi
327                         echo "No C compiler found in '$TOOLCHAIN'." >&2
328                         exit 1
329                 ;;
330
331                 --print-target)
332                         if probe_cc; then
333                                 exec "$CC" $CFLAGS -dumpmachine
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 exec $0 --help