aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/ext-toolchain.sh
blob: 7283bf9cc0cceaead9f62d7c126e8f31fdfaa309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env bash
#
#   Script for various external toolchain tasks, refer to
#   the --help output for more information.
#
#   Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

CC=""
CXX=""
CPP=""

CFLAGS=""
TOOLCHAIN="."

LIBC_TYPE=""


# Library specs
LIB_SPECS="
	c:        ld-* lib{anl,c,cidn,crypt,dl,m,nsl,nss_dns,nss_files,resolv,util}
	rt:       librt-* librt
	pthread:  libpthread-* libpthread
	cpp:      libstdc++
	gcc:      libgcc_s
	ssp:      libssp
	gfortran: libgfortran
"

# Binary specs
BIN_SPECS="
	ldd:       ldd
	ldconfig:  ldconfig
	gdb:       gdb
	gdbserver: gdbserver
"


test_c() {
	cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
		#include <stdio.h>

		int main(int argc, char **argv)
		{
			printf("Hello, world!\n");
			return 0;
		}
	EOT
}

test_cxx() {
	cat <<-EOT | "${CXX:-false}" $CFLAGS -o /dev/null -x c++ - 2>/dev/null
		#include <iostream>

		using namespace std;

		int main()
		{
			cout << "Hello, world!" << endl;
			return 0;
		}
	EOT
}

test_softfloat() {
	cat <<-EOT | "$CC" $CFLAGS -msoft-float -o /dev/null -x c - 2>/dev/null
		int main(int argc, char **argv)
		{
			double a = 0.1;
			double b = 0.2;
			double c = (a + b) / (a * b);
			return 1;
		}
	EOT
}

test_uclibc() {
	local sysroot="$("$CC" $CFLAGS -print-sysroot 2>/dev/null)"
	if [ -d "$sysroot" ]; then
		local lib
		for lib in "$sysroot"/{lib,usr/lib,usr/local/lib}/ld-uClibc*.so*; do
			if [ -f "$lib" ] && [ ! -h "$lib" ]; then
				return 0
			fi
		done
	fi
	return 1
}

test_feature() {
	local feature="$1"; shift

	# find compilers, libc type
	probe_cc
	probe_cxx
	probe_libc

	# common toolchain feature tests
	case "$feature" in
		c)     test_c;         return $? ;;
		c++)   test_cxx;       return $? ;;
		soft*) test_softfloat; return $? ;;
	esac

	# assume eglibc/glibc supports all libc features
	if [ "$LIBC_TYPE" != "uclibc" ]; then
		return 0
	fi

	# uclibc feature tests
	local inc
	local sysroot="$("$CC" "$@" -muclibc -print-sysroot 2>/dev/null)"
	for inc in "include" "usr/include" "usr/local/include"; do
		local conf="$sysroot/$inc/bits/uClibc_config.h"
		if [ -f "$conf" ]; then
			case "$feature" in
				lfs)     grep -q '__UCLIBC_HAS_LFS__ 1'     "$conf"; return $?;;
				ipv6)    grep -q '__UCLIBC_HAS_IPV6__ 1'    "$conf"; return $?;;
				rpc)     grep -q '__UCLIBC_HAS_RPC__ 1'     "$conf"; return $?;;
				locale)  grep -q '__UCLIBC_HAS_LOCALE__ 1'  "$conf"; return $?;;
				wchar)   grep -q '__UCLIBC_HAS_WCHAR__ 1'   "$conf"; return $?;;
				threads) grep -q '__UCLIBC_HAS_THREADS__ 1' "$conf"; return $?;;
			esac
		fi
	done

	return 1
}


find_libs() {
	local spec="$(echo "$LIB_SPECS" | sed -ne "s#^[[:space:]]*$1:##p")"

	if [ -n "$spec" ] && probe_cpp; then
		local libdir libdirs
		for libdir in $(
			"$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
				sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'
		); do
			if [ -d "$libdir" ]; then
				libdirs="$libdirs $(cd "$libdir"; pwd)/"
			fi
		done

		local pattern
		for pattern in $(eval echo $spec); do
			find $libdirs -name "$pattern.so*" | sort -u
		done

		return 0
	fi

	return 1
}

find_bins() {
	local spec="$(echo "$BIN_SPECS" | sed -ne "s#^[[:space:]]*$1:##p")"

	if [ -n "$spec" ] && probe_cpp; then
		local sysroot="$("$CPP" -print-sysroot)"

		local bindir bindirs
		for bindir in $(
			echo "$sysroot/bin";
			echo "$sysroot/usr/bin";
			echo "$sysroot/usr/local/bin";
			echo "$TOOLCHAIN/bin";
			echo "$TOOLCHAIN/usr/bin";
			echo "$TOOLCHAIN/usr/local/bin";
 			"$CPP" $CFLAGS -v -x c /dev/null 2>&1 | \
				sed -ne 's#:# #g; s#^COMPILER_PATH=##p'
		); do
			if [ -d "$bindir" ]; then
				bindirs="$bindirs $(cd "$bindir"; pwd)/"
			fi
		done

		local pattern
		for pattern in $(eval echo $spec); do
			find $bindirs -name "$pattern" | sort -u
		done

		return 0
	fi

	return 1
}


wrap_bins() {
	if probe_cc; then
		mkdir -p "$1" || return 1

		local cmd
		for cmd in "${CC%-*}-"*; do
			if [ -x "$cmd" ]; then
				local out="$1/${cmd##*/}"

				echo '#!/bin/sh' > "$out"
				case "${cmd##*/}" in
					*-*cc|*-*cc-*|*-*++|*-*++-*|*-cpp)
						echo -n 'exec "'"$cmd"'" '"$CFLAGS"' '         >> "$out"
						echo -n '${STAGING_DIR:+-idirafter '           >> "$out"
						echo -n '"$STAGING_DIR/usr/include" '          >> "$out"
						echo -n '-L "$STAGING_DIR/usr/lib" '           >> "$out"
						echo -n '-Wl,-rpath-link,'                     >> "$out"
						echo    '"$STAGING_DIR/usr/lib"} "$@"'         >> "$out"
					;;
					*-ld)
						echo -n 'exec "'"$cmd"'" ${STAGING_DIR:+'      >> "$out"
						echo -n '-L "$STAGING_DIR/usr/lib" '           >> "$out"
						echo -n '-rpath-link '                         >> "$out"
						echo    '"$STAGING_DIR/usr/lib"} "$@"'         >> "$out"
					;;
					*)
						echo "exec '$cmd' \"\$@\"" >> "$out"
					;;
				esac
				chmod +x "$out"
			fi
		done

		return 0
	fi

	return 1
}


probe_cc() {
	if [ -z "$CC" ]; then
		local bin
		for bin in "bin" "usr/bin" "usr/local/bin"; do
			local cmd
			for cmd in "$TOOLCHAIN/$bin/"*-*cc*; do
				if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
					CC="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
					return 0
				fi
			done
		done
		return 1
	fi
	return 0
}

probe_cxx() {
	if [ -z "$CXX" ]; then
		local bin
		for bin in "bin" "usr/bin" "usr/local/bin"; do
			local cmd
			for cmd in "$TOOLCHAIN/$bin/"*-*++*; do
				if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
					CXX="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
					return 0
				fi
			done
		done
		return 1
	fi
	return 0
}

probe_cpp() {
	if [ -z "$CPP" ]; then
		local bin
		for bin in "bin" "usr/bin" "usr/local/bin"; do
			local cmd
			for cmd in "$TOOLCHAIN/$bin/"*-cpp*; do
				if [ -x "$cmd" ] && [ ! -h "$cmd" ]; then
					CPP="$(cd "${cmd%/*}"; pwd)/${cmd##*/}"
					return 0
				fi
			done
		done
		return 1
	fi
	return 0
}

probe_libc() {
	if [ -z "$LIBC_TYPE" ]; then
		if test_uclibc; then
			LIBC_TYPE="uclibc"
		else
			LIBC_TYPE="glibc"
		fi
	fi
	return 0
}


while [ -n "$1" ]; do
	arg="$1"; shift
	case "$arg" in
		--toolchain)
			[ -d "$1" ] || {
				echo "Toolchain directory '$1' does not exist." >&2
				exit 1
			}
			TOOLCHAIN="$(cd "$1"; pwd)"; shift
		;;

		--cflags)
			CFLAGS="${CFLAGS:+$CFLAGS }$1"; shift
		;;

		--print-libc)
			if probe_cc; then
				probe_libc
				echo "$LIBC_TYPE"
				exit 0
			fi
			echo "No C compiler found in '$TOOLCHAIN'." >&2
			exit 1
		;;

		--print-target)
			if probe_cc; then
				exec "$CC" $CFLAGS -dumpmachine
			fi
			echo "No C compiler found in '$TOOLCHAIN'." >&2
			exit 1
		;;

		--print-bin)
			if [ -z "$1" ]; then
				echo "Available programs:"                      >&2
				echo $(echo "$BIN_SPECS" | sed -ne 's#:.*$##p') >&2
				exit 1
			fi

			find_bins "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-bin
			exit 0
		;;

		--print-libs)
			if [ -z "$1" ]; then
				echo "Available libraries:"                     >&2
				echo $(echo "$LIB_SPECS" | sed -ne 's#:.*$##p') >&2
				exit 1
			fi

			find_libs "$1" || exec "$0" --toolchain "$TOOLCHAIN" --print-libs
			exit 0
		;;

		--test)
			test_feature "$1"
			exit $?
		;;

		--wrap)
			[ -n "$1" ] || exec "$0" --help
			wrap_bins "$1"
			exit $?
		;;

		-h|--help)
			me="$(basename "$0")"
			echo -e "\nUsage:\n"                                            >&2
			echo -e "  $me --toolchain {directory} --print-libc"            >&2
			echo -e "    Print the libc implementation and exit.\n"         >&2
			echo -e "  $me --toolchain {directory} --print-target"          >&2
			echo -e "    Print the GNU target name and exit.\n"             >&2
			echo -e "  $me --toolchain {directory} --print-bin {program}"   >&2
			echo -e "    Print executables belonging to given program,"     >&2
			echo -e "    omit program argument to get a list of names.\n"   >&2
			echo -e "  $me --toolchain {directory} --print-libs {library}"  >&2
			echo -e "    Print shared objects belonging to given library,"  >&2
			echo -e "    omit library argument to get a list of names.\n"   >&2
			echo -e "  $me --toolchain {directory} --test {feature}"        >&2
			echo -e "    Test given feature, exit code indicates success."  >&2
			echo -e "    Possible features are 'c', 'c++', 'softfloat',"    >&2
			echo -e "    'lfs', 'rpc', 'ipv6', 'wchar', 'locale' and "      >&2
			echo -e "    'threads'.\n"                                      >&2
			echo -e "  $me --toolchain {directory} --wrap {directory}"      >&2
			echo -e "    Create wrapper scripts for C and C++ compiler, "   >&2
			echo -e "    linker, assembler and other key executables in "   >&2
			echo -e "    the directory given with --wrap.\n"                >&2
			echo -e "  $me --help"                                          >&2
			echo -e "    Display this help text and exit.\n\n"              >&2
			echo -e "  Most commands also take a --cflags parameter which " >&2
			echo -e "  is used to specify C flags to be passed to the "     >&2
			echo -e "  cross compiler when performing tests."               >&2
			echo -e "  This paremter may be repeated multiple times."       >&2
			exit 1
		;;

		*)
			echo "Unknown argument '$arg'" >&2
			exec $0 --help
		;;
	esac
done

exec $0 --help