1
1
use std:: borrow:: Cow ;
2
2
use std:: env;
3
+ use std:: fmt:: { Display , from_fn} ;
3
4
use std:: num:: ParseIntError ;
4
5
5
6
use rustc_middle:: bug;
6
7
use rustc_session:: Session ;
7
8
9
+ use crate :: errors:: AppleDeploymentTarget ;
10
+
8
11
#[ cfg( test) ]
9
12
mod tests;
10
13
@@ -13,6 +16,17 @@ mod tests;
13
16
/// The size of the numbers in here are limited by Mach-O's `LC_BUILD_VERSION`.
14
17
pub type OsVersion = ( u16 , u8 , u8 ) ;
15
18
19
+ pub fn pretty_version ( version : OsVersion ) -> impl Display {
20
+ let ( major, minor, patch) = version;
21
+ from_fn ( move |f| {
22
+ write ! ( f, "{major}.{minor}" ) ?;
23
+ if patch != 0 {
24
+ write ! ( f, ".{patch}" ) ?;
25
+ }
26
+ Ok ( ( ) )
27
+ } )
28
+ }
29
+
16
30
/// Parse an OS version triple (SDK version or deployment target).
17
31
fn parse_version ( version : & str ) -> Result < OsVersion , ParseIntError > {
18
32
if let Some ( ( major, minor) ) = version. split_once ( '.' ) {
@@ -74,14 +88,26 @@ pub fn deployment_target(sess: &Session) -> OsVersion {
74
88
75
89
if let Ok ( deployment_target) = env:: var ( env_var) {
76
90
match parse_version ( & deployment_target) {
77
- // It is common that the deployment target is set too low, e.g. on macOS Aarch64 to also
78
- // target older x86_64, the user may set a lower deployment target than supported.
79
- //
80
- // To avoid such issues, we silently raise the deployment target here.
81
- // FIXME: We want to show a warning when `version < os_min`.
82
- Ok ( version) => version. max ( min) ,
83
- // FIXME: Report erroneous environment variable to user.
84
- Err ( _) => min,
91
+ Ok ( version) => {
92
+ // It is common that the deployment target is set a bit too low, for example on
93
+ // macOS Aarch64 to also target older x86_64. So we only want to warn when variable
94
+ // is lower than the minimum OS supported by rustc, not when the variable is lower
95
+ // than the minimum for a specific target.
96
+ if version < os_min {
97
+ sess. dcx ( ) . emit_warn ( AppleDeploymentTarget :: TooLow {
98
+ env_var,
99
+ version : pretty_version ( version) . to_string ( ) ,
100
+ os_min : pretty_version ( os_min) . to_string ( ) ,
101
+ } ) ;
102
+ }
103
+
104
+ // Raise the deployment target to the minimum supported.
105
+ version. max ( min)
106
+ }
107
+ Err ( error) => {
108
+ sess. dcx ( ) . emit_err ( AppleDeploymentTarget :: Invalid { env_var, error } ) ;
109
+ min
110
+ }
85
111
}
86
112
} else {
87
113
// If no deployment target variable is set, default to the minimum found above.
0 commit comments