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.
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"
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!)
50 patch -Np1 -i "${worktree}/patches/gcc/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch"
52 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"
56 patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
58 patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
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"
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"
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
88 for file in "${worktree}"/patches/mpfr/*
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
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" \
119 --libdir=/tools/lib${libSuffix} \
123 --enable-languages=c,c++ \
124 --enable-clocale=generic \
126 --enable-initfini-array \
128 --enable-libstdcxx-time \
129 --enable-fully-dynamic-string \
130 --enable-default-ssp \
131 --enable-default-pie \
134 --disable-bootstrap \
135 --disable-libstdcxx-pch \
136 --disable-gnu-indirect-function \
137 --disable-libsanitizer \
139 --disable-install-libiberty \
140 --with-linker-hash-style=gnu \
141 --with-local-prefix=/tools \
142 --with-native-system-header-dir=/tools/include \
147 AS_FOR_TARGET="$AS" \
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
167 rm -rf gcc-${version} gcc-build \
168 gmp-${gmp_version} mpfr-${mpfr_version} \
169 mpc-${mpc_version} isl-${isl_version}