Skip to content

Commit

Permalink
Introduce CFG.change_detection bool flag
Browse files Browse the repository at this point in the history
Adds `cargo:rerun-if-changed=<path>` for all files used by the bridge if the `CFG.change_detection` is `true`.
  • Loading branch information
nyurik committed Mar 6, 2025
1 parent 82765ef commit f40d72c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ cxx-build = "1.0"
// build.rs

fn main() {
cxx_build::CFG.change_detection = true;
cxx_build::bridge("src/main.rs") // returns a cc::Build
.file("src/demo.cc")
.std("c++11")
.compile("cxxbridge-demo");

println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/demo.cc");
println!("cargo:rerun-if-changed=include/demo.h");
}
Expand Down
2 changes: 1 addition & 1 deletion book/src/build/cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ set up any additional source files and compiler flags as normal.
// build.rs
fn main() {
cxx_build::CFG.change_detection = true;
cxx_build::bridge("src/main.rs") // returns a cc::Build
.file("src/demo.cc")
.std("c++11")
.compile("cxxbridge-demo");
println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/demo.cc");
println!("cargo:rerun-if-changed=include/demo.h");
}
Expand Down
2 changes: 1 addition & 1 deletion book/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ build.
// build.rs
fn main() {
cxx_build::CFG.change_detection = true;
cxx_build::bridge("src/main.rs")
.file("src/blobstore.cc")
.compile("cxx-demo");
println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/blobstore.cc");
println!("cargo:rerun-if-changed=include/blobstore.h");
}
Expand Down
2 changes: 1 addition & 1 deletion demo/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn main() {
cxx_build::CFG.change_detection = true;
cxx_build::bridge("src/main.rs")
.file("src/blobstore.cc")
.std("c++14")
.compile("cxxbridge-demo");

println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/blobstore.cc");
println!("cargo:rerun-if-changed=include/blobstore.h");
}
11 changes: 11 additions & 0 deletions gen/build/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct Cfg<'a> {
pub exported_header_links: Vec<&'a str>,
/// See [`CFG.doxygen`][CFG#cfgdoxygen].
pub doxygen: bool,
/// See [`CFG.change_detection`][CFG#cfgchange_detection].
pub change_detection: bool,
marker: PhantomData<*const ()>, // !Send + !Sync
}

Expand Down Expand Up @@ -323,6 +325,7 @@ impl<'a> Debug for Cfg<'a> {
exported_header_prefixes,
exported_header_links,
doxygen,
change_detection,
marker: _,
} = self;
formatter
Expand All @@ -332,6 +335,7 @@ impl<'a> Debug for Cfg<'a> {
.field("exported_header_prefixes", exported_header_prefixes)
.field("exported_header_links", exported_header_links)
.field("doxygen", doxygen)
.field("change_detection", change_detection)
.finish()
}
}
Expand All @@ -356,6 +360,7 @@ mod r#impl {
exported_header_prefixes: Vec<InternedString>,
exported_header_links: Vec<InternedString>,
doxygen: bool,
change_detection: bool,
}

impl CurrentCfg {
Expand All @@ -367,12 +372,14 @@ mod r#impl {
let exported_header_prefixes = Vec::new();
let exported_header_links = Vec::new();
let doxygen = false;
let change_detection = false;
CurrentCfg {
include_prefix,
exported_header_dirs,
exported_header_prefixes,
exported_header_links,
doxygen,
change_detection,
}
}
}
Expand Down Expand Up @@ -409,12 +416,14 @@ mod r#impl {
let exported_header_prefixes = current.exported_header_prefixes.vec();
let exported_header_links = current.exported_header_links.vec();
let doxygen = current.doxygen;
let change_detection = current.change_detection;
super::Cfg {
include_prefix,
exported_header_dirs,
exported_header_prefixes,
exported_header_links,
doxygen,
change_detection,
marker: PhantomData,
}
}
Expand Down Expand Up @@ -481,6 +490,7 @@ mod r#impl {
exported_header_prefixes,
exported_header_links,
doxygen,
change_detection,
marker: _,
} = cfg;
let mut current = current().write().unwrap_or_else(PoisonError::into_inner);
Expand All @@ -489,6 +499,7 @@ mod r#impl {
current.exported_header_prefixes = vec::intern(exported_header_prefixes);
current.exported_header_links = vec::intern(exported_header_links);
current.doxygen = *doxygen;
current.change_detection = *change_detection;
} else {
CONST_DEREFS.with(|derefs| derefs.borrow_mut().remove(&self.handle()));
}
Expand Down
5 changes: 4 additions & 1 deletion gen/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
//! // build.rs
//!
//! fn main() {
//! cxx_build::CFG.change_detection = true;
//! cxx_build::bridge("src/main.rs")
//! .file("src/demo.cc")
//! .std("c++11")
//! .compile("cxxbridge-demo");
//!
//! println!("cargo:rerun-if-changed=src/main.rs");
//! println!("cargo:rerun-if-changed=src/demo.cc");
//! println!("cargo:rerun-if-changed=include/demo.h");
//! }
Expand Down Expand Up @@ -397,6 +397,9 @@ fn generate_bridge(prj: &Project, build: &mut Build, rust_source_file: &Path) ->
doxygen: CFG.doxygen,
..Opt::default()
};
if CFG.change_detection {
println!("cargo:rerun-if-changed={}", rust_source_file.display());
}
let generated = gen::generate_from_path(rust_source_file, &opt);
let ref rel_path = paths::local_relative_path(rust_source_file);

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@
//! // build.rs
//!
//! fn main() {
//! cxx_build::CFG.change_detection = true;
//! cxx_build::bridge("src/main.rs") // returns a cc::Build
//! .file("src/demo.cc")
//! .std("c++11")
//! .compile("cxxbridge-demo");
//!
//! println!("cargo:rerun-if-changed=src/main.rs");
//! println!("cargo:rerun-if-changed=src/demo.cc");
//! println!("cargo:rerun-if-changed=include/demo.h");
//! }
Expand Down

0 comments on commit f40d72c

Please sign in to comment.