Skip to content
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

Apple: Re-implement SDK discovery instead of using xcrun #131433

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

madsmtm
Copy link
Contributor

@madsmtm madsmtm commented Oct 9, 2024

Shelling out to a global command line tool like this makes it difficult for rustc to properly implement change-tracking of the involved variables and paths (e.g. we could tell Cargo to relink after the user runs xcode-select --switch or installs/updates Xcode) as required for #118204, and makes error handling more difficult. So instead, this PR re-implements the SDK discovery logic that xcrun does, to avoid depending on that.

The logic is now roughly:

let sdkroot = if let Some(sdkroot) = env::var("SDKROOT") && sdk_matches(sdkroot, target_os) {
    sdkroot
} else if let Some(dir) = env::var("DEVELOPER_DIR") {
    search_for_platform_and_sdk(dir, sdk_name)
} else if let Some(path) = fs::read_link("/var/db/xcode_select_link")? { // Special path that `xcode-select --switch` configures, undocumented
    search_for_platform_and_sdk(path, sdk_name)
} else if fs::exists("/Applications/Xcode.app")? { // Hard-coded fall back
    search_for_platform_and_sdk("/Applications/Xcode.app/Contents/Developer", sdk_name)
} else if fs::exists("/Library/Developer/CommandLineTools")? { // Hard-coded fall back
    search_for_sdk("/Library/Developer/CommandLineTools/SDKs", sdk_name)
} else {
    todo!("tell the user that they need to install Xcode")
};

I have tested this in a newly created virtual machine, and verified that:

Part of #129432.

I'm not sure, but I don't think release notes are desirable here? The behaviour should be mostly identical apart from better error messages, and besides, this was very under-documented before.

CC @BlackHoleFox @thomcc.

@rustbot
Copy link
Collaborator

rustbot commented Oct 9, 2024

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Oct 9, 2024

These commits modify compiler targets.
(See the Target Tier Policy.)

@rustbot rustbot added O-apple Operating system: Apple (macOS, iOS, tvOS, visionOS, watchOS) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 9, 2024
@madsmtm madsmtm changed the title Re-implement SDK discovery instead of using xcrun Apple: Re-implement SDK discovery instead of using xcrun Oct 9, 2024
@workingjubilee
Copy link
Member

workingjubilee commented Oct 9, 2024

not saying we shouldn't add this, but how long do you think is left until we get fed up with shoving this kind of code into cg_ssa and split out rustc_linker? ...or would it be rustc_archiver?

@madsmtm
Copy link
Contributor Author

madsmtm commented Oct 9, 2024

not saying we shouldn't add this, but how long do you think is left until we get fed up with shoving this kind of code into cg_ssa and split out rustc_linker? ...or would it be rustc_archiver?

No idea honestly, I'm not really familiar enough with the overall crate structure to say.

compiler/rustc_codegen_ssa/src/apple.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/apple.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/apple.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/apple.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/apple/tests.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/apple/tests.rs Outdated Show resolved Hide resolved
@BlackHoleFox
Copy link
Contributor

Short initial review since I was tagged, but not an actual compiler reviewer etc etc.

@rustbot
Copy link
Collaborator

rustbot commented Oct 31, 2024

Some changes occurred in src/doc/rustc/src/platform-support

cc @Noratrieb

@workingjubilee
Copy link
Member

r? compiler

@BlackHoleFox
Copy link
Contributor

macOS changes here look fine to me.

@bors
Copy link
Contributor

bors commented Nov 2, 2024

☔ 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-apple Operating system: Apple (macOS, iOS, tvOS, visionOS, watchOS) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
7 participants