Skip to content

Commit e345067

Browse files
committed
fix: prefer xcrun SDK over cfg-baked sysroot on macOS
probe_sysroot() called -print-sysroot first, which on macOS with xlings LLVM just echoed back the --sysroot from clang++.cfg. That cfg-baked path points to CommandLineTools SDK, which may differ from the active Xcode SDK. When the two SDKs have different header versions, std module precompilation fails with undeclared _CTYPE_A and related C runtime macro errors. Fix: on macOS, probe xcrun --show-sdk-path FIRST (always returns the active SDK), then fall back to -print-sysroot for non-macOS. The xcrun sysroot passed on the command line overrides the cfg-baked one.
1 parent 722cb0e commit e345067

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

src/toolchain/probe.cppm

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ probe_target_triple(const std::filesystem::path& compilerBin,
254254
std::filesystem::path
255255
probe_sysroot(const std::filesystem::path& compilerBin,
256256
const std::string& envPrefix) {
257+
// macOS: prefer xcrun's SDK path over -print-sysroot.
258+
// xlings LLVM bakes --sysroot into clang++.cfg at install time, and
259+
// -print-sysroot echoes that back. If the CLT/Xcode SDK was updated
260+
// after the LLVM package was cached, the cfg-baked path may be stale
261+
// or point to an incompatible SDK. xcrun always returns the currently
262+
// active SDK, so mcpp passes it on the command line to override the cfg.
263+
if (auto sdk = mcpp::platform::macos::sdk_path())
264+
return *sdk;
265+
257266
auto r = run_capture(std::format("{}{} -print-sysroot {}",
258267
envPrefix,
259268
mcpp::xlings::shq(compilerBin.string()),
@@ -262,17 +271,6 @@ probe_sysroot(const std::filesystem::path& compilerBin,
262271
auto s = trim_line(*r);
263272
if (!s.empty() && std::filesystem::exists(s)) return s;
264273
}
265-
// macOS: do NOT fall back to xcrun SDK path for xlings-installed LLVM.
266-
// xlings LLVM uses -nostdinc++ (via clang++.cfg) to provide its own
267-
// libc++ headers. Combining --sysroot=<MacOSX.sdk> with -nostdinc++
268-
// breaks C runtime header resolution (_CTYPE_A, memcpy, errno etc.
269-
// become undeclared) because the SDK headers assume the C runtime
270-
// macros are available via default include paths, which --sysroot
271-
// overrides.
272-
//
273-
// If the compiler itself reports a sysroot (-print-sysroot), use it —
274-
// that's an explicit choice by the toolchain. But the xcrun SDK is
275-
// meant for Apple's system clang, not for standalone xlings LLVM.
276274
return {};
277275
}
278276

0 commit comments

Comments
 (0)