-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmacos.cppm
More file actions
102 lines (84 loc) · 2.9 KB
/
macos.cppm
File metadata and controls
102 lines (84 loc) · 2.9 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
// mcpp.platform.macos — macOS-specific platform capabilities.
//
// Provides:
// has_xcode_clt() — detect Xcode Command Line Tools
// sdk_path() — discover macOS SDK via xcrun
// runtime_lib_dirs() — macOS-specific library search paths
// supports_full_static — macOS cannot fully static-link (libSystem)
module;
#include <cstdio>
#include <cstdlib>
#if defined(_WIN32)
#define popen _popen
#define pclose _pclose
#endif
export module mcpp.platform.macos;
import std;
export namespace mcpp::platform::macos {
// Whether macOS supports full static linking (it does not — libSystem
// must be dynamically linked).
constexpr bool supports_full_static = false;
// Check whether Xcode Command Line Tools are installed.
// Returns true if `xcode-select -p` succeeds.
bool has_xcode_clt();
// Discover the macOS SDK path via `xcrun --show-sdk-path`.
// Returns the SDK path if found, or nullopt.
std::optional<std::filesystem::path> sdk_path();
// Return macOS-specific runtime library directories for LLVM toolchains.
std::vector<std::filesystem::path>
runtime_lib_dirs(const std::filesystem::path& toolchain_root);
} // namespace mcpp::platform::macos
// ─── Implementation ──────────────────────────────────────────────────────
namespace mcpp::platform::macos {
namespace {
std::string run_capture_trimmed(const std::string& cmd) {
std::array<char, 4096> buf{};
std::string out;
#if defined(__APPLE__)
std::string full = cmd + " </dev/null";
std::FILE* fp = ::popen(full.c_str(), "r");
if (!fp) return {};
while (std::fgets(buf.data(), static_cast<int>(buf.size()), fp) != nullptr)
out += buf.data();
::pclose(fp);
while (!out.empty() && (out.back() == '\n' || out.back() == '\r' || out.back() == ' '))
out.pop_back();
#else
(void)cmd;
(void)buf;
#endif
return out;
}
} // namespace
bool has_xcode_clt() {
#if defined(__APPLE__)
int rc = std::system("xcode-select -p </dev/null >/dev/null 2>&1");
return rc == 0;
#else
return false;
#endif
}
std::optional<std::filesystem::path> sdk_path() {
#if defined(__APPLE__)
auto result = run_capture_trimmed("xcrun --show-sdk-path 2>/dev/null");
if (!result.empty() && std::filesystem::exists(result))
return std::filesystem::path(result);
#endif
return std::nullopt;
}
std::vector<std::filesystem::path>
runtime_lib_dirs(const std::filesystem::path& toolchain_root) {
std::vector<std::filesystem::path> dirs;
#if defined(__APPLE__)
auto add = [&](const std::filesystem::path& p) {
if (std::filesystem::exists(p))
dirs.push_back(p);
};
add(toolchain_root / "lib" / "aarch64-apple-darwin");
add(toolchain_root / "lib" / "darwin");
#else
(void)toolchain_root;
#endif
return dirs;
}
} // namespace mcpp::platform::macos