Skip to content

Commit bd197ca

Browse files
[clang][modules] Determine if the SDK supports builtin modules independent of the target (#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.
1 parent 9f2feeb commit bd197ca

File tree

15 files changed

+72
-41
lines changed

15 files changed

+72
-41
lines changed

clang/include/clang/Basic/DarwinSDKInfo.h

Lines changed: 5 additions & 1 deletion
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.

clang/lib/Basic/DarwinSDKInfo.cpp

Lines changed: 25 additions & 1 deletion
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

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,8 @@ struct DarwinPlatform {
18861886
assert(IsValid && "invalid SDK version");
18871887
return DarwinSDKInfo(
18881888
Version,
1889-
/*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99));
1889+
/*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99),
1890+
getOSFromPlatform(Platform));
18901891
}
18911892

18921893
private:
@@ -1916,6 +1917,23 @@ struct DarwinPlatform {
19161917
}
19171918
}
19181919

1920+
static llvm::Triple::OSType getOSFromPlatform(DarwinPlatformKind Platform) {
1921+
switch (Platform) {
1922+
case DarwinPlatformKind::MacOS:
1923+
return llvm::Triple::MacOSX;
1924+
case DarwinPlatformKind::IPhoneOS:
1925+
return llvm::Triple::IOS;
1926+
case DarwinPlatformKind::TvOS:
1927+
return llvm::Triple::TvOS;
1928+
case DarwinPlatformKind::WatchOS:
1929+
return llvm::Triple::WatchOS;
1930+
case DarwinPlatformKind::DriverKit:
1931+
return llvm::Triple::DriverKit;
1932+
case DarwinPlatformKind::XROS:
1933+
return llvm::Triple::XROS;
1934+
}
1935+
}
1936+
19191937
SourceKind Kind;
19201938
DarwinPlatformKind Platform;
19211939
DarwinEnvironmentKind Environment = DarwinEnvironmentKind::NativeEnvironment;
@@ -2966,20 +2984,8 @@ bool Darwin::isAlignedAllocationUnavailable() const {
29662984
return TargetVersion < alignedAllocMinVersion(OS);
29672985
}
29682986

2969-
static bool sdkSupportsBuiltinModules(
2970-
const Darwin::DarwinPlatformKind &TargetPlatform,
2971-
const Darwin::DarwinEnvironmentKind &TargetEnvironment,
2972-
const std::optional<DarwinSDKInfo> &SDKInfo) {
2973-
if (TargetEnvironment == Darwin::NativeEnvironment ||
2974-
TargetEnvironment == Darwin::Simulator ||
2975-
TargetEnvironment == Darwin::MacCatalyst) {
2976-
// Standard xnu/Mach/Darwin based environments
2977-
// depend on the SDK version.
2978-
} else {
2979-
// All other environments support builtin modules from the start.
2980-
return true;
2981-
}
2982-
2987+
static bool
2988+
sdkSupportsBuiltinModules(const std::optional<DarwinSDKInfo> &SDKInfo) {
29832989
if (!SDKInfo)
29842990
// If there is no SDK info, assume this is building against a
29852991
// pre-SDK version of macOS (i.e. before Mac OS X 10.4). Those
@@ -2990,26 +2996,18 @@ static bool sdkSupportsBuiltinModules(
29902996
return false;
29912997

29922998
VersionTuple SDKVersion = SDKInfo->getVersion();
2993-
switch (TargetPlatform) {
2999+
switch (SDKInfo->getOS()) {
29943000
// Existing SDKs added support for builtin modules in the fall
29953001
// 2024 major releases.
2996-
case Darwin::MacOS:
3002+
case llvm::Triple::MacOSX:
29973003
return SDKVersion >= VersionTuple(15U);
2998-
case Darwin::IPhoneOS:
2999-
switch (TargetEnvironment) {
3000-
case Darwin::MacCatalyst:
3001-
// Mac Catalyst uses `-target arm64-apple-ios18.0-macabi` so the platform
3002-
// is iOS, but it builds with the macOS SDK, so it's the macOS SDK version
3003-
// that's relevant.
3004-
return SDKVersion >= VersionTuple(15U);
3005-
default:
3006-
return SDKVersion >= VersionTuple(18U);
3007-
}
3008-
case Darwin::TvOS:
3004+
case llvm::Triple::IOS:
30093005
return SDKVersion >= VersionTuple(18U);
3010-
case Darwin::WatchOS:
3006+
case llvm::Triple::TvOS:
3007+
return SDKVersion >= VersionTuple(18U);
3008+
case llvm::Triple::WatchOS:
30113009
return SDKVersion >= VersionTuple(11U);
3012-
case Darwin::XROS:
3010+
case llvm::Triple::XROS:
30133011
return SDKVersion >= VersionTuple(2U);
30143012

30153013
// New SDKs support builtin modules from the start.
@@ -3138,7 +3136,7 @@ void Darwin::addClangTargetOptions(
31383136
// i.e. when the builtin stdint.h is in the Darwin module too, the cycle
31393137
// goes away. Note that -fbuiltin-headers-in-system-modules does nothing
31403138
// to fix the same problem with C++ headers, and is generally fragile.
3141-
if (!sdkSupportsBuiltinModules(TargetPlatform, TargetEnvironment, SDKInfo))
3139+
if (!sdkSupportsBuiltinModules(SDKInfo))
31423140
CC1Args.push_back("-fbuiltin-headers-in-system-modules");
31433141

31443142
if (!DriverArgs.hasArgNoClaim(options::OPT_fdefine_target_os_macros,
Lines changed: 1 addition & 1 deletion
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"}
Lines changed: 1 addition & 1 deletion
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"}

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

Lines changed: 1 addition & 0 deletions
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" : {
Lines changed: 1 addition & 1 deletion
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"}
Lines changed: 1 addition & 1 deletion
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"}
Lines changed: 1 addition & 1 deletion
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"}
Lines changed: 1 addition & 1 deletion
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"}

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

Lines changed: 2 additions & 2 deletions
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"

clang/test/InstallAPI/Inputs/MacOSX13.0.sdk/SDKSettings.json

Lines changed: 1 addition & 0 deletions
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": {

clang/test/Sema/Inputs/AppleTVOS15.0.sdk/SDKSettings.json

Lines changed: 1 addition & 0 deletions
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": {

clang/test/Sema/Inputs/MacOSX11.0.sdk/SDKSettings.json

Lines changed: 1 addition & 0 deletions
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": {

clang/test/Sema/Inputs/WatchOS7.0.sdk/SDKSettings.json

Lines changed: 1 addition & 0 deletions
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)