https://github.com/ian-twilightcoder created https://github.com/llvm/llvm-project/pull/134005
Whether the SDK supports builtin modules is a property of the SDK itself, and really has nothing to do with the target. This was already worked around for Mac Catalyst, but there are some other more esoteric non-obvious target-to-sdk mappings that aren't handled. Have the SDK parse its OS out of CanonicalName and use that instead of the target to determine if builtin modules are supported. >From dc28888199388197063189d76ded13cd48fcd238 Mon Sep 17 00:00:00 2001 From: Ian Anderson <i...@apple.com> Date: Tue, 1 Apr 2025 16:24:30 -0700 Subject: [PATCH] [clang][modules] Determine if the SDK supports builtin modules independent of the target Whether the SDK supports builtin modules is a property of the SDK itself, and really has nothing to do with the target. This was already worked around for Mac Catalyst, but there are some other more esoteric non-obvious target-to-sdk mappings that aren't handled. Have the SDK parse its OS out of CanonicalName and use that instead of the target to determine if builtin modules are supported. --- clang/include/clang/Basic/DarwinSDKInfo.h | 6 +- clang/lib/Basic/DarwinSDKInfo.cpp | 26 +++++++- clang/lib/Driver/ToolChains/Darwin.cpp | 60 +++++++++---------- .../Inputs/DriverKit23.0.sdk/SDKSettings.json | 2 +- .../Inputs/MacOSX10.14.sdk/SDKSettings.json | 2 +- .../SDKSettings.json | 1 + .../Inputs/MacOSX15.0.sdk/SDKSettings.json | 2 +- .../Inputs/MacOSX15.1.sdk/SDKSettings.json | 2 +- .../Inputs/WatchOS6.0.sdk/SDKSettings.json | 2 +- .../Inputs/iPhoneOS13.0.sdk/SDKSettings.json | 2 +- .../darwin-ld-platform-version-watchos.c | 4 +- .../Inputs/MacOSX13.0.sdk/SDKSettings.json | 1 + .../Inputs/AppleTVOS15.0.sdk/SDKSettings.json | 1 + .../Inputs/MacOSX11.0.sdk/SDKSettings.json | 1 + .../Inputs/WatchOS7.0.sdk/SDKSettings.json | 1 + 15 files changed, 72 insertions(+), 41 deletions(-) diff --git a/clang/include/clang/Basic/DarwinSDKInfo.h b/clang/include/clang/Basic/DarwinSDKInfo.h index db20b968a898e..bc122c7d21c72 100644 --- a/clang/include/clang/Basic/DarwinSDKInfo.h +++ b/clang/include/clang/Basic/DarwinSDKInfo.h @@ -143,16 +143,19 @@ class DarwinSDKInfo { DarwinSDKInfo( VersionTuple Version, VersionTuple MaximumDeploymentTarget, + llvm::Triple::OSType OS, llvm::DenseMap<OSEnvPair::StorageType, std::optional<RelatedTargetVersionMapping>> VersionMappings = llvm::DenseMap<OSEnvPair::StorageType, std::optional<RelatedTargetVersionMapping>>()) : Version(Version), MaximumDeploymentTarget(MaximumDeploymentTarget), - VersionMappings(std::move(VersionMappings)) {} + OS(OS), VersionMappings(std::move(VersionMappings)) {} const llvm::VersionTuple &getVersion() const { return Version; } + const llvm::Triple::OSType &getOS() const { return OS; } + // Returns the optional, target-specific version mapping that maps from one // target to another target. // @@ -177,6 +180,7 @@ class DarwinSDKInfo { private: VersionTuple Version; VersionTuple MaximumDeploymentTarget; + llvm::Triple::OSType OS; // Need to wrap the value in an optional here as the value has to be default // constructible, and std::unique_ptr doesn't like DarwinSDKInfo being // Optional as Optional is trying to copy it in emplace. diff --git a/clang/lib/Basic/DarwinSDKInfo.cpp b/clang/lib/Basic/DarwinSDKInfo.cpp index 00aa5f9e63cd3..eaf600d8ffed3 100644 --- a/clang/lib/Basic/DarwinSDKInfo.cpp +++ b/clang/lib/Basic/DarwinSDKInfo.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/DarwinSDKInfo.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/JSON.h" #include "llvm/Support/MemoryBuffer.h" @@ -62,6 +63,28 @@ DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON( Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping)); } +static llvm::Triple::OSType parseOS(const llvm::json::Object &Obj) { + // The CanonicalName is the Xcode platform, a version, and an optional suffix, + // e.g. macosx16.0. + auto CanonicalName = Obj.getString("CanonicalName"); + if (!CanonicalName) + return llvm::Triple::UnknownOS; + size_t VersionStart = CanonicalName->find_first_of("0123456789"); + StringRef XcodePlatform = CanonicalName->slice(0, VersionStart); + return llvm::StringSwitch<llvm::Triple::OSType>(XcodePlatform) + .Case("macosx", llvm::Triple::MacOSX) + .Case("iphoneos", llvm::Triple::IOS) + .Case("iphonesimulator", llvm::Triple::IOS) + .Case("appletvos", llvm::Triple::TvOS) + .Case("appletvsimulator", llvm::Triple::TvOS) + .Case("watchos", llvm::Triple::WatchOS) + .Case("watchsimulator", llvm::Triple::WatchOS) + .Case("xros", llvm::Triple::XROS) + .Case("xrsimulator", llvm::Triple::XROS) + .Case("driverkit", llvm::Triple::DriverKit) + .Default(llvm::Triple::UnknownOS); +} + static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj, StringRef Key) { auto Value = Obj.getString(Key); @@ -82,6 +105,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) { getVersionKey(*Obj, "MaximumDeploymentTarget"); if (!MaximumDeploymentVersion) return std::nullopt; + llvm::Triple::OSType OS = parseOS(*Obj); llvm::DenseMap<OSEnvPair::StorageType, std::optional<RelatedTargetVersionMapping>> VersionMappings; @@ -124,7 +148,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) { } return DarwinSDKInfo(std::move(*Version), - std::move(*MaximumDeploymentVersion), + std::move(*MaximumDeploymentVersion), OS, std::move(VersionMappings)); } diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp index 32a5fe68e8cff..de3e00b52050e 100644 --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -1886,7 +1886,8 @@ struct DarwinPlatform { assert(IsValid && "invalid SDK version"); return DarwinSDKInfo( Version, - /*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99)); + /*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99), + getOSFromPlatform(Platform)); } private: @@ -1916,6 +1917,23 @@ struct DarwinPlatform { } } + static llvm::Triple::OSType getOSFromPlatform(DarwinPlatformKind Platform) { + switch (Platform) { + case DarwinPlatformKind::MacOS: + return llvm::Triple::MacOSX; + case DarwinPlatformKind::IPhoneOS: + return llvm::Triple::IOS; + case DarwinPlatformKind::TvOS: + return llvm::Triple::TvOS; + case DarwinPlatformKind::WatchOS: + return llvm::Triple::WatchOS; + case DarwinPlatformKind::DriverKit: + return llvm::Triple::DriverKit; + case DarwinPlatformKind::XROS: + return llvm::Triple::XROS; + } + } + SourceKind Kind; DarwinPlatformKind Platform; DarwinEnvironmentKind Environment = DarwinEnvironmentKind::NativeEnvironment; @@ -2966,20 +2984,8 @@ bool Darwin::isAlignedAllocationUnavailable() const { return TargetVersion < alignedAllocMinVersion(OS); } -static bool sdkSupportsBuiltinModules( - const Darwin::DarwinPlatformKind &TargetPlatform, - const Darwin::DarwinEnvironmentKind &TargetEnvironment, - const std::optional<DarwinSDKInfo> &SDKInfo) { - if (TargetEnvironment == Darwin::NativeEnvironment || - TargetEnvironment == Darwin::Simulator || - TargetEnvironment == Darwin::MacCatalyst) { - // Standard xnu/Mach/Darwin based environments - // depend on the SDK version. - } else { - // All other environments support builtin modules from the start. - return true; - } - +static bool +sdkSupportsBuiltinModules(const std::optional<DarwinSDKInfo> &SDKInfo) { if (!SDKInfo) // If there is no SDK info, assume this is building against a // pre-SDK version of macOS (i.e. before Mac OS X 10.4). Those @@ -2990,26 +2996,18 @@ static bool sdkSupportsBuiltinModules( return false; VersionTuple SDKVersion = SDKInfo->getVersion(); - switch (TargetPlatform) { + switch (SDKInfo->getOS()) { // Existing SDKs added support for builtin modules in the fall // 2024 major releases. - case Darwin::MacOS: + case llvm::Triple::MacOSX: return SDKVersion >= VersionTuple(15U); - case Darwin::IPhoneOS: - switch (TargetEnvironment) { - case Darwin::MacCatalyst: - // Mac Catalyst uses `-target arm64-apple-ios18.0-macabi` so the platform - // is iOS, but it builds with the macOS SDK, so it's the macOS SDK version - // that's relevant. - return SDKVersion >= VersionTuple(15U); - default: - return SDKVersion >= VersionTuple(18U); - } - case Darwin::TvOS: + case llvm::Triple::IOS: return SDKVersion >= VersionTuple(18U); - case Darwin::WatchOS: + case llvm::Triple::TvOS: + return SDKVersion >= VersionTuple(18U); + case llvm::Triple::WatchOS: return SDKVersion >= VersionTuple(11U); - case Darwin::XROS: + case llvm::Triple::XROS: return SDKVersion >= VersionTuple(2U); // New SDKs support builtin modules from the start. @@ -3138,7 +3136,7 @@ void Darwin::addClangTargetOptions( // i.e. when the builtin stdint.h is in the Darwin module too, the cycle // goes away. Note that -fbuiltin-headers-in-system-modules does nothing // to fix the same problem with C++ headers, and is generally fragile. - if (!sdkSupportsBuiltinModules(TargetPlatform, TargetEnvironment, SDKInfo)) + if (!sdkSupportsBuiltinModules(SDKInfo)) CC1Args.push_back("-fbuiltin-headers-in-system-modules"); if (!DriverArgs.hasArgNoClaim(options::OPT_fdefine_target_os_macros, diff --git a/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json b/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json index 7ba6c244df211..edee441adb474 100644 --- a/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json +++ b/clang/test/Driver/Inputs/DriverKit23.0.sdk/SDKSettings.json @@ -1 +1 @@ -{"Version":"23.0", "MaximumDeploymentTarget": "23.0.99"} +{"Version":"23.0", "CanonicalName": "driverkit23.0", "MaximumDeploymentTarget": "23.0.99"} diff --git a/clang/test/Driver/Inputs/MacOSX10.14.sdk/SDKSettings.json b/clang/test/Driver/Inputs/MacOSX10.14.sdk/SDKSettings.json index b612107cef394..e7383550e42bd 100644 --- a/clang/test/Driver/Inputs/MacOSX10.14.sdk/SDKSettings.json +++ b/clang/test/Driver/Inputs/MacOSX10.14.sdk/SDKSettings.json @@ -1 +1 @@ -{"Version":"10.14", "MaximumDeploymentTarget": "10.14.99"} +{"Version":"10.14", "CanonicalName": "macosx10.14", "MaximumDeploymentTarget": "10.14.99"} diff --git a/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json b/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json index b0769e9f86045..7325cc45a2808 100644 --- a/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json +++ b/clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json @@ -1,5 +1,6 @@ { "Version":"10.15", + "CanonicalName": "macosx10.15", "MaximumDeploymentTarget": "10.15.99", "VersionMap" : { "macOS_iOSMac" : { diff --git a/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json b/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json index ced45d5c21996..81d5ee28a5a05 100644 --- a/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json +++ b/clang/test/Driver/Inputs/MacOSX15.0.sdk/SDKSettings.json @@ -1 +1 @@ -{"Version":"15.0", "MaximumDeploymentTarget": "15.0.99"} +{"Version":"15.0", "CanonicalName": "macosx15.0", "MaximumDeploymentTarget": "15.0.99"} diff --git a/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json b/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json index d46295b2ab5a1..956cbe4041b9a 100644 --- a/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json +++ b/clang/test/Driver/Inputs/MacOSX15.1.sdk/SDKSettings.json @@ -1 +1 @@ -{"Version":"15.1", "MaximumDeploymentTarget": "15.1.99"} +{"Version":"15.1", "CanonicalName": "macosx15.1", "MaximumDeploymentTarget": "15.1.99"} diff --git a/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json b/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json index 9e30a153cb5fb..314fc22edf7eb 100644 --- a/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json +++ b/clang/test/Driver/Inputs/WatchOS6.0.sdk/SDKSettings.json @@ -1 +1 @@ -{"Version":"6.0.0", "MaximumDeploymentTarget": "6.0.99"} +{"Version":"6.0", "CanonicalName": "watchos6.0", "MaximumDeploymentTarget": "6.0.99"} diff --git a/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json b/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json index b05260f994868..c122cf47ce0f7 100644 --- a/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json +++ b/clang/test/Driver/Inputs/iPhoneOS13.0.sdk/SDKSettings.json @@ -1 +1 @@ -{"Version":"13.0", "MaximumDeploymentTarget": "13.0.99"} +{"Version":"13.0", "CanonicalName": "iphoneos13.0", "MaximumDeploymentTarget": "13.0.99"} diff --git a/clang/test/Driver/darwin-ld-platform-version-watchos.c b/clang/test/Driver/darwin-ld-platform-version-watchos.c index 3d405a7c09f62..fc8e859f30efa 100644 --- a/clang/test/Driver/darwin-ld-platform-version-watchos.c +++ b/clang/test/Driver/darwin-ld-platform-version-watchos.c @@ -18,5 +18,5 @@ // RUN: | FileCheck --check-prefix=SIMUL %s // LINKER-OLD: "-watchos_version_min" "5.2.0" -// LINKER-NEW: "-platform_version" "watchos" "5.2.0" "6.0.0" -// SIMUL: "-platform_version" "watchos-simulator" "6.0.0" "6.0.0" +// LINKER-NEW: "-platform_version" "watchos" "5.2.0" "6.0" +// SIMUL: "-platform_version" "watchos-simulator" "6.0.0" "6.0" diff --git a/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json b/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json index 258d8288fc6b4..d08288ed1af10 100644 --- a/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json +++ b/clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json @@ -1,6 +1,7 @@ { "DefaultVariant": "macos", "DisplayName": "macOS 13", "Version": "13.0", + "CanonicalName": "macosx13.0", "MaximumDeploymentTarget": "13.0.99", "PropertyConditionFallbackNames": [], "VersionMap": { "iOSMac_macOS": { diff --git a/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json b/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json index dc10836f4887e..6cd02f33471ed 100644 --- a/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json +++ b/clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json @@ -1,6 +1,7 @@ { "DisplayName": "tvOS 15.0", "Version": "15.0", + "CanonicalName": "appletvos15.0", "MaximumDeploymentTarget": "15.0.99", "PropertyConditionFallbackNames": [], "VersionMap": { diff --git a/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json b/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json index b40e35e882e60..e6220abfbe573 100644 --- a/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json +++ b/clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json @@ -1,6 +1,7 @@ { "DefaultVariant": "macos", "DisplayName": "macOS 11", "Version": "11.0", + "CanonicalName": "macosx11.0", "MaximumDeploymentTarget": "11.0.99", "PropertyConditionFallbackNames": [], "VersionMap": { "iOSMac_macOS": { diff --git a/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json b/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json index fff3e3a80286e..84914c105749a 100644 --- a/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json +++ b/clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json @@ -1,6 +1,7 @@ { "DisplayName": "watchOS 7.0", "Version": "7.0", + "CanonicalName": "watchos7.0", "MaximumDeploymentTarget": "7.0.99", "PropertyConditionFallbackNames": [], "VersionMap": { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits