Skip to content

Commit 7e4379c

Browse files
committed
refactor: Move env parsing of deployment target to rustc_session
1 parent d74ce25 commit 7e4379c

File tree

11 files changed

+74
-75
lines changed

11 files changed

+74
-75
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_archive_build_failure = failed to build archive at `{$path}`: {$error}
148
159
codegen_ssa_atomic_compare_exchange = Atomic compare-exchange intrinsic missing failure memory ordering

compiler/rustc_codegen_ssa/src/back/apple.rs

+1-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::env;
21
use std::ffi::OsString;
3-
use std::str::FromStr;
42
use std::path::PathBuf;
53
use std::process::Command;
64

@@ -11,7 +9,7 @@ use rustc_target::spec::Target;
119
pub(super) use rustc_target::spec::apple::OSVersion;
1210
use tracing::debug;
1311

14-
use crate::errors::{AppleDeploymentTarget, XcrunError, XcrunSdkPathWarning};
12+
use crate::errors::{XcrunError, XcrunSdkPathWarning};
1513
use crate::fluent_generated as fluent;
1614

1715
#[cfg(test)]
@@ -134,54 +132,6 @@ pub(super) fn add_data_and_relocation(
134132
Ok(())
135133
}
136134

137-
/// Name of the environment variable used to fetch the deployment target on the given OS.
138-
pub fn deployment_target_env_var(os: &str) -> &'static str {
139-
match os {
140-
"macos" => "MACOSX_DEPLOYMENT_TARGET",
141-
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",
142-
"watchos" => "WATCHOS_DEPLOYMENT_TARGET",
143-
"tvos" => "TVOS_DEPLOYMENT_TARGET",
144-
"visionos" => "XROS_DEPLOYMENT_TARGET",
145-
_ => unreachable!("tried to get deployment target env var for non-Apple platform"),
146-
}
147-
}
148-
149-
/// Get the deployment target based on the standard environment variables, or fall back to the
150-
/// minimum version supported by `rustc`.
151-
pub fn deployment_target(sess: &Session) -> OSVersion {
152-
let min = OSVersion::minimum_deployment_target(&sess.target);
153-
let env_var = deployment_target_env_var(&sess.target.os);
154-
155-
if let Ok(deployment_target) = env::var(env_var) {
156-
match OSVersion::from_str(&deployment_target) {
157-
Ok(version) => {
158-
let os_min = OSVersion::os_minimum_deployment_target(&sess.target.os);
159-
// It is common that the deployment target is set a bit too low, for example on
160-
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
161-
// is lower than the minimum OS supported by rustc, not when the variable is lower
162-
// than the minimum for a specific target.
163-
if version < os_min {
164-
sess.dcx().emit_warn(AppleDeploymentTarget::TooLow {
165-
env_var,
166-
version: version.fmt_pretty().to_string(),
167-
os_min: os_min.fmt_pretty().to_string(),
168-
});
169-
}
170-
171-
// Raise the deployment target to the minimum supported.
172-
version.max(min)
173-
}
174-
Err(error) => {
175-
sess.dcx().emit_err(AppleDeploymentTarget::Invalid { env_var, error });
176-
min
177-
}
178-
}
179-
} else {
180-
// If no deployment target variable is set, default to the minimum found above.
181-
min
182-
}
183-
}
184-
185135
pub(super) fn add_version_to_llvm_target(
186136
llvm_target: &str,
187137
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_darwin {
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

@@ -738,14 +737,6 @@ pub enum ExtractBundledLibsError<'a> {
738737
ExtractSection { rlib: &'a Path, error: Box<dyn std::error::Error> },
739738
}
740739

741-
#[derive(Diagnostic)]
742-
pub(crate) enum AppleDeploymentTarget {
743-
#[diag(codegen_ssa_apple_deployment_target_invalid)]
744-
Invalid { env_var: &'static str, error: ParseIntError },
745-
#[diag(codegen_ssa_apple_deployment_target_too_low)]
746-
TooLow { env_var: &'static str, version: String, os_min: String },
747-
}
748-
749740
#[derive(Diagnostic)]
750741
#[diag(codegen_ssa_read_file)]
751742
pub(crate) struct ReadFileError {

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::{
@@ -810,8 +809,8 @@ fn print_crate_info(
810809
if sess.target.is_like_darwin {
811810
println_info!(
812811
"{}={}",
813-
apple::deployment_target_env_var(&sess.target.os),
814-
apple::deployment_target(sess).fmt_pretty(),
812+
rustc_target::spec::apple::deployment_target_env_var(&sess.target.os),
813+
sess.apple_deployment_target().fmt_pretty(),
815814
)
816815
} else {
817816
#[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;
@@ -891,6 +891,45 @@ impl Session {
891891
FileNameDisplayPreference::Local
892892
}
893893
}
894+
895+
/// Get the deployment target on Apple platforms based on the standard environment variables,
896+
/// or fall back to the minimum version supported by `rustc`.
897+
///
898+
/// This should be guarded behind `if sess.target.is_like_darwin`.
899+
pub fn apple_deployment_target(&self) -> apple::OSVersion {
900+
let min = apple::OSVersion::minimum_deployment_target(&self.target);
901+
let env_var = apple::deployment_target_env_var(&self.target.os);
902+
903+
// FIXME(madsmtm): Track changes to this.
904+
if let Ok(deployment_target) = env::var(env_var) {
905+
match apple::OSVersion::from_str(&deployment_target) {
906+
Ok(version) => {
907+
let os_min = apple::OSVersion::os_minimum_deployment_target(&self.target.os);
908+
// It is common that the deployment target is set a bit too low, for example on
909+
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
910+
// is lower than the minimum OS supported by rustc, not when the variable is lower
911+
// than the minimum for a specific target.
912+
if version < os_min {
913+
self.dcx().emit_warn(errors::AppleDeploymentTarget::TooLow {
914+
env_var,
915+
version: version.fmt_pretty().to_string(),
916+
os_min: os_min.fmt_pretty().to_string(),
917+
});
918+
}
919+
920+
// Raise the deployment target to the minimum supported.
921+
version.max(min)
922+
}
923+
Err(error) => {
924+
self.dcx().emit_err(errors::AppleDeploymentTarget::Invalid { env_var, error });
925+
min
926+
}
927+
}
928+
} else {
929+
// If no deployment target variable is set, default to the minimum found above.
930+
min
931+
}
932+
}
894933
}
895934

896935
// 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)