Skip to content

Commit

Permalink
clang: skip the -nostdlibinc patch on Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
emilazy committed Oct 24, 2024
1 parent 65f010f commit bf7b0b5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
5 changes: 0 additions & 5 deletions pkgs/build-support/cc-wrapper/add-flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions pkgs/development/compilers/llvm/common/clang/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
'';

Expand Down
18 changes: 17 additions & 1 deletion pkgs/development/compilers/llvm/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion pkgs/stdenv/darwin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ let
overrides ? (self: super: { }),
extraNativeBuildInputs ? [ ],
extraPreHook ? "",
# TODO: Remove after the bootstrap tools have been updated.
extraBuildCommands ? "",
}:

let
Expand Down Expand Up @@ -133,7 +135,8 @@ 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
'';
''
+ extraBuildCommands;

cc = prevStage.llvmPackages.clang-unwrapped;
bintools = prevStage.darwin.binutils;
Expand Down Expand Up @@ -392,6 +395,18 @@ assert bootstrapTools.passthru.isFromBootstrapFiles or false; # sanity check
stageFun prevStage {
name = "bootstrap-stage0";

# Work around the `-nostdlibinc` patch in the bootstrap tools.
# TODO: Remove after the bootstrap tools have been updated.
extraBuildCommands = ''
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
'';

overrides = self: super: {
# We thread stage0's stdenv through under this name so downstream stages
# can use it for wrapping gcc too. This way, downstream stages don't need
Expand Down

0 comments on commit bf7b0b5

Please sign in to comment.