Skip to content

Commit 7157ef7

Browse files
authored
Rollup merge of rust-lang#105354 - BlackHoleFox:apple-deployment-printer, r=oli-obk
Add deployment-target --print flag for Apple targets This is very useful for crates that need to know what the Apple OS deployment target is for their build scripts or inside of a build environment. Right now, the defaults just get copy/pasted around the ecosystem since they've been stable for so long. But with rust-lang#104385 in progress, that won't be true anymore and everything will need to move. Ideally whenever it happens again, this could be less painful as everything can ask the compiler what its default is instead. To show examples of the copy/paste proliferation, here's some crates and/or apps that do: - [cc](https://github.com/rust-lang/cc-rs/pull/708/files), Soon - [mac-notification-sys](https://github.com/h4llow3En/mac-notification-sys/pull/46/files#diff-d0d98998092552a1d3259338c2c71e118a5b8343dd4703c0c7f552ada7f9cb42R10-R12) - [PyO3](https://github.com/PyO3/maturin/blob/ccb02d1aa1cc41e82a3572a3c8b35cace15f3e78/src/target.rs#L755-L758) - [Anki](https://github.com/ankitects/anki/blob/613b5c1034cc9943f3f68d818ae22b2e0acec877/build/runner/src/bundle/artifacts.rs#L49-L54) - [jsc-rs](https://github.com/Brooooooklyn/jsc-rs/blob/37767267568fb2de62fc441473e7d158dd980520/xtask/src/build.rs#L402-L405) ... and probably more that a simple GitHub codesearch didn't see
2 parents 6f8c055 + 1e45f08 commit 7157ef7

File tree

11 files changed

+72
-14
lines changed

11 files changed

+72
-14
lines changed

compiler/rustc_driver_impl/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,22 @@ fn print_crate_info(
745745
}
746746
}
747747
}
748+
DeploymentTarget => {
749+
use rustc_target::spec::current_apple_deployment_target;
750+
751+
if sess.target.is_like_osx {
752+
safe_println!(
753+
"deployment_target={}",
754+
current_apple_deployment_target(&sess.target)
755+
.expect("unknown Apple target OS")
756+
)
757+
} else {
758+
early_error(
759+
ErrorOutputType::default(),
760+
"only Apple targets currently support deployment version info",
761+
)
762+
}
763+
}
748764
}
749765
}
750766
Compilation::Stop

compiler/rustc_session/src/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ pub enum PrintRequest {
587587
StackProtectorStrategies,
588588
LinkArgs,
589589
SplitDebuginfo,
590+
DeploymentTarget,
590591
}
591592

592593
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
@@ -1452,7 +1453,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
14521453
"[crate-name|file-names|sysroot|target-libdir|cfg|calling-conventions|\
14531454
target-list|target-cpus|target-features|relocation-models|code-models|\
14541455
tls-models|target-spec-json|all-target-specs-json|native-static-libs|\
1455-
stack-protector-strategies|link-args]",
1456+
stack-protector-strategies|link-args|deployment-target]",
14561457
),
14571458
opt::flagmulti_s("g", "", "Equivalent to -C debuginfo=2"),
14581459
opt::flagmulti_s("O", "", "Equivalent to -C opt-level=2"),
@@ -1902,6 +1903,7 @@ fn collect_print_requests(
19021903
("all-target-specs-json", PrintRequest::AllTargetSpecs),
19031904
("link-args", PrintRequest::LinkArgs),
19041905
("split-debuginfo", PrintRequest::SplitDebuginfo),
1906+
("deployment-target", PrintRequest::DeploymentTarget),
19051907
];
19061908

19071909
prints.extend(matches.opt_strs("print").into_iter().map(|req| {

compiler/rustc_target/src/spec/apple_base.rs

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::Cow, env};
22

33
use crate::spec::{cvs, Cc, DebuginfoKind, FramePointer, LinkArgs};
4-
use crate::spec::{LinkerFlavor, Lld, SplitDebuginfo, StaticCow, TargetOptions};
4+
use crate::spec::{LinkerFlavor, Lld, SplitDebuginfo, StaticCow, Target, TargetOptions};
55

66
#[cfg(test)]
77
#[path = "apple/tests.rs"]
@@ -179,12 +179,28 @@ pub fn opts(os: &'static str, arch: Arch) -> TargetOptions {
179179
}
180180
}
181181

182-
fn deployment_target(var_name: &str) -> Option<(u32, u32)> {
183-
let deployment_target = env::var(var_name).ok();
184-
deployment_target
185-
.as_ref()
186-
.and_then(|s| s.split_once('.'))
187-
.and_then(|(a, b)| a.parse::<u32>().and_then(|a| b.parse::<u32>().map(|b| (a, b))).ok())
182+
pub fn deployment_target(target: &Target) -> Option<String> {
183+
let (major, minor) = match &*target.os {
184+
"macos" => {
185+
// This does not need to be specific. It just needs to handle x86 vs M1.
186+
let arch = if target.arch == "x86" || target.arch == "x86_64" { X86_64 } else { Arm64 };
187+
macos_deployment_target(arch)
188+
}
189+
"ios" => ios_deployment_target(),
190+
"watchos" => watchos_deployment_target(),
191+
"tvos" => tvos_deployment_target(),
192+
_ => return None,
193+
};
194+
195+
Some(format!("{major}.{minor}"))
196+
}
197+
198+
fn from_set_deployment_target(var_name: &str) -> Option<(u32, u32)> {
199+
let deployment_target = env::var(var_name).ok()?;
200+
let (unparsed_major, unparsed_minor) = deployment_target.split_once('.')?;
201+
let (major, minor) = (unparsed_major.parse().ok()?, unparsed_minor.parse().ok()?);
202+
203+
Some((major, minor))
188204
}
189205

190206
fn macos_default_deployment_target(arch: Arch) -> (u32, u32) {
@@ -198,7 +214,8 @@ fn macos_default_deployment_target(arch: Arch) -> (u32, u32) {
198214
}
199215

200216
fn macos_deployment_target(arch: Arch) -> (u32, u32) {
201-
deployment_target("MACOSX_DEPLOYMENT_TARGET")
217+
// If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
218+
from_set_deployment_target("MACOSX_DEPLOYMENT_TARGET")
202219
.unwrap_or_else(|| macos_default_deployment_target(arch))
203220
}
204221

@@ -247,7 +264,8 @@ fn link_env_remove(arch: Arch, os: &'static str) -> StaticCow<[StaticCow<str>]>
247264
}
248265

249266
fn ios_deployment_target() -> (u32, u32) {
250-
deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((7, 0))
267+
// If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
268+
from_set_deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((7, 0))
251269
}
252270

253271
pub fn ios_llvm_target(arch: Arch) -> String {
@@ -272,7 +290,8 @@ pub fn ios_sim_llvm_target(arch: Arch) -> String {
272290
}
273291

274292
fn tvos_deployment_target() -> (u32, u32) {
275-
deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((7, 0))
293+
// If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
294+
from_set_deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((7, 0))
276295
}
277296

278297
fn tvos_lld_platform_version() -> String {
@@ -281,7 +300,8 @@ fn tvos_lld_platform_version() -> String {
281300
}
282301

283302
fn watchos_deployment_target() -> (u32, u32) {
284-
deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0))
303+
// If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
304+
from_set_deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0))
285305
}
286306

287307
fn watchos_lld_platform_version() -> String {

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
mod aix_base;
6161
mod android_base;
6262
mod apple_base;
63+
pub use apple_base::deployment_target as current_apple_deployment_target;
6364
mod avr_gnu_base;
6465
mod bpf_base;
6566
mod dragonfly_base;

src/doc/rustc/src/command-line-arguments.md

+6
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,14 @@ The valid types of print values are:
248248
exact format of this debugging output is not a stable guarantee, other than
249249
that it will include the linker executable and the text of each command-line
250250
argument passed to the linker.
251+
- `deployment-target` - The currently selected [deployment target] (or minimum OS version)
252+
for the selected Apple platform target. This value can be used or passed along to other
253+
components alongside a Rust build that need this information, such as C compilers.
254+
This returns rustc's minimum supported deployment target if no `*_DEPLOYMENT_TARGET` variable
255+
is present in the environment, or otherwise returns the variable's parsed value.
251256

252257
[conditional compilation]: ../reference/conditional-compilation.html
258+
[deployment target]: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html
253259

254260
<a id="option-g-debug"></a>
255261
## `-g`: include debug information

src/tools/tidy/src/ui_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::path::{Path, PathBuf};
1010
const ENTRY_LIMIT: usize = 900;
1111
// FIXME: The following limits should be reduced eventually.
1212
const ISSUES_ENTRY_LIMIT: usize = 1953;
13-
const ROOT_ENTRY_LIMIT: usize = 894;
13+
const ROOT_ENTRY_LIMIT: usize = 895;
1414

1515
fn check_entries(tests_path: &Path, bad: &mut bool) {
1616
let mut directories: HashMap<PathBuf, usize> = HashMap::new();
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: unknown print request `uwu`. Valid print requests are: `crate-name`, `file-names`, `sysroot`, `target-libdir`, `cfg`, `calling-conventions`, `target-list`, `target-cpus`, `target-features`, `relocation-models`, `code-models`, `tls-models`, `native-static-libs`, `stack-protector-strategies`, `target-spec-json`, `all-target-specs-json`, `link-args`, `split-debuginfo`
1+
error: unknown print request `uwu`. Valid print requests are: `crate-name`, `file-names`, `sysroot`, `target-libdir`, `cfg`, `calling-conventions`, `target-list`, `target-cpus`, `target-features`, `relocation-models`, `code-models`, `tls-models`, `native-static-libs`, `stack-protector-strategies`, `target-spec-json`, `all-target-specs-json`, `link-args`, `split-debuginfo`, `deployment-target`
22

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: --target x86_64-unknown-linux-gnu --print deployment-target
2+
// needs-llvm-components: x86
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: only Apple targets currently support deployment version info
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// only-macos
2+
// compile-flags: --print deployment-target
3+
// normalize-stdout-test: "\d+\.0" -> "$$CURRENT_MAJOR_VERSION.0"
4+
// check-pass
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deployment_target=$CURRENT_MAJOR_VERSION.0

0 commit comments

Comments
 (0)