LLDB mainline
XcodeSDK.cpp
Go to the documentation of this file.
1//===-- XcodeSDK.cpp ------------------------------------------------------===//
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
11
12#include "lldb/lldb-types.h"
13
14#include "llvm/TargetParser/Triple.h"
15
16#include <string>
17
18using namespace lldb;
19using namespace lldb_private;
20
21static llvm::StringRef GetName(XcodeSDK::Type type) {
22 switch (type) {
24 return "MacOSX";
26 return "iPhoneSimulator";
28 return "iPhoneOS";
30 return "AppleTVSimulator";
32 return "AppleTVOS";
34 return "WatchSimulator";
36 return "WatchOS";
38 return "XRSimulator";
39 case XcodeSDK::XROS:
40 return "XROS";
42 return "BridgeOS";
43 case XcodeSDK::Linux:
44 return "Linux";
46 return {};
47 }
48 llvm_unreachable("Unhandled sdk type!");
49}
50
52 if (!m_name.empty()) {
53 if (!info.version.empty())
54 m_name += info.version.getAsString();
55 if (info.internal)
56 m_name += ".Internal";
57 m_name += ".sdk";
58 }
59}
60
61XcodeSDK &XcodeSDK::operator=(const XcodeSDK &other) = default;
62
63bool XcodeSDK::operator==(const XcodeSDK &other) const {
64 return m_name == other.m_name;
65}
66
67static XcodeSDK::Type ParseSDKName(llvm::StringRef &name) {
68 if (name.consume_front("MacOSX"))
69 return XcodeSDK::MacOSX;
70 if (name.consume_front("iPhoneSimulator"))
72 if (name.consume_front("iPhoneOS"))
73 return XcodeSDK::iPhoneOS;
74 if (name.consume_front("AppleTVSimulator"))
76 if (name.consume_front("AppleTVOS"))
78 if (name.consume_front("WatchSimulator"))
80 if (name.consume_front("WatchOS"))
81 return XcodeSDK::watchOS;
82 if (name.consume_front("XRSimulator"))
84 if (name.consume_front("XROS"))
85 return XcodeSDK::XROS;
86 if (name.consume_front("BridgeOS"))
87 return XcodeSDK::BridgeOS;
88 if (name.consume_front("Linux"))
89 return XcodeSDK::Linux;
90 static_assert(XcodeSDK::Linux == XcodeSDK::numSDKTypes - 1,
91 "New SDK type was added, update this list!");
92 return XcodeSDK::unknown;
93}
94
95static llvm::VersionTuple ParseSDKVersion(llvm::StringRef &name) {
96 unsigned i = 0;
97 while (i < name.size() && name[i] >= '0' && name[i] <= '9')
98 ++i;
99 if (i == name.size() || name[i++] != '.')
100 return {};
101 while (i < name.size() && name[i] >= '0' && name[i] <= '9')
102 ++i;
103 if (i == name.size() || name[i++] != '.')
104 return {};
105
106 llvm::VersionTuple version;
107 version.tryParse(name.slice(0, i - 1));
108 name = name.drop_front(i);
109 return version;
110}
111
112static bool ParseAppleInternalSDK(llvm::StringRef &name) {
113 return name.consume_front("Internal.") || name.consume_front(".Internal.");
114}
115
117 XcodeSDK::Info info;
118 llvm::StringRef input(m_name);
119 info.type = ParseSDKName(input);
120 info.version = ParseSDKVersion(input);
121 info.internal = ParseAppleInternalSDK(input);
122 return info;
123}
124
126 llvm::StringRef input(m_name);
127 ParseSDKName(input);
128 ParseSDKVersion(input);
129 return ParseAppleInternalSDK(input);
130}
131
132llvm::VersionTuple XcodeSDK::GetVersion() const {
133 llvm::StringRef input(m_name);
134 ParseSDKName(input);
135 return ParseSDKVersion(input);
136}
137
139 llvm::StringRef input(m_name);
140 return ParseSDKName(input);
141}
142
143llvm::StringRef XcodeSDK::GetString() const { return m_name; }
144
145const FileSpec &XcodeSDK::GetSysroot() const { return m_sysroot; }
146
147bool XcodeSDK::Info::operator<(const Info &other) const {
148 return std::tie(type, version, internal) <
149 std::tie(other.type, other.version, other.internal);
150}
151
152bool XcodeSDK::Info::operator==(const Info &other) const {
153 return std::tie(type, version, internal) ==
154 std::tie(other.type, other.version, other.internal);
155}
156
157void XcodeSDK::Merge(const XcodeSDK &other) {
158 auto add_internal_sdk_suffix = [](llvm::StringRef sdk) {
159 return (sdk.substr(0, sdk.size() - 3) + "Internal.sdk").str();
160 };
161
162 // The "bigger" SDK always wins.
163 auto l = Parse();
164 auto r = other.Parse();
165 if (l < r)
166 *this = other;
167 else {
168 // The Internal flag always wins.
169 if (!l.internal && r.internal) {
170 if (llvm::StringRef(m_name).ends_with(".sdk")) {
171 m_name = add_internal_sdk_suffix(m_name);
172
173 // The internal SDK is a sibling of the public one, so the sysroot can
174 // be renamed along with the SDK. Leave a sysroot that doesn't name an
175 // SDK directory alone: the compiler may have remapped it, in which
176 // case it is only meaningful verbatim.
177 if (m_sysroot.GetFileNameExtension() == ".sdk")
178 m_sysroot.SetFilename(
179 add_internal_sdk_suffix(m_sysroot.GetFilename()));
180 }
181 }
182 }
183}
184
186 std::string name;
187 switch (info.type) {
188 case MacOSX:
189 name = "macosx";
190 break;
191 case iPhoneSimulator:
192 name = "iphonesimulator";
193 break;
194 case iPhoneOS:
195 name = "iphoneos";
196 break;
197 case AppleTVSimulator:
198 name = "appletvsimulator";
199 break;
200 case AppleTVOS:
201 name = "appletvos";
202 break;
203 case WatchSimulator:
204 name = "watchsimulator";
205 break;
206 case watchOS:
207 name = "watchos";
208 break;
209 case XRSimulator:
210 name = "xrsimulator";
211 break;
212 case XROS:
213 name = "xros";
214 break;
215 case BridgeOS:
216 name = "bridgeos";
217 break;
218 case Linux:
219 name = "linux";
220 break;
221 case unknown:
222 return {};
223 }
224 if (!info.version.empty())
225 name += info.version.getAsString();
226 if (info.internal)
227 name += ".internal";
228 return name;
229}
230
232 llvm::VersionTuple version) {
233 switch (sdk_type) {
234 case Type::MacOSX:
235 return version >= llvm::VersionTuple(10, 10);
236 case Type::iPhoneOS:
238 case Type::AppleTVOS:
240 return version >= llvm::VersionTuple(8);
241 case Type::watchOS:
243 return version >= llvm::VersionTuple(6);
244 case Type::XROS:
246 return true;
247 default:
248 return false;
249 }
250
251 return false;
252}
253
255 const FileSpec &sdk_path) {
256 llvm::StringRef last_path_component = sdk_path.GetFilename();
257
258 if (last_path_component.empty())
259 return false;
260
261 XcodeSDK sdk(last_path_component.str());
262 if (sdk.GetType() != desired_type)
263 return false;
264 return SDKSupportsModules(sdk.GetType(), sdk.GetVersion());
265}
266
268 using namespace llvm;
269 switch (triple.getOS()) {
270 case Triple::MacOSX:
271 case Triple::Darwin:
272 return XcodeSDK::MacOSX;
273 case Triple::IOS:
274 switch (triple.getEnvironment()) {
275 case Triple::MacABI:
276 return XcodeSDK::MacOSX;
277 case Triple::Simulator:
279 default:
280 return XcodeSDK::iPhoneOS;
281 }
282 case Triple::TvOS:
283 if (triple.getEnvironment() == Triple::Simulator)
285 return XcodeSDK::AppleTVOS;
286 case Triple::WatchOS:
287 if (triple.getEnvironment() == Triple::Simulator)
289 return XcodeSDK::watchOS;
290 case Triple::XROS:
291 if (triple.getEnvironment() == Triple::Simulator)
293 return XcodeSDK::XROS;
294 case Triple::Linux:
295 return XcodeSDK::Linux;
296 default:
297 return XcodeSDK::unknown;
298 }
299}
300
301std::string XcodeSDK::FindXcodeContentsDirectoryInPath(llvm::StringRef path) {
302 auto begin = llvm::sys::path::begin(path);
303 auto end = llvm::sys::path::end(path);
304
305 // Iterate over the path components until we find something that ends with
306 // .app. If the next component is Contents then we've found the Contents
307 // directory.
308 for (auto it = begin; it != end; ++it) {
309 if (it->ends_with(".app")) {
310 auto next = it;
311 if (++next != end && *next == "Contents") {
312 llvm::SmallString<128> buffer;
313 llvm::sys::path::append(buffer, begin, ++next,
314 llvm::sys::path::Style::posix);
315 return buffer.str().str();
316 }
317 }
318 }
319
320 return {};
321}
static llvm::VersionTuple ParseSDKVersion(llvm::StringRef &name)
Definition XcodeSDK.cpp:95
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition XcodeSDK.cpp:21
static bool ParseAppleInternalSDK(llvm::StringRef &name)
Definition XcodeSDK.cpp:112
static XcodeSDK::Type ParseSDKName(llvm::StringRef &name)
Definition XcodeSDK.cpp:67
A file utility class.
Definition FileSpec.h:56
llvm::StringRef GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:248
An abstraction for Xcode-style SDKs that works like ArchSpec.
Definition XcodeSDK.h:25
static constexpr int numSDKTypes
Definition XcodeSDK.h:45
Type
Different types of Xcode SDKs.
Definition XcodeSDK.h:31
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path)
Definition XcodeSDK.cpp:301
const FileSpec & GetSysroot() const
Definition XcodeSDK.cpp:145
void Merge(const XcodeSDK &other)
The merge function follows a strict order to maintain monotonicity:
Definition XcodeSDK.cpp:157
XcodeSDK & operator=(const XcodeSDK &other)
XcodeSDK()=default
Default constructor, constructs an empty string.
std::string m_name
Definition XcodeSDK.h:26
llvm::VersionTuple GetVersion() const
Definition XcodeSDK.cpp:132
llvm::StringRef GetString() const
Definition XcodeSDK.cpp:143
static std::string GetCanonicalName(Info info)
Return the canonical SDK name, such as "macosx" for the macOS SDK.
Definition XcodeSDK.cpp:185
static XcodeSDK::Type GetSDKTypeForTriple(const llvm::Triple &triple)
Return the best-matching SDK type for a specific triple.
Definition XcodeSDK.cpp:267
Info Parse() const
Return parsed SDK type and version number.
Definition XcodeSDK.cpp:116
bool operator==(const XcodeSDK &other) const
Definition XcodeSDK.cpp:63
bool IsAppleInternalSDK() const
Definition XcodeSDK.cpp:125
static bool SDKSupportsModules(Type type, llvm::VersionTuple version)
Whether LLDB feels confident importing Clang modules from this SDK.
Definition XcodeSDK.cpp:231
A class that represents a running process on the host machine.
A parsed SDK directory name.
Definition XcodeSDK.h:48
llvm::VersionTuple version
Definition XcodeSDK.h:50
bool operator<(const Info &other) const
Definition XcodeSDK.cpp:147
bool operator==(const Info &other) const
Definition XcodeSDK.cpp:152