-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·982 lines (849 loc) · 33.8 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
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
#!/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://sourceware.org/newlib/
# 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, host, i686, or x86_64. This is the toolchain's target architecture. If host is specified, target the host instead."
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*, 10* and 11* [*GNU only]). This is the GCC version to build. If host is specified in ARCH, only version that matches or newer than the distribution one is allowed."
echo
echo "${BOLD}OPTIONAL PARAMETERS:${RST}"
echo " -e | --elf: Make a bare metal / ELF toolchain, will automatically trigger -nl (--with-newlib) flag (Default No)."
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 " -nu | --no-update: Do not update the downloaded components before building (useful if you have slow internet)."
echo " -nl | --with-newlib: Use newlib instead of glic (Default no)."
echo " -p | --package: Possible values: gz, xz or zstd. Compresses toolchain after build."
echo " -r | --release: Make a Push / Release of the toolchain"
echo " -tm | --tmpfs: Use tmpfs for building (useful if you have much RAM)."
echo " -tr | --tarballs: Use tarballs for binutils, MPFR, MPC, ISL, glibc, 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() {
push_logs
[[ -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}"
}
# Date checking for GMP daily snapshot download
function check_date() {
local GAP
[[ $(TZ=Europe/Berlin date +%Z) == CEST ]] && \
GAP=15600 || \
GAP=19200
date --date=@$(($(date +%s) - GAP)) -u +%Y%m%d
}
# 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!"
sudo -v 2>/dev/null || die "Sudo is not available!" -n
}
# Unmount tmpfs
function unmount_tmpfs() {
if [[ ${NEWLIB} == true ]]; then
sudo umount -f build-newlib 2>/dev/null
else
sudo umount -f build-glibc 2>/dev/null
fi
sudo umount -f build-gcc 2>/dev/null
sudo umount -f build-binutils 2>/dev/null
}
# git clone wrapper
function git_clone() {
git clone --quiet ${DEPTH_FLAG:+"--depth=1"} "${@}"
}
# git fetch wrapper
function git_fetch() {
git fetch --quiet ${DEPTH_FLAG:+"--depth=1"} "${@}"
}
# aria2 wrapper
function dl_file() {
aria2c --split=16 --max-connection-per-server=16 --summary-interval=0 "${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"
"CFLAGS=-g0 -O3 -fstack-protector-strong"
"CXXFLAGS=-g0 -O3 -fstack-protector-strong" )
JOBS="-j$(($(nproc --all) + 1))"
DEPTH_FLAG=true
# Binary versions
BINUTILS_git="master"
BINUTILS_tar="2.38"
GMP="gmp-6.2.1"
MPFR_svn="trunk"
MPFR_tar="mpfr-4.1.0"
MPC_git="master"
MPC_tar="mpc-1.2.1"
ISL_git="master"
ISL_tar="isl-0.24"
GLIBC_git="master"
GLIBC_tar="glibc-2.35"
NEWLIB_git="master"
NEWLIB_tar="newlib-4.1.0"
LINUX="5.18"
# Timezone
export TZ=Asia/Kolkata
# 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
"-e"|"--elf") BARE_METAL=true ;;
"-f"|"--full-src") DEPTH_FLAG=false ;;
"-j"|"--jobs") shift && JOBS=-j${1} ;;
"-nu"|"--no-update") NO_UPDATE=true ;;
"-nl"|"--with-newlib") NEWLIB=true ;;
"-p"|"--package") shift && COMPRESSION=${1} ;;
"-r"|"--release") RELEASE=true ;;
"-tm"|"--tmpfs") TMPFS=true ;;
"-tr"|"--tarballs") TARBALLS=true ;;
"-V"|"--verbose") VERBOSE=true ;;
# HELP!
"-h"|"--help") help_menu; exit ;;
esac
shift
done
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" ;;
"host") [[ ${VERSION} -le $(gcc -dumpversion | cut -d '.' -f 1) ]] && die "Building toolchain older than distribution one is not supported on host target!"
ARCH="$(uname -m)"
TARGET="$(gcc -dumpmachine)"
FOR_HOST=true ;;
"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
# Override TARGET if BARE_METAL flag is parsed
if [[ ${BARE_METAL} == true ]]; then
if [[ ${TARGET} == "arm-linux-gnueabi" ]]; then
TARGET="arm-eabi"
fi
if [[ ${TARGET} == "aarch64-linux-gnu" ]]; then
TARGET="aarch64-elf"
fi
if [[ ${TARGET} == "i686-linux-gnu" ]]; then
TARGET="i686-elf"
fi
if [[ ${TARGET} == "x86_64-linux-gnu" ]]; then
TARGET="x86_64-elf"
fi
# Build with newlib instead of glibc
NEWLIB=true
fi
# 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_git="release/2.26/master"
ISL_git="isl-0.17.1" ;;
"gnu:5") GCC=gcc-5-branch
GLIBC_git="release/2.27/master"
ISL_git="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=gcc-10-branch ;;
"gnu:11") GCC=master ;;
"linaro:4") GCC=linaro-local/releases/linaro-4.9-2017.01
GLIBC_git="release/2.27/master"
ISL_git="isl-0.17.1" ;;
"linaro:5") GCC=linaro-local/gcc-5-integration-branch
GLIBC_git="release/2.27/master"
ISL_git="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_tar="glibc-2.26"
ISL_tar="isl-0.17.1"
EXT=gz ;;
"gnu:5") GCC=gcc-5.5.0
GLIBC_tar="glibc-2.27"
ISL_tar="isl-0.17.1" ;;
"gnu:6") GCC=gcc-6.5.0 ;;
"gnu:7") GCC=gcc-7.4.0 ;;
"gnu:8") GCC=gcc-8.3.0 ;;
"gnu:9") GCC=gcc-9.2.0 ;;
"gnu:10") GCC=gcc-10.2.0 ;;
"gnu:11") die "GCC 11.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_tar="glibc-2.27"
ISL_tar="isl-0.17.1" ;;
"linaro:5") GCC=linaro-5.5-2017.10
GLIBC_tar="glibc-2.27"
ISL_tar="isl-0.17.1" ;;
"linaro:6") GCC=linaro-snapshot-6.5-2018.11 ;;
"linaro:7") GCC=linaro-snapshot-7.4-2019.01 ;;
# 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"
[[ -z ${TMPFS} ]] && { for i in binutils gcc glibc newlib; do rm -rf build-$i/*; done; }
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 build-newlib ]] ||
[[ -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
DOWNLOADER=$(command -v aria2c || command -v wget || command -v curl)
if [[ -n ${DOWNLOADER} ]]; then
[[ ${DOWNLOADER} = "aria2c" ]] && DOWNLOADER+=" -x16"
[[ ${DOWNLOADER} = "curl" ]] && DOWNLOADER+=" -LO"
else
die "Neither wget nor curl could be found on your system!"
fi
HAS_AXEL=$(command -v axel)
mkdir -p sources
if [[ -z ${HAS_AXEL} && ! -f ${PREBUILTS_BIN}/txt2man ]]; then
TXT2MAN=/tmp/sources/txt2man
[[ ! -d ${TXT2MAN} ]] && git clone --quiet --depth=1 https://github.com/mvertes/txt2man "${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!"
) || exit
fi
export PATH=${PREBUILTS_BIN}:${PATH}
if [[ ! -f ${PREBUILTS_BIN}/pigz ]]; then
PIGZ=/tmp/sources/pigz
[[ ! -d ${PIGZ} ]] && git clone --quiet --depth=1 https://github.com/madler/pigz "${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
}
function download_sources() {
cd sources || die "Failed to create sources directory!"
if [[ ! -f ${GMP}.tar.lz ]]; then
header "DOWNLOADING GMP"
dl_file https://gmplib.org/download/gmp/"${GMP}".tar.lz
fi
if [[ ! -f linux-${LINUX}.tar.xz ]]; then
header "DOWNLOADING LINUX KERNEL"
dl_file https://cdn.kernel.org/pub/linux/kernel/v${LINUX/.*}.x/linux-${LINUX}.tar.xz
fi
if [[ -z ${TARBALLS} ]]; then
if [[ ! -d mpfr ]]; then
header "DOWNLOADING MPFR"
svn co svn://scm.gforge.inria.fr/svnroot/mpfr/${MPFR_svn} mpfr
fi
if [[ ! -d mpc ]]; then
header "DOWNLOADING MPC"
git_clone https://scm.gforge.inria.fr/anonscm/git/mpc/mpc.git -b ${MPC_git}
fi
if [[ ${NEWLIB} == true ]]; then
if [[ ! -d newlib ]]; then
header "DOWNLOADING NEWLIB"
git_clone git://sourceware.org/git/newlib-cygwin.git -b ${NEWLIB_git} newlib
fi
else
if [[ ! -d glibc ]]; then
header "DOWNLOADING GLIBC"
git_clone git://sourceware.org/git/glibc.git -b ${GLIBC_git}
fi
fi
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"
git_clone git://repo.or.cz/isl.git -b ${ISL_git}
fi
if [[ ! -d gcc ]]; then
header "DOWNLOADING GCC"
git_clone https://gcc.gnu.org/git/gcc.git -b ${GCC}
fi
else
if [[ ! -f ${MPFR_tar}.tar.xz ]]; then
header "DOWNLOADING MPFR"
dl_file https://www.mpfr.org/mpfr-current/${MPFR_tar}.tar.xz
fi
if [[ ! -f ${MPC_tar}.tar.gz ]]; then
header "DOWNLOADING MPC"
dl_file https://ftp.gnu.org/gnu/mpc/${MPC_tar}.tar.gz
fi
if [[ ${NEWLIB} == true ]]; then
if [[ ! -f ${NEWLIB_tar}.tar.gz ]]; then
header "DOWNLOADING NEWLIB ${NEWLIB_tar} FOR GCC ${VERSION}"
dl_file ftp://sourceware.org/pub/newlib/{NEWLIB_tar}.tar.gz
fi
else
if [[ ! -f ${GLIBC_tar}.tar.xz ]]; then
header "DOWNLOADING GLIBC ${GLIBC_tar} FOR GCC ${VERSION}"
dl_file https://ftp.gnu.org/gnu/glibc/${GLIBC_tar}.tar.xz
fi
fi
if [[ ! -f binutils-${BINUTILS_tar}.tar.xz ]]; then
header "DOWNLOADING BINUTILS ${BINUTILS_tar} FOR GCC ${VERSION}"
dl_file https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_tar}.tar.xz
fi
if [[ ! -f ${ISL_tar}.tar.xz ]]; then
header "DOWNLOADING ISL ${ISL_tar} FOR GCC ${VERSION}"
dl_file http://isl.gforge.inria.fr/${ISL_tar}.tar.xz
fi
# GNU, any version
if [[ ${SOURCE} = "gnu" ]]; then
if [[ ! -f ${GCC}.tar.${EXT:-xz} ]]; then
header "DOWNLOADING GCC"
dl_file 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"
dl_file 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"
dl_file https://git.linaro.org/toolchain/gcc.git/snapshot/gcc-${GCC}.tar.gz
fi
GCC_TAR=gcc-${GCC}.tar.gz
fi
fi
}
function extract() {
mkdir -p "${2}"
tar xf "${1}" -C "${2}" --strip-components=1
}
# Extract tarballs to their proper locations
function extract_sources() {
header "EXTRACTING DOWNLOADED TARBALLS"
extract "${GMP}".tar.lz ../"${GMP}"
extract linux-${LINUX}.tar.xz ../linux
if [[ -n ${TARBALLS} ]]; then
extract ${MPFR_tar}.tar.xz ../${MPFR_tar}
extract ${MPC_tar}.tar.gz ../${MPC_tar}
if [[ ${NEWLIB} == true ]]; then
extract ${NEWLIB_tar}.tar.gz ../${NEWLIB_tar}
else
extract ${GLIBC_tar}.tar.xz ../${GLIBC_tar}
fi
extract binutils-${BINUTILS_tar}.tar.xz ../binutils
extract ${ISL_tar}.tar.xz ../${ISL_tar}
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 mpfr || die "MPFR did not get fetched properly!"
svn up
./autogen.sh
automake --add-missing
)
(
cd mpc || die "MPC did not get cloned properly!"
[[ -n ${FULL_SOURCE} ]] && BRANCH=origin/${MPC_git} || BRANCH=FETCH_HEAD
git_fetch origin ${MPC_git}
git checkout -f ${MPC_git} || git checkout -f -b ${MPC_git} ${BRANCH}
git reset --hard ${BRANCH}
autoreconf -i
)
if [[ ${NEWLIB} == true ]]; then
(
cd newlib || die "NEWLIB did not get cloned properly!"
[[ -n ${FULL_SOURCE} ]] && BRANCH=origin/${NEWLIB_git} || BRANCH=FETCH_HEAD
git_fetch origin ${NEWLIB_git}
git checkout -f ${NEWLIB_git} || git checkout -f -b ${NEWLIB_git} ${BRANCH}
git reset --hard ${BRANCH}
)
else
(
cd glibc || die "GLIBC did not get cloned properly!"
[[ -n ${FULL_SOURCE} ]] && BRANCH=origin/${GLIBC_git} || BRANCH=FETCH_HEAD
git_fetch origin ${GLIBC_git}
git checkout -f ${GLIBC_git} || git checkout -f -b ${GLIBC_git} ${BRANCH}
git reset --hard ${BRANCH}
)
fi
(
cd isl || die "ISL did not get cloned properly!"
[[ -n ${FULL_SOURCE} ]] && BRANCH=origin/${ISL_git} || BRANCH=FETCH_HEAD
git_fetch origin ${ISL_git}
git checkout -f ${ISL_git} || git checkout -f -b ${ISL_git} ${BRANCH}
git reset --hard ${BRANCH}
./autogen.sh
) || exit
(
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}
git reset --hard ${BRANCH}
) || exit
(
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}
git reset --hard ${BRANCH}
) || exit
else
if [[ -d mpfr ]]; then
(
cd mpfr || die "MPFR did not get downloaded properly!"
./autogen.sh
automake --add-missing
)
fi
if [[ -d mpc ]]; then
(
cd mpc || die "MPC did not get downloaded properly!"
autoreconf -i
)
fi
if [[ -d isl ]]; then
(
cd isl || die "ISL did not get downloaded properly!"
./autogen.sh
) || exit
fi
fi
cd ..
[[ ! -d binutils ]] && ln -s sources/binutils binutils
[[ ! -d gcc ]] && ln -s sources/gcc gcc
if [[ ${NEWLIB} == true ]]; then
[[ ! -d newlib ]] && ln -s sources/newlib newlib
else
[[ ! -d glibc ]] && ln -s sources/glibc glibc
fi
}
# 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
if [[ ${NEWLIB} == true ]]; then
mkdir build-newlib
else
mkdir build-glibc
fi
mkdir build-gcc
mkdir build-binutils
if [[ -n ${TMPFS} ]]; then
if [[ ${NEWLIB} == true ]]; then
sudo mount -t tmpfs -o rw none build-newlib
else
sudo mount -t tmpfs -o rw none build-glibc
fi
sudo mount -t tmpfs -o rw none build-gcc
sudo mount -t tmpfs -o rw none build-binutils
else
# Bind mount from somewhere else, assuming these folders exist
sudo mount -B "${HOME}"/KudProject/build/build-tools-gcc/glibc build-glibc
sudo mount -B "${HOME}"/KudProject/build/build-tools-gcc/gcc build-gcc
sudo mount -B "${HOME}"/KudProject/build/build-tools-gcc/binutils build-binutils
fi
cd gcc || die "GCC folder does not exit!"
ln -s -f "${ROOT}/${GMP}" gmp
if [[ -n ${TARBALLS} ]]; then
ln -s -f "${ROOT}/${MPFR_tar}" mpfr
ln -s -f "${ROOT}/${ISL_tar}" isl
ln -s -f "${ROOT}/${MPC_tar}" mpc
else
ln -s -f "${ROOT}/sources/mpfr" mpfr
ln -s -f "${ROOT}/sources/isl" isl
ln -s -f "${ROOT}/sources/mpc" mpc
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
if [[ -z ${NO_UPDATE} ]]; then
patch -Np1 < "${ROOT}"/patches/${GCC_PATCH}.patch || die "Failed to patch GCC source!"
fi
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 --target=${TARGET} \
--prefix="${INSTALL}" \
--disable-gdb \
--disable-nls \
--enable-gold \
--enable-lto \
--enable-plugins \
--enable-relro \
--with-sysroot \
"${CONFIGURATION[@]}"
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!"
if [[ ${NEWLIB} == true ]]; then
../gcc/configure --enable-languages=c,c++ \
--target=${TARGET} \
--prefix="${INSTALL}" \
--disable-nls \
--disable-shared \
--with-newlib \
"${CONFIGURATION[@]}"
else
../gcc/configure --enable-languages=c,c++ \
--target=${TARGET} \
--prefix="${INSTALL}" \
--disable-nls \
"${CONFIGURATION[@]}"
fi
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!"
[[ -z ${TARBALLS} ]] && GLIBC=glibc || GLIBC=${GLIBC_tar}
../${GLIBC}/configure --prefix="${INSTALL}/${TARGET}" \
--build="${MACHTYPE}" \
--host=${TARGET} \
--target=${TARGET} \
--with-headers="${INSTALL}/${TARGET}/include" \
"${CONFIGURATION[@]}" \
libc_cv_forced_unwind=yes with_selinux=no
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 [[ -n ${FOR_HOST} || ${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
}
# Build newlib
function build_newlib() {
header "MAKING NEWLIB"
cd ../build-newlib || die "newlib build folder does not exist!"
[[ -z ${TARBALLS} ]] && NEWLIB=newlib || NEWLIB=${NEWLIB_tar}
../${NEWLIB}/configure --prefix="${INSTALL}" \
--build=${MACHTYPE} \
--host=${MACHTYPE} \
--target=${TARGET}
cd ../build-newlib || die "newlib build folder does not exist!"
make "${JOBS}" || die "Error while building newlib for target" -n
make install "${JOBS}" || die "Error while installing newlib for target" -n
}
# Install GCC
function install_gcc() {
header "INSTALLING GCC"
cd ../build-gcc || die "GCC build folder does not exist!"
# FIXME: This is hacky
sed -i '38d' gcc/include-fixed/bits/statx.h
sed -i '43d' gcc/include-fixed/bits/statx.h
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} ;;
"zstd")
echo "Packaging with ZSTD..."
ZSTD_OPT=-9 tar -c --use-compress-program=zstd -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)"
GCC_HEAD_COMMIT=$(cd /home/runner/work/build-tools-gcc/build-tools-gcc/sources/gcc/ && git rev-parse HEAD)
echo "${BOLD}GCC COMMIT HEAD:${RST} ${GCC_HEAD_COMMIT}"
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
}
# Push build info to telegram
function push_tg_info()
{
HEAD_COMMIT=$(git log -n 1 --pretty=format:%H)
GIT_URL="https://github.com/radcolor/${TARGET}/commit/${HEAD_COMMIT}"
if [ "${TARBALLS}" == true ]; then
curl -s -X POST https://api.telegram.org/bot${TG_BOT_API}/sendMessage?chat_id=${CHANNEL_ID} \
-d "disable_web_page_preview=true" \
-d "parse_mode=markdown&text=⚒️ New commit to ${TARGET}:stable"$'\n\n'"[$(echo ${HEAD_COMMIT} | cut -c1-8)](${GIT_URL}): ${COMMIT_MSG}"
else
curl -s -X POST https://api.telegram.org/bot${TG_BOT_API}/sendMessage?chat_id=${CHANNEL_ID} \
-d "disable_web_page_preview=true" \
-d "parse_mode=markdown&text=⚒️ New commit to ${TARGET}:master"$'\n\n'"[$(echo ${HEAD_COMMIT} | cut -c1-8)](${GIT_URL}): ${COMMIT_MSG}"
fi
}
# Push Sources to remote
function push_sources()
{
DATE="$(date +%d%m%Y)"
BUILDER_COMMIT="$(git rev-parse HEAD)"
git config --global user.name "Shashank Baghel"
git config --global user.email "[email protected]"
# As our REPO_NAME == TARGET
git clone --quiet "https://radcolor:${TOKEN_GITHUB}@github.com/radcolor/${TARGET}" upstream && cd upstream
rm -rf *
cp -r ../${TARGET}/* .
git checkout README.md && git add .
if [ "${TARBALLS}" == true ]; then
git commit -am "toolchain: Bump GCC version: $(/home/runner/work/build-tools-gcc/build-tools-gcc/${TARGET}/bin/${TARGET}-gcc --version | head -n 1)
BUILDER COMMIT: ${BUILDER_COMMIT}" && git push && COMMIT_MSG=$(git log -1 --pretty=%B) && push_tg_info
else
git commit -am "Update to https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=${GCC_HEAD_COMMIT}, ${DATE} build.
GCC VERSION: $(/home/runner/work/build-tools-gcc/build-tools-gcc/${TARGET}/bin/${TARGET}-gcc --version | head -n 1)
GCC COMMIT URL: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=${GCC_HEAD_COMMIT}
BUILDER COMMIT: ${BUILDER_COMMIT}" && git push && COMMIT_MSG=$(git log -1 --pretty=%B) && push_tg_info
fi
}
# Send Logs
function push_logs()
{
LOG="/home/runner/work/build-tools-gcc/build-tools-gcc/build-gnu-gcc-tc.log"
curl -F document=@"${LOG}" "https://api.telegram.org/bot${TG_BOT_API}/sendDocument" \
-F chat_id="${CHAT_ID}" \
-F "disable_web_page_preview=false" \
-F "parse_mode=html" \
-F caption="Build logs GCC for Target: ${TARGET}. GCC Version: $(/home/runner/work/build-tools-gcc/build-tools-gcc/${TARGET}/bin/${TARGET}-gcc --version | head -n 1)"
}
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
if [[ ${NEWLIB} == true ]]; then
echo "Skippung HEADERS"
else
build_headers
fi
build_gcc
if [[ ${NEWLIB} == true ]]; then
build_newlib
else
build_glibc
fi
install_gcc
package_tc
ending_info
if [[ ${RELEASE} == true ]]; then
push_sources
fi
push_logs