@@ -56,6 +56,11 @@ pub fn pretty_version(version: OSVersion) -> impl Display {
5656 } )
5757}
5858
59+ fn full_version ( version : OSVersion ) -> impl Display {
60+ let ( major, minor, patch) = version;
61+ from_fn ( move |f| write ! ( f, "{major}.{minor}.{patch}" ) )
62+ }
63+
5964/// Minimum operating system versions currently supported by `rustc`.
6065fn os_minimum_deployment_target ( os : & str ) -> OSVersion {
6166 // When bumping a version in here, remember to update the platform-support docs too.
@@ -155,7 +160,7 @@ pub(super) fn add_version_to_llvm_target(
155160 let environment = components. next ( ) ;
156161 assert_eq ! ( components. next( ) , None , "too many LLVM triple components" ) ;
157162
158- let ( major , minor , patch ) = deployment_target;
163+ let version = full_version ( deployment_target) ;
159164
160165 assert ! (
161166 !os. contains( |c: char | c. is_ascii_digit( ) ) ,
@@ -164,8 +169,47 @@ pub(super) fn add_version_to_llvm_target(
164169
165170 if let Some ( env) = environment {
166171 // Insert version into OS, before environment
167- format ! ( "{arch}-{vendor}-{os}{major}.{minor}.{patch }-{env}" )
172+ format ! ( "{arch}-{vendor}-{os}{version }-{env}" )
168173 } else {
169- format ! ( "{arch}-{vendor}-{os}{major}.{minor}.{patch}" )
174+ format ! ( "{arch}-{vendor}-{os}{version}" )
175+ }
176+ }
177+
178+ /// The flag needed to specify the OS and deployment target to a C compiler.
179+ ///
180+ /// There are many aliases for these, and `-mtargetos=` is preferred on Clang
181+ /// nowadays, but for compatibility with older Clang and other tooling, we try
182+ /// use the earliest supported name here.
183+ pub ( super ) fn cc_os_version_min_flag ( os : & str , abi : & str , deployment_target : OSVersion ) -> String {
184+ let version = full_version ( deployment_target) ;
185+ // NOTE: GCC does not support `-miphoneos-version-min=` etc. (because it
186+ // does not support iOS in general), but we specify them there anyhow in
187+ // case GCC adds support for these in the future, and to force a
188+ // compilation error if GCC compiler is not configured for Darwin:
189+ // https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html
190+ //
191+ // See also:
192+ // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mmacos-version-min
193+ // https://clang.llvm.org/docs/AttributeReference.html#availability
194+ // https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html#index-mmacosx-version-min
195+ //
196+ // Note: These are intentionally passed as a single argument, Clang
197+ // doesn't seem to like it otherwise.
198+ match ( os, abi) {
199+ ( "macos" , "" ) => format ! ( "-mmacosx-version-min={version}" ) ,
200+ ( "ios" , "" ) => format ! ( "-miphoneos-version-min={version}" ) ,
201+ ( "ios" , "sim" ) => format ! ( "-mios-simulator-version-min={version}" ) ,
202+ // Mac Catalyst came after the introduction of `-mtargetos=`, so no
203+ // other flag exists for that.
204+ ( "ios" , "macabi" ) => format ! ( "-mtargetos=ios{version}-macabi" ) ,
205+ ( "tvos" , "" ) => format ! ( "-mappletvos-version-min={version}" ) ,
206+ ( "tvos" , "sim" ) => format ! ( "-mappletvsimulator-version-min={version}" ) ,
207+ ( "watchos" , "" ) => format ! ( "-mwatchos-version-min={version}" ) ,
208+ ( "watchos" , "sim" ) => format ! ( "-mwatchsimulator-version-min={version}" ) ,
209+ // `-mxros-version-min=` does not exist, use `-mtargetos=`.
210+ // https://github.com/llvm/llvm-project/issues/88271
211+ ( "visionos" , "" ) => format ! ( "-mtargetos=xros{version}" ) ,
212+ ( "visionos" , "sim" ) => format ! ( "-mtargetos=xros{version}-simulator" ) ,
213+ _ => unreachable ! ( "tried to get cc version flag for non-Apple platform" ) ,
170214 }
171215}
0 commit comments