Skip to content

Commit 39b5847

Browse files
committed
refactor: Move env parsing of deployment target to rustc_session
1 parent b536256 commit 39b5847

File tree

11 files changed

+73
-78
lines changed

11 files changed

+73
-78
lines changed

compiler/rustc_codegen_ssa/messages.ftl

-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ codegen_ssa_add_native_library = failed to add native library {$library_path}: {
44
55
codegen_ssa_aix_strip_not_used = using host's `strip` binary to cross-compile to AIX which is not guaranteed to work
66
7-
codegen_ssa_apple_deployment_target_invalid =
8-
failed to parse deployment target specified in {$env_var}: {$error}
9-
10-
codegen_ssa_apple_deployment_target_too_low =
11-
deployment target in {$env_var} was set to {$version}, but the minimum supported by `rustc` is {$os_min}
12-
137
codegen_ssa_apple_sdk_error_sdk_path = failed to get {$sdk_name} SDK path: {$error}
148
159
codegen_ssa_archive_build_failure = failed to build archive at `{$path}`: {$error}

compiler/rustc_codegen_ssa/src/back/apple.rs

-54
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
use std::env;
2-
use std::str::FromStr;
3-
41
use rustc_middle::middle::exported_symbols::SymbolExportKind;
5-
use rustc_session::Session;
62
use rustc_target::spec::Target;
73
pub(super) use rustc_target::spec::apple::OSVersion;
84

9-
use crate::errors::AppleDeploymentTarget;
10-
115
#[cfg(test)]
126
mod tests;
137

@@ -110,54 +104,6 @@ pub(super) fn add_data_and_relocation(
110104
Ok(())
111105
}
112106

113-
/// Name of the environment variable used to fetch the deployment target on the given OS.
114-
pub fn deployment_target_env_var(os: &str) -> &'static str {
115-
match os {
116-
"macos" => "MACOSX_DEPLOYMENT_TARGET",
117-
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",
118-
"watchos" => "WATCHOS_DEPLOYMENT_TARGET",
119-
"tvos" => "TVOS_DEPLOYMENT_TARGET",
120-
"visionos" => "XROS_DEPLOYMENT_TARGET",
121-
_ => unreachable!("tried to get deployment target env var for non-Apple platform"),
122-
}
123-
}
124-
125-
/// Get the deployment target based on the standard environment variables, or fall back to the
126-
/// minimum version supported by `rustc`.
127-
pub fn deployment_target(sess: &Session) -> OSVersion {
128-
let min = OSVersion::minimum_deployment_target(&sess.target);
129-
let env_var = deployment_target_env_var(&sess.target.os);
130-
131-
if let Ok(deployment_target) = env::var(env_var) {
132-
match OSVersion::from_str(&deployment_target) {
133-
Ok(version) => {
134-
let os_min = OSVersion::os_minimum_deployment_target(&sess.target.os);
135-
// It is common that the deployment target is set a bit too low, for example on
136-
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
137-
// is lower than the minimum OS supported by rustc, not when the variable is lower
138-
// than the minimum for a specific target.
139-
if version < os_min {
140-
sess.dcx().emit_warn(AppleDeploymentTarget::TooLow {
141-
env_var,
142-
version: version.fmt_pretty().to_string(),
143-
os_min: os_min.fmt_pretty().to_string(),
144-
});
145-
}
146-
147-
// Raise the deployment target to the minimum supported.
148-
version.max(min)
149-
}
150-
Err(error) => {
151-
sess.dcx().emit_err(AppleDeploymentTarget::Invalid { env_var, error });
152-
min
153-
}
154-
}
155-
} else {
156-
// If no deployment target variable is set, default to the minimum found above.
157-
min
158-
}
159-
}
160-
161107
pub(super) fn add_version_to_llvm_target(
162108
llvm_target: &str,
163109
deployment_target: OSVersion,

compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3115,7 +3115,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31153115
_ => bug!("invalid OS/ABI combination for Apple target: {target_os}, {target_abi}"),
31163116
};
31173117

3118-
let min_version = apple::deployment_target(sess).fmt_full().to_string();
3118+
let min_version = sess.apple_deployment_target().fmt_full().to_string();
31193119

31203120
// The SDK version is used at runtime when compiling with a newer SDK / version of Xcode:
31213121
// - By dyld to give extra warnings and errors, see e.g.:
@@ -3184,7 +3184,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31843184

31853185
// The presence of `-mmacosx-version-min` makes CC default to
31863186
// macOS, and it sets the deployment target.
3187-
let version = apple::deployment_target(sess).fmt_full();
3187+
let version = sess.apple_deployment_target().fmt_full();
31883188
// Intentionally pass this as a single argument, Clang doesn't
31893189
// seem to like it otherwise.
31903190
cmd.cc_arg(&format!("-mmacosx-version-min={version}"));

compiler/rustc_codegen_ssa/src/back/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ fn macho_object_build_version_for_target(sess: &Session) -> object::write::MachO
394394
}
395395

396396
let platform = apple::macho_platform(&sess.target);
397-
let min_os = apple::deployment_target(sess);
397+
let min_os = sess.apple_deployment_target();
398398

399399
let mut build_version = object::write::MachOBuildVersion::default();
400400
build_version.platform = platform;

compiler/rustc_codegen_ssa/src/back/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub mod write;
2020
/// Certain optimizations also depend on the deployment target.
2121
pub fn versioned_llvm_target(sess: &Session) -> Cow<'_, str> {
2222
if sess.target.is_like_osx {
23-
apple::add_version_to_llvm_target(&sess.target.llvm_target, apple::deployment_target(sess))
23+
apple::add_version_to_llvm_target(&sess.target.llvm_target, sess.apple_deployment_target())
2424
.into()
2525
} else {
2626
// FIXME(madsmtm): Certain other targets also include a version,

compiler/rustc_codegen_ssa/src/errors.rs

-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::borrow::Cow;
44
use std::ffi::OsString;
55
use std::io::Error;
6-
use std::num::ParseIntError;
76
use std::path::{Path, PathBuf};
87
use std::process::ExitStatus;
98

@@ -745,14 +744,6 @@ pub(crate) struct UnsupportedArch<'a> {
745744
pub os: &'a str,
746745
}
747746

748-
#[derive(Diagnostic)]
749-
pub(crate) enum AppleDeploymentTarget {
750-
#[diag(codegen_ssa_apple_deployment_target_invalid)]
751-
Invalid { env_var: &'static str, error: ParseIntError },
752-
#[diag(codegen_ssa_apple_deployment_target_too_low)]
753-
TooLow { env_var: &'static str, version: String, os_min: String },
754-
}
755-
756747
#[derive(Diagnostic)]
757748
pub(crate) enum AppleSdkRootError<'a> {
758749
#[diag(codegen_ssa_apple_sdk_error_sdk_path)]

compiler/rustc_driver_impl/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use std::time::{Instant, SystemTime};
3434
use std::{env, str};
3535

3636
use rustc_ast as ast;
37-
use rustc_codegen_ssa::back::apple;
3837
use rustc_codegen_ssa::traits::CodegenBackend;
3938
use rustc_codegen_ssa::{CodegenErrors, CodegenResults};
4039
use rustc_data_structures::profiling::{
@@ -782,8 +781,8 @@ fn print_crate_info(
782781
if sess.target.is_like_osx {
783782
println_info!(
784783
"{}={}",
785-
apple::deployment_target_env_var(&sess.target.os),
786-
apple::deployment_target(sess).fmt_pretty(),
784+
rustc_target::spec::apple::deployment_target_env_var(&sess.target.os),
785+
sess.apple_deployment_target().fmt_pretty(),
787786
)
788787
} else {
789788
#[allow(rustc::diagnostic_outside_of_impl)]

compiler/rustc_session/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
session_apple_deployment_target_invalid =
2+
failed to parse deployment target specified in {$env_var}: {$error}
3+
4+
session_apple_deployment_target_too_low =
5+
deployment target in {$env_var} was set to {$version}, but the minimum supported by `rustc` is {$os_min}
6+
17
session_binary_float_literal_not_supported = binary float literal is not supported
28
session_branch_protection_requires_aarch64 = `-Zbranch-protection` is only supported on aarch64
39

compiler/rustc_session/src/errors.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::num::NonZero;
1+
use std::num::{NonZero, ParseIntError};
22

33
use rustc_ast::token;
44
use rustc_ast::util::literal::LitError;
@@ -14,6 +14,14 @@ use rustc_target::spec::{SplitDebuginfo, StackProtector, TargetTuple};
1414
use crate::config::CrateType;
1515
use crate::parse::ParseSess;
1616

17+
#[derive(Diagnostic)]
18+
pub(crate) enum AppleDeploymentTarget {
19+
#[diag(session_apple_deployment_target_invalid)]
20+
Invalid { env_var: &'static str, error: ParseIntError },
21+
#[diag(session_apple_deployment_target_too_low)]
22+
TooLow { env_var: &'static str, version: String, os_min: String },
23+
}
24+
1725
pub(crate) struct FeatureGateError {
1826
pub(crate) span: MultiSpan,
1927
pub(crate) explain: DiagMessage,

compiler/rustc_session/src/session.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_target::asm::InlineAsmArch;
2929
use rustc_target::spec::{
3030
CodeModel, DebuginfoKind, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
3131
SmallDataThresholdSupport, SplitDebuginfo, StackProtector, SymbolVisibility, Target,
32-
TargetTuple, TlsModel,
32+
TargetTuple, TlsModel, apple,
3333
};
3434

3535
use crate::code_stats::CodeStats;
@@ -883,6 +883,45 @@ impl Session {
883883
FileNameDisplayPreference::Local
884884
}
885885
}
886+
887+
/// Get the deployment target on Apple platforms based on the standard environment variables,
888+
/// or fall back to the minimum version supported by `rustc`.
889+
///
890+
/// This should be guarded behind `if sess.target.is_like_osx`.
891+
pub fn apple_deployment_target(&self) -> apple::OSVersion {
892+
let min = apple::OSVersion::minimum_deployment_target(&self.target);
893+
let env_var = apple::deployment_target_env_var(&self.target.os);
894+
895+
// FIXME(madsmtm): Track changes to this.
896+
if let Ok(deployment_target) = env::var(env_var) {
897+
match apple::OSVersion::from_str(&deployment_target) {
898+
Ok(version) => {
899+
let os_min = apple::OSVersion::os_minimum_deployment_target(&self.target.os);
900+
// It is common that the deployment target is set a bit too low, for example on
901+
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
902+
// is lower than the minimum OS supported by rustc, not when the variable is lower
903+
// than the minimum for a specific target.
904+
if version < os_min {
905+
self.dcx().emit_warn(errors::AppleDeploymentTarget::TooLow {
906+
env_var,
907+
version: version.fmt_pretty().to_string(),
908+
os_min: os_min.fmt_pretty().to_string(),
909+
});
910+
}
911+
912+
// Raise the deployment target to the minimum supported.
913+
version.max(min)
914+
}
915+
Err(error) => {
916+
self.dcx().emit_err(errors::AppleDeploymentTarget::Invalid { env_var, error });
917+
min
918+
}
919+
}
920+
} else {
921+
// If no deployment target variable is set, default to the minimum found above.
922+
min
923+
}
924+
}
886925
}
887926

888927
// JUSTIFICATION: part of session construction

compiler/rustc_target/src/spec/base/apple/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,15 @@ impl OSVersion {
317317
Self { major, minor, patch }
318318
}
319319
}
320+
321+
/// Name of the environment variable used to fetch the deployment target on the given OS.
322+
pub fn deployment_target_env_var(os: &str) -> &'static str {
323+
match os {
324+
"macos" => "MACOSX_DEPLOYMENT_TARGET",
325+
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",
326+
"watchos" => "WATCHOS_DEPLOYMENT_TARGET",
327+
"tvos" => "TVOS_DEPLOYMENT_TARGET",
328+
"visionos" => "XROS_DEPLOYMENT_TARGET",
329+
_ => unreachable!("tried to get deployment target env var for non-Apple platform"),
330+
}
331+
}

0 commit comments

Comments
 (0)