Skip to content

Commit 995d985

Browse files
committed
Fix cross-compilation to macOS
1 parent 6dc763e commit 995d985

File tree

4 files changed

+62
-50
lines changed

4 files changed

+62
-50
lines changed

src/librustc_target/spec/apple_base.rs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, io, path::Path, process::Command};
1+
use std::{env, path::Path};
22

33
use crate::spec::{LinkArgs, TargetOptions};
44

@@ -52,43 +52,60 @@ pub fn macos_llvm_target(arch: &str) -> String {
5252
format!("{}-apple-macosx{}.{}.0", arch, major, minor)
5353
}
5454

55-
pub fn sysroot(sdk: &str) -> Result<String, String> {
56-
let actual_sdk_path = sdk_path(sdk)?;
55+
#[cfg(target_os = "macos")]
56+
pub fn sysroot(sdk: &str) -> Result<Option<String>, String> {
5757
// Like Clang, allow the `SDKROOT` environment variable used by Xcode to define the sysroot.
5858
if let Some(sdk_root) = env::var("SDKROOT").ok() {
59+
let actual_sdk_path = sdk_path(sdk)?;
5960
let sdk_root_p = Path::new(&sdk_root);
6061
// Ignore `SDKROOT` if it's not a valid path.
6162
if !sdk_root_p.is_absolute() || sdk_root_p == Path::new("/") || !sdk_root_p.exists() {
62-
return Ok(actual_sdk_path);
63+
return Ok(Some(actual_sdk_path));
6364
}
6465
// Ignore `SDKROOT` if it's clearly set for the wrong platform, which may occur when we're
6566
// compiling a custom build script while targeting iOS for example.
66-
return Ok(match sdk {
67+
return Ok(Some(match sdk {
6768
"iphoneos" if sdk_root.contains("iPhoneSimulator.platform")
6869
|| sdk_root.contains("MacOSX.platform") => actual_sdk_path,
6970
"iphonesimulator" if sdk_root.contains("iPhoneOS.platform")
7071
|| sdk_root.contains("MacOSX.platform") => actual_sdk_path,
7172
"macosx" | "macosx10.15" if sdk_root.contains("iPhoneOS.platform")
7273
|| sdk_root.contains("iPhoneSimulator.platform") => actual_sdk_path,
7374
_ => sdk_root,
74-
})
75+
}))
7576
}
76-
Ok(actual_sdk_path)
77+
Ok(None)
7778
}
7879

80+
// `xcrun` is only available on macOS.
81+
#[cfg(not(target_os = "macos"))]
82+
pub fn sysroot(_sdk: &str) -> Result<Option<String>, String> {
83+
if let Some(sdk_root) = env::var("SDKROOT").ok() {
84+
let sdk_root_p = Path::new(&sdk_root);
85+
// Use `SDKROOT` only if it's a valid path.
86+
if sdk_root_p.is_absolute() && sdk_root_p != Path::new("/") && sdk_root_p.exists() {
87+
return Ok(Some(sdk_root));
88+
}
89+
}
90+
Ok(None)
91+
}
92+
93+
#[cfg(target_os = "macos")]
7994
fn sdk_path(sdk_name: &str) -> Result<String, String> {
80-
let res =
81-
Command::new("xcrun").arg("--show-sdk-path").arg("-sdk").arg(sdk_name).output().and_then(
82-
|output| {
83-
if output.status.success() {
84-
Ok(String::from_utf8(output.stdout).unwrap())
85-
} else {
86-
let error = String::from_utf8(output.stderr);
87-
let error = format!("process exit with error: {}", error.unwrap());
88-
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
89-
}
90-
},
91-
);
95+
let res = std::process::Command::new("xcrun")
96+
.arg("--show-sdk-path")
97+
.arg("-sdk")
98+
.arg(sdk_name)
99+
.output()
100+
.and_then(|output| {
101+
if output.status.success() {
102+
Ok(String::from_utf8(output.stdout).unwrap())
103+
} else {
104+
let error = String::from_utf8(output.stderr);
105+
let error = format!("process exit with error: {}", error.unwrap());
106+
Err(std::io::Error::new(std::io::ErrorKind::Other, &error[..]))
107+
}
108+
});
92109
match res {
93110
Ok(output) => Ok(output.trim().to_string()),
94111
Err(e) => Err(format!("failed to get {} SDK path: {}", sdk_name, e)),

src/librustc_target/spec/apple_ios_base.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ fn build_pre_link_args(arch: Arch) -> Result<LinkArgs, String> {
3535

3636
let arch_name = arch.to_string();
3737

38-
let sdk_root = super::apple_base::sysroot(sdk_name)?;
39-
4038
let mut args = LinkArgs::new();
41-
args.insert(LinkerFlavor::Gcc,
42-
vec!["-arch".to_string(),
43-
arch_name.to_string(),
44-
"-isysroot".to_string(),
45-
sdk_root.clone(),
46-
"-Wl,-syslibroot".to_string(),
47-
sdk_root]);
39+
args.insert(LinkerFlavor::Gcc, vec!["-arch".to_string(), arch_name.to_string()]);
40+
if let Some(sdk_root) = super::apple_base::sysroot(sdk_name)? {
41+
args.insert(
42+
LinkerFlavor::Gcc,
43+
vec![
44+
"-isysroot".to_string(),
45+
sdk_root.clone(),
46+
"-Wl,-syslibroot".to_string(),
47+
sdk_root
48+
],
49+
);
50+
}
4851

4952
Ok(args)
5053
}

src/librustc_target/spec/i686_apple_darwin.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
22

33
pub fn target() -> TargetResult {
4-
let sysroot = super::apple_base::sysroot("macosx")?;
54
let mut base = super::apple_base::opts();
65
base.cpu = "yonah".to_string();
76
base.max_atomic_width = Some(64);
8-
base.pre_link_args.insert(
9-
LinkerFlavor::Gcc,
10-
vec![
11-
"-m32".to_string(),
12-
"-isysroot".to_string(),
13-
sysroot.clone(),
14-
"-Wl,-syslibroot".to_string(),
15-
sysroot,
16-
],
17-
);
7+
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".to_string()]);
8+
if let Some(sysroot) = super::apple_base::sysroot("macosx")? {
9+
base.pre_link_args.insert(
10+
LinkerFlavor::Gcc,
11+
vec!["-isysroot".to_string(), sysroot.clone(), "-Wl,-syslibroot".to_string(), sysroot],
12+
);
13+
}
1814
base.stack_probes = true;
1915
base.eliminate_frame_pointer = false;
2016

src/librustc_target/spec/x86_64_apple_darwin.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
22

33
pub fn target() -> TargetResult {
4-
let sysroot = super::apple_base::sysroot("macosx")?;
54
let mut base = super::apple_base::opts();
65
base.cpu = "core2".to_string();
76
base.max_atomic_width = Some(128); // core2 support cmpxchg16b
87
base.eliminate_frame_pointer = false;
9-
base.pre_link_args.insert(
10-
LinkerFlavor::Gcc,
11-
vec![
12-
"-m64".to_string(),
13-
"-isysroot".to_string(),
14-
sysroot.clone(),
15-
"-Wl,-syslibroot".to_string(),
16-
sysroot,
17-
],
18-
);
8+
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m64".to_string()]);
9+
if let Some(sysroot) = super::apple_base::sysroot("macosx")? {
10+
base.pre_link_args.insert(
11+
LinkerFlavor::Gcc,
12+
vec!["-isysroot".to_string(), sysroot.clone(), "-Wl,-syslibroot".to_string(), sysroot],
13+
);
14+
}
1915
base.stack_probes = true;
2016

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

0 commit comments

Comments
 (0)