1source $stdenv/setup
2
3
4oldOpts="$(shopt -po nounset)" || true
5set -euo pipefail
6
7
8export NIX_FIXINC_DUMMY="$NIX_BUILD_TOP/dummy"
9mkdir "$NIX_FIXINC_DUMMY"
10
11
12if test "$staticCompiler" = "1"; then
13 EXTRA_LDFLAGS="-static"
14else
15 EXTRA_LDFLAGS="-Wl,-rpath,${!outputLib}/lib"
16fi
17
18
19# GCC interprets empty paths as ".", which we don't want.
20if test -z "${CPATH-}"; then unset CPATH; fi
21if test -z "${LIBRARY_PATH-}"; then unset LIBRARY_PATH; fi
22echo "\$CPATH is \`${CPATH-}'"
23echo "\$LIBRARY_PATH is \`${LIBRARY_PATH-}'"
24
25if test "$noSysDirs" = "1"; then
26
27 declare \
28 EXTRA_BUILD_FLAGS EXTRA_FLAGS EXTRA_TARGET_FLAGS \
29 EXTRA_BUILD_LDFLAGS EXTRA_TARGET_LDFLAGS
30
31 # Extract flags from Bintools Wrappers
32 for pre in 'BUILD_' ''; do
33 curBintools="NIX_${pre}BINTOOLS"
34
35 declare -a extraLDFlags=()
36 if [[ -e "${!curBintools}/nix-support/orig-libc" ]]; then
37 # Figure out what extra flags when linking to pass to the gcc
38 # compilers being generated to make sure that they use our libc.
39 extraLDFlags=($(< "${!curBintools}/nix-support/libc-ldflags") $(< "${!curBintools}/nix-support/libc-ldflags-before" || true))
40
41 # The path to the Libc binaries such as `crti.o'.
42 libc_libdir="$(< "${!curBintools}/nix-support/orig-libc")/lib"
43 else
44 # Hack: support impure environments.
45 extraLDFlags=("-L/usr/lib64" "-L/usr/lib")
46 libc_libdir="/usr/lib"
47 fi
48 extraLDFlags=("-L$libc_libdir" "-rpath" "$libc_libdir"
49 "${extraLDFlags[@]}")
50 for i in "${extraLDFlags[@]}"; do
51 declare EXTRA_${pre}LDFLAGS+=" -Wl,$i"
52 done
53 done
54
55 # Extract flags from CC Wrappers
56 for pre in 'BUILD_' ''; do
57 curCC="NIX_${pre}CC"
58 curFIXINC="NIX_${pre}FIXINC_DUMMY"
59
60 declare -a extraFlags=()
61 if [[ -e "${!curCC}/nix-support/orig-libc" ]]; then
62 # Figure out what extra compiling flags to pass to the gcc compilers
63 # being generated to make sure that they use our libc.
64 extraFlags=($(< "${!curCC}/nix-support/libc-cflags"))
65
66 # The path to the Libc headers
67 libc_devdir="$(< "${!curCC}/nix-support/orig-libc-dev")"
68
69 # Use *real* header files, otherwise a limits.h is generated that
70 # does not include Libc's limits.h (notably missing SSIZE_MAX,
71 # which breaks the build).
72 declare NIX_${pre}FIXINC_DUMMY="$libc_devdir/include"
73 else
74 # Hack: support impure environments.
75 extraFlags=("-isystem" "/usr/include")
76 declare NIX_${pre}FIXINC_DUMMY=/usr/include
77 fi
78
79 extraFlags=("-I${!curFIXINC}" "${extraFlags[@]}")
80
81 # BOOT_CFLAGS defaults to `-g -O2'; since we override it below, make
82 # sure to explictly add them so that files compiled with the bootstrap
83 # compiler are optimized and (optionally) contain debugging information
84 # (info "(gccinstall) Building").
85 if test -n "${dontStrip-}"; then
86 extraFlags=("-O2" "-g" "${extraFlags[@]}")
87 else
88 # Don't pass `-g' at all; this saves space while building.
89 extraFlags=("-O2" "${extraFlags[@]}")
90 fi
91
92 declare EXTRA_${pre}FLAGS="${extraFlags[*]}"
93 done
94
95 if test -z "${targetConfig-}"; then
96 # host = target, so the flags are the same
97 EXTRA_TARGET_FLAGS="$EXTRA_FLAGS"
98 EXTRA_TARGET_LDFLAGS="$EXTRA_LDFLAGS"
99 fi
100
101 # CFLAGS_FOR_TARGET are needed for the libstdc++ configure script to find
102 # the startfiles.
103 # FLAGS_FOR_TARGET are needed for the target libraries to receive the -Bxxx
104 # for the startfiles.
105 makeFlagsArray+=(
106 "BUILD_SYSTEM_HEADER_DIR=$NIX_BUILD_FIXINC_DUMMY"
107 "SYSTEM_HEADER_DIR=$NIX_BUILD_FIXINC_DUMMY"
108 "NATIVE_SYSTEM_HEADER_DIR=$NIX_FIXINC_DUMMY"
109
110 "LDFLAGS_FOR_BUILD=$EXTRA_BUILD_LDFLAGS"
111 #"LDFLAGS=$EXTRA_LDFLAGS"
112 "LDFLAGS_FOR_TARGET=$EXTRA_TARGET_LDFLAGS"
113
114 "CFLAGS_FOR_BUILD=$EXTRA_BUILD_FLAGS $EXTRA_BUILD_LDFLAGS"
115 "CXXFLAGS_FOR_BUILD=$EXTRA_BUILD_FLAGS $EXTRA_BUILD_LDFLAGS"
116 "FLAGS_FOR_BUILD=$EXTRA_BUILD_FLAGS $EXTRA_BUILD_LDFLAGS"
117
118 # It seems there is a bug in GCC 5
119 #"CFLAGS=$EXTRA_FLAGS $EXTRA_LDFLAGS"
120 #"CXXFLAGS=$EXTRA_FLAGS $EXTRA_LDFLAGS"
121
122 "CFLAGS_FOR_TARGET=$EXTRA_TARGET_FLAGS $EXTRA_TARGET_LDFLAGS"
123 "CXXFLAGS_FOR_TARGET=$EXTRA_TARGET_FLAGS $EXTRA_TARGET_LDFLAGS"
124 "FLAGS_FOR_TARGET=$EXTRA_TARGET_FLAGS $EXTRA_TARGET_LDFLAGS"
125 )
126
127 if test -z "${targetConfig-}"; then
128 makeFlagsArray+=(
129 "BOOT_CFLAGS=$EXTRA_FLAGS $EXTRA_LDFLAGS"
130 "BOOT_LDFLAGS=$EXTRA_TARGET_FLAGS $EXTRA_TARGET_LDFLAGS"
131 )
132 fi
133
134 if test -n "${targetConfig-}" -a "$crossStageStatic" == 1; then
135 # We don't want the gcc build to assume there will be a libc providing
136 # limits.h in this stagae
137 makeFlagsArray+=(
138 'LIMITS_H_TEST=false'
139 )
140 else
141 makeFlagsArray+=(
142 'LIMITS_H_TEST=true'
143 )
144 fi
145fi
146
147if test -n "${targetConfig-}"; then
148 # The host strip will destroy some important details of the objects
149 dontStrip=1
150fi
151
152eval "$oldOpts"
153
154providedPreConfigure="$preConfigure";
155preConfigure() {
156 if test -n "$newlibSrc"; then
157 tar xvf "$newlibSrc" -C ..
158 ln -s ../newlib-*/newlib newlib
159 # Patch to get armvt5el working:
160 sed -i -e 's/ arm)/ arm*)/' newlib/configure.host
161 fi
162
163 # Bug - they packaged zlib
164 if test -d "zlib"; then
165 # This breaks the build without-headers, which should build only
166 # the target libgcc as target libraries.
167 # See 'configure:5370'
168 rm -Rf zlib
169 fi
170
171 if test -f "$NIX_CC/nix-support/orig-libc"; then
172 # Patch the configure script so it finds glibc headers. It's
173 # important for example in order not to get libssp built,
174 # because its functionality is in glibc already.
175 sed -i \
176 -e "s,glibc_header_dir=/usr/include,glibc_header_dir=$libc_dev/include", \
177 gcc/configure
178 fi
179
180 if test -n "$crossMingw" -a -n "$crossStageStatic"; then
181 mkdir -p ../mingw
182 # --with-build-sysroot expects that:
183 cp -R $libcCross/include ../mingw
184 configureFlags="$configureFlags --with-build-sysroot=`pwd`/.."
185 fi
186
187 # Eval the preConfigure script from nix expression.
188 eval "$providedPreConfigure"
189
190 # Perform the build in a different directory.
191 mkdir ../build
192 cd ../build
193 configureScript=../$sourceRoot/configure
194}
195
196
197postConfigure() {
198 # Don't store the configure flags in the resulting executables.
199 sed -e '/TOPLEVEL_CONFIGURE_ARGUMENTS=/d' -i Makefile
200}
201
202
203preInstall() {
204 # Make ‘lib64’ symlinks to ‘lib’.
205 if [ -n "$is64bit" -a -z "$enableMultilib" ]; then
206 mkdir -p "$out/lib"
207 ln -s lib "$out/lib64"
208 mkdir -p "$lib/lib"
209 ln -s lib "$lib/lib64"
210 fi
211}
212
213
214postInstall() {
215 # Move runtime libraries to $lib.
216 moveToOutput "lib/lib*.so*" "$lib"
217 moveToOutput "lib/lib*.la" "$lib"
218 moveToOutput "lib/lib*.dylib" "$lib"
219 moveToOutput "share/gcc-*/python" "$lib"
220
221 for i in "$lib"/lib/*.{la,py}; do
222 substituteInPlace "$i" --replace "$out" "$lib"
223 done
224
225 if [ -n "$enableMultilib" ]; then
226 moveToOutput "lib64/lib*.so*" "$lib"
227 moveToOutput "lib64/lib*.la" "$lib"
228 moveToOutput "lib64/lib*.dylib" "$lib"
229
230 for i in "$lib"/lib64/*.{la,py}; do
231 substituteInPlace "$i" --replace "$out" "$lib"
232 done
233 fi
234
235 # Remove `fixincl' to prevent a retained dependency on the
236 # previous gcc.
237 rm -rf $out/libexec/gcc/*/*/install-tools
238 rm -rf $out/lib/gcc/*/*/install-tools
239
240 # More dependencies with the previous gcc or some libs (gccbug stores the build command line)
241 rm -rf $out/bin/gccbug
242
243 if type "patchelf"; then
244 # Take out the bootstrap-tools from the rpath, as it's not needed at all having $out
245 for i in $(find "$out"/libexec/gcc/*/*/* -type f -a \! -name '*.la'); do
246 PREV_RPATH=`patchelf --print-rpath "$i"`
247 NEW_RPATH=`echo "$PREV_RPATH" | sed 's,:[^:]*bootstrap-tools/lib,,g'`
248 patchelf --set-rpath "$NEW_RPATH" "$i" && echo OK
249 done
250
251 # For some reason the libs retain RPATH to $out
252 for i in "$lib"/lib/{libtsan,libasan,libubsan}.so.*.*.*; do
253 PREV_RPATH=`patchelf --print-rpath "$i"`
254 NEW_RPATH=`echo "$PREV_RPATH" | sed "s,:${out}[^:]*,,g"`
255 patchelf --set-rpath "$NEW_RPATH" "$i" && echo OK
256 done
257 fi
258
259 if type "install_name_tool"; then
260 for i in "$lib"/lib/*.*.dylib; do
261 install_name_tool -id "$i" "$i" || true
262 for old_path in $(otool -L "$i" | grep "$out" | awk '{print $1}'); do
263 new_path=`echo "$old_path" | sed "s,$out,$lib,"`
264 install_name_tool -change "$old_path" "$new_path" "$i" || true
265 done
266 done
267 fi
268
269 # Get rid of some "fixed" header files
270 rm -rfv $out/lib/gcc/*/*/include-fixed/{root,linux}
271
272 # Replace hard links for i686-pc-linux-gnu-gcc etc. with symlinks.
273 for i in $out/bin/*-gcc*; do
274 if cmp -s $out/bin/gcc $i; then
275 ln -sfn gcc $i
276 fi
277 done
278
279 for i in $out/bin/c++ $out/bin/*-c++* $out/bin/*-g++*; do
280 if cmp -s $out/bin/g++ $i; then
281 ln -sfn g++ $i
282 fi
283 done
284
285 # Disable RANDMMAP on grsec, which causes segfaults when using
286 # precompiled headers.
287 # See https://bugs.gentoo.org/show_bug.cgi?id=301299#c31
288 paxmark r $out/libexec/gcc/*/*/{cc1,cc1plus}
289
290 eval "$postInstallGhdl"
291
292 # Two identical man pages are shipped (moving and compressing is done later)
293 ln -sf gcc.1 "$out"/share/man/man1/g++.1
294}
295
296genericBuild