Skip to content

Commit 7750660

Browse files
committed
Emit diagnostics for incorrect deployment targets
1 parent 3a2f4db commit 7750660

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
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

+38-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,58 @@
11
use std::borrow::Cow;
22
use std::env;
3+
use std::fmt::{Display, from_fn};
34

45
use rustc_session::Session;
56
use rustc_target::spec::{
67
AppleOSVersion, apple_deployment_target_env_var, apple_minimum_deployment_target,
7-
apple_parse_version,
8+
apple_os_minimum_deployment_target, apple_parse_version,
89
};
910

11+
use crate::errors::AppleDeploymentTarget;
12+
1013
#[cfg(test)]
1114
mod tests;
1215

16+
pub fn pretty_version(version: AppleOSVersion) -> impl Display {
17+
let (major, minor, patch) = version;
18+
from_fn(move |f| {
19+
write!(f, "{major}.{minor}")?;
20+
if patch != 0 {
21+
write!(f, ".{patch}")?;
22+
}
23+
Ok(())
24+
})
25+
}
26+
1327
/// Get the deployment target based on the standard environment variables, or fall back to the
1428
/// minimum version supported by `rustc`.
1529
pub fn deployment_target(sess: &Session) -> AppleOSVersion {
30+
let os_min = apple_os_minimum_deployment_target(&sess.target.os);
1631
let min = apple_minimum_deployment_target(&sess.target);
32+
let env_var = apple_deployment_target_env_var(&sess.target.os);
1733

18-
if let Ok(deployment_target) = env::var(apple_deployment_target_env_var(&sess.target.os)) {
34+
if let Ok(deployment_target) = env::var(env_var) {
1935
match apple_parse_version(&deployment_target) {
20-
// It is common that the deployment target is set too low, e.g. on macOS Aarch64 to also
21-
// target older x86_64, the user may set a lower deployment target than supported.
22-
//
23-
// To avoid such issues, we silently raise the deployment target here.
24-
// FIXME: We want to show a warning when `version < os_min`.
25-
Ok(version) => version.max(min),
26-
// FIXME: Report erroneous environment variable to user.
27-
Err(_) => min,
36+
Ok(version) => {
37+
// It is common that the deployment target is set a bit too low, for example on
38+
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
39+
// is lower than the minimum OS supported by rustc, not when the variable is lower
40+
// than the minimum for a specific target.
41+
if version < os_min {
42+
sess.dcx().emit_warn(AppleDeploymentTarget::TooLow {
43+
env_var,
44+
version: pretty_version(version).to_string(),
45+
os_min: pretty_version(os_min).to_string(),
46+
});
47+
}
48+
49+
// Raise the deployment target to the minimum supported.
50+
version.max(min)
51+
}
52+
Err(error) => {
53+
sess.dcx().emit_err(AppleDeploymentTarget::Invalid { env_var, error });
54+
min
55+
}
2856
}
2957
} else {
3058
// 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)