@@ -2194,21 +2194,31 @@ impl Build {
21942194
21952195 // Pass `--target` with the LLVM target to configure Clang for cross-compiling.
21962196 //
2197- // NOTE: In the past, we passed this, along with the deployment version in here
2198- // on Apple targets, but versioned targets were found to have poor compatibility
2199- // with older versions of Clang, especially around comes to configuration files.
2197+ // This is **required** for cross-compilation, as it's the only flag that
2198+ // consistently forces Clang to change the "toolchain" that is responsible for
2199+ // parsing target-specific flags:
2200+ // https://github.com/llvm/llvm-project/blob/llvmorg-19.1.7/clang/lib/Driver/Driver.cpp#L6347-L6532
22002201 //
2201- // Instead, we specify `-arch` along with `-mmacosx-version-min=`, `-mtargetos=`
2202- // and similar flags in `.apple_flags()`.
2202+ // This can be confusing, because on e.g. host macOS, you can usually get by
2203+ // with `-arch` and `-mtargetos=`. But that only works because the _default_
2204+ // toolchain is `Darwin`, which enables parsing of darwin-specific options.
22032205 //
2204- // Note that Clang errors when both `-mtargetos=` and `-target` are specified,
2205- // so we omit this entirely on Apple targets (it's redundant when specifying
2206- // both the `-arch` and the deployment target / OS flag) (in theory we _could_
2207- // specify this on some of the Apple targets that use the older
2208- // `-m*-version-min=`, but for consistency we omit it entirely).
2209- if target. vendor != "apple" {
2210- cmd. push_cc_arg ( format ! ( "--target={}" , target. llvm_target) . into ( ) ) ;
2211- }
2206+ // NOTE: In the past, we passed the deployment version in here on all Apple
2207+ // targets, but versioned targets were found to have poor compatibility with
2208+ // older versions of Clang, especially when it comes to configuration files:
2209+ // https://github.com/rust-lang/cc-rs/issues/1278
2210+ //
2211+ // So instead, we pass the deployment target with `-m*-version-min=`, and only
2212+ // pass it here on visionOS and Mac Catalyst where that option does not exist.
2213+ let clang_target = if target. os == "visionos" || target. abi == "macabi" {
2214+ Cow :: Owned (
2215+ target. versioned_llvm_target ( & self . apple_deployment_target ( target) ) ,
2216+ )
2217+ } else {
2218+ Cow :: Borrowed ( target. llvm_target )
2219+ } ;
2220+
2221+ cmd. push_cc_arg ( format ! ( "--target={clang_target}" ) . into ( ) ) ;
22122222 }
22132223 }
22142224 ToolFamily :: Msvc { clang_cl } => {
@@ -2648,21 +2658,23 @@ impl Build {
26482658 fn apple_flags ( & self , cmd : & mut Tool ) -> Result < ( ) , Error > {
26492659 let target = self . get_target ( ) ?;
26502660
2651- // Add `-arch` on all compilers. This is a Darwin/Apple-specific flag
2652- // that works both on GCC and Clang.
2661+ // This is a Darwin/Apple-specific flag that works both on GCC and Clang, but it is only
2662+ // necessary on GCC since we specify `-target` on Clang.
26532663 // https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html#:~:text=arch
26542664 // https://clang.llvm.org/docs/CommandGuide/clang.html#cmdoption-arch
2655- let arch = map_darwin_target_from_rust_to_compiler_architecture ( & target) ;
2656- cmd. args . push ( "-arch" . into ( ) ) ;
2657- cmd. args . push ( arch. into ( ) ) ;
2665+ if cmd. is_like_gnu ( ) {
2666+ let arch = map_darwin_target_from_rust_to_compiler_architecture ( & target) ;
2667+ cmd. args . push ( "-arch" . into ( ) ) ;
2668+ cmd. args . push ( arch. into ( ) ) ;
2669+ }
26582670
2659- // Pass the deployment target via `-mmacosx-version-min=`, `-mtargetos=` and similar.
2660- //
2661- // It is also necessary on GCC, as it forces a compilation error if the compiler is not
2671+ // Pass the deployment target via `-mmacosx-version-min=`, `-miphoneos-version-min=` and
2672+ // similar. Also necessary on GCC, as it forces a compilation error if the compiler is not
26622673 // configured for Darwin: https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html
26632674 let min_version = self . apple_deployment_target ( & target) ;
2664- cmd. args
2665- . push ( target. apple_version_flag ( & min_version) . into ( ) ) ;
2675+ if let Some ( flag) = target. apple_version_flag ( & min_version) {
2676+ cmd. args . push ( flag. into ( ) ) ;
2677+ }
26662678
26672679 // AppleClang sometimes requires sysroot even on macOS
26682680 if cmd. is_xctoolchain_clang ( ) || target. os != "macos" {
0 commit comments