1 # The Nixpkgs CC is not directly usable, since it doesn't know where
2 # the C library and standard header files are. Therefore the compiler
3 # produced by that package cannot be installed directly in a user
4 # environment and used from the command line. So we use a wrapper
5 # script that sets up the right environment variables so that the
6 # compiler and the linker just "work".
11 , cc ? null, libc ? null, bintools, coreutils ? null, shell ? stdenvNoCC.shell
13 , nativeTools, noLibc ? false, nativeLibc, nativePrefix ? ""
14 , propagateDoc ? cc != null && cc ? man
15 , extraTools ? [], extraPackages ? [], extraBuildCommands ? ""
17 , isGNU ? false, isClang ? cc.isClang or false, isCcache ? cc.isCcache or false, gnugrep ? null
21 # Whether or not to add `-B` and `-L` to `nix-support/cc-{c,ld}flags`
24 # Always add these flags for Clang, because in order to compile (most
25 # software) it needs libraries that are shipped and compiled with gcc.
28 # Never add these flags for a build!=host cross-compiler or a host!=target
29 # ("cross-built-native") compiler; currently nixpkgs has a special build
30 # path for these (`crossStageStatic`). Hopefully at some point that build
31 # path will be merged with this one and this conditional will be removed.
32 else if (with stdenvNoCC; buildPlatform != hostPlatform || hostPlatform != targetPlatform) then false
34 # Never add these flags when wrapping the bootstrapFiles' compiler; it has a
35 # /usr/-like layout with everything smashed into a single outpath, so it has
36 # no trouble finding its own libraries.
37 else if (cc.passthru.isFromBootstrapFiles or false) then false
39 # Add these flags when wrapping `xgcc` (the first compiler that nixpkgs builds)
40 else if (cc.passthru.isXgcc or false) then true
42 # Add these flags when wrapping `stdenv.cc`
43 else if (cc.stdenv.cc.cc.passthru.isXgcc or false) then true
45 # Do not add these flags in any other situation. This is `false` mainly to
46 # prevent these flags from being added when wrapping *old* versions of gcc
47 # (e.g. `gcc6Stdenv`), since they will cause the old gcc to get `-B` and
48 # `-L` flags pointing at the new gcc's libstdc++ headers. Example failure:
49 # https://hydra.nixos.org/build/213125495
52 # the derivation at which the `-B` and `-L` flags added by `useCcForLibs` will point
53 , gccForLibs ? if useCcForLibs then cc else null
54 , fortify-headers ? null
55 , includeFortifyHeaders ? null
60 assert nativeTools -> !propagateDoc && nativePrefix != "";
61 assert !nativeTools ->
62 cc != null && coreutils != null && gnugrep != null;
63 assert !(nativeLibc && noLibc);
64 assert (noLibc || nativeLibc) == (libc == null);
68 inherit (stdenv) hostPlatform targetPlatform;
70 includeFortifyHeaders' = if includeFortifyHeaders != null
71 then includeFortifyHeaders
72 else (targetPlatform.libc == "musl" && isGNU);
74 # Prefix for binaries. Customarily ends with a dash separator.
76 # TODO(@Ericson2314) Make unconditional, or optional but always true by
78 targetPrefix = lib.optionalString (targetPlatform != hostPlatform)
79 (targetPlatform.config + "-");
81 ccVersion = lib.getVersion cc;
82 ccName = lib.removePrefix targetPrefix (lib.getName cc);
84 libc_bin = optionalString (libc != null) (getBin libc);
85 libc_dev = optionalString (libc != null) (getDev libc);
86 libc_lib = optionalString (libc != null) (getLib libc);
88 + optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}";
90 # The wrapper scripts use 'cat' and 'grep', so we may need coreutils.
91 coreutils_bin = optionalString (!nativeTools) (getBin coreutils);
93 # The "suffix salt" is a arbitrary string added in the end of env vars
94 # defined by cc-wrapper's hooks so that multiple cc-wrappers can be used
95 # without interfering. For the moment, it is defined as the target triple,
96 # adjusted to be a valid bash identifier. This should be considered an
97 # unstable implementation detail, however.
98 suffixSalt = replaceStrings ["-" "."] ["_" "_"] targetPlatform.config;
100 expand-response-params =
101 lib.optionalString ((buildPackages.stdenv.hasCC or false) && buildPackages.stdenv.cc != "/dev/null") (import ../expand-response-params { inherit (buildPackages) stdenv; });
103 useGccForLibs = useCcForLibs
105 && !stdenv.targetPlatform.isDarwin
106 && !(stdenv.targetPlatform.useLLVM or false)
107 && !(stdenv.targetPlatform.useAndroidPrebuilt or false)
108 && !(stdenv.targetPlatform.isiOS or false)
109 && gccForLibs != null;
110 gccForLibs_solib = getLib gccForLibs
111 + optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}";
113 # The following two functions, `isGccArchSupported` and
114 # `isGccTuneSupported`, only handle those situations where a flag
115 # (`-march` or `-mtune`) is accepted by one compiler but rejected
116 # by another, and both compilers are relevant to nixpkgs. We are
117 # not trying to maintain a complete list of all flags accepted by
118 # all versions of all compilers ever in nixpkgs.
120 # The two main cases of interest are:
122 # - One compiler is gcc and the other is clang
123 # - One compiler is pkgs.gcc and the other is bootstrap-files.gcc
124 # -- older compilers (for example bootstrap's GCC 5) fail with
125 # -march=too-modern-cpu
127 isGccArchSupported = arch:
128 if targetPlatform.isPower then false else # powerpc does not allow -march=
131 x86-64-v2 = versionAtLeast ccVersion "11.0";
132 x86-64-v3 = versionAtLeast ccVersion "11.0";
133 x86-64-v4 = versionAtLeast ccVersion "11.0";
136 skylake = versionAtLeast ccVersion "6.0";
137 skylake-avx512 = versionAtLeast ccVersion "6.0";
138 cannonlake = versionAtLeast ccVersion "8.0";
139 icelake-client = versionAtLeast ccVersion "8.0";
140 icelake-server = versionAtLeast ccVersion "8.0";
141 cascadelake = versionAtLeast ccVersion "9.0";
142 cooperlake = versionAtLeast ccVersion "10.0";
143 tigerlake = versionAtLeast ccVersion "10.0";
144 knm = versionAtLeast ccVersion "8.0";
145 alderlake = versionAtLeast ccVersion "12.0";
148 znver1 = versionAtLeast ccVersion "6.0";
149 znver2 = versionAtLeast ccVersion "9.0";
150 znver3 = versionAtLeast ccVersion "11.0";
151 znver4 = versionAtLeast ccVersion "13.0";
155 x86-64-v2 = versionAtLeast ccVersion "12.0";
156 x86-64-v3 = versionAtLeast ccVersion "12.0";
157 x86-64-v4 = versionAtLeast ccVersion "12.0";
160 cannonlake = versionAtLeast ccVersion "5.0";
161 icelake-client = versionAtLeast ccVersion "7.0";
162 icelake-server = versionAtLeast ccVersion "7.0";
163 knm = versionAtLeast ccVersion "7.0";
164 alderlake = versionAtLeast ccVersion "16.0";
167 znver1 = versionAtLeast ccVersion "4.0";
168 znver2 = versionAtLeast ccVersion "9.0";
169 znver3 = versionAtLeast ccVersion "12.0";
170 znver4 = versionAtLeast ccVersion "16.0";
175 isGccTuneSupported = tune:
176 # for x86 -mtune= takes the same values as -march, plus two more:
177 if targetPlatform.isx86 then
181 }.${tune} or (isGccArchSupported tune)
182 # on arm64, the -mtune= values are specific processors
183 else if targetPlatform.isAarch64 then
186 cortex-a53 = versionAtLeast ccVersion "4.8"; # gcc 8c075f
187 cortex-a72 = versionAtLeast ccVersion "5.1"; # gcc d8f70d
188 "cortex-a72.cortex-a53" = versionAtLeast ccVersion "5.1"; # gcc d8f70d
192 cortex-a53 = versionAtLeast ccVersion "3.9"; # llvm dfc5d1
195 else if targetPlatform.isPower then
196 # powerpc does not support -march
198 else if targetPlatform.isMips then
199 # for mips -mtune= takes the same values as -march
200 isGccArchSupported tune
204 # Clang does not support as many `-mtune=` values as gcc does;
205 # this function will return the best possible approximation of the
206 # provided `-mtune=` value, or `null` if none exists.
208 # Note: this function can make use of ccVersion; for example, `if
209 # versionOlder ccVersion "12" then ...`
210 findBestTuneApproximation = tune:
211 let guess = if isClang
213 # clang does not tune for big.LITTLE chips
214 "cortex-a72.cortex-a53" = "cortex-a72";
217 in if isGccTuneSupported guess
221 darwinPlatformForCC = optionalString stdenv.targetPlatform.isDarwin (
222 if (targetPlatform.darwinPlatform == "macos" && isGNU) then "macosx"
223 else targetPlatform.darwinPlatform
226 darwinMinVersion = optionalString stdenv.targetPlatform.isDarwin (
227 stdenv.targetPlatform.darwinMinVersion
230 darwinMinVersionVariable = optionalString stdenv.targetPlatform.isDarwin
231 stdenv.targetPlatform.darwinMinVersionVariable;
234 assert includeFortifyHeaders' -> fortify-headers != null;
236 # Ensure bintools matches
237 assert libc_bin == bintools.libc_bin;
238 assert libc_dev == bintools.libc_dev;
239 assert libc_lib == bintools.libc_lib;
240 assert nativeTools == bintools.nativeTools;
241 assert nativeLibc == bintools.nativeLibc;
242 assert nativePrefix == bintools.nativePrefix;
244 stdenv.mkDerivation {
246 + (if name != "" then name else "${ccName}-wrapper");
247 version = optionalString (cc != null) ccVersion;
249 preferLocalBuild = true;
251 outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ];
254 inherit targetPrefix suffixSalt;
255 # "cc" is the generic name for a C compiler, but there is no one for package
256 # providing the linker and related tools. The two we use now are GNU
257 # Binutils, and Apple's "cctools"; "bintools" as an attempt to find an
258 # unused middle-ground name that evokes both.
260 inherit cc libc libcxx nativeTools nativeLibc nativePrefix isGNU isClang;
262 emacsBufferSetup = pkgs: ''
263 ; We should handle propagation here too
266 (when (file-directory-p (concat arg "/include"))
267 (setenv "NIX_CFLAGS_COMPILE_${suffixSalt}" (concat (getenv "NIX_CFLAGS_COMPILE_${suffixSalt}") " -isystem " arg "/include"))))
268 '(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)}))
271 inherit expand-response-params;
277 dontConfigure = true;
278 enableParallelBuilding = true;
284 wrapper = ./cc-wrapper.sh;
288 mkdir -p $out/bin $out/nix-support
294 export use_response_file_by_default=${if isClang && !isCcache then "1" else "0"}
295 substituteAll "$wrapper" "$out/bin/$dst"
296 chmod +x "$out/bin/$dst"
300 + (if nativeTools then ''
301 echo ${if targetPlatform.isDarwin then cc else nativePrefix} > $out/nix-support/orig-cc
303 ccPath="${if targetPlatform.isDarwin then cc else nativePrefix}/bin"
305 echo $cc > $out/nix-support/orig-cc
310 # Create symlinks to everything in the bintools wrapper.
312 for bbin in $bintools/bin/*; do
314 ln -s "$bbin" "$out/bin/$(basename $bbin)"
318 # We export environment variables pointing to the wrapped nonstandard
319 # cmds, lest some lousy configure script use those to guess compiler
322 export named_cc=${targetPrefix}cc
323 export named_cxx=${targetPrefix}c++
325 if [ -e $ccPath/${targetPrefix}gcc ]; then
326 wrap ${targetPrefix}gcc $wrapper $ccPath/${targetPrefix}gcc
327 ln -s ${targetPrefix}gcc $out/bin/${targetPrefix}cc
328 export named_cc=${targetPrefix}gcc
329 export named_cxx=${targetPrefix}g++
330 elif [ -e $ccPath/clang ]; then
331 wrap ${targetPrefix}clang $wrapper $ccPath/clang
332 ln -s ${targetPrefix}clang $out/bin/${targetPrefix}cc
333 export named_cc=${targetPrefix}clang
334 export named_cxx=${targetPrefix}clang++
337 if [ -e $ccPath/${targetPrefix}g++ ]; then
338 wrap ${targetPrefix}g++ $wrapper $ccPath/${targetPrefix}g++
339 ln -s ${targetPrefix}g++ $out/bin/${targetPrefix}c++
340 elif [ -e $ccPath/clang++ ]; then
341 wrap ${targetPrefix}clang++ $wrapper $ccPath/clang++
342 ln -s ${targetPrefix}clang++ $out/bin/${targetPrefix}c++
345 if [ -e $ccPath/${targetPrefix}cpp ]; then
346 wrap ${targetPrefix}cpp $wrapper $ccPath/${targetPrefix}cpp
347 elif [ -e $ccPath/cpp ]; then
348 wrap ${targetPrefix}cpp $wrapper $ccPath/cpp
352 # No need to wrap gnat, gnatkr, gnatname or gnatprep; we can just symlink them in
353 + optionalString cc.langAda or false ''
354 for cmd in gnatbind gnatchop gnatclean gnatlink gnatls gnatmake; do
355 wrap ${targetPrefix}$cmd ${./gnat-wrapper.sh} $ccPath/${targetPrefix}$cmd
358 for cmd in gnat gnatkr gnatname gnatprep; do
359 ln -s $ccPath/${targetPrefix}$cmd $out/bin/${targetPrefix}$cmd
362 # this symlink points to the unwrapped gnat's output "out". It is used by
363 # our custom gprconfig compiler description to find GNAT's ada runtime. See
364 # ../../development/tools/build-managers/gprbuild/{boot.nix, nixpkgs-gnat.xml}
365 ln -sf ${cc} $out/nix-support/gprconfig-gnat-unwrapped
368 + optionalString cc.langD or false ''
369 wrap ${targetPrefix}gdc $wrapper $ccPath/${targetPrefix}gdc
372 + optionalString cc.langFortran or false ''
373 wrap ${targetPrefix}gfortran $wrapper $ccPath/${targetPrefix}gfortran
374 ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}g77
375 ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}f77
376 export named_fc=${targetPrefix}gfortran
379 + optionalString cc.langJava or false ''
380 wrap ${targetPrefix}gcj $wrapper $ccPath/${targetPrefix}gcj
383 + optionalString cc.langGo or false ''
384 wrap ${targetPrefix}gccgo $wrapper $ccPath/${targetPrefix}gccgo
385 wrap ${targetPrefix}go ${./go-wrapper.sh} $ccPath/${targetPrefix}go
389 propagatedBuildInputs = [ bintools ] ++ extraTools ++ optionals cc.langD or cc.langJava or false [ zlib ];
390 depsTargetTargetPropagated = optional (libcxx != null) libcxx ++ extraPackages;
393 ../setup-hooks/role.bash
394 ] ++ lib.optional (cc.langC or true) ./setup-hook.sh
395 ++ lib.optional (cc.langFortran or false) ./fortran-hook.sh
396 ++ lib.optional (targetPlatform.isWindows) (stdenv.mkDerivation {
397 name = "win-dll-hook.sh";
400 echo addToSearchPath "LINK_DLL_FOLDERS" "${cc_solib}/lib" > $out
401 echo addToSearchPath "LINK_DLL_FOLDERS" "${cc_solib}/lib64" >> $out
402 echo addToSearchPath "LINK_DLL_FOLDERS" "${cc_solib}/lib32" >> $out
407 # Ensure flags files exists, as some other programs cat them. (That these
408 # are considered an exposed interface is a bit dubious, but fine for now.)
410 touch "$out/nix-support/cc-cflags"
411 touch "$out/nix-support/cc-ldflags"
414 # Backwards compatibility for packages expecting this file, e.g. with
415 # `$NIX_CC/nix-support/dynamic-linker`.
417 # TODO(@Ericson2314): Remove this after stable release and force
418 # everyone to refer to bintools-wrapper directly.
420 if [[ -f "$bintools/nix-support/dynamic-linker" ]]; then
421 ln -s "$bintools/nix-support/dynamic-linker" "$out/nix-support"
423 if [[ -f "$bintools/nix-support/dynamic-linker-m32" ]]; then
424 ln -s "$bintools/nix-support/dynamic-linker-m32" "$out/nix-support"
429 ## GCC libs for non-GCC support
431 + optionalString (useGccForLibs && isClang) ''
433 echo "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-cflags
435 + optionalString useGccForLibs ''
436 echo "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-ldflags
437 echo "-L${gccForLibs_solib}/lib" >> $out/nix-support/cc-ldflags
440 # TODO We would like to connect this to `useGccForLibs`, but we cannot yet
441 # because `libcxxStdenv` on linux still needs this. Maybe someday we'll
442 # always set `useLLVM` on Darwin, and maybe also break down `useLLVM` into
443 # fine-grained use flags (libgcc vs compiler-rt, ld.lld vs legacy, libc++
444 # vs libstdc++, etc.) since Darwin isn't `useLLVM` on all counts. (See
445 # https://clang.llvm.org/docs/Toolchain.html for all the axes one might
446 # break `useLLVM` into.)
447 + optionalString (isClang
448 && targetPlatform.isLinux
449 && !(stdenv.targetPlatform.useAndroidPrebuilt or false)
450 && !(stdenv.targetPlatform.useLLVM or false)
451 && gccForLibs != null) (''
452 echo "--gcc-toolchain=${gccForLibs}" >> $out/nix-support/cc-cflags
454 # Pull in 'cc.out' target to get 'libstdc++fs.a'. It should be in
455 # 'cc.lib'. But it's a gcc package bug.
456 # TODO(trofi): remove once gcc is fixed to move libraries to .lib output.
457 echo "-L${gccForLibs}/${optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}"}/lib" >> $out/nix-support/cc-ldflags
459 # this ensures that when clang passes -lgcc_s to lld (as it does
460 # when building e.g. firefox), lld is able to find libgcc_s.so
461 + concatMapStrings (libgcc: ''
462 echo "-L${libgcc}/lib" >> $out/nix-support/cc-ldflags
463 '') (lib.toList (gccForLibs.libgcc or [])))
466 ## General libc support
469 # The "-B${libc_lib}/lib/" flag is a quick hack to force gcc to link
470 # against the crt1.o from our own glibc, rather than the one in
471 # /usr/lib. (This is only an issue when using an `impure'
472 # compiler/linker, i.e., one that searches /usr/lib and so on.)
474 # Unfortunately, setting -B appears to override the default search
475 # path. Thus, the gcc-specific "../includes-fixed" directory is
476 # now longer searched and glibc's <limits.h> header fails to
477 # compile, because it uses "#include_next <limits.h>" to find the
478 # limits.h file in ../includes-fixed. To remedy the problem,
479 # another -idirafter is necessary to add that directory again.
480 + optionalString (libc != null) (''
481 touch "$out/nix-support/libc-cflags"
482 touch "$out/nix-support/libc-ldflags"
483 echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags
484 '' + optionalString (!(cc.langD or false)) ''
485 echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags
486 '' + optionalString (isGNU && (!(cc.langD or false))) ''
487 for dir in "${cc}"/lib/gcc/*/*/include-fixed; do
488 echo '-idirafter' ''${dir} >> $out/nix-support/libc-cflags
492 echo "${libc_lib}" > $out/nix-support/orig-libc
493 echo "${libc_dev}" > $out/nix-support/orig-libc-dev
495 # fortify-headers is a set of wrapper headers that augment libc
496 # and use #include_next to pass through to libc's true
497 # implementations, so must appear before them in search order.
498 # in theory a correctly placed -idirafter could be used, but in
499 # practice the compiler may have been built with a --with-headers
500 # like option that forces the libc headers before all -idirafter,
501 # hence -isystem here.
502 + optionalString includeFortifyHeaders' ''
503 echo "-isystem ${fortify-headers}/include" >> $out/nix-support/libc-cflags
507 ## General libc++ support
510 # We have a libc++ directly, we have one via "smuggled" GCC, or we have one
511 # bundled with the C compiler because it is GCC
512 + optionalString (libcxx != null || (useGccForLibs && gccForLibs.langCC or false) || (isGNU && cc.langCC or false)) ''
513 touch "$out/nix-support/libcxx-cxxflags"
514 touch "$out/nix-support/libcxx-ldflags"
516 # Adding -isystem flags should be done only for clang; gcc
517 # already knows how to find its own libstdc++, and adding
518 # additional -isystem flags will confuse gfortran (see
519 # https://github.com/NixOS/nixpkgs/pull/209870#issuecomment-1500550903)
520 + optionalString (libcxx == null && isClang && (useGccForLibs && gccForLibs.langCC or false)) ''
521 for dir in ${gccForLibs}${lib.optionalString (hostPlatform != targetPlatform) "/${targetPlatform.config}"}/include/c++/*; do
522 echo "-isystem $dir" >> $out/nix-support/libcxx-cxxflags
524 for dir in ${gccForLibs}${lib.optionalString (hostPlatform != targetPlatform) "/${targetPlatform.config}"}/include/c++/*/${targetPlatform.config}; do
525 echo "-isystem $dir" >> $out/nix-support/libcxx-cxxflags
528 + optionalString (libcxx.isLLVM or false) ''
529 echo "-isystem ${lib.getDev libcxx}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags
530 echo "-isystem ${lib.getDev libcxx.cxxabi}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags
531 echo "-stdlib=libc++" >> $out/nix-support/libcxx-ldflags
532 echo "-l${libcxx.cxxabi.libName}" >> $out/nix-support/libcxx-ldflags
539 # GCC shows ${cc_solib}/lib in `gcc -print-search-dirs', but not
540 # ${cc_solib}/lib64 (even though it does actually search there...)..
541 # This confuses libtool. So add it to the compiler tool search
543 + optionalString (!nativeTools) ''
544 if [ -e "${cc_solib}/lib64" -a ! -L "${cc_solib}/lib64" ]; then
545 ccLDFlags+=" -L${cc_solib}/lib64"
546 ccCFlags+=" -B${cc_solib}/lib64"
548 ccLDFlags+=" -L${cc_solib}/lib"
549 ccCFlags+=" -B${cc_solib}/lib"
551 '' + optionalString cc.langAda or false ''
552 touch "$out/nix-support/gnat-cflags"
553 touch "$out/nix-support/gnat-ldflags"
554 basePath=$(echo $cc/lib/*/*/*)
555 ccCFlags+=" -B$basePath -I$basePath/adainclude"
556 gnatCFlags="-I$basePath/adainclude -I$basePath/adalib"
558 echo "$gnatCFlags" >> $out/nix-support/gnat-cflags
560 echo "$ccLDFlags" >> $out/nix-support/cc-ldflags
561 echo "$ccCFlags" >> $out/nix-support/cc-cflags
562 '' + optionalString (targetPlatform.isDarwin && (libcxx != null) && (cc.isClang or false)) ''
563 echo " -L${lib.getLib libcxx}/lib" >> $out/nix-support/cc-ldflags
567 ## Man page and info support
569 + optionalString propagateDoc ''
571 ln -s ${cc.info} $info
572 '' + optionalString (cc.langD or cc.langJava or false) ''
573 echo "-B${zlib}${zlib.libdir or "/lib/"}" >> $out/nix-support/libc-cflags
580 export hardening_unsupported_flags="${builtins.concatStringsSep " " (cc.hardeningUnsupportedFlags or [])}"
583 # Machine flags. These are necessary to support
585 # TODO: We should make a way to support miscellaneous machine
586 # flags and other gcc flags as well.
588 # Always add -march based on cpu in triple. Sometimes there is a
589 # discrepency (x86_64 vs. x86-64), so we provide an "arch" arg in
591 # TODO: aarch64-darwin has mcpu incompatible with gcc
592 + optionalString ((targetPlatform ? gcc.arch) && (isClang || !(stdenv.isDarwin && stdenv.isAarch64)) &&
593 isGccArchSupported targetPlatform.gcc.arch) ''
594 echo "-march=${targetPlatform.gcc.arch}" >> $out/nix-support/cc-cflags-before
597 # -mcpu is not very useful, except on PowerPC where it is used
598 # instead of march. On all other platforms you should use mtune
600 # TODO: aarch64-darwin has mcpu incompatible with gcc
601 + optionalString ((targetPlatform ? gcc.cpu) && (isClang || !(stdenv.isDarwin && stdenv.isAarch64))) ''
602 echo "-mcpu=${targetPlatform.gcc.cpu}" >> $out/nix-support/cc-cflags-before
605 # -mfloat-abi only matters on arm32 but we set it here
606 # unconditionally just in case. If the abi specifically sets hard
607 # vs. soft floats we use it here.
608 + optionalString (targetPlatform ? gcc.float-abi) ''
609 echo "-mfloat-abi=${targetPlatform.gcc.float-abi}" >> $out/nix-support/cc-cflags-before
611 + optionalString (targetPlatform ? gcc.fpu) ''
612 echo "-mfpu=${targetPlatform.gcc.fpu}" >> $out/nix-support/cc-cflags-before
614 + optionalString (targetPlatform ? gcc.mode) ''
615 echo "-mmode=${targetPlatform.gcc.mode}" >> $out/nix-support/cc-cflags-before
617 + optionalString (targetPlatform ? gcc.thumb) ''
618 echo "-m${if targetPlatform.gcc.thumb then "thumb" else "arm"}" >> $out/nix-support/cc-cflags-before
620 + (let tune = if targetPlatform ? gcc.tune
621 then findBestTuneApproximation targetPlatform.gcc.tune
623 in optionalString (tune != null) ''
624 echo "-mtune=${tune}" >> $out/nix-support/cc-cflags-before
627 # TODO: categorize these and figure out a better place for them
628 + optionalString targetPlatform.isWindows ''
629 hardening_unsupported_flags+=" pic"
630 '' + optionalString targetPlatform.isMinGW ''
631 hardening_unsupported_flags+=" stackprotector fortify"
632 '' + optionalString targetPlatform.isAvr ''
633 hardening_unsupported_flags+=" stackprotector pic"
634 '' + optionalString (targetPlatform.libc == "newlib" || targetPlatform.libc == "newlib-nano") ''
635 hardening_unsupported_flags+=" stackprotector fortify pie pic"
636 '' + optionalString (targetPlatform.libc == "musl" && targetPlatform.isx86_32) ''
637 hardening_unsupported_flags+=" stackprotector"
638 '' + optionalString targetPlatform.isNetBSD ''
639 hardening_unsupported_flags+=" stackprotector fortify"
640 '' + optionalString cc.langAda or false ''
641 hardening_unsupported_flags+=" format stackprotector strictoverflow"
642 '' + optionalString cc.langD or false ''
643 hardening_unsupported_flags+=" format"
644 '' + optionalString cc.langFortran or false ''
645 hardening_unsupported_flags+=" format"
646 '' + optionalString targetPlatform.isWasm ''
647 hardening_unsupported_flags+=" stackprotector fortify pie pic"
648 '' + optionalString targetPlatform.isMicroBlaze ''
649 hardening_unsupported_flags+=" stackprotector"
652 + optionalString (libc != null && targetPlatform.isAvr) ''
653 for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do
654 echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-crt1-cflags
658 + optionalString stdenv.targetPlatform.isDarwin ''
659 echo "-arch ${targetPlatform.darwinArch}" >> $out/nix-support/cc-cflags
662 + optionalString targetPlatform.isAndroid ''
663 echo "-D__ANDROID_API__=${targetPlatform.sdkVer}" >> $out/nix-support/cc-cflags
666 # There are a few tools (to name one libstdcxx5) which do not work
667 # well with multi line flags, so make the flags single line again
669 for flags in "$out/nix-support"/*flags*; do
670 substituteInPlace "$flags" --replace $'\n' ' '
673 substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
674 substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh
675 substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash
678 + optionalString cc.langAda or false ''
679 substituteAll ${./add-gnat-extra-flags.sh} $out/nix-support/add-gnat-extra-flags.sh
683 ## General Clang support
684 ## Needs to go after ^ because the for loop eats \n and makes this file an invalid script
686 + optionalString isClang ''
687 export defaultTarget=${targetPlatform.config}
688 substituteAll ${./add-clang-cc-cflags-before.sh} $out/nix-support/add-local-cc-cflags-before.sh
692 ## Extra custom steps
695 + lib.strings.concatStringsSep "; "
696 (lib.attrsets.mapAttrsToList
697 (name: value: "echo ${toString value} >> $out/nix-support/${name}")
704 # for substitution in utils.bash
705 expandResponseParams = "${expand-response-params}/bin/expand-response-params";
706 shell = getBin shell + shell.shellPath or "";
707 gnugrep_bin = optionalString (!nativeTools) gnugrep;
708 # stdenv.cc.cc should not be null and we have nothing better for now.
709 # if the native impure bootstrap is gotten rid of this can become `inherit cc;` again.
710 cc = optionalString (!nativeTools) cc;
711 wrapperName = "CC_WRAPPER";
712 inherit suffixSalt coreutils_bin bintools;
713 inherit libc_bin libc_dev libc_lib;
714 inherit darwinPlatformForCC darwinMinVersion darwinMinVersionVariable;
718 let cc_ = lib.optionalAttrs (cc != null) cc; in
719 (lib.optionalAttrs (cc_ ? meta) (removeAttrs cc.meta ["priority"])) //
721 lib.attrByPath ["meta" "description"] "System C compiler" cc_
722 + " (wrapper script)";
724 mainProgram = if name != "" then name else ccName;