wasm-bindgen/crates/webidl-tests/build.rs
Alex Crichton 269c491380
Gate web-sys APIs on activated features (#790)
* Gate `web-sys` APIs on activated features

Currently the compile times of `web-sys` are unfortunately prohibitive,
increasing the barrier to using it. This commit updates the crate to instead
have all APIs gated by a set of Cargo features which affect what bindings are
generated at compile time (and which are then compiled by rustc). It's
significantly faster to activate only a handful of features vs all thousand of
them!

A magical env var is added to print the list of all features that should be
generated, and then necessary logic is added to ferry features from the build
script to the webidl crate which then uses that as a filter to remove items
after parsing. Currently parsing is pretty speedy so we'll unconditionally parse
all WebIDL files, but this may change in the future!

For now this will make the `web-sys` crate a bit less ergonomic to use as lots
of features will need to be specified, but it should make it much more
approachable in terms of first-user experience with compile times.

* Fix AppVeyor testing web-sys

* FIx a typo

* Udpate feature listings from rebase conflicts

* Add some crate docs and such
2018-09-05 12:55:30 -07:00

54 lines
1.7 KiB
Rust

extern crate wasm_bindgen_webidl;
extern crate env_logger;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
fn main() {
env_logger::init();
let idls = fs::read_dir(".")
.unwrap()
.map(|f| f.unwrap().path())
.filter(|f| f.extension().and_then(|s| s.to_str()) == Some("webidl"))
.map(|f| (fs::read_to_string(&f).unwrap(), f));
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
for (i, (idl, path)) in idls.enumerate() {
println!("processing {:?}", path);
let mut generated_rust = wasm_bindgen_webidl::compile(&idl, None).unwrap();
let out_file = out_dir.join(path.file_name().unwrap())
.with_extension("rs");
let js_file = path.with_extension("js")
.canonicalize()
.unwrap();
generated_rust.push_str(&format!(r#"
pub mod import_script {{
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[wasm_bindgen(module = r"{}")]
extern {{
fn not_actually_a_function{1}();
}}
#[wasm_bindgen_test]
fn foo() {{
if ::std::env::var("NOT_GONNA_WORK").is_ok() {{
not_actually_a_function{1}();
}}
}}
}}
"#, js_file.display(), i));
fs::write(&out_file, generated_rust).unwrap();
// Attempt to run rustfmt, but don't worry if it fails or if it isn't
// installed, this is just to help with debugging
drop(Command::new("rustfmt").arg(&out_file).status());
}
}