diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2023-05-08 19:41:48 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-08 19:41:48 +0900 |
| commit | e3eb6a87bf044925c9faf72a2b9737b0c7e2c76b (patch) | |
| tree | 5740ffac225a4d4dbbf28508759c2204428bc32c | |
| parent | c86e7fb60f5343041fd0c27d4affaf3261115666 (diff) | |
| parent | a427d418fd0004f12a7d59899e6994238daed9aa (diff) | |
| download | rust-e3eb6a87bf044925c9faf72a2b9737b0c7e2c76b.tar.gz rust-e3eb6a87bf044925c9faf72a2b9737b0c7e2c76b.zip | |
Rollup merge of #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 #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
| -rw-r--r-- | compiler/rustc_driver_impl/src/lib.rs | 16 | ||||
| -rw-r--r-- | compiler/rustc_session/src/config.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_target/src/spec/apple_base.rs | 42 | ||||
| -rw-r--r-- | compiler/rustc_target/src/spec/mod.rs | 1 | ||||
| -rw-r--r-- | src/doc/rustc/src/command-line-arguments.md | 6 | ||||
| -rw-r--r-- | src/tools/tidy/src/ui_tests.rs | 2 | ||||
| -rw-r--r-- | tests/run-make/valid-print-requests/valid-print-requests.stderr | 2 | ||||
| -rw-r--r-- | tests/ui/deployment-target/invalid-target.rs | 4 | ||||
| -rw-r--r-- | tests/ui/deployment-target/invalid-target.stderr | 2 | ||||
| -rw-r--r-- | tests/ui/deployment-target/macos-target.rs | 7 | ||||
| -rw-r--r-- | tests/ui/deployment-target/macos-target.stdout | 1 |
11 files changed, 73 insertions, 14 deletions
diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 405f3d5b66d..446e29199c6 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -745,6 +745,22 @@ fn print_crate_info( } } } + DeploymentTarget => { + use rustc_target::spec::current_apple_deployment_target; + + if sess.target.is_like_osx { + safe_println!( + "deployment_target={}", + current_apple_deployment_target(&sess.target) + .expect("unknown Apple target OS") + ) + } else { + early_error( + ErrorOutputType::default(), + "only Apple targets currently support deployment version info", + ) + } + } } } Compilation::Stop diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index d80cc0aa043..aa3cb03bad8 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -599,6 +599,7 @@ pub enum PrintRequest { StackProtectorStrategies, LinkArgs, SplitDebuginfo, + DeploymentTarget, } #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -1481,7 +1482,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> { "[crate-name|file-names|sysroot|target-libdir|cfg|calling-conventions|\ target-list|target-cpus|target-features|relocation-models|code-models|\ tls-models|target-spec-json|all-target-specs-json|native-static-libs|\ - stack-protector-strategies|link-args]", + stack-protector-strategies|link-args|deployment-target]", ), opt::flagmulti_s("g", "", "Equivalent to -C debuginfo=2"), opt::flagmulti_s("O", "", "Equivalent to -C opt-level=2"), @@ -1931,6 +1932,7 @@ fn collect_print_requests( ("all-target-specs-json", PrintRequest::AllTargetSpecs), ("link-args", PrintRequest::LinkArgs), ("split-debuginfo", PrintRequest::SplitDebuginfo), + ("deployment-target", PrintRequest::DeploymentTarget), ]; prints.extend(matches.opt_strs("print").into_iter().map(|req| { diff --git a/compiler/rustc_target/src/spec/apple_base.rs b/compiler/rustc_target/src/spec/apple_base.rs index 4c7f64b2078..ff224631828 100644 --- a/compiler/rustc_target/src/spec/apple_base.rs +++ b/compiler/rustc_target/src/spec/apple_base.rs @@ -1,7 +1,7 @@ use std::{borrow::Cow, env}; use crate::spec::{cvs, Cc, DebuginfoKind, FramePointer, LinkArgs}; -use crate::spec::{LinkerFlavor, Lld, SplitDebuginfo, StaticCow, TargetOptions}; +use crate::spec::{LinkerFlavor, Lld, SplitDebuginfo, StaticCow, Target, TargetOptions}; #[cfg(test)] #[path = "apple/tests.rs"] @@ -179,12 +179,28 @@ pub fn opts(os: &'static str, arch: Arch) -> TargetOptions { } } -fn deployment_target(var_name: &str) -> Option<(u32, u32)> { - let deployment_target = env::var(var_name).ok(); - deployment_target - .as_ref() - .and_then(|s| s.split_once('.')) - .and_then(|(a, b)| a.parse::<u32>().and_then(|a| b.parse::<u32>().map(|b| (a, b))).ok()) +pub fn deployment_target(target: &Target) -> Option<String> { + let (major, minor) = match &*target.os { + "macos" => { + // This does not need to be specific. It just needs to handle x86 vs M1. + let arch = if target.arch == "x86" || target.arch == "x86_64" { X86_64 } else { Arm64 }; + macos_deployment_target(arch) + } + "ios" => ios_deployment_target(), + "watchos" => watchos_deployment_target(), + "tvos" => tvos_deployment_target(), + _ => return None, + }; + + Some(format!("{major}.{minor}")) +} + +fn from_set_deployment_target(var_name: &str) -> Option<(u32, u32)> { + let deployment_target = env::var(var_name).ok()?; + let (unparsed_major, unparsed_minor) = deployment_target.split_once('.')?; + let (major, minor) = (unparsed_major.parse().ok()?, unparsed_minor.parse().ok()?); + + Some((major, minor)) } fn macos_default_deployment_target(arch: Arch) -> (u32, u32) { @@ -198,7 +214,8 @@ fn macos_default_deployment_target(arch: Arch) -> (u32, u32) { } fn macos_deployment_target(arch: Arch) -> (u32, u32) { - deployment_target("MACOSX_DEPLOYMENT_TARGET") + // If you are looking for the default deployment target, prefer `rustc --print deployment-target`. + from_set_deployment_target("MACOSX_DEPLOYMENT_TARGET") .unwrap_or_else(|| macos_default_deployment_target(arch)) } @@ -247,7 +264,8 @@ fn link_env_remove(arch: Arch, os: &'static str) -> StaticCow<[StaticCow<str>]> } fn ios_deployment_target() -> (u32, u32) { - deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((7, 0)) + // If you are looking for the default deployment target, prefer `rustc --print deployment-target`. + from_set_deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((7, 0)) } pub fn ios_llvm_target(arch: Arch) -> String { @@ -272,7 +290,8 @@ pub fn ios_sim_llvm_target(arch: Arch) -> String { } fn tvos_deployment_target() -> (u32, u32) { - deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((7, 0)) + // If you are looking for the default deployment target, prefer `rustc --print deployment-target`. + from_set_deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((7, 0)) } fn tvos_lld_platform_version() -> String { @@ -281,7 +300,8 @@ fn tvos_lld_platform_version() -> String { } fn watchos_deployment_target() -> (u32, u32) { - deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0)) + // If you are looking for the default deployment target, prefer `rustc --print deployment-target`. + from_set_deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0)) } fn watchos_lld_platform_version() -> String { diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 10d38c34919..ba4b89c9ea1 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -60,6 +60,7 @@ pub mod crt_objects; mod aix_base; mod android_base; mod apple_base; +pub use apple_base::deployment_target as current_apple_deployment_target; mod avr_gnu_base; mod bpf_base; mod dragonfly_base; diff --git a/src/doc/rustc/src/command-line-arguments.md b/src/doc/rustc/src/command-line-arguments.md index d4d26654ed1..3be4382b0a3 100644 --- a/src/doc/rustc/src/command-line-arguments.md +++ b/src/doc/rustc/src/command-line-arguments.md @@ -248,8 +248,14 @@ The valid types of print values are: exact format of this debugging output is not a stable guarantee, other than that it will include the linker executable and the text of each command-line argument passed to the linker. +- `deployment-target` - The currently selected [deployment target] (or minimum OS version) + for the selected Apple platform target. This value can be used or passed along to other + components alongside a Rust build that need this information, such as C compilers. + This returns rustc's minimum supported deployment target if no `*_DEPLOYMENT_TARGET` variable + is present in the environment, or otherwise returns the variable's parsed value. [conditional compilation]: ../reference/conditional-compilation.html +[deployment target]: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html <a id="option-g-debug"></a> ## `-g`: include debug information diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index a2f7b8ba7be..e4f328ec0dd 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -10,7 +10,7 @@ use std::path::{Path, PathBuf}; const ENTRY_LIMIT: usize = 900; // FIXME: The following limits should be reduced eventually. const ISSUES_ENTRY_LIMIT: usize = 1953; -const ROOT_ENTRY_LIMIT: usize = 894; +const ROOT_ENTRY_LIMIT: usize = 895; fn check_entries(tests_path: &Path, bad: &mut bool) { let mut directories: HashMap<PathBuf, usize> = HashMap::new(); diff --git a/tests/run-make/valid-print-requests/valid-print-requests.stderr b/tests/run-make/valid-print-requests/valid-print-requests.stderr index bea6ce067f6..4f57550c29a 100644 --- a/tests/run-make/valid-print-requests/valid-print-requests.stderr +++ b/tests/run-make/valid-print-requests/valid-print-requests.stderr @@ -1,2 +1,2 @@ -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` +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` diff --git a/tests/ui/deployment-target/invalid-target.rs b/tests/ui/deployment-target/invalid-target.rs new file mode 100644 index 00000000000..336624320a3 --- /dev/null +++ b/tests/ui/deployment-target/invalid-target.rs @@ -0,0 +1,4 @@ +// compile-flags: --target x86_64-unknown-linux-gnu --print deployment-target +// needs-llvm-components: x86 + +fn main() {} diff --git a/tests/ui/deployment-target/invalid-target.stderr b/tests/ui/deployment-target/invalid-target.stderr new file mode 100644 index 00000000000..eb4ac131c40 --- /dev/null +++ b/tests/ui/deployment-target/invalid-target.stderr @@ -0,0 +1,2 @@ +error: only Apple targets currently support deployment version info + diff --git a/tests/ui/deployment-target/macos-target.rs b/tests/ui/deployment-target/macos-target.rs new file mode 100644 index 00000000000..701ccf4799a --- /dev/null +++ b/tests/ui/deployment-target/macos-target.rs @@ -0,0 +1,7 @@ +// only-macos +// compile-flags: --print deployment-target +// normalize-stdout-test: "\d+\." -> "$$CURRENT_MAJOR_VERSION." +// normalize-stdout-test: "\d+" -> "$$CURRENT_MINOR_VERSION" +// check-pass + +fn main() {} diff --git a/tests/ui/deployment-target/macos-target.stdout b/tests/ui/deployment-target/macos-target.stdout new file mode 100644 index 00000000000..f55ef568ed6 --- /dev/null +++ b/tests/ui/deployment-target/macos-target.stdout @@ -0,0 +1 @@ +deployment_target=$CURRENT_MAJOR_VERSION.$CURRENT_MINOR_VERSION |
