Skip to content

Commit

Permalink
Fix build issue when MAINTAINER_MODE=OFF / not set
Browse files Browse the repository at this point in the history
It appears that when MAINTAINER_MODE=ON, the change in that environment
variable for the demorust ( lib/rust ) module causes the build.rs script
to be re-run.  I think that's because the MAINTAINER_MODE variable may
not be passed to `cargo test` from `ctest`, while it is passed to `cargo
build` from `cmake`.  However, when MAINTAINER_MODE=OFF there is no
change in the environment variables passed and so `build.rs` is not
re-run for `cargo test`.  As a consequence, it never links the test
executable with the required librar(ies).

The primary change in this commit to fix that is to move:
```rust
println!("cargo:rerun-if-env-changed=LIBDEMO");
```
out of the "test" match case so it applies in general.
That seems to fix it. I'm ... not 100% sure why, but it works for me!
  • Loading branch information
val-ms committed Sep 7, 2022
1 parent 8972992 commit fd293fa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app_rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn main() -> Result<(), &'static str> {
/// Use bindgen to generate Rust bindings to call into C libraries.
fn generate_rust_bindings() -> Result<(), &'static str> {
let build_dir = PathBuf::from(env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| ".".into()));
let build_include_path = format!("-I{}", build_dir.join("..").to_str().unwrap());
let build_include_path = format!("-I{}", build_dir.join(".").to_str().unwrap());

// Configure and generate bindings.
let mut builder = builder()
Expand Down Expand Up @@ -115,6 +115,8 @@ fn generate_rust_bindings() -> Result<(), &'static str> {
.write_to_file(BINDGEN_OUTPUT_FILE)
.expect("Failed to write Rust bindings to output file");

eprintln!("bindgen outputting \"{}\"", BINDGEN_OUTPUT_FILE);

Ok(())
}

Expand Down
9 changes: 6 additions & 3 deletions lib/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ fn main() -> Result<(), &'static str> {
// FindRust.cmake defines $CARGO_CMD so we can differentiate.
let cargo_cmd = env::var("CARGO_CMD").unwrap_or_else(|_| "".into());

// If this environmment variable chnages, we should re-run this script.
println!("cargo:rerun-if-env-changed=LIBDEMO");

match cargo_cmd.as_str() {
"build" => {
// Generate bindings as a part of the build.
Expand All @@ -61,8 +64,6 @@ fn main() -> Result<(), &'static str> {

"test" => {
// Link test executable with library dependencies.
println!("cargo:rerun-if-env-changed=LIBDEMO");

for var in LIB_ENV_LINK {
if !search_and_link_lib(var)? {
eprintln!("Undefined library dependency environment variable: {}", var);
Expand All @@ -88,7 +89,7 @@ fn main() -> Result<(), &'static str> {
/// Use bindgen to generate Rust bindings to call into C libraries.
fn generate_rust_bindings() -> Result<(), &'static str> {
let build_dir = PathBuf::from(env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| ".".into()));
let build_include_path = format!("-I{}", build_dir.join("..").to_str().unwrap());
let build_include_path = format!("-I{}", build_dir.join(".").to_str().unwrap());

// Configure and generate bindings.
let mut builder = builder()
Expand Down Expand Up @@ -122,6 +123,8 @@ fn generate_rust_bindings() -> Result<(), &'static str> {
.write_to_file(BINDGEN_OUTPUT_FILE)
.expect("Failed to write Rust bindings to output file");

eprintln!("bindgen outputting \"{}\"", BINDGEN_OUTPUT_FILE);

Ok(())
}

Expand Down

0 comments on commit fd293fa

Please sign in to comment.