1
Fork 0

refactor: Move env parsing of deployment target to rustc_session

This commit is contained in:
Mads Marquart 2025-02-11 11:02:36 +01:00
parent d74ce25b65
commit 7e4379c4eb
11 changed files with 74 additions and 75 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;
@ -891,6 +891,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