11use std:: borrow:: Cow ;
22use std:: env;
3+ use std:: fmt:: { Display , from_fn} ;
34use std:: num:: ParseIntError ;
45
56use rustc_middle:: bug;
67use rustc_session:: Session ;
78
9+ use crate :: errors:: AppleDeploymentTarget ;
10+
811#[ cfg( test) ]
912mod tests;
1013
@@ -13,6 +16,17 @@ mod tests;
1316/// The size of the numbers in here are limited by Mach-O's `LC_BUILD_VERSION`.
1417pub type OsVersion = ( u16 , u8 , u8 ) ;
1518
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+
1630/// Parse an OS version triple (SDK version or deployment target).
1731fn parse_version ( version : & str ) -> Result < OsVersion , ParseIntError > {
1832 if let Some ( ( major, minor) ) = version. split_once ( '.' ) {
@@ -74,14 +88,26 @@ pub fn deployment_target(sess: &Session) -> OsVersion {
7488
7589 if let Ok ( deployment_target) = env:: var ( env_var) {
7690 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+ }
85111 }
86112 } else {
87113 // If no deployment target variable is set, default to the minimum found above.
0 commit comments