Skip to content

Commit ecb186f

Browse files
committed
feat: P1 — add mcpp.platform module, centralize platform constants
New src/platform.cppm exports exe_suffix, static_lib_ext, shared_lib_ext, lib_prefix, null_redirect, is_windows/is_macos/is_linux. Refactored: plan.cppm, probe.cppm, clang.cppm, llvm.cppm now use mcpp::platform:: constants instead of scattered #if blocks.
1 parent c8f7086 commit ecb186f

5 files changed

Lines changed: 89 additions & 46 deletions

File tree

src/build/plan.cppm

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import mcpp.manifest;
1010
import mcpp.modgraph.graph;
1111
import mcpp.toolchain.detect;
1212
import mcpp.toolchain.fingerprint;
13+
import mcpp.platform;
1314

1415
export namespace mcpp::build {
1516

@@ -172,33 +173,23 @@ BuildPlan make_plan(const mcpp::manifest::Manifest& manifest,
172173
lu.targetName = t.name;
173174
if (t.kind == mcpp::manifest::Target::Library) {
174175
lu.kind = LinkUnit::StaticLibrary;
175-
#if defined(_WIN32)
176-
lu.output = std::filesystem::path("bin") / std::format("{}.lib", t.name);
177-
#else
178-
lu.output = std::filesystem::path("bin") / std::format("lib{}.a", t.name);
179-
#endif
176+
lu.output = std::filesystem::path("bin") /
177+
std::format("{}{}{}", mcpp::platform::lib_prefix, t.name,
178+
mcpp::platform::static_lib_ext);
180179
} else if (t.kind == mcpp::manifest::Target::SharedLibrary) {
181180
lu.kind = LinkUnit::SharedLibrary;
182-
#if defined(_WIN32)
183-
lu.output = std::filesystem::path("bin") / std::format("{}.dll", t.name);
184-
#else
185-
lu.output = std::filesystem::path("bin") / std::format("lib{}.so", t.name);
186-
#endif
181+
lu.output = std::filesystem::path("bin") /
182+
std::format("{}{}{}", mcpp::platform::lib_prefix, t.name,
183+
mcpp::platform::shared_lib_ext);
187184
} else if (t.kind == mcpp::manifest::Target::TestBinary) {
188185
lu.kind = LinkUnit::TestBinary;
189-
#if defined(_WIN32)
190-
lu.output = std::filesystem::path("bin") / (t.name + ".exe");
191-
#else
192-
lu.output = std::filesystem::path("bin") / t.name;
193-
#endif
186+
lu.output = std::filesystem::path("bin") /
187+
std::format("{}{}", t.name, mcpp::platform::exe_suffix);
194188
if (!t.main.empty()) lu.entryMain = projectRoot / t.main;
195189
} else {
196190
lu.kind = LinkUnit::Binary;
197-
#if defined(_WIN32)
198-
lu.output = std::filesystem::path("bin") / (t.name + ".exe");
199-
#else
200-
lu.output = std::filesystem::path("bin") / t.name;
201-
#endif
191+
lu.output = std::filesystem::path("bin") /
192+
std::format("{}{}", t.name, mcpp::platform::exe_suffix);
202193
if (!t.main.empty()) lu.entryMain = projectRoot / t.main;
203194
}
204195

src/platform.cppm

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// mcpp.platform — centralized platform-specific constants.
2+
//
3+
// Consumers import this module instead of scattering #if/_WIN32 / __APPLE__
4+
// blocks throughout the codebase. All compile-time branching lives here.
5+
6+
module;
7+
8+
// Nothing to #include for compile-time constants; the module fragment is kept
9+
// for future OS headers if needed.
10+
11+
export module mcpp.platform;
12+
13+
import std;
14+
15+
export namespace mcpp::platform {
16+
17+
// ── Binary / library name conventions ─────────────────────────────────────
18+
19+
#if defined(_WIN32)
20+
constexpr std::string_view exe_suffix = ".exe";
21+
constexpr std::string_view static_lib_ext = ".lib";
22+
constexpr std::string_view shared_lib_ext = ".dll";
23+
constexpr std::string_view lib_prefix = "";
24+
#elif defined(__APPLE__)
25+
constexpr std::string_view exe_suffix = "";
26+
constexpr std::string_view static_lib_ext = ".a";
27+
constexpr std::string_view shared_lib_ext = ".dylib";
28+
constexpr std::string_view lib_prefix = "lib";
29+
#else
30+
// Linux and other POSIX
31+
constexpr std::string_view exe_suffix = "";
32+
constexpr std::string_view static_lib_ext = ".a";
33+
constexpr std::string_view shared_lib_ext = ".so";
34+
constexpr std::string_view lib_prefix = "lib";
35+
#endif
36+
37+
// ── Shell / process helpers ────────────────────────────────────────────────
38+
39+
#if defined(_WIN32)
40+
constexpr std::string_view null_redirect = "2>nul";
41+
#else
42+
constexpr std::string_view null_redirect = "2>/dev/null";
43+
#endif
44+
45+
// ── Platform identification ────────────────────────────────────────────────
46+
47+
#if defined(_WIN32)
48+
constexpr bool is_windows = true;
49+
constexpr bool is_macos = false;
50+
constexpr bool is_linux = false;
51+
#elif defined(__APPLE__)
52+
constexpr bool is_windows = false;
53+
constexpr bool is_macos = true;
54+
constexpr bool is_linux = false;
55+
#elif defined(__linux__)
56+
constexpr bool is_windows = false;
57+
constexpr bool is_macos = false;
58+
constexpr bool is_linux = true;
59+
#else
60+
constexpr bool is_windows = false;
61+
constexpr bool is_macos = false;
62+
constexpr bool is_linux = false;
63+
#endif
64+
65+
} // namespace mcpp::platform

src/toolchain/clang.cppm

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import std;
66
import mcpp.toolchain.model;
77
import mcpp.toolchain.probe;
88
import mcpp.xlings;
9+
import mcpp.platform;
910

1011
export namespace mcpp::toolchain::clang {
1112

@@ -88,16 +89,11 @@ std::optional<std::filesystem::path> find_libcxx_std_module_source(
8889
const std::filesystem::path& cxx_binary,
8990
const std::string& envPrefix)
9091
{
91-
#if defined(_WIN32)
92-
constexpr auto kDevNull = "2>nul";
93-
#else
94-
constexpr auto kDevNull = "2>/dev/null";
95-
#endif
9692
auto manifest_r = mcpp::toolchain::run_capture(std::format(
9793
"{}{} -print-library-module-manifest-path {}",
9894
envPrefix,
9995
mcpp::xlings::shq(cxx_binary.string()),
100-
kDevNull));
96+
mcpp::platform::null_redirect));
10197
if (manifest_r) {
10298
auto manifestPath = std::filesystem::path(
10399
mcpp::toolchain::trim_line(*manifest_r));

src/toolchain/llvm.cppm

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export module mcpp.toolchain.llvm;
44

55
import std;
6+
import mcpp.platform;
67

78
export namespace mcpp::toolchain::llvm {
89

@@ -24,11 +25,7 @@ std::string package_name() {
2425
}
2526

2627
std::vector<std::string> frontend_candidates() {
27-
#if defined(_WIN32)
28-
return {"clang++.exe", "clang++"};
29-
#else
30-
return {"clang++"};
31-
#endif
28+
return {std::format("clang++{}", mcpp::platform::exe_suffix)};
3229
}
3330

3431
std::vector<std::string> list_aliases() {

src/toolchain/probe.cppm

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export module mcpp.toolchain.probe;
1313
import std;
1414
import mcpp.toolchain.model;
1515
import mcpp.xlings;
16+
import mcpp.platform;
1617

1718
export namespace mcpp::toolchain {
1819

@@ -250,9 +251,11 @@ probe_compiler_binary(const std::filesystem::path& explicit_compiler) {
250251
}
251252

252253
#if defined(_WIN32)
253-
auto bin_path_r = run_capture(std::format("where {} 2>nul", cxx));
254+
auto bin_path_r = run_capture(std::format("where {} {}", cxx,
255+
mcpp::platform::null_redirect));
254256
#else
255-
auto bin_path_r = run_capture(std::format("command -v '{}' 2>/dev/null", cxx));
257+
auto bin_path_r = run_capture(std::format("command -v '{}' {}", cxx,
258+
mcpp::platform::null_redirect));
256259
#endif
257260
if (!bin_path_r) {
258261
return std::unexpected(DetectError{std::format("compiler '{}' not found in PATH", cxx)});
@@ -268,38 +271,29 @@ probe_compiler_binary(const std::filesystem::path& explicit_compiler) {
268271
std::expected<std::string, DetectError>
269272
probe_target_triple(const std::filesystem::path& compilerBin,
270273
const std::string& envPrefix) {
271-
#if defined(_WIN32)
272-
constexpr auto kNullRedirect = "2>nul";
273-
#else
274-
constexpr auto kNullRedirect = "2>/dev/null";
275-
#endif
276274
auto triple_r = run_capture(std::format("{}{} -dumpmachine {}",
277275
envPrefix,
278276
mcpp::xlings::shq(compilerBin.string()),
279-
kNullRedirect));
277+
mcpp::platform::null_redirect));
280278
if (!triple_r) return std::unexpected(triple_r.error());
281279
return trim_line(*triple_r);
282280
}
283281

284282
std::filesystem::path
285283
probe_sysroot(const std::filesystem::path& compilerBin,
286284
const std::string& envPrefix) {
287-
#if defined(_WIN32)
288-
constexpr auto kNullRedir = "2>nul";
289-
#else
290-
constexpr auto kNullRedir = "2>/dev/null";
291-
#endif
292285
auto r = run_capture(std::format("{}{} -print-sysroot {}",
293286
envPrefix,
294287
mcpp::xlings::shq(compilerBin.string()),
295-
kNullRedir));
288+
mcpp::platform::null_redirect));
296289
if (r) {
297290
auto s = trim_line(*r);
298291
if (!s.empty() && std::filesystem::exists(s)) return s;
299292
}
300293
#if defined(__APPLE__)
301294
// macOS fallback: use xcrun to discover the SDK path
302-
auto xcrun_r = run_capture("xcrun --show-sdk-path 2>/dev/null");
295+
auto xcrun_r = run_capture(std::format("xcrun --show-sdk-path {}",
296+
mcpp::platform::null_redirect));
303297
if (xcrun_r) {
304298
auto sdk = trim_line(*xcrun_r);
305299
if (!sdk.empty() && std::filesystem::exists(sdk)) return sdk;

0 commit comments

Comments
 (0)