Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I would split this out into a separate job, but we're getting a lot of jobs now, so I don't think it matters that much.

If it turns out this job is always the slowest, or it fails, we can change it later.

run: bash cpp-overload-test/rerun-check.sh

cpp-overload-test-fail:
name: 10. Must-fail C++ overloads
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
4 changes: 4 additions & 0 deletions cpp-overload-test/build.rs
Original file line number Diff line number Diff line change
@@ -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");
Comment thread
teor2345 marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to upstream this fix to cpp. But this is an optional change, please prioritise completing your overloading wrapper PR during the Outreachy application period.

If you do have time to do both, feel free to include it in your Outreachy application.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi teor, i have created a pr upstream suggesting a fix for the build script, mystor/rust-cpp#118.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I see that it has been merged, congratulations!

// `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()
Expand Down
35 changes: 35 additions & 0 deletions cpp-overload-test/rerun-check.sh
Original file line number Diff line number Diff line change
@@ -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"
22 changes: 22 additions & 0 deletions cpp-overload-test/rerun-check/rerun_check.rs
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 4 additions & 0 deletions cpp-overload-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
17 changes: 17 additions & 0 deletions cpp-overload-test/src/rerun_check.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Loading