pkg-config: fix occasional install problem
[oweals/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         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_bin_cc() {
201         local out="$1"
202         local bin="$2"
203
204         echo    '#!/bin/sh'                                                > "$out"
205         echo    'for arg in "$@"; do'                                     >> "$out"
206         echo    ' case "$arg" in -l*|-L*|-shared|-static)'                >> "$out"
207         echo -n '  exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+'           >> "$out"
208         echo -n '-idirafter "$STAGING_DIR/usr/include" '                  >> "$out"
209         echo -n '-L "$STAGING_DIR/usr/lib" '                              >> "$out"
210         echo    '-Wl,-rpath-link,"$STAGING_DIR/usr/lib"} "$@" ;;'         >> "$out"
211         echo    ' esac'                                                   >> "$out"
212         echo    'done'                                                    >> "$out"
213         echo -n 'exec "'"$bin"'" '"$CFLAGS"' ${STAGING_DIR:+'             >> "$out"
214         echo    '-idirafter "$STAGING_DIR/usr/include"} "$@"'             >> "$out"
215
216         chmod +x "$out"
217 }
218
219 wrap_bin_ld() {
220         local out="$1"
221         local bin="$2"
222
223         echo    '#!/bin/sh'                                                > "$out"
224         echo -n 'exec "'"$bin"'" ${STAGING_DIR:+'                         >> "$out"
225         echo -n '-L "$STAGING_DIR/usr/lib" '                              >> "$out"
226         echo    '-rpath-link "$STAGING_DIR/usr/lib"} "$@"'                >> "$out"
227
228         chmod +x "$out"
229 }
230
231 wrap_bin_other() {
232         local out="$1"
233         local bin="$2"
234
235         echo    '#!/bin/sh'                                                > "$out"
236         echo    'exec "'"$bin"'" "$@"'                                    >> "$out"
237
238         chmod +x "$out"
239 }
240
241 wrap_bins() {
242         if probe_cc; then
243                 mkdir -p "$1" || return 1
244
245                 local cmd
246                 for cmd in "${CC%-*}-"*; do
247                         if [ -x "$cmd" ]; then
248                                 local out="$1/${cmd##*/}"
249                                 local bin="$cmd"
250
251                                 if [ -x "$out" ] && ! grep -q STAGING_DIR "$out"; then
252                                         mv "$out" "$out.bin"
253                                         bin='$(dirname "$0")/'"${out##*/}"'.bin'
254                                 fi
255
256                                 case "${cmd##*/}" in
257                                         *-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
258                                                 wrap_bin_cc "$out" "$bin"
259                                         ;;
260                                         *-ld)
261                                                 wrap_bin_ld "$out" "$bin"
262                                         ;;
263                                         *)
264                                                 wrap_bin_other "$out" "$bin"
265                                         ;;
266                                 esac
267                         fi
268                 done
269
270                 return 0
271         fi
272
273         return 1
274 }
275
276
277 print_config() {
278         local mktarget="$1"
279         local mksubtarget
280
281         local target="$("$CC" $CFLAGS -dumpmachine)"
282         local cpuarch="${target%%-*}"
283         local prefix="${CC##*/}"; prefix="${prefix%-*}-"
284         local config="${0%/scripts/*}/.config"
285
286         # if no target specified, print choice list and exit
287         if [ -z "$mktarget" ]; then
288                 # prepare metadata
289                 if [ ! -f "${0%/scripts/*}/tmp/.targetinfo" ]; then
290                         "${0%/*}/scripts/config/mconf" prepare-tmpinfo
291                 fi
292
293                 local mktargets=$(
294                         sed -ne "
295                                 /^Target: / { h };
296                                 /^Target-Arch: $cpuarch\$/ { x; s#^Target: ##p }
297                         " "${0%/scripts/*}/tmp/.targetinfo" | sort -u
298                 )
299
300                 for mktarget in $mktargets; do
301                         case "$mktarget" in */*)
302                                 mktargets=$(echo "$mktargets" | sed -e "/^${mktarget%/*}\$/d")
303                         esac
304                 done
305
306                 if [ -n "$mktargets" ]; then
307                         echo "Available targets:"                               >&2
308                         echo $mktargets                                         >&2
309                 else
310                         echo -e "Could not find a suitable OpenWrt target for " >&2
311                         echo -e "CPU architecture '$cpuarch' - you need to "    >&2
312                         echo -e "define one first!"                             >&2
313                 fi
314                 return 1
315         fi
316
317         # bail out if there is a .config already
318         if [ -f "${0%/scripts/*}/.config" ]; then
319                 echo "There already is a .config file, refusing to overwrite!" >&2
320                 return 1
321         fi
322
323         case "$mktarget" in */*)
324                 mksubtarget="${mktarget#*/}"
325                 mktarget="${mktarget%/*}"
326         ;; esac
327
328
329         echo "CONFIG_TARGET_${mktarget}=y" > "$config"
330
331         if [ -n "$mksubtarget" ]; then
332                 echo "CONFIG_TARGET_${mktarget}_${mksubtarget}=y" >> "$config"
333         fi
334
335         if test_feature "softfloat"; then
336                 echo "CONFIG_SOFT_FLOAT=y" >> "$config"
337         else
338                 echo "# CONFIG_SOFT_FLOAT is not set" >> "$config"
339         fi
340
341         if test_feature "ipv6"; then
342                 echo "CONFIG_IPV6=y" >> "$config"
343         else
344                 echo "# CONFIG_IPV6 is not set" >> "$config"
345         fi
346
347         if test_feature "locale"; then
348                 echo "CONFIG_BUILD_NLS=y" >> "$config"
349         else
350                 echo "# CONFIG_BUILD_NLS is not set" >> "$config"
351         fi
352
353         echo "CONFIG_DEVEL=y" >> "$config"
354         echo "CONFIG_EXTERNAL_TOOLCHAIN=y" >> "$config"
355         echo "CONFIG_TOOLCHAIN_ROOT=\"$TOOLCHAIN\"" >> "$config"
356         echo "CONFIG_TOOLCHAIN_PREFIX=\"$prefix\"" >> "$config"
357         echo "CONFIG_TARGET_NAME=\"$target\"" >> "$config"
358
359         if [ "$LIBC_TYPE" != glibc ]; then
360                 echo "CONFIG_TOOLCHAIN_LIBC=\"$LIBC_TYPE\"" >> "$config"
361         fi
362
363         local lib
364         for lib in C RT PTHREAD GCC STDCPP SSP GFORTRAN; do
365                 local file
366                 local spec=""
367                 local llib="$(echo "$lib" | sed -e 's#.*#\L&#')"
368                 for file in $(find_libs "$lib"); do
369                         spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
370                 done
371                 if [ -n "$spec" ]; then
372                         echo "CONFIG_PACKAGE_lib${llib}=y" >> "$config"
373                         echo "CONFIG_LIB${lib}_FILE_SPEC=\"$spec\"" >> "$config"
374                 else
375                         echo "# CONFIG_PACKAGE_lib${llib} is not set" >> "$config"
376                 fi
377         done
378
379         local bin
380         for bin in LDD LDCONFIG; do
381                 local file
382                 local spec=""
383                 local lbin="$(echo "$bin" | sed -e 's#.*#\L&#')"
384                 for file in $(find_bins "$bin"); do
385                         spec="${spec:+$spec }$(echo "$file" | sed -e "s#^$TOOLCHAIN#.#")"
386                 done
387                 if [ -n "$spec" ]; then
388                         echo "CONFIG_PACKAGE_${lbin}=y" >> "$config"
389                         echo "CONFIG_${bin}_FILE_SPEC=\"$spec\"" >> "$config"
390                 else
391                         echo "# CONFIG_PACKAGE_${lbin} is not set" >> "$config"
392                 fi
393         done
394
395         # inflate
396         make -C "${0%/scripts/*}" defconfig
397         return 0
398 }
399
400
401 probe_cc() {
402         if [ -z "$CC" ]; then
403                 local bin
404                 for bin in "bin" "usr/bin" "usr/local/bin"; do
405                         local cmd
406                         for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
407                                 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
408                                         CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
409                                         return 0
410                                 fi
411                         done
412                 done
413                 return 1
414         fi
415         return 0
416 }
417
418 probe_cxx() {
419         if [ -z "$CXX" ]; then
420                 local bin
421                 for bin in "bin" "usr/bin" "usr/local/bin"; do
422                         local cmd
423                         for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
424                                 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
425                                         CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
426                                         return 0
427                                 fi
428                         done
429                 done
430                 return 1
431         fi
432         return 0
433 }
434
435 probe_cpp() {
436         if [ -z "$CPP" ]; then
437                 local bin
438                 for bin in "bin" "usr/bin" "usr/local/bin"; do
439                         local cmd
440                         for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
441                                 if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
442                                         CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
443                                         return 0
444                                 fi
445                         done
446                 done
447                 return 1
448         fi
449         return 0
450 }
451
452 probe_libc() {
453         if [ -z "$LIBC_TYPE" ]; then
454                 if test_uclibc; then
455                         LIBC_TYPE="uclibc"
456                 else
457                         LIBC_TYPE="glibc"
458                 fi
459         fi
460         return 0
461 }
462
463
464 while [ -n "$1" ]; do
465         arg="$1"; shift
466         case "$arg" in
467                 --toolchain)
468                         [ -d "$1" ] || {
469                                 echo "Toolchain directory '$1' does not exist." >&2
470                                 exit 1
471                         }
472                         TOOLCHAIN="$(cd "$1"; pwd)"; shift
473                 ;;
474
475                 --cflags)
476                         CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
477                 ;;
478
479                 --print-libc)
480                         if probe_cc; then
481                                 probe_libc
482                                 echo "$LIBC_TYPE"
483                                 exit 0
484                         fi
485                         echo "No C compiler found in '$TOOLCHAIN'." >&2
486                         exit 1
487                 ;;
488
489                 --print-target)
490                         if probe_cc; then
491                                 exec "$CC" $CFLAGS -dumpmachine
492                         fi
493                         echo "No C compiler found in '$TOOLCHAIN'." >&2
494                         exit 1
495                 ;;
496
497                 --print-bin)
498                         if [ -z "$1" ]; then
499                                 echo "Available programs:"                      >&2
500                                 echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
501                                 exit 1
502                         fi
503
504                         find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
505                         exit 0
506                 ;;
507
508                 --print-libs)
509                         if [ -z "$1" ]; then
510                                 echo "Available libraries:"                     >&2
511                                 echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
512                                 exit 1
513                         fi
514
515                         find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
516                         exit 0
517                 ;;
518
519                 --test)
520                         test_feature "$1"
521                         exit $?
522                 ;;
523
524                 --wrap)
525                         [ -n "$1" ] || exec "$0" --help
526                         wrap_bins "$1"
527                         exit $?
528                 ;;
529
530                 --config)
531                         if probe_cc; then
532                                 print_config "$1"
533                                 exit $?
534                         fi
535                         echo "No C compiler found in '$TOOLCHAIN'." >&2
536                         exit 1
537                 ;;
538
539                 -h|--help)
540                         me="$(basename "$0")"
541                         echo -e "\nUsage:\n"                                            >&2
542                         echo -e "  $me --toolchain {directory} --print-libc"            >&2
543                         echo -e "    Print the libc implementation and exit.\n"         >&2
544                         echo -e "  $me --toolchain {directory} --print-target"          >&2
545                         echo -e "    Print the GNU target name and exit.\n"             >&2
546                         echo -e "  $me --toolchain {directory} --print-bin {program}"   >&2
547                         echo -e "    Print executables belonging to given program,"     >&2
548                         echo -e "    omit program argument to get a list of names.\n"   >&2
549                         echo -e "  $me --toolchain {directory} --print-libs {library}"  >&2
550                         echo -e "    Print shared objects belonging to given library,"  >&2
551                         echo -e "    omit library argument to get a list of names.\n"   >&2
552                         echo -e "  $me --toolchain {directory} --test {feature}"        >&2
553                         echo -e "    Test given feature, exit code indicates success."  >&2
554                         echo -e "    Possible features are 'c', 'c++', 'softfloat',"    >&2
555                         echo -e "    'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and "      >&2
556                         echo -e "    'threads'.\n"                                      >&2
557                         echo -e "  $me --toolchain {directory} --wrap {directory}"      >&2
558                         echo -e "    Create wrapper scripts for C and C++ compiler, "   >&2
559                         echo -e "    linker, assembler and other key executables in "   >&2
560                         echo -e "    the directory given with --wrap.\n"                >&2
561                         echo -e "  $me --toolchain {directory} --config {target}"       >&2
562                         echo -e "    Analyze the given toolchain and print a suitable"  >&2
563                         echo -e "    .config for the given target. Omit target "        >&2
564                         echo -e "    argument to get a list of names.\n"                >&2
565                         echo -e "  $me --help"                                          >&2
566                         echo -e "    Display this help text and exit.\n\n"              >&2
567                         echo -e "  Most commands also take a --cflags parameter which " >&2
568                         echo -e "  is used to specify C flags to be passed to the "     >&2
569                         echo -e "  cross compiler when performing tests."               >&2
570                         echo -e "  This paremter may be repeated multiple times."       >&2
571                         exit 1
572                 ;;
573
574                 *)
575                         echo "Unknown argument '$arg'" >&2
576                         exec $0 --help
577                 ;;
578         esac
579 done
580
581 exec $0 --help