idiosyn/stdenv.nix
2024-07-23 15:28:38 +02:00

51 lines
1.6 KiB
Nix
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{ pkgs ? buildPackages
, stdenv ? baseStdenv
, buildPackages ? null
, baseStdenv ? null
}:
let
inherit (pkgs) addAttrsToDerivation overrideCC;
inherit (pkgs.lib) optionalAttrs optionals toList;
inherit (pkgs.llvmPackages_latest) clangUseLLVM;
inherit (stdenv) targetPlatform;
cflags = [
"-pipe" # Prefer pipes over temporary files between stages
"-O2"
# Clang/LLD
"--flto=thin"
];
ldflags = [
"-O2" # Enable tail merging of strings
"--hash-style=gnu" # Produce only DT_GNU_HASH
# LLD
"--icf=safe" # Fold identical code where safe
"--lto-O2"
"--pack-dyn-relocs=relr"
];
rustflags = [
"-C lto=thin"
"-C linker-plugin-lto"
"-C opt-level=2"
] ++ optionals (targetPlatform.isx86_64 && targetPlatform ? gcc.arch) [
"-C target-cpu=${targetPlatform.gcc.arch}"
];
in addAttrsToDerivation (base: {
env = (base.env or { }) // optionalAttrs (!base ? NIX_CFLAGS_COMPILE) {
NIX_CFLAGS_COMPILE = toString (toList base.env.NIX_CFLAGS_COMPILE or [ ] ++ cflags);
} // optionalAttrs (base ? env.NIX_LDFLAGS) {
NIX_LDFLAGS = toString (toList base.env.NIX_LDFLAGS or [ ] ++ ldflags);
};
NIX_CFLAGS_LINK = toList base.NIX_CFLAGS_LINK or [ ] ++ map (flag: "--Wl,${flag}") ldflags;
NIX_RUSTFLAGS = toList base.NIX_RUSTFLAGS or [ ] ++ rustflags;
} // optionalAttrs (base ? env.NIX_CFLAGS) {
NIX_CFLAGS_COMPILE = toList base.NIX_CFLAGS_COMPILE or [ ] ++ cflags;
} // optionalAttrs (!base ? env.NIX_LDFLAGS) {
NIX_LDFLAGS = toList base.NIX_LDFLAGS or [ ] ++ ldflags;
}) (overrideCC stdenv clangUseLLVM)