Upgraded to GCC 10-20211217
[dragora.git] / stages / 1 / 06-gcc
blob2ff3a293208f3ea732c67ed691df4091b8916959
1 # Build script for gcc.
3 # Copyright (c) 2016-2021 Matias Fonzo, <selk@dragora.org>.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 #    http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 version=10-20211217
19 # Prerequisites
20 gmp_version=6.2.1
21 mpfr_version=4.1.0
22 mpc_version=1.2.1
23 isl_version=0.24
25 cd -- "$TMPDIR"
27 rm -rf gcc-${version} gmp-${gmp_version} \
28        mpfr-${mpfr_version} mpc-${mpc_version} isl-${isl_version}
29 unpack "${worktree}/sources/gcc-${version}.tar.xz" \
30        "${worktree}/sources/gmp-${gmp_version}.tar.lz" \
31        "${worktree}/sources/mpfr-${mpfr_version}.tar.bz2" \
32        "${worktree}/sources/mpc-${mpc_version}.tar.gz" \
33        "${worktree}/sources/isl-${isl_version}.tar.bz2"
35 # Build instructions
36 cd gcc-${version}
38 # Apply specific patches for the support in musl.
39 # https://port70.net/~nsz/musl/gcc-10.3.0/
41 patch -Np1 -i "${worktree}/patches/gcc/10/0002-posix_memalign.patch"
42 patch -Np1 -i "${worktree}/patches/gcc/10/0003-j2.patch"
43 patch -Np1 -i "${worktree}/patches/gcc/10/0004-static-pie.patch"
44 patch -Np1 -i "${worktree}/patches/gcc/10/0005-libstdc-futex-time64.patch"
45 patch -Np1 -i "${worktree}/patches/gcc/10/0006-m68k-sqrt.patch"
47 # Apply extra patches to increment the security
48 patch -Np1 -i "${worktree}/patches/gcc/10/extra-relro-in-dragora.patch"
49 patch -Np1 -i "${worktree}/patches/gcc/10/extra-musl_libssp.patch"
51 # Apply patches from "Alpine Linux" in order to improve the security (Thanks!)
52 patch -Np1 -i "${worktree}/patches/gcc/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch"
53 patch -Np1 -i "${worktree}/patches/gcc/0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch"
54 patch -Np1 -i "${worktree}/patches/gcc/0006-Enable-Wformat-and-Wformat-security-by-default.patch"
55 patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
56 patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
57 patch -Np1 -i "${worktree}/patches/gcc/0020-add-fortify-headers-paths.patch"
59 # Hard code default ld path to look at /tools
61 find . -type f -name '*.h' -exec grep -l "/lib/ld-" {} + | while read -r file
63     test -f "$file" || continue;
64     cp -f "$file" "${file}.orig"
66     sed -e 's@/lib\(64\)\{0,1\}\(32\)\{0,1\}/ld@/tools&@g' \
67         -e 's@/usr@/tools@g' "${file}.orig" > "$file"
68     echo '
69 #undef STANDARD_STARTFILE_PREFIX_1
70 #undef STANDARD_STARTFILE_PREFIX_2
71 #define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
72 #define STANDARD_STARTFILE_PREFIX_2 ""' >> "$file"
73 done
75 # Make symlinks for requisites
76 ln -s ../gmp-${gmp_version} gmp
77 ln -s ../mpfr-${mpfr_version} mpfr
78 ln -s ../mpc-${mpc_version} mpc
79 ln -s ../isl-${isl_version} isl
81 # Apply patches for MPFR
83     cd mpfr || exit 1
85     for file in "${worktree}"/patches/mpfr/*
86     do
87         if test -f "$file"
88         then
89             rm -f PATCHES
90             patch -p1 < "$file"
91         fi
92     done
95 # Update detection for hosts based on musl
97 cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
98 cp -f "${worktree}/archive/common/config.sub" gmp/config.sub
100 cp -f "${worktree}/archive/common/config.guess" isl/config.guess
101 cp -f "${worktree}/archive/common/config.sub" isl/config.sub
103 # Build in a separate directory
104 rm -rf ../gcc-build
105 mkdir ../gcc-build
106 cd ../gcc-build
108 # Import and export toolchain variables
109 . "${worktree}/stages/env.d/cross-staticenv"
111 ../gcc-${version}/configure \
112 AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
113 CC="$BTCC" CXX="$BTCXX" \
114 CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
115  --prefix=/tools \
116  --libdir=/tools/lib${libSuffix} \
117  --build=$host \
118  --host=$target \
119  --target=$target \
120  --enable-languages=c,c++ \
121  --enable-clocale=generic \
122  --enable-cet=auto \
123  --enable-initfini-array \
124  --enable-tls \
125  --enable-libstdcxx-time \
126  --enable-fully-dynamic-string \
127  --enable-default-ssp \
128  --enable-default-pie \
129  --enable-libssp \
130  --disable-symvers \
131  --disable-bootstrap \
132  --disable-libstdcxx-pch \
133  --disable-gnu-indirect-function \
134  --disable-libsanitizer \
135  --disable-nls \
136  --disable-install-libiberty \
137  --with-linker-hash-style=gnu \
138  --with-local-prefix=/tools \
139  --with-native-system-header-dir=/tools/include \
140  $multilib_options \
141  $gcc_options
143 make -j${jobs} all \
144  AS_FOR_TARGET="$AS" \
145  LD_FOR_TARGET="$LD"
147 make -j${jobs} install
149 # Provide minimal libssp_nonshared.a
151 $BTCC -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
152       -o __stack_chk_fail_local.o
153 $AR rc libssp_nonshared.a __stack_chk_fail_local.o
154 $RANLIB libssp_nonshared.a
155 cp -p libssp_nonshared.a /tools/lib${libSuffix}
157 # Unset some imported variables from file
158 unset AR AS LD RANLIB READELF STRIP
160 cd -- "$TMPDIR"
162 cleanup()
164     rm -rf gcc-${version} gcc-build \
165      gmp-${gmp_version} mpfr-${mpfr_version} \
166      mpc-${mpc_version} isl-${isl_version}