Skip to content

Commit 8521a7f

Browse files
jfgoogNobodyXu
andauthoredMay 6, 2024··
Don't use the value of SDKROOT if it doesn't match the target. (#1047)
* Don't use the value of SDKROOT if it doesn't match the target. The SDKROOT environment variable can be problematic on Mac. In cases where you are compiling for multiple targets, SDKROOT cannot be right for all of them. Furthermore, the system Python interpreter sets SDKROOT to the MacOSX SDK if not already set. So, if you are using a Python script to run a build, and you need to build for multiple targets, the logic in the cc crate doesn't work. This is precisely what is happening with rustc itself, and so we can't upgrade the version of the cc crate used by the bootstrap code. (Unsetting SDKROOT doesn't work either because the custom clang build that rustc uses for CI depends on it being set) * use env::var_os instead of env::var * Fix compilation * Fix compilation error * fix compilation --------- Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 8084e0f commit 8521a7f

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
 

‎src/lib.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -3651,8 +3651,43 @@ impl Build {
36513651
}
36523652

36533653
fn apple_sdk_root(&self, sdk: &str) -> Result<OsString, Error> {
3654+
// Code copied from rustc's compiler/rustc_codegen_ssa/src/back/link.rs.
36543655
if let Some(sdkroot) = env::var_os("SDKROOT") {
3655-
return Ok(sdkroot);
3656+
let p = PathBuf::from(sdkroot);
3657+
let sdkroot = p.to_string_lossy();
3658+
match sdk {
3659+
// Ignore `SDKROOT` if it's clearly set for the wrong platform.
3660+
"appletvos"
3661+
if sdkroot.contains("TVSimulator.platform")
3662+
|| sdkroot.contains("MacOSX.platform") => {}
3663+
"appletvsimulator"
3664+
if sdkroot.contains("TVOS.platform") || sdkroot.contains("MacOSX.platform") => {
3665+
}
3666+
"iphoneos"
3667+
if sdkroot.contains("iPhoneSimulator.platform")
3668+
|| sdkroot.contains("MacOSX.platform") => {}
3669+
"iphonesimulator"
3670+
if sdkroot.contains("iPhoneOS.platform")
3671+
|| sdkroot.contains("MacOSX.platform") => {}
3672+
"macosx10.15"
3673+
if sdkroot.contains("iPhoneOS.platform")
3674+
|| sdkroot.contains("iPhoneSimulator.platform") => {}
3675+
"watchos"
3676+
if sdkroot.contains("WatchSimulator.platform")
3677+
|| sdkroot.contains("MacOSX.platform") => {}
3678+
"watchsimulator"
3679+
if sdkroot.contains("WatchOS.platform")
3680+
|| sdkroot.contains("MacOSX.platform") => {}
3681+
"xros"
3682+
if sdkroot.contains("XRSimulator.platform")
3683+
|| sdkroot.contains("MacOSX.platform") => {}
3684+
"xrsimulator"
3685+
if sdkroot.contains("XROS.platform") || sdkroot.contains("MacOSX.platform") => {
3686+
}
3687+
// Ignore `SDKROOT` if it's not a valid path.
3688+
_ if !p.is_absolute() || p == Path::new("/") || !p.exists() => {}
3689+
_ => return Ok(p.into()),
3690+
}
36563691
}
36573692

36583693
let mut cache = self

‎tests/test.rs

+26
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,32 @@ fn clang_apple_visionos() {
626626
test.cmd(0).must_not_have("-mxrsimulator-version-min=1.0");
627627
}
628628

629+
#[cfg(target_os = "macos")]
630+
#[test]
631+
fn apple_sdkroot_wrong() {
632+
let output = std::process::Command::new("xcrun")
633+
.args(["--show-sdk-path", "--sdk", "iphoneos"])
634+
.output()
635+
.unwrap();
636+
if !output.status.success() {
637+
return;
638+
}
639+
640+
let wrong_sdkroot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk";
641+
let test = Test::clang();
642+
test.gcc()
643+
.__set_env("SDKROOT", wrong_sdkroot)
644+
.target("aarch64-apple-ios")
645+
.file("foo.c")
646+
.compile("foo");
647+
648+
dbg!(test.cmd(0).args);
649+
650+
test.cmd(0)
651+
.must_have(std::str::from_utf8(&output.stdout).unwrap().trim());
652+
test.cmd(0).must_not_have(wrong_sdkroot);
653+
}
654+
629655
#[test]
630656
fn compile_intermediates() {
631657
let test = Test::gnu();

0 commit comments

Comments
 (0)