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
104 const llvm::json::Object &Obj, std::optional<StringRef> XcodePlatform,
105 llvm::Triple::OSType SDKOS, llvm::Triple::EnvironmentType SDKEnvironment,
106 VersionTuple Version) {
108 auto SupportedTargets = Obj.getObject("SupportedTargets");
109 if (!SupportedTargets) {
110 // For older SDKs that don't have SupportedTargets, infer one from the SDK's
111 // OS/Environment.
112 StringRef PlatformPrefix;
113 if (SDKOS == llvm::Triple::DriverKit)
114 PlatformPrefix = "/System/DriverKit";
115 PlatformInfos.push_back({llvm::Triple::Apple, SDKOS, SDKEnvironment,
116 llvm::Triple::MachO, PlatformPrefix});
117 return PlatformInfos;
118 }
119
120 for (auto SupportedTargetPair : *SupportedTargets) {
121 llvm::json::Object *SupportedTarget =
122 SupportedTargetPair.getSecond().getAsObject();
123 auto Vendor = SupportedTarget->getString("LLVMTargetTripleVendor");
124 auto OS = SupportedTarget->getString("LLVMTargetTripleSys");
125 if (!Vendor || !OS)
126 continue;
127
128 StringRef Arch = llvm::Triple::getArchName(llvm::Triple::UnknownArch);
129 auto Environment =
130 SupportedTarget->getString("LLVMTargetTripleEnvironment");
131 llvm::Triple Triple;
132 if (Environment)
133 Triple = llvm::Triple(Arch, *Vendor, *OS, *Environment);
134 else
135 Triple = llvm::Triple(Arch, *Vendor, *OS);
136
137 // The key is either the Xcode platform, or a variant. The platform must be
138 // the first entry in the returned PlatformInfoStorageType.
139 StringRef PlatformOrVariant = SupportedTargetPair.getFirst();
140
141 StringRef EffectivePlatformPrefix;
142 // Ignore iosmac value if it exists.
143 if ((PlatformOrVariant != "iosmac") || (Version >= VersionTuple(99))) {
144 auto PlatformPrefix = SupportedTarget->getString("SystemPrefix");
145 if (PlatformPrefix) {
146 EffectivePlatformPrefix = *PlatformPrefix;
147 } else {
148 // Older SDKs don't have SystemPrefix in SupportedTargets, manually add
149 // their prefixes.
150 if ((Triple.getOS() == llvm::Triple::DriverKit) &&
151 (Version < VersionTuple(22, 1)))
152 EffectivePlatformPrefix = "/System/DriverKit";
153 }
154 }
155
157 Triple.getVendor(), Triple.getOS(), Triple.getEnvironment(),
158 Triple.getObjectFormat(), EffectivePlatformPrefix);
159 if (PlatformOrVariant == XcodePlatform)
160 PlatformInfos.insert(PlatformInfos.begin(), PlatformInfo);
161 else
162 PlatformInfos.push_back(PlatformInfo);
163 }
164 return PlatformInfos;
165}
166
167static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
168 StringRef Key) {
169 auto Value = Obj.getString(Key);
170 if (!Value)
171 return std::nullopt;
172 VersionTuple Version;
173 if (Version.tryParse(*Value))
174 return std::nullopt;
175 return Version;
176}
177
178std::optional<DarwinSDKInfo>
180 const llvm::json::Object *Obj) {
181 auto Version = getVersionKey(*Obj, "Version");
182 if (!Version)
183 return std::nullopt;
184 auto MaximumDeploymentVersion =
185 getVersionKey(*Obj, "MaximumDeploymentTarget");
186 if (!MaximumDeploymentVersion)
187 return std::nullopt;
188 std::optional<StringRef> XcodePlatform = parseXcodePlatform(*Obj);
189 std::pair<llvm::Triple::OSType, llvm::Triple::EnvironmentType>
190 OSAndEnvironment = parseOSAndEnvironment(XcodePlatform);
191 PlatformInfoStorageType PlatformInfos =
192 parsePlatformInfos(*Obj, XcodePlatform, OSAndEnvironment.first,
193 OSAndEnvironment.second, *Version);
194 llvm::DenseMap<OSEnvPair::StorageType,
195 std::optional<RelatedTargetVersionMapping>>
196 VersionMappings;
197 if (const auto *VM = Obj->getObject("VersionMap")) {
198 // FIXME: Generalize this out beyond iOS-deriving targets.
199 // Look for ios_<targetos> version mapping for targets that derive from ios.
200 for (const auto &KV : *VM) {
201 auto Pair = StringRef(KV.getFirst()).split("_");
202 if (Pair.first.compare_insensitive("ios") == 0) {
203 llvm::Triple TT(llvm::Twine("--") + Pair.second.lower());
204 if (TT.getOS() != llvm::Triple::UnknownOS) {
206 *KV.getSecond().getAsObject(), *MaximumDeploymentVersion);
207 if (Mapping)
208 VersionMappings[OSEnvPair(llvm::Triple::IOS,
209 llvm::Triple::UnknownEnvironment,
210 TT.getOS(),
211 llvm::Triple::UnknownEnvironment)
212 .Value] = std::move(Mapping);
213 }
214 }
215 }
216
217 if (const auto *Mapping = VM->getObject("macOS_iOSMac")) {
219 *Mapping, *MaximumDeploymentVersion);
220 if (!VersionMap)
221 return std::nullopt;
222 VersionMappings[OSEnvPair::macOStoMacCatalystPair().Value] =
223 std::move(VersionMap);
224 }
225 if (const auto *Mapping = VM->getObject("iOSMac_macOS")) {
227 *Mapping, *MaximumDeploymentVersion);
228 if (!VersionMap)
229 return std::nullopt;
230 VersionMappings[OSEnvPair::macCatalystToMacOSPair().Value] =
231 std::move(VersionMap);
232 }
233 }
234
235 return DarwinSDKInfo(std::move(FilePath), OSAndEnvironment.first,
236 OSAndEnvironment.second, std::move(*Version),
237 std::move(*MaximumDeploymentVersion),
238 std::move(PlatformInfos), std::move(VersionMappings));
239}
240
242clang::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
243 llvm::SmallString<256> Filepath = SDKRootPath;
244 llvm::sys::path::append(Filepath, "SDKSettings.json");
245 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
246 VFS.getBufferForFile(Filepath);
247 if (!File) {
248 // If the file couldn't be read, assume it just doesn't exist.
249 return std::nullopt;
250 }
252 llvm::json::parse(File.get()->getBuffer());
253 if (!Result)
254 return Result.takeError();
255
256 if (const auto *Obj = Result->getAsObject()) {
258 Filepath.str().str(), Obj))
259 return std::move(SDKInfo);
260 }
261 return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
262 llvm::inconvertibleErrorCode());
263}
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, VersionTuple MaximumDeploymentTarget, PlatformInfoStorageType PlatformInfos, llvm::DenseMap< OSEnvPair::StorageType, std::optional< RelatedTargetVersionMapping > > VersionMappings=llvm::DenseMap< OSEnvPair::StorageType, std::optional< RelatedTargetVersionMapping > >())
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...
Information about the supported platforms, derived from the target triple definitions,...