Skip to content

Commit 1892fe5

Browse files
[swift/release/6.2][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. rdar://147975407
1 parent 886d8cc commit 1892fe5

File tree

17 files changed

+74
-41
lines changed

17 files changed

+74
-41
lines changed

Diff for: clang/include/clang/Basic/DarwinSDKInfo.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,19 @@ class DarwinSDKInfo {
143143

144144
DarwinSDKInfo(
145145
VersionTuple Version, VersionTuple MaximumDeploymentTarget,
146+
llvm::Triple::OSType OS,
146147
llvm::DenseMap<OSEnvPair::StorageType,
147148
std::optional<RelatedTargetVersionMapping>>
148149
VersionMappings =
149150
llvm::DenseMap<OSEnvPair::StorageType,
150151
std::optional<RelatedTargetVersionMapping>>())
151152
: Version(Version), MaximumDeploymentTarget(MaximumDeploymentTarget),
152-
VersionMappings(std::move(VersionMappings)) {}
153+
OS(OS), VersionMappings(std::move(VersionMappings)) {}
153154

154155
const llvm::VersionTuple &getVersion() const { return Version; }
155156

157+
const llvm::Triple::OSType &getOS() const { return OS; }
158+
156159
// Returns the optional, target-specific version mapping that maps from one
157160
// target to another target.
158161
//
@@ -177,6 +180,7 @@ class DarwinSDKInfo {
177180
private:
178181
VersionTuple Version;
179182
VersionTuple MaximumDeploymentTarget;
183+
llvm::Triple::OSType OS;
180184
// Need to wrap the value in an optional here as the value has to be default
181185
// constructible, and std::unique_ptr doesn't like DarwinSDKInfo being
182186
// Optional as Optional is trying to copy it in emplace.

Diff for: clang/lib/Basic/DarwinSDKInfo.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Basic/DarwinSDKInfo.h"
10+
#include "llvm/ADT/StringSwitch.h"
1011
#include "llvm/Support/ErrorOr.h"
1112
#include "llvm/Support/JSON.h"
1213
#include "llvm/Support/MemoryBuffer.h"
@@ -62,6 +63,28 @@ DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(
6263
Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
6364
}
6465

66+
static llvm::Triple::OSType parseOS(const llvm::json::Object &Obj) {
67+
// The CanonicalName is the Xcode platform followed by a version, e.g.
68+
// macosx16.0.
69+
auto CanonicalName = Obj.getString("CanonicalName");
70+
if (!CanonicalName)
71+
return llvm::Triple::UnknownOS;
72+
size_t VersionStart = CanonicalName->find_first_of("0123456789");
73+
StringRef XcodePlatform = CanonicalName->slice(0, VersionStart);
74+
return llvm::StringSwitch<llvm::Triple::OSType>(XcodePlatform)
75+
.Case("macosx", llvm::Triple::MacOSX)
76+
.Case("iphoneos", llvm::Triple::IOS)
77+
.Case("iphonesimulator", llvm::Triple::IOS)
78+
.Case("appletvos", llvm::Triple::TvOS)
79+
.Case("appletvsimulator", llvm::Triple::TvOS)
80+
.Case("watchos", llvm::Triple::WatchOS)
81+
.Case("watchsimulator", llvm::Triple::WatchOS)
82+
.Case("xros", llvm::Triple::XROS)
83+
.Case("xrsimulator", llvm::Triple::XROS)
84+
.Case("driverkit", llvm::Triple::DriverKit)
85+
.Default(llvm::Triple::UnknownOS);
86+
}
87+
6588
static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
6689
StringRef Key) {
6790
auto Value = Obj.getString(Key);
@@ -82,6 +105,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) {
82105
getVersionKey(*Obj, "MaximumDeploymentTarget");
83106
if (!MaximumDeploymentVersion)
84107
return std::nullopt;
108+
llvm::Triple::OSType OS = parseOS(*Obj);
85109
llvm::DenseMap<OSEnvPair::StorageType,
86110
std::optional<RelatedTargetVersionMapping>>
87111
VersionMappings;
@@ -124,7 +148,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) {
124148
}
125149

126150
return DarwinSDKInfo(std::move(*Version),
127-
std::move(*MaximumDeploymentVersion),
151+
std::move(*MaximumDeploymentVersion), OS,
128152
std::move(VersionMappings));
129153
}
130154

Diff for: clang/lib/Driver/ToolChains/Darwin.cpp

+29-31
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,8 @@ struct DarwinPlatform {
19601960
assert(IsValid && "invalid SDK version");
19611961
return DarwinSDKInfo(
19621962
Version,
1963-
/*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99));
1963+
/*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99),
1964+
getOSFromPlatform(Platform));
19641965
}
19651966

19661967
private:
@@ -1990,6 +1991,23 @@ struct DarwinPlatform {
19901991
}
19911992
}
19921993

1994+
static llvm::Triple::OSType getOSFromPlatform(DarwinPlatformKind Platform) {
1995+
switch (Platform) {
1996+
case DarwinPlatformKind::MacOS:
1997+
return llvm::Triple::MacOSX;
1998+
case DarwinPlatformKind::IPhoneOS:
1999+
return llvm::Triple::IOS;
2000+
case DarwinPlatformKind::TvOS:
2001+
return llvm::Triple::TvOS;
2002+
case DarwinPlatformKind::WatchOS:
2003+
return llvm::Triple::WatchOS;
2004+
case DarwinPlatformKind::DriverKit:
2005+
return llvm::Triple::DriverKit;
2006+
case DarwinPlatformKind::XROS:
2007+
return llvm::Triple::XROS;
2008+
}
2009+
}
2010+
19932011
SourceKind Kind;
19942012
DarwinPlatformKind Platform;
19952013
DarwinEnvironmentKind Environment = DarwinEnvironmentKind::NativeEnvironment;
@@ -3063,20 +3081,8 @@ bool Darwin::isAlignedAllocationUnavailable() const {
30633081
return TargetVersion < alignedAllocMinVersion(OS);
30643082
}
30653083

3066-
static bool sdkSupportsBuiltinModules(
3067-
const Darwin::DarwinPlatformKind &TargetPlatform,
3068-
const Darwin::DarwinEnvironmentKind &TargetEnvironment,
3069-
const std::optional<DarwinSDKInfo> &SDKInfo) {
3070-
if (TargetEnvironment == Darwin::NativeEnvironment ||
3071-
TargetEnvironment == Darwin::Simulator ||
3072-
TargetEnvironment == Darwin::MacCatalyst) {
3073-
// Standard xnu/Mach/Darwin based environments
3074-
// depend on the SDK version.
3075-
} else {
3076-
// All other environments support builtin modules from the start.
3077-
return true;
3078-
}
3079-
3084+
static bool
3085+
sdkSupportsBuiltinModules(const std::optional<DarwinSDKInfo> &SDKInfo) {
30803086
if (!SDKInfo)
30813087
// If there is no SDK info, assume this is building against a
30823088
// pre-SDK version of macOS (i.e. before Mac OS X 10.4). Those
@@ -3087,26 +3093,18 @@ static bool sdkSupportsBuiltinModules(
30873093
return false;
30883094

30893095
VersionTuple SDKVersion = SDKInfo->getVersion();
3090-
switch (TargetPlatform) {
3096+
switch (SDKInfo->getOS()) {
30913097
// Existing SDKs added support for builtin modules in the fall
30923098
// 2024 major releases.
3093-
case Darwin::MacOS:
3099+
case llvm::Triple::MacOSX:
30943100
return SDKVersion >= VersionTuple(15U);
3095-
case Darwin::IPhoneOS:
3096-
switch (TargetEnvironment) {
3097-
case Darwin::MacCatalyst:
3098-
// Mac Catalyst uses `-target arm64-apple-ios18.0-macabi` so the platform
3099-
// is iOS, but it builds with the macOS SDK, so it's the macOS SDK version
3100-
// that's relevant.
3101-
return SDKVersion >= VersionTuple(15U);
3102-
default:
3103-
return SDKVersion >= VersionTuple(18U);
3104-
}
3105-
case Darwin::TvOS:
3101+
case llvm::Triple::IOS:
31063102
return SDKVersion >= VersionTuple(18U);
3107-
case Darwin::WatchOS:
3103+
case llvm::Triple::TvOS:
3104+
return SDKVersion >= VersionTuple(18U);
3105+
case llvm::Triple::WatchOS:
31083106
return SDKVersion >= VersionTuple(11U);
3109-
case Darwin::XROS:
3107+
case llvm::Triple::XROS:
31103108
return SDKVersion >= VersionTuple(2U);
31113109

31123110
// New SDKs support builtin modules from the start.
@@ -3267,7 +3265,7 @@ void Darwin::addClangTargetOptions(
32673265
// i.e. when the builtin stdint.h is in the Darwin module too, the cycle
32683266
// goes away. Note that -fbuiltin-headers-in-system-modules does nothing
32693267
// to fix the same problem with C++ headers, and is generally fragile.
3270-
if (!sdkSupportsBuiltinModules(TargetPlatform, TargetEnvironment, SDKInfo))
3268+
if (!sdkSupportsBuiltinModules(SDKInfo))
32713269
CC1Args.push_back("-fbuiltin-headers-in-system-modules");
32723270
}
32733271

Diff for: clang/test/CAS/Inputs/MacOSX11.0.sdk/SDKSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"DefaultVariant": "macos", "DisplayName": "macOS 11",
33
"Version": "11.0",
4+
"CanonicalName": "macosx11.0",
45
"MaximumDeploymentTarget": "11.0.99",
56
"PropertyConditionFallbackNames": [], "VersionMap": {
67
"iOSMac_macOS": {
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Version":"23.0", "MaximumDeploymentTarget": "23.0.99"}
1+
{"Version":"23.0", "CanonicalName": "driverkit23.0", "MaximumDeploymentTarget": "23.0.99"}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Version":"10.14", "MaximumDeploymentTarget": "10.14.99"}
1+
{"Version":"10.14", "CanonicalName": "macosx10.14", "MaximumDeploymentTarget": "10.14.99"}

Diff for: clang/test/Driver/Inputs/MacOSX10.14.versioned.sdk/SDKSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"Version":"10.14",
3+
"CanonicalName": "macosx10.14",
34
"VersionMap" : {
45
"macOS_iOSMac" : {
56
"10.14.4" : "12.4",

Diff for: clang/test/Driver/Inputs/MacOSX10.15.versioned.sdk/SDKSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"Version":"10.15",
3+
"CanonicalName": "macosx10.15",
34
"MaximumDeploymentTarget": "10.15.99",
45
"VersionMap" : {
56
"macOS_iOSMac" : {
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Version":"15.0", "MaximumDeploymentTarget": "15.0.99"}
1+
{"Version":"15.0", "CanonicalName": "macosx15.0", "MaximumDeploymentTarget": "15.0.99"}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Version":"15.1", "MaximumDeploymentTarget": "15.1.99"}
1+
{"Version":"15.1", "CanonicalName": "macosx15.1", "MaximumDeploymentTarget": "15.1.99"}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Version":"6.0.0", "MaximumDeploymentTarget": "6.0.99"}
1+
{"Version":"6.0", "CanonicalName": "watchos6.0", "MaximumDeploymentTarget": "6.0.99"}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Version":"13.0", "MaximumDeploymentTarget": "13.0.99"}
1+
{"Version":"13.0", "CanonicalName": "iphoneos13.0", "MaximumDeploymentTarget": "13.0.99"}

Diff for: clang/test/Driver/darwin-ld-platform-version-watchos.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
// RUN: | FileCheck --check-prefix=SIMUL %s
1919

2020
// LINKER-OLD: "-watchos_version_min" "5.2.0"
21-
// LINKER-NEW: "-platform_version" "watchos" "5.2.0" "6.0.0"
22-
// SIMUL: "-platform_version" "watchos-simulator" "6.0.0" "6.0.0"
21+
// LINKER-NEW: "-platform_version" "watchos" "5.2.0" "6.0"
22+
// SIMUL: "-platform_version" "watchos-simulator" "6.0.0" "6.0"

Diff for: clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"DefaultVariant": "macos", "DisplayName": "macOS 13",
33
"Version": "13.0",
4+
"CanonicalName": "macosx13.0",
45
"MaximumDeploymentTarget": "13.0.99",
56
"PropertyConditionFallbackNames": [], "VersionMap": {
67
"iOSMac_macOS": {

Diff for: clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"DisplayName": "tvOS 15.0",
33
"Version": "15.0",
4+
"CanonicalName": "appletvos15.0",
45
"MaximumDeploymentTarget": "15.0.99",
56
"PropertyConditionFallbackNames": [],
67
"VersionMap": {

Diff for: clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"DefaultVariant": "macos", "DisplayName": "macOS 11",
33
"Version": "11.0",
4+
"CanonicalName": "macosx11.0",
45
"MaximumDeploymentTarget": "11.0.99",
56
"PropertyConditionFallbackNames": [], "VersionMap": {
67
"iOSMac_macOS": {

Diff for: clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"DisplayName": "watchOS 7.0",
33
"Version": "7.0",
4+
"CanonicalName": "watchos7.0",
45
"MaximumDeploymentTarget": "7.0.99",
56
"PropertyConditionFallbackNames": [],
67
"VersionMap": {

0 commit comments

Comments
 (0)