Skip to content

Commit 190c9d9

Browse files
committed
Emit diagnostics for incorrect deployment targets
1 parent 6f6292e commit 190c9d9

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

compiler/rustc_codegen_ssa/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ codegen_ssa_L4Bender_exporting_symbols_unimplemented = exporting symbols not imp
22
33
codegen_ssa_add_native_library = failed to add native library {$library_path}: {$error}
44
5+
codegen_ssa_apple_deployment_target_invalid =
6+
failed to parse deployment target specified in {$env_var}: {$error}
7+
8+
codegen_ssa_apple_deployment_target_too_low =
9+
deployment target in {$env_var} was set to {$version}, but the minimum supported by `rustc` is {$os_min}
10+
511
codegen_ssa_apple_sdk_error_sdk_path = failed to get {$sdk_name} SDK path: {$error}
612
713
codegen_ssa_archive_build_failure = failed to build archive at `{$path}`: {$error}

compiler/rustc_codegen_ssa/src/apple.rs

+34-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use std::borrow::Cow;
22
use std::env;
3+
use std::fmt::{Display, from_fn};
34
use std::num::ParseIntError;
45

56
use rustc_middle::bug;
67
use rustc_session::Session;
78

9+
use crate::errors::AppleDeploymentTarget;
10+
811
#[cfg(test)]
912
mod tests;
1013

@@ -13,6 +16,17 @@ mod tests;
1316
/// The size of the numbers in here are limited by Mach-O's `LC_BUILD_VERSION`.
1417
pub type OsVersion = (u16, u8, u8);
1518

19+
pub fn pretty_version(version: OsVersion) -> impl Display {
20+
let (major, minor, patch) = version;
21+
from_fn(move |f| {
22+
write!(f, "{major}.{minor}")?;
23+
if patch != 0 {
24+
write!(f, ".{patch}")?;
25+
}
26+
Ok(())
27+
})
28+
}
29+
1630
/// Parse an OS version triple (SDK version or deployment target).
1731
fn parse_version(version: &str) -> Result<OsVersion, ParseIntError> {
1832
if let Some((major, minor)) = version.split_once('.') {
@@ -74,14 +88,26 @@ pub fn deployment_target(sess: &Session) -> OsVersion {
7488

7589
if let Ok(deployment_target) = env::var(env_var) {
7690
match parse_version(&deployment_target) {
77-
// It is common that the deployment target is set too low, e.g. on macOS Aarch64 to also
78-
// target older x86_64, the user may set a lower deployment target than supported.
79-
//
80-
// To avoid such issues, we silently raise the deployment target here.
81-
// FIXME: We want to show a warning when `version < os_min`.
82-
Ok(version) => version.max(min),
83-
// FIXME: Report erroneous environment variable to user.
84-
Err(_) => min,
91+
Ok(version) => {
92+
// It is common that the deployment target is set a bit too low, for example on
93+
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
94+
// is lower than the minimum OS supported by rustc, not when the variable is lower
95+
// than the minimum for a specific target.
96+
if version < os_min {
97+
sess.dcx().emit_warn(AppleDeploymentTarget::TooLow {
98+
env_var,
99+
version: pretty_version(version).to_string(),
100+
os_min: pretty_version(os_min).to_string(),
101+
});
102+
}
103+
104+
// Raise the deployment target to the minimum supported.
105+
version.max(min)
106+
}
107+
Err(error) => {
108+
sess.dcx().emit_err(AppleDeploymentTarget::Invalid { env_var, error });
109+
min
110+
}
85111
}
86112
} else {
87113
// If no deployment target variable is set, default to the minimum found above.

compiler/rustc_codegen_ssa/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::borrow::Cow;
44
use std::io::Error;
5+
use std::num::ParseIntError;
56
use std::path::{Path, PathBuf};
67
use std::process::ExitStatus;
78

@@ -539,6 +540,14 @@ pub(crate) struct UnsupportedArch<'a> {
539540
pub os: &'a str,
540541
}
541542

543+
#[derive(Diagnostic)]
544+
pub(crate) enum AppleDeploymentTarget {
545+
#[diag(codegen_ssa_apple_deployment_target_invalid)]
546+
Invalid { env_var: &'static str, error: ParseIntError },
547+
#[diag(codegen_ssa_apple_deployment_target_too_low)]
548+
TooLow { env_var: &'static str, version: String, os_min: String },
549+
}
550+
542551
#[derive(Diagnostic)]
543552
pub(crate) enum AppleSdkRootError<'a> {
544553
#[diag(codegen_ssa_apple_sdk_error_sdk_path)]

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![doc(rust_logo)]
77
#![feature(assert_matches)]
88
#![feature(box_patterns)]
9+
#![feature(debug_closure_helpers)]
910
#![feature(file_buffered)]
1011
#![feature(if_let_guard)]
1112
#![feature(let_chains)]

compiler/rustc_driver_impl/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -856,10 +856,10 @@ fn print_crate_info(
856856
}
857857
}
858858
DeploymentTarget => {
859+
use rustc_codegen_ssa::apple::{deployment_target, pretty_version};
860+
859861
if sess.target.is_like_osx {
860-
let (major, minor, patch) = rustc_codegen_ssa::apple::deployment_target(sess);
861-
let patch = if patch != 0 { format!(".{patch}") } else { String::new() };
862-
println_info!("deployment_target={major}.{minor}{patch}")
862+
println_info!("deployment_target={}", pretty_version(deployment_target(sess)))
863863
} else {
864864
#[allow(rustc::diagnostic_outside_of_impl)]
865865
sess.dcx().fatal("only Apple targets currently support deployment version info")

0 commit comments

Comments
 (0)