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"
20 const VersionTuple &Key,
const VersionTuple &MinimumValue,
21 std::optional<VersionTuple> MaximumValue)
const {
22 if (Key < MinimumKeyVersion)
24 if (Key > MaximumKeyVersion)
26 auto KV = Mapping.find(Key.normalize());
27 if (KV != Mapping.end())
28 return KV->getSecond();
33 return map(VersionTuple(Key.getMajor()), MinimumValue, MaximumValue);
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))
51 Mapping[KeyVersion.normalize()] = ValueVersion;
56 if (ValueVersion < MinValue)
57 MinValue = ValueVersion;
63 Min,
Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
66static std::optional<StringRef>
70 auto CanonicalName = Obj.getString(
"CanonicalName");
73 size_t VersionStart = CanonicalName->find_first_of(
"0123456789");
74 return CanonicalName->slice(0, VersionStart);
77static std::pair<llvm::Triple::OSType, llvm::Triple::EnvironmentType>
80 return {llvm::Triple::UnknownOS, llvm::Triple::UnknownEnvironment};
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);
93 llvm::Triple::EnvironmentType Environment =
94 llvm::StringSwitch<llvm::Triple::EnvironmentType>(*XcodePlatform)
95 .Cases({
"iphonesimulator",
"appletvsimulator",
"watchsimulator",
97 llvm::Triple::Simulator)
98 .
Default(llvm::Triple::UnknownEnvironment);
100 return {OS, Environment};
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) {
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;
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");
128 StringRef
Arch = llvm::Triple::getArchName(llvm::Triple::UnknownArch);
130 SupportedTarget->getString(
"LLVMTargetTripleEnvironment");
133 Triple = llvm::Triple(
Arch, *Vendor, *OS, *Environment);
135 Triple = llvm::Triple(
Arch, *Vendor, *OS);
139 StringRef PlatformOrVariant = SupportedTargetPair.getFirst();
141 StringRef EffectivePlatformPrefix;
143 if ((PlatformOrVariant !=
"iosmac") || (Version >= VersionTuple(99))) {
144 auto PlatformPrefix = SupportedTarget->getString(
"SystemPrefix");
145 if (PlatformPrefix) {
146 EffectivePlatformPrefix = *PlatformPrefix;
150 if ((Triple.getOS() == llvm::Triple::DriverKit) &&
151 (Version < VersionTuple(22, 1)))
152 EffectivePlatformPrefix =
"/System/DriverKit";
157 Triple.getVendor(), Triple.getOS(), Triple.getEnvironment(),
158 Triple.getObjectFormat(), EffectivePlatformPrefix);
159 if (PlatformOrVariant == XcodePlatform)
160 PlatformInfos.insert(PlatformInfos.begin(), PlatformInfo);
162 PlatformInfos.push_back(PlatformInfo);
164 return PlatformInfos;
167static std::optional<VersionTuple>
getVersionKey(
const llvm::json::Object &Obj,
169 auto Value = Obj.getString(Key);
172 VersionTuple Version;
173 if (Version.tryParse(*
Value))
178std::optional<DarwinSDKInfo>
180 const llvm::json::Object *Obj) {
184 auto MaximumDeploymentVersion =
186 if (!MaximumDeploymentVersion)
189 std::pair<llvm::Triple::OSType, llvm::Triple::EnvironmentType>
193 OSAndEnvironment.second, *Version);
195 std::optional<RelatedTargetVersionMapping>>
197 if (
const auto *VM = Obj->getObject(
"VersionMap")) {
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);
208 VersionMappings[
OSEnvPair(llvm::Triple::IOS,
209 llvm::Triple::UnknownEnvironment,
211 llvm::Triple::UnknownEnvironment)
212 .Value] = std::move(Mapping);
217 if (
const auto *Mapping = VM->getObject(
"macOS_iOSMac")) {
219 *Mapping, *MaximumDeploymentVersion);
223 std::move(VersionMap);
225 if (
const auto *Mapping = VM->getObject(
"iOSMac_macOS")) {
227 *Mapping, *MaximumDeploymentVersion);
231 std::move(VersionMap);
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));
244 llvm::sys::path::append(Filepath,
"SDKSettings.json");
245 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
File =
246 VFS.getBufferForFile(Filepath);
252 llvm::json::parse(
File.get()->getBuffer());
254 return Result.takeError();
256 if (
const auto *Obj =
Result->getAsObject()) {
258 Filepath.str().str(), Obj))
259 return std::move(SDKInfo);
261 return llvm::make_error<llvm::StringError>(
"invalid SDKSettings.json",
262 llvm::inconvertibleErrorCode());
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)
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.
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...