XcodeSDK.cpp
8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//===-- XcodeSDK.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/XcodeSDK.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/lldb-types.h"
#include "llvm/ADT/Triple.h"
#include <string>
using namespace lldb;
using namespace lldb_private;
static llvm::StringRef GetName(XcodeSDK::Type type) {
switch (type) {
case XcodeSDK::MacOSX:
return "MacOSX";
case XcodeSDK::iPhoneSimulator:
return "iPhoneSimulator";
case XcodeSDK::iPhoneOS:
return "iPhoneOS";
case XcodeSDK::AppleTVSimulator:
return "AppleTVSimulator";
case XcodeSDK::AppleTVOS:
return "AppleTVOS";
case XcodeSDK::WatchSimulator:
return "WatchSimulator";
case XcodeSDK::watchOS:
return "WatchOS";
case XcodeSDK::bridgeOS:
return "bridgeOS";
case XcodeSDK::Linux:
return "Linux";
case XcodeSDK::unknown:
return {};
}
llvm_unreachable("Unhandled sdk type!");
}
XcodeSDK::XcodeSDK(XcodeSDK::Info info) : m_name(GetName(info.type).str()) {
if (!m_name.empty()) {
if (!info.version.empty())
m_name += info.version.getAsString();
if (info.internal)
m_name += ".Internal";
m_name += ".sdk";
}
}
XcodeSDK &XcodeSDK::operator=(const XcodeSDK &other) {
m_name = other.m_name;
return *this;
}
bool XcodeSDK::operator==(const XcodeSDK &other) {
return m_name == other.m_name;
}
static XcodeSDK::Type ParseSDKName(llvm::StringRef &name) {
if (name.consume_front("MacOSX"))
return XcodeSDK::MacOSX;
if (name.consume_front("iPhoneSimulator"))
return XcodeSDK::iPhoneSimulator;
if (name.consume_front("iPhoneOS"))
return XcodeSDK::iPhoneOS;
if (name.consume_front("AppleTVSimulator"))
return XcodeSDK::AppleTVSimulator;
if (name.consume_front("AppleTVOS"))
return XcodeSDK::AppleTVOS;
if (name.consume_front("WatchSimulator"))
return XcodeSDK::WatchSimulator;
if (name.consume_front("WatchOS"))
return XcodeSDK::watchOS;
if (name.consume_front("bridgeOS"))
return XcodeSDK::bridgeOS;
if (name.consume_front("Linux"))
return XcodeSDK::Linux;
static_assert(XcodeSDK::Linux == XcodeSDK::numSDKTypes - 1,
"New SDK type was added, update this list!");
return XcodeSDK::unknown;
}
static llvm::VersionTuple ParseSDKVersion(llvm::StringRef &name) {
unsigned i = 0;
while (i < name.size() && name[i] >= '0' && name[i] <= '9')
++i;
if (i == name.size() || name[i++] != '.')
return {};
while (i < name.size() && name[i] >= '0' && name[i] <= '9')
++i;
if (i == name.size() || name[i++] != '.')
return {};
llvm::VersionTuple version;
version.tryParse(name.slice(0, i - 1));
name = name.drop_front(i);
return version;
}
static bool ParseAppleInternalSDK(llvm::StringRef &name) {
return name.consume_front("Internal.") || name.consume_front(".Internal.");
}
XcodeSDK::Info XcodeSDK::Parse() const {
XcodeSDK::Info info;
llvm::StringRef input(m_name);
info.type = ParseSDKName(input);
info.version = ParseSDKVersion(input);
info.internal = ParseAppleInternalSDK(input);
return info;
}
bool XcodeSDK::IsAppleInternalSDK() const {
llvm::StringRef input(m_name);
ParseSDKName(input);
ParseSDKVersion(input);
return ParseAppleInternalSDK(input);
}
llvm::VersionTuple XcodeSDK::GetVersion() const {
llvm::StringRef input(m_name);
ParseSDKName(input);
return ParseSDKVersion(input);
}
XcodeSDK::Type XcodeSDK::GetType() const {
llvm::StringRef input(m_name);
return ParseSDKName(input);
}
llvm::StringRef XcodeSDK::GetString() const { return m_name; }
bool XcodeSDK::Info::operator<(const Info &other) const {
return std::tie(type, version, internal) <
std::tie(other.type, other.version, other.internal);
}
bool XcodeSDK::Info::operator==(const Info &other) const {
return std::tie(type, version, internal) ==
std::tie(other.type, other.version, other.internal);
}
void XcodeSDK::Merge(const XcodeSDK &other) {
// The "bigger" SDK always wins.
auto l = Parse();
auto r = other.Parse();
if (l < r)
*this = other;
else {
// The Internal flag always wins.
if (llvm::StringRef(m_name).endswith(".sdk"))
if (!l.internal && r.internal)
m_name =
m_name.substr(0, m_name.size() - 3) + std::string("Internal.sdk");
}
}
std::string XcodeSDK::GetCanonicalName(XcodeSDK::Info info) {
std::string name;
switch (info.type) {
case MacOSX:
name = "macosx";
break;
case iPhoneSimulator:
name = "iphonesimulator";
break;
case iPhoneOS:
name = "iphoneos";
break;
case AppleTVSimulator:
name = "appletvsimulator";
break;
case AppleTVOS:
name = "appletvos";
break;
case WatchSimulator:
name = "watchsimulator";
break;
case watchOS:
name = "watchos";
break;
case bridgeOS:
name = "bridgeos";
break;
case Linux:
name = "linux";
break;
case unknown:
return {};
}
if (!info.version.empty())
name += info.version.getAsString();
if (info.internal)
name += ".internal";
return name;
}
bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type sdk_type,
llvm::VersionTuple version) {
switch (sdk_type) {
case Type::MacOSX:
return version >= llvm::VersionTuple(10, 10);
case Type::iPhoneOS:
case Type::iPhoneSimulator:
case Type::AppleTVOS:
case Type::AppleTVSimulator:
return version >= llvm::VersionTuple(8);
case Type::watchOS:
case Type::WatchSimulator:
return version >= llvm::VersionTuple(6);
default:
return false;
}
return false;
}
bool XcodeSDK::SupportsSwift() const {
XcodeSDK::Info info = Parse();
switch (info.type) {
case Type::MacOSX:
return info.version.empty() || info.version >= llvm::VersionTuple(10, 10);
case Type::iPhoneOS:
case Type::iPhoneSimulator:
return info.version.empty() || info.version >= llvm::VersionTuple(8);
case Type::AppleTVSimulator:
case Type::AppleTVOS:
return info.version.empty() || info.version >= llvm::VersionTuple(9);
case Type::WatchSimulator:
case Type::watchOS:
return info.version.empty() || info.version >= llvm::VersionTuple(2);
case Type::Linux:
return true;
default:
return false;
}
}
bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type desired_type,
const FileSpec &sdk_path) {
ConstString last_path_component = sdk_path.GetLastPathComponent();
if (!last_path_component)
return false;
XcodeSDK sdk(last_path_component.GetStringRef().str());
if (sdk.GetType() != desired_type)
return false;
return SDKSupportsModules(sdk.GetType(), sdk.GetVersion());
}
XcodeSDK::Type XcodeSDK::GetSDKTypeForTriple(const llvm::Triple &triple) {
using namespace llvm;
switch (triple.getOS()) {
case Triple::MacOSX:
case Triple::Darwin:
return XcodeSDK::MacOSX;
case Triple::IOS:
switch (triple.getEnvironment()) {
case Triple::MacABI:
return XcodeSDK::MacOSX;
case Triple::Simulator:
return XcodeSDK::iPhoneSimulator;
default:
return XcodeSDK::iPhoneOS;
}
case Triple::TvOS:
if (triple.getEnvironment() == Triple::Simulator)
return XcodeSDK::AppleTVSimulator;
return XcodeSDK::AppleTVOS;
case Triple::WatchOS:
if (triple.getEnvironment() == Triple::Simulator)
return XcodeSDK::WatchSimulator;
return XcodeSDK::watchOS;
case Triple::Linux:
return XcodeSDK::Linux;
default:
return XcodeSDK::unknown;
}
}
std::string XcodeSDK::FindXcodeContentsDirectoryInPath(llvm::StringRef path) {
auto begin = llvm::sys::path::begin(path);
auto end = llvm::sys::path::end(path);
// Iterate over the path components until we find something that ends with
// .app. If the next component is Contents then we've found the Contents
// directory.
for (auto it = begin; it != end; ++it) {
if (it->endswith(".app")) {
auto next = it;
if (++next != end && *next == "Contents") {
llvm::SmallString<128> buffer;
llvm::sys::path::append(buffer, begin, ++next,
llvm::sys::path::Style::posix);
return buffer.str().str();
}
}
}
return {};
}