@@ -2923,22 +2923,47 @@ bool Darwin::isAlignedAllocationUnavailable() const {
29232923  return  TargetVersion < alignedAllocMinVersion (OS);
29242924}
29252925
2926- static  bool  sdkSupportsBuiltinModules (const  Darwin::DarwinPlatformKind &TargetPlatform, const  std::optional<DarwinSDKInfo> &SDKInfo) {
2926+ static  bool  sdkSupportsBuiltinModules (
2927+     const  Darwin::DarwinPlatformKind &TargetPlatform,
2928+     const  Darwin::DarwinEnvironmentKind &TargetEnvironment,
2929+     const  std::optional<DarwinSDKInfo> &SDKInfo) {
2930+   switch  (TargetEnvironment) {
2931+   case  Darwin::NativeEnvironment:
2932+   case  Darwin::Simulator:
2933+   case  Darwin::MacCatalyst:
2934+     //  Standard xnu/Mach/Darwin based environments
2935+     //  depend on the SDK version.
2936+     break ;
2937+   default :
2938+     //  All other environments support builtin modules from the start.
2939+     return  true ;
2940+   }
2941+ 
29272942  if  (!SDKInfo)
2943+     //  If there is no SDK info, assume this is building against a
2944+     //  pre-SDK version of macOS (i.e. before Mac OS X 10.4). Those
2945+     //  don't support modules anyway, but the headers definitely
2946+     //  don't support builtin modules either. It might also be some
2947+     //  kind of degenerate build environment, err on the side of
2948+     //  the old behavior which is to not use builtin modules.
29282949    return  false ;
29292950
29302951  VersionTuple SDKVersion = SDKInfo->getVersion ();
29312952  switch  (TargetPlatform) {
2953+   //  Existing SDKs added support for builtin modules in the fall
2954+   //  2024 major releases.
29322955  case  Darwin::MacOS:
2933-     return  SDKVersion >= VersionTuple (99U );
2956+     return  SDKVersion >= VersionTuple (15U );
29342957  case  Darwin::IPhoneOS:
2935-     return  SDKVersion >= VersionTuple (99U );
2958+     return  SDKVersion >= VersionTuple (18U );
29362959  case  Darwin::TvOS:
2937-     return  SDKVersion >= VersionTuple (99U );
2960+     return  SDKVersion >= VersionTuple (18U );
29382961  case  Darwin::WatchOS:
2939-     return  SDKVersion >= VersionTuple (99U );
2962+     return  SDKVersion >= VersionTuple (11U );
29402963  case  Darwin::XROS:
2941-     return  SDKVersion >= VersionTuple (99U );
2964+     return  SDKVersion >= VersionTuple (2U );
2965+ 
2966+   //  New SDKs support builtin modules from the start.
29422967  default :
29432968    return  true ;
29442969  }
@@ -3030,7 +3055,7 @@ void Darwin::addClangTargetOptions(
30303055  //  i.e. when the builtin stdint.h is in the Darwin module too, the cycle
30313056  //  goes away. Note that -fbuiltin-headers-in-system-modules does nothing
30323057  //  to fix the same problem with C++ headers, and is generally fragile.
3033-   if  (!sdkSupportsBuiltinModules (TargetPlatform, SDKInfo))
3058+   if  (!sdkSupportsBuiltinModules (TargetPlatform, TargetEnvironment,  SDKInfo))
30343059    CC1Args.push_back (" -fbuiltin-headers-in-system-modules"  );
30353060
30363061  if  (!DriverArgs.hasArgNoClaim (options::OPT_fdefine_target_os_macros,
0 commit comments