Skip to content

Fix sysroot on macOS when cross-compiling and SDKROOT is set #64254

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

Merged
merged 6 commits into from Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/librustc_target/spec/apple_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,19 @@ pub fn macos_llvm_target(arch: &str) -> String {
let (major, minor) = macos_deployment_target();
format!("{}-apple-macosx{}.{}.0", arch, major, minor)
}

pub fn macos_link_env() -> Vec<(String, String)> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit of an odd interface since it actually only returns env vars which should be cleared out, not env vars which should be set. Do we know what the various tools will behave right when set to blank values which effectively disables them? The alternative would be to extend link_env with the ability to delete env vars as well (e.g. env_remove).

Additionally, it seems like this would also want to encompass the ios targets perhaps? For example if you set SDKROOT for osx or MACOSX_DEPLOYMENT_TARGET, should those values be automatically cleared out for ios compliation in addition to macos compilation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit of an odd interface since it actually only returns env vars which should be cleared out, not env vars which should be set. Do we know what the various tools will behave right when set to blank values which effectively disables them? The alternative would be to extend link_env with the ability to delete env vars as well (e.g. env_remove).

If the variable is set to an empty string, the linker behaves as if the environment variable wasn't set. We can rework this to remove environment variables instead if that feels better.

Additionally, it seems like this would also want to encompass the ios targets perhaps? For example if you set SDKROOT for osx or MACOSX_DEPLOYMENT_TARGET, should those values be automatically cleared out for ios compliation in addition to macos compilation?

Yes, this makes sense. We should clear SDKROOT if it's set to the wrong platform when targeting iOS. The value of MACOSX_DEPLOYMENT_TARGET doesn't appear to be meaningful in the same way that IPHONEOS_DEPLOYMENT_TARGET is (i.e. it doesn't appear to be used by the linker to infer the target platform), so perhaps we don't need to clear this one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that last bit actually—MACOSX_DEPLOYMENT_TARGET is used, but it doesn't manifest until you actually try running the executable on the simulator, which will fail with dyld: program was built for a platform that is not supported by this runtime if the environment variable was set.

let mut env = Vec::with_capacity(2);
// Ignore the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
// may occur when we're linking a custom build script while targeting iOS for example.
if let Some(sdkroot) = env::var("SDKROOT").ok() {
if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("iPhoneSimulator.platform") {
env.push(("SDKROOT".to_string(), String::new()))
}
}
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
// although this is apparently ignored when using the linker at "/usr/bin/ld".
env.push(("IPHONEOS_DEPLOYMENT_TARGET".to_string(), String::new()));
env
}
1 change: 1 addition & 0 deletions src/librustc_target/spec/i686_apple_darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub fn target() -> TargetResult {
base.cpu = "yonah".to_string();
base.max_atomic_width = Some(64);
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".to_string()]);
base.link_env.extend(super::apple_base::macos_link_env());
base.stack_probes = true;
base.eliminate_frame_pointer = false;

Expand Down
1 change: 1 addition & 0 deletions src/librustc_target/spec/x86_64_apple_darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn target() -> TargetResult {
base.max_atomic_width = Some(128); // core2 support cmpxchg16b
base.eliminate_frame_pointer = false;
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m64".to_string()]);
base.link_env.extend(super::apple_base::macos_link_env());
base.stack_probes = true;

// Clang automatically chooses a more specific target based on
Expand Down