Skip to content

Commit

Permalink
Fix mozjs-sys always being out of date (#524)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Bump mozjs_sys to 128.3-5

Signed-off-by: Jonathan Schwender <[email protected]>

---------

Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
  • Loading branch information
jschwe authored Nov 22, 2024
1 parent 8526195 commit 18ab014
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mozjs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 39 additions & 4 deletions mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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| {
Expand All @@ -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};

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 18ab014

Please sign in to comment.