-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Apple: Always pass SDK root when linking with cc
, and pass it via SDKROOT
env var
#131477
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
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.
This comment has been minimized.
This comment has been minimized.
The exact reasoning why we do not always pass the SDK root when linking on macOS eludes me, but I suspect it's because we want to support compiler drivers which do not support the `-isysroot` option. Since we now pass the SDK root via the environment variable SDKROOT, compiler drivers that don't support it can just ignore it. Similarly, since we only warn when xcrun fails, users that expect their compiler driver to provide the SDK location can do so now.
4f25b1e
to
1d13162
Compare
@bors r+ |
…thercote Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var Fixes rust-lang#80817, fixes rust-lang#96943, and generally simplifies our linker invocation on Apple platforms. Part of rust-lang#129432. ### Necessary background on trampoline binaries The developer binaries such as `/usr/bin/cc` and `/usr/bin/clang` are actually trampolines (similar in spirit to the Rust binaries in `~/.cargo/bin`) which effectively invokes `xcrun` to get the current Xcode developer directory, which allows it to find the actual binary under `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*`. This 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. `clang foo.c` in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools. (It acts roughly as-if you typed `xcrun -sdk macosx clang foo.c`). ### Finding a suitable SDK All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (`/usr/include` is gone), and the system libraries are elsewhere in the file system (`/usr/lib` is basically empty). Instead, the logic for finding the SDK is handled by the `/usr/bin/cc` trampoline (see above). But relying on the `cc` trampoline doesn't work when: - Cross-compiling, since a different SDK is needed there. - Invoking the linker directly, since the linker doesn't understand `SDKROOT`. - Linking build scripts inside Xcode (see rust-lang#80817), since Xcode prepends `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin` to `PATH`, which means `cc` refers to the _actual_ Clang binary, and we end up with the wrong SDK root specified. Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with `cc` for macOS (i.e. the most common path), so that **we now always invoke `xcrun` (if `SDKROOT` is not explicitly specified) to find the SDK root**. Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the `-isysroot` flag. These difficulties are explored below. #### No Xcode There are several compiler drivers which work without Xcode by bundling their own SDK, including `zig cc`, Nixpkgs' `clang` and Homebrew's `llvm` package. Additionally, `xcrun` is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with `-Clink-args=...`. We do still want to _try_ to invoke `xcrun` if possible, since it is usually the SDK that the user wants (and if not, the environment should override `xcrun`, such as is done by Nixpkgs). But we do not want failure to invoke `xcrun` to stop the linking process. This is changed in the second-to-last commit. #### `SDKROOT` vs. `-isysroot` The exact reasoning why we do not always pass the SDK root 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 compiler drivers which do not support the `-isysroot` option. To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it. One small danger here would be if there's some compiler driver out there which works with the `-isysroot` flag, but not with the `SDKROOT` environment variable. I am not aware of any? In a sense, this also shifts the blame; if a compiler driver does not understand `SDKROOT`, it won't work with e.g. `xcrun -sdk macosx15.0 $tool` either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool. Note also that this overrides the behaviour discussed above (`/usr/bin/cc` sets some extra environment variables), I will argue that is fine since `MANPATH` and `CPATH` is useless when linking, and `/usr/local/lib` is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with `-Clinker=ld`). ### Considered alternatives - Invoke `/usr/bin/cc` instead of `cc`. - This breaks many other use-cases though where overriding `cc` in the PATH is desired. - Look up `which cc`, and do special logic if in Xcode toolchain. - Seems brittle, and besides, it's not the `cc` in the Xcode toolchain that's wrong, it's the `/usr/bin/cc` behaviour that is a bit too magical. - Invoke `xcrun --sdk macosx cc`. - This completely ignores `SDKROOT`, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable. - Maybe somehow configure the linker with extra flags such that it'll be able to link regardless of linking for macOS or e.g. iOS? Though I doubt this is possible. - Bundle the SDK, similar to `zig-cc`. - Comes with it's own host of problems. ### Testing Tested that this works with the following `-Clinker=...`: - [x] Default (`cc`) - [x] `/usr/bin/ld` - [x] Actual Clang from Xcode (`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang`) - [x] `/usr/bin/clang` (invoked via `clang` instead of `cc`) - [x] Homebrew's `llvm` package (ignores `SDKROOT`, uses their own SDK) - [x] Homebrew's `gcc` package (`SDKROOT` is preferred over their own SDK) - [x] ~Macports `clang`~ Couldn't get it to build - [x] Macports `gcc` (`SDKROOT` is preferred over their own SDK) - [x] Zig CC installed via. homebrew (ignores both `-isysroot` and `SDKROOT`, uses their own SDK) - [x] Nixpkgs `clang` (ignores `SDKROOT`, uses their own SDK) - [x] Nixpkgs `gcc` (ignores `SDKROOT`, uses their own SDK) - [x] ~[`cosmocc`](https://github.com/jart/cosmopolitan)?~ Doesn't accept common flags (like `-arch`) CC `@BlackHoleFox` `@thomcc`
…thercote Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var Fixes rust-lang#80817, fixes rust-lang#96943, and generally simplifies our linker invocation on Apple platforms. Part of rust-lang#129432. ### Necessary background on trampoline binaries The developer binaries such as `/usr/bin/cc` and `/usr/bin/clang` are actually trampolines (similar in spirit to the Rust binaries in `~/.cargo/bin`) which effectively invokes `xcrun` to get the current Xcode developer directory, which allows it to find the actual binary under `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*`. This 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. `clang foo.c` in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools. (It acts roughly as-if you typed `xcrun -sdk macosx clang foo.c`). ### Finding a suitable SDK All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (`/usr/include` is gone), and the system libraries are elsewhere in the file system (`/usr/lib` is basically empty). Instead, the logic for finding the SDK is handled by the `/usr/bin/cc` trampoline (see above). But relying on the `cc` trampoline doesn't work when: - Cross-compiling, since a different SDK is needed there. - Invoking the linker directly, since the linker doesn't understand `SDKROOT`. - Linking build scripts inside Xcode (see rust-lang#80817), since Xcode prepends `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin` to `PATH`, which means `cc` refers to the _actual_ Clang binary, and we end up with the wrong SDK root specified. Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with `cc` for macOS (i.e. the most common path), so that **we now always invoke `xcrun` (if `SDKROOT` is not explicitly specified) to find the SDK root**. Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the `-isysroot` flag. These difficulties are explored below. #### No Xcode There are several compiler drivers which work without Xcode by bundling their own SDK, including `zig cc`, Nixpkgs' `clang` and Homebrew's `llvm` package. Additionally, `xcrun` is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with `-Clink-args=...`. We do still want to _try_ to invoke `xcrun` if possible, since it is usually the SDK that the user wants (and if not, the environment should override `xcrun`, such as is done by Nixpkgs). But we do not want failure to invoke `xcrun` to stop the linking process. This is changed in the second-to-last commit. #### `SDKROOT` vs. `-isysroot` The exact reasoning why we do not always pass the SDK root 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 compiler drivers which do not support the `-isysroot` option. To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it. One small danger here would be if there's some compiler driver out there which works with the `-isysroot` flag, but not with the `SDKROOT` environment variable. I am not aware of any? In a sense, this also shifts the blame; if a compiler driver does not understand `SDKROOT`, it won't work with e.g. `xcrun -sdk macosx15.0 $tool` either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool. Note also that this overrides the behaviour discussed above (`/usr/bin/cc` sets some extra environment variables), I will argue that is fine since `MANPATH` and `CPATH` is useless when linking, and `/usr/local/lib` is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with `-Clinker=ld`). ### Considered alternatives - Invoke `/usr/bin/cc` instead of `cc`. - This breaks many other use-cases though where overriding `cc` in the PATH is desired. - Look up `which cc`, and do special logic if in Xcode toolchain. - Seems brittle, and besides, it's not the `cc` in the Xcode toolchain that's wrong, it's the `/usr/bin/cc` behaviour that is a bit too magical. - Invoke `xcrun --sdk macosx cc`. - This completely ignores `SDKROOT`, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable. - Maybe somehow configure the linker with extra flags such that it'll be able to link regardless of linking for macOS or e.g. iOS? Though I doubt this is possible. - Bundle the SDK, similar to `zig-cc`. - Comes with it's own host of problems. ### Testing Tested that this works with the following `-Clinker=...`: - [x] Default (`cc`) - [x] `/usr/bin/ld` - [x] Actual Clang from Xcode (`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang`) - [x] `/usr/bin/clang` (invoked via `clang` instead of `cc`) - [x] Homebrew's `llvm` package (ignores `SDKROOT`, uses their own SDK) - [x] Homebrew's `gcc` package (`SDKROOT` is preferred over their own SDK) - [x] ~Macports `clang`~ Couldn't get it to build - [x] Macports `gcc` (`SDKROOT` is preferred over their own SDK) - [x] Zig CC installed via. homebrew (ignores both `-isysroot` and `SDKROOT`, uses their own SDK) - [x] Nixpkgs `clang` (ignores `SDKROOT`, uses their own SDK) - [x] Nixpkgs `gcc` (ignores `SDKROOT`, uses their own SDK) - [x] ~[`cosmocc`](https://github.com/jart/cosmopolitan)?~ Doesn't accept common flags (like `-arch`) CC ``@BlackHoleFox`` ``@thomcc``
…thercote Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var Fixes rust-lang#80817, fixes rust-lang#96943, and generally simplifies our linker invocation on Apple platforms. Part of rust-lang#129432. ### Necessary background on trampoline binaries The developer binaries such as `/usr/bin/cc` and `/usr/bin/clang` are actually trampolines (similar in spirit to the Rust binaries in `~/.cargo/bin`) which effectively invokes `xcrun` to get the current Xcode developer directory, which allows it to find the actual binary under `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*`. This 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. `clang foo.c` in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools. (It acts roughly as-if you typed `xcrun -sdk macosx clang foo.c`). ### Finding a suitable SDK All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (`/usr/include` is gone), and the system libraries are elsewhere in the file system (`/usr/lib` is basically empty). Instead, the logic for finding the SDK is handled by the `/usr/bin/cc` trampoline (see above). But relying on the `cc` trampoline doesn't work when: - Cross-compiling, since a different SDK is needed there. - Invoking the linker directly, since the linker doesn't understand `SDKROOT`. - Linking build scripts inside Xcode (see rust-lang#80817), since Xcode prepends `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin` to `PATH`, which means `cc` refers to the _actual_ Clang binary, and we end up with the wrong SDK root specified. Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with `cc` for macOS (i.e. the most common path), so that **we now always invoke `xcrun` (if `SDKROOT` is not explicitly specified) to find the SDK root**. Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the `-isysroot` flag. These difficulties are explored below. #### No Xcode There are several compiler drivers which work without Xcode by bundling their own SDK, including `zig cc`, Nixpkgs' `clang` and Homebrew's `llvm` package. Additionally, `xcrun` is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with `-Clink-args=...`. We do still want to _try_ to invoke `xcrun` if possible, since it is usually the SDK that the user wants (and if not, the environment should override `xcrun`, such as is done by Nixpkgs). But we do not want failure to invoke `xcrun` to stop the linking process. This is changed in the second-to-last commit. #### `SDKROOT` vs. `-isysroot` The exact reasoning why we do not always pass the SDK root 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 compiler drivers which do not support the `-isysroot` option. To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it. One small danger here would be if there's some compiler driver out there which works with the `-isysroot` flag, but not with the `SDKROOT` environment variable. I am not aware of any? In a sense, this also shifts the blame; if a compiler driver does not understand `SDKROOT`, it won't work with e.g. `xcrun -sdk macosx15.0 $tool` either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool. Note also that this overrides the behaviour discussed above (`/usr/bin/cc` sets some extra environment variables), I will argue that is fine since `MANPATH` and `CPATH` is useless when linking, and `/usr/local/lib` is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with `-Clinker=ld`). ### Considered alternatives - Invoke `/usr/bin/cc` instead of `cc`. - This breaks many other use-cases though where overriding `cc` in the PATH is desired. - Look up `which cc`, and do special logic if in Xcode toolchain. - Seems brittle, and besides, it's not the `cc` in the Xcode toolchain that's wrong, it's the `/usr/bin/cc` behaviour that is a bit too magical. - Invoke `xcrun --sdk macosx cc`. - This completely ignores `SDKROOT`, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable. - Maybe somehow configure the linker with extra flags such that it'll be able to link regardless of linking for macOS or e.g. iOS? Though I doubt this is possible. - Bundle the SDK, similar to `zig-cc`. - Comes with it's own host of problems. ### Testing Tested that this works with the following `-Clinker=...`: - [x] Default (`cc`) - [x] `/usr/bin/ld` - [x] Actual Clang from Xcode (`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang`) - [x] `/usr/bin/clang` (invoked via `clang` instead of `cc`) - [x] Homebrew's `llvm` package (ignores `SDKROOT`, uses their own SDK) - [x] Homebrew's `gcc` package (`SDKROOT` is preferred over their own SDK) - [x] ~Macports `clang`~ Couldn't get it to build - [x] Macports `gcc` (`SDKROOT` is preferred over their own SDK) - [x] Zig CC installed via. homebrew (ignores both `-isysroot` and `SDKROOT`, uses their own SDK) - [x] Nixpkgs `clang` (ignores `SDKROOT`, uses their own SDK) - [x] Nixpkgs `gcc` (ignores `SDKROOT`, uses their own SDK) - [x] ~[`cosmocc`](https://github.com/jart/cosmopolitan)?~ Doesn't accept common flags (like `-arch`) CC ```@BlackHoleFox``` ```@thomcc```
…thercote Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var Fixes rust-lang#80817, fixes rust-lang#96943, and generally simplifies our linker invocation on Apple platforms. Part of rust-lang#129432. ### Necessary background on trampoline binaries The developer binaries such as `/usr/bin/cc` and `/usr/bin/clang` are actually trampolines (similar in spirit to the Rust binaries in `~/.cargo/bin`) which effectively invokes `xcrun` to get the current Xcode developer directory, which allows it to find the actual binary under `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*`. This 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. `clang foo.c` in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools. (It acts roughly as-if you typed `xcrun -sdk macosx clang foo.c`). ### Finding a suitable SDK All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (`/usr/include` is gone), and the system libraries are elsewhere in the file system (`/usr/lib` is basically empty). Instead, the logic for finding the SDK is handled by the `/usr/bin/cc` trampoline (see above). But relying on the `cc` trampoline doesn't work when: - Cross-compiling, since a different SDK is needed there. - Invoking the linker directly, since the linker doesn't understand `SDKROOT`. - Linking build scripts inside Xcode (see rust-lang#80817), since Xcode prepends `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin` to `PATH`, which means `cc` refers to the _actual_ Clang binary, and we end up with the wrong SDK root specified. Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with `cc` for macOS (i.e. the most common path), so that **we now always invoke `xcrun` (if `SDKROOT` is not explicitly specified) to find the SDK root**. Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the `-isysroot` flag. These difficulties are explored below. #### No Xcode There are several compiler drivers which work without Xcode by bundling their own SDK, including `zig cc`, Nixpkgs' `clang` and Homebrew's `llvm` package. Additionally, `xcrun` is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with `-Clink-args=...`. We do still want to _try_ to invoke `xcrun` if possible, since it is usually the SDK that the user wants (and if not, the environment should override `xcrun`, such as is done by Nixpkgs). But we do not want failure to invoke `xcrun` to stop the linking process. This is changed in the second-to-last commit. #### `SDKROOT` vs. `-isysroot` The exact reasoning why we do not always pass the SDK root 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 compiler drivers which do not support the `-isysroot` option. To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it. One small danger here would be if there's some compiler driver out there which works with the `-isysroot` flag, but not with the `SDKROOT` environment variable. I am not aware of any? In a sense, this also shifts the blame; if a compiler driver does not understand `SDKROOT`, it won't work with e.g. `xcrun -sdk macosx15.0 $tool` either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool. Note also that this overrides the behaviour discussed above (`/usr/bin/cc` sets some extra environment variables), I will argue that is fine since `MANPATH` and `CPATH` is useless when linking, and `/usr/local/lib` is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with `-Clinker=ld`). ### Considered alternatives - Invoke `/usr/bin/cc` instead of `cc`. - This breaks many other use-cases though where overriding `cc` in the PATH is desired. - Look up `which cc`, and do special logic if in Xcode toolchain. - Seems brittle, and besides, it's not the `cc` in the Xcode toolchain that's wrong, it's the `/usr/bin/cc` behaviour that is a bit too magical. - Invoke `xcrun --sdk macosx cc`. - This completely ignores `SDKROOT`, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable. - Maybe somehow configure the linker with extra flags such that it'll be able to link regardless of linking for macOS or e.g. iOS? Though I doubt this is possible. - Bundle the SDK, similar to `zig-cc`. - Comes with it's own host of problems. ### Testing Tested that this works with the following `-Clinker=...`: - [x] Default (`cc`) - [x] `/usr/bin/ld` - [x] Actual Clang from Xcode (`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang`) - [x] `/usr/bin/clang` (invoked via `clang` instead of `cc`) - [x] Homebrew's `llvm` package (ignores `SDKROOT`, uses their own SDK) - [x] Homebrew's `gcc` package (`SDKROOT` is preferred over their own SDK) - [x] ~Macports `clang`~ Couldn't get it to build - [x] Macports `gcc` (`SDKROOT` is preferred over their own SDK) - [x] Zig CC installed via. homebrew (ignores both `-isysroot` and `SDKROOT`, uses their own SDK) - [x] Nixpkgs `clang` (ignores `SDKROOT`, uses their own SDK) - [x] Nixpkgs `gcc` (ignores `SDKROOT`, uses their own SDK) - [x] ~[`cosmocc`](https://github.com/jart/cosmopolitan)?~ Doesn't accept common flags (like `-arch`) CC ````@BlackHoleFox```` ````@thomcc````
Rollup of 15 pull requests Successful merges: - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var) - #139806 (std: sys: pal: uefi: Overhaul Time) - #144386 (Extract TraitImplHeader in AST/HIR) - #144542 (Stabilize `sse4a` and `tbm` target features) - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`) - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2)) - #145214 (fix: re-enable self-assignment) - #145216 (rustdoc: correct negative-to-implicit discriminant display) - #145238 (Tweak invalid builtin attribute output) - #145249 (Rename entered trace span variables from `_span` to `_trace`) - #145251 (Support using #[unstable_feature_bound] on trait) - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job) - #145260 (Make explicit guarantees about `Vec`’s allocator) - #145263 (Update books) - #145273 (Account for new `assert!` desugaring in `!condition` suggestion) r? `@ghost` `@rustbot` modify labels: rollup
…thercote Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var Fixes rust-lang#80817, fixes rust-lang#96943, and generally simplifies our linker invocation on Apple platforms. Part of rust-lang#129432. ### Necessary background on trampoline binaries The developer binaries such as `/usr/bin/cc` and `/usr/bin/clang` are actually trampolines (similar in spirit to the Rust binaries in `~/.cargo/bin`) which effectively invokes `xcrun` to get the current Xcode developer directory, which allows it to find the actual binary under `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*`. This 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. `clang foo.c` in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools. (It acts roughly as-if you typed `xcrun -sdk macosx clang foo.c`). ### Finding a suitable SDK All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (`/usr/include` is gone), and the system libraries are elsewhere in the file system (`/usr/lib` is basically empty). Instead, the logic for finding the SDK is handled by the `/usr/bin/cc` trampoline (see above). But relying on the `cc` trampoline doesn't work when: - Cross-compiling, since a different SDK is needed there. - Invoking the linker directly, since the linker doesn't understand `SDKROOT`. - Linking build scripts inside Xcode (see rust-lang#80817), since Xcode prepends `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin` to `PATH`, which means `cc` refers to the _actual_ Clang binary, and we end up with the wrong SDK root specified. Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with `cc` for macOS (i.e. the most common path), so that **we now always invoke `xcrun` (if `SDKROOT` is not explicitly specified) to find the SDK root**. Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the `-isysroot` flag. These difficulties are explored below. #### No Xcode There are several compiler drivers which work without Xcode by bundling their own SDK, including `zig cc`, Nixpkgs' `clang` and Homebrew's `llvm` package. Additionally, `xcrun` is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with `-Clink-args=...`. We do still want to _try_ to invoke `xcrun` if possible, since it is usually the SDK that the user wants (and if not, the environment should override `xcrun`, such as is done by Nixpkgs). But we do not want failure to invoke `xcrun` to stop the linking process. This is changed in the second-to-last commit. #### `SDKROOT` vs. `-isysroot` The exact reasoning why we do not always pass the SDK root 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 compiler drivers which do not support the `-isysroot` option. To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it. One small danger here would be if there's some compiler driver out there which works with the `-isysroot` flag, but not with the `SDKROOT` environment variable. I am not aware of any? In a sense, this also shifts the blame; if a compiler driver does not understand `SDKROOT`, it won't work with e.g. `xcrun -sdk macosx15.0 $tool` either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool. Note also that this overrides the behaviour discussed above (`/usr/bin/cc` sets some extra environment variables), I will argue that is fine since `MANPATH` and `CPATH` is useless when linking, and `/usr/local/lib` is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with `-Clinker=ld`). ### Considered alternatives - Invoke `/usr/bin/cc` instead of `cc`. - This breaks many other use-cases though where overriding `cc` in the PATH is desired. - Look up `which cc`, and do special logic if in Xcode toolchain. - Seems brittle, and besides, it's not the `cc` in the Xcode toolchain that's wrong, it's the `/usr/bin/cc` behaviour that is a bit too magical. - Invoke `xcrun --sdk macosx cc`. - This completely ignores `SDKROOT`, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable. - Maybe somehow configure the linker with extra flags such that it'll be able to link regardless of linking for macOS or e.g. iOS? Though I doubt this is possible. - Bundle the SDK, similar to `zig-cc`. - Comes with it's own host of problems. ### Testing Tested that this works with the following `-Clinker=...`: - [x] Default (`cc`) - [x] `/usr/bin/ld` - [x] Actual Clang from Xcode (`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang`) - [x] `/usr/bin/clang` (invoked via `clang` instead of `cc`) - [x] Homebrew's `llvm` package (ignores `SDKROOT`, uses their own SDK) - [x] Homebrew's `gcc` package (`SDKROOT` is preferred over their own SDK) - [x] ~Macports `clang`~ Couldn't get it to build - [x] Macports `gcc` (`SDKROOT` is preferred over their own SDK) - [x] Zig CC installed via. homebrew (ignores both `-isysroot` and `SDKROOT`, uses their own SDK) - [x] Nixpkgs `clang` (ignores `SDKROOT`, uses their own SDK) - [x] Nixpkgs `gcc` (ignores `SDKROOT`, uses their own SDK) - [x] ~[`cosmocc`](https://github.com/jart/cosmopolitan)?~ Doesn't accept common flags (like `-arch`) CC `````@BlackHoleFox````` `````@thomcc`````
Rollup of 14 pull requests Successful merges: - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var) - #139806 (std: sys: pal: uefi: Overhaul Time) - #144210 (std: thread: Return error if setting thread stack size fails) - #144386 (Extract TraitImplHeader in AST/HIR) - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`) - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2)) - #145214 (fix: re-enable self-assignment) - #145216 (rustdoc: correct negative-to-implicit discriminant display) - #145238 (Tweak invalid builtin attribute output) - #145249 (Rename entered trace span variables from `_span` to `_trace`) - #145251 (Support using #[unstable_feature_bound] on trait) - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job) - #145263 (Update books) - #145273 (Account for new `assert!` desugaring in `!condition` suggestion) r? `@ghost` `@rustbot` modify labels: rollup
…thercote Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var Fixes rust-lang#80817, fixes rust-lang#96943, and generally simplifies our linker invocation on Apple platforms. Part of rust-lang#129432. ### Necessary background on trampoline binaries The developer binaries such as `/usr/bin/cc` and `/usr/bin/clang` are actually trampolines (similar in spirit to the Rust binaries in `~/.cargo/bin`) which effectively invokes `xcrun` to get the current Xcode developer directory, which allows it to find the actual binary under `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*`. This 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. `clang foo.c` in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools. (It acts roughly as-if you typed `xcrun -sdk macosx clang foo.c`). ### Finding a suitable SDK All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (`/usr/include` is gone), and the system libraries are elsewhere in the file system (`/usr/lib` is basically empty). Instead, the logic for finding the SDK is handled by the `/usr/bin/cc` trampoline (see above). But relying on the `cc` trampoline doesn't work when: - Cross-compiling, since a different SDK is needed there. - Invoking the linker directly, since the linker doesn't understand `SDKROOT`. - Linking build scripts inside Xcode (see rust-lang#80817), since Xcode prepends `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin` to `PATH`, which means `cc` refers to the _actual_ Clang binary, and we end up with the wrong SDK root specified. Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with `cc` for macOS (i.e. the most common path), so that **we now always invoke `xcrun` (if `SDKROOT` is not explicitly specified) to find the SDK root**. Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the `-isysroot` flag. These difficulties are explored below. #### No Xcode There are several compiler drivers which work without Xcode by bundling their own SDK, including `zig cc`, Nixpkgs' `clang` and Homebrew's `llvm` package. Additionally, `xcrun` is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with `-Clink-args=...`. We do still want to _try_ to invoke `xcrun` if possible, since it is usually the SDK that the user wants (and if not, the environment should override `xcrun`, such as is done by Nixpkgs). But we do not want failure to invoke `xcrun` to stop the linking process. This is changed in the second-to-last commit. #### `SDKROOT` vs. `-isysroot` The exact reasoning why we do not always pass the SDK root 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 compiler drivers which do not support the `-isysroot` option. To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it. One small danger here would be if there's some compiler driver out there which works with the `-isysroot` flag, but not with the `SDKROOT` environment variable. I am not aware of any? In a sense, this also shifts the blame; if a compiler driver does not understand `SDKROOT`, it won't work with e.g. `xcrun -sdk macosx15.0 $tool` either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool. Note also that this overrides the behaviour discussed above (`/usr/bin/cc` sets some extra environment variables), I will argue that is fine since `MANPATH` and `CPATH` is useless when linking, and `/usr/local/lib` is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with `-Clinker=ld`). ### Considered alternatives - Invoke `/usr/bin/cc` instead of `cc`. - This breaks many other use-cases though where overriding `cc` in the PATH is desired. - Look up `which cc`, and do special logic if in Xcode toolchain. - Seems brittle, and besides, it's not the `cc` in the Xcode toolchain that's wrong, it's the `/usr/bin/cc` behaviour that is a bit too magical. - Invoke `xcrun --sdk macosx cc`. - This completely ignores `SDKROOT`, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable. - Maybe somehow configure the linker with extra flags such that it'll be able to link regardless of linking for macOS or e.g. iOS? Though I doubt this is possible. - Bundle the SDK, similar to `zig-cc`. - Comes with it's own host of problems. ### Testing Tested that this works with the following `-Clinker=...`: - [x] Default (`cc`) - [x] `/usr/bin/ld` - [x] Actual Clang from Xcode (`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang`) - [x] `/usr/bin/clang` (invoked via `clang` instead of `cc`) - [x] Homebrew's `llvm` package (ignores `SDKROOT`, uses their own SDK) - [x] Homebrew's `gcc` package (`SDKROOT` is preferred over their own SDK) - [x] ~Macports `clang`~ Couldn't get it to build - [x] Macports `gcc` (`SDKROOT` is preferred over their own SDK) - [x] Zig CC installed via. homebrew (ignores both `-isysroot` and `SDKROOT`, uses their own SDK) - [x] Nixpkgs `clang` (ignores `SDKROOT`, uses their own SDK) - [x] Nixpkgs `gcc` (ignores `SDKROOT`, uses their own SDK) - [x] ~[`cosmocc`](https://github.com/jart/cosmopolitan)?~ Doesn't accept common flags (like `-arch`) CC ``````@BlackHoleFox`````` ``````@thomcc``````
Rollup of 17 pull requests Successful merges: - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var) - #139806 (std: sys: pal: uefi: Overhaul Time) - #144210 (std: thread: Return error if setting thread stack size fails) - #144386 (Extract TraitImplHeader in AST/HIR) - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`) - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2)) - #145214 (fix: re-enable self-assignment) - #145216 (rustdoc: correct negative-to-implicit discriminant display) - #145238 (Tweak invalid builtin attribute output) - #145249 (Rename entered trace span variables from `_span` to `_trace`) - #145251 (Support using #[unstable_feature_bound] on trait) - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job) - #145260 (Make explicit guarantees about `Vec`’s allocator) - #145263 (Update books) - #145273 (Account for new `assert!` desugaring in `!condition` suggestion) - #145283 (Make I-miscompile imply I-prioritize) - #145291 (bootstrap: Only warn about `rust.debug-assertions` if downloading rustc) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 17 pull requests Successful merges: - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var) - #139806 (std: sys: pal: uefi: Overhaul Time) - #144386 (Extract TraitImplHeader in AST/HIR) - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`) - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2)) - #145214 (fix: re-enable self-assignment) - #145216 (rustdoc: correct negative-to-implicit discriminant display) - #145238 (Tweak invalid builtin attribute output) - #145249 (Rename entered trace span variables from `_span` to `_trace`) - #145251 (Support using #[unstable_feature_bound] on trait) - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job) - #145260 (Make explicit guarantees about `Vec`’s allocator) - #145263 (Update books) - #145273 (Account for new `assert!` desugaring in `!condition` suggestion) - #145283 (Make I-miscompile imply I-prioritize) - #145291 (bootstrap: Only warn about `rust.debug-assertions` if downloading rustc) - #145292 (Fix a typo in range docs) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #131477 - madsmtm:sdkroot-via-env-var, r=nnethercote Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var Fixes #80817, fixes #96943, and generally simplifies our linker invocation on Apple platforms. Part of #129432. ### Necessary background on trampoline binaries The developer binaries such as `/usr/bin/cc` and `/usr/bin/clang` are actually trampolines (similar in spirit to the Rust binaries in `~/.cargo/bin`) which effectively invokes `xcrun` to get the current Xcode developer directory, which allows it to find the actual binary under `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*`. This 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. `clang foo.c` in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools. (It acts roughly as-if you typed `xcrun -sdk macosx clang foo.c`). ### Finding a suitable SDK All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (`/usr/include` is gone), and the system libraries are elsewhere in the file system (`/usr/lib` is basically empty). Instead, the logic for finding the SDK is handled by the `/usr/bin/cc` trampoline (see above). But relying on the `cc` trampoline doesn't work when: - Cross-compiling, since a different SDK is needed there. - Invoking the linker directly, since the linker doesn't understand `SDKROOT`. - Linking build scripts inside Xcode (see #80817), since Xcode prepends `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin` to `PATH`, which means `cc` refers to the _actual_ Clang binary, and we end up with the wrong SDK root specified. Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with `cc` for macOS (i.e. the most common path), so that **we now always invoke `xcrun` (if `SDKROOT` is not explicitly specified) to find the SDK root**. Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the `-isysroot` flag. These difficulties are explored below. #### No Xcode There are several compiler drivers which work without Xcode by bundling their own SDK, including `zig cc`, Nixpkgs' `clang` and Homebrew's `llvm` package. Additionally, `xcrun` is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with `-Clink-args=...`. We do still want to _try_ to invoke `xcrun` if possible, since it is usually the SDK that the user wants (and if not, the environment should override `xcrun`, such as is done by Nixpkgs). But we do not want failure to invoke `xcrun` to stop the linking process. This is changed in the second-to-last commit. #### `SDKROOT` vs. `-isysroot` The exact reasoning why we do not always pass the SDK root when linking on macOS eludes me (the git history dead ends in #100286), but I suspect it's because we want to support compiler drivers which do not support the `-isysroot` option. To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it. One small danger here would be if there's some compiler driver out there which works with the `-isysroot` flag, but not with the `SDKROOT` environment variable. I am not aware of any? In a sense, this also shifts the blame; if a compiler driver does not understand `SDKROOT`, it won't work with e.g. `xcrun -sdk macosx15.0 $tool` either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool. Note also that this overrides the behaviour discussed above (`/usr/bin/cc` sets some extra environment variables), I will argue that is fine since `MANPATH` and `CPATH` is useless when linking, and `/usr/local/lib` is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with `-Clinker=ld`). ### Considered alternatives - Invoke `/usr/bin/cc` instead of `cc`. - This breaks many other use-cases though where overriding `cc` in the PATH is desired. - Look up `which cc`, and do special logic if in Xcode toolchain. - Seems brittle, and besides, it's not the `cc` in the Xcode toolchain that's wrong, it's the `/usr/bin/cc` behaviour that is a bit too magical. - Invoke `xcrun --sdk macosx cc`. - This completely ignores `SDKROOT`, so we'd still have to parse that first to figure out if it's suitable or not, but would probably be workable. - Maybe somehow configure the linker with extra flags such that it'll be able to link regardless of linking for macOS or e.g. iOS? Though I doubt this is possible. - Bundle the SDK, similar to `zig-cc`. - Comes with it's own host of problems. ### Testing Tested that this works with the following `-Clinker=...`: - [x] Default (`cc`) - [x] `/usr/bin/ld` - [x] Actual Clang from Xcode (`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang`) - [x] `/usr/bin/clang` (invoked via `clang` instead of `cc`) - [x] Homebrew's `llvm` package (ignores `SDKROOT`, uses their own SDK) - [x] Homebrew's `gcc` package (`SDKROOT` is preferred over their own SDK) - [x] ~Macports `clang`~ Couldn't get it to build - [x] Macports `gcc` (`SDKROOT` is preferred over their own SDK) - [x] Zig CC installed via. homebrew (ignores both `-isysroot` and `SDKROOT`, uses their own SDK) - [x] Nixpkgs `clang` (ignores `SDKROOT`, uses their own SDK) - [x] Nixpkgs `gcc` (ignores `SDKROOT`, uses their own SDK) - [x] ~[`cosmocc`](https://github.com/jart/cosmopolitan)?~ Doesn't accept common flags (like `-arch`) CC ```````@BlackHoleFox``````` ```````@thomcc```````
Rollup of 17 pull requests Successful merges: - rust-lang/rust#131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var) - rust-lang/rust#139806 (std: sys: pal: uefi: Overhaul Time) - rust-lang/rust#144386 (Extract TraitImplHeader in AST/HIR) - rust-lang/rust#144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`) - rust-lang/rust#145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2)) - rust-lang/rust#145214 (fix: re-enable self-assignment) - rust-lang/rust#145216 (rustdoc: correct negative-to-implicit discriminant display) - rust-lang/rust#145238 (Tweak invalid builtin attribute output) - rust-lang/rust#145249 (Rename entered trace span variables from `_span` to `_trace`) - rust-lang/rust#145251 (Support using #[unstable_feature_bound] on trait) - rust-lang/rust#145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job) - rust-lang/rust#145260 (Make explicit guarantees about `Vec`’s allocator) - rust-lang/rust#145263 (Update books) - rust-lang/rust#145273 (Account for new `assert!` desugaring in `!condition` suggestion) - rust-lang/rust#145283 (Make I-miscompile imply I-prioritize) - rust-lang/rust#145291 (bootstrap: Only warn about `rust.debug-assertions` if downloading rustc) - rust-lang/rust#145292 (Fix a typo in range docs) r? `@ghost` `@rustbot` modify labels: rollup
Fixes #80817, fixes #96943, and generally simplifies our linker invocation on Apple platforms.
Part of #129432.
Necessary background on trampoline binaries
The developer binaries such as
/usr/bin/cc
and/usr/bin/clang
are actually trampolines (similar in spirit to the Rust binaries in~/.cargo/bin
) which effectively invokesxcrun
to get the current Xcode developer directory, which allows it to find the actual binary under/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/*
.This 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.
clang foo.c
in their terminal on macOS, and have it automatically pick up a suitable Clang binary and SDK from either an installed Xcode.app or the Xcode Command Line Tools.(It acts roughly as-if you typed
xcrun -sdk macosx clang foo.c
).Finding a suitable SDK
All compilation on macOS is cross-compilation using SDKs, there are no system headers any more (
/usr/include
is gone), and the system libraries are elsewhere in the file system (/usr/lib
is basically empty). Instead, the logic for finding the SDK is handled by the/usr/bin/cc
trampoline (see above).But relying on the
cc
trampoline doesn't work when:SDKROOT
./Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
toPATH
, which meanscc
refers to the actual Clang binary, and we end up with the wrong SDK root specified.Basically, we cannot rely on the trampoline at all, so the last commit removes the special-casing that was done when linking with
cc
for macOS (i.e. the most common path), so that we now always invokexcrun
(ifSDKROOT
is not explicitly specified) to find the SDK root.Making sure this is non-breaking has a few difficulties though, namely that the user might not have Xcode installed, and that the compiler driver may not understand the
-isysroot
flag. These difficulties are explored below.No Xcode
There are several compiler drivers which work without Xcode by bundling their own SDK, including
zig cc
, Nixpkgs'clang
and Homebrew'sllvm
package. Additionally,xcrun
is rarely available when cross-compiling from non-macOS and instead the user might provide a downloaded SDK manually with-Clink-args=...
.We do still want to try to invoke
xcrun
if possible, since it is usually the SDK that the user wants (and if not, the environment should overridexcrun
, such as is done by Nixpkgs). But we do not want failure to invokexcrun
to stop the linking process. This is changed in the second-to-last commit.SDKROOT
vs.-isysroot
The exact reasoning why we do not always pass the SDK root when linking on macOS eludes me (the git history dead ends in #100286), but I suspect it's because we want to support compiler drivers which do not support the
-isysroot
option.To make sure that such use-cases continue to work, we now 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 in some other way) can just ignore it.One small 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?In a sense, this also shifts the blame; if a compiler driver does not understand
SDKROOT
, it won't work with e.g.xcrun -sdk macosx15.0 $tool
either, so it can more clearly be argued that this is incorrect behaviour on the part of the tool.Note also that this overrides the behaviour discussed above (
/usr/bin/cc
sets some extra environment variables), I will argue that is fine sinceMANPATH
andCPATH
is useless when linking, and/usr/local/lib
is empty on a default system at least since macOS 10.14 (it might be filled by extra libraries installed by the user, but I'll argue that if we want it to be part of the default library search path, we should set it explicitly so that it's also set when linking with-Clinker=ld
).Considered 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.zig-cc
.Testing
Tested that this works with the following
-Clinker=...
: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
)CC @BlackHoleFox @thomcc