Skip to content

Commit 078873d

Browse files
committed
fix: override stale cfg sysroot with xcrun SDK for macOS std precompile
The previous fix skipped --sysroot entirely on macOS, but xlings LLVM's clang++.cfg still applies its own --sysroot pointing to CommandLineTools SDK. When the active SDK is Xcode (different path), the cfg's stale sysroot causes _CTYPE_A undeclared errors. Fix: on macOS, always probe xcrun and pass the active SDK as --sysroot to OVERRIDE the cfg-baked stale path. This ensures the std module precompile uses the correct SDK regardless of what was baked into clang++.cfg at LLVM install time.
1 parent 1838fb2 commit 078873d

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

src/toolchain/stdmod.cppm

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,21 @@ std::expected<StdModule, StdModError> ensure_built(
9393
sm.objectPath = sm.cacheDir / "std.o";
9494

9595
std::string sysroot_flag;
96-
if (!tc.sysroot.empty()) {
97-
// On macOS, skip --sysroot for std module precompilation.
98-
// xlings LLVM ships a clang++.cfg with a --sysroot that is tuned to
99-
// work for normal compilation. Adding a second --sysroot (even the
100-
// identical value) changes header include ordering and breaks macOS
101-
// SDK internal dependencies — ___wctype.h references _CTYPE_A which
102-
// is defined in _ctype.h, but the duplicate --sysroot flag causes
103-
// the C runtime header to not be included transitively during module
104-
// purview compilation. Let the cfg's own --sysroot handle it.
105-
bool skip_sysroot = tc.targetTriple.find("apple") != std::string::npos
106-
|| tc.targetTriple.find("darwin") != std::string::npos;
107-
if (!skip_sysroot)
108-
sysroot_flag = std::format(" --sysroot='{}'", tc.sysroot.string());
96+
bool is_macos_target = tc.targetTriple.find("apple") != std::string::npos
97+
|| tc.targetTriple.find("darwin") != std::string::npos;
98+
if (is_macos_target) {
99+
// macOS: always pass the *active* SDK path (from xcrun) to override
100+
// any stale --sysroot baked into clang++.cfg by xlings at install
101+
// time. The cfg path may point to CommandLineTools SDK while the
102+
// runner has Xcode active, or vice versa. A mismatched SDK causes
103+
// ___wctype.h → _CTYPE_A undeclared errors during std module
104+
// precompilation because the SDK's internal C headers reference
105+
// macros defined in a sibling header that the wrong SDK doesn't
106+
// include transitively.
107+
if (auto sdk = mcpp::platform::macos::sdk_path())
108+
sysroot_flag = std::format(" --sysroot='{}'", sdk->string());
109+
} else if (!tc.sysroot.empty()) {
110+
sysroot_flag = std::format(" --sysroot='{}'", tc.sysroot.string());
109111
}
110112

111113
bool std_cached = std::filesystem::exists(sm.bmiPath) && std::filesystem::exists(sm.objectPath);

0 commit comments

Comments
 (0)