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".
7
8{ name ? ""
9, lib
10, stdenvNoCC
11, cc ? null, libc ? null, bintools, coreutils ? null, shell ? stdenvNoCC.shell
12, zlib ? null
13, nativeTools, noLibc ? false, nativeLibc, nativePrefix ? ""
14, propagateDoc ? cc != null && cc ? man
15, extraTools ? [], extraPackages ? [], extraBuildCommands ? ""
16, nixSupport ? {}
17, isGNU ? false, isClang ? cc.isClang or false, isCcache ? cc.isCcache or false, gnugrep ? null
18, buildPackages ? {}
19, libcxx ? null
20
21# Whether or not to add `-B` and `-L` to `nix-support/cc-{c,ld}flags`
22, useCcForLibs ?
23
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.
26 if isClang then true
27
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
33
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
38
39 # Add these flags when wrapping `xgcc` (the first compiler that nixpkgs builds)
40 else if (cc.passthru.isXgcc or false) then true
41
42 # Add these flags when wrapping `stdenv.cc`
43 else if (cc.stdenv.cc.cc.passthru.isXgcc or false) then true
44
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
50 else false
51
52# the derivation at which the `-B` and `-L` flags added by `useCcForLibs` will point
53, gccForLibs ? if useCcForLibs then cc else null
54}:
55
56with lib;
57
58assert nativeTools -> !propagateDoc && nativePrefix != "";
59assert !nativeTools ->
60 cc != null && coreutils != null && gnugrep != null;
61assert !(nativeLibc && noLibc);
62assert (noLibc || nativeLibc) == (libc == null);
63
64let
65 stdenv = stdenvNoCC;
66 inherit (stdenv) hostPlatform targetPlatform;
67
68 # Prefix for binaries. Customarily ends with a dash separator.
69 #
70 # TODO(@Ericson2314) Make unconditional, or optional but always true by
71 # default.
72 targetPrefix = lib.optionalString (targetPlatform != hostPlatform)
73 (targetPlatform.config + "-");
74
75 ccVersion = lib.getVersion cc;
76 ccName = lib.removePrefix targetPrefix (lib.getName cc);
77
78 libc_bin = if libc == null then "" else getBin libc;
79 libc_dev = if libc == null then "" else getDev libc;
80 libc_lib = if libc == null then "" else getLib libc;
81 cc_solib = getLib cc
82 + optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}";
83
84 # The wrapper scripts use 'cat' and 'grep', so we may need coreutils.
85 coreutils_bin = if nativeTools then "" else getBin coreutils;
86
87 # The "suffix salt" is a arbitrary string added in the end of env vars
88 # defined by cc-wrapper's hooks so that multiple cc-wrappers can be used
89 # without interfering. For the moment, it is defined as the target triple,
90 # adjusted to be a valid bash identifier. This should be considered an
91 # unstable implementation detail, however.
92 suffixSalt = replaceStrings ["-" "."] ["_" "_"] targetPlatform.config;
93
94 expand-response-params =
95 lib.optionalString ((buildPackages.stdenv.hasCC or false) && buildPackages.stdenv.cc != "/dev/null") (import ../expand-response-params { inherit (buildPackages) stdenv; });
96
97 useGccForLibs = useCcForLibs
98 && libcxx == null
99 && !stdenv.targetPlatform.isDarwin
100 && !(stdenv.targetPlatform.useLLVM or false)
101 && !(stdenv.targetPlatform.useAndroidPrebuilt or false)
102 && !(stdenv.targetPlatform.isiOS or false)
103 && gccForLibs != null;
104
105 # older compilers (for example bootstrap's GCC 5) fail with -march=too-modern-cpu
106 isGccArchSupported = arch:
107 if targetPlatform.isPower then false else # powerpc does not allow -march=
108 if isGNU then
109 { # Intel
110 skylake = versionAtLeast ccVersion "6.0";
111 skylake-avx512 = versionAtLeast ccVersion "6.0";
112 cannonlake = versionAtLeast ccVersion "8.0";
113 icelake-client = versionAtLeast ccVersion "8.0";
114 icelake-server = versionAtLeast ccVersion "8.0";
115 cascadelake = versionAtLeast ccVersion "9.0";
116 cooperlake = versionAtLeast ccVersion "10.0";
117 tigerlake = versionAtLeast ccVersion "10.0";
118 knm = versionAtLeast ccVersion "8.0";
119 # AMD
120 znver1 = versionAtLeast ccVersion "6.0";
121 znver2 = versionAtLeast ccVersion "9.0";
122 znver3 = versionAtLeast ccVersion "11.0";
123 }.${arch} or true
124 else if isClang then
125 { # Intel
126 cannonlake = versionAtLeast ccVersion "5.0";
127 icelake-client = versionAtLeast ccVersion "7.0";
128 icelake-server = versionAtLeast ccVersion "7.0";
129 knm = versionAtLeast ccVersion "7.0";
130 # AMD
131 znver1 = versionAtLeast ccVersion "4.0";
132 znver2 = versionAtLeast ccVersion "9.0";
133 }.${arch} or true
134 else
135 false;
136
137
138 darwinPlatformForCC = optionalString stdenv.targetPlatform.isDarwin (
139 if (targetPlatform.darwinPlatform == "macos" && isGNU) then "macosx"
140 else targetPlatform.darwinPlatform
141 );
142
143 darwinMinVersion = optionalString stdenv.targetPlatform.isDarwin (
144 stdenv.targetPlatform.darwinMinVersion
145 );
146
147 darwinMinVersionVariable = optionalString stdenv.targetPlatform.isDarwin
148 stdenv.targetPlatform.darwinMinVersionVariable;
149in
150
151# Ensure bintools matches
152assert libc_bin == bintools.libc_bin;
153assert libc_dev == bintools.libc_dev;
154assert libc_lib == bintools.libc_lib;
155assert nativeTools == bintools.nativeTools;
156assert nativeLibc == bintools.nativeLibc;
157assert nativePrefix == bintools.nativePrefix;
158
159stdenv.mkDerivation {
160 pname = targetPrefix
161 + (if name != "" then name else "${ccName}-wrapper");
162 version = if cc == null then "" else ccVersion;
163
164 preferLocalBuild = true;
165
166 outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ];
167
168 passthru = {
169 inherit targetPrefix suffixSalt;
170 # "cc" is the generic name for a C compiler, but there is no one for package
171 # providing the linker and related tools. The two we use now are GNU
172 # Binutils, and Apple's "cctools"; "bintools" as an attempt to find an
173 # unused middle-ground name that evokes both.
174 inherit bintools;
175 inherit cc libc nativeTools nativeLibc nativePrefix isGNU isClang;
176
177 emacsBufferSetup = pkgs: ''
178 ; We should handle propagation here too
179 (mapc
180 (lambda (arg)
181 (when (file-directory-p (concat arg "/include"))
182 (setenv "NIX_CFLAGS_COMPILE_${suffixSalt}" (concat (getenv "NIX_CFLAGS_COMPILE_${suffixSalt}") " -isystem " arg "/include"))))
183 '(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)}))
184 '';
185
186 inherit expand-response-params;
187
188 inherit nixSupport;
189 };
190
191 dontBuild = true;
192 dontConfigure = true;
193 enableParallelBuilding = true;
194
195 unpackPhase = ''
196 src=$PWD
197 '';
198
199 wrapper = ./cc-wrapper.sh;
200
201 installPhase =
202 ''
203 mkdir -p $out/bin $out/nix-support
204
205 wrap() {
206 local dst="$1"
207 local wrapper="$2"
208 export prog="$3"
209 export use_response_file_by_default=${if isClang && !isCcache then "1" else "0"}
210 substituteAll "$wrapper" "$out/bin/$dst"
211 chmod +x "$out/bin/$dst"
212 }
213 ''
214
215 + (if nativeTools then ''
216 echo ${if targetPlatform.isDarwin then cc else nativePrefix} > $out/nix-support/orig-cc
217
218 ccPath="${if targetPlatform.isDarwin then cc else nativePrefix}/bin"
219 '' else ''
220 echo $cc > $out/nix-support/orig-cc
221
222 ccPath="${cc}/bin"
223 '')
224
225 # Create symlinks to everything in the bintools wrapper.
226 + ''
227 for bbin in $bintools/bin/*; do
228 mkdir -p "$out/bin"
229 ln -s "$bbin" "$out/bin/$(basename $bbin)"
230 done
231 ''
232
233 # We export environment variables pointing to the wrapped nonstandard
234 # cmds, lest some lousy configure script use those to guess compiler
235 # version.
236 + ''
237 export named_cc=${targetPrefix}cc
238 export named_cxx=${targetPrefix}c++
239
240 if [ -e $ccPath/${targetPrefix}gcc ]; then
241 wrap ${targetPrefix}gcc $wrapper $ccPath/${targetPrefix}gcc
242 ln -s ${targetPrefix}gcc $out/bin/${targetPrefix}cc
243 export named_cc=${targetPrefix}gcc
244 export named_cxx=${targetPrefix}g++
245 elif [ -e $ccPath/clang ]; then
246 wrap ${targetPrefix}clang $wrapper $ccPath/clang
247 ln -s ${targetPrefix}clang $out/bin/${targetPrefix}cc
248 export named_cc=${targetPrefix}clang
249 export named_cxx=${targetPrefix}clang++
250 fi
251
252 if [ -e $ccPath/${targetPrefix}g++ ]; then
253 wrap ${targetPrefix}g++ $wrapper $ccPath/${targetPrefix}g++
254 ln -s ${targetPrefix}g++ $out/bin/${targetPrefix}c++
255 elif [ -e $ccPath/clang++ ]; then
256 wrap ${targetPrefix}clang++ $wrapper $ccPath/clang++
257 ln -s ${targetPrefix}clang++ $out/bin/${targetPrefix}c++
258 fi
259
260 if [ -e $ccPath/${targetPrefix}cpp ]; then
261 wrap ${targetPrefix}cpp $wrapper $ccPath/${targetPrefix}cpp
262 elif [ -e $ccPath/cpp ]; then
263 wrap ${targetPrefix}cpp $wrapper $ccPath/cpp
264 fi
265 ''
266
267 # No need to wrap gnat, gnatkr, gnatname or gnatprep; we can just symlink them in
268 + optionalString cc.langAda or false ''
269 for cmd in gnatbind gnatchop gnatclean gnatlink gnatls gnatmake; do
270 wrap ${targetPrefix}$cmd ${./gnat-wrapper.sh} $ccPath/${targetPrefix}$cmd
271 done
272
273 for cmd in gnat gnatkr gnatname gnatprep; do
274 ln -s $ccPath/${targetPrefix}$cmd $out/bin/${targetPrefix}$cmd
275 done
276
277 # this symlink points to the unwrapped gnat's output "out". It is used by
278 # our custom gprconfig compiler description to find GNAT's ada runtime. See
279 # ../../development/tools/build-managers/gprbuild/{boot.nix, nixpkgs-gnat.xml}
280 ln -sf ${cc} $out/nix-support/gprconfig-gnat-unwrapped
281 ''
282
283 + optionalString cc.langD or false ''
284 wrap ${targetPrefix}gdc $wrapper $ccPath/${targetPrefix}gdc
285 ''
286
287 + optionalString cc.langFortran or false ''
288 wrap ${targetPrefix}gfortran $wrapper $ccPath/${targetPrefix}gfortran
289 ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}g77
290 ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}f77
291 export named_fc=${targetPrefix}gfortran
292 ''
293
294 + optionalString cc.langJava or false ''
295 wrap ${targetPrefix}gcj $wrapper $ccPath/${targetPrefix}gcj
296 ''
297
298 + optionalString cc.langGo or false ''
299 wrap ${targetPrefix}gccgo $wrapper $ccPath/${targetPrefix}gccgo
300 wrap ${targetPrefix}go ${./go-wrapper.sh} $ccPath/${targetPrefix}go
301 '';
302
303 strictDeps = true;
304 propagatedBuildInputs = [ bintools ] ++ extraTools ++ optionals cc.langD or false [ zlib ];
305 depsTargetTargetPropagated = optional (libcxx != null) libcxx ++ extraPackages;
306
307 setupHooks = [
308 ../setup-hooks/role.bash
309 ] ++ lib.optional (cc.langC or true) ./setup-hook.sh
310 ++ lib.optional (cc.langFortran or false) ./fortran-hook.sh;
311
312 postFixup =
313 # Ensure flags files exists, as some other programs cat them. (That these
314 # are considered an exposed interface is a bit dubious, but fine for now.)
315 ''
316 touch "$out/nix-support/cc-cflags"
317 touch "$out/nix-support/cc-ldflags"
318 ''
319
320 # Backwards compatibility for packages expecting this file, e.g. with
321 # `$NIX_CC/nix-support/dynamic-linker`.
322 #
323 # TODO(@Ericson2314): Remove this after stable release and force
324 # everyone to refer to bintools-wrapper directly.
325 + ''
326 if [[ -f "$bintools/nix-support/dynamic-linker" ]]; then
327 ln -s "$bintools/nix-support/dynamic-linker" "$out/nix-support"
328 fi
329 if [[ -f "$bintools/nix-support/dynamic-linker-m32" ]]; then
330 ln -s "$bintools/nix-support/dynamic-linker-m32" "$out/nix-support"
331 fi
332 ''
333
334 ##
335 ## GCC libs for non-GCC support
336 ##
337 + optionalString (useGccForLibs && isClang) ''
338
339 echo "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-cflags
340 ''
341 + optionalString useGccForLibs ''
342 echo "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-ldflags
343 echo "-L${gccForLibs.lib}/${targetPlatform.config}/lib" >> $out/nix-support/cc-ldflags
344 ''
345
346 # TODO We would like to connect this to `useGccForLibs`, but we cannot yet
347 # because `libcxxStdenv` on linux still needs this. Maybe someday we'll
348 # always set `useLLVM` on Darwin, and maybe also break down `useLLVM` into
349 # fine-grained use flags (libgcc vs compiler-rt, ld.lld vs legacy, libc++
350 # vs libstdc++, etc.) since Darwin isn't `useLLVM` on all counts. (See
351 # https://clang.llvm.org/docs/Toolchain.html for all the axes one might
352 # break `useLLVM` into.)
353 + optionalString (isClang
354 && targetPlatform.isLinux
355 && !(stdenv.targetPlatform.useAndroidPrebuilt or false)
356 && !(stdenv.targetPlatform.useLLVM or false)
357 && gccForLibs != null) (''
358 echo "--gcc-toolchain=${gccForLibs}" >> $out/nix-support/cc-cflags
359
360 # Pull in 'cc.out' target to get 'libstdc++fs.a'. It should be in
361 # 'cc.lib'. But it's a gcc package bug.
362 # TODO(trofi): remove once gcc is fixed to move libraries to .lib output.
363 echo "-L${gccForLibs}/${optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}"}/lib" >> $out/nix-support/cc-ldflags
364 ''
365 # this ensures that when clang passes -lgcc_s to lld (as it does
366 # when building e.g. firefox), lld is able to find libgcc_s.so
367 + concatMapStrings (libgcc: ''
368 echo "-L${libgcc}/lib" >> $out/nix-support/cc-ldflags
369 '') (lib.toList (gccForLibs.libgcc or [])))
370
371 ##
372 ## General libc support
373 ##
374
375 # The "-B${libc_lib}/lib/" flag is a quick hack to force gcc to link
376 # against the crt1.o from our own glibc, rather than the one in
377 # /usr/lib. (This is only an issue when using an `impure'
378 # compiler/linker, i.e., one that searches /usr/lib and so on.)
379 #
380 # Unfortunately, setting -B appears to override the default search
381 # path. Thus, the gcc-specific "../includes-fixed" directory is
382 # now longer searched and glibc's <limits.h> header fails to
383 # compile, because it uses "#include_next <limits.h>" to find the
384 # limits.h file in ../includes-fixed. To remedy the problem,
385 # another -idirafter is necessary to add that directory again.
386 + optionalString (libc != null) (''
387 touch "$out/nix-support/libc-cflags"
388 touch "$out/nix-support/libc-ldflags"
389 echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags
390 '' + optionalString (!(cc.langD or false)) ''
391 echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags
392 '' + optionalString (isGNU && (!(cc.langD or false))) ''
393 for dir in "${cc}"/lib/gcc/*/*/include-fixed; do
394 echo '-idirafter' ''${dir} >> $out/nix-support/libc-cflags
395 done
396 '' + ''
397
398 echo "${libc_lib}" > $out/nix-support/orig-libc
399 echo "${libc_dev}" > $out/nix-support/orig-libc-dev
400 '')
401
402 ##
403 ## General libc++ support
404 ##
405
406 # We have a libc++ directly, we have one via "smuggled" GCC, or we have one
407 # bundled with the C compiler because it is GCC
408 + optionalString (libcxx != null || (useGccForLibs && gccForLibs.langCC or false) || (isGNU && cc.langCC or false)) ''
409 touch "$out/nix-support/libcxx-cxxflags"
410 touch "$out/nix-support/libcxx-ldflags"
411 ''
412 # Adding -isystem flags should be done only for clang; gcc
413 # already knows how to find its own libstdc++, and adding
414 # additional -isystem flags will confuse gfortran (see
415 # https://github.com/NixOS/nixpkgs/pull/209870#issuecomment-1500550903)
416 + optionalString (libcxx == null && isClang && (useGccForLibs && gccForLibs.langCC or false)) ''
417 for dir in ${gccForLibs}${lib.optionalString (hostPlatform != targetPlatform) "/${targetPlatform.config}"}/include/c++/*; do
418 echo "-isystem $dir" >> $out/nix-support/libcxx-cxxflags
419 done
420 for dir in ${gccForLibs}${lib.optionalString (hostPlatform != targetPlatform) "/${targetPlatform.config}"}/include/c++/*/${targetPlatform.config}; do
421 echo "-isystem $dir" >> $out/nix-support/libcxx-cxxflags
422 done
423 ''
424 + optionalString (libcxx.isLLVM or false) ''
425 echo "-isystem ${lib.getDev libcxx}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags
426 echo "-stdlib=libc++" >> $out/nix-support/libcxx-ldflags
427 echo "-l${libcxx.cxxabi.libName}" >> $out/nix-support/libcxx-ldflags
428 ''
429
430 ##
431 ## Initial CFLAGS
432 ##
433
434 # GCC shows ${cc_solib}/lib in `gcc -print-search-dirs', but not
435 # ${cc_solib}/lib64 (even though it does actually search there...)..
436 # This confuses libtool. So add it to the compiler tool search
437 # path explicitly.
438 + optionalString (!nativeTools) ''
439 if [ -e "${cc_solib}/lib64" -a ! -L "${cc_solib}/lib64" ]; then
440 ccLDFlags+=" -L${cc_solib}/lib64"
441 ccCFlags+=" -B${cc_solib}/lib64"
442 fi
443 ccLDFlags+=" -L${cc_solib}/lib"
444 ccCFlags+=" -B${cc_solib}/lib"
445
446 '' + optionalString cc.langAda or false ''
447 touch "$out/nix-support/gnat-cflags"
448 touch "$out/nix-support/gnat-ldflags"
449 basePath=$(echo $cc/lib/*/*/*)
450 ccCFlags+=" -B$basePath -I$basePath/adainclude"
451 gnatCFlags="-I$basePath/adainclude -I$basePath/adalib"
452
453 echo "$gnatCFlags" >> $out/nix-support/gnat-cflags
454 '' + ''
455 echo "$ccLDFlags" >> $out/nix-support/cc-ldflags
456 echo "$ccCFlags" >> $out/nix-support/cc-cflags
457 '' + optionalString (targetPlatform.isDarwin && (libcxx != null) && (cc.isClang or false)) ''
458 echo " -L${lib.getLib libcxx}/lib" >> $out/nix-support/cc-ldflags
459 ''
460
461 ##
462 ## Man page and info support
463 ##
464 + optionalString propagateDoc ''
465 ln -s ${cc.man} $man
466 ln -s ${cc.info} $info
467 '' + optionalString (cc.langD or false) ''
468 echo "-B${zlib}${zlib.libdir or "/lib/"}" >> $out/nix-support/libc-cflags
469 ''
470
471 ##
472 ## Hardening support
473 ##
474 + ''
475 export hardening_unsupported_flags="${builtins.concatStringsSep " " (cc.hardeningUnsupportedFlags or [])}"
476 ''
477
478 # Machine flags. These are necessary to support
479
480 # TODO: We should make a way to support miscellaneous machine
481 # flags and other gcc flags as well.
482
483 # Always add -march based on cpu in triple. Sometimes there is a
484 # discrepency (x86_64 vs. x86-64), so we provide an "arch" arg in
485 # that case.
486 # TODO: aarch64-darwin has mcpu incompatible with gcc
487 + optionalString ((targetPlatform ? gcc.arch) && (isClang || !(stdenv.isDarwin && stdenv.isAarch64)) &&
488 isGccArchSupported targetPlatform.gcc.arch) ''
489 echo "-march=${targetPlatform.gcc.arch}" >> $out/nix-support/cc-cflags-before
490 ''
491
492 # -mcpu is not very useful, except on PowerPC where it is used
493 # instead of march. On all other platforms you should use mtune
494 # and march instead.
495 # TODO: aarch64-darwin has mcpu incompatible with gcc
496 + optionalString ((targetPlatform ? gcc.cpu) && (isClang || !(stdenv.isDarwin && stdenv.isAarch64))) ''
497 echo "-mcpu=${targetPlatform.gcc.cpu}" >> $out/nix-support/cc-cflags-before
498 ''
499
500 # -mfloat-abi only matters on arm32 but we set it here
501 # unconditionally just in case. If the abi specifically sets hard
502 # vs. soft floats we use it here.
503 + optionalString (targetPlatform ? gcc.float-abi) ''
504 echo "-mfloat-abi=${targetPlatform.gcc.float-abi}" >> $out/nix-support/cc-cflags-before
505 ''
506 + optionalString (targetPlatform ? gcc.fpu) ''
507 echo "-mfpu=${targetPlatform.gcc.fpu}" >> $out/nix-support/cc-cflags-before
508 ''
509 + optionalString (targetPlatform ? gcc.mode) ''
510 echo "-mmode=${targetPlatform.gcc.mode}" >> $out/nix-support/cc-cflags-before
511 ''
512 + optionalString (targetPlatform ? gcc.thumb) ''
513 echo "-m${if targetPlatform.gcc.thumb then "thumb" else "arm"}" >> $out/nix-support/cc-cflags-before
514 ''
515 + optionalString (targetPlatform ? gcc.tune &&
516 isGccArchSupported targetPlatform.gcc.tune) ''
517 echo "-mtune=${targetPlatform.gcc.tune}" >> $out/nix-support/cc-cflags-before
518 ''
519
520 # TODO: categorize these and figure out a better place for them
521 + optionalString targetPlatform.isWindows ''
522 hardening_unsupported_flags+=" pic"
523 '' + optionalString targetPlatform.isMinGW ''
524 hardening_unsupported_flags+=" stackprotector fortify"
525 '' + optionalString targetPlatform.isAvr ''
526 hardening_unsupported_flags+=" stackprotector pic"
527 '' + optionalString (targetPlatform.libc == "newlib" || targetPlatform.libc == "newlib-nano") ''
528 hardening_unsupported_flags+=" stackprotector fortify pie pic"
529 '' + optionalString (targetPlatform.libc == "musl" && targetPlatform.isx86_32) ''
530 hardening_unsupported_flags+=" stackprotector"
531 '' + optionalString targetPlatform.isNetBSD ''
532 hardening_unsupported_flags+=" stackprotector fortify"
533 '' + optionalString cc.langAda or false ''
534 hardening_unsupported_flags+=" format stackprotector strictoverflow"
535 '' + optionalString cc.langD or false ''
536 hardening_unsupported_flags+=" format"
537 '' + optionalString cc.langFortran or false ''
538 hardening_unsupported_flags+=" format"
539 '' + optionalString targetPlatform.isWasm ''
540 hardening_unsupported_flags+=" stackprotector fortify pie pic"
541 '' + optionalString targetPlatform.isMicroBlaze ''
542 hardening_unsupported_flags+=" stackprotector"
543 ''
544
545 + optionalString (libc != null && targetPlatform.isAvr) ''
546 for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do
547 echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-crt1-cflags
548 done
549 ''
550
551 + optionalString stdenv.targetPlatform.isDarwin ''
552 echo "-arch ${targetPlatform.darwinArch}" >> $out/nix-support/cc-cflags
553 ''
554
555 + optionalString targetPlatform.isAndroid ''
556 echo "-D__ANDROID_API__=${targetPlatform.sdkVer}" >> $out/nix-support/cc-cflags
557 ''
558
559 # There are a few tools (to name one libstdcxx5) which do not work
560 # well with multi line flags, so make the flags single line again
561 + ''
562 for flags in "$out/nix-support"/*flags*; do
563 substituteInPlace "$flags" --replace $'\n' ' '
564 done
565
566 substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
567 substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh
568 substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash
569 ''
570
571 + optionalString cc.langAda or false ''
572 substituteAll ${./add-gnat-extra-flags.sh} $out/nix-support/add-gnat-extra-flags.sh
573 ''
574
575 ##
576 ## General Clang support
577 ## Needs to go after ^ because the for loop eats \n and makes this file an invalid script
578 ##
579 + optionalString isClang ''
580 export defaultTarget=${targetPlatform.config}
581 substituteAll ${./add-clang-cc-cflags-before.sh} $out/nix-support/add-local-cc-cflags-before.sh
582 ''
583
584 ##
585 ## Extra custom steps
586 ##
587 + extraBuildCommands
588 + lib.strings.concatStringsSep "; "
589 (lib.attrsets.mapAttrsToList
590 (name: value: "echo ${toString value} >> $out/nix-support/${name}")
591 nixSupport);
592
593
594 env = {
595 # for substitution in utils.bash
596 expandResponseParams = "${expand-response-params}/bin/expand-response-params";
597 shell = getBin shell + shell.shellPath or "";
598 gnugrep_bin = if nativeTools then "" else gnugrep;
599 # stdenv.cc.cc should not be null and we have nothing better for now.
600 # if the native impure bootstrap is gotten rid of this can become `inherit cc;` again.
601 cc = if nativeTools then "" else cc;
602 wrapperName = "CC_WRAPPER";
603 inherit suffixSalt coreutils_bin bintools;
604 inherit libc_bin libc_dev libc_lib;
605 inherit darwinPlatformForCC darwinMinVersion darwinMinVersionVariable;
606 };
607
608 meta =
609 let cc_ = if cc != null then cc else {}; in
610 (if cc_ ? meta then removeAttrs cc.meta ["priority"] else {}) //
611 { description =
612 lib.attrByPath ["meta" "description"] "System C compiler" cc_
613 + " (wrapper script)";
614 priority = 10;
615 };
616}