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 llvm::Triple::OSType
parseOS(
const llvm::json::Object &Obj) {
69 auto CanonicalName = Obj.getString(
"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);
88static std::optional<VersionTuple>
getVersionKey(
const llvm::json::Object &Obj,
90 auto Value = Obj.getString(Key);
94 if (Version.tryParse(*
Value))
99std::optional<DarwinSDKInfo>
104 auto MaximumDeploymentVersion =
106 if (!MaximumDeploymentVersion)
108 llvm::Triple::OSType OS =
parseOS(*Obj);
110 std::optional<RelatedTargetVersionMapping>>
112 if (
const auto *VM = Obj->getObject(
"VersionMap")) {
115 for (
const auto &KV : *VM) {
116 auto Pair = StringRef(KV.getFirst()).split(
"_");
117 if (Pair.first.compare_insensitive(
"ios") == 0) {
118 llvm::Triple TT(llvm::Twine(
"--") + Pair.second.lower());
119 if (TT.getOS() != llvm::Triple::UnknownOS) {
121 *KV.getSecond().getAsObject(), *MaximumDeploymentVersion);
123 VersionMappings[
OSEnvPair(llvm::Triple::IOS,
124 llvm::Triple::UnknownEnvironment,
126 llvm::Triple::UnknownEnvironment)
127 .Value] = std::move(Mapping);
132 if (
const auto *Mapping = VM->getObject(
"macOS_iOSMac")) {
134 *Mapping, *MaximumDeploymentVersion);
138 std::move(VersionMap);
140 if (
const auto *Mapping = VM->getObject(
"iOSMac_macOS")) {
142 *Mapping, *MaximumDeploymentVersion);
146 std::move(VersionMap);
151 std::move(*MaximumDeploymentVersion), OS,
152 std::move(VersionMappings));
158 llvm::sys::path::append(Filepath,
"SDKSettings.json");
159 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
File =
160 VFS.getBufferForFile(Filepath);
166 llvm::json::parse(
File.get()->getBuffer());
168 return Result.takeError();
170 if (
const auto *Obj =
Result->getAsObject()) {
172 return std::move(SDKInfo);
174 return llvm::make_error<llvm::StringError>(
"invalid SDKSettings.json",
175 llvm::inconvertibleErrorCode());
static llvm::Triple::OSType parseOS(const llvm::json::Object &Obj)
static std::optional< VersionTuple > getVersionKey(const llvm::json::Object &Obj, StringRef Key)
The information about the darwin SDK that was used during this compilation.
static std::optional< DarwinSDKInfo > parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj)
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...