Skip to content

Commit 9c95d01

Browse files
authored
refactor(macos): single deployment-target resolver shared by flags, fingerprint and stdmod (#119)
platform::macos::deployment_target(manifestValue) is now THE resolution point (env override > manifest default > empty). Consumed by flags.cppm (compile+link -mmacosx-version-min), the BMI fingerprint rule, and — NEW — the std-module prebuild (stdmod previously assembled its compile flags with no deployment-target concept, so manifest-resolved targets split the std.pcm triple from the importing TUs; env-only setups got lucky via the clang driver reading the env in both places). Prerequisite for re-landing the built-in default floor (TODO(macos-default-floor)). No behavior change for env-only or unconfigured builds.
1 parent 4323a40 commit 9c95d01

4 files changed

Lines changed: 41 additions & 13 deletions

File tree

src/build/flags.cppm

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,8 @@ CompileFlags compute_flags(const BuildPlan& plan) {
103103
// override, the convention cargo/rustc/cc honor) > the manifest's
104104
// [build] macos_deployment_target (project default, SwiftPM-style) >
105105
// empty (toolchain/SDK default).
106-
std::string macosDeploymentTarget;
107-
if (const char* dt = std::getenv("MACOSX_DEPLOYMENT_TARGET"); dt && *dt) {
108-
macosDeploymentTarget = dt;
109-
} else {
110-
macosDeploymentTarget = plan.manifest.buildConfig.macosDeploymentTarget;
111-
}
106+
std::string macosDeploymentTarget = mcpp::platform::macos::deployment_target(
107+
plan.manifest.buildConfig.macosDeploymentTarget);
112108

113109
f.cxxBinary = plan.toolchain.binaryPath;
114110
f.ccBinary = mcpp::toolchain::derive_c_compiler(plan.toolchain);

src/cli.cppm

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,8 @@ std::string canonical_compile_flags(const mcpp::manifest::Manifest& m) {
602602
// Centralize the resolution in one helper, then re-land.
603603
// See xlings .agents/docs/2026-06-05-macos-min-version-support.md §5.
604604
if constexpr (mcpp::platform::is_macos) {
605-
std::string dtv;
606-
if (const char* dt = std::getenv("MACOSX_DEPLOYMENT_TARGET"); dt && *dt) {
607-
dtv = dt;
608-
} else {
609-
dtv = m.buildConfig.macosDeploymentTarget;
610-
}
605+
auto dtv = mcpp::platform::macos::deployment_target(
606+
m.buildConfig.macosDeploymentTarget);
611607
if (!dtv.empty()) {
612608
s += " macos_deployment_target=";
613609
s += dtv;
@@ -3334,7 +3330,9 @@ prepare_build(bool print_fingerprint,
33343330
std::filesystem::path stdCompatObjectPath;
33353331
if (needsStdModule) {
33363332
auto sm = mcpp::toolchain::ensure_built(
3337-
*tc, fp.hex, m->package.standard, m->cppStandard.flag);
3333+
*tc, fp.hex, m->package.standard, m->cppStandard.flag,
3334+
mcpp::platform::macos::deployment_target(
3335+
m->buildConfig.macosDeploymentTarget));
33383336
if (!sm) return std::unexpected(sm.error().message);
33393337
stdBmiPath = sm->bmiPath;
33403338
stdObjectPath = sm->objectPath;

src/platform/macos.cppm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,28 @@ bool has_xcode_clt();
3232
// Returns the SDK path if found, or nullopt.
3333
std::optional<std::filesystem::path> sdk_path();
3434

35+
// Resolve the effective macOS deployment target: the
36+
// MACOSX_DEPLOYMENT_TARGET env var (explicit per-invocation override,
37+
// the convention cargo/rustc/cc honor) wins over `manifestValue` (the
38+
// [build] macos_deployment_target project default); empty means
39+
// toolchain/SDK default. THE single source of truth — flags.cppm, the
40+
// BMI fingerprint rule and the std-module prebuild must all consume
41+
// this same resolution, or cached std.pcm modules drift from the TUs
42+
// (config-mismatch / unstaged-module failures observed on macos CI).
43+
std::string deployment_target(std::string_view manifestValue);
44+
3545
// Return macOS-specific runtime library directories for LLVM toolchains.
46+
std::string deployment_target(std::string_view manifestValue) {
47+
#if defined(__APPLE__)
48+
if (const char* dt = std::getenv("MACOSX_DEPLOYMENT_TARGET"); dt && *dt)
49+
return dt;
50+
return std::string(manifestValue);
51+
#else
52+
(void)manifestValue;
53+
return {};
54+
#endif
55+
}
56+
3657
std::vector<std::filesystem::path>
3758
runtime_lib_dirs(const std::filesystem::path& toolchain_root);
3859

src/toolchain/stdmod.cppm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ struct StdModError { std::string message; };
4545
std::filesystem::path default_cache_root();
4646

4747
// Build std module if not already cached. Returns paths to BMI + object.
48+
// `macos_deployment_target` is the RESOLVED value from
49+
// platform::macos::deployment_target() — it must match what flags.cppm
50+
// emits for normal TUs, or the produced std.pcm targets a different
51+
// arm64-apple-macosxNN triple than the code importing it.
4852
std::expected<StdModule, StdModError> ensure_built(
4953
const Toolchain& tc,
5054
std::string_view fingerprint_hex,
5155
std::string_view cpp_standard,
5256
std::string_view cpp_standard_flag,
57+
std::string_view macos_deployment_target = {},
5358
const std::filesystem::path& cache_root = default_cache_root());
5459

5560
} // namespace mcpp::toolchain
@@ -174,6 +179,7 @@ std::expected<StdModule, StdModError> ensure_built(
174179
std::string_view fingerprint_hex,
175180
std::string_view cpp_standard,
176181
std::string_view cpp_standard_flag,
182+
std::string_view macos_deployment_target,
177183
const std::filesystem::path& cache_root)
178184
{
179185
if (tc.stdModuleSource.empty()) {
@@ -253,6 +259,13 @@ std::expected<StdModule, StdModError> ensure_built(
253259
add_inc(tc.payloadPaths->linuxInclude);
254260
}
255261

262+
// Deployment target must mirror what flags.cppm emits for normal TUs
263+
// (single resolver: platform::macos::deployment_target).
264+
if (!macos_deployment_target.empty()) {
265+
sysroot_flag += std::format(" -mmacosx-version-min={}",
266+
macos_deployment_target);
267+
}
268+
256269
std::vector<std::string> stdCommands;
257270
if (is_clang(tc)) {
258271
stdCommands = mcpp::toolchain::clang::std_module_build_commands(

0 commit comments

Comments
 (0)