Skip to content

Commit 8d0afd7

Browse files
refactor: every unit reaches the system through mcpplibs.os; no header, no preprocessor branch
The rules and dist members ask programs for facts through mcpp::plugins::tool::capture (argv, no shell), the CUDA rule opens the driver through os::library, the test kit states each case's environment in its spawn options, and platform differences are if constexpr. CI refuses an #include or #if in a unit. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 37497fc commit 8d0afd7

18 files changed

Lines changed: 140 additions & 379 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ jobs:
222222
fi
223223
echo "ok: one version, two files agreeing"
224224
225+
# NO HEADER AND NO PREPROCESSOR BRANCH (0.21.0, docs/plugin-development.md
226+
# §4): the operating system is mcpplibs.os's, the C library std's, and a
227+
# platform difference of the building machine is `if constexpr`.
228+
- name: no unit includes a header or branches in the preprocessor
229+
run: |
230+
found=$(grep -nE '^[[:space:]]*#[[:space:]]*(include|if|ifdef|ifndef|elif)\b' \
231+
src/*.cppm src/*/*.cppm rules/*.cppm dist/*.cppm deps/*.cppm deps/*/*.cppm tools/*.cppm 2>/dev/null || true)
232+
[ -z "$found" ] || { echo "FAIL: a unit includes a header or branches in the preprocessor"; echo "$found"; exit 1; }
233+
echo "ok: every unit reaches the system through modules"
234+
225235
# ONE DERIVATION, AND THE CHECK THAT KEEPS IT ONE.
226236
#
227237
# `common_base_dir` and `namespace_of` were written in `rules/spirv.cppm`

‎dist/apk.cppm‎

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@
9696
// caller's configuration, not this member's (rule 4, 2026-09-12 design
9797
// record: "a name is cache-safe, a path is not").
9898

99-
module;
100-
#include <cstdio>
101-
#if !defined(_WIN32)
102-
#include <sys/wait.h>
103-
#endif
104-
10599
export module mcpp.dist.apk;
106100

107101
import std;
@@ -790,47 +784,15 @@ inline std::string sha256_file(const std::string& path) {
790784

791785
// ─── Running a tool at plan time ─────────────────────────────────────────
792786

793-
// POSIX single quotes: the only character that needs care inside them is the
794-
// quote itself. This member runs on the hosts that build Android rows, which
795-
// are Linux and macOS (xim:android-ndk publishes no Windows table).
796-
inline std::string shell_quote(const std::string& s) {
797-
std::string out = "'";
798-
for (std::size_t i = 0; i < s.size(); ++i) {
799-
if (s[i] == '\'') out += "'\\''";
800-
else out += s[i];
801-
}
802-
return out + "'";
803-
}
804-
805-
// Runs `argv` through `/bin/sh`, returning its exit status and its combined
806-
// output -- the same `popen` the rules members use to probe a tool at plan
807-
// time. Only the explicit Maven modes and AAR extraction call it; an ordinary
808-
// build of an unchanged project runs nothing here.
809-
inline int run_capture(const std::vector<std::string>& argv, std::string& output) {
810-
std::string cmd;
811-
for (std::size_t i = 0; i < argv.size(); ++i) {
812-
if (i) cmd += ' ';
813-
cmd += shell_quote(argv[i]);
814-
}
815-
cmd += " 2>&1";
816-
output.clear();
817-
#if defined(_WIN32)
818-
FILE* p = ::_popen(cmd.c_str(), "r");
819-
#else
820-
FILE* p = ::popen(cmd.c_str(), "r");
821-
#endif
822-
if (!p) return -1;
823-
char buf[4096];
824-
std::size_t n;
825-
while ((n = std::fread(buf, 1, sizeof buf, p)) > 0) output.append(buf, n);
826-
#if defined(_WIN32)
827-
// Compiled on every host (`tests/all-rules-compile`); an Android row is
828-
// never packed on Windows, so this branch only has to be well-formed.
829-
return ::_pclose(p);
830-
#else
831-
const int status = ::pclose(p);
832-
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
833-
#endif
787+
// Runs `argv`, returning its exit status and its combined output, through
788+
// `mcpp::plugins::tool::capture`: no shell between, so no argument is quoted.
789+
// Only the explicit Maven modes and AAR extraction call it; an ordinary build
790+
// of an unchanged project runs nothing here.
791+
inline int run_capture(const std::vector<std::string>& argv, std::string& output,
792+
const std::filesystem::path& cwd = {}) {
793+
auto r = mcpp::plugins::tool::capture(argv, /*with_errors=*/true, cwd);
794+
output = std::move(r.out);
795+
return r.status;
834796
}
835797

836798
// ─── Merging library manifests into the application's ────────────────────
@@ -1196,8 +1158,7 @@ inline bool unpack_aar(const std::string& aar, const fs::path& dest, const std::
11961158
fs::remove_all(dest, ec);
11971159
fs::create_directories(dest, ec);
11981160
std::string output;
1199-
const int rc = run_capture({"/bin/sh", "-c", "cd \"$1\" && \"$2\" xf \"$3\"", "unpack-aar",
1200-
dest.string(), jarTool, aar}, output);
1161+
const int rc = run_capture({jarTool, "xf", aar}, output, dest);
12011162
if (rc != 0) {
12021163
message = std::format("mcpp.dist.apk: unpacking {} with {} failed ({}): {}", aar, jarTool, rc, output);
12031164
return false;

‎dist/appimage.cppm‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
// the edge depends only on the program binary, and a metadata-only change would
3737
// leave the previous AppImage in place, reported as up to date.
3838

39-
module;
40-
#include <cstdio>
41-
4239
export module mcpp.dist.appimage;
4340

4441
import std;

‎dist/apple.cppm‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@
187187
// reaches the device through the runner named `app` this member supplies,
188188
// `devicectl-run` (`xim:apple-device-tools`), as `macapp-run` serves macOS.
189189

190-
module;
191-
#include <cstdio>
192-
193190
export module mcpp.dist.apple;
194191

195192
import std;

‎dist/web.cppm‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@
8585
// the side file as the copy action's input is what makes an edit reach
8686
// `<out_dir>/web/index.html`.
8787

88-
module;
89-
#include <cstdio>
90-
9188
export module mcpp.dist.web;
9289

9390
import std;

‎dist/wix.cppm‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@
9393
// shims into an executable named like an installer). A project with its own
9494
// bootstrapper application supplies its own bundle definition.
9595

96-
module;
97-
#include <cstdio>
98-
9996
export module mcpp.dist.wix;
10097

10198
import std;

‎docs/plugin-development.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Known differences, each one met in this package:
202202
- **A type that holds a container of itself does not use `std::pair` or `std::tuple` for the element.** Under clang with libstdc++'s `std` module, `std::vector<std::pair<std::string, value>>` inside `value` instantiates the pair while `value` is incomplete and fails with "incomplete type used in type trait expression"; GCC and clang with libc++ accept it. The element is a plain struct, and a member function that walks the container is defined after both types are complete (`mcpp::plugins::json::member`, 0.19.1).
203203
- **No compiler's leniency is relied on.** An extension, a missing `typename`, or a template accepted only because one compiler instantiates it later is a failure in another lane.
204204
- **A build program does not use `std::println`.** On macOS 14 the build program links the system libc++, which lacks a symbol it needs; `std::format` into a string and `std::cout` work everywhere.
205-
- **A unit takes the C library from `std`, not from a header.** `std` exports the functions and types of ISO C (`std::isalnum`, `std::getenv`, `std::FILE`) but no macro; a macro of ISO C (`stdout`, `errno`, `SEEK_SET`) comes from `mcpplibs.mwrap.libc` under its own name (`mcpplibs::mwrap::libc::macros::stdout()`). A global module fragment includes only the headers of platform functions outside ISO C (`popen`, `dlopen`, `setenv`), and a comment beside the include names them.
205+
- **No unit includes a header or branches on the platform in the preprocessor.** The C library comes from `std`, which exports the functions and types of ISO C (`std::isalnum`, `std::getenv`, `std::FILE`) but no macro; a macro of ISO C (`stdout`, `errno`, `SEEK_SET`) comes from `mcpplibs.mwrap.libc` under its own name (`mcpplibs::mwrap::libc::macros::stdout()`). The operating system comes from `mcpplibs.os`: a program a member asks for a fact runs through `mcpp::plugins::tool::capture` (its arguments as a vector, no shell, so nothing is quoted and no null device is named), a library opens through `mcpplibs::os::library::open`, and a child's environment is stated in its spawn options rather than written into this process's. A build program runs on the machine that builds, so a platform difference is a compile-time question of that machine: `if constexpr (mcpplibs::os::is_windows)`. CI refuses an `#include`, `#if` or `#ifdef` in a unit (0.21.0).
206206
- **A module does not instantiate `std::filesystem::path`'s iterator.** Under MSVC 14.52 every importer that then touches `std::filesystem` fails inside the STL; `mcpp::plugins::names::components` splits a path as a string instead.
207207

208208
## 5. A new plugin
@@ -221,6 +221,6 @@ A project's own plugin is a module in the project (a path dependency with `host-
221221

222222
`mcpp.plugins.progress` is part of `plugins-core`. `enable(action, requested)` sets the optional action flag when the engine API provides it. With older APIs it reports that telemetry is unavailable and leaves the action usable. Engine support starts at mcpp 2026.11.5.1 (protocol 18); this optional path does not raise the package's 2026.10.30.1 floor.
223223

224-
Worker programs select `plugins-progress` and import `mcpp.plugins.progress.writer`. `writer` reads `MCPP_ACTION_PROGRESS_FILE`; an absent channel is a no-op. `report({.phase="compile", .done=3, .total=9})` atomically replaces the latest schema-1 snapshot, assigns increasing sequence numbers and publishes at most five times per second. Omit counts when the total is unknown. `report()` returns whether it published; this result does not determine whether the worker's actual task succeeded. The optional writer feature uses `mcpplibs.os.fs` for portable atomic replacement. The general helper and CMake adapter do not add this dependency to ordinary consumers.
224+
Worker programs select `plugins-progress` and import `mcpp.plugins.progress.writer`. `writer` reads `MCPP_ACTION_PROGRESS_FILE`; an absent channel is a no-op. `report({.phase="compile", .done=3, .total=9})` atomically replaces the latest schema-1 snapshot, assigns increasing sequence numbers and publishes at most five times per second. Omit counts when the total is unknown. `report()` returns whether it published; this result does not determine whether the worker's actual task succeeded. The optional writer feature uses `mcpplibs.os.fs` for portable atomic replacement; `plugins-core` depends on `mcpplibs.os` as well (§4).
225225

226226
The writer feature is usable by an ordinary standalone worker with an ordinary dependency; it imports no build-program-only `mcpp` module. Importing `mcpp.plugins.progress.writer` also exposes the `snapshot` type. Channel paths preserve UTF-8 on every host, including Windows with a non-UTF-8 ANSI code page. `tests/progress-worker` checks disabled channels, Unicode paths and optional write failure on Linux, macOS and Windows.

‎mcpp.toml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ default = []
9595
sources = ["src/declare.cppm", "src/toolset.cppm", "src/fs.cppm", "src/tool.cppm", "src/accel.cppm", "src/progress.cppm",
9696
"src/compat/detected_toolset.cppm"]
9797

98+
# The operating system of every member: the programs a member asks for a fact
99+
# (`mcpp.plugins.tool::capture`), the libraries it opens and the platform it
100+
# builds on, through mcpplibs.os. No unit includes a system header (0.21.0,
101+
# docs/plugin-development.md §4).
102+
[feature-deps.plugins-core]
103+
mcpplibs.os = "0.3.1"
104+
98105
# `mcpp.plugins.toolchain` -- the builders a root build program states its
99106
# build toolchain with, in the toolchain phase of a project whose `[toolchain]`
100107
# says `configure = "build.mcpp"` (mcpp#755). Its own feature rather than part

‎rules/cuda.cppm‎

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,13 @@
3333
// - whether the embedded PTX can be JIT-compiled by this driver, as an
3434
// advisory, because the SASS for the named architectures still runs.
3535

36-
module;
37-
// `popen` and `pclose` (`_popen` and `_pclose` on Windows), which POSIX and the
38-
// CRT declare in <stdio.h> and the `std` module, being ISO C++, does not export.
39-
#include <cstdio>
40-
#if !defined(_WIN32)
41-
#include <dlfcn.h>
42-
#endif
43-
4436
export module mcpp.rules.cuda;
4537

4638
import std;
4739
import mcpp;
4840
import mcpp.plugins.accel;
4941
import mcpp.plugins.tool;
42+
import mcpplibs.os;
5043

5144

5245
// WHY NOTHING HERE USES `std::println`, AND WHY THAT IS NOT A STYLE CHOICE.
@@ -78,13 +71,8 @@ export namespace mcpp::rules::cuda {
7871
// from those two facts, so both are stated once here rather than at each site
7972
// -- a `std::filesystem::exists` against the wrong spelling does not fail, it
8073
// answers false, and this rule reads several such answers as decisions.
81-
#if defined(_WIN32)
82-
inline constexpr bool kWindows = true;
83-
inline constexpr const char* kExe = ".exe";
84-
#else
85-
inline constexpr bool kWindows = false;
86-
inline constexpr const char* kExe = "";
87-
#endif
74+
inline constexpr bool kWindows = mcpplibs::os::is_windows;
75+
inline constexpr const char* kExe = kWindows ? ".exe" : "";
8876

8977
enum class route { automatic, clang, nvcc };
9078

@@ -331,29 +319,21 @@ inline std::optional<toolkit> find_toolkit(const options& opt, bool& pending) {
331319
// sentinel package rather than /usr/lib. "" when there is no driver here,
332320
// which is a fact about the machine and not a failure of the build.
333321
inline std::string driver_version(const toolkit& t) {
334-
#if defined(_WIN32)
335-
return {};
336-
#else
322+
if constexpr (kWindows) return {};
337323
if (t.driver_dir.empty()) return {};
338324
const auto lib = t.driver_dir + "/lib/libcuda.so.1";
339325
if (!std::filesystem::exists(lib)) return {};
340326
// What would change the answer is the library the answer was read from.
341327
mcpp::rerun_if_changed(lib.c_str());
342-
void* h = ::dlopen(lib.c_str(), RTLD_LAZY | RTLD_LOCAL);
328+
auto h = mcpplibs::os::library::open(lib);
343329
if (!h) {
344-
const char* why = ::dlerror();
345-
mcpp::warning(std::format("could not open the driver library {}: {}", lib,
346-
why ? why : "(no reason given)").c_str());
330+
mcpp::warning(std::format("could not open the driver library {}: {}", lib, h.error().message()).c_str());
347331
return {};
348332
}
349-
using fn = int (*)(int*);
350-
auto get = reinterpret_cast<fn>(::dlsym(h, "cuDriverGetVersion"));
333+
auto get = h->function<int(int*)>("cuDriverGetVersion");
351334
int v = 0;
352-
std::string out;
353-
if (get && get(&v) == 0 && v > 0) out = std::format("{}.{}", v / 1000, (v % 1000) / 10);
354-
::dlclose(h);
355-
return out;
356-
#endif
335+
if (get && get(&v) == 0 && v > 0) return std::format("{}.{}", v / 1000, (v % 1000) / 10);
336+
return {};
357337
}
358338

359339
inline int major_of(std::string_view v) {
@@ -464,39 +444,21 @@ inline bool libc_declares_c23_pi_math(std::string_view sysroot) {
464444
// `<compiler> -dumpversion` → major. The host compiler is the toolchain mcpp
465445
// resolved for this build, so its version is a fact of the build, not a guess.
466446
inline int compiler_major(const std::string& cc) {
467-
#if defined(_WIN32)
468-
(void)cc; return 0;
469-
#else
470-
std::string cmd = cc + " -dumpversion 2>/dev/null";
471-
if (FILE* p = ::popen(cmd.c_str(), "r")) {
472-
char buf[64] = {};
473-
std::string s;
474-
if (std::fgets(buf, sizeof buf, p)) s = buf;
475-
::pclose(p);
476-
return major_of(s);
477-
}
478-
return 0;
479-
#endif
447+
if constexpr (kWindows) return 0;
448+
return major_of(mcpp::plugins::tool::first_line({cc, "-dumpversion"}));
480449
}
481450

482451
// The first back-end stage nvcc names but cannot resolve, from its own plan.
483452
// nvcc invokes cicc, cudafe++, ptxas and fatbinary by bare name on a PATH it
484453
// states in the plan; a stage that does not resolve there fails the compile
485454
// with `sh: 1: cicc: not found`, naming nothing that helps.
486455
inline std::optional<std::string> unreachable_stage(const toolkit& t, const std::string& ccbin) {
487-
#if defined(_WIN32)
488-
(void)t; (void)ccbin; return std::nullopt;
489-
#else
456+
if constexpr (kWindows) return std::nullopt;
490457
const auto probe = std::filesystem::temp_directory_path() / "mcpp-rules-cuda-dryrun.cu";
491458
{ std::ofstream(probe) << "__global__ void k() {}\n"; }
492-
std::string cmd = std::format("{} --dryrun -ccbin {} -c {} -o /dev/null 2>&1",
493-
t.nvcc(), ccbin, probe.string());
494-
std::string text;
495-
if (FILE* p = ::popen(cmd.c_str(), "r")) {
496-
char buf[4096];
497-
while (std::fgets(buf, sizeof buf, p)) text += buf;
498-
::pclose(p);
499-
}
459+
const std::string text = mcpp::plugins::tool::capture(
460+
{t.nvcc(), "--dryrun", "-ccbin", ccbin, "-c", probe.string(), "-o", "/dev/null"},
461+
/*with_errors=*/true).out;
500462
std::filesystem::remove(probe);
501463
std::string path;
502464
std::vector<std::string> stages;
@@ -520,7 +482,6 @@ inline std::optional<std::string> unreachable_stage(const toolkit& t, const std:
520482
if (!found) return stage;
521483
}
522484
return std::nullopt;
523-
#endif
524485
}
525486

526487
// ─── Planning ──────────────────────────────────────────────────────────────
@@ -558,11 +519,8 @@ struct edge {
558519
// the same way it does for every ordinary C++ translation unit on this host.
559520
inline route decide(route asked) {
560521
if (asked != route::automatic) return asked;
561-
#if defined(_WIN32)
562-
return route::clang;
563-
#else
522+
if constexpr (kWindows) return route::clang;
564523
return std::string_view(mcpp::compiler()) == "clang" ? route::clang : route::nvcc;
565-
#endif
566524
}
567525

568526
inline std::vector<edge> plan(std::span<const std::string> sources, options opt = {}) {

‎rules/hip.cppm‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import std;
3535
import mcpp;
3636
import mcpp.plugins.accel;
3737
import mcpp.plugins.tool;
38+
import mcpplibs.os;
3839

3940

4041
// WHY NOTHING HERE USES `std::println`, AND WHY THAT IS NOT A STYLE CHOICE.
@@ -392,11 +393,7 @@ inline std::vector<edge> plan(std::span<const std::string> sources, options opt
392393
// because the alternative is a path that is wrong on a host this rule
393394
// will one day be asked about, and a wrong path reports itself as a
394395
// missing toolchain.
395-
#if defined(_WIN32)
396-
const std::string cc = tcdir + "/bin/clang++.exe";
397-
#else
398-
const std::string cc = tcdir + "/bin/clang++";
399-
#endif
396+
const std::string cc = tcdir + (mcpplibs::os::is_windows ? "/bin/clang++.exe" : "/bin/clang++");
400397
if (tcdir.empty() || !std::filesystem::exists(cc)) {
401398
std::cerr << std::format("mcpp.rules.hip: the NVIDIA platform compiles through clang, and this "
402399
"project's\n toolchain has no clang++ at {}.\n"

0 commit comments

Comments
 (0)