Avoid top-level with in pkgs/build-support/cc-wrapper/default.nix (#295213)

This commit is contained in:
Philip Taron 2024-03-12 00:34:58 -07:00 committed by GitHub
parent d6cd575b84
commit e27bcfed1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -55,31 +55,48 @@
, includeFortifyHeaders ? null , includeFortifyHeaders ? null
}: }:
with lib;
assert nativeTools -> !propagateDoc && nativePrefix != ""; assert nativeTools -> !propagateDoc && nativePrefix != "";
assert !nativeTools -> assert !nativeTools -> cc != null && coreutils != null && gnugrep != null;
cc != null && coreutils != null && gnugrep != null;
assert !(nativeLibc && noLibc); assert !(nativeLibc && noLibc);
assert (noLibc || nativeLibc) == (libc == null); assert (noLibc || nativeLibc) == (libc == null);
let let
stdenv = stdenvNoCC; inherit (lib)
attrByPath
concatMapStrings
concatStringsSep
escapeShellArg
getBin
getDev
getLib
getName
getVersion
mapAttrsToList
optional
optionalAttrs
optionals
optionalString
removePrefix
replaceStrings
toList
versionAtLeast
;
inherit (stdenv) hostPlatform targetPlatform; inherit (stdenv) hostPlatform targetPlatform;
stdenv = stdenvNoCC;
includeFortifyHeaders' = if includeFortifyHeaders != null includeFortifyHeaders' = if includeFortifyHeaders != null
then includeFortifyHeaders then includeFortifyHeaders
else (targetPlatform.libc == "musl" && isGNU); else (targetPlatform.libc == "musl" && isGNU);
# Prefix for binaries. Customarily ends with a dash separator. # Prefix for binaries. Customarily ends with a dash separator.
# #
# TODO(@Ericson2314) Make unconditional, or optional but always true by # TODO(@Ericson2314) Make unconditional, or optional but always true by default.
# default. targetPrefix = optionalString (targetPlatform != hostPlatform) (targetPlatform.config + "-");
targetPrefix = lib.optionalString (targetPlatform != hostPlatform)
(targetPlatform.config + "-");
ccVersion = lib.getVersion cc; ccVersion = getVersion cc;
ccName = lib.removePrefix targetPrefix (lib.getName cc); ccName = removePrefix targetPrefix (getName cc);
libc_bin = optionalString (libc != null) (getBin libc); libc_bin = optionalString (libc != null) (getBin libc);
libc_dev = optionalString (libc != null) (getDev libc); libc_dev = optionalString (libc != null) (getDev libc);
@ -98,7 +115,7 @@ let
suffixSalt = replaceStrings ["-" "."] ["_" "_"] targetPlatform.config; suffixSalt = replaceStrings ["-" "."] ["_" "_"] targetPlatform.config;
expand-response-params = expand-response-params =
lib.optionalString ((buildPackages.stdenv.hasCC or false) && buildPackages.stdenv.cc != "/dev/null") (import ../expand-response-params { inherit (buildPackages) stdenv; }); optionalString ((buildPackages.stdenv.hasCC or false) && buildPackages.stdenv.cc != "/dev/null") (import ../expand-response-params { inherit (buildPackages) stdenv; });
useGccForLibs = useCcForLibs useGccForLibs = useCcForLibs
&& libcxx == null && libcxx == null
@ -111,7 +128,7 @@ let
+ optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}"; + optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}";
# Analogously to cc_solib and gccForLibs_solib # Analogously to cc_solib and gccForLibs_solib
libcxx_solib = "${lib.getLib libcxx}/lib"; libcxx_solib = "${getLib libcxx}/lib";
# The following two functions, `isGccArchSupported` and # The following two functions, `isGccArchSupported` and
# `isGccTuneSupported`, only handle those situations where a flag # `isGccTuneSupported`, only handle those situations where a flag
@ -407,9 +424,9 @@ stdenv.mkDerivation {
setupHooks = [ setupHooks = [
../setup-hooks/role.bash ../setup-hooks/role.bash
] ++ lib.optional (cc.langC or true) ./setup-hook.sh ] ++ optional (cc.langC or true) ./setup-hook.sh
++ lib.optional (cc.langFortran or false) ./fortran-hook.sh ++ optional (cc.langFortran or false) ./fortran-hook.sh
++ lib.optional (targetPlatform.isWindows) (stdenv.mkDerivation { ++ optional (targetPlatform.isWindows) (stdenv.mkDerivation {
name = "win-dll-hook.sh"; name = "win-dll-hook.sh";
dontUnpack = true; dontUnpack = true;
installPhase = '' installPhase = ''
@ -476,7 +493,7 @@ stdenv.mkDerivation {
# when building e.g. firefox), lld is able to find libgcc_s.so # when building e.g. firefox), lld is able to find libgcc_s.so
+ concatMapStrings (libgcc: '' + concatMapStrings (libgcc: ''
echo "-L${libgcc}/lib" >> $out/nix-support/cc-ldflags echo "-L${libgcc}/lib" >> $out/nix-support/cc-ldflags
'') (lib.toList (gccForLibs.libgcc or []))) '') (toList (gccForLibs.libgcc or [])))
## ##
## General libc support ## General libc support
@ -542,7 +559,7 @@ stdenv.mkDerivation {
done done
'' ''
+ optionalString (libcxx.isLLVM or false) '' + optionalString (libcxx.isLLVM or false) ''
echo "-isystem ${lib.getDev libcxx}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags echo "-isystem ${getDev libcxx}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags
echo "-stdlib=libc++" >> $out/nix-support/libcxx-ldflags echo "-stdlib=libc++" >> $out/nix-support/libcxx-ldflags
'' ''
# can remove once LLVM9 and LLVM11 are dropped from nixpkgs # can remove once LLVM9 and LLVM11 are dropped from nixpkgs
@ -596,7 +613,7 @@ stdenv.mkDerivation {
## Hardening support ## Hardening support
## ##
+ '' + ''
export hardening_unsupported_flags="${builtins.concatStringsSep " " ccHardeningUnsupportedFlags}" export hardening_unsupported_flags="${concatStringsSep " " ccHardeningUnsupportedFlags}"
'' ''
# Machine flags. These are necessary to support # Machine flags. These are necessary to support
@ -701,9 +718,9 @@ stdenv.mkDerivation {
## ##
+ optionalString isClang '' + optionalString isClang ''
# Escape twice: once for this script, once for the one it gets substituted into. # Escape twice: once for this script, once for the one it gets substituted into.
export march=${lib.escapeShellArg export march=${escapeShellArg
(lib.optionalString (targetPlatform ? gcc.arch) (optionalString (targetPlatform ? gcc.arch)
(lib.escapeShellArg "-march=${targetPlatform.gcc.arch}"))} (escapeShellArg "-march=${targetPlatform.gcc.arch}"))}
export defaultTarget=${targetPlatform.config} export defaultTarget=${targetPlatform.config}
substituteAll ${./add-clang-cc-cflags-before.sh} $out/nix-support/add-local-cc-cflags-before.sh substituteAll ${./add-clang-cc-cflags-before.sh} $out/nix-support/add-local-cc-cflags-before.sh
'' ''
@ -712,8 +729,8 @@ stdenv.mkDerivation {
## Extra custom steps ## Extra custom steps
## ##
+ extraBuildCommands + extraBuildCommands
+ lib.strings.concatStringsSep "; " + concatStringsSep "; "
(lib.attrsets.mapAttrsToList (mapAttrsToList
(name: value: "echo ${toString value} >> $out/nix-support/${name}") (name: value: "echo ${toString value} >> $out/nix-support/${name}")
nixSupport); nixSupport);
@ -736,11 +753,9 @@ stdenv.mkDerivation {
}; };
meta = meta =
let cc_ = lib.optionalAttrs (cc != null) cc; in let cc_ = optionalAttrs (cc != null) cc; in
(lib.optionalAttrs (cc_ ? meta) (removeAttrs cc.meta ["priority"])) // (optionalAttrs (cc_ ? meta) (removeAttrs cc.meta ["priority"])) //
{ description = { description = attrByPath ["meta" "description"] "System C compiler" cc_ + " (wrapper script)";
lib.attrByPath ["meta" "description"] "System C compiler" cc_
+ " (wrapper script)";
priority = 10; priority = 10;
mainProgram = if name != "" then name else ccName; mainProgram = if name != "" then name else ccName;
}; };