Skip to content

Commit 3287a65

Browse files
committed
Auto merge of #64254 - aleksijuvani:fix-macos-sysroot, r=alexcrichton
Fix sysroot on macOS when cross-compiling and SDKROOT is set Fixes rust-lang/cargo#7283 Closes rust-lang/cargo#7284 r? @alexcrichton
2 parents f43ac06 + fe6d626 commit 3287a65

File tree

7 files changed

+63
-4
lines changed

7 files changed

+63
-4
lines changed

src/librustc_codegen_ssa/back/command.rs

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct Command {
1515
program: Program,
1616
args: Vec<OsString>,
1717
env: Vec<(OsString, OsString)>,
18+
env_remove: Vec<OsString>,
1819
}
1920

2021
#[derive(Clone)]
@@ -42,6 +43,7 @@ impl Command {
4243
program,
4344
args: Vec::new(),
4445
env: Vec::new(),
46+
env_remove: Vec::new(),
4547
}
4648
}
4749

@@ -81,6 +83,17 @@ impl Command {
8183
self.env.push((key.to_owned(), value.to_owned()));
8284
}
8385

86+
pub fn env_remove<K>(&mut self, key: K) -> &mut Command
87+
where K: AsRef<OsStr>,
88+
{
89+
self._env_remove(key.as_ref());
90+
self
91+
}
92+
93+
fn _env_remove(&mut self, key: &OsStr) {
94+
self.env_remove.push(key.to_owned());
95+
}
96+
8497
pub fn output(&mut self) -> io::Result<Output> {
8598
self.command().output()
8699
}
@@ -106,6 +119,9 @@ impl Command {
106119
};
107120
ret.args(&self.args);
108121
ret.envs(self.env.clone());
122+
for k in &self.env_remove {
123+
ret.env_remove(k);
124+
}
109125
return ret
110126
}
111127

src/librustc_codegen_ssa/back/link.rs

+3
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
533533
for &(ref k, ref v) in &sess.target.target.options.link_env {
534534
cmd.env(k, v);
535535
}
536+
for k in &sess.target.target.options.link_env_remove {
537+
cmd.env_remove(k);
538+
}
536539

537540
if sess.opts.debugging_opts.print_link_args {
538541
println!("{:?}", &cmd);

src/librustc_target/spec/apple_base.rs

+16
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,19 @@ pub fn macos_llvm_target(arch: &str) -> String {
5151
let (major, minor) = macos_deployment_target();
5252
format!("{}-apple-macosx{}.{}.0", arch, major, minor)
5353
}
54+
55+
pub fn macos_link_env_remove() -> Vec<String> {
56+
let mut env_remove = Vec::with_capacity(2);
57+
// Remove the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
58+
// may occur when we're linking a custom build script while targeting iOS for example.
59+
if let Some(sdkroot) = env::var("SDKROOT").ok() {
60+
if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("iPhoneSimulator.platform") {
61+
env_remove.push("SDKROOT".to_string())
62+
}
63+
}
64+
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
65+
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
66+
// although this is apparently ignored when using the linker at "/usr/bin/ld".
67+
env_remove.push("IPHONEOS_DEPLOYMENT_TARGET".to_string());
68+
env_remove
69+
}

src/librustc_target/spec/apple_ios_base.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ pub fn get_sdk_root(sdk_name: &str) -> Result<String, String> {
3838
// SDKROOT; for rustc, the user or build system can set it, or we
3939
// can fall back to checking for xcrun on PATH.)
4040
if let Some(sdkroot) = env::var("SDKROOT").ok() {
41-
let sdkroot_path = Path::new(&sdkroot);
42-
if sdkroot_path.is_absolute() && sdkroot_path != Path::new("/") && sdkroot_path.exists() {
43-
return Ok(sdkroot);
41+
let p = Path::new(&sdkroot);
42+
match sdk_name {
43+
// Ignore `SDKROOT` if it's clearly set for the wrong platform.
44+
"iphoneos" if sdkroot.contains("iPhoneSimulator.platform")
45+
|| sdkroot.contains("MacOSX.platform") => (),
46+
"iphonesimulator" if sdkroot.contains("iPhoneOS.platform")
47+
|| sdkroot.contains("MacOSX.platform") => (),
48+
"macosx10.15" if sdkroot.contains("iPhoneOS.platform")
49+
|| sdkroot.contains("iPhoneSimulator.platform") => (),
50+
// Ignore `SDKROOT` if it's not a valid path.
51+
_ if !p.is_absolute() || p == Path::new("/") || !p.exists() => (),
52+
_ => return Ok(sdkroot),
4453
}
4554
}
4655
let res = Command::new("xcrun")
@@ -100,13 +109,21 @@ fn target_cpu(arch: Arch) -> String {
100109
}.to_string()
101110
}
102111

112+
fn link_env_remove(arch: Arch) -> Vec<String> {
113+
match arch {
114+
Armv7 | Armv7s | Arm64 | I386 | X86_64 => vec!["MACOSX_DEPLOYMENT_TARGET".to_string()],
115+
X86_64_macabi => vec!["IPHONEOS_DEPLOYMENT_TARGET".to_string()],
116+
}
117+
}
118+
103119
pub fn opts(arch: Arch) -> Result<TargetOptions, String> {
104120
let pre_link_args = build_pre_link_args(arch)?;
105121
Ok(TargetOptions {
106122
cpu: target_cpu(arch),
107123
dynamic_linking: false,
108124
executables: true,
109125
pre_link_args,
126+
link_env_remove: link_env_remove(arch),
110127
has_elf_tls: false,
111128
eliminate_frame_pointer: false,
112129
.. super::apple_base::opts()

src/librustc_target/spec/i686_apple_darwin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub fn target() -> TargetResult {
55
base.cpu = "yonah".to_string();
66
base.max_atomic_width = Some(64);
77
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".to_string()]);
8+
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
89
base.stack_probes = true;
910
base.eliminate_frame_pointer = false;
1011

src/librustc_target/spec/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,10 @@ pub struct TargetOptions {
581581
/// user-defined libraries.
582582
pub post_link_args: LinkArgs,
583583

584-
/// Environment variables to be set before invoking the linker.
584+
/// Environment variables to be set for the linker invocation.
585585
pub link_env: Vec<(String, String)>,
586+
/// Environment variables to be removed for the linker invocation.
587+
pub link_env_remove: Vec<String>,
586588

587589
/// Extra arguments to pass to the external assembler (when used)
588590
pub asm_args: Vec<String>,
@@ -844,6 +846,7 @@ impl Default for TargetOptions {
844846
post_link_objects_crt: Vec::new(),
845847
late_link_args: LinkArgs::new(),
846848
link_env: Vec::new(),
849+
link_env_remove: Vec::new(),
847850
archive_format: "gnu".to_string(),
848851
custom_unwind_resume: false,
849852
allow_asm: true,
@@ -1119,6 +1122,7 @@ impl Target {
11191122
key!(post_link_objects_crt, list);
11201123
key!(post_link_args, link_args);
11211124
key!(link_env, env);
1125+
key!(link_env_remove, list);
11221126
key!(asm_args, list);
11231127
key!(cpu);
11241128
key!(features);
@@ -1335,6 +1339,7 @@ impl ToJson for Target {
13351339
target_option_val!(post_link_objects_crt);
13361340
target_option_val!(link_args - post_link_args);
13371341
target_option_val!(env - link_env);
1342+
target_option_val!(link_env_remove);
13381343
target_option_val!(asm_args);
13391344
target_option_val!(cpu);
13401345
target_option_val!(features);

src/librustc_target/spec/x86_64_apple_darwin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub fn target() -> TargetResult {
66
base.max_atomic_width = Some(128); // core2 support cmpxchg16b
77
base.eliminate_frame_pointer = false;
88
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m64".to_string()]);
9+
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
910
base.stack_probes = true;
1011

1112
// Clang automatically chooses a more specific target based on

0 commit comments

Comments
 (0)