From 8280399a74dcd730eadb5e960020f83476af7559 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Thu, 6 Mar 2025 13:42:07 -0500 Subject: [PATCH] Introduce `CFG.change_detection` bool flag Adds `cargo:rerun-if-changed=` for all files used by the bridge if the `CFG.change_detection` is `true`. --- README.md | 2 +- book/src/build/cargo.md | 2 +- book/src/tutorial.md | 2 +- demo/build.rs | 2 +- gen/build/src/cfg.rs | 12 ++++++++++++ gen/build/src/lib.rs | 5 ++++- src/lib.rs | 2 +- 7 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9c6dab1b8..87535c7db 100644 --- a/README.md +++ b/README.md @@ -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"); } diff --git a/book/src/build/cargo.md b/book/src/build/cargo.md index 6e9af8027..81391edb8 100644 --- a/book/src/build/cargo.md +++ b/book/src/build/cargo.md @@ -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"); } diff --git a/book/src/tutorial.md b/book/src/tutorial.md index 1182dc2c8..6d7906992 100644 --- a/book/src/tutorial.md +++ b/book/src/tutorial.md @@ -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"); } diff --git a/demo/build.rs b/demo/build.rs index 7e19892a9..8b43cebad 100644 --- a/demo/build.rs +++ b/demo/build.rs @@ -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"); } diff --git a/gen/build/src/cfg.rs b/gen/build/src/cfg.rs index 474c11d38..213b8897e 100644 --- a/gen/build/src/cfg.rs +++ b/gen/build/src/cfg.rs @@ -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 } @@ -312,6 +314,7 @@ pub static mut CFG: Cfg = Cfg { exported_header_prefixes: Vec::new(), exported_header_links: Vec::new(), doxygen: false, + change_detection: false, marker: PhantomData, }; @@ -323,6 +326,7 @@ impl<'a> Debug for Cfg<'a> { exported_header_prefixes, exported_header_links, doxygen, + change_detection, marker: _, } = self; formatter @@ -332,6 +336,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() } } @@ -356,6 +361,7 @@ mod r#impl { exported_header_prefixes: Vec, exported_header_links: Vec, doxygen: bool, + change_detection: bool, } impl CurrentCfg { @@ -367,12 +373,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, } } } @@ -409,12 +417,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, } } @@ -481,6 +491,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); @@ -489,6 +500,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())); } diff --git a/gen/build/src/lib.rs b/gen/build/src/lib.rs index b30856703..92bd8f128 100644 --- a/gen/build/src/lib.rs +++ b/gen/build/src/lib.rs @@ -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"); //! } @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 7a8a2a758..2bb8d6c49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); //! }