|
| 1 | +#!/usr/bin/env bash |
| 2 | +# requires: gcc |
| 3 | +# Issue #131 scenario 1, verbatim shape: a single bin target carries a per-target |
| 4 | +# *codegen* cxxflag that must affect ONLY its own entry `main`, not shared code: |
| 5 | +# |
| 6 | +# [targets.test_contracts] |
| 7 | +# kind = "bin" |
| 8 | +# main = "tests/test_contracts.cpp" |
| 9 | +# cxxflags = ["-fcontract-evaluation-semantic=observe"] |
| 10 | +# |
| 11 | +# We use `-fno-exceptions` as a portable stand-in for the contracts semantic |
| 12 | +# flag (which needs a contracts-enabled toolchain not guaranteed across the |
| 13 | +# Linux/macOS/Windows CI matrix). The mechanism under test is identical: a real |
| 14 | +# per-target codegen flag scoped to that target's exclusive entry. Its effect is |
| 15 | +# preprocessor-observable via `__EXCEPTIONS` (defined iff exceptions are on), so |
| 16 | +# both "reached the entry" and "did NOT leak to shared" are checked at compile |
| 17 | +# time, plus the flag is asserted on the target's main edge in build.ninja. |
| 18 | +set -e |
| 19 | + |
| 20 | +TMP=$(mktemp -d) |
| 21 | +trap "rm -rf $TMP" EXIT |
| 22 | +cd "$TMP" |
| 23 | +mkdir -p proj/src proj/tests |
| 24 | +cd proj |
| 25 | + |
| 26 | +# Shared source — globbed, compiled once with DEFAULT flags (exceptions on). |
| 27 | +# If the per-target flag leaked here, __EXCEPTIONS would be undefined → error. |
| 28 | +cat > src/shared.cpp <<'EOF' |
| 29 | +#ifndef __EXCEPTIONS |
| 30 | +#error "per-target cxxflag leaked into a shared compile unit" |
| 31 | +#endif |
| 32 | +extern "C" int shared_val(void) { return 42; } |
| 33 | +EOF |
| 34 | + |
| 35 | +# The target's own entry — compiled WITH the per-target flag (-fno-exceptions), |
| 36 | +# so __EXCEPTIONS must be undefined here. |
| 37 | +cat > tests/test_contracts.cpp <<'EOF' |
| 38 | +#ifdef __EXCEPTIONS |
| 39 | +#error "per-target cxxflag did not reach this target's own entry" |
| 40 | +#endif |
| 41 | +extern "C" int shared_val(void); |
| 42 | +int main() { return shared_val() == 42 ? 0 : 1; } |
| 43 | +EOF |
| 44 | + |
| 45 | +cat > mcpp.toml <<'EOF' |
| 46 | +[package] |
| 47 | +name = "proj" |
| 48 | +version = "0.1.0" |
| 49 | +
|
| 50 | +[targets.test_contracts] |
| 51 | +kind = "bin" |
| 52 | +main = "tests/test_contracts.cpp" |
| 53 | +cxxflags = ["-fno-exceptions"] |
| 54 | +EOF |
| 55 | + |
| 56 | +# Compiles only if the flag reached the entry (entry check) AND did not leak to |
| 57 | +# the shared unit (shared check) — both are #error guards above. |
| 58 | +"$MCPP" build > build.log 2>&1 || { cat build.log; echo "build failed"; exit 1; } |
| 59 | + |
| 60 | +ninja_file="$(find target -name build.ninja | head -1)" |
| 61 | +[[ -n "$ninja_file" ]] || { echo "no build.ninja"; exit 1; } |
| 62 | +grep -q -- "-fno-exceptions" "$ninja_file" || { echo "per-target cxxflag missing from build.ninja"; exit 1; } |
| 63 | + |
| 64 | +"$MCPP" run test_contracts > /dev/null 2>&1 || { echo "run failed"; exit 1; } |
| 65 | + |
| 66 | +echo "OK" |
0 commit comments