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