This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build-tools.sh
executable file
·764 lines (624 loc) · 18.7 KB
/
build-tools.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#!/bin/bash
TOOLCHAIN_DIR=$(cd "`dirname \"$0\"`"; pwd)
TOP=$(cd ${TOOLCHAIN_DIR}/..; pwd)
# ====================================================================
GDBSERVER_ONLY=no
SKIP_GCC_STAGE_1=no
CLEAN_BUILD=no
DEBUG_BUILD=no
BUILD_DIR=${TOP}/build
VERILATOR_DIR=`pkg-config --variable=prefix verilator`
INSTALL_DIR=${TOP}/install
# ====================================================================
# These are deliberately left blank, defaults are filled in below as
# appropriate.
WITH_XLEN=
WITH_ARCH=
WITH_ABI=
JOBS=
LOAD=
PICORV32_BUILD_DIR=
RI5CY_BUILD_DIR=
# ====================================================================
function usage () {
MSG=$1
echo "${MSG}"
echo
echo "Usage: ./build-tools.sh [--build-dir <build_dir>]"
echo " [--install-dir <install_dir>]"
echo " [--with-xlen <xlen>]"
echo " [--clean]"
echo " [--debug]"
echo " [--picorv32-build-dir <picorv32_build_dir>]"
echo " [--ri5cy-build-dir <ri5cy_build_dir>]"
echo " [--jobs <count>] [--load <load>]"
echo " [--single-thread]"
echo " [--gdbserver-only]"
echo " [--skip-gcc-stage-1]"
echo " [--with-target <target>]"
echo " [--with-arch <arch>]"
echo " [--with-abi <abi>]"
echo
echo "--with-xlen:"
echo " Choose between 32 or 64. Default is 32."
echo ""
echo "--with-target:"
echo " Defaults are riscv32-unknown-elf or riscv64-unknown-elf"
echo " depending on the value of --with-xlen."
echo ""
echo "--with-arch:"
echo " Defaults are rv32im or rv64im dependind on the value"
echo " of --with-xlen. Add the 'c' flag for compressed, 'f'"
echo " for single precision floating point, and 'd' for double"
echo " precision floating point support. When specifiying, don't"
echo " include the 'rv32' or 'rv64' prefix, this will be added"
echo " automatically based on the value passed in --with-xlen."
echo ""
echo "--with-abi:"
echo " Only pass this if you need to override the default that"
echo " this script selects for you. The selected ABI will be"
echo " ilp32 or lp64 for 32 or 64 xlen respectively. The ABI"
echo " will be extended with the 'd' or 'f' modifier if 'd' or"
echo " 'f' is included in the --with-arch value. The 'd' is"
echo " preferred over 'f' if both are present in the arch value"
exit 1
}
XLEN_SPECIFIED=no
ARCH_SPECIFIED=no
ABI_SPECIFIED=no
TARGET_SPECIFIED=no
# Parse options
until
opt=$1
case ${opt} in
--build-dir)
shift
BUILD_DIR=$(realpath -m $1)
;;
--install-dir)
shift
INSTALL_DIR=$(realpath -m $1)
;;
--picorv32-build-dir)
shift
PICORV32_BUILD_DIR=$(realpath -m $1)
;;
--ri5cy-build-dir)
shift
RI5CY_BUILD_DIR=$(realpath -m $1)
;;
--jobs)
shift
JOBS=$1
;;
--load)
shift
LOAD=$1
;;
--single-thread)
JOBS=1
LOAD=1000
;;
--clean)
CLEAN_BUILD=yes
;;
--gdbserver-only)
GDBSERVER_ONLY=yes
;;
--debug)
DEBUG_BUILD=yes
;;
--with-xlen)
shift
WITH_XLEN=$1
XLEN_SPECIFIED=yes
;;
--with-target)
shift
TARGET_TRIPLET=$1
TARGET_SPECIFIED=yes
;;
--with-arch)
shift
WITH_ARCH=$1
ARCH_SPECIFIED=yes
;;
--with-abi)
shift
WITH_ABI=$1
ABI_SPECIFIED=yes
;;
?*)
usage "Unknown argument $1"
;;
*)
;;
esac
[ "x${opt}" = "x" ]
do
shift
done
set -u
# ====================================================================
# Select suitable defaults based on the value of --with-xlen
if [ "${XLEN_SPECIFIED}" == "no" ]
then
WITH_XLEN=32
fi
if [ "${ARCH_SPECIFIED}" == "no" ]
then
WITH_ARCH=im
else
case ${WITH_ARCH} in
rv32* | rv64*)
echo "Don't include 'rv32' or 'rv64' prefix in --with-arch value ${WITH_ARCH}"
exit 1
;;
esac
fi
if [ "${ABI_SPECIFIED}" == "no" ]
then
# The base ABI, matching 32 or 64 bit.
if [ "${WITH_XLEN}" == "32" ]
then
WITH_ABI="ilp32"
else
WITH_ABI="lp64"
fi
# Now, any floating point extensions to the ABI.
case ${WITH_ARCH} in
*d*)
WITH_ABI="${WITH_ABI}d"
;;
*f*)
WITH_ABI="${WITH_ABI}f"
;;
esac
fi
if [ "${TARGET_SPECIFIED}" == "no" ]
then
TARGET_TRIPLET=riscv${WITH_XLEN}-unknown-elf
fi
# ====================================================================
# Check that we have a valid VERILATOR_DIR value, otherwise we'll not
# spot until we try to build verilator.
if [ -z "${VERILATOR_DIR}" -o ! -d "${VERILATOR_DIR}" ]
then
echo "Failed to get header directory from verilator"
exit 1
fi
# ====================================================================
WITH_ARCH=rv${WITH_XLEN}${WITH_ARCH}
# ====================================================================
if [ -z "${PICORV32_BUILD_DIR}" ]
then
PICORV32_BUILD_DIR=${BUILD_DIR}/picorv32
fi
if [ -z "${RI5CY_BUILD_DIR}" ]
then
RI5CY_BUILD_DIR=${BUILD_DIR}/ri5cy
fi
BINUTILS_BUILD_DIR=${BUILD_DIR}/binutils
GDB_BUILD_DIR=${BUILD_DIR}/gdb
GCC_STAGE_1_BUILD_DIR=${BUILD_DIR}/gcc-stage1
GCC_STAGE_2_BUILD_DIR=${BUILD_DIR}/gcc-stage2
NEWLIB_BUILD_DIR=${BUILD_DIR}/newlib
GDBSERVER_BUILD_DIR=${BUILD_DIR}/gdbserver
DEJAGNU_BUILD_DIR=${BUILD_DIR}/dejagnu
INSTALL_PREFIX_DIR=${INSTALL_DIR}
INSTALL_SYSCONF_DIR=${INSTALL_DIR}/etc
INSTALL_LOCALSTATE_DIR=${INSTALL_DIR}/var
SYSROOT_DIR=${INSTALL_DIR}/${TARGET_TRIPLET}/sysroot
SYSROOT_HEADER_DIR=${SYSROOT_DIR}/usr
# Default parallellism
processor_count="`(echo processor; cat /proc/cpuinfo 2>/dev/null echo processor) \
| grep -c processor`"
if [ -z "${JOBS}" ]; then JOBS=${processor_count}; fi
if [ -z "${LOAD}" ]; then LOAD=${processor_count}; fi
PARALLEL="-j ${JOBS} -l ${LOAD}"
INSTALL_DIR=${INSTALL_PREFIX_DIR}
# ====================================================================
echo " Top: ${TOP}"
echo " Toolchain: ${TOOLCHAIN_DIR}"
echo " Target: ${TARGET_TRIPLET}"
echo " Xlen: ${WITH_XLEN}"
echo " Arch: ${WITH_ARCH}"
echo " ABI: ${WITH_ABI}"
echo " Debug build: ${DEBUG_BUILD}"
echo " Build Dir: ${BUILD_DIR}"
if [ -e "${PICORV32_BUILD_DIR}" ]
then
echo "PICORV32 Build Dir: ${PICORV32_BUILD_DIR}"
else
echo "PICORV32 Build Dir: N/A"
fi
if [ -e "${RI5CY_BUILD_DIR}" ]
then
echo " RI5CY Build Dir: ${RI5CY_BUILD_DIR}"
else
echo " RI5CY Build Dir: N/A"
fi
echo " Install Dir: ${INSTALL_DIR}"
if [ "x${CLEAN_BUILD}" = "xyes" ]
then
for T in `seq 5 -1 1`
do
echo -ne "\r Clean Build: yes (in ${T} seconds)"
sleep 1
done
echo -e "\r Clean Build: yes "
if [ "x${GDBSERVER_ONLY}" = "xno" ]
then
rm -fr ${BINUTILS_BUILD_DIR} ${GDB_BUILD_DIR} \
${GCC_STAGE_1_BUILD_DIR} ${NEWLIB_BUILD_DIR} \
${GCC_STAGE_2_BUILD_DIR} ${GDBSERVER_BUILD_DIR}
else
rm -fr ${GDBSERVER_BUILD_DIR}
fi
else
echo " Clean Build: no"
fi
if [ "x${DEBUG_BUILD}" = "xyes" ]
then
export CFLAGS="-g3 -O0"
export CXXFLAGS="-g3 -O0"
fi
# ====================================================================
JOB_START_TIME=
JOB_TITLE=
SCRIPT_START_TIME=`date +%s`
LOGDIR=${TOP}/logs
if ! mkdir -p ${LOGDIR}
then
echo "Failed to create log directory: ${LOGDIR}"
exit 1
fi
LOGFILE=`mktemp -p ${LOGDIR} build-$(date +%F-%H%M)-XXXX.log`
if [ ! -w "${LOGFILE}" ]
then
echo "Logfile is not writable: ${LOGFILE}"
exit 1
fi
echo " Log file: ${LOGFILE}"
echo " Start at: "`date`
echo " Parallel: ${PARALLEL}"
echo ""
if ! touch ${LOGFILE}
then
echo "Failed to initialise logfile: ${LOGFILE}"
exit 1
fi
# ====================================================================
# Defines: msg, error, times_to_time_string, job_start, job_done,
# mkdir_and_enter, enter_dir, run_command
#
# Requires LOGFILE and SCRIPT_START_TIME environment variables to be
# set.
source common.sh
# ====================================================================
# Locations of all the source
# ====================================================================
BINUTILS_SOURCE_DIR=${TOP}/binutils
GDB_SOURCE_DIR=${TOP}/gdb
GCC_SOURCE_DIR=${TOP}/gcc
NEWLIB_SOURCE_DIR=${TOP}/newlib
GDBSERVER_SOURCE_DIR=${TOP}/gdbserver
# ====================================================================
# Log git versions into the build log
# ====================================================================
job_start "Writing git versions to log file"
log_git_versions binutils "${BINUTILS_SOURCE_DIR}" \
gdb "${GDB_SOURCE_DIR}" \
gcc "${GCC_SOURCE_DIR}" \
newlib "${NEWLIB_SOURCE_DIR}" \
gdbserver "${GDBSERVER_SOURCE_DIR}"
job_done
# ====================================================================
# Build and install binutils
# ====================================================================
if [ "x${GDBSERVER_ONLY}" = "xno" ]
then
job_start "Building binutils"
mkdir_and_enter "${BINUTILS_BUILD_DIR}"
if ! run_command ${TOP}/binutils/configure \
--prefix=${INSTALL_PREFIX_DIR} \
--sysconfdir=${INSTALL_SYSCONF_DIR} \
--localstatedir=${INSTALL_LOCALSTATE_DIR} \
--disable-gtk-doc \
--disable-gtk-doc-html \
--disable-doc \
--disable-docs \
--disable-documentation \
--with-xmlto=no \
--with-fop=no \
--disable-multilib \
--target=${TARGET_TRIPLET} \
--with-sysroot=${SYSROOT_DIR} \
--enable-poison-system-directories \
--disable-tls \
--disable-gdb \
--disable-libdecnumber \
--disable-readline \
--enable-shared \
--disable-sim
then
error "Failed to configure binutils"
fi
if ! run_command make ${PARALLEL}
then
error "Failed to build binutils"
fi
if ! run_command make ${PARALLEL} install
then
error "Failed to install binutils"
fi
job_done
fi
# ====================================================================
# Build and install GDB and sim
# ====================================================================
if [ "x${GDBSERVER_ONLY}" = "xno" ]
then
job_start "Building GDB and sim"
mkdir_and_enter "${GDB_BUILD_DIR}"
if ! run_command ${TOP}/gdb/configure \
--prefix=${INSTALL_PREFIX_DIR} \
--sysconfdir=${INSTALL_SYSCONF_DIR} \
--localstatedir=${INSTALL_LOCALSTATE_DIR} \
--disable-gtk-doc \
--disable-gtk-doc-html \
--disable-doc \
--disable-docs \
--disable-documentation \
--with-xmlto=no \
--with-fop=no \
--disable-multilib \
--target=${TARGET_TRIPLET} \
--with-sysroot=${SYSROOT_DIR} \
--enable-poison-system-directories \
--disable-tls \
--disable-gprof \
--disable-ld \
--disable-gas \
--disable-binutils
then
error "Failed to configure GDB and sim"
fi
if ! run_command make ${PARALLEL}
then
error "Failed to build GDB and sim"
fi
if ! run_command make ${PARALLEL} install
then
error "Failed to install GDB and sim"
fi
job_done
fi
# ====================================================================
# Build and Install GCC (Stage 1)
# ====================================================================
if [ "x${GDBSERVER_ONLY}" = "xno" ]
then
if [ "x${SKIP_GCC_STAGE_1}" = "xno" ]
then
job_start "Building stage 1 GCC"
mkdir_and_enter ${GCC_STAGE_1_BUILD_DIR}
if ! run_command ${TOP}/gcc/configure \
--prefix="${INSTALL_PREFIX_DIR}" \
--sysconfdir="${INSTALL_SYSCONF_DIR}" \
--localstatedir="${INSTALL_LOCALSTATE_DIR}" \
--disable-shared \
--disable-static \
--disable-gtk-doc \
--disable-gtk-doc-html \
--disable-doc \
--disable-docs \
--disable-documentation \
--with-xmlto=no \
--with-fop=no \
--target=${TARGET_TRIPLET} \
--with-sysroot=${SYSROOT_DIR} \
--disable-__cxa_atexit \
--with-gnu-ld \
--disable-libssp \
--disable-multilib \
--enable-target-optspace \
--disable-libsanitizer \
--disable-tls \
--disable-libmudflap \
--disable-threads \
--disable-libquadmath \
--disable-libgomp \
--without-isl \
--without-cloog \
--disable-decimal-float \
--with-arch=${WITH_ARCH} \
--with-abi=${WITH_ABI} \
--enable-languages=c \
--without-headers \
--with-newlib \
--disable-largefile \
--disable-nls \
--enable-checking=yes
then
error "Failed to configure GCC (stage 1)"
fi
if ! run_command make ${PARALLEL} all-gcc
then
error "Failed to build GCC (stage 1)"
fi
if ! run_command make ${PARALLEL} install-gcc
then
error "Failed to install GCC (stage 1)"
fi
else
job_start "Skipping stage 1 GCC"
fi
job_done
fi
# ====================================================================
# Build and install newlib
# ====================================================================
if [ "x${GDBSERVER_ONLY}" = "xno" ]
then
job_start "Building newlib"
# Add Binutils and GCC to path to build newlib
export PATH=${INSTALL_PREFIX_DIR}/bin:$PATH
mkdir_and_enter "${NEWLIB_BUILD_DIR}"
if ! run_command ${TOP}/newlib/configure \
--prefix=${INSTALL_PREFIX_DIR} \
--sysconfdir=${INSTALL_SYSCONF_DIR} \
--localstatedir=${INSTALL_LOCALSTATE_DIR} \
--target=${TARGET_TRIPLET} \
--with-sysroot=${SYSROOT_DIR}
then
error "Failed to configure newlib"
fi
if ! run_command make ${PARALLEL}
then
error "Failed to build newlib"
fi
if ! run_command make ${PARALLEL} install
then
error "Failed to install newlib"
fi
job_done
fi
# ====================================================================
# Build and Install GCC (Stage 2)
# ====================================================================
if [ "x${GDBSERVER_ONLY}" = "xno" ]
then
job_start "Building stage 2 GCC"
mkdir_and_enter ${GCC_STAGE_2_BUILD_DIR}
if ! run_command ${TOP}/gcc/configure \
--prefix="${INSTALL_PREFIX_DIR}" \
--sysconfdir="${INSTALL_SYSCONF_DIR}" \
--localstatedir="${INSTALL_LOCALSTATE_DIR}" \
--disable-shared \
--enable-static \
--disable-gtk-doc \
--disable-gtk-doc-html \
--disable-doc \
--disable-docs \
--disable-documentation \
--with-xmlto=no \
--with-fop=no \
--target=${TARGET_TRIPLET} \
--with-sysroot=${SYSROOT_DIR} \
--disable-__cxa_atexit \
--with-gnu-ld \
--disable-libssp \
--disable-multilib \
--enable-target-optspace \
--disable-libsanitizer \
--disable-tls \
--disable-libmudflap \
--disable-threads \
--disable-libquadmath \
--disable-libgomp \
--without-isl \
--without-cloog \
--disable-decimal-float \
--with-arch=${WITH_ARCH} \
--with-abi=${WITH_ABI} \
--enable-languages=c,c++ \
--with-newlib \
--disable-largefile \
--disable-nls \
--enable-checking=yes \
--with-build-time-tools=${INSTALL_PREFIX_DIR}/${TARGET_TRIPLET}/bin
then
error "Failed to configure GCC (stage 2)"
fi
if ! run_command make ${PARALLEL} all
then
error "Failed to build GCC (stage 2)"
fi
if ! run_command make ${PARALLEL} install
then
error "Failed to install GCC (stage 2)"
fi
job_done
fi
# ====================================================================
# Build and Install DejaGNU
# ====================================================================
if [ "x${GDBSERVER_ONLY}" = "xno" ]
then
job_start "Building DejaGNU"
mkdir_and_enter ${DEJAGNU_BUILD_DIR}
if ! run_command ${TOP}/dejagnu/configure \
--prefix="${INSTALL_PREFIX_DIR}"
then
error "Failed to configure DejaGNU"
fi
if ! run_command make
then
error "Failed to build DejaGNU"
fi
if ! run_command make install
then
error "Failed to install DejaGNU"
fi
job_done
fi
# ====================================================================
# Build GDB Server for provided targets
# ====================================================================
if [ -e "${PICORV32_BUILD_DIR}" ]
then
echo "PICORV32 for GDBServer: ${PICORV32_BUILD_DIR}"
fi
if [ -e "${RI5CY_BUILD_DIR}" ]
then
echo " RI5CY for GDBServer: ${RI5CY_BUILD_DIR}"
fi
job_start "Building GDB Server for provided targets"
cd ${TOP}/gdbserver
if ! run_command autoreconf --install
then
error "Failed to autoreconf for GDB Server"
fi
mkdir_and_enter ${GDBSERVER_BUILD_DIR}
GDBSERVER_CONFIG_ARGS="\
--with-verilator-headers=${VERILATOR_DIR}/share/verilator/include \
--prefix=${INSTALL_PREFIX_DIR} \
--with-xlen=${WITH_XLEN}"
if [ -e "${RI5CY_BUILD_DIR}" ]
then
GDBSERVER_CONFIG_ARGS="${GDBSERVER_CONFIG_ARGS} \
--with-ri5cy-modeldir=${RI5CY_BUILD_DIR}/verilator-model/obj_dir \
--with-ri5cy-topmodule=top"
fi
if [ -e "${PICORV32_BUILD_DIR}" ]
then
GDBSERVER_CONFIG_ARGS="${GDBSERVER_CONFIG_ARGS} \
--with-picorv32-modeldir=${PICORV32_BUILD_DIR}/obj_dir \
--with-picorv32-topmodule=testbench"
fi
GDBSERVER_CONFIG_ARGS="${GDBSERVER_CONFIG_ARGS} \
--with-gdbsim-builddir=${GDB_BUILD_DIR}"
GDBSERVER_CONFIG_ARGS="${GDBSERVER_CONFIG_ARGS} \
--with-binutils-incdir=${INSTALL_DIR}/x86_64-pc-linux-gnu/${TARGET_TRIPLET}/include"
if ! run_command ${TOP}/gdbserver/configure ${GDBSERVER_CONFIG_ARGS}
then
error "Failed to configure GDB Server"
fi
if ! run_command make clean
then
error "Failed to make clean for GDB Server"
fi
if ! run_command make
then
error "Failed to build GDB Server"
fi
if ! run_command make install
then
error "Failed to install GDB Server"
fi
job_done
# ====================================================================
# Finished
# ====================================================================
all_finished