nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, targetPackages, fetchurl, fetchpatch, fetchFromGitHub, noSysDirs
2, langC ? true, langCC ? true, langFortran ? false
3, langAda ? false
4, langObjC ? stdenv.targetPlatform.isDarwin
5, langObjCpp ? stdenv.targetPlatform.isDarwin
6, langJava ? false
7, langGo ? false
8, reproducibleBuild ? true
9, profiledCompiler ? false
10, langJit ? false
11, staticCompiler ? false
12, enableShared ? !stdenv.targetPlatform.isStatic
13, enableLTO ? !stdenv.hostPlatform.isStatic
14, texinfo ? null
15, flex
16, perl ? null # optional, for texi2pod (then pod2man); required for Java
17, gmp, mpfr, libmpc, gettext, which, patchelf, binutils
18, isl ? null # optional, for the Graphite optimization framework.
19, zlib ? null, boehmgc ? null
20, gnat-bootstrap ? null
21, zip ? null, unzip ? null, pkg-config ? null
22, gtk2 ? null, libart_lgpl ? null
23, libX11 ? null, libXt ? null, libSM ? null, libICE ? null, libXtst ? null
24, libXrender ? null, xorgproto ? null
25, libXrandr ? null, libXi ? null
26, x11Support ? langJava
27, enableMultilib ? false
28, enablePlugin ? stdenv.hostPlatform == stdenv.buildPlatform # Whether to support user-supplied plug-ins
29, name ? "gcc"
30, libcCross ? null
31, threadsCross ? null # for MinGW
32, crossStageStatic ? false
33, gnused ? null
34, cloog ? null # unused; just for compat with gcc4, as we override the parameter on some places
35, buildPackages
36}:
37
38assert langJava -> zip != null && unzip != null
39 && zlib != null && boehmgc != null
40 && perl != null; # for `--enable-java-home'
41
42# Make sure we get GNU sed.
43assert stdenv.buildPlatform.isDarwin -> gnused != null;
44
45# The go frontend is written in c++
46assert langGo -> langCC;
47
48assert langAda -> gnat-bootstrap != null;
49
50# threadsCross is just for MinGW
51assert threadsCross != {} -> stdenv.targetPlatform.isWindows;
52
53# profiledCompiler builds inject non-determinism in one of the compilation stages.
54# If turned on, we can't provide reproducible builds anymore
55assert reproducibleBuild -> profiledCompiler == false;
56
57with lib;
58with builtins;
59
60let majorVersion = "6";
61 version = "${majorVersion}.5.0";
62
63 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
64
65 patches = [ ../9/fix-struct-redefinition-on-glibc-2.36.patch ]
66 ++ optionals (!stdenv.targetPlatform.isRedox) [
67 ../use-source-date-epoch.patch ./0001-Fix-build-for-glibc-2.31.patch
68
69 # Fix https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80431
70 (fetchurl {
71 name = "fix-bug-80431.patch";
72 url = "https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=de31f5445b12fd9ab9969dc536d821fe6f0edad0";
73 sha256 = "0sd52c898msqg7m316zp0ryyj7l326cjcn2y19dcxqp15r74qj0g";
74 })
75 ../install-info-files-serially.patch
76 ] ++ optional (targetPlatform != hostPlatform) ../libstdc++-target.patch
77 ++ optional noSysDirs ../no-sys-dirs.patch
78 ++ optional langAda ../gnat-cflags.patch
79 ++ optional langAda ./gnat-glibc234.patch
80 ++ optional langFortran ../gfortran-driving.patch
81 ++ optional (targetPlatform.libc == "musl") ../libgomp-dont-force-initial-exec.patch
82 ++ optional langGo ./gogcc-workaround-glibc-2.36.patch
83
84 # Obtain latest patch with ../update-mcfgthread-patches.sh
85 ++ optional (!crossStageStatic && targetPlatform.isMinGW && threadsCross.model == "mcf") ./Added-mcf-thread-model-support-from-mcfgthread.patch
86 ++ optional (targetPlatform.libc == "musl" && targetPlatform.isx86_32) (fetchpatch {
87 url = "https://git.alpinelinux.org/aports/plain/main/gcc/gcc-6.1-musl-libssp.patch?id=5e4b96e23871ee28ef593b439f8c07ca7c7eb5bb";
88 sha256 = "1jf1ciz4gr49lwyh8knfhw6l5gvfkwzjy90m7qiwkcbsf4a3fqn2";
89 })
90
91 ++ [ ../libsanitizer-no-cyclades-9.patch ];
92
93 javaEcj = fetchurl {
94 # The `$(top_srcdir)/ecj.jar' file is automatically picked up at
95 # `configure' time.
96
97 # XXX: Eventually we might want to take it from upstream.
98 url = "ftp://sourceware.org/pub/java/ecj-4.3.jar";
99 sha256 = "0jz7hvc0s6iydmhgh5h2m15yza7p2rlss2vkif30vm9y77m97qcx";
100 };
101
102 # Antlr (optional) allows the Java `gjdoc' tool to be built. We want a
103 # binary distribution here to allow the whole chain to be bootstrapped.
104 javaAntlr = fetchurl {
105 url = "https://www.antlr.org/download/antlr-4.4-complete.jar";
106 sha256 = "02lda2imivsvsis8rnzmbrbp8rh1kb8vmq4i67pqhkwz7lf8y6dz";
107 };
108
109 xlibs = [
110 libX11 libXt libSM libICE libXtst libXrender libXrandr libXi
111 xorgproto
112 ];
113
114 javaAwtGtk = langJava && x11Support;
115
116 /* Cross-gcc settings (build == host != target) */
117 crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
118 stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
119 crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";
120
121 callFile = lib.callPackageWith {
122 # lets
123 inherit
124 majorVersion
125 version
126 buildPlatform
127 hostPlatform
128 targetPlatform
129 patches
130 javaEcj
131 javaAntlr
132 xlibs
133 javaAwtGtk
134 crossMingw
135 stageNameAddon
136 crossNameAddon
137 ;
138 # inherit generated with 'nix eval --json --impure --expr "with import ./. {}; lib.attrNames (lib.functionArgs gcc6.cc.override)" | jq '.[]' --raw-output'
139 inherit
140 binutils
141 boehmgc
142 buildPackages
143 cloog
144 crossStageStatic
145 enableLTO
146 enableMultilib
147 enablePlugin
148 enableShared
149 fetchFromGitHub
150 fetchpatch
151 fetchurl
152 flex
153 gettext
154 gmp
155 gnat-bootstrap
156 gnused
157 gtk2
158 isl
159 langAda
160 langC
161 langCC
162 langFortran
163 langGo
164 langJava
165 langJit
166 langObjC
167 langObjCpp
168 lib
169 libICE
170 libSM
171 libX11
172 libXi
173 libXrandr
174 libXrender
175 libXt
176 libXtst
177 libart_lgpl
178 libcCross
179 libmpc
180 mpfr
181 name
182 noSysDirs
183 patchelf
184 perl
185 pkg-config
186 profiledCompiler
187 reproducibleBuild
188 staticCompiler
189 stdenv
190 targetPackages
191 texinfo
192 threadsCross
193 unzip
194 which
195 x11Support
196 xorgproto
197 zip
198 zlib
199 ;
200 };
201in
202
203# We need all these X libraries when building AWT with GTK.
204assert x11Support -> (filter (x: x == null) ([ gtk2 libart_lgpl ] ++ xlibs)) == [];
205
206stdenv.mkDerivation ({
207 pname = "${crossNameAddon}${name}";
208 inherit version;
209
210 builder = ../builder.sh;
211
212 src = if stdenv.targetPlatform.isVc4 then fetchFromGitHub {
213 owner = "itszor";
214 repo = "gcc-vc4";
215 rev = "e90ff43f9671c760cf0d1dd62f569a0fb9bf8918";
216 sha256 = "0gxf66hwqk26h8f853sybphqa5ca0cva2kmrw5jsiv6139g0qnp8";
217 } else if stdenv.targetPlatform.isRedox then fetchFromGitHub {
218 owner = "redox-os";
219 repo = "gcc";
220 rev = "f360ac095028d286fc6dde4d02daed48f59813fa"; # `redox` branch
221 sha256 = "1an96h8l58pppyh3qqv90g8hgcfd9hj7igvh2gigmkxbrx94khfl";
222 } else fetchurl {
223 url = "mirror://gnu/gcc/gcc-${version}/gcc-${version}.tar.xz";
224 sha256 = "0i89fksfp6wr1xg9l8296aslcymv2idn60ip31wr9s4pwin7kwby";
225 };
226
227 inherit patches;
228
229 outputs = if langJava || langGo || langJit then ["out" "man" "info"]
230 else [ "out" "lib" "man" "info" ];
231 setOutputFlags = false;
232 NIX_NO_SELF_RPATH = true;
233
234 libc_dev = stdenv.cc.libc_dev;
235
236 hardeningDisable = [ "format" "pie" ];
237
238 postPatch =
239 # This should kill all the stdinc frameworks that gcc and friends like to
240 # insert into default search paths.
241 lib.optionalString hostPlatform.isDarwin ''
242 substituteInPlace gcc/config/darwin-c.c \
243 --replace 'if (stdinc)' 'if (0)'
244
245 substituteInPlace libgcc/config/t-slibgcc-darwin \
246 --replace "-install_name @shlib_slibdir@/\$(SHLIB_INSTALL_NAME)" "-install_name ''${!outputLib}/lib/\$(SHLIB_INSTALL_NAME)"
247
248 substituteInPlace libgfortran/configure \
249 --replace "-install_name \\\$rpath/\\\$soname" "-install_name ''${!outputLib}/lib/\\\$soname"
250 ''
251 + (
252 lib.optionalString (targetPlatform != hostPlatform || stdenv.cc.libc != null)
253 # On NixOS, use the right path to the dynamic linker instead of
254 # `/lib/ld*.so'.
255 (let
256 libc = if libcCross != null then libcCross else stdenv.cc.libc;
257 in
258 (
259 '' echo "fixing the \`GLIBC_DYNAMIC_LINKER', \`UCLIBC_DYNAMIC_LINKER', and \`MUSL_DYNAMIC_LINKER' macros..."
260 for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
261 do
262 grep -q _DYNAMIC_LINKER "$header" || continue
263 echo " fixing \`$header'..."
264 sed -i "$header" \
265 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g' \
266 -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
267 done
268 ''
269 + lib.optionalString (targetPlatform.libc == "musl")
270 ''
271 sed -i gcc/config/linux.h -e '1i#undef LOCAL_INCLUDE_DIR'
272 ''
273 ))
274 );
275
276 inherit noSysDirs staticCompiler langJava crossStageStatic
277 libcCross crossMingw;
278
279 inherit (callFile ../common/dependencies.nix { })
280 depsBuildBuild nativeBuildInputs depsBuildTarget buildInputs depsTargetTarget;
281
282 NIX_LDFLAGS = lib.optionalString hostPlatform.isSunOS "-lm";
283
284 preConfigure = callFile ../common/pre-configure.nix { };
285
286 dontDisableStatic = true;
287
288 configurePlatforms = [ "build" "host" "target" ];
289
290 configureFlags = callFile ../common/configure-flags.nix { };
291
292 targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
293
294 buildFlags = optional
295 (targetPlatform == hostPlatform && hostPlatform == buildPlatform)
296 (if profiledCompiler then "profiledbootstrap" else "bootstrap");
297
298 inherit (callFile ../common/strip-attributes.nix { })
299 stripDebugList
300 stripDebugListTarget
301 preFixup;
302
303 doCheck = false; # requires a lot of tools, causes a dependency cycle for stdenv
304
305 # https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
306 ${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
307
308 # Setting $CPATH and $LIBRARY_PATH to make sure both `gcc' and `xgcc' find the
309 # library headers and binaries, regarless of the language being compiled.
310 #
311 # Note: When building the Java AWT GTK peer, the build system doesn't honor
312 # `--with-gmp' et al., e.g., when building
313 # `libjava/classpath/native/jni/java-math/gnu_java_math_GMP.c', so we just add
314 # them to $CPATH and $LIBRARY_PATH in this case.
315 #
316 # Likewise, the LTO code doesn't find zlib.
317 #
318 # Cross-compiling, we need gcc not to read ./specs in order to build the g++
319 # compiler (after the specs for the cross-gcc are created). Having
320 # LIBRARY_PATH= makes gcc read the specs from ., and the build breaks.
321
322 CPATH = optionals (targetPlatform == hostPlatform) (makeSearchPathOutput "dev" "include" ([]
323 ++ optional (zlib != null) zlib
324 ++ optional langJava boehmgc
325 ++ optionals javaAwtGtk xlibs
326 ++ optionals javaAwtGtk [ gmp mpfr ]
327 ));
328
329 LIBRARY_PATH = optionals (targetPlatform == hostPlatform) (makeLibraryPath ([]
330 ++ optional (zlib != null) zlib
331 ++ optional langJava boehmgc
332 ++ optionals javaAwtGtk xlibs
333 ++ optionals javaAwtGtk [ gmp mpfr ]
334 ));
335
336 inherit (callFile ../common/extra-target-flags.nix { })
337 EXTRA_FLAGS_FOR_TARGET
338 EXTRA_LDFLAGS_FOR_TARGET
339 ;
340
341 passthru = {
342 inherit langC langCC langObjC langObjCpp langFortran langAda langGo version;
343 isGNU = true;
344 hardeningUnsupportedFlags = [ "fortify3" ];
345 };
346
347 enableParallelBuilding = true;
348 inherit enableShared enableMultilib;
349
350 meta = {
351 inherit (callFile ../common/meta.nix { })
352 homepage
353 license
354 description
355 longDescription
356 platforms
357 maintainers
358 ;
359 badPlatforms = [ "aarch64-darwin" ];
360 };
361}
362
363// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
364 makeFlags = [ "all-gcc" "all-target-libgcc" ];
365 installTargets = "install-gcc install-target-libgcc";
366}
367
368// optionalAttrs (enableMultilib) { dontMoveLib64 = true; }
369
370// optionalAttrs (langJava && !stdenv.hostPlatform.isDarwin) {
371 postFixup = ''
372 target="$(echo "$out/libexec/gcc"/*/*/ecj*)"
373 patchelf --set-rpath "$(patchelf --print-rpath "$target"):$out/lib" "$target"
374 '';}
375)