From 65bf9330c622fac40c3e8bada075d7b2d22c85c0 Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Fri, 27 Dec 2024 08:42:26 -0800 Subject: [PATCH] lib.systems: introduce libcxx, unwinderlib, and rtlib --- lib/systems/default.nix | 18 +++++++++++++++++- pkgs/build-support/cc-wrapper/default.nix | 4 ++-- pkgs/by-name/cy/cyrus_sasl/package.nix | 2 +- pkgs/by-name/el/elfutils/package.nix | 6 +++--- pkgs/by-name/ke/kexec-tools/package.nix | 2 +- pkgs/by-name/so/sourceHighlight/package.nix | 4 ++-- .../libraries/libunwind/default.nix | 2 +- pkgs/development/libraries/libva/default.nix | 2 +- pkgs/development/libraries/openssl/default.nix | 3 ++- pkgs/development/libraries/quictls/default.nix | 2 +- .../libraries/silgraphite/graphite2.nix | 2 +- .../python-modules/jedi/default.nix | 2 +- .../python-modules/mako/default.nix | 2 +- pkgs/os-specific/linux/systemd/default.nix | 2 +- pkgs/os-specific/windows/default.nix | 2 +- pkgs/servers/x11/xorg/overrides.nix | 4 ++-- pkgs/top-level/all-packages.nix | 6 +++--- pkgs/top-level/stage.nix | 9 ++++++--- 18 files changed, 47 insertions(+), 27 deletions(-) diff --git a/lib/systems/default.nix b/lib/systems/default.nix index 38fbad3898a575..a215583e48ec15 100644 --- a/lib/systems/default.nix +++ b/lib/systems/default.nix @@ -105,12 +105,17 @@ let "clang" else "gcc"; - bintools = if final.toolchain == "bintools" then + bintools = if final.toolchain == "llvm" then "llvm" else if final.toolchain == "apple" then "apple" else "gnu"; + libcxx = + /**/ if final.toolchain == "llvm" then "libcxx" + else if final.toolchain == "gnu" then "libstdcxx" + else null; + libc = /**/ if final.isDarwin then "libSystem" else if final.isMinGW then "msvcrt" @@ -129,6 +134,17 @@ let else if final.isNone then "newlib" # TODO(@Ericson2314) think more about other operating systems else "native/impure"; + + unwinderlib = + /**/ if final.toolchain == "llvm" then "libunwinder" + else if final.toolchain == "gnu" then "libgcc_s" + else null; + + rtlib = + /**/ if final.toolchain == "llvm" then "compiler-rt" + else if final.toolchain == "gnu" then "libgcc" + else null; + # 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 diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index d69f6a13fbe938..8cc19deda4c66b 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -120,8 +120,8 @@ let useGccForLibs = useCcForLibs && libcxx == null + && targetPlatform.rtlib == "libgcc" && !targetPlatform.isDarwin - && !(targetPlatform.toolchain == "llvm") && !(targetPlatform.useAndroidPrebuilt or false) && !(targetPlatform.isiOS or false) && gccForLibs != null; @@ -514,7 +514,7 @@ stdenvNoCC.mkDerivation { + optionalString (isClang && targetPlatform.isLinux && !(targetPlatform.useAndroidPrebuilt or false) - && !(targetPlatform.toolchain == "llvm") + && targetPlatform.rtlib == "libgcc" && gccForLibs != null) ('' echo "--gcc-toolchain=${gccForLibs}" >> $out/nix-support/cc-cflags diff --git a/pkgs/by-name/cy/cyrus_sasl/package.nix b/pkgs/by-name/cy/cyrus_sasl/package.nix index 6036bc91c0afe4..8ebece88d7cfeb 100644 --- a/pkgs/by-name/cy/cyrus_sasl/package.nix +++ b/pkgs/by-name/cy/cyrus_sasl/package.nix @@ -74,7 +74,7 @@ stdenv.mkDerivation rec { "--enable-shared" ] ++ lib.optional enableLdap "--with-ldap=${openldap.dev}" - ++ lib.optionals (stdenv.targetPlatform.toolchain == "llvm") [ + ++ lib.optionals (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) [ "--disable-sample" "CFLAGS=-DTIME_WITH_SYS_TIME" ]; diff --git a/pkgs/by-name/el/elfutils/package.nix b/pkgs/by-name/el/elfutils/package.nix index aed1d2ea2b3dce..80ca5846271c7d 100644 --- a/pkgs/by-name/el/elfutils/package.nix +++ b/pkgs/by-name/el/elfutils/package.nix @@ -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.toolchain == "llvm") ./cxx-header-collision.patch; + ++ lib.optional (stdenv.hostPlatform.libcxx == "libcxx") ./cxx-header-collision.patch; postPatch = '' @@ -94,7 +94,7 @@ stdenv.mkDerivation rec { bzip2 ] ++ lib.optional enableDebuginfod pkg-config - ++ lib.optional (stdenv.targetPlatform.toolchain == "llvm") autoreconfHook; + ++ lib.optional (stdenv.hostPlatform.libcxx == "libcxx") autoreconfHook; buildInputs = [ zlib @@ -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.toolchain == "llvm") "--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" diff --git a/pkgs/by-name/ke/kexec-tools/package.nix b/pkgs/by-name/ke/kexec-tools/package.nix index c7aa9d8e9fa4aa..2d6d144d669deb 100644 --- a/pkgs/by-name/ke/kexec-tools/package.nix +++ b/pkgs/by-name/ke/kexec-tools/package.nix @@ -25,7 +25,7 @@ stdenv.mkDerivation rec { url = "https://raw.githubusercontent.com/void-linux/void-packages/6c1192cbf166698932030c2e3de71db1885a572d/srcpkgs/kexec-tools/patches/ppc64-elfv2.patch"; sha256 = "19wzfwb0azm932v0vhywv4221818qmlmvdfwpvvpfyw4hjsc2s1l"; }) - ] ++ lib.optional (stdenv.hostPlatform.toolchain == "llvm") ./fix-purgatory-llvm-libunwind.patch; + ] ++ lib.optional (stdenv.hostPlatform.unwinderlib == "libunwind") ./fix-purgatory-llvm-libunwind.patch; hardeningDisable = [ "format" diff --git a/pkgs/by-name/so/sourceHighlight/package.nix b/pkgs/by-name/so/sourceHighlight/package.nix index 613dd187822a33..774c7b76796760 100644 --- a/pkgs/by-name/so/sourceHighlight/package.nix +++ b/pkgs/by-name/so/sourceHighlight/package.nix @@ -62,7 +62,7 @@ stdenv.mkDerivation rec { nativeBuildInputs = [ updateAutotoolsGnuConfigScriptsHook ]; buildInputs = [ boost ] - ++ lib.optional (stdenv.targetPlatform.toolchain == "llvm") ( + ++ lib.optional (stdenv.hostPlatform.rtlib == "compiler-rt") ( llvmPackages.compiler-rt.override { doFakeLibgcc = true; } @@ -92,7 +92,7 @@ stdenv.mkDerivation rec { maintainers = with maintainers; [ SuperSandro2000 ]; }; } -// lib.optionalAttrs (stdenv.targetPlatform.toolchain == "llvm") { +// lib.optionalAttrs (stdenv.targetPlatform.rtlib == "libunwind") { # Force linking to "libgcc" so tests pass NIX_CFLAGS_COMPILE = "-lgcc"; } diff --git a/pkgs/development/libraries/libunwind/default.nix b/pkgs/development/libraries/libunwind/default.nix index 9c8107313719f0..687297a1121bcd 100644 --- a/pkgs/development/libraries/libunwind/default.nix +++ b/pkgs/development/libraries/libunwind/default.nix @@ -20,7 +20,7 @@ stdenv.mkDerivation (finalAttrs: { hash = "sha256-rCFBHs6rCSnp5FEwbUR5veNNTqSQpFblAv8ebSPX0qE="; }; - patches = lib.optional (stdenv.targetPlatform.toolchain == "llvm") (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="; }); diff --git a/pkgs/development/libraries/libva/default.nix b/pkgs/development/libraries/libva/default.nix index 00ecc2ac1f525e..667c03e8a5aed3 100644 --- a/pkgs/development/libraries/libva/default.nix +++ b/pkgs/development/libraries/libva/default.nix @@ -69,7 +69,7 @@ stdenv.mkDerivation (finalAttrs: { { NIX_LDFLAGS = "--undefined-version"; } - // lib.optionalAttrs (stdenv.targetPlatform.toolchain == "llvm") { + // lib.optionalAttrs (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) { NIX_CFLAGS_COMPILE = "-DHAVE_SECURE_GETENV"; }; diff --git a/pkgs/development/libraries/openssl/default.nix b/pkgs/development/libraries/openssl/default.nix index fe033d1425b6c3..f6022fb2258a40 100644 --- a/pkgs/development/libraries/openssl/default.nix +++ b/pkgs/development/libraries/openssl/default.nix @@ -98,7 +98,8 @@ let ++ lib.optional static "etc"; setOutputFlags = false; separateDebugInfo = - !stdenv.hostPlatform.isDarwin && !(stdenv.hostPlatform.toolchain == "llvm") && 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 diff --git a/pkgs/development/libraries/quictls/default.nix b/pkgs/development/libraries/quictls/default.nix index 2984b25a495bd4..55044aabc1b013 100644 --- a/pkgs/development/libraries/quictls/default.nix +++ b/pkgs/development/libraries/quictls/default.nix @@ -64,7 +64,7 @@ stdenv.mkDerivation rec { separateDebugInfo = !stdenv.hostPlatform.isDarwin && - !(stdenv.hostPlatform.toolchain == "llvm") && + !stdenv.cc.isClang && stdenv.cc.isGNU; # TODO(@Ericson2314): Improve with mass rebuild diff --git a/pkgs/development/libraries/silgraphite/graphite2.nix b/pkgs/development/libraries/silgraphite/graphite2.nix index b4055e7e3938ff..00f1a16941c293 100644 --- a/pkgs/development/libraries/silgraphite/graphite2.nix +++ b/pkgs/development/libraries/silgraphite/graphite2.nix @@ -32,7 +32,7 @@ stdenv.mkDerivation (finalAttrs: { ]; buildInputs = [ freetype ] - ++ lib.optional (stdenv.targetPlatform.toolchain == "llvm") ( + ++ lib.optional (stdenv.hostPlatform.rtlib == "compiler-rt") ( llvmPackages.compiler-rt.override { doFakeLibgcc = true; } diff --git a/pkgs/development/python-modules/jedi/default.nix b/pkgs/development/python-modules/jedi/default.nix index e9676a63a5d11a..49792d75774268 100644 --- a/pkgs/development/python-modules/jedi/default.nix +++ b/pkgs/development/python-modules/jedi/default.nix @@ -53,7 +53,7 @@ buildPythonPackage rec { # AssertionError: assert 'foo' in ['setup'] "test_init_extension_module" ] - ++ lib.optionals (stdenv.targetPlatform.toolchain == "llvm") [ + ++ lib.optionals (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) [ # InvalidPythonEnvironment: The python binary is potentially unsafe. "test_create_environment_executable" # AssertionError: assert ['', '.1000000000000001'] == ['', '.1'] diff --git a/pkgs/development/python-modules/mako/default.nix b/pkgs/development/python-modules/mako/default.nix index 4f2bc95bfa3fce..d8eea81246b9b7 100644 --- a/pkgs/development/python-modules/mako/default.nix +++ b/pkgs/development/python-modules/mako/default.nix @@ -61,7 +61,7 @@ buildPythonPackage rec { "test_bytestring_passthru" ] # https://github.com/sqlalchemy/mako/issues/408 - ++ lib.optional (stdenv.targetPlatform.toolchain == "llvm") "test_future_import"; + ++ lib.optional (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) "test_future_import"; meta = with lib; { description = "Super-fast templating language"; diff --git a/pkgs/os-specific/linux/systemd/default.nix b/pkgs/os-specific/linux/systemd/default.nix index 4ae9694a1bb272..ad09788f4af4a2 100644 --- a/pkgs/os-specific/linux/systemd/default.nix +++ b/pkgs/os-specific/linux/systemd/default.nix @@ -375,7 +375,7 @@ stdenv.mkDerivation (finalAttrs: { ++ lib.optionals withPasswordQuality [ libpwquality ] ++ lib.optionals withQrencode [ qrencode ] ++ lib.optionals withLibarchive [ libarchive ] - ++ lib.optional (withBootloader && stdenv.targetPlatform.toolchain == "llvm") (llvmPackages.compiler-rt.override { + ++ lib.optional (withBootloader && stdenv.hostPlatform.rtlib == "compiler-rt") (llvmPackages.compiler-rt.override { doFakeLibgcc = true; }) ; diff --git a/pkgs/os-specific/windows/default.nix b/pkgs/os-specific/windows/default.nix index a7d376021dfe34..42ab0c9d75c8fa 100644 --- a/pkgs/os-specific/windows/default.nix +++ b/pkgs/os-specific/windows/default.nix @@ -26,7 +26,7 @@ lib.makeScope newScope ( # FIXME untested with llvmPackages_16 was using llvmPackages_8 crossThreadsStdenv = overrideCC stdenvNoLibc ( - if stdenv.hostPlatform.toolchain == "llvm" then + if stdenv.cc.isClang then buildPackages.llvmPackages.clangNoLibcxx else buildPackages.gccWithoutTargetLibc.override (old: { diff --git a/pkgs/servers/x11/xorg/overrides.nix b/pkgs/servers/x11/xorg/overrides.nix index 6620dd7c69342d..e865d1e733fd52 100644 --- a/pkgs/servers/x11/xorg/overrides.nix +++ b/pkgs/servers/x11/xorg/overrides.nix @@ -159,7 +159,7 @@ self: super: ]; configureFlags = attrs.configureFlags or [] ++ malloc0ReturnsNullCrossFlag - ++ lib.optional (stdenv.targetPlatform.toolchain == "llvm") "ac_cv_path_RAWCPP=cpp"; + ++ lib.optional (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) "ac_cv_path_RAWCPP=cpp"; depsBuildBuild = [ buildPackages.stdenv.cc ] ++ lib.optionals stdenv.hostPlatform.isStatic [ @@ -269,7 +269,7 @@ self: super: ''; configureFlags = attrs.configureFlags or [] ++ malloc0ReturnsNullCrossFlag - ++ lib.optional (stdenv.targetPlatform.toolchain == "llvm") "ac_cv_path_RAWCPP=cpp"; + ++ lib.optional (stdenv.cc.isClang && !stdenv.hostPlatform.isDarwin) "ac_cv_path_RAWCPP=cpp"; propagatedBuildInputs = attrs.propagatedBuildInputs or [] ++ [ xorg.libSM ]; depsBuildBuild = [ buildPackages.stdenv.cc ]; CPP = if stdenv.hostPlatform.isDarwin then "clang -E -" else "${stdenv.cc.targetPrefix}cc -E -"; diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 2bafd50d0151c4..c9ab7bce9b38b4 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -61,7 +61,7 @@ with pkgs; # thing to to create an earlier thing (leading to infinite recursion) and # we also would still respect the stage arguments choices for these # things. - (if stdenvNoCC.hostPlatform.isDarwin || stdenvNoCC.hostPlatform.toolchain == "llvm" + (if stdenvNoCC.hostPlatform.cc == "clang" then overrideCC stdenvNoCC buildPackages.llvmPackages.clangNoCompilerRt else gccCrossLibcStdenv) else mkStdenvNoLibs stdenv; @@ -69,7 +69,7 @@ with pkgs; stdenvNoLibc = if stdenvNoCC.hostPlatform != stdenvNoCC.buildPlatform then - (if stdenvNoCC.hostPlatform.isDarwin || stdenvNoCC.hostPlatform.toolchain == "llvm" + (if stdenvNoCC.hostPlatform.cc == "clang" then overrideCC stdenvNoCC buildPackages.llvmPackages.clangNoLibc else gccCrossLibcStdenv) else mkStdenvNoLibs stdenv; @@ -12163,7 +12163,7 @@ with pkgs; busybox = callPackage ../os-specific/linux/busybox { # Fixes libunwind from being dynamically linked to a static binary. - stdenv = if stdenv.targetPlatform.toolchain == "llvm" then + stdenv = if stdenv.cc.isClang && stdenv.hostPlatform.unwinderlib == "libunwind" then overrideCC stdenv buildPackages.llvmPackages.clangNoLibcxx else stdenv; }; diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix index e0c8304aaa5545..b73886e192192f 100644 --- a/pkgs/top-level/stage.nix +++ b/pkgs/top-level/stage.nix @@ -118,6 +118,9 @@ let inherit (self.pkgsBuildHost.xorg) lndir; }; + cleanToolchain = platform: builtins.removeAttrs platform + [ "toolchain" "cc" "bintools" "linker" "libc" "libcxx" "unwinderlib" "rtlib" ]; + stdenvBootstappingAndPlatforms = self: super: let withFallback = thisPkgs: (if adjacentPackages == null then self else thisPkgs) @@ -201,7 +204,7 @@ let # Bootstrap a cross stdenv using the LLVM toolchain. # This is currently not possible when compiling natively, # so we don't need to check hostPlatform != buildPlatform. - crossSystem = stdenv.hostPlatform // { + crossSystem = (cleanToolchain stdenv.hostPlatform) // { toolchain = "llvm"; }; }; @@ -215,7 +218,7 @@ let # Bootstrap a cross stdenv using the Aro C compiler. # This is currently not possible when compiling natively, # so we don't need to check hostPlatform != buildPlatform. - crossSystem = stdenv.hostPlatform // { + crossSystem = (cleanToolchain stdenv.hostPlatform) // { toolchain = "llvm"; cc = "arocc"; linker = "lld"; @@ -231,7 +234,7 @@ let # Bootstrap a cross stdenv using the Zig toolchain. # This is currently not possible when compiling natively, # so we don't need to check hostPlatform != buildPlatform. - crossSystem = stdenv.hostPlatform // { + crossSystem = (cleanToolchain stdenv.hostPlatform) // { toolchain = "llvm"; cc = "zig"; linker = "lld";