Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MOZJS_MIRROR and MOZJS_CREATE_MIRROR env variables #443

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env:
RUST_BACKTRACE: 1
SHELL: /bin/bash
CARGO_INCREMENTAL: 0
MOZJS_CREATE_MIRROR: 1

jobs:
mac:
Expand Down
2 changes: 2 additions & 0 deletions mozjs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ libz-sys = "1.1"
bindgen.workspace = true
cc.workspace = true
walkdir = "2"
flate2 = "1"
tar = "0.4"
107 changes: 100 additions & 7 deletions mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use bindgen::Formatter;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
use tar::Archive;
use walkdir::WalkDir;

const ENV_VARS: &'static [&'static str] = &[
Expand Down Expand Up @@ -52,12 +56,21 @@ fn main() {
// Used by mozjs downstream, don't remove.
println!("cargo:outdir={}", build_dir.display());

fs::create_dir_all(&build_dir).expect("could not create build dir");

build_spidermonkey(&build_dir);
build_jsapi(&build_dir);
build_jsapi_bindings(&build_dir);
jsglue::build(&build_dir);
let mirror = env::var_os("MOZJS_MIRROR");
if mirror.is_some() {
download_static_lib_binaries(&PathBuf::from(mirror.unwrap()), &build_dir);
} else {
fs::create_dir_all(&build_dir).expect("could not create build dir");
build_spidermonkey(&build_dir);
build_jsapi(&build_dir);
build_jsapi_bindings(&build_dir);
jsglue::build(&build_dir);

// If this env variable is set, create the compressed tarball of spidermonkey.
if env::var_os("MOZJS_CREATE_MIRROR").is_some() {
compress_static_lib(&build_dir).expect("Failed to compress static lib binaries.");
}
}

if env::var_os("MOZJS_FORCE_RERUN").is_none() {
for var in ENV_VARS {
Expand Down Expand Up @@ -615,6 +628,7 @@ mod jsglue {
false
};

build.out_dir(outdir);
build.compile("jsglue");
println!("cargo:rerun-if-changed=src/jsglue.cpp");
let mut builder = bindgen::Builder::default()
Expand Down Expand Up @@ -702,3 +716,82 @@ mod jsglue {
("root::JS", "pub(crate) use crate::jsapi::JS::*;"),
];
}

/// Compress spidermonkey build into a tarball with necessary static binaries and bindgen wrappers.
fn compress_static_lib(build_dir: &Path) -> Result<(), std::io::Error> {
// // Strip symbols from the static binary since it could bump up to 1.6GB on Linux.
// // TODO: Maybe we could separate symbols for thos who still want the debug ability.
// // https://github.com/GabrielMajeri/separate-symbols
// let status = Command::new("strip")
// .arg(build_dir.join("js/src/build/libjs_static.a"))
// .status()
// .unwrap();
// assert!(status.success());

let tar_gz = File::create("libjs.tar.gz")?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
// This is the static library of spidermonkey.
tar.append_file(
"libjs_static.a",
&mut File::open(build_dir.join("js/src/build/libjs_static.a")).unwrap(),
)?;
// The bindgen binaries and generated rust files for mozjs.
tar.append_file(
"libjsapi.a",
&mut File::open(build_dir.join("libjsapi.a")).unwrap(),
)?;
tar.append_file(
"libjsglue.a",
&mut File::open(build_dir.join("libjsglue.a")).unwrap(),
)?;
tar.append_file(
"jsapi.rs",
&mut File::open(build_dir.join("jsapi.rs")).unwrap(),
)?;
tar.append_file(
"gluebindings.rs",
&mut File::open(build_dir.join("gluebindings.rs")).unwrap(),
)?;
Ok(())
}

/// Decompress the mirror of spidermonkey build to to build directory.
fn decompress_static_lib(mirror: &Path, build_dir: &Path) -> Result<(), std::io::Error> {
let tar_gz = File::open(mirror)?;
let tar = GzDecoder::new(tar_gz);
let mut archive = Archive::new(tar);
archive.unpack(build_dir)?;
Ok(())
}

/// Download static library tarball instead of building it from source.
fn download_static_lib_binaries(mirror: &Path, build_dir: &Path) {
// Only download the files if build directory doesn't exist.
if !build_dir.exists() {
// TODO download from https
decompress_static_lib(mirror, build_dir).expect("Failed to decompress statuc libs");
}

// Link static lib binaries
let target = env::var("TARGET").unwrap();
println!("cargo:rustc-link-search=native={}", build_dir.display());
println!("cargo:rustc-link-lib=static=js_static"); // Must come before c++
if target.contains("windows") {
println!("cargo:rustc-link-search=native={}", build_dir.display());
println!("cargo:rustc-link-lib=winmm");
println!("cargo:rustc-link-lib=psapi");
println!("cargo:rustc-link-lib=user32");
println!("cargo:rustc-link-lib=Dbghelp");
if target.contains("gnu") {
println!("cargo:rustc-link-lib=stdc++");
}
} else if target.contains("apple") || target.contains("freebsd") {
println!("cargo:rustc-link-lib=c++");
} else {
println!("cargo:rustc-link-lib=stdc++");
}
// Link bindgen binaries
println!("cargo:rustc-link-lib=static=jsapi");
println!("cargo:rustc-link-lib=static=jsglue");
}