1 { lib, stdenv, targetPackages, fetchurl, fetchpatch, noSysDirs
2 , langC ? true, langCC ? true, langFortran ? false
3 , langObjC ? stdenv.targetPlatform.isDarwin
4 , langObjCpp ? stdenv.targetPlatform.isDarwin
7 , profiledCompiler ? false
9 , staticCompiler ? false
10 , # N.B. the defult is intentionally not from an `isStatic`. See
11 # https://gcc.gnu.org/install/configure.html - this is about target
12 # platform libraries not host platform ones unlike normal. But since
13 # we can't rebuild those without also rebuilding the compiler itself,
14 # we opt to always build everything unlike our usual policy.
18 , perl ? null # optional, for texi2pod (then pod2man); required for Java
19 , gmp, mpfr, libmpc, gettext, which, patchelf
20 , libelf # optional, for link-time optimizations (LTO)
21 , cloog ? null, isl ? null # optional, for the Graphite optimization framework.
22 , zlib ? null, boehmgc ? null
23 , zip ? null, unzip ? null, pkg-config ? null
24 , gtk2 ? null, libart_lgpl ? null
25 , libX11 ? null, libXt ? null, libSM ? null, libICE ? null, libXtst ? null
26 , libXrender ? null, xorgproto ? null
27 , libXrandr ? null, libXi ? null
28 , x11Support ? langJava
29 , enableMultilib ? false
30 , enablePlugin ? stdenv.hostPlatform == stdenv.buildPlatform # Whether to support user-supplied plug-ins
33 , threadsCross ? null # for MinGW
34 , crossStageStatic ? false
35 , # Strip kills static libs of other archs (hence no cross)
36 stripped ? stdenv.hostPlatform == stdenv.buildPlatform
37 && stdenv.targetPlatform == stdenv.hostPlatform
42 assert langJava -> zip != null && unzip != null
43 && zlib != null && boehmgc != null
44 && perl != null; # for `--enable-java-home'
46 # We enable the isl cloog backend.
47 assert cloog != null -> isl != null;
49 # LTO needs libelf and zlib.
50 assert libelf != null -> zlib != null;
52 # Make sure we get GNU sed.
53 assert stdenv.hostPlatform.isDarwin -> gnused != null;
55 # The go frontend is written in c++
56 assert langGo -> langCC;
58 # threadsCross is just for MinGW
59 assert threadsCross != null -> stdenv.targetPlatform.isWindows;
64 let majorVersion = "4";
65 version = "${majorVersion}.9.4";
67 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
70 [ ../use-source-date-epoch.patch ../parallel-bconfig.patch ./parallel-strsignal.patch
73 name = "avoid-ustat-glibc-2.28.patch";
74 url = "https://gitweb.gentoo.org/proj/gcc-patches.git/plain/4.9.4/gentoo/100_all_avoid-ustat-glibc-2.28.patch?id=55fcb515620a8f7d3bb77eba938aa0fcf0d67c96";
75 sha256 = "0b32sb4psv5lq0ij9fwhi1b4pjbwdjnv24nqprsk14dsc6xmi1g0";
78 ++ optional (targetPlatform != hostPlatform) ../libstdc++-target.patch
79 ++ optional noSysDirs ../no-sys-dirs.patch
80 ++ optional langFortran ../gfortran-driving.patch
81 ++ [ ../struct-ucontext.patch ../struct-sigaltstack-4.9.patch ] # glibc-2.26
82 # Retpoline patches pulled from the branch hjl/indirect/gcc-4_9-branch (by H.J. Lu, the author of GCC upstream retpoline commits)
83 ++ builtins.map ({commit, sha256}: fetchpatch {url = "https://github.com/hjl-tools/gcc/commit/${commit}.patch"; inherit sha256;})
84 [{ commit = "e623d21608e96ecd6b65f0d06312117d20488a38"; sha256 = "1ix8i4d2r3ygbv7npmsdj790rhxqrnfwcqzv48b090r9c3ij8ay3"; }
85 { commit = "2015a09e332309f12de1dadfe179afa6a29368b8"; sha256 = "0xcfs0cbb63llj2gbcdrvxim79ax4k4aswn0a3yjavxsj71s1n91"; }
86 { commit = "6b11591f4494f705e8746e7d58b7f423191f4e92"; sha256 = "0aydyhsm2ig0khgbp27am7vq7liyqrq6kfhfi2ki0ij0ab1hfbga"; }
87 { commit = "203c7d9c3e9cb0f88816b481ef8e7e87b3ecc373"; sha256 = "0wqn16y7wy5kg8ngfcni5qdwfphl01axczibbk49bxclwnzvldqa"; }
88 { commit = "f039c6f284b2c9ce97c8353d6034978795c4872e"; sha256 = "13fkgdb17lpyxfksz1zanxhgpsm0jrss9w61nbl7an4im22hz7ci"; }
89 { commit = "ed42606bdab1c5d9e5ad828cd6fe1a0557f193b7"; sha256 = "0gdnn8v3p03imj3qga2mzdhpgbmjcklkxdl97jvz5xia2ikzknxm"; }
90 { commit = "5278e062ef292fd2fbf987d25389785f4c5c0f99"; sha256 = "0j81x758wf8v7j4rx5wc1cy7yhkvhlhv3wmnarwakxiwsspq0vrs"; }
91 { commit = "76f1ffbbb6cd9f6ecde6c82cd16e20a27242e890"; sha256 = "1py56y6gp7fjf4f8bbsfwh5bs1gnmlqda1ycsmnwlzfm0cshdp0c"; }
92 { commit = "4ca48b2b688b135c0390f54ea9077ef10aedd52c"; sha256 = "15r019pzr3k0lpgyvdc92c8fayw8b5lrzncna4bqmamcsdz7vsaw"; }
93 { commit = "98c7bf9ddc80db965d69d61521b1c7a1cec32d9a"; sha256 = "1d7pfdv1q23nf0wadw7jbp6d6r7pnzjpbyxgbdfv7j1vr9l1bp60"; }
94 { commit = "3dc76b53ad896494ca62550a7a752fecbca3f7a2"; sha256 = "0jvdzfpvfdmklfcjwqblwq1i22iqis7ljpvm7adra5d7zf2xk7xz"; }
95 { commit = "1e961ed49b18e176c7457f53df2433421387c23b"; sha256 = "04dnqqs4qsvz4g8cq6db5id41kzys7hzhcaycwmc9rpqygs2ajwz"; }
96 { commit = "e137c72d099f9b3b47f4cc718aa11eab14df1a9c"; sha256 = "1ms0dmz74yf6kwgjfs4d2fhj8y6mcp2n184r3jk44wx2xc24vgb2"; }];
99 # The `$(top_srcdir)/ecj.jar' file is automatically picked up at
102 # XXX: Eventually we might want to take it from upstream.
103 url = "ftp://sourceware.org/pub/java/ecj-4.3.jar";
104 sha256 = "0jz7hvc0s6iydmhgh5h2m15yza7p2rlss2vkif30vm9y77m97qcx";
107 # Antlr (optional) allows the Java `gjdoc' tool to be built. We want a
108 # binary distribution here to allow the whole chain to be bootstrapped.
109 javaAntlr = fetchurl {
110 url = "https://www.antlr.org/download/antlr-4.4-complete.jar";
111 sha256 = "02lda2imivsvsis8rnzmbrbp8rh1kb8vmq4i67pqhkwz7lf8y6dz";
115 libX11 libXt libSM libICE libXtst libXrender libXrandr libXi
119 javaAwtGtk = langJava && x11Support;
121 /* Cross-gcc settings (build == host != target) */
122 crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt";
123 stageNameAddon = if crossStageStatic then "stage-static" else "stage-final";
124 crossNameAddon = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-${stageNameAddon}-";
128 # We need all these X libraries when building AWT with GTK.
129 assert x11Support -> (filter (x: x == null) ([ gtk2 libart_lgpl ] ++ xlibs)) == [];
131 stdenv.mkDerivation ({
132 pname = "${crossNameAddon}${name}${if stripped then "" else "-debug"}";
135 builder = ../builder.sh;
138 url = "mirror://gnu/gcc/gcc-${version}/gcc-${version}.tar.bz2";
139 sha256 = "14l06m7nvcvb0igkbip58x59w3nq6315k6jcz3wr9ch1rn9d44bc";
144 hardeningDisable = [ "format" "pie" ];
146 outputs = if langJava || langGo then ["out" "man" "info"]
147 else [ "out" "lib" "man" "info" ];
148 setOutputFlags = false;
149 NIX_NO_SELF_RPATH = true;
151 libc_dev = stdenv.cc.libc_dev;
154 if targetPlatform != hostPlatform || stdenv.cc.libc != null then
155 # On NixOS, use the right path to the dynamic linker instead of
158 libc = if libcCross != null then libcCross else stdenv.cc.libc;
160 '' echo "fixing the \`GLIBC_DYNAMIC_LINKER' and \`UCLIBC_DYNAMIC_LINKER' macros..."
161 for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
163 grep -q LIBC_DYNAMIC_LINKER "$header" || continue
164 echo " fixing \`$header'..."
166 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g'
171 inherit noSysDirs staticCompiler langJava crossStageStatic
172 libcCross crossMingw;
174 depsBuildBuild = [ buildPackages.stdenv.cc ];
175 nativeBuildInputs = [ texinfo which gettext ]
176 ++ (optional (perl != null) perl)
177 ++ (optional javaAwtGtk pkg-config);
179 # For building runtime libs
182 if hostPlatform == buildPlatform then [
183 targetPackages.stdenv.cc.bintools # newly-built gcc will be used
184 ] else assert targetPlatform == hostPlatform; [ # build != host == target
188 ++ optional targetPlatform.isLinux patchelf;
191 gmp mpfr libmpc libelf
192 targetPackages.stdenv.cc.bintools # For linking code at run-time
193 ] ++ (optional (cloog != null) cloog)
194 ++ (optional (isl != null) isl)
195 ++ (optional (zlib != null) zlib)
196 ++ (optionals langJava [ boehmgc zip unzip ])
197 ++ (optionals javaAwtGtk ([ gtk2 libart_lgpl ] ++ xlibs))
198 # The builder relies on GNU sed (for instance, Darwin's `sed' fails with
199 # "-i may not be used with stdin"), and `stdenvNative' doesn't provide it.
200 ++ (optional hostPlatform.isDarwin gnused)
203 depsTargetTarget = optional (!crossStageStatic && threadsCross != null) threadsCross;
205 preConfigure = import ../common/pre-configure.nix {
207 inherit version hostPlatform langJava langGo;
210 dontDisableStatic = true;
212 # TODO(@Ericson2314): Always pass "--target" and always prefix.
213 configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target";
215 configureFlags = import ../common/configure-flags.nix {
220 crossStageStatic libcCross
223 gmp mpfr libmpc libelf isl
234 langJava javaAwtGtk javaAntlr javaEcj
242 targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
244 buildFlags = optional
245 (targetPlatform == hostPlatform && hostPlatform == buildPlatform)
246 (if profiledCompiler then "profiledbootstrap" else "bootstrap");
248 dontStrip = !stripped;
250 doCheck = false; # requires a lot of tools, causes a dependency cycle for stdenv
252 installTargets = optional stripped "install-strip";
254 # https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
255 ${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
257 # Setting $CPATH and $LIBRARY_PATH to make sure both `gcc' and `xgcc' find the
258 # library headers and binaries, regarless of the language being compiled.
260 # Note: When building the Java AWT GTK peer, the build system doesn't honor
261 # `--with-gmp' et al., e.g., when building
262 # `libjava/classpath/native/jni/java-math/gnu_java_math_GMP.c', so we just add
263 # them to $CPATH and $LIBRARY_PATH in this case.
265 # Likewise, the LTO code doesn't find zlib.
267 # Cross-compiling, we need gcc not to read ./specs in order to build the g++
268 # compiler (after the specs for the cross-gcc are created). Having
269 # LIBRARY_PATH= makes gcc read the specs from ., and the build breaks.
271 CPATH = optionals (targetPlatform == hostPlatform) (makeSearchPathOutput "dev" "include" ([]
272 ++ optional (zlib != null) zlib
273 ++ optional langJava boehmgc
274 ++ optionals javaAwtGtk xlibs
275 ++ optionals javaAwtGtk [ gmp mpfr ]
278 LIBRARY_PATH = optionals (targetPlatform == hostPlatform) (makeLibraryPath ([]
279 ++ optional (zlib != null) zlib
280 ++ optional langJava boehmgc
281 ++ optionals javaAwtGtk xlibs
282 ++ optionals javaAwtGtk [ gmp mpfr ]
286 (import ../common/extra-target-flags.nix {
287 inherit lib stdenv crossStageStatic libcCross threadsCross;
289 EXTRA_FLAGS_FOR_TARGET
290 EXTRA_LDFLAGS_FOR_TARGET
294 inherit langC langCC langObjC langObjCpp langFortran langGo version;
298 enableParallelBuilding = true;
299 inherit enableMultilib;
301 inherit (stdenv) is64bit;
304 homepage = "https://gcc.gnu.org/";
305 license = lib.licenses.gpl3Plus; # runtime support libraries are typically LGPLv3+
306 description = "GNU Compiler Collection, version ${version}"
307 + (if stripped then "" else " (with debugging info)");
310 The GNU Compiler Collection includes compiler front ends for C, C++,
311 Objective-C, Fortran, OpenMP for C/C++/Fortran, Java, and Ada, as well
312 as libraries for these languages (libstdc++, libgcj, libgomp,...).
314 GCC development is a part of the GNU Project, aiming to improve the
315 compiler used in the GNU system including the GNU/Linux variant.
318 maintainers = with lib.maintainers; [ peti veprbl ];
321 lib.platforms.linux ++
322 lib.platforms.freebsd ++
323 lib.platforms.illumos ++
324 lib.platforms.darwin;
325 badPlatforms = [ "x86_64-darwin" ];
329 // optionalAttrs (targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt" && crossStageStatic) {
330 makeFlags = [ "all-gcc" "all-target-libgcc" ];
331 installTargets = "install-gcc install-target-libgcc";
334 // optionalAttrs (enableMultilib) { dontMoveLib64 = true; }
336 // optionalAttrs (langJava) {
338 target="$(echo "$out/libexec/gcc"/*/*/ecj*)"
339 patchelf --set-rpath "$(patchelf --print-rpath "$target"):$out/lib" "$target"