fix(rust): watch all source and header files (#2159)

This will trigger a rebuild when any files change, instead of having to
have to trigger it manually
This commit is contained in:
Amaan Qureshi
2025-04-12 22:35:38 -04:00
committed by GitHub
parent bac37d2ed7
commit 1b98fec009

View File

@@ -73,20 +73,12 @@ fn build_with_cmake() {
// Set cmake path for the cmake crate // Set cmake path for the cmake crate
config.define("CMAKE_PROGRAM", cmake_path.to_str().unwrap()); config.define("CMAKE_PROGRAM", cmake_path.to_str().unwrap());
} }
// MSVC-specific linker flags
println!("cargo:rustc-link-arg=/FORCE:MULTIPLE");
true true
} else { } else {
// Non-MSVC setup // Non-MSVC setup
ninja_available() ninja_available()
}; };
// GNU-specific linker flags (but not on macOS)
// if compiler.is_like_gnu() && env::consts::OS != "macos" {
// println!("cargo:rustc-link-arg=-Wl,-allow-multiple-definition");
// }
// Configure build generator // Configure build generator
if has_ninja { if has_ninja {
config.generator("Ninja"); config.generator("Ninja");
@@ -165,6 +157,56 @@ fn build_with_cmake() {
} }
} }
fn watch_source_files() {
let current_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let project_root = std::path::Path::new(&current_dir)
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
println!(
"cargo:rerun-if-changed={}",
project_root.join("uc.c").display()
);
println!(
"cargo:rerun-if-changed={}",
project_root.join("list.c").display()
);
// Directories to watch for changes
let watch_dirs = vec!["qemu", "include", "bindings", "glib_compat"];
let watch_extensions = vec![".c", ".h"];
for dir in watch_dirs {
let dir_path = project_root.join(dir);
if dir_path.exists() {
register_dir_files(&dir_path, &watch_extensions);
}
}
}
fn register_dir_files(dir: &std::path::Path, extensions: &[&str]) {
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if path.is_dir() {
register_dir_files(&path, extensions);
} else if let Some(ext) = path.extension() {
if extensions
.iter()
.any(|&e| e == format!(".{}", ext.to_string_lossy()))
{
println!("cargo:rerun-if-changed={}", path.display());
}
}
}
}
}
#[derive(Debug)] #[derive(Debug)]
struct Renamer; struct Renamer;
@@ -291,7 +333,6 @@ fn generate_bindings() {
env!("CARGO_MANIFEST_DIR"), env!("CARGO_MANIFEST_DIR"),
"/../../../include/unicorn/unicorn.h" "/../../../include/unicorn/unicorn.h"
); );
println!("cargo:rerun-if-changed={HEADER_PATH}");
let bitflag_enums = [ let bitflag_enums = [
"uc_hook_type", "uc_hook_type",
@@ -325,6 +366,8 @@ fn generate_bindings() {
} }
fn main() { fn main() {
watch_source_files();
generate_bindings(); generate_bindings();
match pkg_config::Config::new() match pkg_config::Config::new()