clang 22.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 VersionTuple Version, VersionTuple MaximumDeploymentTarget,
185 PlatformInfoStorageType PlatformInfos,
186 llvm::DenseMap<OSEnvPair::StorageType,
187 std::optional<RelatedTargetVersionMapping>>
188 VersionMappings =
189 llvm::DenseMap<OSEnvPair::StorageType,
190 std::optional<RelatedTargetVersionMapping>>())
191 : Version(Version), MaximumDeploymentTarget(MaximumDeploymentTarget),
192 PlatformInfos(std::move(PlatformInfos)),
193 VersionMappings(std::move(VersionMappings)) {}
194
195 const llvm::VersionTuple &getVersion() const { return Version; }
196
198 return PlatformInfos[0];
199 }
200
201 const StringRef getPlatformPrefix(llvm::Triple Triple) const {
202 auto PlatformInfoIt = llvm::find(PlatformInfos, Triple);
203 if (PlatformInfoIt == PlatformInfos.end())
204 return StringRef();
205 return PlatformInfoIt->getPlatformPrefix();
206 }
207
208 // Returns the optional, target-specific version mapping that maps from one
209 // target to another target.
210 //
211 // This mapping is constructed from an appropriate mapping in the SDKSettings,
212 // for instance, when building for Mac Catalyst, the mapping would contain the
213 // "macOS_iOSMac" mapping as it maps the macOS versions to the Mac Catalyst
214 // versions.
215 //
216 // This mapping does not exist when the target doesn't have an appropriate
217 // related version mapping, or when there was an error reading the mapping
218 // from the SDKSettings, or when it's missing in the SDKSettings.
220 auto Mapping = VersionMappings.find(Kind.Value);
221 if (Mapping == VersionMappings.end())
222 return nullptr;
223 return Mapping->getSecond() ? &*Mapping->getSecond() : nullptr;
224 }
225
226 static std::optional<DarwinSDKInfo>
227 parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj);
228
229private:
230 VersionTuple Version;
231 VersionTuple MaximumDeploymentTarget;
232 PlatformInfoStorageType PlatformInfos;
233 // Need to wrap the value in an optional here as the value has to be default
234 // constructible, and std::unique_ptr doesn't like DarwinSDKInfo being
235 // Optional as Optional is trying to copy it in emplace.
236 llvm::DenseMap<OSEnvPair::StorageType,
237 std::optional<RelatedTargetVersionMapping>>
238 VersionMappings;
239};
240
241/// Parse the SDK information from the SDKSettings.json file.
242///
243/// \returns an error if the SDKSettings.json file is invalid, std::nullopt if
244/// the SDK has no SDKSettings.json, or a valid \c DarwinSDKInfo otherwise.
246parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath);
247
248} // end namespace clang
249
250#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)
SmallVector< SDKPlatformInfo, 2 > PlatformInfoStorageType
const RelatedTargetVersionMapping * getVersionMapping(OSEnvPair Kind) const
const SDKPlatformInfo & getCanonicalPlatformInfo() const
const StringRef getPlatformPrefix(llvm::Triple Triple) const
DarwinSDKInfo(VersionTuple Version, VersionTuple MaximumDeploymentTarget, PlatformInfoStorageType PlatformInfos, llvm::DenseMap< OSEnvPair::StorageType, std::optional< RelatedTargetVersionMapping > > VersionMappings=llvm::DenseMap< OSEnvPair::StorageType, std::optional< RelatedTargetVersionMapping > >())
const llvm::VersionTuple & getVersion() const
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.
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