diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index fcfd73c..553b63c 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -185,6 +185,8 @@ jobs: cargo test --all-features --package cpp-overload-test --lib cargo test --doc --all-features --package cpp-overload-test cargo fmt --package cpp-overload-test -- --check + - name: Check changed cpp! code is rebuilt + run: bash cpp-overload-test/rerun-check.sh cpp-overload-test-fail: name: 10. Must-fail C++ overloads diff --git a/README.md b/README.md index 7387500..2829ad8 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,10 @@ cargo run --bin multiple-args ## Testing Quirks -Sometimes the C++ code extracted by `cpp_build` will become outdated, and you will get errors like: +`cpp-overload-test/build.rs` tells Cargo to rerun `cpp_build` whenever a file in +`cpp-overload-test/src` changes, so new or changed `cpp!` blocks are picked up on the next build. + +If you still get errors like: ```text error: This cpp! macro is not found in the library's rust-cpp metadata. @@ -125,5 +128,7 @@ error: This cpp! macro is not found in the library's rust-cpp metadata. error: cannot find macro `__cpp_closure_impl` in this scope ``` -To fix these errors, run `cargo clean` then rebuild. -If the errors still happen after a clean, check your Rust and C++ code is compiling correctly. +check that the `cpp!` block is written directly in `cpp-overload-test/src`, and isn't generated by +another macro, or used in an example or binary (`cpp_build` only runs for `lib.rs`). +If the errors still happen, run `cargo clean` then rebuild, and check your Rust and C++ code is +compiling correctly. diff --git a/cpp-overload-test/build.rs b/cpp-overload-test/build.rs index 5cf150f..adf2ea9 100644 --- a/cpp-overload-test/build.rs +++ b/cpp-overload-test/build.rs @@ -1,4 +1,8 @@ fn main() { + // `cc` prints `rerun-if-env-changed` lines, which turns off Cargo's default rescan of the + // package, so instruct Cargo to rerun this script whenever `src/` changes and pick up + // `cpp!` updates. + println!("cargo::rerun-if-changed=src"); // `cpp_build` doesn't work with multiple example/binary builds in the same crate, so we use // lib.rs submodules for successful overloads. cpp_build::Config::new() diff --git a/cpp-overload-test/rerun-check.sh b/cpp-overload-test/rerun-check.sh new file mode 100755 index 0000000..59a2c42 --- /dev/null +++ b/cpp-overload-test/rerun-check.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Checks that new or changed `cpp!` blocks get compiled after an edit, without running +# `cargo clean` or touching `build.rs`. See `src/rerun_check.rs` for details. +set -euo pipefail + +cd "$(dirname "$0")" +ORIGINAL=src/rerun_check.rs +REPLACEMENT=rerun-check/rerun_check.rs +BACKUP=$(mktemp) +cp "$ORIGINAL" "$BACKUP" +trap 'cp "$BACKUP" "$ORIGINAL"; rm -f "$BACKUP"' EXIT + +run_check() { + local output + if ! output=$(cargo test --all-features --package cpp-overload-test --lib rerun_check 2>&1); then + echo "$output" + return 1 + fi + echo "$output" + # A test filter that matches nothing still passes, so check that the test actually ran. + if ! grep -q "test rerun_check::test_rerun_check ... ok" <<<"$output"; then + echo "rerun_check::test_rerun_check didn't run" >&2 + return 1 + fi +} + +echo "Testing the original cpp! code:" +run_check + +# Make sure the replacement is newer than the last build, even with coarse file timestamps. +sleep 1 +echo "Replacing the cpp! code, then testing again without cargo clean:" +cp "$REPLACEMENT" "$ORIGINAL" +run_check +echo "cpp! changes were rebuilt" \ No newline at end of file diff --git a/cpp-overload-test/rerun-check/rerun_check.rs b/cpp-overload-test/rerun-check/rerun_check.rs new file mode 100644 index 0000000..66b0388 --- /dev/null +++ b/cpp-overload-test/rerun-check/rerun_check.rs @@ -0,0 +1,22 @@ +//! The replacement for `src/rerun_check.rs` that `rerun-check.sh` swaps in. +//! +//! It changes the C++ code in the existing `cpp!` block and adds a new `cpp!` block, so the +//! second test run covers both kinds of change. + +use cpp::cpp; + +/// A value from an existing `cpp!` block, which this file changes. +fn changed_value() -> i32 { + unsafe { cpp!([] -> i32 as "int" { return 2; }) } +} + +/// A value from a new `cpp!` block. +fn added_value() -> i32 { + unsafe { cpp!([] -> i32 as "int" { return 3; }) } +} + +#[test] +fn test_rerun_check() { + assert_eq!(changed_value(), 2); + assert_eq!(added_value(), 3); +} diff --git a/cpp-overload-test/src/lib.rs b/cpp-overload-test/src/lib.rs index b72552c..45ffeec 100644 --- a/cpp-overload-test/src/lib.rs +++ b/cpp-overload-test/src/lib.rs @@ -9,3 +9,7 @@ pub mod stdcpp { // Add new modules for each type here, in alphabetical order. pub mod forward_list; } + +// Checks that `build.rs` rebuilds changed `cpp!` code, see `rerun-check.sh`. +#[cfg(test)] +mod rerun_check; diff --git a/cpp-overload-test/src/rerun_check.rs b/cpp-overload-test/src/rerun_check.rs new file mode 100644 index 0000000..5106018 --- /dev/null +++ b/cpp-overload-test/src/rerun_check.rs @@ -0,0 +1,17 @@ +//! Checks that new or changed `cpp!` blocks get compiled without needing `cargo clean`. +//! +//! `rerun-check.sh` runs the tests in this file, then swaps it for `rerun-check/rerun_check.rs`, +//! and runs the tests again. The second run only passes if `build.rs` reruns `cpp_build` +//! when a file under `src/` changes. + +use cpp::cpp; + +/// A value from an existing `cpp` block, which the replacement file changes. +fn changed_value() -> i32 { + unsafe { cpp!([] -> i32 as "int" { return 1; }) } +} + +#[test] +fn test_rerun_check() { + assert_eq!(changed_value(), 1); +}