Debian Codegraph @

clang/lib/Basic/DarwinSDKInfo.cpp

raw
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
9 #include "clang/Basic/DarwinSDKInfo.h"
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
17 using namespace clang;
18
19 std::optional<VersionTuple> DarwinSDKInfo::RelatedTargetVersionMapping::map(
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
38 std::optional<DarwinSDKInfo::RelatedTargetVersionMapping>
39 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(
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;
62 return RelatedTargetVersionMapping(
63 Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
64 }
65
66 static DarwinSDKInfo::PlatformInfoStorageType
67 parsePlatformInfos(const llvm::json::Object &Obj, VersionTuple Version) {
68 // The CanonicalName is the Xcode platform followed by a version, e.g.
69 // macosx15.0. The associated SDKPlatformInfo must be the first entry in the
70 // returned PlatformInfoStorageType.
71 StringRef XcodePlatform;
72 if (auto CanonicalName = Obj.getString("CanonicalName")) {
73 size_t VersionStart = CanonicalName->find_first_of("0123456789");
74 XcodePlatform = CanonicalName->slice(0, VersionStart);
75 }
76
77 DarwinSDKInfo::PlatformInfoStorageType PlatformInfos;
78 auto SupportedTargets = Obj.getObject("SupportedTargets");
79 if (!SupportedTargets) {
80 // For older SDKs that don't have SupportedTargets, infer one from the Xcode
81 // platform.
82 if (XcodePlatform == "macosx") {
83 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::MacOSX,
84 llvm::Triple::UnknownEnvironment,
85 llvm::Triple::MachO, ""});
86 } else if (XcodePlatform == "iphoneos") {
87 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::IOS,
88 llvm::Triple::UnknownEnvironment,
89 llvm::Triple::MachO, ""});
90 } else if (XcodePlatform == "iphonesimulator") {
91 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::IOS,
92 llvm::Triple::Simulator, llvm::Triple::MachO,
93 ""});
94 } else if (XcodePlatform == "appletvos") {
95 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::TvOS,
96 llvm::Triple::UnknownEnvironment,
97 llvm::Triple::MachO, ""});
98 } else if (XcodePlatform == "appletvsimulator") {
99 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::TvOS,
100 llvm::Triple::Simulator, llvm::Triple::MachO,
101 ""});
102 } else if (XcodePlatform == "watchos") {
103 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::WatchOS,
104 llvm::Triple::UnknownEnvironment,
105 llvm::Triple::MachO, ""});
106 } else if (XcodePlatform == "watchsimulator") {
107 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::WatchOS,
108 llvm::Triple::Simulator, llvm::Triple::MachO,
109 ""});
110 } else if (XcodePlatform == "driverkit") {
111 PlatformInfos.push_back({llvm::Triple::Apple, llvm::Triple::DriverKit,
112 llvm::Triple::UnknownEnvironment,
113 llvm::Triple::MachO, "/System/DriverKit"});
114 } else {
115 llvm::reportFatalUsageError(
116 "Unrecognized CanonicalName in SDKSettings.json. SupportedTargets is "
117 "expected, or a recognized CanonicalName.");
118 }
119 return PlatformInfos;
120 }
121
122 for (auto SupportedTargetPair : *SupportedTargets) {
123 llvm::json::Object *SupportedTarget =
124 SupportedTargetPair.getSecond().getAsObject();
125 auto Vendor = SupportedTarget->getString("LLVMTargetTripleVendor");
126 auto OS = SupportedTarget->getString("LLVMTargetTripleSys");
127 if (!Vendor || !OS)
128 continue;
129
130 StringRef Arch = llvm::Triple::getArchName(llvm::Triple::UnknownArch);
131 auto Environment =
132 SupportedTarget->getString("LLVMTargetTripleEnvironment");
133 llvm::Triple Triple;
134 if (Environment)
135 Triple = llvm::Triple(Arch, *Vendor, *OS, *Environment);
136 else
137 Triple = llvm::Triple(Arch, *Vendor, *OS);
138
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
156 DarwinSDKInfo::SDKPlatformInfo PlatformInfo(
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
167 static 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
178 std::optional<DarwinSDKInfo>
179 DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) {
180 auto Version = getVersionKey(*Obj, "Version");
181 if (!Version)
182 return std::nullopt;
183 auto MaximumDeploymentVersion =
184 getVersionKey(*Obj, "MaximumDeploymentTarget");
185 if (!MaximumDeploymentVersion)
186 return std::nullopt;
187 PlatformInfoStorageType PlatformInfos = parsePlatformInfos(*Obj, *Version);
188 llvm::DenseMap<OSEnvPair::StorageType,
189 std::optional<RelatedTargetVersionMapping>>
190 VersionMappings;
191 if (const auto *VM = Obj->getObject("VersionMap")) {
192 // FIXME: Generalize this out beyond iOS-deriving targets.
193 // Look for ios_<targetos> version mapping for targets that derive from ios.
194 for (const auto &KV : *VM) {
195 auto Pair = StringRef(KV.getFirst()).split("_");
196 if (Pair.first.compare_insensitive("ios") == 0) {
197 llvm::Triple TT(llvm::Twine("--") + Pair.second.lower());
198 if (TT.getOS() != llvm::Triple::UnknownOS) {
199 auto Mapping = RelatedTargetVersionMapping::parseJSON(
200 *KV.getSecond().getAsObject(), *MaximumDeploymentVersion);
201 if (Mapping)
202 VersionMappings[OSEnvPair(llvm::Triple::IOS,
203 llvm::Triple::UnknownEnvironment,
204 TT.getOS(),
205 llvm::Triple::UnknownEnvironment)
206 .Value] = std::move(Mapping);
207 }
208 }
209 }
210
211 if (const auto *Mapping = VM->getObject("macOS_iOSMac")) {
212 auto VersionMap = RelatedTargetVersionMapping::parseJSON(
213 *Mapping, *MaximumDeploymentVersion);
214 if (!VersionMap)
215 return std::nullopt;
216 VersionMappings[OSEnvPair::macOStoMacCatalystPair().Value] =
217 std::move(VersionMap);
218 }
219 if (const auto *Mapping = VM->getObject("iOSMac_macOS")) {
220 auto VersionMap = RelatedTargetVersionMapping::parseJSON(
221 *Mapping, *MaximumDeploymentVersion);
222 if (!VersionMap)
223 return std::nullopt;
224 VersionMappings[OSEnvPair::macCatalystToMacOSPair().Value] =
225 std::move(VersionMap);
226 }
227 }
228
229 return DarwinSDKInfo(std::move(*Version),
230 std::move(*MaximumDeploymentVersion),
231 std::move(PlatformInfos), std::move(VersionMappings));
232 }
233
234 Expected<std::optional<DarwinSDKInfo>>
235 clang::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
236 llvm::SmallString<256> Filepath = SDKRootPath;
237 llvm::sys::path::append(Filepath, "SDKSettings.json");
238 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
239 VFS.getBufferForFile(Filepath);
240 if (!File) {
241 // If the file couldn't be read, assume it just doesn't exist.
242 return std::nullopt;
243 }
244 Expected<llvm::json::Value> Result =
245 llvm::json::parse(File.get()->getBuffer());
246 if (!Result)
247 return Result.takeError();
248
249 if (const auto *Obj = Result->getAsObject()) {
250 if (auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(Obj))
251 return std::move(SDKInfo);
252 }
253 return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
254 llvm::inconvertibleErrorCode());
255 }