|
| 1 | +#!/usr/bin/env bash |
| 2 | +# requires: |
| 3 | +# 880 -- mcpp#760: a member with shared and bin targets builds an independent |
| 4 | +# executable from its own implementation, not from its sibling shared library. |
| 5 | +# |
| 6 | +# A module interface, a separate implementation unit and a static dependency |
| 7 | +# must all reach the executable. Another member still consumes the shared |
| 8 | +# target. Both --workspace and an explicit member selection keep this rule. |
| 9 | +set -e |
| 10 | +source "$(dirname "$0")/_host_path.sh" |
| 11 | + |
| 12 | +TMP=$(mktemp -d) |
| 13 | +trap 'rm -rf "$TMP"' EXIT |
| 14 | +fail() { echo "FAIL: $1"; shift; for f in "$@"; do echo "--- $f ---"; cat "$f" 2>/dev/null; done; exit 1; } |
| 15 | +MCPP="${MCPP:-mcpp}" |
| 16 | + |
| 17 | +# Share the installed tool payloads, but keep the build and package caches |
| 18 | +# private. The fixture has no registry package dependencies; the default host |
| 19 | +# toolchain is resolved through the existing registry, as in the E2E runner. |
| 20 | +REGISTRY_HOST=$(host_path "${MCPP_HOME:-$HOME/.mcpp}/registry") |
| 21 | +export MCPP_HOME="$TMP/mcpp-home" |
| 22 | +mkdir -p "$MCPP_HOME" "$TMP/ws" |
| 23 | +cat > "$MCPP_HOME/config.toml" <<EOF |
| 24 | +[xlings] |
| 25 | +home = "$REGISTRY_HOST" |
| 26 | +EOF |
| 27 | + |
| 28 | +EXE="" |
| 29 | +case "$(uname -s)" in MINGW*|MSYS*|CYGWIN*) EXE=".exe" ;; esac |
| 30 | +bin_of() { find target -path '*/bin/*' -name "$1$EXE" -type f | head -1; } |
| 31 | + |
| 32 | +cd "$TMP/ws" |
| 33 | +cat > mcpp.toml <<'EOF' |
| 34 | +[workspace] |
| 35 | +members = ["dual", "client", "support"] |
| 36 | +EOF |
| 37 | +mkdir -p common/src dual/src client/src support/src |
| 38 | +cat > support/mcpp.toml <<'EOF' |
| 39 | +[package] |
| 40 | +name = "support" |
| 41 | +version = "0.1.0" |
| 42 | +
|
| 43 | +[targets.support] |
| 44 | +kind = "shared" |
| 45 | +EOF |
| 46 | +printf 'export module t880_support;\nexport int delta() { return 1; }\n' > support/src/support.cppm |
| 47 | +cat > common/mcpp.toml <<'EOF' |
| 48 | +[package] |
| 49 | +name = "common" |
| 50 | +version = "0.1.0" |
| 51 | +
|
| 52 | +[targets.common] |
| 53 | +kind = "lib" |
| 54 | +EOF |
| 55 | +printf 'export module t880_base;\nexport int base_value() { return 41; }\n' > common/src/base.cppm |
| 56 | +cat > dual/mcpp.toml <<'EOF' |
| 57 | +[package] |
| 58 | +name = "dual" |
| 59 | +version = "0.1.0" |
| 60 | +
|
| 61 | +[dependencies] |
| 62 | +common = { path = "../common" } |
| 63 | +support = { path = "../support" } |
| 64 | +
|
| 65 | +[targets.dual_dll] |
| 66 | +kind = "shared" |
| 67 | +
|
| 68 | +[targets.dual] |
| 69 | +kind = "bin" |
| 70 | +main = "src/main.cpp" |
| 71 | +EOF |
| 72 | +printf 'export module t880_dual;\nexport int interface_value() { return 1; }\nexport int answer();\n' > dual/src/dual.cppm |
| 73 | +printf 'module t880_dual;\nimport t880_base;\nimport t880_support;\nint answer() { return base_value() + delta(); }\n' > dual/src/impl.cpp |
| 74 | +printf 'import t880_dual;\nint main() { return answer() == 42 && interface_value() == 1 ? 0 : 1; }\n' > dual/src/main.cpp |
| 75 | +cat > client/mcpp.toml <<'EOF' |
| 76 | +[package] |
| 77 | +name = "client" |
| 78 | +version = "0.1.0" |
| 79 | +
|
| 80 | +[dependencies] |
| 81 | +dual = { path = "../dual" } |
| 82 | +
|
| 83 | +[targets.client] |
| 84 | +kind = "bin" |
| 85 | +main = "src/main.cpp" |
| 86 | +EOF |
| 87 | +cp dual/src/main.cpp client/src/main.cpp |
| 88 | + |
| 89 | +"$MCPP" build --workspace > workspace.log 2>&1 || fail "the workspace did not build" workspace.log |
| 90 | +dual=$(bin_of dual) |
| 91 | +client=$(bin_of client) |
| 92 | +[ -n "$dual" ] && [ -n "$client" ] || fail "a member's executable is missing" workspace.log |
| 93 | +"$dual" || fail "the owner's executable did not run" |
| 94 | +"$client" || fail "the shared-library consumer did not run" |
| 95 | + |
| 96 | +# Remove every placement of the sibling library. A client that imports it must |
| 97 | +# fail, while the owner's executable must still run: mere success with both |
| 98 | +# products beside it would also accept the import-library workaround for #760. |
| 99 | +mkdir hidden |
| 100 | +n=0 |
| 101 | +while IFS= read -r library; do |
| 102 | + n=$((n + 1)) |
| 103 | + mv "$library" "hidden/$n" |
| 104 | +done < <(find target -path '*/bin/*' \( -name '*dual_dll.dll' -o -name '*dual_dll.so*' -o -name '*dual_dll.dylib' \)) |
| 105 | +[ "$n" -gt 0 ] || fail "the sibling shared library was not built" workspace.log |
| 106 | +"$dual" || fail "the owner's executable depends on its sibling shared library" |
| 107 | +if "$client" > client.log 2>&1; then |
| 108 | + fail "the external consumer did not use the shared library" client.log |
| 109 | +fi |
| 110 | + |
| 111 | +"$MCPP" build -p dual > selected.log 2>&1 || fail "the selected member did not build" selected.log |
| 112 | +"$(bin_of dual)" || fail "the selected member's executable did not run" |
| 113 | +echo "PASS: 880_a_members_executable_keeps_its_own_implementation" |
0 commit comments