Skip to content

Commit

Permalink
fuzz: retrieve the WebAssembly spec repository in build.rs
Browse files Browse the repository at this point in the history
To avoid the large download size of the spec repository mentioned
[here](bytecodealliance#3124 (comment)),
this change removes it as a submodule and instead clones it shallowly
when the directory is empty (or not present) when `build.rs` is run.
  • Loading branch information
abrown committed Aug 10, 2021
1 parent 92b382b commit 42acb72
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@
[submodule "crates/wasi-crypto/spec"]
path = crates/wasi-crypto/spec
url = https://github.com/WebAssembly/wasi-crypto.git
[submodule "crates/fuzzing/wasm-spec-interpreter/ocaml/spec"]
path = crates/fuzzing/wasm-spec-interpreter/ocaml/spec
url = https://github.com/WebAssembly/spec
shallow = true
33 changes: 32 additions & 1 deletion crates/fuzzing/wasm-spec-interpreter/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
/// approach to avoid missing symbols was to imitate `dune`: I observed `rm -rf
/// _build && dune build ./ocaml/interpret.exe.o --display=verbose` and used
/// that as a pattern, now encoded in `ocaml/Makefile` for easier debugging.
use std::{env, process::Command};
use std::{env, path::PathBuf, process::Command};

const LIB_NAME: &'static str = "interpret";
const OCAML_DIR: &'static str = "ocaml";
const SPEC_DIR: &'static str = "ocaml/spec";
const SPEC_REPOSITORY: &'static str = "https://github.com/bytecodealliance/wasm-spec-mirror";

fn main() {
if cfg!(feature = "build-libinterpret") {
Expand All @@ -28,6 +30,11 @@ fn build() {
println!("cargo:rustc-link-search={}", other_dir.to_str().unwrap());
println!("cargo:rustc-link-lib=static={}", LIB_NAME);
} else {
// Ensure the spec repository is present.
if is_spec_repository_empty(SPEC_DIR) {
retrieve_spec_repository(SPEC_REPOSITORY, SPEC_DIR)
}

// Build the library to link to.
build_lib(out_dir, OCAML_DIR);
println!("cargo:rustc-link-search={}", out_dir);
Expand All @@ -52,3 +59,27 @@ fn build_lib(out_dir: &str, ocaml_dir: &str) {
"Failed to build the OCaml library using 'make'."
)
}

// Check if the spec repository directory contains any files.
fn is_spec_repository_empty(destination: &str) -> bool {
PathBuf::from(destination)
.read_dir()
.map(|mut i| i.next().is_none())
.unwrap_or(true)
}

// Clone the spec repository into `destination`. This exists due to the large
// size of the dependencies (e.g. KaTeX) that are pulled if this were cloned
// recursively as a submodule.
fn retrieve_spec_repository(repository: &str, destination: &str) {
let status = Command::new("git")
.arg("clone")
.arg("--depth")
.arg("1")
.arg(repository)
.arg(destination)
.status()
.expect("Failed to execute 'git' command to clone spec repository.");

assert!(status.success(), "Failed to retrieve the spec repository.")
}
1 change: 1 addition & 0 deletions crates/fuzzing/wasm-spec-interpreter/ocaml/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
_build
spec
1 change: 0 additions & 1 deletion crates/fuzzing/wasm-spec-interpreter/ocaml/spec
Submodule spec deleted from 46cf27

0 comments on commit 42acb72

Please sign in to comment.