nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, targetPackages, fetchurl, fetchpatch, noSysDirs
2, langC ? true, langCC ? true, langFortran ? false
3, langObjC ? stdenv.targetPlatform.isDarwin
4, langObjCpp ? stdenv.targetPlatform.isDarwin
5, langJava ? false
6, langGo ? false
7, reproducibleBuild ? true
8, profiledCompiler ? false
9, langJit ? false
10, staticCompiler ? false
11, enableShared ? !stdenv.targetPlatform.isStatic
12, enableLTO ? !stdenv.hostPlatform.isStatic
13, texinfo ? null
14, perl ? null # optional, for texi2pod (then pod2man); required for Java
15, gmp, mpfr, libmpc, gettext, which, patchelf, binutils
16, cloog ? null, isl ? null # optional, for the Graphite optimization framework.
17, zlib ? null, boehmgc ? null
18, zip ? null, unzip ? null, pkg-config ? null
19, gtk2 ? null, libart_lgpl ? null
20, libX11 ? null, libXt ? null, libSM ? null, libICE ? null, libXtst ? null
21, libXrender ? null, xorgproto ? null
22, libXrandr ? null, libXi ? null
23, x11Support ? langJava
24, enableMultilib ? false
25, enablePlugin ? stdenv.hostPlatform == stdenv.buildPlatform # Whether to support user-supplied plug-ins
26, name ? "gcc"
27, libcCross ? null
28, threadsCross ? null # for MinGW
29, crossStageStatic ? false
30, gnused ? null
31, buildPackages
32}:
33
34assert langJava -> zip != null && unzip != null
35 && zlib != null && boehmgc != null
36 && perl != null; # for `--enable-java-home'
37
38# We enable the isl cloog backend.
39assert cloog != null -> isl != null;
40
41# Make sure we get GNU sed.
42assert stdenv.buildPlatform.isDarwin -> gnused != null;
43
44# The go frontend is written in c++
45assert langGo -> langCC;
46
47# threadsCross is just for MinGW
48assert threadsCross != {} -> stdenv.targetPlatform.isWindows;
49
50# profiledCompiler builds inject non-determinism in one of the compilation stages.
51# If turned on, we can't provide reproducible builds anymore
52assert reproducibleBuild -> profiledCompiler == false;
53
54with lib;
55with builtins;
56
57let majorVersion = "4";
58 version = "${majorVersion}.8.5";
59
60 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
61
62 patches = [ ../parallel-bconfig.patch ]
63 ++ optional (targetPlatform != hostPlatform) ../libstdc++-target.patch
64 ++ optional noSysDirs ../no-sys-dirs.patch
65 ++ optional langFortran ../gfortran-driving.patch
66 ++ optional hostPlatform.isDarwin ../gfortran-darwin-NXConstStr.patch
67 ++ [(fetchpatch {
68 name = "libc_name_p.diff"; # needed to build with gcc6
69 url = "https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff_plain;h=ec1cc0263f1";
70 sha256 = "01jd7pdarh54ki498g6sz64ijl9a1l5f9v8q2696aaxalvh2vwzl";
71 excludes = [ "gcc/cp/ChangeLog" ];
72 })]
73 ++ [ # glibc-2.26
74 ../struct-ucontext-4.8.patch
75 ../sigsegv-not-declared.patch
76 ../res_state-not-declared.patch
77 ../install-info-files-serially.patch
78 # gcc-11 compatibility
79 (fetchpatch {
80 name = "gcc4-char-reload.patch";
81 url = "https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff_plain;h=d57c99458933a21fdf94f508191f145ad8d5ec58";
82 includes = [ "gcc/reload.h" ];
83 sha256 = "sha256-66AMP7/ajunGKAN5WJz/yPn42URZ2KN51yPrFdsxEuM=";
84 })
85 ];
86
87 javaEcj = fetchurl {
88 # The `$(top_srcdir)/ecj.jar' file is automatically picked up at
89 # `configure' time.
90
91 # XXX: Eventually we might want to take it from upstream.
92 url = "ftp://sourceware.org/pub/java/ecj-4.3.jar";
93 sha256 = "0jz7hvc0s6iydmhgh5h2m15yza7p2rlss2vkif30vm9y77m97qcx";
94 };
95
96 # Antlr (optional) allows the Java `gjdoc' tool to be built. We want a
97 # binary distribution here to allow the whole chain to be bootstrapped.
98 javaAntlr = fetchurl {
99 url = "https://www.antlr.org/download/antlr-4.4-complete.jar";
100 sha256 = "02lda2imivsvsis8rnzmbrbp8rh1kb8vmq4i67pqhkwz7lf8y6dz";
101 };
102
103 xlibs = [
104 libX11 libXt libSM libICE libXtst libXrender libXrandr libXi
105 xorgproto
106 ];
107
108 javaAwtGtk = langJava && x11Support;
109
110 /* Cross-gcc settings (build == host != target) */
111 crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
112 stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
113 crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";
114
115 callFile = lib.callPackageWith {
116 # lets
117 inherit
118 majorVersion
119 version
120 buildPlatform
121 hostPlatform
122 targetPlatform
123 patches
124 javaEcj
125 javaAntlr
126 xlibs
127 javaAwtGtk
128 crossMingw
129 stageNameAddon
130 crossNameAddon
131 ;
132 # inherit generated with 'nix eval --json --impure --expr "with import ./. {}; lib.attrNames (lib.functionArgs gcc48.cc.override)" | jq '.[]' --raw-output'
133 inherit
134 binutils
135 boehmgc
136 buildPackages
137 cloog
138 crossStageStatic
139 enableLTO
140 enableMultilib
141 enablePlugin
142 enableShared
143 fetchpatch
144 fetchurl
145 gettext
146 gmp
147 gnused
148 gtk2
149 isl
150 langC
151 langCC
152 langFortran
153 langGo
154 langJava
155 langJit
156 langObjC
157 langObjCpp
158 lib
159 libICE
160 libSM
161 libX11
162 libXi
163 libXrandr
164 libXrender
165 libXt
166 libXtst
167 libart_lgpl
168 libcCross threadsCross
169 libmpc
170 mpfr
171 name
172 noSysDirs
173 patchelf
174 perl
175 pkg-config
176 profiledCompiler
177 reproducibleBuild
178 staticCompiler
179 stdenv
180 targetPackages
181 texinfo
182 unzip
183 which
184 x11Support
185 xorgproto
186 zip
187 zlib
188 ;
189 };
190
191in
192
193# We need all these X libraries when building AWT with GTK.
194assert x11Support -> (filter (x: x == null) ([ gtk2 libart_lgpl ] ++ xlibs)) == [];
195
196stdenv.mkDerivation ({
197 pname = "${crossNameAddon}${name}";
198 inherit version;
199
200 builder = ../builder.sh;
201
202 src = fetchurl {
203 url = "mirror://gnu/gcc/gcc-${version}/gcc-${version}.tar.bz2";
204 sha256 = "08yggr18v373a1ihj0rg2vd6psnic42b518xcgp3r9k81xz1xyr2";
205 };
206
207 inherit patches;
208
209 hardeningDisable = [ "format" "pie" ];
210
211 outputs = [ "out" "lib" "man" "info" ];
212 setOutputFlags = false;
213 NIX_NO_SELF_RPATH = true;
214
215 libc_dev = stdenv.cc.libc_dev;
216
217 postPatch =
218 if targetPlatform != hostPlatform || stdenv.cc.libc != null then
219 # On NixOS, use the right path to the dynamic linker instead of
220 # `/lib/ld*.so'.
221 let
222 libc = if libcCross != null then libcCross else stdenv.cc.libc;
223 in
224 '' echo "fixing the \`GLIBC_DYNAMIC_LINKER' and \`UCLIBC_DYNAMIC_LINKER' macros..."
225 for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
226 do
227 grep -q LIBC_DYNAMIC_LINKER "$header" || continue
228 echo " fixing \`$header'..."
229 sed -i "$header" \
230 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g'
231 done
232 ''
233 else null;
234
235 inherit noSysDirs staticCompiler langJava crossStageStatic
236 libcCross crossMingw;
237
238 inherit (callFile ../common/dependencies.nix { })
239 depsBuildBuild nativeBuildInputs depsBuildTarget buildInputs depsTargetTarget;
240
241 preConfigure = callFile ../common/pre-configure.nix { };
242
243 dontDisableStatic = true;
244
245 configurePlatforms = [ "build" "host" "target" ];
246
247 configureFlags = callFile ../common/configure-flags.nix { };
248
249 targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
250
251 buildFlags = optional
252 (targetPlatform == hostPlatform && hostPlatform == buildPlatform)
253 (if profiledCompiler then "profiledbootstrap" else "bootstrap");
254
255 inherit (callFile ../common/strip-attributes.nix { })
256 stripDebugList
257 stripDebugListTarget
258 preFixup;
259
260 doCheck = false; # requires a lot of tools, causes a dependency cycle for stdenv
261
262 # https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
263 ${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
264
265 # Setting $CPATH and $LIBRARY_PATH to make sure both `gcc' and `xgcc' find the
266 # library headers and binaries, regarless of the language being compiled.
267 #
268 # Note: When building the Java AWT GTK peer, the build system doesn't honor
269 # `--with-gmp' et al., e.g., when building
270 # `libjava/classpath/native/jni/java-math/gnu_java_math_GMP.c', so we just add
271 # them to $CPATH and $LIBRARY_PATH in this case.
272 #
273 # Likewise, the LTO code doesn't find zlib.
274 #
275 # Cross-compiling, we need gcc not to read ./specs in order to build the g++
276 # compiler (after the specs for the cross-gcc are created). Having
277 # LIBRARY_PATH= makes gcc read the specs from ., and the build breaks.
278
279 CPATH = optionals (targetPlatform == hostPlatform) (makeSearchPathOutput "dev" "include" ([]
280 ++ optional (zlib != null) zlib
281 ++ optional langJava boehmgc
282 ++ optionals javaAwtGtk xlibs
283 ++ optionals javaAwtGtk [ gmp mpfr ]
284 ));
285
286 LIBRARY_PATH = optionals (targetPlatform == hostPlatform) (makeLibraryPath ([]
287 ++ optional (zlib != null) zlib
288 ++ optional langJava boehmgc
289 ++ optionals javaAwtGtk xlibs
290 ++ optionals javaAwtGtk [ gmp mpfr ]
291 ));
292
293 inherit (callFile ../common/extra-target-flags.nix { })
294 EXTRA_FLAGS_FOR_TARGET
295 EXTRA_LDFLAGS_FOR_TARGET
296 ;
297
298 passthru = {
299 inherit langC langCC langObjC langObjCpp langFortran langGo version;
300 isGNU = true;
301 hardeningUnsupportedFlags = [ "stackprotector" "fortify3" ];
302 };
303
304 enableParallelBuilding = true;
305 inherit enableShared enableMultilib;
306
307 meta = {
308 inherit (callFile ../common/meta.nix { })
309 homepage
310 license
311 description
312 longDescription
313 platforms
314 maintainers
315 ;
316 badPlatforms = lib.platforms.darwin;
317 };
318}
319
320// optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
321 makeFlags = [ "all-gcc" "all-target-libgcc" ];
322 installTargets = "install-gcc install-target-libgcc";
323}
324
325// optionalAttrs (enableMultilib) { dontMoveLib64 = true; }
326)