Auto merge of #138947 - madsmtm:refactor-apple-versions, r=Noratrieb

Refactor Apple version handling in the compiler

Move various Apple version handling code in the compiler out `rustc_codegen_ssa` and into a place where it can be accessed by `rustc_attr_parsing`, which I found to be necessary when doing https://github.com/rust-lang/rust/pull/136867. Thought I'd split it out to make it easier to land, and to make further changes like https://github.com/rust-lang/rust/pull/131477 have fewer conflicts / PR dependencies.

There should be no functional changes in this PR.

`@rustbot` label O-apple
r? rust-lang/compiler
This commit is contained in:
bors 2025-04-06 10:16:28 +00:00
commit f5c510260b
17 changed files with 190 additions and 166 deletions

View file

@ -1,3 +1,9 @@
session_apple_deployment_target_invalid =
failed to parse deployment target specified in {$env_var}: {$error}
session_apple_deployment_target_too_low =
deployment target in {$env_var} was set to {$version}, but the minimum supported by `rustc` is {$os_min}
session_binary_float_literal_not_supported = binary float literal is not supported
session_branch_protection_requires_aarch64 = `-Zbranch-protection` is only supported on aarch64

View file

@ -1,4 +1,4 @@
use std::num::NonZero;
use std::num::{NonZero, ParseIntError};
use rustc_ast::token;
use rustc_ast::util::literal::LitError;
@ -14,6 +14,14 @@ use rustc_target::spec::{SplitDebuginfo, StackProtector, TargetTuple};
use crate::config::CrateType;
use crate::parse::ParseSess;
#[derive(Diagnostic)]
pub(crate) enum AppleDeploymentTarget {
#[diag(session_apple_deployment_target_invalid)]
Invalid { env_var: &'static str, error: ParseIntError },
#[diag(session_apple_deployment_target_too_low)]
TooLow { env_var: &'static str, version: String, os_min: String },
}
pub(crate) struct FeatureGateError {
pub(crate) span: MultiSpan,
pub(crate) explain: DiagMessage,

View file

@ -29,7 +29,7 @@ use rustc_target::asm::InlineAsmArch;
use rustc_target::spec::{
CodeModel, DebuginfoKind, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
SmallDataThresholdSupport, SplitDebuginfo, StackProtector, SymbolVisibility, Target,
TargetTuple, TlsModel,
TargetTuple, TlsModel, apple,
};
use crate::code_stats::CodeStats;
@ -895,6 +895,45 @@ impl Session {
FileNameDisplayPreference::Local
}
}
/// Get the deployment target on Apple platforms based on the standard environment variables,
/// or fall back to the minimum version supported by `rustc`.
///
/// This should be guarded behind `if sess.target.is_like_darwin`.
pub fn apple_deployment_target(&self) -> apple::OSVersion {
let min = apple::OSVersion::minimum_deployment_target(&self.target);
let env_var = apple::deployment_target_env_var(&self.target.os);
// FIXME(madsmtm): Track changes to this.
if let Ok(deployment_target) = env::var(env_var) {
match apple::OSVersion::from_str(&deployment_target) {
Ok(version) => {
let os_min = apple::OSVersion::os_minimum_deployment_target(&self.target.os);
// It is common that the deployment target is set a bit too low, for example on
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
// is lower than the minimum OS supported by rustc, not when the variable is lower
// than the minimum for a specific target.
if version < os_min {
self.dcx().emit_warn(errors::AppleDeploymentTarget::TooLow {
env_var,
version: version.fmt_pretty().to_string(),
os_min: os_min.fmt_pretty().to_string(),
});
}
// Raise the deployment target to the minimum supported.
version.max(min)
}
Err(error) => {
self.dcx().emit_err(errors::AppleDeploymentTarget::Invalid { env_var, error });
min
}
}
} else {
// If no deployment target variable is set, default to the minimum found above.
min
}
}
}
// JUSTIFICATION: part of session construction