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.
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 # http://port70.net/~nsz/musl/gcc-10.1.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"
45 # Apply extra patches to increment the security
46 patch -Np1 -i "${worktree}/patches/gcc/10/extra-relro-in-dragora.patch"
47 patch -Np1 -i "${worktree}/patches/gcc/10/extra-musl_libssp.patch"
49 # 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"
51 patch -Np1 -i "${worktree}/patches/gcc/0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch"
52 patch -Np1 -i "${worktree}/patches/gcc/0006-Enable-Wformat-and-Wformat-security-by-default.patch"
53 patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
54 patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
55 patch -Np1 -i "${worktree}/patches/gcc/0020-add-fortify-headers-paths.patch"
56 patch -Np1 -i "${worktree}/patches/gcc/0022-DP-Use-push-state-pop-state-for-gold-as-well-when-li.patch"
58 # Hard code default ld path to look at /tools
60 find . -type f -name '*.h' -exec grep -l "/lib/ld-" {} + | while read -r file
62 test -f "$file" || continue;
63 cp -f "$file" "${file}.orig"
65 sed -e 's@/lib\(64\)\{0,1\}\(32\)\{0,1\}/ld@/tools&@g' \
66 -e 's@/usr@/tools@g' "${file}.orig" > "$file"
68 #undef STANDARD_STARTFILE_PREFIX_1
69 #undef STANDARD_STARTFILE_PREFIX_2
70 #define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
71 #define STANDARD_STARTFILE_PREFIX_2 ""' >> "$file"
74 # Make symlinks for requisites
75 ln -s ../gmp-${gmp_version} gmp
76 ln -s ../mpfr-${mpfr_version} mpfr
77 ln -s ../mpc-${mpc_version} mpc
78 ln -s ../isl-${isl_version} isl
80 # Apply patches for MPFR
84 for file in "${worktree}"/patches/mpfr/*
94 # Update detection for hosts based on musl
96 cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
97 cp -f "${worktree}/archive/common/config.sub" gmp/config.sub
99 cp -f "${worktree}/archive/common/config.guess" isl/config.guess
100 cp -f "${worktree}/archive/common/config.sub" isl/config.sub
102 # Build in a separate directory
107 # Import and export toolchain variables
108 . "${worktree}/stages/env.d/cross-staticenv"
110 ../gcc-${version}/configure \
111 AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
112 CC="$BTCC" CXX="$BTCXX" \
113 CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
115 --libdir=/tools/lib${libSuffix} \
119 --enable-languages=c,c++ \
120 --enable-clocale=generic \
122 --enable-initfini-array \
124 --enable-libstdcxx-time \
125 --enable-fully-dynamic-string \
126 --enable-default-ssp \
127 --enable-default-pie \
130 --disable-bootstrap \
131 --disable-libstdcxx-pch \
132 --disable-gnu-indirect-function \
133 --disable-libmudflap \
134 --disable-libsanitizer \
136 --disable-install-libiberty \
137 --with-linker-hash-style=gnu \
138 --with-local-prefix=/tools \
139 --with-native-system-header-dir=/tools/include \
144 AS_FOR_TARGET="$AS" \
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
164 rm -rf gcc-${version} gcc-build \
165 gmp-${gmp_version} mpfr-${mpfr_version} \
166 mpc-${mpc_version} isl-${isl_version}