-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolchain_profile.cpp
More file actions
377 lines (342 loc) · 13.3 KB
/
toolchain_profile.cpp
File metadata and controls
377 lines (342 loc) · 13.3 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "toolchain_profile.h"
#include "abi/target_info.h"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <filesystem>
#include <string>
#include <unordered_set>
namespace {
std::string to_lower_ascii(std::string_view value) {
std::string lowered(value);
std::transform(lowered.begin(), lowered.end(), lowered.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return lowered;
}
TargetOS target_os_from_triple(std::string_view triple) {
const std::string lowered = to_lower_ascii(triple);
if (lowered.empty()) {
return TargetInfo::create_host()->os;
}
if (lowered.find("darwin") != std::string::npos ||
lowered.find("apple") != std::string::npos ||
lowered.find("macos") != std::string::npos) {
return TargetOS::MACOS;
}
if (lowered.find("linux") != std::string::npos) {
return TargetOS::LINUX;
}
if (lowered.find("windows") != std::string::npos ||
lowered.find("mingw") != std::string::npos ||
lowered.find("msvc") != std::string::npos) {
return TargetOS::WINDOWS;
}
if (lowered.find("freebsd") != std::string::npos) {
return TargetOS::FREEBSD;
}
return TargetOS::NONE;
}
std::string normalize_candidate_path(const std::filesystem::path& path) {
std::error_code ec;
auto normalized = std::filesystem::weakly_canonical(path, ec);
if (!ec) {
return normalized.string();
}
return path.lexically_normal().string();
}
std::filesystem::path resolve_installed_dir(const char* argv0) {
if (argv0 == nullptr || *argv0 == '\0') {
return std::filesystem::path(".");
}
std::error_code ec;
auto absolute_path = std::filesystem::absolute(std::filesystem::path(argv0), ec);
if (ec) {
absolute_path = std::filesystem::path(argv0);
}
auto parent = absolute_path.lexically_normal().parent_path();
if (parent.empty()) {
return std::filesystem::path(".");
}
return parent;
}
void maybe_record_candidate(const std::filesystem::path& candidate,
std::vector<std::string>& attempted_paths,
std::unordered_set<std::string>& attempted_seen,
std::vector<std::string>& include_paths,
std::unordered_set<std::string>& include_seen) {
if (candidate.empty()) {
return;
}
const std::string normalized = normalize_candidate_path(candidate);
if (attempted_seen.insert(normalized).second) {
attempted_paths.push_back(normalized);
}
std::error_code ec;
if (!std::filesystem::exists(candidate, ec) ||
!std::filesystem::is_directory(candidate, ec)) {
return;
}
if (include_seen.insert(normalized).second) {
include_paths.push_back(normalized);
}
}
void add_latest_darwin_sdk_candidate(const std::filesystem::path& sdk_dir,
std::vector<std::string>& attempted_paths,
std::unordered_set<std::string>& attempted_seen,
std::vector<std::string>& include_paths,
std::unordered_set<std::string>& include_seen) {
std::error_code ec;
if (!std::filesystem::exists(sdk_dir, ec) || !std::filesystem::is_directory(sdk_dir, ec)) {
return;
}
const std::filesystem::path default_sdk = sdk_dir / "MacOSX.sdk";
if (std::filesystem::exists(default_sdk, ec)) {
maybe_record_candidate(default_sdk / "usr" / "include" / "c++" / "v1",
attempted_paths, attempted_seen, include_paths, include_seen);
return;
}
std::vector<std::filesystem::path> candidates;
for (const auto& entry : std::filesystem::directory_iterator(sdk_dir, ec)) {
if (ec) {
break;
}
if (!entry.is_directory(ec)) {
continue;
}
auto path = entry.path();
const std::string name = path.filename().string();
if (name.rfind("MacOSX", 0) == 0 && path.extension() == ".sdk") {
candidates.push_back(path);
}
}
if (candidates.empty()) {
return;
}
std::sort(candidates.begin(), candidates.end());
maybe_record_candidate(candidates.back() / "usr" / "include" / "c++" / "v1",
attempted_paths, attempted_seen, include_paths, include_seen);
}
void add_darwin_libcxx_candidates(const char* argv0,
std::vector<std::string>& attempted_paths,
std::unordered_set<std::string>& attempted_seen,
std::vector<std::string>& include_paths,
std::unordered_set<std::string>& include_seen) {
const std::filesystem::path installed_dir = resolve_installed_dir(argv0);
maybe_record_candidate(installed_dir.parent_path() / "include" / "c++" / "v1",
attempted_paths, attempted_seen, include_paths, include_seen);
maybe_record_candidate(installed_dir / "include" / "c++" / "v1",
attempted_paths, attempted_seen, include_paths, include_seen);
if (const char* sdkroot = std::getenv("SDKROOT")) {
maybe_record_candidate(std::filesystem::path(sdkroot) / "usr" / "include" / "c++" / "v1",
attempted_paths, attempted_seen, include_paths, include_seen);
}
add_latest_darwin_sdk_candidate("/Library/Developer/CommandLineTools/SDKs",
attempted_paths, attempted_seen, include_paths, include_seen);
add_latest_darwin_sdk_candidate(
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs",
attempted_paths,
attempted_seen,
include_paths,
include_seen);
maybe_record_candidate("/Library/Developer/CommandLineTools/usr/include/c++/v1",
attempted_paths, attempted_seen, include_paths, include_seen);
maybe_record_candidate(
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
attempted_paths,
attempted_seen,
include_paths,
include_seen);
}
bool try_add_include_path(const std::filesystem::path& path,
std::vector<std::string>& paths,
std::unordered_set<std::string>& seen) {
if (path.empty()) {
return false;
}
std::error_code ec;
if (!std::filesystem::exists(path, ec)) {
return false;
}
auto norm = std::filesystem::weakly_canonical(path, ec);
std::string norm_str = ec ? path.lexically_normal().string() : norm.string();
if (!seen.insert(norm_str).second) {
return false;
}
paths.push_back(norm_str);
return true;
}
void add_sdk_include_from_root(const std::filesystem::path& sdk_root,
std::vector<std::string>& paths,
std::unordered_set<std::string>& seen) {
auto add_subframework_roots = [&](const std::filesystem::path& frameworks_root) {
std::error_code ec;
if (!std::filesystem::exists(frameworks_root, ec) ||
!std::filesystem::is_directory(frameworks_root, ec)) {
return;
}
for (const auto& entry : std::filesystem::directory_iterator(frameworks_root, ec)) {
if (ec) break;
if (!entry.is_directory(ec)) continue;
auto framework_path = entry.path();
if (framework_path.extension() != ".framework") continue;
(void)try_add_include_path(framework_path / "Frameworks", paths, seen);
}
};
if (sdk_root.empty()) {
return;
}
(void)try_add_include_path(sdk_root / "usr" / "include", paths, seen);
std::filesystem::path frameworks_root = sdk_root / "System" / "Library" / "Frameworks";
std::filesystem::path private_frameworks_root =
sdk_root / "System" / "Library" / "PrivateFrameworks";
(void)try_add_include_path(frameworks_root, paths, seen);
(void)try_add_include_path(private_frameworks_root, paths, seen);
add_subframework_roots(frameworks_root);
add_subframework_roots(private_frameworks_root);
}
void add_sdk_from_dir(const std::filesystem::path& sdk_dir,
std::vector<std::string>& paths,
std::unordered_set<std::string>& seen) {
std::error_code ec;
if (!std::filesystem::exists(sdk_dir, ec) || !std::filesystem::is_directory(sdk_dir, ec)) {
return;
}
std::filesystem::path default_sdk = sdk_dir / "MacOSX.sdk";
if (std::filesystem::exists(default_sdk, ec)) {
add_sdk_include_from_root(default_sdk, paths, seen);
return;
}
std::vector<std::filesystem::path> candidates;
for (const auto& entry : std::filesystem::directory_iterator(sdk_dir, ec)) {
if (ec) break;
if (!entry.is_directory(ec)) continue;
auto path = entry.path();
const std::string name = path.filename().string();
if (name.rfind("MacOSX", 0) == 0 && path.extension() == ".sdk") {
candidates.push_back(path);
}
}
if (candidates.empty()) {
return;
}
std::sort(candidates.begin(), candidates.end());
add_sdk_include_from_root(candidates.back(), paths, seen);
}
void discover_clang_resource_includes(std::vector<std::string>& paths,
std::unordered_set<std::string>& seen) {
const std::filesystem::path bases[] = {
"/Library/Developer/CommandLineTools/usr/lib/clang",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang",
};
for (const auto& base : bases) {
std::error_code ec;
if (!std::filesystem::exists(base, ec) || !std::filesystem::is_directory(base, ec)) {
continue;
}
std::vector<std::filesystem::path> versions;
for (const auto& entry : std::filesystem::directory_iterator(base, ec)) {
if (ec) break;
if (!entry.is_directory(ec)) continue;
auto include_dir = entry.path() / "include";
if (std::filesystem::exists(include_dir, ec)) {
versions.push_back(include_dir);
}
}
if (!versions.empty()) {
std::sort(versions.begin(), versions.end());
(void)try_add_include_path(versions.back(), paths, seen);
return;
}
}
}
} // namespace
std::optional<StdLibKind> parse_stdlib_kind(std::string_view value) {
const std::string lowered = to_lower_ascii(value);
if (lowered == "auto") {
return StdLibKind::Auto;
}
if (lowered == "libc++") {
return StdLibKind::LibCxx;
}
if (lowered == "libstdc++") {
return StdLibKind::LibStdCxx;
}
return std::nullopt;
}
std::string stdlib_kind_name(StdLibKind kind) {
switch (kind) {
case StdLibKind::Auto:
return "auto";
case StdLibKind::LibCxx:
return "libc++";
case StdLibKind::LibStdCxx:
return "libstdc++";
}
return "auto";
}
std::string default_target_triple() {
#if defined(__APPLE__) && defined(__aarch64__)
return "aarch64-apple-darwin";
#elif defined(__APPLE__) && defined(__arm64__)
return "arm64-apple-darwin";
#elif defined(__APPLE__) && defined(__x86_64__)
return "x86_64-apple-darwin";
#elif defined(__linux__) && defined(__aarch64__)
return "aarch64-unknown-linux-gnu";
#elif defined(__linux__) && defined(__x86_64__)
return "x86_64-unknown-linux-gnu";
#else
return "unknown-unknown-unknown";
#endif
}
CxxStdlibDiscoveryResult discover_cxx_stdlib_include_paths(
const char* argv0,
std::string_view target_triple,
bool cxx_mode,
StdLibKind requested_kind) {
CxxStdlibDiscoveryResult result;
result.requested = requested_kind;
result.resolved = requested_kind;
if (!cxx_mode) {
return result;
}
const TargetOS target_os = target_os_from_triple(target_triple);
if (requested_kind == StdLibKind::Auto) {
if (target_os == TargetOS::MACOS) {
result.resolved = StdLibKind::LibCxx;
} else {
return result;
}
}
std::unordered_set<std::string> attempted_seen;
std::unordered_set<std::string> include_seen;
if (target_os == TargetOS::MACOS && result.resolved == StdLibKind::LibCxx) {
add_darwin_libcxx_candidates(argv0,
result.attempted_paths,
attempted_seen,
result.include_paths,
include_seen);
// Use one active libc++ root. Keeping multiple libc++ wrapper roots in the
// search list breaks headers like <stdint.h> that rely on include_next to
// drop from the C++ wrapper layer to the C header layer.
if (result.include_paths.size() > 1) {
result.include_paths.resize(1);
}
}
return result;
}
std::vector<std::string> discover_macos_sdk_include_paths(const char* argv0) {
(void)argv0;
std::vector<std::string> paths;
std::unordered_set<std::string> seen;
if (const char* sdkroot = std::getenv("SDKROOT")) {
add_sdk_include_from_root(std::filesystem::path(sdkroot), paths, seen);
}
add_sdk_from_dir("/Library/Developer/CommandLineTools/SDKs", paths, seen);
add_sdk_from_dir(
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs",
paths,
seen);
discover_clang_resource_includes(paths, seen);
return paths;
}