-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-all.sh
executable file
·570 lines (524 loc) · 15.1 KB
/
build-all.sh
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/bin/bash
# Script to build the RISC-V GNU tool chain
# Copyright (C) 2009, 2013, 2014, 2015, 2016, 2017, 2022, 2023, 2024 Embecosm Limited
# Contributor Jeremy Bennett <[email protected]>
# This file is part of the Embecosm GNU toolchain build system for RISC-V.
# SPDX-License-Identifier: GPL-3.0-or-later
set -u
usage () {
cat <<EOF
Usage ./build-all.sh : Build riscv64-unknown-linux-gnu
tool chain and QEMU (default)
[--build-qemu] : Build qemu-riscv32 and qemu-riscv64
[--build-clang] : Build Clang/LLVM
[--build-gdbserver] : Build gdbserver
[--qemu-only] : Only build qemu
[--qemu-configs] : Additional QEMU config otions
[--qemu-cflags] : CFLAGS for building QEMU (default
"-Wno-error")
[--profile-qemu] : Enable profiling by gperf
[--prefix <path>] : Install path of the tool chain.
Default path is ../install
[--arch <arch>] : Target architecture. Default
architecture is rv64gc
[--abi <abi>] : Target ABI. Default ABI is lp64d
[--tune <tune>] : Target tuning. Default tuning is
unset
[--multilib-linux [scalar | vector] Type of multilib to build.
[--disable-multilib] : Disable multilibs
[--hashes] : Print to hashes.txt the hashes of
the HEAD commits of each tool in
the tool chain
[--autoconf-version] : Version of autoconf to generate.
[--autoconf-temp-dir] : Build/install directory for autoconf.
[--clean] : Delete build directories in
riscv-gnu-toolchain and the install
directory before building
[--clean-qemu] : Clean just the QEMU build
[--help] : Print this message and exit
EOF
}
TOPDIR="$(dirname $(cd $(dirname $0) && echo $PWD))"
actmpdir=$(mktemp -d -p /tmp build-all-ac-XXXXXX)
INSTALLDIR=${TOPDIR}/install
BUILDDIR=${TOPDIR}/build
LOGDIR=${TOPDIR}/logs
DEFAULTARCH=rv64gc
DEFAULTTUNE=
DEFAULTABI=lp64d
DEFAULTTRIPLE=riscv64-unknown-elf
build_linux=true
qemu_only=false
qemu_configs=""
qemu_cflags=""
profile_qemu=""
build_gdbserver=false
build_clang=false
clean_build=false
clean_qemu_build=false
enable_multilib=true
print_help=false
print_hashes=false
autoconf_version=""
autoconf_temp_dir=""
TARGETARCH="${DEFAULTARCH}"
TARGETTUNE="${DEFAULTTUNE}"
TARGETABI="${DEFAULTABI}"
TARGETTRIPLE="${DEFAULTTRIPLE}"
# Fixed set of permitted multilibs
SCALAR_MULTILIB_LINUX="rv32gc-ilp32d rv64gc-lp64d"
VECTOR_MULTILIB_LINUX="rv32gcv-ilp32d rv64gcv-lp64d"
SCALAR_MULTILIB_OPTIONS="march=rv32gc/march=rv64gc/ mabi=ilp32d/mabi=lp64d"
VECTOR_MULTILIB_OPTIONS="march=rv32gcv/march=rv64gcv/ mabi=ilp32d/mabi=lp64d"
SCALAR_MULTILIB_DIRNAMES="rv32gc rv64gc ilp32d lp64d"
VECTOR_MULTILIB_DIRNAMES="rv32gcv rv64gcv ilp32d lp64d"
SCALAR_MULTILIB_REQUIRED="march=rv32gc/mabi=ilp32d march=rv64gc/mabi=lp64d"
VECTOR_MULTILIB_REQUIRED="march=rv32gcv/mabi=ilp32d march=rv64gcv/mabi=lp64d"
EXTRA_OPTS=""
EXTRA_LLVM_OPTS=""
EXTRA_CFLAGS=""
MULTILIB_LINUX="${SCALAR_MULTILIB_LINUX}"
MULTILIB_OPTIONS="${SCALAR_MULTILIB_OPTIONS}"
MULTILIB_DIRNAMES="${SCALAR_MULTILIB_DIRNAMES}"
MULTILIB_REQUIRED="${SCALAR_MULTILIB_REQUIRED}"
# Parse command line options
set +u
until
opt="$1"
case "${opt}" in
--prefix)
shift
INSTALLDIR="$1"
;;
--arch)
shift
TARGETARCH="$1"
;;
--tune)
shift
TARGETTUNE="$1"
;;
--abi)
shift
TARGETABI="$1"
;;
--build-elf)
build_linux=false
;;
--qemu-only)
qemu_only=true
;;
--qemu-configs)
shift
qemu_configs="$1"
;;
--qemu-cflags)
shift
qemu_cflags="$1"
;;
--profile-qemu)
profile_qemu="--enable-gprof"
;;
--build-gdbserver)
build_gdbserver=true
;;
--build-clang)
build_clang=true
;;
--disable-multilib)
enable_multilib=false
;;
--multilib-generator)
shift
MULTILIB_ELF="$1"
;;
--multilib-linux)
shift
case "x$1" in
scalar|Scalar|SCALAR)
MULTILIB_LINUX="${SCALAR_MULTILIB_LINUX}"
MULTILIB_OPTIONS="${SCALAR_MULTILIB_OPTIONS}"
MULTILIB_DIRNAMES="${SCALAR_MULTILIB_DIRNAMES}"
MULTILIB_REQUIRED="${SCALAR_MULTILIB_REQUIRED}"
;;
vector|Vector|VECTOR)
MULTILIB_LINUX="${VECTOR_MULTILIB_LINUX}"
MULTILIB_OPTIONS="${VECTOR_MULTILIB_OPTIONS}"
MULTILIB_DIRNAMES="${VECTOR_MULTILIB_DIRNAMES}"
MULTILIB_REQUIRED="${VECTOR_MULTILIB_REQUIRED}"
;;
*)
echo "Unknown Linux multilib type: $1"
;;
esac
;;
--hashes)
print_hashes=true
;;
--autoconf-version)
shift
autoconf_version="$1"
;;
--autoconf-temp-dir)
shift
autoconf_temp_dir="$1"
;;
--clean)
clean_build=true
clean_qemu_build=true
;;
--clean-qemu)
clean_qemu_build=true
;;
--help)
print_help=true
;;
?*)
echo "Unknown argument '$1'"
exit 1
;;
*)
;;
esac
[ "x${opt}" = "x" ]
do
shift
done
set -u
if ${print_help}
then
usage
exit 1
fi
echo "Logging in: ${LOGDIR}"
mkdir -p ${LOGDIR}
# Function to build a version of autoconf
# - $1: The version to build
# - $2: A directory in which to build and install
getautoconf () {
if [[ "x$1" == "x" || "x$2" == "x" ]]
then
echo "Usage: $0 <version> <tmpdir>" >&2
return 1
fi
v="$1"
d="$2"
pushd ${d}
wget http://ftp.gnu.org/gnu/autoconf/autoconf-${v}.tar.gz
tar xf autoconf-${v}.tar.gz
mkdir build
pushd build
../autoconf-${v}/configure --prefix="${d}/install"
make -j
make install
popd
popd
}
# Sanity check the autoconf version
if ! ${qemu_only}
then
if ! autoconf --version | grep -q '2.69'
then
log_file="${LOGDIR}/build-autoconf.log"
echo "Building autoconf 2.69... logging to ${log_file}"
getautoconf "2.69" ${actmpdir} > ${log_file} 2>&1
PATH=${actmpdir}/install/bin:$PATH
fi
fi
# Print the GCC and G++ used in this build
which gcc
which g++
if ${qemu_only}
then
build_linux=false
build_gdbserver=false
build_clang=false
enable_multilib=false
fi
echo
echo "Build plan:"
echo " arch: ${TARGETARCH}"
echo " tune: ${TARGETTUNE}"
echo " abi: ${TARGETABI}"
if ${build_linux}
then
echo " build linux: yes"
EXTRA_OPTS="${EXTRA_OPTS} --enable-linux"
TARGETTRIPLE="riscv64-unknown-linux-gnu"
if ${enable_multilib}
then
sed -i -r -e "s/\[AC_SUBST\(glibc_multilib_names,\"rv.*\"\)\]/\[AC_SUBST\(glibc_multilib_names,\"$MULTILIB_LINUX\"\)\]/g" $TOPDIR/riscv-gnu-toolchain/configure.ac
cd $TOPDIR/riscv-gnu-toolchain
autoconf
cd - > /dev/null 2>&1
# Hack to fix up broken multilibs
cd ${TOPDIR}/gcc/gcc/config/riscv > /dev/null 2>&1
mv t-linux-multilib t-linux-multilib.orig
touch t-linux-multilib
echo "MULTILIB_OPTIONS = ${MULTILIB_OPTIONS}" >> t-linux-multilib
echo "MULTILIB_DIRNAMES = ${MULTILIB_DIRNAMES}" >> t-linux-multilib
echo "MULTILIB_REQUIRED = ${MULTILIB_REQUIRED}" >> t-linux-multilib
cd - > /dev/null 2>&1
fi
else
echo " build linux: no"
fi
if ${enable_multilib}
then
echo " multilib: yes"
EXTRA_OPTS="${EXTRA_OPTS} --enable-multilib"
else
echo " multilib: no"
EXTRA_OPTS="${EXTRA_OPTS} --disable-multilib"
fi
echo " build qemu: yes"
echo " qemu_configs: ${qemu_configs}"
echo " qemu_cflags: ${qemu_cflags}"
if ${clean_qemu_build}
then
echo " qemu_clean: yes"
else
echo " qemu_clean: no"
fi
if ${build_gdbserver}
then
echo " build gdbserver: yes"
else
echo " build gdbserver: no"
fi
if ${build_clang}
then
echo " build Clang: yes"
else
echo " build Clang: no"
fi
cd $TOPDIR/riscv-gnu-toolchain
log_file="${LOGDIR}/clean-toolchain.log"
if ${clean_build} && ! ${qemu_only}
then
echo
echo "Cleaning... logging to ${log_file}"
# We need to configure in case we have a broken existing configuration
(
set -ex
rm -f config.status
./configure
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error configuring for cleaning, check log file!" >&2
exit 1
fi
(
make -j $(nproc) clean
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error cleaning, check log file!" >&2
exit 1
fi
rm -rf ${BUILDDIR}/*
# Don't blow away SPEC installations
rm -rf ${INSTALLDIR}/{bin,include,lib*,risc*,share,sysroot}
fi
echo
echo "Building in: $TOPDIR/riscv-gnu-toolchain/"
echo "Installing in: ${INSTALLDIR}"
if ! ${qemu_only}
then
log_file="${LOGDIR}/configure-toolchain.log"
echo "Configuring Tool Chain... logging to ${log_file}"
(
# export EXTRA_CFLAGS="${EXTRA_CFLAGS} -mavx"
set -ex
./configure \
--with-arch=$TARGETARCH --with-tune=$TARGETTUNE --with-abi=$TARGETABI \
--prefix=$INSTALLDIR \
--with-gcc-src=$TOPDIR/gcc \
--with-binutils-src=$TOPDIR/binutils \
--with-newlib-src=$TOPDIR/newlib \
--with-glibc-src=$TOPDIR/glibc \
--with-gdb-src=$TOPDIR/gdb \
--with-cmodel=medany \
${EXTRA_OPTS}
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error configuring, check log file!" >&2
exit 1
fi
fi
if ! ${qemu_only}
then
log_file="${LOGDIR}/build-toolchain.log"
echo "Building Tool Chain... logging to ${log_file}"
(
make -j $(nproc)
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error building, check log file!" >&2
exit 1
fi
fi
# No need to configure the QEMU targets as the default ones are already
# riscv32-linux-user and riscv64-linux-user.
log_file="${LOGDIR}/build-qemu.log"
echo "Building QEMU... logging to ${log_file}"
(
mkdir -p ${BUILDDIR}/qemu
cd ${BUILDDIR}/qemu
$TOPDIR/qemu/configure --prefix=$INSTALLDIR \
--target-list=riscv64-linux-user,riscv32-linux-user \
--interp-prefix=$INSTALLDIR/sysroot \
--python=python3 ${profile_qemu} \
${qemu_configs} \
--extra-cflags="${qemu_cflags}"
if ${clean_build} || ${clean_qemu_build}
then
rm -f ${INSTALLDIR}/bin/qemu-riscv??
make clean
fi
make -j $(nproc)
make install
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error building, check log file!" >&2
exit 1
fi
# Clang
if ${build_clang}
then
mkdir -p ${LOGDIR}
log_file="${LOGDIR}/configure-clang.log"
echo "Configuring Clang... logging to ${log_file}"
(
# Attempt to identify the host architecture, and include this in the
# build
if [ "$(arch)" == "arm64" ]; then
LLVM_NATIVE_ARCH="AArch64"
else
LLVM_NATIVE_ARCH="X86"
fi
# Location of the binutils repository in order to pass the linker
# plugin header to LLVM's build system
BINUTILS_DIR="${TOPDIR}/binutils"
# Build and install it
set -e
mkdir -p ${BUILDDIR}/llvm
set -x
cd ${BUILDDIR}/llvm
cmake -G"Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${INSTALLDIR} \
-DLLVM_ENABLE_PROJECTS=clang\;lld \
-DLLVM_ENABLE_PLUGINS=ON \
-DLLVM_BINUTILS_INCDIR=${BINUTILS_DIR}/include \
-DLLVM_DISTRIBUTION_COMPONENTS=clang\;clang-resource-headers\;lld\;llvm-ar\;llvm-cov\;llvm-cxxfilt\;llvm-dwp\;llvm-ranlib\;llvm-nm\;llvm-objcopy\;llvm-objdump\;llvm-readobj\;llvm-size\;llvm-strings\;llvm-strip\;llvm-profdata\;llvm-symbolizer\;LLVMgold \
-DLLVM_PARALLEL_LINK_JOBS=5 \
-DLLVM_TARGETS_TO_BUILD=${LLVM_NATIVE_ARCH}\;RISCV \
-DDEFAULT_SYSROOT=${INSTALLDIR}/sysroot \
${EXTRA_LLVM_OPTS} \
${TOPDIR}/llvm-project/llvm
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error configuring Clang, check log file!" >&2
exit 1
fi
log_file="${LOGDIR}/build-clang.log"
echo "Building Clang... logging to ${log_file}"
(
cd ${BUILDDIR}/llvm
make -j$(nproc)
make install-distribution
# Add symlinks to LLVM tools
cd ${INSTALLDIR}/bin
for TOOL in clang clang++; do
ln -sv clang riscv64-unknown-linux-gnu-${TOOL}
done
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error building Clang, check log file!" >&2
exit 1
fi
fi
PATH=${INSTALLDIR}/bin:$PATH
if ${build_gdbserver}
then
log_file="${LOGDIR}/configure-gdbserver.log"
echo "Configuring gdbserver... logging to ${log_file}"
(
set -e
mkdir -p ${BUILDDIR}/gdbserver
set -x
cd ${BUILDDIR}/gdbserver
../../gdb/configure \
CC="${TARGETTRIPLE}-gcc -static" \
CXX="${TARGETTRIPLE}-g++ -static" \
--prefix=${INSTALLDIR} \
--cache-file=/dev/null \
--disable-bfd \
--disable-binutils \
--disable-gas \
--disable-gdb \
--disable-gold \
--disable-gprof \
--disable-ld \
--disable-libctf \
--disable-libdecnumber \
--disable-opcodes \
--disable-readline \
--disable-sim \
--host=${TARGETTRIPLE} \
${EXTRA_OPTS}
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error configuring gdbserver, check log file!" >&2
exit 1
fi
log_file="${LOGDIR}/build-gdbserver.log"
echo "Building gdbserver... logging to ${log_file}"
(
cd ${BUILDDIR}/gdbserver
make -j $(nproc)
make install
) > ${log_file} 2>&1
if [ $? -ne 0 ]; then
echo "Error building gdbserver, check log file!" >&2
exit 1
fi
fi
if $print_hashes
then
hashes_file="$TOPDIR/hashes.txt"
echo "Printing hashes to $hashes_file"
cd $TOPDIR/binutils
BINUTILS_HASH=$(git rev-parse HEAD)
cd $TOPDIR/gdb
GDB_HASH=$(git rev-parse HEAD)
cd $TOPDIR/gcc
GCC_HASH=$(git rev-parse HEAD)
cd $TOPDIR/newlib
NEWLIB_HASH=$(git rev-parse HEAD)
cd $TOPDIR/glibc
GLIBC_HASH=$(git rev-parse HEAD)
cd $TOPDIR
echo "binutils: $BINUTILS_HASH" >> ${hashes_file}
echo "gdb: $GDB_HASH" >> ${hashes_file}
echo "gcc: $GCC_HASH" >> ${hashes_file}
echo "newlib: $NEWLIB_HASH" >> ${hashes_file}
echo "glibc: $GLIBC_HASH" >> ${hashes_file}
fi
echo "Cleaning up"
pushd $TOPDIR/riscv-gnu-toolchain > /dev/null 2>&1
if ! git checkout configure configure.ac > /dev/null 2>&1
then
echo "Unable to restore configure and configure.ac in riscv-gnu-toolchain"
echo "- manual restoration recommended."
fi
popd > /dev/null 2>&1
cd ${TOPDIR}/gcc/gcc/config/riscv > /dev/null 2>&1
if [[ -e t-linux-multilib.orig ]]
then
rm -f t-linux-multilib
mv t-linux-multilib.orig t-linux-multilib
fi
cd - > /dev/null 2>&1
rm -rf ${actmpdir}
echo "Build completed successfully."