Skip to content

Commit dc8b9f9

Browse files
committed
Apple: Rebuild when deployment target changes
Rebuild when the `*_DEPLOYMENT_TARGET` variables change when building Apple targets. This is done by: 1. Adding it as a tracked variable to `Options` to make sure it clears the incremental cache. 2. Emitting the variable in `--emit=dep-info` (`.d`) files, so that Cargo can pick up changes to it, and correctly trigger a rebuild.
1 parent 7fc9179 commit dc8b9f9

File tree

12 files changed

+196
-48
lines changed

12 files changed

+196
-48
lines changed

compiler/rustc_interface/src/passes.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ fn escape_dep_filename(filename: &str) -> String {
391391

392392
// Makefile comments only need escaping newlines and `\`.
393393
// The result can be unescaped by anything that can unescape `escape_default` and friends.
394-
fn escape_dep_env(symbol: Symbol) -> String {
395-
let s = symbol.as_str();
394+
fn escape_dep_env(s: &str) -> String {
396395
let mut escaped = String::with_capacity(s.len());
397396
for c in s.chars() {
398397
match c {
@@ -492,16 +491,22 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
492491

493492
// Emit special comments with information about accessed environment variables.
494493
let env_depinfo = sess.psess.env_depinfo.borrow();
494+
495+
// We will soon sort, so the initial order does not matter.
496+
#[allow(rustc::potential_query_instability)]
497+
let mut env_depinfo: Vec<_> = env_depinfo
498+
.iter()
499+
.map(|(k, v)| (escape_dep_env(k.as_str()), v.map(|v| escape_dep_env(v.as_str()))))
500+
.chain(tcx.sess.target.is_like_osx.then(|| {
501+
// On Apple targets, we also depend on the deployment target environment variable.
502+
let name = rustc_target::spec::apple_deployment_target_env(&tcx.sess.target.os);
503+
(name.into(), std::env::var(name).ok().map(|var| escape_dep_env(&var)))
504+
}))
505+
.collect();
506+
env_depinfo.sort_unstable();
495507
if !env_depinfo.is_empty() {
496-
// We will soon sort, so the initial order does not matter.
497-
#[allow(rustc::potential_query_instability)]
498-
let mut envs: Vec<_> = env_depinfo
499-
.iter()
500-
.map(|(k, v)| (escape_dep_env(*k), v.map(escape_dep_env)))
501-
.collect();
502-
envs.sort_unstable();
503508
writeln!(file)?;
504-
for (k, v) in envs {
509+
for (k, v) in env_depinfo {
505510
write!(file, "# env-dep:{k}")?;
506511
if let Some(v) = v {
507512
write!(file, "={v}")?;

compiler/rustc_session/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,7 @@ impl Default for Options {
11641164
color: ColorConfig::Auto,
11651165
logical_env: FxIndexMap::default(),
11661166
verbose: false,
1167+
apple_deployment_target_env: None,
11671168
}
11681169
}
11691170
}
@@ -2710,6 +2711,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27102711
color,
27112712
logical_env,
27122713
verbose,
2714+
apple_deployment_target_env: None,
27132715
}
27142716
}
27152717

compiler/rustc_session/src/options.rs

+9
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ top_level_options!(
222222
color: ColorConfig [UNTRACKED],
223223

224224
verbose: bool [TRACKED_NO_CRATE_HASH],
225+
226+
/// The raw value of the `*_DEPLOYMENT_TARGET` environment variable
227+
/// for the selected target OS.
228+
///
229+
/// The exact environment variable to use depends on the target that
230+
/// the user has chosen, and we do not want to re-compile if an
231+
/// unrelated deployment target environment variable changed, so we
232+
/// defer the initialization of this to `build_session`.
233+
apple_deployment_target_env: Option<String> [TRACKED],
225234
}
226235
);
227236

compiler/rustc_session/src/session.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ fn default_emitter(
10061006
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
10071007
pub fn build_session(
10081008
early_dcx: EarlyDiagCtxt,
1009-
sopts: config::Options,
1009+
mut sopts: config::Options,
10101010
io: CompilerIO,
10111011
bundle: Option<Lrc<rustc_errors::FluentBundle>>,
10121012
registry: rustc_errors::registry::Registry,
@@ -1110,6 +1110,13 @@ pub fn build_session(
11101110

11111111
let asm_arch = if target.allow_asm { InlineAsmArch::from_str(&target.arch).ok() } else { None };
11121112

1113+
// Configure the deployment target for change-tracking, now that target
1114+
// details are available.
1115+
if target.is_like_osx {
1116+
let name = rustc_target::spec::apple_deployment_target_env(&target.os);
1117+
sopts.apple_deployment_target_env = std::env::var(name).ok();
1118+
}
1119+
11131120
let sess = Session {
11141121
target,
11151122
host,

compiler/rustc_target/src/spec/base/apple/mod.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ pub fn platform(target: &Target) -> Option<u32> {
274274
})
275275
}
276276

277+
/// Name of the environment variable used to fetch the deployment target on
278+
/// the given OS.
279+
pub fn deployment_target_env(os: &str) -> &'static str {
280+
match os {
281+
"macos" => "MACOSX_DEPLOYMENT_TARGET",
282+
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",
283+
"watchos" => "WATCHOS_DEPLOYMENT_TARGET",
284+
"tvos" => "TVOS_DEPLOYMENT_TARGET",
285+
"visionos" => "XROS_DEPLOYMENT_TARGET",
286+
_ => unreachable!("tried to get deployment target env var for non-Apple platform"),
287+
}
288+
}
289+
277290
/// Hack for calling `deployment_target` outside of this module.
278291
pub fn deployment_target_for_target(target: &Target) -> (u16, u8, u8) {
279292
let arch = if target.llvm_target.starts_with("arm64e") {
@@ -323,17 +336,13 @@ fn deployment_target(os: &str, arch: Arch, abi: TargetAbi) -> (u16, u8, u8) {
323336
_ => os_min,
324337
};
325338

326-
// The environment variable used to fetch the deployment target.
327-
let env_var = match os {
328-
"macos" => "MACOSX_DEPLOYMENT_TARGET",
329-
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",
330-
"watchos" => "WATCHOS_DEPLOYMENT_TARGET",
331-
"tvos" => "TVOS_DEPLOYMENT_TARGET",
332-
"visionos" => "XROS_DEPLOYMENT_TARGET",
333-
_ => unreachable!("tried to get deployment target env var for non-Apple platform"),
334-
};
335-
336-
if let Ok(deployment_target) = env::var(env_var) {
339+
// NOTE: We access the deployment target environment variable here, which
340+
// makes the variable an **implicit** input which affects compilation.
341+
//
342+
// We make sure to rebuild when the variable changes, both by busting the
343+
// incremental cache, and by telling Cargo that it is a dependency.
344+
// Search for usages of `deployment_target_env` to see how.
345+
if let Ok(deployment_target) = env::var(deployment_target_env(os)) {
337346
match parse_version(&deployment_target) {
338347
// It is common that the deployment target is set too low, e.g. on
339348
// macOS Aarch64 to also target older x86_64, the user may set a

compiler/rustc_target/src/spec/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub mod crt_objects;
6060

6161
mod base;
6262
pub use base::apple::{
63+
deployment_target_env as apple_deployment_target_env,
6364
deployment_target_for_target as current_apple_deployment_target,
6465
platform as current_apple_platform, sdk_version as current_apple_sdk_version,
6566
};

src/tools/run-make-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub use env::{env_var, env_var_os};
7070
pub use run::{cmd, run, run_fail, run_with_args};
7171

7272
/// Helpers for checking target information.
73-
pub use targets::{is_darwin, is_msvc, is_windows, llvm_components_contain, target, uname};
73+
pub use targets::{is_darwin, is_msvc, is_windows, llvm_components_contain, target, uname, apple_os};
7474

7575
/// Helpers for building names of output artifacts that are potentially target-specific.
7676
pub use artifact_names::{

src/tools/run-make-support/src/targets.rs

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ pub fn is_darwin() -> bool {
2828
target().contains("darwin")
2929
}
3030

31+
/// Get the target OS on Apple operating systems.
32+
#[must_use]
33+
pub fn apple_os() -> &'static str {
34+
if target().contains("darwin") {
35+
"macos"
36+
} else if target().contains("ios") {
37+
"ios"
38+
} else if target().contains("tvos") {
39+
"tvos"
40+
} else if target().contains("watchos") {
41+
"watchos"
42+
} else if target().contains("visionos") {
43+
"visionos"
44+
} else {
45+
panic!("not an Apple OS")
46+
}
47+
}
48+
3149
/// Check if `component` is within `LLVM_COMPONENTS`
3250
#[must_use]
3351
pub fn llvm_components_contain(component: &str) -> bool {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//! Test codegen when setting deployment targets on Apple platforms.
2+
//!
3+
//! This is important since its a compatibility hazard. The linker will
4+
//! generate load commands differently based on what minimum OS it can assume.
5+
//!
6+
//! See https://github.com/rust-lang/rust/pull/105123.
7+
8+
//@ only-apple
9+
10+
use run_make_support::{cmd, rustc, target, apple_os};
11+
12+
/// Run vtool to check the `minos` field in LC_BUILD_VERSION.
13+
///
14+
/// On lower deployment targets, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS and similar
15+
/// are used instead of LC_BUILD_VERSION - these have a `version` field, so also check that.
16+
fn minos(file: &str, version: &str) {
17+
cmd("vtool").arg("-show-build").arg(file).run().assert_stdout_contains_regex(format!("(minos|version) {version}"));
18+
}
19+
20+
fn main() {
21+
let (env_var, example_version, higher_example_version) = match apple_os() {
22+
"macos" => ("MACOSX_DEPLOYMENT_TARGET", "12.0", "13.0"),
23+
"ios" => ("IPHONEOS_DEPLOYMENT_TARGET", "14.0", "15.0"),
24+
"watchos" => ("WATCHOS_DEPLOYMENT_TARGET", "7.0", "9.0"),
25+
"tvos" => ("TVOS_DEPLOYMENT_TARGET", "14.0", "15.0"),
26+
"visionos" => ("XROS_DEPLOYMENT_TARGET", "1.1", "1.2"),
27+
_ => unreachable!(),
28+
};
29+
let default_version = rustc().target(target()).env_remove(env_var).print("deployment-target").run().stdout_utf8();
30+
let default_version = default_version.strip_prefix("deployment_target=").unwrap().trim();
31+
32+
// Ensure that the versions we test with are not the default
33+
assert_ne!(default_version, example_version);
34+
assert_ne!(default_version, higher_example_version);
35+
36+
// Test that version makes it to the object file.
37+
{
38+
let mut rustc = rustc();
39+
rustc.target(target());
40+
rustc.crate_type("lib");
41+
rustc.emit("obj");
42+
rustc.input("foo.rs");
43+
rustc.output("foo.o");
44+
45+
rustc.env(env_var, example_version).run();
46+
minos("foo.o", example_version);
47+
48+
rustc.env_remove(env_var).run();
49+
minos("foo.o", default_version);
50+
}
51+
52+
// Test that version makes it to the linker when linking dylibs.
53+
{
54+
let mut rustc = rustc();
55+
rustc.target(target());
56+
rustc.crate_type("dylib");
57+
rustc.input("foo.rs");
58+
rustc.output("libfoo.dylib");
59+
60+
rustc.env(env_var, example_version).run();
61+
minos("libfoo.dylib", example_version);
62+
63+
// FIXME(madsmtm): Deployment target is not currently passed properly to linker
64+
// rustc.env_remove(env_var).run();
65+
// minos("libfoo.dylib", default_version);
66+
67+
// Test with ld64 instead
68+
rustc.arg("-Clinker-flavor=ld");
69+
70+
rustc.env(env_var, example_version).run();
71+
minos("libfoo.dylib", example_version);
72+
73+
rustc.env_remove(env_var).run();
74+
minos("libfoo.dylib", default_version);
75+
}
76+
77+
// Test that version makes it to the linker when linking executables.
78+
{
79+
let mut rustc = rustc();
80+
rustc.target(target());
81+
rustc.crate_type("bin");
82+
rustc.input("foo.rs");
83+
rustc.output("foo");
84+
85+
rustc.env(env_var, example_version).run();
86+
minos("foo", example_version);
87+
88+
// FIXME(madsmtm): Deployment target is not currently passed properly to linker
89+
// rustc.env_remove(env_var).run();
90+
// minos("foo", default_version);
91+
92+
// Test with ld64 instead
93+
rustc.arg("-Clinker-flavor=ld");
94+
95+
rustc.env(env_var, example_version).run();
96+
minos("foo", example_version);
97+
98+
rustc.env_remove(env_var).run();
99+
minos("foo", default_version);
100+
}
101+
102+
// Test that changing the deployment target busts the incremental cache.
103+
{
104+
let mut rustc = rustc();
105+
rustc.target(target());
106+
rustc.incremental("incremental");
107+
rustc.crate_type("lib");
108+
rustc.emit("obj");
109+
rustc.input("foo.rs");
110+
rustc.output("foo.o");
111+
112+
rustc.env(env_var, example_version).run();
113+
minos("foo.o", example_version);
114+
115+
rustc.env(env_var, higher_example_version).run();
116+
minos("foo.o", higher_example_version);
117+
118+
rustc.env_remove(env_var).run();
119+
minos("foo.o", default_version);
120+
}
121+
}

tests/run-make/macos-deployment-target/Makefile

-21
This file was deleted.

tests/run-make/macos-deployment-target/with_deployment_target.rs

-4
This file was deleted.

0 commit comments

Comments
 (0)