bootstrap: Switch back to the GPLv3+ license
[dragora.git] / stages / 1 / 06-gcc
blob26d0f9feef824299c5d93052c3de09aa0c88b720
1 # Build script for gcc.
3 # Copyright (c) 2016-2022 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-20220114
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"
46 patch -Np1 -i "${worktree}/patches/gcc/10/extra-musl_libssp.patch"
48 # Apply patches from "Alpine Linux" in order to improve the security (Thanks!)
49 echo "patch1:"
50 patch -Np1 -i "${worktree}/patches/gcc/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch"
51 echo "patch2:"
52 patch -Np1 -i "${worktree}/patches/gcc/0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch"
53 echo "patch3:"
54 patch -Np1 -i "${worktree}/patches/gcc/0006-Enable-Wformat-and-Wformat-security-by-default.patch"
55 echo "patch4:"
56 patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
57 echo "patch5:"
58 patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
59 echo "patch6:"
60 patch -Np1 -i "${worktree}/patches/gcc/0020-add-fortify-headers-paths.patch"
62 # Hard code default ld path to look at /tools
64 find . -type f -name '*.h' -exec grep -l "/lib/ld-" {} + | while read -r file
66     test -f "$file" || continue;
67     cp -f "$file" "${file}.orig"
69     sed -e 's@/lib\(64\)\{0,1\}\(32\)\{0,1\}/ld@/tools&@g' \
70         -e 's@/usr@/tools@g' "${file}.orig" > "$file"
71     echo '
72 #undef STANDARD_STARTFILE_PREFIX_1
73 #undef STANDARD_STARTFILE_PREFIX_2
74 #define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
75 #define STANDARD_STARTFILE_PREFIX_2 ""' >> "$file"
76 done
78 # Make symlinks for requisites
79 ln -s ../gmp-${gmp_version} gmp
80 ln -s ../mpfr-${mpfr_version} mpfr
81 ln -s ../mpc-${mpc_version} mpc
82 ln -s ../isl-${isl_version} isl
84 # Apply patches for MPFR
86     cd mpfr || exit 1
88     for file in "${worktree}"/patches/mpfr/*
89     do
90         if test -f "$file"
91         then
92             rm -f PATCHES
93             patch -p1 < "$file"
94         fi
95     done
98 # Update detection for hosts based on musl
100 cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
101 cp -f "${worktree}/archive/common/config.sub" gmp/config.sub
103 cp -f "${worktree}/archive/common/config.guess" isl/config.guess
104 cp -f "${worktree}/archive/common/config.sub" isl/config.sub
106 # Build in a separate directory
107 rm -rf ../gcc-build
108 mkdir ../gcc-build
109 cd ../gcc-build
111 # Import and export toolchain variables
112 . "${worktree}/stages/env.d/cross-staticenv"
114 ../gcc-${version}/configure \
115 AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
116 CC="$BTCC" CXX="$BTCXX" \
117 CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
118  --prefix=/tools \
119  --libdir=/tools/lib${libSuffix} \
120  --build=$host \
121  --host=$target \
122  --target=$target \
123  --enable-languages=c,c++ \
124  --enable-clocale=generic \
125  --enable-cet=auto \
126  --enable-initfini-array \
127  --enable-tls \
128  --enable-libstdcxx-time \
129  --enable-fully-dynamic-string \
130  --enable-default-ssp \
131  --enable-default-pie \
132  --enable-libssp \
133  --disable-symvers \
134  --disable-bootstrap \
135  --disable-libstdcxx-pch \
136  --disable-gnu-indirect-function \
137  --disable-libsanitizer \
138  --disable-nls \
139  --disable-install-libiberty \
140  --with-linker-hash-style=gnu \
141  --with-local-prefix=/tools \
142  --with-native-system-header-dir=/tools/include \
143  $multilib_options \
144  $gcc_options
146 make -j${jobs} all \
147  AS_FOR_TARGET="$AS" \
148  LD_FOR_TARGET="$LD"
150 make -j${jobs} install
152 # Provide minimal libssp_nonshared.a
154 $BTCC -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
155       -o __stack_chk_fail_local.o
156 $AR rc libssp_nonshared.a __stack_chk_fail_local.o
157 $RANLIB libssp_nonshared.a
158 cp -p libssp_nonshared.a /tools/lib${libSuffix}
160 # Unset some imported variables from file
161 unset AR AS LD RANLIB READELF STRIP
163 cd -- "$TMPDIR"
165 cleanup()
167     rm -rf gcc-${version} gcc-build \
168      gmp-${gmp_version} mpfr-${mpfr_version} \
169      mpc-${mpc_version} isl-${isl_version}