Skip to content

Commit 18ab014

Browse files
authored
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 <schwenderjonathan@gmail.com> * Bump mozjs_sys to 128.3-5 Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com> --------- Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com> Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>
1 parent 8526195 commit 18ab014

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

mozjs-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "mozjs_sys"
33
description = "System crate for the Mozilla SpiderMonkey JavaScript engine."
44
repository.workspace = true
5-
version = "0.128.3-4"
5+
version = "0.128.3-5"
66
authors = ["Mozilla"]
77
links = "mozjs"
88
build = "build.rs"

mozjs-sys/build.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
use bindgen::callbacks::ParseCallbacks;
56
use bindgen::Formatter;
67
use flate2::read::GzDecoder;
78
use flate2::write::GzEncoder;
@@ -55,8 +56,7 @@ const SM_TARGET_ENV_VARS: &'static [&'static str] = &[
5556
"OBJCOPY",
5657
];
5758

58-
const EXTRA_FILES: &'static [&'static str] =
59-
&["makefile.cargo", "src/rustfmt.toml", "src/jsapi.cpp"];
59+
const EXTRA_FILES: &'static [&'static str] = &["makefile.cargo", "src/jsapi.cpp"];
6060

6161
/// Which version of moztools we expect
6262
#[cfg(windows)]
@@ -647,6 +647,10 @@ fn ignore(path: &Path) -> bool {
647647
return true;
648648
}
649649

650+
if path.ends_with("js/src/configure") {
651+
return true;
652+
}
653+
650654
let ignored_extensions = ["pyc", "o", "so", "dll", "dylib"];
651655

652656
path.extension().map_or(false, |extension| {
@@ -656,7 +660,37 @@ fn ignore(path: &Path) -> bool {
656660
})
657661
}
658662

663+
/// Customization of [`bindgen::CargoCallbacks`]
664+
///
665+
/// This accounts for generated header files, to prevent needless rebuilds
666+
#[derive(Debug)]
667+
struct CustomCargoCallbacks;
668+
669+
impl CustomCargoCallbacks {
670+
pub fn new() -> Self {
671+
Self {}
672+
}
673+
}
674+
675+
impl ParseCallbacks for CustomCargoCallbacks {
676+
fn header_file(&self, filename: &str) {
677+
println!("cargo:rerun-if-changed={}", filename);
678+
}
679+
fn include_file(&self, filename: &str) {
680+
// These header files are generated by the build-script
681+
// so cargo checking for changes would only cause needless rebuilds.
682+
if !filename.contains("dist/include") {
683+
println!("cargo:rerun-if-changed={}", filename);
684+
}
685+
}
686+
687+
fn read_env_var(&self, key: &str) {
688+
println!("cargo:rerun-if-env-changed={}", key);
689+
}
690+
}
691+
659692
mod jsglue {
693+
use crate::CustomCargoCallbacks;
660694
use std::env;
661695
use std::path::{Path, PathBuf};
662696

@@ -692,12 +726,13 @@ mod jsglue {
692726
pub fn build(outdir: &Path) {
693727
//let mut build = cxx_build::bridge("src/jsglue.rs"); // returns a cc::Build;
694728
let mut build = cc::Build::new();
695-
let include_path: PathBuf = outdir.join("dist/include");
729+
let include_path = outdir.join("dist/include");
696730

697731
build
698732
.cpp(true)
699733
.file("src/jsglue.cpp")
700734
.include(&include_path);
735+
701736
for flag in cc_flags(false) {
702737
build.flag_if_supported(flag);
703738
}
@@ -725,7 +760,7 @@ mod jsglue {
725760
println!("cargo:rerun-if-changed=src/jsglue.cpp");
726761
let mut builder = bindgen::Builder::default()
727762
.header("./src/jsglue.cpp")
728-
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
763+
.parse_callbacks(Box::new(CustomCargoCallbacks::new()))
729764
.size_t_is_usize(true)
730765
.formatter(bindgen::Formatter::Rustfmt)
731766
.clang_arg("-x")

0 commit comments

Comments
 (0)