|
| 1 | +#!/usr/bin/env bash |
| 2 | +# requires: unix-shell |
| 3 | +# `MCPP_TARGET_REQUESTED` — the value a build program needs and `MCPP_TARGET` |
| 4 | +# cannot give it. |
| 5 | +# |
| 6 | +# ⭐⭐ WHY THE TWO VARIABLES ARE NOT THE SAME QUESTION. |
| 7 | +# |
| 8 | +# `MCPP_TARGET` answers "which machine is this for", and it is filled in with |
| 9 | +# the host when nobody named a target — which is right for that question and |
| 10 | +# makes it useless for a different one a platform package has to ask: **was this |
| 11 | +# build POINTED at a target**, or is it an ordinary native build? |
| 12 | +# |
| 13 | +# The two differ even when the triples are equal. `mcpp build --target |
| 14 | +# aarch64-macos` on an arm64 Mac names the very machine the host is, and yet it |
| 15 | +# is the dependency graph that supplies the target side — so mcpp puts no system |
| 16 | +# SDK on the link, and the package that knows the system is the only thing that |
| 17 | +# can name one. A native build on the same machine gets the SDK and needs |
| 18 | +# nothing from the package. |
| 19 | +# |
| 20 | +# ⚠️ Both readings of that question have already been measured wrong in |
| 21 | +# `openkal-macos`: |
| 22 | +# |
| 23 | +# from the host → right for the cross, and `library not found for -lSystem` |
| 24 | +# for `--target aarch64-macos` ON a Mac |
| 25 | +# from MCPP_TARGET → right for the cross, and `undefined symbol: wcslen` for |
| 26 | +# the native build, because it is never empty and the |
| 27 | +# package's three-name stub shadowed the vendor's complete one |
| 28 | +# |
| 29 | +# ⇒ The assertions below are the contract those two attempts needed: EMPTY for a |
| 30 | +# native build, and the requested triple otherwise. |
| 31 | +# |
| 32 | +# ⚠️ `"$MCPP"`, never a bare `mcpp`: the harness passes the binary under test, |
| 33 | +# and a bare name resolves through PATH to whichever engine is installed. |
| 34 | +set -e |
| 35 | + |
| 36 | +TMP=$(mktemp -d) |
| 37 | +trap "rm -rf $TMP" EXIT |
| 38 | +cd "$TMP" |
| 39 | + |
| 40 | +mkdir -p src |
| 41 | +cat > mcpp.toml <<'EOF' |
| 42 | +[package] |
| 43 | +name = "target-requested-probe" |
| 44 | +version = "0.1.0" |
| 45 | +EOF |
| 46 | +printf 'int main() { return 0; }\n' > src/main.cpp |
| 47 | + |
| 48 | +# ⚠️ The build program reports through a NON-ZERO exit, because mcpp prints what |
| 49 | +# it captured only from a program that failed. A probe that returned zero would |
| 50 | +# have its output discarded and this test would assert nothing. |
| 51 | +cat > build.mcpp <<'EOF' |
| 52 | +import mcpp; |
| 53 | +import std; |
| 54 | +int main() { |
| 55 | + const char* t = std::getenv("MCPP_TARGET"); |
| 56 | + const char* r = std::getenv("MCPP_TARGET_REQUESTED"); |
| 57 | + std::cerr << "PROBE target=[" << (t ? t : "") << "] " |
| 58 | + << "requested=[" << (r ? r : "") << "]\n"; |
| 59 | + return 1; |
| 60 | +} |
| 61 | +EOF |
| 62 | + |
| 63 | +# ── A native build: nobody named a target ────────────────────────────────── |
| 64 | +native=$("$MCPP" build 2>&1 || true) |
| 65 | +echo "$native" | grep -q 'PROBE ' || { |
| 66 | + echo "the build program did not report; mcpp prints a failing program's output" >&2 |
| 67 | + echo "$native" >&2 |
| 68 | + exit 1 |
| 69 | +} |
| 70 | +echo "$native" | grep -qE 'requested=\[\]' || { |
| 71 | + echo "MCPP_TARGET_REQUESTED should be empty for a native build" >&2 |
| 72 | + echo "$native" | grep 'PROBE ' >&2 |
| 73 | + exit 1 |
| 74 | +} |
| 75 | +# ⚠️ And the positive control on the same line: `MCPP_TARGET` must be filled in. |
| 76 | +# Without this the assertion above would also pass if mcpp had stopped setting |
| 77 | +# any of them. |
| 78 | +echo "$native" | grep -qE 'target=\[[a-z0-9_]+-[a-z0-9-]+\]' || { |
| 79 | + echo "MCPP_TARGET should carry the host triple for a native build" >&2 |
| 80 | + echo "$native" | grep 'PROBE ' >&2 |
| 81 | + exit 1 |
| 82 | +} |
| 83 | + |
| 84 | +# ── A build pointed at a target ──────────────────────────────────────────── |
| 85 | +# x86_64-linux-musl is chosen because every host in this suite can resolve it |
| 86 | +# without a payload download beyond what the other cross tests already use. |
| 87 | +cross=$("$MCPP" build --target x86_64-linux-musl 2>&1 || true) |
| 88 | +echo "$cross" | grep -q 'PROBE ' || { |
| 89 | + echo "the build program did not report on the cross build" >&2 |
| 90 | + echo "$cross" >&2 |
| 91 | + exit 1 |
| 92 | +} |
| 93 | +echo "$cross" | grep -qE 'requested=\[x86_64-linux-musl\]' || { |
| 94 | + echo "MCPP_TARGET_REQUESTED should carry the triple that was named" >&2 |
| 95 | + echo "$cross" | grep 'PROBE ' >&2 |
| 96 | + exit 1 |
| 97 | +} |
| 98 | + |
| 99 | +echo "MCPP_TARGET_REQUESTED distinguishes a native build from a named target" |
0 commit comments