clang 23.0.0git
DarwinSDKInfo.cpp
Go to the documentation of this file.
1//===--- DarwinSDKInfo.cpp - SDK Information parser for darwin - ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/StringSwitch.h"
11#include "llvm/Support/ErrorOr.h"
12#include "llvm/Support/JSON.h"
13#include "llvm/Support/MemoryBuffer.h"
14#include "llvm/Support/Path.h"
15#include <optional>
16
17using namespace clang;
18
20 const VersionTuple &Key, const VersionTuple &MinimumValue,
21 std::optional<VersionTuple> MaximumValue) const {
22 if (Key < MinimumKeyVersion)
23 return MinimumValue;
24 if (Key > MaximumKeyVersion)
25 return MaximumValue;
26 auto KV = Mapping.find(Key.normalize());
27 if (KV != Mapping.end())
28 return KV->getSecond();
29 // If no exact entry found, try just the major key version. Only do so when
30 // a minor version number is present, to avoid recursing indefinitely into
31 // the major-only check.
32 if (Key.getMinor())
33 return map(VersionTuple(Key.getMajor()), MinimumValue, MaximumValue);
34 // If this a major only key, return std::nullopt for a missing entry.
35 return std::nullopt;
36}
37
38std::optional<DarwinSDKInfo::RelatedTargetVersionMapping>
40 const llvm::json::Object &Obj, VersionTuple MaximumDeploymentTarget) {
41 VersionTuple Min = VersionTuple(std::numeric_limits<unsigned>::max());
42 VersionTuple Max = VersionTuple(0);
43 VersionTuple MinValue = Min;
44 llvm::DenseMap<VersionTuple, VersionTuple> Mapping;
45 for (const auto &KV : Obj) {
46 if (auto Val = KV.getSecond().getAsString()) {
47 llvm::VersionTuple KeyVersion;
48 llvm::VersionTuple ValueVersion;
49 if (KeyVersion.tryParse(KV.getFirst()) || ValueVersion.tryParse(*Val))
50 return std::nullopt;
51 Mapping[KeyVersion.normalize()] = ValueVersion;
52 if (KeyVersion < Min)
53 Min = KeyVersion;
54 if (KeyVersion > Max)
55 Max = KeyVersion;
56 if (ValueVersion < MinValue)
57 MinValue = ValueVersion;
58 }
59 }
60 if (Mapping.empty())
61 return std::nullopt;
63 Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
64}
65
66static std::optional<StringRef>
67parseXcodePlatform(const llvm::json::Object &Obj) {
68 // The CanonicalName is the Xcode platform followed by a version, e.g.
69 // macosx15.0.
70 auto CanonicalName = Obj.getString("CanonicalName");
71 if (!CanonicalName)
72 return std::nullopt;
73 size_t VersionStart = CanonicalName->find_first_of("0123456789");
74 return CanonicalName->slice(0, VersionStart);
75}
76
77static std::pair<llvm::Triple::OSType, llvm::Triple::EnvironmentType>
78parseOSAndEnvironment(std::optional<StringRef> XcodePlatform) {
79 if (!XcodePlatform)
80 return {llvm::Triple::UnknownOS, llvm::Triple::UnknownEnvironment};
81
82 llvm::Triple::OSType OS =
83 llvm::StringSwitch<llvm::Triple::OSType>(*XcodePlatform)
84 .Case("macosx", llvm::Triple::MacOSX)
85 .Cases({"iphoneos", "iphonesimulator"}, llvm::Triple::IOS)
86 .Cases({"appletvos", "appletvsimulator"}, llvm::Triple::TvOS)
87 .Cases({"watchos", "watchsimulator"}, llvm::Triple::WatchOS)
88 .Case("bridgeos", llvm::Triple::BridgeOS)
89 .Cases({"xros", "xrsimulator"}, llvm::Triple::XROS)
90 .Case("driverkit", llvm::Triple::DriverKit)
91 .Default(llvm::Triple::UnknownOS);
92
93 llvm::Triple::EnvironmentType Environment =
94 llvm::StringSwitch<llvm::Triple::EnvironmentType>(*XcodePlatform)
95 .Cases({"iphonesimulator", "appletvsimulator", "watchsimulator",
96 "xrsimulator"},
97 llvm::Triple::Simulator)
98 .Default(llvm::Triple::UnknownEnvironment);
99
100 return {OS, Environment};
101}
102
104legacyPlatformInfos(llvm::Triple::OSType SDKOS,
105 llvm::Triple::EnvironmentType SDKEnvironment) {
107 // Synthesize platform infos for older SDKs from the first SDKs with
108 // SupportedTargets: macOS 10.15 (DriverKit 19.0), iOS 13.0, tvOS 13.0,
109 // watchOS 6.0. Older SDKs (especially iOS) most likely supported armv6 and
110 // armv7 architectures that aren't listed here and are difficult to identify
111 // from the SDK.
112 switch (SDKOS) {
113 case llvm::Triple::MacOSX:
114 PlatformInfos.push_back({{llvm::Triple("x86_64-apple-macosx")}, ""});
115 // macOS 10.15 also has a Mac Catalyst supported target, but Mac Catalyst
116 // was new in that version so omit it for older versions.
117 break;
118 case llvm::Triple::DriverKit:
119 // DriverKit 19.0 only had SDKSettings.plist, which isn't used. So this code
120 // path is used in 19.x as well, not just earlier versions.
121 PlatformInfos.push_back(
122 {{llvm::Triple("x86_64-apple-driverkit")}, "/System/DriverKit"});
123 break;
124 case llvm::Triple::IOS:
125 switch (SDKEnvironment) {
126 case llvm::Triple::UnknownEnvironment:
127 PlatformInfos.push_back(
128 {{llvm::Triple("armv7-apple-ios"), llvm::Triple("armv7s-apple-ios"),
129 llvm::Triple("arm64-apple-ios")},
130 ""});
131 break;
132 case llvm::Triple::Simulator:
133 PlatformInfos.push_back(
134 {{llvm::Triple("x86_64-apple-ios-simulator")}, ""});
135 break;
136 default:
137 break;
138 }
139 break;
140 case llvm::Triple::TvOS:
141 switch (SDKEnvironment) {
142 case llvm::Triple::UnknownEnvironment:
143 PlatformInfos.push_back({{llvm::Triple("arm64-apple-tvos")}, ""});
144 break;
145 case llvm::Triple::Simulator:
146 PlatformInfos.push_back(
147 {{llvm::Triple("x86_64-apple-tvos-simulator")}, ""});
148 break;
149 default:
150 break;
151 }
152 break;
153 case llvm::Triple::WatchOS:
154 switch (SDKEnvironment) {
155 case llvm::Triple::UnknownEnvironment:
156 PlatformInfos.push_back({{llvm::Triple("armv7k-apple-watchos"),
157 llvm::Triple("arm64_32-apple-watchos")},
158 ""});
159 break;
160 case llvm::Triple::Simulator:
161 PlatformInfos.push_back(
162 {{llvm::Triple("x86_64-apple-watchos-simulator")}, ""});
163 break;
164 default:
165 break;
166 }
167 break;
168 case llvm::Triple::BridgeOS:
169 PlatformInfos.push_back({{llvm::Triple("armv7-apple-bridgeos"),
170 llvm::Triple("armv7s-apple-bridgeos"),
171 llvm::Triple("arm64-apple-bridgeos")},
172 ""});
173 break;
174 default:
175 break;
176 }
177 return PlatformInfos;
178}
179
181 const llvm::json::Object &Obj, std::optional<StringRef> XcodePlatform,
182 llvm::Triple::OSType SDKOS, llvm::Triple::EnvironmentType SDKEnvironment,
183 VersionTuple Version) {
185 auto SupportedTargets = Obj.getObject("SupportedTargets");
186 if (!SupportedTargets)
187 return legacyPlatformInfos(SDKOS, SDKEnvironment);
188
189 for (const auto &SupportedTargetPair : *SupportedTargets) {
190 const llvm::json::Object *SupportedTarget =
191 SupportedTargetPair.getSecond().getAsObject();
192 if (!SupportedTarget)
193 continue;
194
195 auto Archs = SupportedTarget->getArray("Archs");
196 auto Vendor = SupportedTarget->getString("LLVMTargetTripleVendor");
197 auto OS = SupportedTarget->getString("LLVMTargetTripleSys");
198 if (!Archs || !Vendor || !OS)
199 continue;
200
202 auto Environment =
203 SupportedTarget->getString("LLVMTargetTripleEnvironment");
204 for (const auto &ArchValue : *Archs) {
205 if (auto Arch = ArchValue.getAsString()) {
206 if (Environment)
207 Triples.emplace_back(*Arch, *Vendor, *OS, *Environment);
208 else
209 Triples.emplace_back(*Arch, *Vendor, *OS);
210 }
211 }
212 if (Triples.empty())
213 continue;
214
215 // The key is either the Xcode platform, or a variant. The platform must be
216 // the first entry in the returned PlatformInfoStorageType.
217 StringRef PlatformOrVariant = SupportedTargetPair.getFirst();
218
219 StringRef EffectivePlatformPrefix;
220 // Ignore iosmac value if it exists.
221 if ((PlatformOrVariant != "iosmac") || (Version >= VersionTuple(99))) {
222 auto PlatformPrefix = SupportedTarget->getString("SystemPrefix");
223 if (PlatformPrefix) {
224 EffectivePlatformPrefix = *PlatformPrefix;
225 } else {
226 // Older SDKs don't have SystemPrefix in SupportedTargets, manually add
227 // their prefixes.
228 if ((Triples[0].getOS() == llvm::Triple::DriverKit) &&
229 (Version < VersionTuple(22, 1)))
230 EffectivePlatformPrefix = "/System/DriverKit";
231 }
232 }
233
234 if (PlatformOrVariant == XcodePlatform)
235 PlatformInfos.insert(PlatformInfos.begin(),
236 {std::move(Triples), EffectivePlatformPrefix});
237 else
238 PlatformInfos.emplace_back(std::move(Triples), EffectivePlatformPrefix);
239 }
240 return PlatformInfos;
241}
242
243static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
244 StringRef Key) {
245 auto Value = Obj.getString(Key);
246 if (!Value)
247 return std::nullopt;
248 VersionTuple Version;
249 if (Version.tryParse(*Value))
250 return std::nullopt;
251 return Version;
252}
253
254std::optional<DarwinSDKInfo>
256 const llvm::json::Object *Obj) {
257 auto Version = getVersionKey(*Obj, "Version");
258 if (!Version)
259 return std::nullopt;
260 auto MaximumDeploymentVersion =
261 getVersionKey(*Obj, "MaximumDeploymentTarget");
262 if (!MaximumDeploymentVersion)
263 return std::nullopt;
264 std::optional<StringRef> XcodePlatform = parseXcodePlatform(*Obj);
265 std::pair<llvm::Triple::OSType, llvm::Triple::EnvironmentType>
266 OSAndEnvironment = parseOSAndEnvironment(XcodePlatform);
267 // DisplayName should always be present, but don't require it.
268 StringRef DisplayName =
269 Obj->getString("DisplayName")
270 .value_or(Obj->getString("CanonicalName").value_or("<unknown>"));
271 PlatformInfoStorageType PlatformInfos =
272 parsePlatformInfos(*Obj, XcodePlatform, OSAndEnvironment.first,
273 OSAndEnvironment.second, *Version);
274 llvm::DenseMap<OSEnvPair::StorageType,
275 std::optional<RelatedTargetVersionMapping>>
276 VersionMappings;
277 if (const auto *VM = Obj->getObject("VersionMap")) {
278 // FIXME: Generalize this out beyond iOS-deriving targets.
279 // Look for ios_<targetos> version mapping for targets that derive from ios.
280 for (const auto &KV : *VM) {
281 auto Pair = StringRef(KV.getFirst()).split("_");
282 if (Pair.first.compare_insensitive("ios") == 0) {
283 llvm::Triple TT(llvm::Twine("--") + Pair.second.lower());
284 if (TT.getOS() != llvm::Triple::UnknownOS) {
286 *KV.getSecond().getAsObject(), *MaximumDeploymentVersion);
287 if (Mapping)
288 VersionMappings[OSEnvPair(llvm::Triple::IOS,
289 llvm::Triple::UnknownEnvironment,
290 TT.getOS(),
291 llvm::Triple::UnknownEnvironment)
292 .Value] = std::move(Mapping);
293 }
294 }
295 }
296
297 if (const auto *Mapping = VM->getObject("macOS_iOSMac")) {
299 *Mapping, *MaximumDeploymentVersion);
300 if (!VersionMap)
301 return std::nullopt;
302 VersionMappings[OSEnvPair::macOStoMacCatalystPair().Value] =
303 std::move(VersionMap);
304 }
305 if (const auto *Mapping = VM->getObject("iOSMac_macOS")) {
307 *Mapping, *MaximumDeploymentVersion);
308 if (!VersionMap)
309 return std::nullopt;
310 VersionMappings[OSEnvPair::macCatalystToMacOSPair().Value] =
311 std::move(VersionMap);
312 }
313 }
314
315 return DarwinSDKInfo(std::move(FilePath), OSAndEnvironment.first,
316 OSAndEnvironment.second, std::move(*Version),
317 DisplayName, std::move(*MaximumDeploymentVersion),
318 std::move(PlatformInfos), std::move(VersionMappings));
319}
320
322clang::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
323 llvm::SmallString<256> Filepath = SDKRootPath;
324 llvm::sys::path::append(Filepath, "SDKSettings.json");
325 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
326 VFS.getBufferForFile(Filepath);
327 if (!File) {
328 // If the file couldn't be read, assume it just doesn't exist.
329 return std::nullopt;
330 }
332 llvm::json::parse(File.get()->getBuffer());
333 if (!Result)
334 return Result.takeError();
335
336 if (const auto *Obj = Result->getAsObject()) {
338 Filepath.str().str(), Obj))
339 return std::move(SDKInfo);
340 }
341 return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
342 llvm::inconvertibleErrorCode());
343}
344
345DarwinSDKInfo::DarwinSDKInfo(llvm::Triple::OSType OS,
346 llvm::Triple::EnvironmentType Environment,
347 VersionTuple Version, StringRef DisplayName,
348 VersionTuple MaximumDeploymentTarget)
349 : DarwinSDKInfo("", OS, Environment, Version, DisplayName,
350 MaximumDeploymentTarget,
351 legacyPlatformInfos(OS, Environment)) {}
static DarwinSDKInfo::PlatformInfoStorageType legacyPlatformInfos(llvm::Triple::OSType SDKOS, llvm::Triple::EnvironmentType SDKEnvironment)
static std::optional< StringRef > parseXcodePlatform(const llvm::json::Object &Obj)
static DarwinSDKInfo::PlatformInfoStorageType parsePlatformInfos(const llvm::json::Object &Obj, std::optional< StringRef > XcodePlatform, llvm::Triple::OSType SDKOS, llvm::Triple::EnvironmentType SDKEnvironment, VersionTuple Version)
static std::pair< llvm::Triple::OSType, llvm::Triple::EnvironmentType > parseOSAndEnvironment(std::optional< StringRef > XcodePlatform)
static std::optional< VersionTuple > getVersionKey(const llvm::json::Object &Obj, StringRef Key)
std::optional< VersionTuple > map(const VersionTuple &Key, const VersionTuple &MinimumValue, std::optional< VersionTuple > MaximumValue) const
Returns the mapped key, or the appropriate Minimum / MaximumValue if they key is outside of the mappi...
RelatedTargetVersionMapping(VersionTuple MinimumKeyVersion, VersionTuple MaximumKeyVersion, VersionTuple MinimumValue, VersionTuple MaximumValue, llvm::DenseMap< VersionTuple, VersionTuple > Mapping)
static std::optional< RelatedTargetVersionMapping > parseJSON(const llvm::json::Object &Obj, VersionTuple MaximumDeploymentTarget)
static std::optional< DarwinSDKInfo > parseDarwinSDKSettingsJSON(std::string FilePath, const llvm::json::Object *Obj)
SmallVector< SDKPlatformInfo, 2 > PlatformInfoStorageType
DarwinSDKInfo(std::string FilePath, llvm::Triple::OSType OS, llvm::Triple::EnvironmentType Environment, VersionTuple Version, StringRef DisplayName, VersionTuple MaximumDeploymentTarget, PlatformInfoStorageType PlatformInfos, llvm::DenseMap< OSEnvPair::StorageType, std::optional< RelatedTargetVersionMapping > > VersionMappings=llvm::DenseMap< OSEnvPair::StorageType, std::optional< RelatedTargetVersionMapping > >())
llvm::Triple::OSType getOS() const
The JSON file list parser is used to communicate input to InstallAPI.
Expected< std::optional< DarwinSDKInfo > > parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath)
Parse the SDK information from the SDKSettings.json file.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
A value that describes two os-environment pairs that can be used as a key to the version map in the S...
static constexpr OSEnvPair macCatalystToMacOSPair()
Returns the os-environment mapping pair that's used to represent the Mac Catalyst -> macOS version ma...
static constexpr OSEnvPair macOStoMacCatalystPair()
Returns the os-environment mapping pair that's used to represent the macOS -> Mac Catalyst version ma...
SmallVector< llvm::Triple, 5 > TripleStorageType