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