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