Skip to content
/ rust Public
forked from rust-lang/rust

Commit 9c5b8ea

Browse files
authored
Rollup merge of rust-lang#133041 - madsmtm:print-deployment-target-env-var, r=davidtwco
Print name of env var in `--print=deployment-target` The deployment target environment variable is OS-specific, and if you're in a place where you're asking `rustc` for the deployment target, you're likely to also wanna know the name of the environment variable. I myself wanted this for some code I'm working on in bootstrap, for example. Behaviour before this PR: ```console $ rustc --print=deployment-target --target=aarch64-apple-darwin deployment_target=11.0 $ rustc --print=deployment-target --target=aarch64-apple-visionos deployment_target=1.0 ``` Behaviour after this PR: ```console $ rustc --print=deployment-target --target=aarch64-apple-darwin MACOSX_DEPLOYMENT_TARGET=11.0 $ rustc --print=deployment-target --target=aarch64-apple-visionos XROS_DEPLOYMENT_TARGET=1.0 ``` My _belief_ is that this option is extremely rarely used in general, and a GitHub search for "rustc print deployment-target" seems to confirm this, it revealed only the following actual pieces of code using this: - https://github.com/PyO3/maturin/blob/b292ef69349f2a56cb8ab1b59fda0be3d3b9f138/src/build_context.rs#L1199-L1220 - https://github.com/rust-lang/cc-rs/blob/daab9244b03e244c4f2511944870d719c443f61f/src/lib.rs#L3422-L3426 `maturin` does `.split('=').last()`, so it will continue to work after this change, but `cc v1.0.84` did `.strip_prefix("deployment_target=")` since [this PR](rust-lang/cc-rs#848), so it would break. That's _probably_ fine though, it was broken in a lot of scenarios anyway, and [got](rust-lang/cc-rs#901) [reverted](rust-lang/cc-rs#943) in `v1.0.85`. So while this is _technically_ a breaking change, I really doubt that anyone is going to observe it, so it's probably fine. `@BlackHoleFox` wdyt? `@rustbot` label O-apple r? compiler
2 parents 41cbe3e + 431c500 commit 9c5b8ea

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

compiler/rustc_codegen_ssa/src/back/apple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn minimum_deployment_target(target: &Target) -> OSVersion {
9797
}
9898

9999
/// Name of the environment variable used to fetch the deployment target on the given OS.
100-
fn deployment_target_env_var(os: &str) -> &'static str {
100+
pub fn deployment_target_env_var(os: &str) -> &'static str {
101101
match os {
102102
"macos" => "MACOSX_DEPLOYMENT_TARGET",
103103
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",

compiler/rustc_driver_impl/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,9 @@ fn print_crate_info(
867867
DeploymentTarget => {
868868
if sess.target.is_like_osx {
869869
println_info!(
870-
"deployment_target={}",
871-
apple::pretty_version(apple::deployment_target(sess))
870+
"{}={}",
871+
apple::deployment_target_env_var(&sess.target.os),
872+
apple::pretty_version(apple::deployment_target(sess)),
872873
)
873874
} else {
874875
#[allow(rustc::diagnostic_outside_of_impl)]

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

+22-12
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,31 @@ fn minos(file: &str, version: &str) {
2424

2525
fn main() {
2626
// These versions should generally be higher than the default versions
27-
let (env_var, example_version, higher_example_version) = match apple_os() {
28-
"macos" => ("MACOSX_DEPLOYMENT_TARGET", "12.0", "13.0"),
27+
let (example_version, higher_example_version) = match apple_os() {
28+
"macos" => ("12.0", "13.0"),
2929
// armv7s-apple-ios and i386-apple-ios only supports iOS 10.0
30-
"ios" if target() == "armv7s-apple-ios" || target() == "i386-apple-ios" => {
31-
("IPHONEOS_DEPLOYMENT_TARGET", "10.0", "10.0")
32-
}
33-
"ios" => ("IPHONEOS_DEPLOYMENT_TARGET", "15.0", "16.0"),
34-
"watchos" => ("WATCHOS_DEPLOYMENT_TARGET", "7.0", "9.0"),
35-
"tvos" => ("TVOS_DEPLOYMENT_TARGET", "14.0", "15.0"),
36-
"visionos" => ("XROS_DEPLOYMENT_TARGET", "1.1", "1.2"),
30+
"ios" if target() == "armv7s-apple-ios" || target() == "i386-apple-ios" => ("10.0", "10.0"),
31+
"ios" => ("15.0", "16.0"),
32+
"watchos" => ("7.0", "9.0"),
33+
"tvos" => ("14.0", "15.0"),
34+
"visionos" => ("1.1", "1.2"),
3735
_ => unreachable!(),
3836
};
39-
let default_version =
40-
rustc().target(target()).env_remove(env_var).print("deployment-target").run().stdout_utf8();
41-
let default_version = default_version.strip_prefix("deployment_target=").unwrap().trim();
37+
38+
// Remove env vars to get `rustc`'s default
39+
let output = rustc()
40+
.target(target())
41+
.env_remove("MACOSX_DEPLOYMENT_TARGET")
42+
.env_remove("IPHONEOS_DEPLOYMENT_TARGET")
43+
.env_remove("WATCHOS_DEPLOYMENT_TARGET")
44+
.env_remove("TVOS_DEPLOYMENT_TARGET")
45+
.env_remove("XROS_DEPLOYMENT_TARGET")
46+
.print("deployment-target")
47+
.run()
48+
.stdout_utf8();
49+
let (env_var, default_version) = output.split_once('=').unwrap();
50+
let env_var = env_var.trim();
51+
let default_version = default_version.trim();
4252

4353
// Test that version makes it to the object file.
4454
run_in_tmpdir(|| {

tests/run-make/apple-sdk-version/rmake.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ fn main() {
2626
// Fetch rustc's inferred deployment target.
2727
let current_deployment_target =
2828
rustc().target(target()).print("deployment-target").run().stdout_utf8();
29-
let current_deployment_target =
30-
current_deployment_target.strip_prefix("deployment_target=").unwrap().trim();
29+
let current_deployment_target = current_deployment_target.split('=').last().unwrap().trim();
3130

3231
// Fetch current SDK version via. xcrun.
3332
//

tests/ui/print-request/macos-target.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ only-apple
22
//@ compile-flags: --print deployment-target
3+
//@ normalize-stdout-test: "\w*_DEPLOYMENT_TARGET" -> "$$OS_DEPLOYMENT_TARGET"
34
//@ normalize-stdout-test: "\d+\." -> "$$CURRENT_MAJOR_VERSION."
45
//@ normalize-stdout-test: "\d+" -> "$$CURRENT_MINOR_VERSION"
56
//@ check-pass
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
deployment_target=$CURRENT_MAJOR_VERSION.$CURRENT_MINOR_VERSION
1+
$OS_DEPLOYMENT_TARGET=$CURRENT_MAJOR_VERSION.$CURRENT_MINOR_VERSION

0 commit comments

Comments
 (0)