From 514b00cf08702b31cdf873a798f1ff100d4f2cf7 Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 18 Oct 2024 16:05:43 +0100 Subject: [PATCH 1/2] clang: skip the `-nostdlibinc` patch on Darwin --- pkgs/build-support/cc-wrapper/add-flags.sh | 5 ----- .../compilers/llvm/common/clang/default.nix | 18 +++++++++++------- .../compilers/llvm/common/default.nix | 18 +++++++++++++++++- pkgs/stdenv/darwin/default.nix | 11 +++++++++++ 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/pkgs/build-support/cc-wrapper/add-flags.sh b/pkgs/build-support/cc-wrapper/add-flags.sh index cd5396f45f8b..838b963ead7b 100644 --- a/pkgs/build-support/cc-wrapper/add-flags.sh +++ b/pkgs/build-support/cc-wrapper/add-flags.sh @@ -95,11 +95,6 @@ if [ "@darwinMinVersion@" ]; then # xcbuild needs `SDKROOT` to be the name of the SDK, which it sets in its own wrapper, # but compilers expect it to point to the absolute path. SDKROOT="$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" - - # Set up various library paths since compilers may not support (or may have disabled) finding them in the sysroot. - NIX_CFLAGS_COMPILE_BEFORE_@suffixSalt@+=" -isysroot $SDKROOT" - NIX_CFLAGS_COMPILE_@suffixSalt@+=" -idirafter $SDKROOT/usr/include" - NIX_CFLAGS_COMPILE_@suffixSalt@+=" -iframework $SDKROOT/System/Library/Frameworks" fi # That way forked processes will not extend these environment variables again. diff --git a/pkgs/development/compilers/llvm/common/clang/default.nix b/pkgs/development/compilers/llvm/common/clang/default.nix index 5c9662c24d58..4bf30400832a 100644 --- a/pkgs/development/compilers/llvm/common/clang/default.nix +++ b/pkgs/development/compilers/llvm/common/clang/default.nix @@ -81,13 +81,17 @@ let # Make sure clang passes the correct location of libLTO to ld64 substituteInPlace lib/Driver/ToolChains/Darwin.cpp \ --replace-fail 'StringRef P = llvm::sys::path::parent_path(D.Dir);' 'StringRef P = "${lib.getLib libllvm}";' - '' + (if lib.versionOlder release_version "13" then '' - sed -i -e 's/DriverArgs.hasArg(options::OPT_nostdlibinc)/true/' \ - -e 's/Args.hasArg(options::OPT_nostdlibinc)/true/' \ - lib/Driver/ToolChains/*.cpp - '' else '' - (cd tools && ln -s ../../clang-tools-extra extra) - '') + lib.optionalString stdenv.hostPlatform.isMusl '' + '' + ( + # See the comment on the `add-nostdlibinc-flag.patch` patch in + # `../default.nix` for why we skip Darwin here. + if lib.versionOlder release_version "13" && (!stdenv.hostPlatform.isDarwin || !stdenv.targetPlatform.isDarwin) then '' + sed -i -e 's/DriverArgs.hasArg(options::OPT_nostdlibinc)/true/' \ + -e 's/Args.hasArg(options::OPT_nostdlibinc)/true/' \ + lib/Driver/ToolChains/*.cpp + '' else '' + (cd tools && ln -s ../../clang-tools-extra extra) + '' + ) + lib.optionalString stdenv.hostPlatform.isMusl '' sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp ''; diff --git a/pkgs/development/compilers/llvm/common/default.nix b/pkgs/development/compilers/llvm/common/default.nix index eaba6f45edfe..c3a98c1f063f 100644 --- a/pkgs/development/compilers/llvm/common/default.nix +++ b/pkgs/development/compilers/llvm/common/default.nix @@ -529,8 +529,24 @@ let # mis-compilation in firefox. # See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 (metadata.getVersionFile "clang/revert-malloc-alignment-assumption.patch") + # This patch prevents global system header directories from + # leaking through on non‐NixOS Linux. However, on macOS, the + # SDK path is used as the sysroot, and forcing `-nostdlibinc` + # breaks `-isysroot` with an unwrapped compiler. As macOS has + # no `/usr/include`, there’s essentially no risk to skipping + # the patch there. It’s possible that Homebrew headers in + # `/usr/local/include` might leak through to unwrapped + # compilers being used without an SDK set or something, but + # it hopefully shouldn’t matter. + # + # TODO: Figure out a better solution to this whole problem so + # that we won’t have to choose between breaking unwrapped + # compilers breaking libclang when we can do Linux‐to‐Darwin + # cross‐compilation again. + ++ lib.optional ( + !args.stdenv.hostPlatform.isDarwin || !args.stdenv.targetPlatform.isDarwin + ) ./clang/add-nostdlibinc-flag.patch ++ [ - ./clang/add-nostdlibinc-flag.patch (substituteAll { src = if (lib.versionOlder metadata.release_version "16") then diff --git a/pkgs/stdenv/darwin/default.nix b/pkgs/stdenv/darwin/default.nix index 5a3ef15e392d..070d5ec23851 100644 --- a/pkgs/stdenv/darwin/default.nix +++ b/pkgs/stdenv/darwin/default.nix @@ -133,6 +133,17 @@ let ln -s "${compiler-rt.out}/lib" "$rsrc/lib" ln -s "${compiler-rt.out}/share" "$rsrc/share" echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags + '' + + lib.optionalString (isFromBootstrapFiles prevStage.llvmPackages.clang-unwrapped) '' + # Work around the `-nostdlibinc` patch in the bootstrap tools. + # TODO: Remove after the bootstrap tools have been updated. + substituteAll ${builtins.toFile "add-flags-extra.sh" '' + if [ "@darwinMinVersion@" ]; then + NIX_CFLAGS_COMPILE_@suffixSalt@+=" -idirafter $SDKROOT/usr/include" + NIX_CFLAGS_COMPILE_@suffixSalt@+=" -iframework $SDKROOT/System/Library/Frameworks" + fi + ''} add-flags-extra.sh + cat add-flags-extra.sh >> $out/nix-support/add-flags.sh ''; cc = prevStage.llvmPackages.clang-unwrapped; From b75c70282f91276908adf0f286437eadbb638119 Mon Sep 17 00:00:00 2001 From: Emily Date: Wed, 23 Oct 2024 17:13:28 +0100 Subject: [PATCH 2/2] ld64: search standard library locations This is basically harmless for the same reason as it is for Clang, and lets us avoid doing wrapper hacks to fix things like the .NET build. This reverts commit 4340a5addb0c9da67b1f68dc00ba2a3fc80436cb. --- .../add-darwin-ldflags-before.sh | 10 -------- ...rching-in-standard-library-locations.patch | 25 ------------------- pkgs/by-name/ld/ld64/package.nix | 2 -- pkgs/stdenv/darwin/default.nix | 13 +++++++++- 4 files changed, 12 insertions(+), 38 deletions(-) delete mode 100644 pkgs/by-name/ld/ld64/0008-Disable-searching-in-standard-library-locations.patch diff --git a/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh b/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh index 0a2961bbe46e..127f83e303a3 100644 --- a/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh +++ b/pkgs/build-support/bintools-wrapper/add-darwin-ldflags-before.sh @@ -85,13 +85,3 @@ mangleVarSingle DEVELOPER_DIR ${role_suffixes[@]+"${role_suffixes[@]}"} # Allow wrapped bintools to do something useful when no `DEVELOPER_DIR` is set, which can happen when # the compiler is run outside of a stdenv or intentionally in an environment with no environment variables set. DEVELOPER_DIR=${DEVELOPER_DIR_@suffixSalt@:-@fallback_sdk@} - -# Darwin looks for frameworks in the SDK located at `DEVELOPER_DIR`. -extraBefore+=("-F$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks") -extraBefore+=("-L$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib") - -# While the Swift wrapper should take care of this, anything that needs to link Swift auto-linked frameworks -# also needs these paths. Note: Test and conditionally add it because the path may not exist in older SDKs. -if [ -d "$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/swift" ]; then - extraBefore+=("-L$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/swift") -fi diff --git a/pkgs/by-name/ld/ld64/0008-Disable-searching-in-standard-library-locations.patch b/pkgs/by-name/ld/ld64/0008-Disable-searching-in-standard-library-locations.patch deleted file mode 100644 index ddda588c6aaf..000000000000 --- a/pkgs/by-name/ld/ld64/0008-Disable-searching-in-standard-library-locations.patch +++ /dev/null @@ -1,25 +0,0 @@ -From 3e80d438e2a3ec50d666f2b6e32007c275d4a08a Mon Sep 17 00:00:00 2001 -From: Randy Eckenrode -Date: Thu, 11 Apr 2024 23:13:29 -0400 -Subject: [PATCH 8/8] Disable searching in standard library locations - ---- - src/ld/Options.cpp | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/src/ld/Options.cpp b/src/ld/Options.cpp -index 67a9f53..611b583 100644 ---- a/src/ld/Options.cpp -+++ b/src/ld/Options.cpp -@@ -4320,7 +4320,7 @@ bool Options::shouldUseBuildVersion(ld::Platform plat, uint32_t minOSvers) const - - void Options::buildSearchPaths(int argc, const char* argv[]) - { -- bool addStandardLibraryDirectories = true; -+ bool addStandardLibraryDirectories = false; - ld::Platform platform = ld::Platform::unknown; - std::vector libraryPaths; - std::vector frameworkPaths; --- -2.45.1 - diff --git a/pkgs/by-name/ld/ld64/package.nix b/pkgs/by-name/ld/ld64/package.nix index b5900f3576ac..d3dfe3b8b7e6 100644 --- a/pkgs/by-name/ld/ld64/package.nix +++ b/pkgs/by-name/ld/ld64/package.nix @@ -128,8 +128,6 @@ stdenv.mkDerivation (finalAttrs: { ./0006-Add-libcd_is_blob_a_linker_signature-implementation.patch # Add OpenSSL implementation of CoreCrypto digest functions. Avoids use of private and non-free APIs. ./0007-Add-OpenSSL-based-CoreCrypto-digest-functions.patch - # ld64 will search `/usr/lib`, `/Library/Frameworks`, etc by default. Disable that. - ./0008-Disable-searching-in-standard-library-locations.patch ]; postPatch = '' diff --git a/pkgs/stdenv/darwin/default.nix b/pkgs/stdenv/darwin/default.nix index 070d5ec23851..0fb0f27f23ba 100644 --- a/pkgs/stdenv/darwin/default.nix +++ b/pkgs/stdenv/darwin/default.nix @@ -449,9 +449,15 @@ assert bootstrapTools.passthru.isFromBootstrapFiles or false; # sanity check bintools = selfDarwin.binutils-unwrapped; - # Bootstrap tools cctools needs the hook and wrappers to make sure things are signed properly. + # Bootstrap tools cctools needs the hook and wrappers to make sure things are signed properly, + # and additional linker flags to work around a since‐removed patch. # This can be dropped once the bootstrap tools cctools has been updated to 1010.6. extraBuildCommands = '' + printf %s ${lib.escapeShellArg '' + extraBefore+=("-F$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks") + extraBefore+=("-L$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib") + ''} >> $out/nix-support/add-local-ldflags-before.sh + echo 'source ${selfDarwin.postLinkSignHook}' >> $out/nix-support/post-link-hook export signingUtils=${selfDarwin.signingUtils} @@ -694,6 +700,11 @@ assert bootstrapTools.passthru.isFromBootstrapFiles or false; # sanity check # Bootstrap tools cctools needs the hook and wrappers to make sure things are signed properly. # This can be dropped once the bootstrap tools cctools has been updated to 1010.6. extraBuildCommands = '' + printf %s ${lib.escapeShellArg '' + extraBefore+=("-F$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks") + extraBefore+=("-L$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib") + ''} >> $out/nix-support/add-local-ldflags-before.sh + echo 'source ${selfDarwin.postLinkSignHook}' >> $out/nix-support/post-link-hook export signingUtils=${selfDarwin.signingUtils}