-
Notifications
You must be signed in to change notification settings - Fork 84
/
build
executable file
·722 lines (616 loc) · 23.6 KB
/
build
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
#!/usr/bin/env bash
# shellcheck disable=SC1117
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2016-2018 USBhost
# Copyright (C) 2016-2017 Joe Maples
# Copyright (C) 2017-2018 Nathan Chancellor
#
# GCC cross compiler compilation script
###########
# SOURCES #
###########
# http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/ # credits for almost everything
# http://www.mpfr.org/
# https://gmplib.org/
# http://www.multiprecision.org/
# http://isl.gforge.inria.fr/
# https://www.gnu.org/software/binutils/
# https://www.gnu.org/software/libc/
# https://www.kernel.org/
# https://git.linaro.org/toolchain/gcc.git # linaro and gnu
#############
# FUNCTIONS #
#############
# Easy alias for escape codes
function echo() {
command echo -e "${@}"
}
# Help menu function
function help_menu() {
echo
echo "${BOLD}OVERVIEW:${RST} Build a gcc toolchain"
echo
echo "${BOLD}USAGE:${RST} ${0} <options>"
echo
echo "${BOLD}EXAMPLE:${RST} ${0} -a arm64 -s linaro -v 8"
echo
echo "${BOLD}EXAMPLE:${RST} ${0} -a x86_64 -s gnu -v 8"
echo
echo "${BOLD}REQUIRED PARAMETERS:${RST}"
echo " -a | --arch: Possible values: arm, arm64, i686, or x86_64. This is the toolchain's target architecture."
echo " -s | --source: Possible values: gnu or linaro. This is the GCC source (GNU official vs. Linaro fork)."
echo " -v | --version: Possible values: (4, 5, 6, 7, 8, 9*, and 10* [*GNU only]). This is the GCC version to build."
echo
echo "${BOLD}OPTIONAL PARAMETERS:${RST}"
echo " -f | --full-src: Download full git repos instead of shallow clones"
echo " -j | --jobs: Specify the amount of threads to use. This overrides the default detection"
echo " -nt | --no-tmpfs: Do not use tmpfs for building (useful if you don't have much RAM)."
echo " -nu | --no-update: Do not update the downloaded components before building (useful if you have slow internet)."
echo " -p | --package: Possible values: gz or xz. Compresses toolchain after build."
echo " -t | --tarballs: Use tarballs for binutils, ISL, and, GCC"
echo " -V | --verbose: Make script print all output, not just errors and the ending information"
echo
}
# Prints a formatted header to let the user know what's being done
function header() {
[[ "${*}" =~ "--no-first-echo" ]] || echo
# shellcheck disable=SC2034
echo "${RED}====$(for i in $(seq ${#1}); do echo "=\c"; done)===="
echo "== ${1} =="
# shellcheck disable=SC2034
echo "====$(for i in $(seq ${#1}); do echo "=\c"; done)====${RST}"
[[ "${*}" =~ "--no-second-echo" ]] || echo
}
# Prints an error in bold red
function die() {
[[ -z ${VERBOSE} ]] && exec 1>&5 2>&6
echo ""
echo "${RED}${1}${RST}"
[[ "${*}" =~ "-n" ]] && echo
[[ "${*}" =~ "-h" ]] && help_menu
exit
}
# Prints a warning in bold yellow
function warn() {
echo ""
echo "${YLW}${1}${RST}"
[[ "${*}" =~ "-n" ]] && echo
}
# Formats the time for the end
function format_time() {
MINS=$(((${2} - ${1}) / 60))
SECS=$(((${2} - ${1}) % 60))
if [[ ${MINS} -ge 60 ]]; then
HOURS=$((MINS / 60))
MINS=$((MINS % 60))
fi
if [[ ${HOURS} -eq 1 ]]; then
TIME_STRING+="1 HOUR, "
elif [[ ${HOURS} -ge 2 ]]; then
TIME_STRING+="${HOURS} HOURS, "
fi
if [[ ${MINS} -eq 1 ]]; then
TIME_STRING+="1 MINUTE"
else
TIME_STRING+="${MINS} MINUTES"
fi
if [[ ${SECS} -eq 1 && -n ${HOURS} ]]; then
TIME_STRING+=", AND 1 SECOND"
elif [[ ${SECS} -eq 1 && -z ${HOURS} ]]; then
TIME_STRING+=" AND 1 SECOND"
elif [[ ${SECS} -ne 1 && -n ${HOURS} ]]; then
TIME_STRING+=", AND ${SECS} SECONDS"
elif [[ ${SECS} -ne 1 && -z ${HOURS} ]]; then
TIME_STRING+=" AND ${SECS} SECONDS"
fi
echo "${TIME_STRING}"
}
# Check if user needs to enter sudo password or not
function check_sudo() {
echo
echo "Checking if sudo is available, please enter your password if a prompt appears!"
if ! sudo -v 2>/dev/null; then
warn "Sudo is not available! Disabling the option for tmpfs..." -n
NO_TMPFS=true
fi
}
# Unmount tmpfs
function unmount_tmpfs() {
if [[ -z ${NO_TMPFS} ]]; then
sudo umount -f build-glibc 2>/dev/null
sudo umount -f build-gcc 2>/dev/null
sudo umount -f build-binutils 2>/dev/null
fi
}
# git clone wrapper
function git_clone() {
git clone ${DEPTH_FLAG:+"--depth=1"} "${@}"
}
# git fetch wrapper
function git_fetch() {
git fetch ${DEPTH_FLAG:+"--depth=1"} "${@}"
}
# Initial setup
function setup_variables() {
# Colors
BOLD="\033[1m"
RED="\033[01;31m"
RST="\033[0m"
YLW="\033[01;33m"
# Configuration variables
CONFIGURATION=( "--disable-multilib" "--disable-werror" )
JOBS="-j$(($(nproc --all) + 1))"
DEPTH_FLAG=true
# Binary versions
BINUTILS_git="binutils-2_34-branch"
BINUTILS_tar="2.34"
GMP="gmp-6.2.0"
MPFR="mpfr-4.1.0"
MPC="mpc-1.2.0"
ISL="isl-0.22.1"
GLIBC="glibc-2.32"
LINUX="5.4.61"
# Start of script
START=$(date +%s)
}
# Parse parameters
function parse_parameters() {
while [[ ${#} -ge 1 ]]; do
case "${1}" in
# REQUIRED FLAGS
"-a"|"--arch") shift && ARCH=${1} ;;
"-s"|"--source") shift && SOURCE=${1} ;;
"-v"|"--version") shift && VERSION=${1} ;;
# OPTIONAL FLAGS
"-f"|"--full-src") DEPTH_FLAG=false ;;
"-j"|"--jobs") shift && JOBS=-j${1} ;;
"-nt"|"--no-tmpfs") NO_TMPFS=true ;;
"-nu"|"--no-update") NO_UPDATE=true ;;
"-p"|"--package") shift && COMPRESSION=${1} ;;
"-t"|"--tarballs") TARBALLS=true ;;
"-V"|"--verbose") VERBOSE=true ;;
# HELP!
"-h"|"--help") help_menu; exit ;;
esac
shift
done
[[ -z ${NO_TMPFS} ]] && check_sudo
[[ -z ${VERBOSE} ]] && exec 6>&2 5>&1 &>/dev/null
# Default values
case "${ARCH}" in
"arm") TARGET="arm-linux-gnueabi" ;;
"arm64") TARGET="aarch64-linux-gnu" ;;
"i686") TARGET="i686-linux-gnu" ;;
"x86_64") [[ ${VERSION} -le 5 ]] && die "Will not build, Use newer version instead" -n
TARGET="x86_64-linux-gnu" ;;
*) die "Absent or invalid arch specified!" -h ;;
esac
# Kernel architecture; i686 and x86_64 targets use x86
[[ ${ARCH} = "i686" || ${ARCH} = "x86_64" ]] && KERNEL_ARCH=x86 || KERNEL_ARCH=${ARCH}
if [[ -z ${TARBALLS} ]]; then
# Set GCC branch based on version and Linaro or not
case "${SOURCE}:${VERSION}" in
"gnu:4") GCC=gcc-4_9-branch
BINUTILS_git="binutils-2_29-branch"
GLIBC="glibc-2.26"
ISL="isl-0.17.1" ;;
"gnu:5") GCC=gcc-5-branch
GLIBC="glibc-2.27"
ISL="isl-0.17.1" ;;
"gnu:6") GCC=gcc-6-branch ;;
"gnu:7") GCC=gcc-7-branch ;;
"gnu:8") GCC=gcc-8-branch ;;
"gnu:9") GCC=gcc-9-branch ;;
"gnu:10") GCC=master ;;
"linaro:4") GCC=linaro-local/releases/linaro-4.9-2017.01
GLIBC="glibc-2.27"
ISL="isl-0.17.1" ;;
"linaro:5") GCC=linaro-local/gcc-5-integration-branch
GLIBC="glibc-2.27"
ISL="isl-0.17.1" ;;
"linaro:6") GCC=linaro-local/gcc-6-integration-branch ;;
"linaro:7") GCC=linaro-local/gcc-7-integration-branch ;;
# ARM has taken the responsibility from Linaro since 8.x
"linaro:8") GCC=linaro-local/ARM/arm-8-branch ;;
"linaro:9") die "There's no such thing as Linaro 9.x Clannad..." -h ;;
"linaro:10") die "There's no such thing as Linaro 10.x Clannad..." -h ;;
*) die "Absent or invalid GCC version or source specified!" -h ;;
esac
else
# Set GCC branch based on version and Linaro or not
case "${SOURCE}:${VERSION}" in
"gnu:4") GCC=gcc-4.9.4
BINUTILS_tar="2.29.1"
GLIBC="glibc-2.26"
ISL="isl-0.17.1"
EXT=gz ;;
"gnu:5") GCC=gcc-5.5.0
GLIBC="glibc-2.27"
ISL="isl-0.17.1" ;;
"gnu:6") GCC=gcc-6.5.0 ;;
"gnu:7") GCC=gcc-7.5.0 ;;
"gnu:8") GCC=gcc-8.4.0 ;;
"gnu:9") GCC=gcc-9.3.0 ;;
"gnu:10") die "GCC 10.0 is currently a WIP so there is no tarball to download! Either use the git repo or choose a new version..." ;;
"linaro:4") GCC=linaro-4.9-2017.01
GLIBC="glibc-2.27"
ISL="isl-0.17.1" ;;
"linaro:5") GCC=linaro-5.5-2017.10
GLIBC="glibc-2.27"
ISL="isl-0.17.1" ;;
"linaro:6") GCC=linaro-snapshot-6.5-2018.11 ;;
"linaro:7") GCC=linaro-snapshot-7.5-2019.11 ;;
# ARM has taken the responsibility from Linaro since 8.x
# See later in this script why it's defined like this
"linaro:8") GCC=8.3-2019.03 ;;
"linaro:9") die "There's no such thing as Linaro 9.x Clannad..." -h ;;
"linaro:10") die "There's no such thing as Linaro 10.x Clannad..." -h ;;
*) die "Absent or invalid GCC version or source specified!" -h ;;
esac
fi
}
# Clean up from a previous compilation
function clean_up() { #FIXME: we dont need to remove everything everytime.
header "CLEANING UP"
unmount_tmpfs
git clean -fxdq -e sources -e prebuilts -e patches
find . -maxdepth 1 -type l -exec rm -rf {} \; #FIXME
if [[ -d binutils ]] ||
[[ -d build-binutils ]] ||
[[ -d build-gcc ]] ||
[[ -d build-glibc ]] ||
[[ -d gcc ]] ||
[[ -d linux ]] ||
[[ -f ${TARGET} ]] ||
[[ $(for FILE in *.tar.*; do if [[ -f "${FILE}" ]]; then echo "true"; break; else echo "false"; fi done) = "true" ]]; then
die "Clean up failed! Aborting. Try checking that you have proper permissions to delete files."
else
echo "Clean up successful!"
fi
}
function build_binaries() {
ROOT=${PWD}
PREBUILTS_BIN=${ROOT}/prebuilts/bin
mkdir -p sources
if [[ ! -f ${PREBUILTS_BIN}/txt2man ]]; then
TXT2MAN=${ROOT}/sources/txt2man
[[ ! -d ${TXT2MAN} ]] && git -C "$(dirname "${TXT2MAN}")" clone --depth=1 https://github.com/mvertes/txt2man
git -C "${TXT2MAN}" clean -fxdq
git -C "${TXT2MAN}" pull
(
cd "${TXT2MAN}" || die "Issue with cloning txt2man source!"
make prefix="$(dirname "${PREBUILTS_BIN}")" install || die "Error installing txt2man!"
)
fi
export PATH=${PREBUILTS_BIN}:${PATH}
if [[ ! -f ${PREBUILTS_BIN}/axel ]]; then
AXEL=${ROOT}/sources/axel
[[ ! -d ${AXEL} ]] && git -C "$(dirname "${AXEL}")" clone --depth=1 https://github.com/axel-download-accelerator/axel
git -C "${AXEL}" clean -fxdq
git -C "${AXEL}" pull
(
cd "${AXEL}" || die "Issue with cloning axel source!"
autoreconf -fiv
./configure --prefix="$(dirname "${PREBUILTS_BIN}")"
make "${JOBS}" || die "Error building axel!"
make "${JOBS}" install || die "Error installing axel!"
)
fi
if [[ ! -f ${PREBUILTS_BIN}/pigz ]]; then
PIGZ=${ROOT}/sources/pigz
[[ ! -d ${PIGZ} ]] && git -C "$(dirname "${PIGZ}")" clone --depth=1 https://github.com/madler/pigz
git -C "${PIGZ}" clean -fxdq
git -C "${PIGZ}" pull
make -C "${PIGZ}" "${JOBS}" pigz || die "Error building pigz!"
mv "${PIGZ}"/pigz "${PREBUILTS_BIN}"
fi
if [[ ! -f ${PREBUILTS_BIN}/pxz ]]; then
PXZ=${ROOT}/sources/pxz
[[ ! -d ${PXZ} ]] && git -C "$(dirname "${PXZ}")" clone --depth=1 https://github.com/krasCGQ/pxz
git -C "${PXZ}" clean -fxdq
git -C "${PXZ}" pull
make -C "${PXZ}" "${JOBS}" pxz || die "Error building pxz!"
mv "${PXZ}"/pxz "${PREBUILTS_BIN}"
fi
}
function download_sources() {
cd sources || die "Failed to create sources directory!"
if [[ ! -f ${MPFR}.tar.xz ]]; then
header "DOWNLOADING MPFR"
axel https://www.mpfr.org/mpfr-current/${MPFR}.tar.xz
fi
if [[ ! -f ${GMP}.tar.xz ]]; then
header "DOWNLOADING GMP"
axel https://ftp.gnu.org/gnu/gmp/${GMP}.tar.xz
fi
if [[ ! -f ${MPC}.tar.gz ]]; then
header "DOWNLOADING MPC"
axel https://ftp.gnu.org/gnu/mpc/${MPC}.tar.gz
fi
if [[ ! -f ${GLIBC}.tar.xz ]]; then
header "DOWNLOADING GLIBC ${GLIBC} FOR GCC ${VERSION}"
axel https://ftp.gnu.org/gnu/glibc/${GLIBC}.tar.xz
fi
if [[ ! -f linux-${LINUX}.tar.xz ]]; then
header "DOWNLOADING LINUX KERNEL"
axel https://cdn.kernel.org/pub/linux/kernel/v${LINUX/.*}.x/linux-${LINUX}.tar.xz
fi
if [[ -z ${TARBALLS} ]]; then
if [[ ! -d binutils ]]; then
header "DOWNLOADING BINUTILS"
git_clone https://git.linaro.org/toolchain/binutils-gdb.git binutils -b ${BINUTILS_git}
fi
if [[ ! -d isl ]]; then
header "DOWNLOADING ISL"
# don't use git_clone
git clone git://repo.or.cz/isl.git
fi
if [[ ! -d gcc ]]; then
header "DOWNLOADING GCC"
git_clone https://git.linaro.org/toolchain/gcc.git -b ${GCC}
fi
else
if [[ ! -f binutils-${BINUTILS_tar}.tar.xz ]]; then
header "DOWNLOADING BINUTILS ${BINUTILS_tar} FOR GCC ${VERSION}"
axel https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_tar}.tar.xz
fi
if [[ ! -f ${ISL}.tar.xz ]]; then
header "DOWNLOADING ISL ${ISL} FOR GCC ${VERSION}"
axel http://isl.gforge.inria.fr/${ISL}.tar.xz
fi
# GNU, any version
if [[ ${SOURCE} = "gnu" ]]; then
if [[ ! -f ${GCC}.tar.${EXT:-xz} ]]; then
header "DOWNLOADING GCC"
axel https://mirrors.kernel.org/gnu/gcc/${GCC}/${GCC}.tar.${EXT:-xz}
fi
GCC_TAR=${GCC}.tar.${EXT:-xz}
# Linaro, 8.x and newer
elif [[ ${VERSION} -ge 8 ]]; then
if [[ ! -f gcc-arm-src-snapshot-${GCC}.tar.xz ]]; then
header "DOWNLOADING GCC"
axel https://developer.arm.com/-/media/Files/downloads/gnu-a/${GCC}/srcrel/gcc-arm-src-snapshot-${GCC}.tar.xz
fi
GCC_TAR=gcc-arm-src-snapshot-${GCC}.tar.xz
# Linaro, 7.x and older
else
if [[ ! -f gcc-${GCC}.tar.gz ]]; then
header "DOWNLOADING GCC"
axel https://git.linaro.org/toolchain/gcc.git/snapshot/gcc-${GCC}.tar.gz
fi
GCC_TAR=gcc-${GCC}.tar.gz
fi
fi
}
function extract() {
case "${1}" in
*.gz) UNPACK=pigz ;;
*.xz) UNPACK=pxz ;;
esac
mkdir -p "${2}"
${UNPACK} -d < "${1}" | tar -xC "${2}" --strip-components=1
}
# Extract tarballs to their proper locations
function extract_sources() {
header "EXTRACTING DOWNLOADED TARBALLS"
extract ${MPFR}.tar.xz ../${MPFR}
extract ${GMP}.tar.xz ../${GMP}
extract ${MPC}.tar.gz ../${MPC}
extract ${GLIBC}.tar.xz ../${GLIBC}
extract linux-${LINUX}.tar.xz ../linux
if [[ -n ${TARBALLS} ]]; then
extract binutils-${BINUTILS_tar}.tar.xz ../binutils
extract ${ISL}.tar.xz ../${ISL}
if [[ ${SOURCE} = "linaro" && ${VERSION} -ge 8 ]]; then
# We can't use extract function here as this ships other GNU tools
# Only extract GCC source and later rename it to gcc
pxz -d < ${GCC_TAR} | tar -xC .. gcc-arm-src-snapshot-${GCC}
mv -f ../gcc-arm-src-snapshot-${GCC} ../gcc
else
extract ${GCC_TAR} ../gcc
fi
fi
}
# Update git repos
function update_repos() {
if [[ -z ${NO_UPDATE} && -z ${TARBALLS} ]]; then
header "UPDATING SOURCES"
(
cd isl || die "ISL did not get cloned properly!"
git remote update
git checkout -f ${ISL}
./autogen.sh
)
(
cd binutils || die "binutils did not get cloned properly!"
[[ -n ${FULL_SOURCE} ]] && BRANCH=origin/${BINUTILS_git} || BRANCH=FETCH_HEAD
git_fetch origin ${BINUTILS_git}
git checkout -f ${BINUTILS_git} || git checkout -f -b ${BINUTILS_git} ${BRANCH}
)
(
cd gcc || die "GCC did not get cloned properly!"
[[ -n ${FULL_SOURCE} ]] && BRANCH=origin/${GCC} || BRANCH=FETCH_HEAD
git_fetch origin ${GCC}
git checkout -f ${GCC} || git checkout -f -b ${GCC} ${BRANCH}
)
else
if [[ -d isl ]]; then
(
cd isl || die "ISL did not get downloaded properly!"
./autogen.sh
)
fi
fi
cd ..
[[ ! -d binutils ]] && ln -s sources/binutils binutils
[[ ! -d gcc ]] && ln -s sources/gcc gcc
}
# Setup source folders and build folders
function setup_env() {
INSTALL=${ROOT}/${TARGET}
export PATH=${INSTALL}/bin:${PATH}
[[ ! -d gcc ]] && die "GCC source is missing! Please check your connection and rerun the script!" -h
mkdir build-glibc
mkdir build-gcc
mkdir build-binutils
if [[ -z ${NO_TMPFS} ]]; then
sudo mount -t tmpfs -o rw none build-glibc
sudo mount -t tmpfs -o rw none build-gcc
sudo mount -t tmpfs -o rw none build-binutils
fi
cd gcc || die "GCC folder does not exit!"
ln -s -f "${ROOT}/${MPFR}" mpfr
ln -s -f "${ROOT}/${GMP}" gmp
ln -s -f "${ROOT}/${MPC}" mpc
if [[ -n ${TARBALLS} ]]; then
ln -s -f "${ROOT}/${ISL}" isl
else
ln -s -f "${ROOT}/isl" isl
fi
if [[ ${VERSION} -eq 4 ]]; then
# GCC 4.9 (ARM/i686): error: ‘SIGSEGV' was not declared in this scope
GCC_PATCH="942-asan-fix-missing-include-signal-h"
elif [[ ${VERSION} -ge 6 ]] && [[ ${VERSION} -le 8 ]]; then
# GCC 6-8 (i686): error: ‘PATH_MAX' undeclared here (not in a function)
GCC_PATCH="GCC_6-8"
elif [[ ${VERSION} -eq 9 ]]; then
# GNU GCC 9: error: ‘PATH_MAX' was not declared on this scope
GCC_PATCH="GCC_9"
else
# GNU GCC 10 (any): error: ‘PATH_MAX' was not declared on this scope
GCC_PATCH="GCC_10_up"
fi
patch -Np1 < "${ROOT}"/patches/${GCC_PATCH}.patch || die "Failed to patch GCC source!"
cd ..
if [[ -z ${VERBOSE} ]]; then
exec 1>&5 2>&6
header "BUILDING TOOLCHAIN"
exec 6>&2 5>&1 &>/dev/null
fi
}
# Build binutils
function build_binutils() {
header "BUILDING BINUTILS"
cd build-binutils || die "binutils build folder does not exist!"
../binutils/configure "${CONFIGURATION[@]}" \
--target=${TARGET} \
--prefix="${INSTALL}" \
--disable-gdb
make "${JOBS}" || die "Error while building binutils!" -n
make install "${JOBS}" || die "Error while installing binutils!" -n
}
# Make Linux kernel headers
function build_headers() {
header "MAKING LINUX HEADERS"
cd ../linux || die "Linux kernel folder does not exist!"
make ARCH="${KERNEL_ARCH}" \
INSTALL_HDR_PATH="${INSTALL}/${TARGET}" \
headers_install "${JOBS}" || die "Error while building/installing Linux headers!" -n
}
# Build GCC
function build_gcc() {
header "MAKING GCC"
cd ../build-gcc || die "GCC build folder does not exist!"
../gcc/configure "${CONFIGURATION[@]}" \
--enable-languages=c,c++ \
--target=${TARGET} \
--prefix="${INSTALL}"
make all-gcc "${JOBS}" || die "Error while building gcc!" -n
make install-gcc "${JOBS}" || die "Error while installing gcc!" -n
if [[ ${ARCH} = "x86_64" ]]; then
make all-target-libgcc "${JOBS}" || die "Error while installing libgcc for host!" -n
make install-target-libgcc "${JOBS}" || die "Error while installing libgcc for target!" -n
fi
}
# Build glibc
function build_glibc() {
header "MAKING GLIBC"
cd ../build-glibc || die "glibc build folder does not exist!"
../${GLIBC}/configure --prefix="${INSTALL}/${TARGET}" \
--build="${MACHTYPE}" \
--host=${TARGET} \
--target=${TARGET} \
--with-headers="${INSTALL}/${TARGET}/include" \
"${CONFIGURATION[@]}" libc_cv_forced_unwind=yes
make install-bootstrap-headers=yes install-headers "${JOBS}" || die "Error installing headers for glibc!" -n
make csu/subdir_lib "${JOBS}" || die "Error while making subdir_lib for glibc!" -n
install csu/crt1.o csu/crti.o csu/crtn.o "${INSTALL}/${TARGET}/lib"
${TARGET}-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o "${INSTALL}/${TARGET}/lib/libc.so"
touch "${INSTALL}/${TARGET}/include/gnu/stubs.h"
if [[ ${ARCH} = "x86_64" ]]; then
make "${JOBS}" || die "Error while building glibc for the host!" -n
make install "${JOBS}" || die "Error while installing glibc for the host!" -n
else
cd ../build-gcc || die "GCC build folder does not exist!"
make all-target-libgcc "${JOBS}" || die "Error while building libgcc for target!" -n
make install-target-libgcc "${JOBS}" || die "Error while installing libgcc for target!" -n
cd ../build-glibc || die "glibc build folder does not exist!"
make "${JOBS}" || die "Error while building glibc for target" -n
make install "${JOBS}" || die "Error while installing glibc for target" -n
fi
}
# Install GCC
function install_gcc() {
header "INSTALLING GCC"
cd ../build-gcc || die "GCC build folder does not exist!"
make all "${JOBS}" || die "Error while compiling final toolchain!" -n
make install "${JOBS}" || die "Error while installing final toolchain!" -n
cd ..
}
# Package toolchain
function package_tc() {
if [[ -n ${COMPRESSION} ]]; then
PACKAGE=${TARGET}-${VERSION}.x-${SOURCE}-$(TZ=UTC date +%Y%m%d).tar.${COMPRESSION}
header "PACKAGING TOOLCHAIN"
echo "Target file: ${PACKAGE}"
case "${COMPRESSION}" in
"gz")
echo "Packaging with GZIP..."
GZ_OPT=-9 tar -c --use-compress-program=pigz -f "${PACKAGE}" ${TARGET} ;;
"xz")
echo "Packaging with XZ..."
XZ_OPT=-9 tar -c --use-compress-program=pxz -f "${PACKAGE}" ${TARGET} ;;
*)
die "Invalid compression specified... skipping" ;;
esac
fi
}
# Ending information
function ending_info() {
END=$(date +%s)
[[ -z ${VERBOSE} ]] && exec 1>&5 2>&6
if [[ -e ${TARGET}/bin/${TARGET}-gcc ]]; then
header "BUILD SUCCESSFUL" ${VERBOSE:-"--no-first-echo"}
echo "${BOLD}Script duration:${RST} $(format_time "${START}" "${END}")"
echo "${BOLD}GCC version:${RST} $(${TARGET}/bin/${TARGET}-gcc --version | head -n 1)"
if [[ -n ${COMPRESSION} ]] && [[ -e ${PACKAGE} ]]; then
echo "${BOLD}File location:${RST} $(pwd)/${PACKAGE}"
echo "${BOLD}File size:${RST} $(du -h "${PACKAGE}" | awk '{print $1}')"
else
echo "${BOLD}Toolchain location:${RST} $(pwd)/${TARGET}"
fi
else
header "BUILD FAILED"
fi
# Alert to script end
echo "\a"
}
setup_variables
parse_parameters "${@}"
trap 'unmount_tmpfs; die "Manually aborted!" -n' SIGINT SIGTERM
trap 'unmount_tmpfs' EXIT
clean_up
build_binaries
download_sources
extract_sources
update_repos
setup_env
build_binutils
build_headers
build_gcc
build_glibc
install_gcc
package_tc
ending_info