2018-07-10 02:35:25 +03:00
|
|
|
extern crate env_logger;
|
2018-08-04 00:39:33 +03:00
|
|
|
#[macro_use]
|
2018-07-10 02:35:25 +03:00
|
|
|
extern crate failure;
|
2018-07-26 20:09:04 +03:00
|
|
|
extern crate sourcefile;
|
2018-09-26 18:26:00 +03:00
|
|
|
extern crate wasm_bindgen_webidl;
|
2018-07-10 02:35:25 +03:00
|
|
|
|
2018-07-26 20:09:04 +03:00
|
|
|
use failure::{Fail, ResultExt};
|
|
|
|
use sourcefile::SourceFile;
|
2018-09-05 22:55:30 +03:00
|
|
|
use std::collections::HashSet;
|
2018-07-10 02:35:25 +03:00
|
|
|
use std::env;
|
2018-07-14 19:04:20 +03:00
|
|
|
use std::ffi::OsStr;
|
2018-07-10 02:35:25 +03:00
|
|
|
use std::fs;
|
2018-09-05 22:55:30 +03:00
|
|
|
use std::path::{self, PathBuf};
|
2018-08-04 00:39:33 +03:00
|
|
|
use std::process::{self, Command};
|
2018-07-10 02:35:25 +03:00
|
|
|
|
|
|
|
fn main() {
|
2018-08-09 23:38:37 +03:00
|
|
|
env_logger::init();
|
|
|
|
|
2018-07-10 02:35:25 +03:00
|
|
|
if let Err(e) = try_main() {
|
|
|
|
eprintln!("Error: {}", e);
|
2018-08-02 00:15:09 +03:00
|
|
|
for c in e.iter_causes() {
|
2018-07-10 02:35:25 +03:00
|
|
|
eprintln!(" caused by {}", c);
|
|
|
|
}
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_main() -> Result<(), failure::Error> {
|
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
println!("cargo:rerun-if-changed=webidls/enabled");
|
|
|
|
|
2018-08-09 23:38:37 +03:00
|
|
|
let entries = fs::read_dir("webidls/enabled").context("reading webidls/enabled directory")?;
|
2018-07-26 20:09:04 +03:00
|
|
|
let mut source = SourceFile::default();
|
2018-07-10 02:35:25 +03:00
|
|
|
for entry in entries {
|
2018-07-14 19:04:20 +03:00
|
|
|
let entry = entry.context("getting webidls/enabled/*.webidl entry")?;
|
2018-08-04 00:39:33 +03:00
|
|
|
let path = entry.path();
|
|
|
|
if path.extension() != Some(OsStr::new("webidl")) {
|
2018-09-26 18:26:00 +03:00
|
|
|
continue;
|
2018-07-14 19:04:20 +03:00
|
|
|
}
|
2018-08-04 00:39:33 +03:00
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
2018-09-26 18:26:00 +03:00
|
|
|
source = source
|
|
|
|
.add_file(&path)
|
2018-08-09 23:38:37 +03:00
|
|
|
.with_context(|_| format!("reading contents of file \"{}\"", path.display()))?;
|
2018-07-10 02:35:25 +03:00
|
|
|
}
|
|
|
|
|
2018-09-05 22:55:30 +03:00
|
|
|
// Read our manifest, learn all `[feature]` directives with "toml parsing".
|
|
|
|
// Use all these names to match against environment variables set by Cargo
|
|
|
|
// to figure out which features are activated to we can pass that down to
|
|
|
|
// the webidl compiler.
|
|
|
|
let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
let manifest = fs::read_to_string(manifest_dir.join("Cargo.toml"))?;
|
2018-09-26 18:26:00 +03:00
|
|
|
let features = manifest
|
|
|
|
.lines()
|
|
|
|
.skip_while(|f| !f.starts_with("[features]"));
|
2018-09-05 22:55:30 +03:00
|
|
|
|
|
|
|
let enabled_features = env::vars()
|
|
|
|
.map(|p| p.0)
|
|
|
|
.filter(|p| p.starts_with("CARGO_FEATURE_"))
|
|
|
|
.map(|mut p| {
|
|
|
|
p.drain(0.."CARGO_FEATURE_".len());
|
|
|
|
p
|
2018-09-26 18:26:00 +03:00
|
|
|
}).collect::<HashSet<_>>();
|
2018-09-05 22:55:30 +03:00
|
|
|
|
|
|
|
let mut allowed = Vec::new();
|
|
|
|
for feature in features.filter(|f| !f.starts_with("#") && !f.starts_with("[")) {
|
|
|
|
let mut parts = feature.split('=');
|
|
|
|
let name = parts.next().unwrap().trim();
|
|
|
|
if enabled_features.contains(&name.to_uppercase()) {
|
|
|
|
allowed.push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're printing all features don't filter anything
|
2018-09-25 21:25:14 +03:00
|
|
|
println!("cargo:rerun-if-env-changed=__WASM_BINDGEN_DUMP_FEATURES");
|
2018-09-05 22:55:30 +03:00
|
|
|
let allowed = if env::var("__WASM_BINDGEN_DUMP_FEATURES").is_ok() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(&allowed[..])
|
|
|
|
};
|
|
|
|
|
|
|
|
let bindings = match wasm_bindgen_webidl::compile(&source.contents, allowed) {
|
2018-07-26 20:09:04 +03:00
|
|
|
Ok(bindings) => bindings,
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
wasm_bindgen_webidl::ErrorKind::ParsingWebIDLSourcePos(pos) => {
|
|
|
|
if let Some(pos) = source.resolve_offset(pos) {
|
2018-09-26 18:26:00 +03:00
|
|
|
let ctx = format!(
|
|
|
|
"compiling WebIDL into wasm-bindgen bindings in file \
|
|
|
|
\"{}\", line {} column {}",
|
|
|
|
pos.filename,
|
|
|
|
pos.line + 1,
|
|
|
|
pos.col + 1
|
|
|
|
);
|
2018-07-26 20:09:04 +03:00
|
|
|
return Err(e.context(ctx).into());
|
|
|
|
} else {
|
2018-09-26 18:26:00 +03:00
|
|
|
return Err(e
|
|
|
|
.context("compiling WebIDL into wasm-bindgen bindings")
|
|
|
|
.into());
|
2018-07-26 20:09:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2018-09-26 18:26:00 +03:00
|
|
|
return Err(e
|
|
|
|
.context("compiling WebIDL into wasm-bindgen bindings")
|
|
|
|
.into());
|
2018-07-26 20:09:04 +03:00
|
|
|
}
|
2018-09-26 18:26:00 +03:00
|
|
|
},
|
2018-07-26 20:09:04 +03:00
|
|
|
};
|
2018-07-10 02:35:25 +03:00
|
|
|
|
|
|
|
let out_dir = env::var("OUT_DIR").context("reading OUT_DIR environment variable")?;
|
2018-07-30 02:07:19 +03:00
|
|
|
let out_file_path = path::Path::new(&out_dir).join("bindings.rs");
|
2018-09-26 18:26:00 +03:00
|
|
|
fs::write(&out_file_path, bindings).context("writing bindings to output file")?;
|
2018-10-08 20:15:51 +03:00
|
|
|
println!("cargo:rustc-env=BINDINGS={}", out_file_path.display());
|
2018-07-10 02:35:25 +03:00
|
|
|
|
2018-07-30 02:07:19 +03:00
|
|
|
// run rustfmt on the generated file - really handy for debugging
|
2018-08-08 01:50:27 +03:00
|
|
|
println!("cargo:rerun-if-env-changed=WEBIDL_RUSTFMT_BINDINGS");
|
2018-08-04 00:39:33 +03:00
|
|
|
if env::var("WEBIDL_RUSTFMT_BINDINGS").is_ok() {
|
|
|
|
let status = Command::new("rustfmt")
|
|
|
|
.arg(&out_file_path)
|
|
|
|
.status()
|
2018-08-09 23:38:37 +03:00
|
|
|
.context("running rustfmt")?;
|
2018-08-04 00:39:33 +03:00
|
|
|
if !status.success() {
|
2018-08-09 23:38:37 +03:00
|
|
|
bail!("rustfmt failed: {}", status)
|
2018-08-04 00:39:33 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 02:07:19 +03:00
|
|
|
|
2018-07-10 02:35:25 +03:00
|
|
|
Ok(())
|
|
|
|
}
|