|
| 1 | +#!/usr/bin/env bash |
| 2 | +# requires: python3 |
| 3 | +# #778: keep the workspace's ninja log, clear only a member's generated |
| 4 | +# outputs, and verify the generator runs again rather than compiling an empty |
| 5 | +# scan placeholder. Configure-only must leave no placeholder for a later run; |
| 6 | +# ordinary warm builds, including a re-prepare, must not rerun the generator. |
| 7 | +set -e |
| 8 | + |
| 9 | +HERE=$(cd "$(dirname "$0")" && pwd) |
| 10 | +TMP=$(mktemp -d) |
| 11 | +trap 'rm -rf "$TMP"' EXIT |
| 12 | +fail() { echo "FAIL: $1"; [ -z "${2:-}" ] || cat "$2"; exit 1; } |
| 13 | +MCPP="${MCPP:-mcpp}" |
| 14 | +export ACTION_PYTHON=$(python3 -c 'import sys; print(sys.executable.replace(chr(92), "/"))') |
| 15 | +export MCPP_HOME="$TMP/mcpp-home" |
| 16 | +source "$HERE/_inherit_toolchain.sh" |
| 17 | + |
| 18 | +mkdir -p "$TMP/ws/app/src" |
| 19 | +cd "$TMP/ws" |
| 20 | +cat > mcpp.toml <<'EOF' |
| 21 | +[workspace] |
| 22 | +members = ["app"] |
| 23 | +EOF |
| 24 | +cat > app/mcpp.toml <<'EOF' |
| 25 | +[package] |
| 26 | +name = "app" |
| 27 | +namespace = "repro" |
| 28 | +version = "0.1.0" |
| 29 | +[build] |
| 30 | +sources = [] |
| 31 | +[targets.resource_test] |
| 32 | +kind = "bin" |
| 33 | +main = "src/main.cpp" |
| 34 | +EOF |
| 35 | +cat > app/src/main.cpp <<'EOF' |
| 36 | +int generated_value(); |
| 37 | +int main() { return generated_value() == 42 ? 0 : 1; } |
| 38 | +EOF |
| 39 | +echo 'int generated_value() { return 42; }' > app/value.cpp.in |
| 40 | +cat > app/generate.py <<'EOF' |
| 41 | +from pathlib import Path |
| 42 | +import sys |
| 43 | +
|
| 44 | +source, output, counter = map(Path, sys.argv[1:]) |
| 45 | +output.parent.mkdir(parents=True, exist_ok=True) |
| 46 | +output.write_bytes(source.read_bytes()) |
| 47 | +count = int(counter.read_text()) if counter.exists() else 0 |
| 48 | +counter.write_text(str(count + 1)) |
| 49 | +EOF |
| 50 | +cat > app/build.mcpp <<'EOF' |
| 51 | +import std; |
| 52 | +import mcpp; |
| 53 | +int main() { |
| 54 | + mcpp::rerun_if_env_changed("ACTION_PYTHON"); |
| 55 | + const std::string root = mcpp::manifest_dir(); |
| 56 | + const std::string out = std::string(mcpp::out_dir()) + "/generated.cpp"; |
| 57 | + mcpp::action a; |
| 58 | + a.id = "generate:value"; |
| 59 | + a.role = mcpp::roles::source; |
| 60 | + a.arg(std::getenv("ACTION_PYTHON")) |
| 61 | + .arg((root + "/generate.py").c_str()) |
| 62 | + .arg((root + "/value.cpp.in").c_str()).arg(out.c_str()) |
| 63 | + .arg((root + "/generator-count.txt").c_str()) |
| 64 | + .input((root + "/generate.py").c_str()) |
| 65 | + .input((root + "/value.cpp.in").c_str()).output(out.c_str()).submit(); |
| 66 | +} |
| 67 | +EOF |
| 68 | + |
| 69 | +build() { "$MCPP" build -p app --release > "$1" 2>&1 || fail "the build failed" "$1"; } |
| 70 | +count_is() { [ "$(cat app/generator-count.txt)" = "$1" ] || fail "expected $1 generator calls"; } |
| 71 | +generated="app/target/.build-mcpp/out/generated.cpp" |
| 72 | + |
| 73 | +# A real cold build establishes the generated source and the retained log. |
| 74 | +build cold.log |
| 75 | +cmp app/value.cpp.in "$generated" || fail "the generated source differs from its input" |
| 76 | +count_is 1 |
| 77 | +log=$(find target -name .ninja_log | head -1) |
| 78 | +[ -n "$log" ] && [ -s "$log" ] || fail "the workspace has no ninja build record" |
| 79 | + |
| 80 | +build warm.log |
| 81 | +count_is 1 |
| 82 | +touch app/build.mcpp |
| 83 | +build reprepare.log |
| 84 | +count_is 1 |
| 85 | +echo "ok: warm builds and re-prepare preserve the real output" |
| 86 | + |
| 87 | +# Retain target/.ninja_log while removing only the member's output tree. |
| 88 | +mv app/target saved-app-target |
| 89 | +build member-clean.log |
| 90 | +cmp app/value.cpp.in "$generated" || fail "the cleared source was replaced by a placeholder" |
| 91 | +count_is 2 |
| 92 | +echo "ok: clearing a member's target reruns its generator" |
| 93 | + |
| 94 | +# A configure-only pass must not leave a newer fake output behind. |
| 95 | +mv app/target saved-app-target-2 |
| 96 | +"$MCPP" build -p app --release --configure-only > configure.log 2>&1 || fail "configure-only failed" configure.log |
| 97 | +[ ! -e "$generated" ] || fail "configure-only left a scan placeholder behind" |
| 98 | +count_is 2 |
| 99 | +build after-configure.log |
| 100 | +cmp app/value.cpp.in "$generated" || fail "the build after configure-only did not generate its source" |
| 101 | +count_is 3 |
| 102 | +build final-warm.log |
| 103 | +count_is 3 |
| 104 | +echo "ok: configure-only leaves the missing output visible to the next build" |
| 105 | + |
| 106 | +# Generated module interfaces still need their declared provider in the scan, |
| 107 | +# even though the temporary interface disappears before the actual build. |
| 108 | +echo 'import generated; int main() { return generated_value() == 42 ? 0 : 1; }' > app/src/main.cpp |
| 109 | +printf 'export module generated;\nexport int generated_value() { return 42; }\n' > app/value.cpp.in |
| 110 | +python3 <<'PY' |
| 111 | +from pathlib import Path |
| 112 | +p = Path("app/build.mcpp") |
| 113 | +p.write_text(p.read_text().replace('"/generated.cpp"', '"/generated.cppm"') |
| 114 | + .replace('a.role = mcpp::roles::source;', |
| 115 | + 'a.role = mcpp::roles::source; a.provides("generated");')) |
| 116 | +PY |
| 117 | +generated="app/target/.build-mcpp/out/generated.cppm" |
| 118 | +build module.log |
| 119 | +count_is 4 |
| 120 | +cmp app/value.cpp.in "$generated" || fail "the module interface was not generated" |
| 121 | +mv app/target saved-app-target-3 |
| 122 | +build module-clean.log |
| 123 | +count_is 5 |
| 124 | +cmp app/value.cpp.in "$generated" || fail "the cleared module interface was not regenerated" |
| 125 | +echo "ok: generated module interfaces retain their scan declarations and regenerate" |
| 126 | +echo "PASS: 890_source_placeholders_do_not_hide_missing_outputs" |
0 commit comments