-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
macOS: Always pass SDK root when linking with cc
, and pass it via SDKROOT
env var
#131477
base: master
Are you sure you want to change the base?
Conversation
r? @nnethercote rustbot has assigned @nnethercote. Use |
These commits modify compiler targets. |
This comment has been minimized.
This comment has been minimized.
6aaa795
to
97e382d
Compare
97e382d
to
eeea5f0
Compare
This PR modifies cc @jieyouxu |
There are a few unit tests under The main reason for that is that this is really hard to write a robust test for, since this is so heavily environment specific - but I guess a test that adds the Xcode paths to |
This comment has been minimized.
This comment has been minimized.
eeea5f0
to
8ca8d4f
Compare
This comment has been minimized.
This comment has been minimized.
8ca8d4f
to
25e7f0f
Compare
25e7f0f
to
5d010db
Compare
Some changes occurred in src/doc/rustc/src/platform-support cc @Noratrieb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approve of the macOS specific parts of this. I'm not a compiler reviewer so I haven't done any review for style/etc. That said that part does look fine to me. It's a great PR overall. Great PR description too, clarified some things I had always wondered about.
✌️ @madsmtm, you can now approve this pull request! If @nnethercote told you to " |
☔ The latest upstream changes (presumably #132497) made this pull request unmergeable. Please resolve the merge conflicts. |
Also make the SDK name be the same casing as used in the file system.
5d010db
to
91c8460
Compare
91c8460
to
c3f0919
Compare
This comment has been minimized.
This comment has been minimized.
c3f0919
to
f928d11
Compare
I have become aware via rust-lang/cc-rs#1278 that Homebrew's Clang uses configuration files that passes |
f928d11
to
425ca73
Compare
The exact reasoning why we do not always pass the SDK root with `-isysroot` to `cc` when linking on macOS eludes me (the git history dead ends in rust-lang#100286), but I suspect it's because we want to support `cc`s which do not support this option. So instead, we pass the SDK root via the environment variable SDKROOT. This way, compiler drivers that support setting the SDK root (such as Clang and GCC) can use it, while compiler drivers that don't (presumably because they figure out the SDK root in some other way) can just ignore it. This fixes rust-lang#80817 (by always passing the SDK root, even when linking with cc on macOS).
425ca73
to
98cde29
Compare
About trampoline binaries
The developer binaries at
/usr/bin/*
are actually just trampolines (similar in spirit to the Rust binaries in~/.cargo/bin
) which invokexcrun
to get the current developer directory, and then launch the actual binary under/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*
.The actual binary is then launched with the following environment variables set (but none of them are set if
SDKROOT
is set explicitly):SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
LIBRARY_PATH=/usr/local/lib
(appended)CPATH=/usr/local/include
(appended)MANPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man:
(prepended)This allows the user to type e.g.
cc --version
in their terminal on macOS, and have it automatically pick up a suitable Clang binary from either an installed Xcode.app or the Xcode Command Line Tools (it acts roughly as-if you typedxcrun cc --version
).Always setting SDK root
So, the logic for finding the macOS SDK root is usually handled by the
/usr/bin/cc
trampoline, which is why we currently just remove theSDKROOT
env var when set for the wrong platform, and don't do anything else.This doesn't work when building inside Xcode though, since Xcode prepends
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
toPATH
, which means we end up calling the actual Clang binary, and end up without an SDK root specified at all.Instead, we now always set the SDK root, even when linking with
cc
on macOS. This fixes #80817, fixes #96943, and generally makes linking withcc
andld
closer in behaviour (ld
doesn't understandSDKROOT
, so it's not affected byxcrun
).Setting
SDKROOT
instead of-isysroot
The exact reasoning why we do not always pass the SDK root with
-isysroot
tocc
when linking on macOS eludes me (the git history dead ends in #100286), but I suspect it's because we want to supportcc
s which do not support this option.To make sure that such use-cases continue to work, we pass the SDK root via the
SDKROOT
environment variable. This way, compiler drivers that support setting the SDK root (such as Clang and GCC) can use it, while compiler drivers that don't (presumably because they figure out the SDK root in some other way) can just ignore it.One danger here would be if there's some compiler driver out there which works with the
-isysroot
flag, but not with theSDKROOT
environment variable. I am not aware of any?Note also that this overrides the behaviour discussed above (
/usr/bin/cc
sets some extra environment variables), I will argue that is fine:MANPATH
andCPATH
is useless when linking/usr/local/lib
is empty on a default system at least since macOS 10.14, but might be filled by extra libraries installed by the user, but I'd argue that if we want it to be part of the library search path, we should set it explicitly so that it's also set when linking withld
directly.Alternatives
/usr/bin/cc
instead ofcc
.cc
in the PATH is desired.which cc
, and do special logic if in Xcode toolchain.cc
in the Xcode toolchain that's wrong, it's the/usr/bin/cc
behaviour that is a bit too magical.xcrun --sdk macosx cc
.SDKROOT
, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable.Testing
Tested that this works with:
/usr/bin/cc
/usr/bin/ld
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
)/usr/bin/clang
(invoked viaclang
instead ofcc
)llvm
package (ignoresSDKROOT
, uses their own SDK)gcc
package (SDKROOT
is preferred over their own SDK)MacportsCouldn't get it to buildclang
gcc
(SDKROOT
is preferred over their own SDK)-isysroot
andSDKROOT
, uses their own SDK)clang
(ignoresSDKROOT
, uses their own SDK)gcc
(ignoresSDKROOT
, uses their own SDK)Doesn't accept common flags (likecosmocc
?-arch
)Meta
Part of #129432.
CC @BlackHoleFox @thomcc
Builds upon #131433, see the last commit for the actual change.
@rustbot blocked
@rustbot label relnotes