From 6c3792330d127a3d681e849b6557ea55605a8c20 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 12:25:30 +0200 Subject: [PATCH] do not overwrite header if content did not change --- build.rs | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/build.rs b/build.rs index 83e9641..615d00f 100644 --- a/build.rs +++ b/build.rs @@ -4,32 +4,55 @@ //! the out directory. This can be used to use the build script as a command line tool from other //! build tools. -use std::{env, fs::copy}; +use std::{env, fs}; fn main() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - println!("cargo::rerun-if-changed={crate_dir}"); + println!("cargo::rerun-if-changed={crate_dir}/src"); + println!("cargo::rerun-if-changed={crate_dir}/build.rs"); + println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml"); + println!("cargo::rerun-if-changed={crate_dir}/Cargo.lock"); + println!("cargo::rerun-if-changed={crate_dir}/cbindgen.toml"); + println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT"); let config = cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml") .unwrap(); - let output_dir = env::var("OUT_DIR").unwrap(); let header_file = output_dir.clone() + "/servicepoint.h"; - if let Ok(bindings) = cbindgen::generate_with_config(crate_dir, config) { - bindings.write_to_file(&header_file); - - println!("cargo:include={output_dir}"); - - println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT"); - if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { - let header_copy = header_out + "/servicepoint.h"; - println!("cargo:warning=Copying header to {header_copy}"); - copy(header_file, &header_copy).unwrap(); - println!("cargo::rerun-if-changed={header_copy}"); + let bindings = match cbindgen::generate_with_config(crate_dir, config) { + Ok(bindings) => bindings, + Err(e) => { + eprintln!("cargo:warning=Servicepoint header could not be generated: {e:?}"); + return; } - } else { - eprintln!("cargo:warning=Servicepoint header could not be generated"); + }; + + bindings.write_to_file(&header_file); + println!("cargo:include={output_dir}"); + + if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { + let header_copy = header_out + "/servicepoint.h"; + + if fs::exists(&header_copy).ok().unwrap_or(false) { + // check if content changed to prevent rebuild of dependents if not + + let mut bindings_text = Vec::new(); + bindings.write(&mut bindings_text); + + match fs::read(&header_copy) { + Ok(old_content) if old_content == bindings_text => { + println!("cargo:warning=Header did not change, not updating timestamp"); + return; + } + _ => {} + }; + } + + // file does not exist or is different + println!("cargo:warning=Copying header to {header_copy}"); + fs::copy(header_file, &header_copy).unwrap(); + println!("cargo::rerun-if-changed={header_copy}"); } }