Skip to content

Commit 1b9ec41

Browse files
committed
refactor: Move env parsing of deployment target to rustc_session
1 parent 82453a4 commit 1b9ec41

File tree

11 files changed

+74
-79
lines changed

11 files changed

+74
-79
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,12 +1,6 @@
1-
use std::env;
2-
use std::str::FromStr;
3-
4-
use rustc_session::Session;
51
use rustc_target::spec::Target;
62
pub(super) use rustc_target::spec::apple::OSVersion;
73

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

@@ -26,54 +20,6 @@ pub(super) fn macho_platform(target: &Target) -> u32 {
2620
}
2721
}
2822

29-
/// Name of the environment variable used to fetch the deployment target on the given OS.
30-
pub fn deployment_target_env_var(os: &str) -> &'static str {
31-
match os {
32-
"macos" => "MACOSX_DEPLOYMENT_TARGET",
33-
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",
34-
"watchos" => "WATCHOS_DEPLOYMENT_TARGET",
35-
"tvos" => "TVOS_DEPLOYMENT_TARGET",
36-
"visionos" => "XROS_DEPLOYMENT_TARGET",
37-
_ => unreachable!("tried to get deployment target env var for non-Apple platform"),
38-
}
39-
}
40-
41-
/// Get the deployment target based on the standard environment variables, or fall back to the
42-
/// minimum version supported by `rustc`.
43-
pub fn deployment_target(sess: &Session) -> OSVersion {
44-
let min = OSVersion::minimum_deployment_target(&sess.target);
45-
let env_var = deployment_target_env_var(&sess.target.os);
46-
47-
if let Ok(deployment_target) = env::var(env_var) {
48-
match OSVersion::from_str(&deployment_target) {
49-
Ok(version) => {
50-
let os_min = OSVersion::os_minimum_deployment_target(&sess.target.os);
51-
// It is common that the deployment target is set a bit too low, for example on
52-
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
53-
// is lower than the minimum OS supported by rustc, not when the variable is lower
54-
// than the minimum for a specific target.
55-
if version < os_min {
56-
sess.dcx().emit_warn(AppleDeploymentTarget::TooLow {
57-
env_var,
58-
version: version.fmt_pretty().to_string(),
59-
os_min: os_min.fmt_pretty().to_string(),
60-
});
61-
}
62-
63-
// Raise the deployment target to the minimum supported.
64-
version.max(min)
65-
}
66-
Err(error) => {
67-
sess.dcx().emit_err(AppleDeploymentTarget::Invalid { env_var, error });
68-
min
69-
}
70-
}
71-
} else {
72-
// If no deployment target variable is set, default to the minimum found above.
73-
min
74-
}
75-
}
76-
7723
pub(super) fn add_version_to_llvm_target(
7824
llvm_target: &str,
7925
deployment_target: OSVersion,

compiler/rustc_codegen_ssa/src/back/link.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use super::command::Command;
5353
use super::linker::{self, Linker};
5454
use super::metadata::{MetadataPosition, create_wrapper_file};
5555
use super::rpath::{self, RPathConfig};
56-
use super::{apple, versioned_llvm_target};
56+
use super::versioned_llvm_target;
5757
use crate::{
5858
CodegenResults, CompiledModule, CrateInfo, NativeLib, common, errors,
5959
looks_like_rust_object_file,
@@ -3114,7 +3114,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31143114
_ => bug!("invalid OS/ABI combination for Apple target: {target_os}, {target_abi}"),
31153115
};
31163116

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

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

31843184
// The presence of `-mmacosx-version-min` makes CC default to
31853185
// macOS, and it sets the deployment target.
3186-
let version = apple::deployment_target(sess).fmt_full();
3186+
let version = sess.apple_deployment_target().fmt_full();
31873187
// Intentionally pass this as a single argument, Clang doesn't
31883188
// seem to like it otherwise.
31893189
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
@@ -432,7 +432,7 @@ fn macho_object_build_version_for_target(sess: &Session) -> object::write::MachO
432432
}
433433

434434
let platform = apple::macho_platform(&sess.target);
435-
let min_os = apple::deployment_target(sess);
435+
let min_os = sess.apple_deployment_target();
436436

437437
let mut build_version = object::write::MachOBuildVersion::default();
438438
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

@@ -642,14 +641,6 @@ pub(crate) struct UnsupportedArch<'a> {
642641
pub os: &'a str,
643642
}
644643

645-
#[derive(Diagnostic)]
646-
pub(crate) enum AppleDeploymentTarget {
647-
#[diag(codegen_ssa_apple_deployment_target_invalid)]
648-
Invalid { env_var: &'static str, error: ParseIntError },
649-
#[diag(codegen_ssa_apple_deployment_target_too_low)]
650-
TooLow { env_var: &'static str, version: String, os_min: String },
651-
}
652-
653644
#[derive(Diagnostic)]
654645
pub(crate) enum AppleSdkRootError<'a> {
655646
#[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::{
@@ -775,8 +774,8 @@ fn print_crate_info(
775774
if sess.target.is_like_osx {
776775
println_info!(
777776
"{}={}",
778-
apple::deployment_target_env_var(&sess.target.os),
779-
apple::deployment_target(sess).fmt_pretty(),
777+
rustc_target::spec::apple::deployment_target_env_var(&sess.target.os),
778+
sess.apple_deployment_target().fmt_pretty(),
780779
)
781780
} else {
782781
#[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;
@@ -877,6 +877,45 @@ impl Session {
877877
FileNameDisplayPreference::Local
878878
}
879879
}
880+
881+
/// Get the deployment target on Apple platforms based on the standard environment variables,
882+
/// or fall back to the minimum version supported by `rustc`.
883+
///
884+
/// This should be guarded behind `if sess.target.is_like_osx`.
885+
pub fn apple_deployment_target(&self) -> apple::OSVersion {
886+
let min = apple::OSVersion::minimum_deployment_target(&self.target);
887+
let env_var = apple::deployment_target_env_var(&self.target.os);
888+
889+
// FIXME(madsmtm): Track changes to this.
890+
if let Ok(deployment_target) = env::var(env_var) {
891+
match apple::OSVersion::from_str(&deployment_target) {
892+
Ok(version) => {
893+
let os_min = apple::OSVersion::os_minimum_deployment_target(&self.target.os);
894+
// It is common that the deployment target is set a bit too low, for example on
895+
// macOS Aarch64 to also target older x86_64. So we only want to warn when variable
896+
// is lower than the minimum OS supported by rustc, not when the variable is lower
897+
// than the minimum for a specific target.
898+
if version < os_min {
899+
self.dcx().emit_warn(errors::AppleDeploymentTarget::TooLow {
900+
env_var,
901+
version: version.fmt_pretty().to_string(),
902+
os_min: os_min.fmt_pretty().to_string(),
903+
});
904+
}
905+
906+
// Raise the deployment target to the minimum supported.
907+
version.max(min)
908+
}
909+
Err(error) => {
910+
self.dcx().emit_err(errors::AppleDeploymentTarget::Invalid { env_var, error });
911+
min
912+
}
913+
}
914+
} else {
915+
// If no deployment target variable is set, default to the minimum found above.
916+
min
917+
}
918+
}
880919
}
881920

882921
// JUSTIFICATION: part of session construction

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

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

0 commit comments

Comments
 (0)