4 , withoutTargetLibc, libcCross
8 , apple-sdk, binutils, gmp, mpfr, libmpc, isl
13 , disableGdbPlugin ? !enablePlugin
26 , disableBootstrap ? stdenv.targetPlatform != stdenv.hostPlatform
29 assert !enablePlugin -> disableGdbPlugin;
31 # Note [Windows Exception Handling]
32 # sjlj (short jump long jump) exception handling makes no sense on x86_64,
33 # it's forcably slowing programs down as it produces a constant overhead.
34 # On x86_64 we have SEH (Structured Exception Handling) and we should use
35 # that. On i686, we do not have SEH, and have to use sjlj with dwarf2.
36 # Hence it's now conditional on x86_32 (i686 is 32bit).
38 # ref: https://stackoverflow.com/questions/15670169/what-is-difference-between-sjlj-vs-dwarf-vs-seh
43 buildPlatform hostPlatform targetPlatform;
45 # See https://github.com/NixOS/nixpkgs/pull/209870#issuecomment-1500550903
46 disableBootstrap' = disableBootstrap && !langFortran && !langGo;
48 crossMingw = targetPlatform != hostPlatform && targetPlatform.isMinGW;
49 crossDarwin = targetPlatform != hostPlatform && targetPlatform.libc == "libSystem";
51 targetPrefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform)
52 "${stdenv.targetPlatform.config}-";
55 # Ensure that -print-prog-name is able to find the correct programs.
57 "--with-as=${if targetPackages.stdenv.cc.bintools.isLLVM then binutils else targetPackages.stdenv.cc.bintools}/bin/${targetPlatform.config}-as"
58 "--with-ld=${targetPackages.stdenv.cc.bintools}/bin/${targetPlatform.config}-ld"
60 ++ (if withoutTargetLibc then [
66 "--disable-libquadmath"
67 (lib.enableFeature enableShared "shared")
68 "--disable-libatomic" # requires libc
69 "--disable-decimal-float" # requires libc
70 "--disable-libmpx" # requires libc
71 ] ++ lib.optionals crossMingw [
72 "--with-headers=${lib.getDev libcCross}/include"
77 "--disable-win32-registry"
78 "--enable-hash-synchronization"
81 # To keep ABI compatibility with upstream mingw-w64
82 "--enable-fully-dynamic-string"
83 ] ++ lib.optionals (crossMingw && targetPlatform.isx86_32) [
84 # See Note [Windows Exception Handling]
85 "--enable-sjlj-exceptions"
88 (if crossDarwin then "--with-sysroot=${lib.getLib libcCross}/share/sysroot"
89 else "--with-headers=${lib.getDev libcCross}${libcCross.incdir or "/include"}")
90 "--enable-__cxa_atexit"
92 "--enable-threads=${if targetPlatform.isUnix then "posix"
93 else if targetPlatform.isWindows then (threadsCross.model or "win32")
96 ] ++ lib.optionals (targetPlatform.libc == "uclibc" || targetPlatform.libc == "musl") [
97 # libsanitizer requires netrom/netrom.h which is not
98 # available in uclibc.
99 "--disable-libsanitizer"
100 ] ++ lib.optional (targetPlatform.libc == "newlib" || targetPlatform.libc == "newlib-nano") "--with-newlib"
101 ++ lib.optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
107 "--with-gmp-include=${gmp.dev}/include"
108 "--with-gmp-lib=${gmp.out}/lib"
109 "--with-mpfr-include=${mpfr.dev}/include"
110 "--with-mpfr-lib=${mpfr.out}/lib"
111 "--with-mpc=${libmpc}"
113 ++ lib.optionals (!withoutTargetLibc) [
114 (if libcCross == null
116 # GCC will search for the headers relative to SDKROOT on Darwin, so it will find them in the store.
117 if targetPlatform.isDarwin then "--with-native-system-header-dir=/usr/include"
118 else "--with-native-system-header-dir=${lib.getDev stdenv.cc.libc}/include"
120 else "--with-native-system-header-dir=${lib.getDev libcCross}${libcCross.incdir or "/include"}")
121 # gcc builds for cross-compilers (build != host) or cross-built
122 # gcc (host != target) always apply the offset prefix to disentangle
123 # target headers from build or host headers:
124 # ${with_build_sysroot}${native_system_header_dir}
125 # or ${test_exec_prefix}/${target_noncanonical}/sys-include
126 # or ${with_sysroot}${native_system_header_dir}
127 # While native build (build == host == target) uses passed headers
129 # ${with_build_sysroot}${native_system_header_dir}
131 # Nixpkgs uses flat directory structure for both native and cross
132 # cases. As a result libc headers don't get found for cross case
133 # and many modern features get disabled (libssp is used instead of
134 # target-specific implementations and similar). More details at:
135 # https://github.com/NixOS/nixpkgs/pull/181802#issuecomment-1186822355
137 # We pick "/" path to effectively avoid sysroot offset and make it work
139 # Darwin requires using the SDK as the sysroot for `SDKROOT` to work correctly.
140 "--with-build-sysroot=${if targetPlatform.isDarwin then apple-sdk.sdkroot else "/"}"
141 # Same with the stdlibc++ headers embedded in the gcc output
142 "--with-gxx-include-dir=${placeholder "out"}/include/c++/${version}/"
145 # Basic configuration
147 # Force target prefix. The behavior if `--target` and `--host`
148 # are specified is inconsistent: Sometimes specifying `--target`
149 # always causes a prefix to be generated, sometimes it's only
150 # added if the `--host` and `--target` differ. This means that
151 # sometimes there may be a prefix even though nixpkgs doesn't
152 # expect one and sometimes there may be none even though nixpkgs
153 # expects one (since not all information is serialized into the
154 # config attribute). The easiest way out of these problems is to
155 # always set the program prefix, so gcc will conform to our
157 "--program-prefix=${targetPrefix}"
159 (lib.enableFeature enableLTO "lto")
160 "--disable-libstdcxx-pch"
161 "--without-included-gettext"
164 "--enable-languages=${
165 lib.concatStringsSep ","
166 ( lib.optional langC "c"
167 ++ lib.optional langCC "c++"
168 ++ lib.optional langD "d"
169 ++ lib.optional langFortran "fortran"
170 ++ lib.optional langAda "ada"
171 ++ lib.optional langGo "go"
172 ++ lib.optional langObjC "objc"
173 ++ lib.optional langObjCpp "obj-c++"
174 ++ lib.optionals crossDarwin [ "objc" "obj-c++" ]
175 ++ lib.optional langJit "jit"
176 ++ lib.optional langRust "rust"
181 ++ (if (enableMultilib || targetPlatform.isAvr)
182 then ["--enable-multilib" "--disable-libquadmath"]
183 else ["--disable-multilib"])
184 ++ lib.optional (!enableShared) "--disable-shared"
185 ++ lib.singleton (lib.enableFeature enablePlugin "plugin")
186 # Libcc1 is the GCC cc1 plugin for the GDB debugger which is only used by gdb
187 ++ lib.optional disableGdbPlugin "--disable-libcc1"
189 # Support -m32 on powerpc64le/be
190 ++ lib.optional (targetPlatform.system == "powerpc64le-linux")
191 "--enable-targets=powerpcle-linux"
192 ++ lib.optional (targetPlatform.system == "powerpc64-linux")
193 "--enable-targets=powerpc-linux"
195 # Fix "unknown long double size, cannot define BFP_FMT"
196 ++ lib.optional (targetPlatform.isPower && targetPlatform.isMusl)
197 "--disable-decimal-float"
200 ++ lib.optional (isl != null) "--with-isl=${isl}"
202 # Ada options, gcc can't build the runtime library for a cross compiler
203 ++ lib.optional langAda
204 (if hostPlatform == targetPlatform
205 then "--enable-libada"
206 else "--disable-libada")
208 ++ import ../common/platform-flags.nix { inherit (stdenv) targetPlatform; inherit lib; }
209 ++ lib.optionals (targetPlatform != hostPlatform) crossConfigureFlags
210 ++ lib.optional disableBootstrap' "--disable-bootstrap"
212 # Platform-specific flags
213 ++ lib.optional (targetPlatform == hostPlatform && targetPlatform.isx86_32) "--with-arch=${stdenv.hostPlatform.parsed.cpu.name}"
214 ++ lib.optional targetPlatform.isNetBSD "--disable-libssp" # Provided by libc.
215 ++ lib.optionals hostPlatform.isSunOS [
216 "--enable-long-long" "--enable-libssp" "--enable-threads=posix" "--disable-nls" "--enable-__cxa_atexit"
217 # On Illumos/Solaris GNU as is preferred
218 "--with-gnu-as" "--without-gnu-ld"
220 ++ lib.optional (targetPlatform.libc == "musl")
221 # musl at least, disable: https://git.buildroot.net/buildroot/commit/?id=873d4019f7fb00f6a80592224236b3ba7d657865
223 ++ lib.optionals (targetPlatform == hostPlatform && targetPlatform.libc == "musl") [
224 "--disable-libsanitizer"
226 "libat_cv_have_ifunc=no"
227 "--disable-gnu-indirect-function"
229 ++ lib.optionals langJit [
230 "--enable-host-shared"
232 ++ lib.optionals (langD) [
233 "--with-target-system-zlib=yes"
235 # On mips64-unknown-linux-gnu libsanitizer defines collide with
236 # glibc's definitions and fail the build. It was fixed in gcc-13+.
237 ++ lib.optionals (targetPlatform.isMips && targetPlatform.parsed.abi.name == "gnu" && lib.versions.major version == "12") [
238 "--disable-libsanitizer"
240 ++ lib.optionals targetPlatform.isAlpha [
241 # Workaround build failures like:
242 # cc1: error: fp software completion requires '-mtrap-precision=i' [-Werror]