From 18ab014e77eee726545ed544ea83d555bdfa46a3 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender <55576758+jschwe@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:33:11 +0100 Subject: [PATCH] Fix mozjs-sys always being out of date (#524) * Fix mozjs-sys always being out of date - `src/rustfmt.toml` does not exist, so always out of date - the configure script is touched by the makefile, so the second build will always be out of date, even without changes. - bindgen::CargoCallbacks will by default tell Cargo to rerun the build-script if any of the included files change. In our case some of the included files are generated by the build-script, so their mtime will always be newer than when the build-script last ran causing cargo to needlessly rerun the build-script. Writing a customized Callback that ignores generated include files avoids this problem Signed-off-by: Jonathan Schwender * Bump mozjs_sys to 128.3-5 Signed-off-by: Jonathan Schwender --------- Signed-off-by: Jonathan Schwender Signed-off-by: Jonathan Schwender --- mozjs-sys/Cargo.toml | 2 +- mozjs-sys/build.rs | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/mozjs-sys/Cargo.toml b/mozjs-sys/Cargo.toml index 5587fdb84c..ebb1facce9 100644 --- a/mozjs-sys/Cargo.toml +++ b/mozjs-sys/Cargo.toml @@ -2,7 +2,7 @@ name = "mozjs_sys" description = "System crate for the Mozilla SpiderMonkey JavaScript engine." repository.workspace = true -version = "0.128.3-4" +version = "0.128.3-5" authors = ["Mozilla"] links = "mozjs" build = "build.rs" diff --git a/mozjs-sys/build.rs b/mozjs-sys/build.rs index 10f14f3073..12639c33fe 100644 --- a/mozjs-sys/build.rs +++ b/mozjs-sys/build.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use bindgen::callbacks::ParseCallbacks; use bindgen::Formatter; use flate2::read::GzDecoder; use flate2::write::GzEncoder; @@ -55,8 +56,7 @@ const SM_TARGET_ENV_VARS: &'static [&'static str] = &[ "OBJCOPY", ]; -const EXTRA_FILES: &'static [&'static str] = - &["makefile.cargo", "src/rustfmt.toml", "src/jsapi.cpp"]; +const EXTRA_FILES: &'static [&'static str] = &["makefile.cargo", "src/jsapi.cpp"]; /// Which version of moztools we expect #[cfg(windows)] @@ -647,6 +647,10 @@ fn ignore(path: &Path) -> bool { return true; } + if path.ends_with("js/src/configure") { + return true; + } + let ignored_extensions = ["pyc", "o", "so", "dll", "dylib"]; path.extension().map_or(false, |extension| { @@ -656,7 +660,37 @@ fn ignore(path: &Path) -> bool { }) } +/// Customization of [`bindgen::CargoCallbacks`] +/// +/// This accounts for generated header files, to prevent needless rebuilds +#[derive(Debug)] +struct CustomCargoCallbacks; + +impl CustomCargoCallbacks { + pub fn new() -> Self { + Self {} + } +} + +impl ParseCallbacks for CustomCargoCallbacks { + fn header_file(&self, filename: &str) { + println!("cargo:rerun-if-changed={}", filename); + } + fn include_file(&self, filename: &str) { + // These header files are generated by the build-script + // so cargo checking for changes would only cause needless rebuilds. + if !filename.contains("dist/include") { + println!("cargo:rerun-if-changed={}", filename); + } + } + + fn read_env_var(&self, key: &str) { + println!("cargo:rerun-if-env-changed={}", key); + } +} + mod jsglue { + use crate::CustomCargoCallbacks; use std::env; use std::path::{Path, PathBuf}; @@ -692,12 +726,13 @@ mod jsglue { pub fn build(outdir: &Path) { //let mut build = cxx_build::bridge("src/jsglue.rs"); // returns a cc::Build; let mut build = cc::Build::new(); - let include_path: PathBuf = outdir.join("dist/include"); + let include_path = outdir.join("dist/include"); build .cpp(true) .file("src/jsglue.cpp") .include(&include_path); + for flag in cc_flags(false) { build.flag_if_supported(flag); } @@ -725,7 +760,7 @@ mod jsglue { println!("cargo:rerun-if-changed=src/jsglue.cpp"); let mut builder = bindgen::Builder::default() .header("./src/jsglue.cpp") - .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + .parse_callbacks(Box::new(CustomCargoCallbacks::new())) .size_t_is_usize(true) .formatter(bindgen::Formatter::Rustfmt) .clang_arg("-x")