|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Observations for .agents/docs/2026-10-11-build-dependencies-one-pipeline-design.md. |
| 3 | +# Prints what each case does; asserts nothing. Usage: MCPP=/path/to/mcpp bash <this> |
| 4 | +# |
| 5 | +# R1 #797 body: a module wrapper over a header library, imported by build.mcpp |
| 6 | +# through `host-module = true` (its GMF #includes the package's own header). |
| 7 | +# R2 #797 comment: one package in [dependencies] and [build-dependencies]. |
| 8 | +# R2b the same library split across two workspace members: m1 links it, m2's |
| 9 | +# build.mcpp imports it (works today; a regression guard). |
| 10 | +# R2c a library reached by the target through another library, and named |
| 11 | +# directly in [build-dependencies] (works today; a regression guard). |
| 12 | +# R3 a feature requested only by a [build-dependencies] edge reaches the |
| 13 | +# target instance of the same package. |
| 14 | +# R4 [workspace.xlings.overrides] on a rooted workspace whose root package |
| 15 | +# depends on a member by path. |
| 16 | +# R5 the same with the root's own [xlings] by position: the warning appears |
| 17 | +# on a plain `mcpp build` of the root package. |
| 18 | +# R6 a header that a path host module includes is edited; the build |
| 19 | +# program is neither recompiled nor rerun (its identity hashes the |
| 20 | +# interface file only). |
| 21 | +# R7 a build plugin built as the root of its own build (the shape of |
| 22 | +# examples/08-build-rules/rules-embed): the engine module does not exist. |
| 23 | +# R8 `mcpp emit build-database` and compile_commands.json do not describe |
| 24 | +# build.mcpp, the host modules or the engine module. |
| 25 | +# W1 a build.mcpp beside a workspace root without [package] is ignored |
| 26 | +# without a word (the package tables there are reported). |
| 27 | +# W2 `features` written in [workspace.dependencies] is dropped without a |
| 28 | +# word when a member inherits the entry. |
| 29 | +set -u |
| 30 | +MCPP="${MCPP:-mcpp}" |
| 31 | +T=$(mktemp -d) |
| 32 | +trap 'rm -rf "$T"' EXIT |
| 33 | +show() { echo "── $1"; shift; ( "$@" ) 2>&1 | grep -E "error|warning|fatal|FEATURE|ok:" | head -8; echo; } |
| 34 | + |
| 35 | +# ── a header library with a module wrapper (the toml++ shape) ─────────────── |
| 36 | +mklib() { # $1 = dir |
| 37 | + mkdir -p "$1/include/hl" "$1/src" |
| 38 | + cat > "$1/include/hl/hl.hpp" <<'EOF' |
| 39 | +#pragma once |
| 40 | +namespace hl { inline int answer() { return 42; } } |
| 41 | +EOF |
| 42 | + cat > "$1/src/hl.cppm" <<'EOF' |
| 43 | +module; |
| 44 | +#include <hl/hl.hpp> |
| 45 | +export module hl; |
| 46 | +export namespace hl { using hl::answer; } |
| 47 | +#ifdef HL_FEATURE_X |
| 48 | +export namespace hl { inline int feature_x() { return 1; } } |
| 49 | +#endif |
| 50 | +EOF |
| 51 | + cat > "$1/mcpp.toml" <<'EOF' |
| 52 | +[package] |
| 53 | +name = "hl" |
| 54 | +version = "0.1.0" |
| 55 | +[lib] |
| 56 | +path = "src/hl.cppm" |
| 57 | +[build] |
| 58 | +sources = ["src/hl.cppm"] |
| 59 | +include_dirs = ["include"] |
| 60 | +[features] |
| 61 | +x = { defines = ["HL_FEATURE_X"] } |
| 62 | +[targets.hl] |
| 63 | +kind = "lib" |
| 64 | +EOF |
| 65 | +} |
| 66 | +mklib "$T/hl" |
| 67 | +# The same library with the include made relative to the unit -- the shape |
| 68 | +# mcpp-index uses today to get past R1 -- so that R2 isolates the edge merge. |
| 69 | +mklib "$T/hlrel" |
| 70 | +sed -i 's|#include <hl/hl.hpp>|#include "../include/hl/hl.hpp"|' "$T/hlrel/src/hl.cppm" |
| 71 | + |
| 72 | +mkapp() { # $1 = dir, $2 = manifest tail |
| 73 | + mkdir -p "$1/src" |
| 74 | + printf '[package]\nname = "app"\nversion = "0.1.0"\n[targets.app]\nkind = "bin"\nmain = "src/main.cpp"\n%s\n' "$2" > "$1/mcpp.toml" |
| 75 | +} |
| 76 | + |
| 77 | +# R1 |
| 78 | +mkapp "$T/r1" $'[build-dependencies]\nhl = { path = "../hl", host-module = true }' |
| 79 | +echo 'int main() { return 0; }' > "$T/r1/src/main.cpp" |
| 80 | +printf 'import std;\nimport hl;\nint main() { std::println("mcpp:cxxflag=-DANSWER={}", hl::answer()); }\n' > "$T/r1/build.mcpp" |
| 81 | +show "R1 host-module over a header library" bash -c "cd $T/r1 && $MCPP build" |
| 82 | + |
| 83 | +# R2 |
| 84 | +mkapp "$T/r2" $'[dependencies]\nhl = { path = "../hlrel" }\n[build-dependencies]\nhl = { path = "../hlrel", host-module = true }' |
| 85 | +printf 'import std;\nimport hl;\nint main() { std::println("ok: {}", hl::answer()); }\n' > "$T/r2/src/main.cpp" |
| 86 | +printf 'import std;\nimport hl;\nint main() { std::println("mcpp:cxxflag=-DANSWER={}", hl::answer()); }\n' > "$T/r2/build.mcpp" |
| 87 | +show "R2 one package in both tables (target import)" bash -c "cd $T/r2 && $MCPP build" |
| 88 | + |
| 89 | +# R2b |
| 90 | +mkdir -p "$T/r2b" |
| 91 | +printf '[workspace]\nmembers = ["m1", "m2"]\n' > "$T/r2b/mcpp.toml" |
| 92 | +mkapp "$T/r2b/m1" $'[dependencies]\nhl = { path = "../../hlrel" }' |
| 93 | +sed -i 's/name = "app"/name = "m1"/; s/targets.app/targets.m1/' "$T/r2b/m1/mcpp.toml" |
| 94 | +printf 'import std;\nimport hl;\nint main() { std::println("ok: m1 {}", hl::answer()); }\n' > "$T/r2b/m1/src/main.cpp" |
| 95 | +mkapp "$T/r2b/m2" $'[build-dependencies]\nhl = { path = "../../hlrel", host-module = true }' |
| 96 | +sed -i 's/name = "app"/name = "m2"/; s/targets.app/targets.m2/' "$T/r2b/m2/mcpp.toml" |
| 97 | +printf 'import std;\nint main() { std::println("ok: m2 {}", ANSWER); }\n' > "$T/r2b/m2/src/main.cpp" |
| 98 | +printf 'import std;\nimport hl;\nint main() { std::println("mcpp:cxxflag=-DANSWER={}", hl::answer()); }\n' > "$T/r2b/m2/build.mcpp" |
| 99 | +show "R2b workspace split" bash -c "cd $T/r2b && $MCPP build --workspace && $MCPP run -p m1 && $MCPP run -p m2" |
| 100 | + |
| 101 | +# R2c |
| 102 | +mkdir -p "$T/lib/src" |
| 103 | +printf '[package]\nname = "lib"\nversion = "0.1.0"\n[dependencies]\nhl = { path = "../hlrel" }\n[targets.lib]\nkind = "lib"\n' > "$T/lib/mcpp.toml" |
| 104 | +printf 'export module lib;\nimport hl;\nexport int via_lib() { return hl::answer(); }\n' > "$T/lib/src/lib.cppm" |
| 105 | +mkapp "$T/r2c" $'[dependencies]\nlib = { path = "../lib" }\n[build-dependencies]\nhl = { path = "../hlrel", host-module = true }' |
| 106 | +printf 'import std;\nimport lib;\nint main() { std::println("ok: {} {}", via_lib(), ANSWER); }\n' > "$T/r2c/src/main.cpp" |
| 107 | +cp "$T/r2b/m2/build.mcpp" "$T/r2c/build.mcpp" |
| 108 | +show "R2c transitive target use and a direct build dependency" bash -c "cd $T/r2c && $MCPP build && $MCPP run" |
| 109 | + |
| 110 | +# R3 -- the library itself is a dependency of `mid`, which asks for feature x |
| 111 | +# only in its [build-dependencies]; the app imports hl for its target. |
| 112 | +mkdir -p "$T/mid/src" |
| 113 | +printf '[package]\nname = "mid"\nversion = "0.1.0"\n[build-dependencies]\nhl = { path = "../hl", features = ["x"] }\n[targets.mid]\nkind = "lib"\n' > "$T/mid/mcpp.toml" |
| 114 | +printf 'export module mid;\nexport int mid_v() { return 1; }\n' > "$T/mid/src/mid.cppm" |
| 115 | +mkapp "$T/r3" $'[dependencies]\nhl = { path = "../hl" }\nmid = { path = "../mid" }' |
| 116 | +cat > "$T/r3/src/main.cpp" <<'EOF' |
| 117 | +import std; |
| 118 | +import hl; |
| 119 | +int main() { |
| 120 | +#ifdef HL_FEATURE_X |
| 121 | + std::println("FEATURE x reached the target"); |
| 122 | +#endif |
| 123 | + std::println("ok: {}", hl::answer()); |
| 124 | +} |
| 125 | +EOF |
| 126 | +show "R3 a build-only feature request and the target instance" bash -c "cd $T/r3 && $MCPP build && $MCPP run" |
| 127 | +grep -rhoE -- "-DHL_FEATURE_X" "$T/r3/target" 2>/dev/null | head -1 | sed 's/^/FEATURE define in target compile: /' |
| 128 | + |
| 129 | +# R4, R5 |
| 130 | +mkws() { # $1 = dir, $2 = root tail |
| 131 | + mkdir -p "$1/src" "$1/sub/src" |
| 132 | + printf '[package]\nname = "root"\nversion = "0.1.0"\n[targets.root]\nkind = "bin"\nmain = "src/main.cpp"\n[dependencies]\nsub = { path = "sub" }\n[workspace]\nmembers = ["sub"]\n%s\n' "$2" > "$1/mcpp.toml" |
| 133 | + echo 'int main() { return 0; }' > "$1/src/main.cpp" |
| 134 | + printf '[package]\nname = "sub"\nversion = "0.1.0"\n[targets.sub]\nkind = "lib"\n' > "$1/sub/mcpp.toml" |
| 135 | + printf 'export module sub;\nexport int f() { return 1; }\n' > "$1/sub/src/sub.cppm" |
| 136 | +} |
| 137 | +mkws "$T/r4" $'[workspace.xlings.overrides]\n"xim:cmake" = "/usr/bin/cmake"' |
| 138 | +show "R4 [workspace.xlings.overrides] with the root depending on a member" bash -c "cd $T/r4 && $MCPP build" |
| 139 | +mkws "$T/r5" $'[xlings.overrides]\n"xim:cmake" = "/usr/bin/cmake"' |
| 140 | +show "R5 the root's own [xlings] by position, plain build of the root" bash -c "cd $T/r5 && $MCPP build" |
| 141 | + |
| 142 | +# R6 -- uses the relative-include library so that the host compile succeeds. |
| 143 | +mkapp "$T/r6" $'[build-dependencies]\nhl = { path = "../hlrel", host-module = true }' |
| 144 | +printf 'import std;\nint main() { std::println("ok: ANSWER={}", ANSWER); }\n' > "$T/r6/src/main.cpp" |
| 145 | +printf 'import std;\nimport hl;\nint main() { std::println("mcpp:cxxflag=-DANSWER={}", hl::answer()); }\n' > "$T/r6/build.mcpp" |
| 146 | +show "R6 before the header edit" bash -c "cd $T/r6 && $MCPP build >/dev/null && $MCPP run" |
| 147 | +sed -i 's/return 42/return 43/' "$T/hlrel/include/hl/hl.hpp" |
| 148 | +show "R6 after editing the header to 43" bash -c "cd $T/r6 && $MCPP build >/dev/null && $MCPP run" |
| 149 | + |
| 150 | +# R7 |
| 151 | +mkdir -p "$T/r7/src" |
| 152 | +printf '[package]\nname = "rule"\nversion = "0.1.0"\n[targets.rule]\nkind = "lib"\n' > "$T/r7/mcpp.toml" |
| 153 | +printf 'export module rule;\nimport std;\nimport mcpp;\nexport namespace rule { inline bool compile() { return true; } }\n' > "$T/r7/src/rule.cppm" |
| 154 | +show "R7 a build plugin as the root of its own build" bash -c "cd $T/r7 && $MCPP build" |
| 155 | + |
| 156 | +# R8 -- r6 has a build.mcpp importing a host module |
| 157 | +( cd "$T/r6" && $MCPP emit build-database --format json 2>/dev/null ) > "$T/db.json" |
| 158 | +python3 - "$T/db.json" <<'PY' |
| 159 | +import json, sys |
| 160 | +doc = json.load(open(sys.argv[1]))["data"]["database"] |
| 161 | +def sources(o): |
| 162 | + if isinstance(o, dict): |
| 163 | + for k, v in o.items(): |
| 164 | + if k == "source" and isinstance(v, str): yield v.replace("\\", "/").split("/")[-1] |
| 165 | + else: yield from sources(v) |
| 166 | + elif isinstance(o, list): |
| 167 | + for v in o: yield from sources(v) |
| 168 | +units = sorted(set(sources(doc))) |
| 169 | +print("── R8 build database") |
| 170 | +print("ok: translation units:", units) |
| 171 | +print("ok: build.mcpp / hl.cppm / mcpp.cppm described:", |
| 172 | + "build.mcpp" in units, "hl.cppm" in units, "mcpp.cppm" in units) |
| 173 | +PY |
| 174 | +echo |
| 175 | + |
| 176 | +# W1 |
| 177 | +mkdir -p "$T/w1/m/src" |
| 178 | +printf '[workspace]\nmembers = ["m"]\n' > "$T/w1/mcpp.toml" |
| 179 | +printf 'int main() { return 1; }\n' > "$T/w1/build.mcpp" # would fail the build if it ran |
| 180 | +printf '[package]\nname = "m"\nversion = "0.1.0"\n[targets.m]\nkind = "bin"\nmain = "src/main.cpp"\n' > "$T/w1/m/mcpp.toml" |
| 181 | +echo 'int main() { return 0; }' > "$T/w1/m/src/main.cpp" |
| 182 | +show "W1 build.mcpp beside a virtual root (no output = ignored silently)" bash -c "cd $T/w1 && $MCPP build" |
| 183 | + |
| 184 | +# W2 |
| 185 | +mkdir -p "$T/w2/m/src" |
| 186 | +printf '[workspace]\nmembers = ["m"]\n[workspace.dependencies]\nhl = { path = "../hl", features = ["x"] }\n' > "$T/w2/mcpp.toml" |
| 187 | +printf '[package]\nname = "m"\nversion = "0.1.0"\n[dependencies]\nhl.workspace = true\n[targets.m]\nkind = "bin"\nmain = "src/main.cpp"\n' > "$T/w2/m/mcpp.toml" |
| 188 | +cat > "$T/w2/m/src/main.cpp" <<'CPP' |
| 189 | +import std; |
| 190 | +import hl; |
| 191 | +int main() { |
| 192 | +#ifdef HL_FEATURE_X |
| 193 | + std::println("ok: feature x inherited"); |
| 194 | +#else |
| 195 | + std::println("FEATURE x from [workspace.dependencies] was dropped"); |
| 196 | +#endif |
| 197 | +} |
| 198 | +CPP |
| 199 | +show "W2 features in [workspace.dependencies]" bash -c "cd $T/w2 && $MCPP build && $MCPP run -p m" |
0 commit comments