summaryrefslogtreecommitdiffstats
path: root/util/xcompile/xcompile
blob: e498cc08293afb09ef71559dd9f84c9f38eb99f1 (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
#!/bin/bash
#
# This file is part of the coreboot project.
#
# Copyright (C) 2007-2010 coresystems GmbH
# Copyright (C) 2012 Google Inc
#
# 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; version 2 of the License.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

TMPFILE=""
XGCCPATH=${1:-"`pwd`/util/crossgcc/xgcc/bin/"}

die() {
	echo "ERROR: $*" >&2
	exit 1
}

clean_up() {
	if [ -n "$TMPFILE" ]; then
		rm -f "$TMPFILE" "$TMPFILE.c" "$TMPFILE.o"
	fi
}

# Create temporary file(s).
TMPFILE="$(mktemp /tmp/temp.XXXX 2>/dev/null || echo /tmp/temp.78gOIUGz)"
touch "$TMPFILE"
trap clean_up EXIT


program_exists() {
	type "$1" >/dev/null 2>&1
}


if [ "$(${XGCCPATH}/iasl 2>/dev/null | grep -c ACPI)" -gt 0 ]; then
	IASL=${XGCCPATH}iasl
elif [ "$(iasl 2>/dev/null | grep -c ACPI)" -gt 0 ]; then
	IASL=iasl
else
	die "no iasl found"
fi

if program_exists gcc; then
	HOSTCC=gcc
elif program_exists cc; then
	HOSTCC=cc
else
	die "no host compiler found"
fi

cat <<EOF
# platform agnostic and host tools
IASL:=${IASL}
HOSTCC:=${HOSTCC}

EOF

testcc() {
	local tmp_c="$TMPFILE.c"
	local tmp_o="$TMPFILE.o"
	rm -f "$tmp_c" "$tmp_o"
	echo "void _start(void) {}" >"$tmp_c"
	"$1" -nostdlib -Werror $2 "$tmp_c" -o "$tmp_o" >/dev/null 2>&1
}

testas() {
	local gccprefixes="$1"
	local twidth="$2"
	local arch="$3"
	local use_dash_twidth="$4"
	local obj_file="$TMPFILE.o"
	local full_arch="elf$twidth-$arch"

	rm -f "$obj_file"
	[ -n "$use_dash_twidth" ] && use_dash_twidth="--$twidth"
	${gccprefixes}as $use_dash_twidth -o "$obj_file" $TMPFILE 2>/dev/null ||
		return 1

	# Check output content type.
	local obj_type="$(${gccprefixes}objdump -p $obj_file)"
	local obj_arch="$(expr "$obj_type" : '.*format \(.[a-z0-9-]*\)')"
	[ "$obj_arch" = "$full_arch" ] || return 1

	# Architecture matched.
	GCCPREFIX="$gccprefixes"

	if [ -z "$use_dash_twidth" ]; then
		ASFLAGS=""
		CFLAGS=""
		LDFLAGS=""
	else
		ASFLAGS="--$twidth"
		CFLAGS="-m$twidth"
		LDFLAGS="-b $full_arch"

	fi

	# Special parameters only available in dash_twidth mode.
	[ -n "$use_dash_twidth" ] && case "$full_arch" in
		"elf32-i386" )
			LDFLAGS="$LDFLAGS -melf_i386"
			CFLAGS="$CFLAGS -Wl,-b,elf32-i386 -Wl,-melf_i386"
			;;
	esac

	return 0
}

detect_special_flags() {
	local architecture="$1"
	# GCC 4.6 is much more picky about unused variables.
	# Turn off it's warnings for now:
	testcc "$CC"   "$CFLAGS -Wno-unused-but-set-variable " &&
		CFLAGS="$CFLAGS -Wno-unused-but-set-variable "

	# Use bfd linker instead of gold if available:
	testcc "$CC"   "$CFLAGS -fuse-ld=bfd" &&
		CFLAGS="$CFLAGS -fuse-ld=bfd" && LINKER_SUFFIX='.bfd'

	testcc "$CC"   "$CFLAGS -fno-stack-protector"&&
		CFLAGS="$CFLAGS -fno-stack-protector"
	testcc "$CC"   "$CFLAGS -Wl,--build-id=none" &&
		CFLAGS="$CFLAGS -Wl,--build-id=none"

	case "$architecture" in
	x86)
		testcc "$CC"   "$CFLAGS -Wa,--divide" &&
			CFLAGS="$CFLAGS -Wa,--divide"
		# Always build for i686 -- no sse/mmx instructions since SMM
		# modules are compiled using these flags. Note that this
		# doesn't prevent a project using xcompile to explicitly
		# specify -mmsse, etc flags.
		CFLAGS="$CFLAGS  -march=i686"
		;;
	esac
}

report_arch_toolchain() {
	cat <<EOF
# elf${TWIDTH}-${TBFDARCH} toolchain (${GCCPREFIX}gcc)
ARCH_SUPPORTED+=${TARCH}
CC_${TARCH}:=${GCCPREFIX}gcc
CFLAGS_${TARCH}:=${CFLAGS}
CPP_${TARCH}:=${GCCPREFIX}cpp
AS_${TARCH}:=${GCCPREFIX}as ${ASFLAGS}
LD_${TARCH}:=${GCCPREFIX}ld${LINKER_SUFFIX} ${LDFLAGS}
NM_${TARCH}:=${GCCPREFIX}nm
OBJCOPY_${TARCH}:=${GCCPREFIX}objcopy
OBJDUMP_${TARCH}:=${GCCPREFIX}objdump
READELF_${TARCH}:=${GCCPREFIX}readelf
STRIP_${TARCH}:=${GCCPREFIX}strip
AR_${TARCH}:=${GCCPREFIX}ar

EOF
}

# Architecture definition
SUPPORTED_ARCHITECTURE="x86 arm aarch64 riscv"

arch_config_arm() {
	TARCH="arm"
	TBFDARCH="littlearm"
	TCLIST="armv7a armv7-a"
	TWIDTH="32"
	TABI="eabi"
}

arch_config_aarch64() {
	TARCH="aarch64"
	TBFDARCH="littleaarch64"
	TCLIST="aarch64"
	TWIDTH="64"
	TABI="eabi"
}

arch_config_riscv() {
	TARCH="riscv"
	TBFDARCH="littleriscv"
	TCLIST="riscv"
	TWIDTH="64"
	TABI="elf"
}

arch_config_x86() {
	TARCH="x86_32"
	TBFDARCH="i386"
	TCLIST="i386 x86_64"
	TWIDTH="32"
	TABI="elf"
}

test_architecture() {
	architecture=$1

	GCCPREFIX="invalid"
	if type arch_config_$architecture > /dev/null; then
		arch_config_$architecture
	else
		die "no architecture definition for $architecture"
	fi

	# To override toolchain, define CROSS_COMPILE_$arch or CROSS_COMPILE as
	# environment variable.
	# Ex: CROSS_COMPILE_arm="armv7a-cros-linux-gnueabi-"
	#     CROSS_COMPILE_x86="i686-pc-linux-gnu-"
	search="$(eval echo \$CROSS_COMPILE_$architecture 2>/dev/null)"
	search="$search $CROSS_COMPILE"
	for toolchain in $TCLIST; do
		search="$search $XGCCPATH$toolchain-$TABI-"
		search="$search $toolchain-$TABI-"
		search="$search $toolchain-linux-gnu-"
		search="$search $toolchain-"
	done
	echo "# $architecture TARCH_SEARCH=$search"

	# Search toolchain by checking assembler capability.
	for gccprefixes in $search ""; do
		program_exists "${gccprefixes}as" || continue
		testas "$gccprefixes" "$TWIDTH" "$TBFDARCH" "" && break
		testas "$gccprefixes" "$TWIDTH" "$TBFDARCH" "TRUE" && break
	done

	if [ "$GCCPREFIX" = "invalid" ]; then
		echo "Warning: no suitable GCC for $architecture." >&2
		continue
	fi
	CC="${GCCPREFIX}"gcc

	detect_special_flags "$architecture"
	report_arch_toolchain
}

# This loops over all supported architectures.
for architecture in $SUPPORTED_ARCHITECTURE; do
	test_architecture $architecture
done