Skip to content

Commit 9318fbb

Browse files
committed
Always set the deployment target when building std
1 parent e790b92 commit 9318fbb

File tree

2 files changed

+57
-3
lines changed
  • src/bootstrap/src/core/build_steps
  • tests/run-make/apple-deployment-target

2 files changed

+57
-3
lines changed

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,41 @@ fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
443443
/// Configure cargo to compile the standard library, adding appropriate env vars
444444
/// and such.
445445
pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) {
446-
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
447-
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
446+
// rustc already ensures that it builds with the minimum deployment
447+
// target, so ideally we shouldn't need to do anything here.
448+
//
449+
// However, `cc` currently defaults to a higher version for backwards
450+
// compatibility, which means that compiler-rt, which is built via
451+
// compiler-builtins' build script, gets built with a higher deployment
452+
// target. This in turn causes warnings while linking, and is generally
453+
// a compatibility hazard.
454+
//
455+
// So, at least until https://github.com/rust-lang/cc-rs/issues/1171, or
456+
// perhaps https://github.com/rust-lang/cargo/issues/13115 is resolved, we
457+
// explicitly set the deployment target environment variables to avoid
458+
// this issue.
459+
//
460+
// This place also serves as an extension point if we ever wanted to raise
461+
// rustc's default deployment target while keeping the prebuilt `std` at
462+
// a lower version, so it's kinda nice to have in any case.
463+
if target.contains("apple") && !builder.config.dry_run() {
464+
// Query rustc for the deployment target, and the associated env var.
465+
// The env var is one of the standard `*_DEPLOYMENT_TARGET` vars, i.e.
466+
// `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET`, etc.
467+
let mut cmd = command(builder.rustc(cargo.compiler()));
468+
cmd.arg("--target").arg(target.rustc_target_arg());
469+
cmd.arg("--print=deployment-target");
470+
let output = cmd.run_capture_stdout(builder).stdout();
471+
472+
let (env_var, value) = output.split_once('=').unwrap();
473+
// Unconditionally set the env var (if it was set in the environment
474+
// already, rustc should've picked that up).
475+
cargo.env(env_var.trim(), value.trim());
476+
477+
// Allow CI to override the deployment target for `std`.
478+
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
479+
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
480+
}
448481
}
449482

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

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

+22-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
///
@@ -166,4 +170,21 @@ fn main() {
166170
rustc().env_remove(env_var).run();
167171
minos("foo.o", default_version);
168172
});
173+
174+
// Test that all binaries in rlibs produced by `rustc` have the same version.
175+
// Regression test for https://github.com/rust-lang/rust/issues/128419.
176+
let sysroot = rustc().print("sysroot").run().stdout_utf8();
177+
let target_sysroot = path(sysroot.trim()).join("lib/rustlib").join(target()).join("lib");
178+
let rlibs = shallow_find_files(&target_sysroot, |path| has_extension(path, "rlib"));
179+
180+
let output = cmd("otool").arg("-l").args(rlibs).run().stdout_utf8();
181+
let re = regex::Regex::new(r"(minos|version) ([0-9.]*)").unwrap();
182+
let mut versions = HashSet::new();
183+
for (_, [_, version]) in re.captures_iter(&output).map(|c| c.extract()) {
184+
versions.insert(version);
185+
}
186+
// FIXME(madsmtm): See above for aarch64-apple-watchos.
187+
if versions.len() != 1 && target() != "aarch64-apple-watchos" {
188+
panic!("std rlibs contained multiple different deployment target versions: {versions:?}");
189+
}
169190
}

0 commit comments

Comments
 (0)