Skip to content

Commit

Permalink
lib.systems: introduce cc, bintools, cxxlib, unwinderlib, and rtlib a…
Browse files Browse the repository at this point in the history
…ttributes
  • Loading branch information
RossComputerGuy committed Jan 21, 2025
1 parent 564ebca commit cad1223
Show file tree
Hide file tree
Showing 27 changed files with 120 additions and 73 deletions.
34 changes: 31 additions & 3 deletions lib/systems/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ let
# `parsed` is inferred from args, both because there are two options with one
# clearly preferred, and to prevent cycles. A simpler fixed point where the RHS
# always just used `final.*` would fail on both counts.
elaborate = systemOrArgs: let
elaborate = systemOrArgs:
assert lib.assertMsg (!(lib.oldestSupportedReleaseIsAtLeast 2511 && systemOrArgs ? useLLVM)) "The useLLVM attribute has been deprecated in favor of the toolchain attributes.";
assert lib.assertMsg (!(lib.oldestSupportedReleaseIsAtLeast 2511 && systemOrArgs ? useArocc)) "The useArocc attribute has been deprecated in favor of the toolchain attributes.";
assert lib.assertMsg (!(lib.oldestSupportedReleaseIsAtLeast 2511 && systemOrArgs ? useZig)) "The useZig attribute has been deprecated in favor of the toolchain attributes.";
let
allArgs = systemToAttrs systemOrArgs;

# Those two will always be derived from "config", if given, so they should NOT
Expand All @@ -92,6 +96,21 @@ let
isCompatible = _: throw "2022-05-23: isCompatible has been removed in favor of canExecute, refer to the 22.11 changelog for details";
# Derived meta-data
useLLVM = final.isFreeBSD || final.isOpenBSD;
useArocc = false;
useZig = false;

cc = if final.useLLVM then "clang"
else if final.useArocc then "arocc"
else if final.useZig then "zig"
else "gcc";

bintools = if final.useLLVM || final.useArocc || final.useZig || final.isDarwin then
"llvm"
else "gnu";

cxxlib =
/**/ if final.useLLVM || final.useArocc || final.useZig || final.isDarwin then "libcxx"
else "libstdcxx";

libc =
/**/ if final.isDarwin then "libSystem"
Expand All @@ -112,15 +131,24 @@ let
else if final.isNone then "newlib"
# TODO(@Ericson2314) think more about other operating systems
else "native/impure";

unwinderlib =
/**/ if final.useLLVM || final.useArocc || final.useZig || final.isDarwin then "libunwind"
else "libgcc_s";

rtlib =
/**/ if final.useLLVM || final.useArocc || final.useZig || final.isDarwin then "compiler-rt"
else "libgcc";

# Choose what linker we wish to use by default. Someday we might also
# choose the C compiler, runtime library, C++ standard library, etc. in
# this way, nice and orthogonally, and deprecate `useLLVM`. But due to
# the monolithic GCC build we cannot actually make those choices
# independently, so we are just doing `linker` and keeping `useLLVM` for
# now.
linker =
/**/ if final.useLLVM or false then "lld"
else if final.isDarwin then "cctools"
/**/ if final.useLLVM then "lld"
else if final.isDarwin then "cctools"
# "bfd" and "gold" both come from GNU binutils. The existence of Gold
# is why we use the more obscure "bfd" and not "binutils" for this
# choice.
Expand Down
16 changes: 8 additions & 8 deletions lib/systems/examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ rec {
androidNdkVersion = "26";
libc = "bionic";
useAndroidPrebuilt = false;
useLLVM = true;
toolchain = "llvm";
};

pogoplug4 = {
Expand Down Expand Up @@ -330,19 +330,19 @@ rec {
config = "aarch64-w64-mingw32";
libc = "ucrt";
rust.rustcTarget = "aarch64-pc-windows-gnullvm";
useLLVM = true;
toolchain = "llvm";
};

# BSDs

aarch64-freebsd = {
config = "aarch64-unknown-freebsd";
useLLVM = true;
toolchain = "llvm";
};

x86_64-freebsd = {
config = "x86_64-unknown-freebsd";
useLLVM = true;
toolchain = "llvm";
};

x86_64-netbsd = {
Expand All @@ -352,12 +352,12 @@ rec {
# this is broken and never worked fully
x86_64-netbsd-llvm = {
config = "x86_64-unknown-netbsd";
useLLVM = true;
toolchain = "llvm";
};

x86_64-openbsd = {
config = "x86_64-unknown-openbsd";
useLLVM = true;
toolchain = "llvm";
};

#
Expand All @@ -366,13 +366,13 @@ rec {

wasi32 = {
config = "wasm32-unknown-wasi";
useLLVM = true;
toolchain = "llvm";
};

wasm32-unknown-none = {
config = "wasm32-unknown-none";
rust.rustcTarget = "wasm32-unknown-unknown";
useLLVM = true;
toolchain = "llvm";
};

# Ghcjs
Expand Down
4 changes: 2 additions & 2 deletions pkgs/build-support/cc-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ let

useGccForLibs = useCcForLibs
&& libcxx == null
&& targetPlatform.rtlib == "libgcc"
&& !targetPlatform.isDarwin
&& !(targetPlatform.useLLVM or false)
&& !(targetPlatform.useAndroidPrebuilt or false)
&& !(targetPlatform.isiOS or false)
&& gccForLibs != null;
Expand Down Expand Up @@ -514,7 +514,7 @@ stdenvNoCC.mkDerivation {
+ optionalString (isClang
&& targetPlatform.isLinux
&& !(targetPlatform.useAndroidPrebuilt or false)
&& !(targetPlatform.useLLVM or false)
&& targetPlatform.rtlib == "libgcc"
&& gccForLibs != null) (''
echo "--gcc-toolchain=${gccForLibs}" >> $out/nix-support/cc-cflags
Expand Down
2 changes: 1 addition & 1 deletion pkgs/by-name/cy/cyrus_sasl/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ stdenv.mkDerivation rec {
"--enable-shared"
]
++ lib.optional enableLdap "--with-ldap=${openldap.dev}"
++ lib.optionals (stdenv.targetPlatform.useLLVM or false) [
++ lib.optionals (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) [
"--disable-sample"
"CFLAGS=-DTIME_WITH_SYS_TIME"
];
Expand Down
6 changes: 3 additions & 3 deletions pkgs/by-name/el/elfutils/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ stdenv.mkDerivation rec {
++ lib.optionals stdenv.hostPlatform.isMusl [ ./musl-error_h.patch ]
# Prevent headers and binaries from colliding which results in an error.
# https://sourceware.org/pipermail/elfutils-devel/2024q3/007281.html
++ lib.optional (stdenv.targetPlatform.useLLVM or false) ./cxx-header-collision.patch;
++ lib.optional (stdenv.hostPlatform.cxxlib == "libcxx") ./cxx-header-collision.patch;

postPatch =
''
Expand Down Expand Up @@ -94,7 +94,7 @@ stdenv.mkDerivation rec {
bzip2
]
++ lib.optional enableDebuginfod pkg-config
++ lib.optional (stdenv.targetPlatform.useLLVM or false) autoreconfHook;
++ lib.optional (stdenv.hostPlatform.libcxx == "libcxx") autoreconfHook;
buildInputs =
[
zlib
Expand Down Expand Up @@ -128,7 +128,7 @@ stdenv.mkDerivation rec {
# Versioned symbols are nice to have, but we can do without.
(lib.enableFeature (!stdenv.hostPlatform.isMicroBlaze) "symbol-versioning")
]
++ lib.optional (stdenv.targetPlatform.useLLVM or false) "--disable-demangler"
++ lib.optional (stdenv.hostPlatform.unwinderlib == "libunwind") "--disable-demangler"
++ lib.optionals stdenv.cc.isClang [
"CFLAGS=-Wno-unused-private-field"
"CXXFLAGS=-Wno-unused-private-field"
Expand Down
18 changes: 11 additions & 7 deletions pkgs/by-name/ke/kexec-tools/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ stdenv.mkDerivation rec {
sha256 = "sha256-Z7GsUDqt5FpU2wvHkiiogwo11dT4PO6TLP8+eoGkqew=";
};

patches = [
# Use ELFv2 ABI on ppc64be
(fetchpatch {
url = "https://raw.githubusercontent.com/void-linux/void-packages/6c1192cbf166698932030c2e3de71db1885a572d/srcpkgs/kexec-tools/patches/ppc64-elfv2.patch";
sha256 = "19wzfwb0azm932v0vhywv4221818qmlmvdfwpvvpfyw4hjsc2s1l";
})
] ++ lib.optional (stdenv.hostPlatform.useLLVM or false) ./fix-purgatory-llvm-libunwind.patch;
patches =
[
# Use ELFv2 ABI on ppc64be
(fetchpatch {
url = "https://raw.githubusercontent.com/void-linux/void-packages/6c1192cbf166698932030c2e3de71db1885a572d/srcpkgs/kexec-tools/patches/ppc64-elfv2.patch";
sha256 = "19wzfwb0azm932v0vhywv4221818qmlmvdfwpvvpfyw4hjsc2s1l";
})
]
++ lib.optional (
stdenv.hostPlatform.unwinderlib == "libunwind"
) ./fix-purgatory-llvm-libunwind.patch;

hardeningDisable = [
"format"
Expand Down
2 changes: 1 addition & 1 deletion pkgs/by-name/li/libseccomp/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ stdenv.mkDerivation rec {
util-linuxMinimal
which
];
doCheck = !(stdenv.targetPlatform.useLLVM or false);
doCheck = !stdenv.targetPlatform.useLLVM;

# Hack to ensure that patchelf --shrink-rpath get rids of a $TMPDIR reference.
preFixup = "rm -rfv src";
Expand Down
4 changes: 2 additions & 2 deletions pkgs/by-name/so/sourceHighlight/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ stdenv.mkDerivation rec {
nativeBuildInputs = [ updateAutotoolsGnuConfigScriptsHook ];
buildInputs =
[ boost ]
++ lib.optional (stdenv.targetPlatform.useLLVM or false) (
++ lib.optional (stdenv.hostPlatform.rtlib == "compiler-rt") (
llvmPackages.compiler-rt.override {
doFakeLibgcc = true;
}
Expand Down Expand Up @@ -92,7 +92,7 @@ stdenv.mkDerivation rec {
maintainers = with maintainers; [ SuperSandro2000 ];
};
}
// lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) {
// lib.optionalAttrs (stdenv.targetPlatform.rtlib == "libunwind") {
# Force linking to "libgcc" so tests pass
NIX_CFLAGS_COMPILE = "-lgcc";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

let

useLLVM = stdenv.hostPlatform.useLLVM or false;
useLLVM = stdenv.hostPlatform.useLLVM;
bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
haveLibc = stdenv.cc.libc != null;
# TODO: Make this account for GCC having libstdcxx, which will help
Expand Down
17 changes: 9 additions & 8 deletions pkgs/development/compilers/llvm/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ let
clang =
if stdenv.targetPlatform.libc == null then
tools.clangNoLibc
else if stdenv.targetPlatform.useLLVM or false then
else if stdenv.targetPlatform.useLLVM then
tools.clangUseLLVM
else if (pkgs.targetPackages.stdenv or args.stdenv).cc.isGNU then
tools.libstdcxxClang
Expand Down Expand Up @@ -781,7 +781,7 @@ let
echo "--unwindlib=libunwind" >> $out/nix-support/cc-cflags
echo "-L${targetLlvmLibraries.libunwind}/lib" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString (!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM or false) ''
+ lib.optionalString (!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM) ''
echo "-lunwind" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString stdenv.targetPlatform.isWasm ''
Expand All @@ -803,7 +803,7 @@ let
++ lib.optional (
!stdenv.targetPlatform.isWasm
&& !stdenv.targetPlatform.isFreeBSD
&& stdenv.targetPlatform.useLLVM or false
&& stdenv.targetPlatform.unwinderlib == "libunwind"
) "-lunwind"
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
nixSupport.cc-ldflags = lib.optionals (
Expand Down Expand Up @@ -836,7 +836,7 @@ let
echo "--unwindlib=libunwind" >> $out/nix-support/cc-cflags
echo "-L${targetLlvmLibraries.libunwind}/lib" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString (!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM or false) ''
+ lib.optionalString (!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isDarwin && stdenv.targetPlatform.unwinderlib == "libunwind") ''
echo "-lunwind" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString stdenv.targetPlatform.isWasm ''
Expand All @@ -858,7 +858,8 @@ let
++ lib.optional (
!stdenv.targetPlatform.isWasm
&& !stdenv.targetPlatform.isFreeBSD
&& stdenv.targetPlatform.useLLVM or false
&& !stdenv.targetPlatform.isDarwin
&& stdenv.targetPlatform.unwinderlib == "libunwind"
) "-lunwind"
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
nixSupport.cc-ldflags = lib.optionals (
Expand Down Expand Up @@ -1084,7 +1085,7 @@ let
# Darwin needs to use a bootstrap stdenv to avoid an infinite recursion when cross-compiling.
if args.stdenv.hostPlatform.isDarwin then
overrideCC darwin.bootstrapStdenv buildLlvmTools.clangWithLibcAndBasicRtAndLibcxx
else if args.stdenv.hostPlatform.useLLVM or false then
else if args.stdenv.hostPlatform.useLLVM then
overrideCC args.stdenv buildLlvmTools.clangWithLibcAndBasicRtAndLibcxx
else
args.stdenv;
Expand All @@ -1093,7 +1094,7 @@ let
patches = compiler-rtPatches;
inherit stdenv;
}
// lib.optionalAttrs (stdenv.hostPlatform.useLLVM or false) {
// lib.optionalAttrs stdenv.hostPlatform.useLLVM {
libxcrypt = (libxcrypt.override { inherit stdenv; }).overrideAttrs (old: {
configureFlags = old.configureFlags ++ [ "--disable-symvers" ];
});
Expand All @@ -1102,7 +1103,7 @@ let

compiler-rt-no-libc = callPackage ./compiler-rt {
patches = compiler-rtPatches;
doFakeLibgcc = stdenv.hostPlatform.useLLVM or false;
doFakeLibgcc = stdenv.hostPlatform.useLLVM;
stdenv =
# Darwin needs to use a bootstrap stdenv to avoid an infinite recursion when cross-compiling.
if stdenv.hostPlatform.isDarwin then
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/compilers/llvm/common/libcxx/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let
runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";

# Note: useLLVM is likely false for Darwin but true under pkgsLLVM
useLLVM = stdenv.hostPlatform.useLLVM or false;
useLLVM = stdenv.hostPlatform.useLLVM;

src' = if monorepoSrc != null then
runCommand "${pname}-src-${version}" { inherit (monorepoSrc) passthru; } (''
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/rust/1_83.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let
{
enableSharedLibraries = true;
}
// lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) {
// lib.optionalAttrs (stdenv.targetPlatform.useLLVM) {
# Force LLVM to compile using clang + LLVM libs when targeting pkgsLLVM
stdenv = pkgSet.stdenv.override {
allowedRequisites = null;
Expand All @@ -59,7 +59,7 @@ import ./default.nix

# Expose llvmPackages used for rustc from rustc via passthru for LTO in Firefox
llvmPackages =
if (stdenv.targetPlatform.useLLVM or false) then
if (stdenv.targetPlatform.useLLVM) then
callPackage (
{
pkgs,
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/rust/rustc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let
concatStringsSep
;
inherit (darwin.apple_sdk.frameworks) Security;
useLLVM = stdenv.targetPlatform.useLLVM or false;
useLLVM = stdenv.targetPlatform.useLLVM;
in
stdenv.mkDerivation (finalAttrs: {
pname = "${targetPackages.stdenv.cc.targetPrefix}rustc";
Expand Down Expand Up @@ -207,7 +207,7 @@ stdenv.mkDerivation (finalAttrs: {
# doesn't work) to build a linker.
"--disable-llvm-bitcode-linker"
]
++ optionals (stdenv.targetPlatform.isLinux && !(stdenv.targetPlatform.useLLVM or false)) [
++ optionals (stdenv.targetPlatform.isLinux && !stdenv.targetPlatform.useLLVM) [
"--enable-profiler" # build libprofiler_builtins
]
++ optionals stdenv.buildPlatform.isMusl [
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/libraries/kerberos/krb5.nix
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ stdenv.mkDerivation rec {
++ lib.optionals (
stdenv.hostPlatform.isLinux
&& stdenv.hostPlatform.libc != "bionic"
&& !(stdenv.hostPlatform.useLLVM or false)
&& !stdenv.hostPlatform.useLLVM
) [ keyutils ]
++ lib.optionals withLdap [ openldap ]
++ lib.optionals withLibedit [ libedit ]
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/libraries/libunwind/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ stdenv.mkDerivation (finalAttrs: {
hash = "sha256-rCFBHs6rCSnp5FEwbUR5veNNTqSQpFblAv8ebSPX0qE=";
};

patches = lib.optional (stdenv.targetPlatform.useLLVM or false) (fetchpatch {
patches = lib.optional (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) (fetchpatch {
url = "https://github.com/libunwind/libunwind/pull/770/commits/a69d0f14c9e6c46e82ba6e02fcdedb2eb63b7f7f.patch";
hash = "sha256-9oBZimCXonNN++jJs3emp9w+q1aj3eNzvSKPgh92itA=";
});
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/libraries/libva/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ stdenv.mkDerivation (finalAttrs: {
{
NIX_LDFLAGS = "--undefined-version";
}
// lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) {
// lib.optionalAttrs (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) {
NIX_CFLAGS_COMPILE = "-DHAVE_SECURE_GETENV";
};

Expand Down
3 changes: 2 additions & 1 deletion pkgs/development/libraries/openssl/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ let
++ lib.optional static "etc";
setOutputFlags = false;
separateDebugInfo =
!stdenv.hostPlatform.isDarwin && !(stdenv.hostPlatform.useLLVM or false) && stdenv.cc.isGNU;
# Using "cc.isClang" causes infinite recursion.
!stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.cc != "clang" && stdenv.cc.isGNU;

nativeBuildInputs =
lib.optional (!stdenv.hostPlatform.isWindows) makeBinaryWrapper
Expand Down
Loading

0 comments on commit cad1223

Please sign in to comment.