clang 23.0.0git
DarwinSDKInfo.h
Go to the documentation of this file.
1//===--- DarwinSDKInfo.h - SDK Information parser for darwin ----*- C++ -*-===//
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#ifndef LLVM_CLANG_BASIC_DARWINSDKINFO_H
10#define LLVM_CLANG_BASIC_DARWINSDKINFO_H
11
12#include "clang/Basic/LLVM.h"
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/VersionTuple.h"
19#include "llvm/Support/VirtualFileSystem.h"
20#include "llvm/TargetParser/Triple.h"
21#include <optional>
22#include <string>
23
24namespace llvm {
25namespace json {
26class Object;
27} // end namespace json
28} // end namespace llvm
29
30namespace clang {
31
32/// The information about the darwin SDK that was used during this compilation.
34public:
35 /// Information about the supported platforms, derived from the target triple
36 /// definitions, in the SDK.
38 public:
39 SDKPlatformInfo(llvm::Triple::VendorType Vendor, llvm::Triple::OSType OS,
40 llvm::Triple::EnvironmentType Environment,
41 llvm::Triple::ObjectFormatType ObjectFormat,
42 StringRef PlatformPrefix)
43 : Vendor(Vendor), OS(OS), Environment(Environment),
44 ObjectFormat(ObjectFormat), PlatformPrefix(PlatformPrefix) {}
45
46 llvm::Triple::VendorType getVendor() const { return Vendor; }
47 llvm::Triple::OSType getOS() const { return OS; }
48 llvm::Triple::EnvironmentType getEnvironment() const { return Environment; }
49 llvm::Triple::ObjectFormatType getObjectFormat() const {
50 return ObjectFormat;
51 }
52 StringRef getPlatformPrefix() const { return PlatformPrefix; }
53
54 bool operator==(const llvm::Triple &RHS) const {
55 return (Vendor == RHS.getVendor()) && (OS == RHS.getOS()) &&
56 (Environment == RHS.getEnvironment()) &&
57 (ObjectFormat == RHS.getObjectFormat());
58 }
59
60 private:
61 llvm::Triple::VendorType Vendor;
62 llvm::Triple::OSType OS;
63 llvm::Triple::EnvironmentType Environment;
64 llvm::Triple::ObjectFormatType ObjectFormat;
65 std::string PlatformPrefix;
66 };
67
68 /// A value that describes two os-environment pairs that can be used as a key
69 /// to the version map in the SDK.
70 struct OSEnvPair {
71 public:
72 using StorageType = uint64_t;
73
74 constexpr OSEnvPair(llvm::Triple::OSType FromOS,
75 llvm::Triple::EnvironmentType FromEnv,
76 llvm::Triple::OSType ToOS,
77 llvm::Triple::EnvironmentType ToEnv)
78 : Value(((StorageType(FromOS) * StorageType(llvm::Triple::LastOSType) +
79 StorageType(FromEnv))
80 << 32ull) |
81 (StorageType(ToOS) * StorageType(llvm::Triple::LastOSType) +
82 StorageType(ToEnv))) {}
83
84 /// Returns the os-environment mapping pair that's used to represent the
85 /// macOS -> Mac Catalyst version mapping.
86 static inline constexpr OSEnvPair macOStoMacCatalystPair() {
87 return OSEnvPair(llvm::Triple::MacOSX, llvm::Triple::UnknownEnvironment,
88 llvm::Triple::IOS, llvm::Triple::MacABI);
89 }
90
91 /// Returns the os-environment mapping pair that's used to represent the
92 /// Mac Catalyst -> macOS version mapping.
93 static inline constexpr OSEnvPair macCatalystToMacOSPair() {
94 return OSEnvPair(llvm::Triple::IOS, llvm::Triple::MacABI,
95 llvm::Triple::MacOSX, llvm::Triple::UnknownEnvironment);
96 }
97
98 /// Returns the os-environment mapping pair that's used to represent the
99 /// iOS -> watchOS version mapping.
100 static inline constexpr OSEnvPair iOStoWatchOSPair() {
101 return OSEnvPair(llvm::Triple::IOS, llvm::Triple::UnknownEnvironment,
102 llvm::Triple::WatchOS, llvm::Triple::UnknownEnvironment);
103 }
104
105 /// Returns the os-environment mapping pair that's used to represent the
106 /// iOS -> tvOS version mapping.
107 static inline constexpr OSEnvPair iOStoTvOSPair() {
108 return OSEnvPair(llvm::Triple::IOS, llvm::Triple::UnknownEnvironment,
109 llvm::Triple::TvOS, llvm::Triple::UnknownEnvironment);
110 }
111
112 private:
114
115 friend class DarwinSDKInfo;
116 };
117
118 /// Represents a version mapping that maps from a version of one target to a
119 /// version of a related target.
120 ///
121 /// e.g. "macOS_iOSMac":{"10.15":"13.1"} is an example of a macOS -> Mac
122 /// Catalyst version map.
124 public:
126 VersionTuple MinimumKeyVersion, VersionTuple MaximumKeyVersion,
127 VersionTuple MinimumValue, VersionTuple MaximumValue,
128 llvm::DenseMap<VersionTuple, VersionTuple> Mapping)
129 : MinimumKeyVersion(MinimumKeyVersion),
130 MaximumKeyVersion(MaximumKeyVersion), MinimumValue(MinimumValue),
131 MaximumValue(MaximumValue), Mapping(Mapping) {
132 assert(!this->Mapping.empty() && "unexpected empty mapping");
133 }
134
135 /// Returns the value with the lowest version in the mapping.
136 const VersionTuple &getMinimumValue() const { return MinimumValue; }
137
138 /// Returns the mapped key, or the appropriate Minimum / MaximumValue if
139 /// they key is outside of the mapping bounds. If they key isn't mapped, but
140 /// within the minimum and maximum bounds, std::nullopt is returned.
141 std::optional<VersionTuple>
142 map(const VersionTuple &Key, const VersionTuple &MinimumValue,
143 std::optional<VersionTuple> MaximumValue) const;
144
145 /// Remap the 'introduced' availability version.
146 /// If None is returned, the 'unavailable' availability should be used
147 /// instead.
148 std::optional<VersionTuple>
149 mapIntroducedAvailabilityVersion(const VersionTuple &Key) const {
150 // API_TO_BE_DEPRECATED is 100000.
151 if (Key.getMajor() == 100000)
152 return VersionTuple(100000);
153 // Use None for maximum to force unavailable behavior for
154 return map(Key, MinimumValue, std::nullopt);
155 }
156
157 /// Remap the 'deprecated' and 'obsoleted' availability version.
158 /// If None is returned for 'obsoleted', the 'unavailable' availability
159 /// should be used instead. If None is returned for 'deprecated', the
160 /// 'deprecated' version should be dropped.
161 std::optional<VersionTuple>
162 mapDeprecatedObsoletedAvailabilityVersion(const VersionTuple &Key) const {
163 // API_TO_BE_DEPRECATED is 100000.
164 if (Key.getMajor() == 100000)
165 return VersionTuple(100000);
166 return map(Key, MinimumValue, MaximumValue);
167 }
168
169 static std::optional<RelatedTargetVersionMapping>
170 parseJSON(const llvm::json::Object &Obj,
171 VersionTuple MaximumDeploymentTarget);
172
173 private:
174 VersionTuple MinimumKeyVersion;
175 VersionTuple MaximumKeyVersion;
176 VersionTuple MinimumValue;
177 VersionTuple MaximumValue;
178 llvm::DenseMap<VersionTuple, VersionTuple> Mapping;
179 };
180
182
184 std::string FilePath, llvm::Triple::OSType OS,
185 llvm::Triple::EnvironmentType Environment, VersionTuple Version,
186 VersionTuple MaximumDeploymentTarget,
187 PlatformInfoStorageType PlatformInfos,
188 llvm::DenseMap<OSEnvPair::StorageType,
189 std::optional<RelatedTargetVersionMapping>>
190 VersionMappings =
191 llvm::DenseMap<OSEnvPair::StorageType,
192 std::optional<RelatedTargetVersionMapping>>())
193 : FilePath(FilePath), OS(OS), Environment(Environment), Version(Version),
194 MaximumDeploymentTarget(MaximumDeploymentTarget),
195 PlatformInfos(std::move(PlatformInfos)),
196 VersionMappings(std::move(VersionMappings)) {}
197
198 StringRef getFilePath() const { return FilePath; }
199
200 llvm::Triple::OSType getOS() const { return OS; }
201
202 llvm::Triple::EnvironmentType getEnvironment() const { return Environment; }
203
204 const llvm::VersionTuple &getVersion() const { return Version; }
205
207 return PlatformInfos[0];
208 }
209
210 const StringRef getPlatformPrefix(llvm::Triple Triple) const {
211 auto PlatformInfoIt = llvm::find(PlatformInfos, Triple);
212 if (PlatformInfoIt == PlatformInfos.end())
213 return StringRef();
214 return PlatformInfoIt->getPlatformPrefix();
215 }
216
217 // Returns the optional, target-specific version mapping that maps from one
218 // target to another target.
219 //
220 // This mapping is constructed from an appropriate mapping in the SDKSettings,
221 // for instance, when building for Mac Catalyst, the mapping would contain the
222 // "macOS_iOSMac" mapping as it maps the macOS versions to the Mac Catalyst
223 // versions.
224 //
225 // This mapping does not exist when the target doesn't have an appropriate
226 // related version mapping, or when there was an error reading the mapping
227 // from the SDKSettings, or when it's missing in the SDKSettings.
229 auto Mapping = VersionMappings.find(Kind.Value);
230 if (Mapping == VersionMappings.end())
231 return nullptr;
232 return Mapping->getSecond() ? &*Mapping->getSecond() : nullptr;
233 }
234
235 static std::optional<DarwinSDKInfo>
236 parseDarwinSDKSettingsJSON(std::string FilePath,
237 const llvm::json::Object *Obj);
238
239private:
240 std::string FilePath;
241 llvm::Triple::OSType OS;
242 llvm::Triple::EnvironmentType Environment;
243 VersionTuple Version;
244 VersionTuple MaximumDeploymentTarget;
245 PlatformInfoStorageType PlatformInfos;
246 // Need to wrap the value in an optional here as the value has to be default
247 // constructible, and std::unique_ptr doesn't like DarwinSDKInfo being
248 // Optional as Optional is trying to copy it in emplace.
249 llvm::DenseMap<OSEnvPair::StorageType,
250 std::optional<RelatedTargetVersionMapping>>
251 VersionMappings;
252};
253
254/// Parse the SDK information from the SDKSettings.json file.
255///
256/// \returns an error if the SDKSettings.json file is invalid, std::nullopt if
257/// the SDK has no SDKSettings.json, or a valid \c DarwinSDKInfo otherwise.
259parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath);
260
261} // end namespace clang
262
263#endif // LLVM_CLANG_BASIC_DARWINSDKINFO_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Represents a version mapping that maps from a version of one target to a version of a related target.
const VersionTuple & getMinimumValue() const
Returns the value with the lowest version in the mapping.
std::optional< VersionTuple > mapIntroducedAvailabilityVersion(const VersionTuple &Key) const
Remap the 'introduced' availability version.
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)
std::optional< VersionTuple > mapDeprecatedObsoletedAvailabilityVersion(const VersionTuple &Key) const
Remap the 'deprecated' and 'obsoleted' availability version.
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
const RelatedTargetVersionMapping * getVersionMapping(OSEnvPair Kind) const
const SDKPlatformInfo & getCanonicalPlatformInfo() const
StringRef getFilePath() const
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 > >())
const StringRef getPlatformPrefix(llvm::Triple Triple) const
llvm::Triple::EnvironmentType getEnvironment() const
const llvm::VersionTuple & getVersion() const
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.
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
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...
static constexpr OSEnvPair iOStoWatchOSPair()
Returns the os-environment mapping pair that's used to represent the iOS -> watchOS version mapping.
static constexpr OSEnvPair iOStoTvOSPair()
Returns the os-environment mapping pair that's used to represent the iOS -> tvOS version mapping.
constexpr OSEnvPair(llvm::Triple::OSType FromOS, llvm::Triple::EnvironmentType FromEnv, llvm::Triple::OSType ToOS, llvm::Triple::EnvironmentType ToEnv)
Information about the supported platforms, derived from the target triple definitions,...
llvm::Triple::OSType getOS() const
llvm::Triple::VendorType getVendor() const
llvm::Triple::EnvironmentType getEnvironment() const
SDKPlatformInfo(llvm::Triple::VendorType Vendor, llvm::Triple::OSType OS, llvm::Triple::EnvironmentType Environment, llvm::Triple::ObjectFormatType ObjectFormat, StringRef PlatformPrefix)
llvm::Triple::ObjectFormatType getObjectFormat() const
bool operator==(const llvm::Triple &RHS) const