Skip to content

Commit 2294b05

Browse files
committed
Always set the deployment target when building std
1 parent 8729ba3 commit 2294b05

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,55 @@ fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
462462
/// Configure cargo to compile the standard library, adding appropriate env vars
463463
/// and such.
464464
pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) {
465-
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
466-
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
465+
// rustc already ensures that it builds with the minimum deployment
466+
// target, so ideally we shouldn't need to do anything here.
467+
//
468+
// However, `cc` currently defaults to a higher version for backwards
469+
// compatibility, which means that compiler-rt, which is built via
470+
// compiler-builtins' build script, gets built with a higher deployment
471+
// target. This in turn causes warnings while linking, and is generally
472+
// a compatibility hazard.
473+
//
474+
// So, at least until https://github.com/rust-lang/cc-rs/issues/1171, or
475+
// perhaps https://github.com/rust-lang/cargo/issues/13115 is resolved, we
476+
// explicitly set the deployment target environment variables to avoid
477+
// this issue.
478+
//
479+
// This place also serves as an extension point if we ever wanted to raise
480+
// rustc's default deployment target while keeping the prebuilt `std` at
481+
// a lower version, so it's kinda nice to have in any case.
482+
if target.contains("apple") && !builder.config.dry_run() {
483+
// Query rustc for the deployment target.
484+
let mut cmd = command(builder.rustc(cargo.compiler()));
485+
cmd.arg("--target").arg(target.rustc_target_arg());
486+
cmd.arg("--print=deployment-target");
487+
let output = cmd.run_capture_stdout(builder).stdout();
488+
489+
let value = output.split('=').last().unwrap().trim();
490+
491+
// FIXME: Simplify after https://github.com/rust-lang/rust/pull/133041
492+
let env_var = if target.contains("apple-darwin") {
493+
"MACOSX_DEPLOYMENT_TARGET"
494+
} else if target.contains("apple-ios") {
495+
"IPHONEOS_DEPLOYMENT_TARGET"
496+
} else if target.contains("apple-tvos") {
497+
"TVOS_DEPLOYMENT_TARGET"
498+
} else if target.contains("apple-watchos") {
499+
"WATCHOS_DEPLOYMENT_TARGET"
500+
} else if target.contains("apple-visionos") {
501+
"XROS_DEPLOYMENT_TARGET"
502+
} else {
503+
panic!("unknown target OS for apple target");
504+
};
505+
506+
// Unconditionally set the env var (if it was set in the environment
507+
// already, rustc should've picked that up).
508+
cargo.env(env_var, value);
509+
510+
// Allow CI to override the deployment target for `std`.
511+
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
512+
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
513+
}
467514
}
468515

469516
// Paths needed by `library/profiler_builtins/build.rs`.

src/bootstrap/src/core/builder/cargo.rs

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ impl Cargo {
122122
cargo
123123
}
124124

125+
pub fn compiler(&self) -> Compiler {
126+
self.compiler
127+
}
128+
125129
pub fn into_cmd(self) -> BootstrapCommand {
126130
self.into()
127131
}

tests/run-make/apple-deployment-target/rmake.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
88
//@ only-apple
99

10-
use run_make_support::{apple_os, cmd, run_in_tmpdir, rustc, target};
10+
use std::collections::HashSet;
11+
12+
use run_make_support::{
13+
apple_os, cmd, has_extension, path, regex, run_in_tmpdir, rustc, shallow_find_files, target,
14+
};
1115

1216
/// Run vtool to check the `minos` field in LC_BUILD_VERSION.
1317
///
@@ -156,4 +160,19 @@ fn main() {
156160
rustc().env_remove(env_var).run();
157161
minos("foo.o", default_version);
158162
});
163+
164+
// Test that all binaries in rlibs produced by `rustc` have the same version.
165+
let sysroot = rustc().print("sysroot").run().stdout_utf8();
166+
let target_sysroot = path(sysroot.trim()).join("lib/rustlib").join(target()).join("lib");
167+
let rlibs = shallow_find_files(&target_sysroot, |path| has_extension(path, "rlib"));
168+
169+
let output = cmd("otool").arg("-l").args(rlibs).run().stdout_utf8();
170+
let re = regex::Regex::new(r"(minos|version) ([0-9.]*)").unwrap();
171+
let mut versions = HashSet::new();
172+
for (_, [_, version]) in re.captures_iter(&output).map(|c| c.extract()) {
173+
versions.insert(version);
174+
}
175+
if versions.len() != 1 {
176+
panic!("std rlibs contained multiple different deployment target versions: {versions:?}");
177+
}
159178
}

0 commit comments

Comments
 (0)