2018-07-10 02:35:25 +03:00
|
|
|
extern crate env_logger;
|
2018-07-30 02:07:19 +03:00
|
|
|
#[macro_use]
|
2018-07-10 02:35:25 +03:00
|
|
|
extern crate failure;
|
|
|
|
extern crate wasm_bindgen_webidl;
|
2018-07-26 20:09:04 +03:00
|
|
|
extern crate sourcefile;
|
2018-07-10 02:35:25 +03:00
|
|
|
|
2018-07-26 20:09:04 +03:00
|
|
|
use failure::{Fail, ResultExt};
|
|
|
|
use sourcefile::SourceFile;
|
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;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path;
|
|
|
|
use std::process;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if let Err(e) = try_main() {
|
|
|
|
eprintln!("Error: {}", e);
|
|
|
|
for c in e.causes().skip(1) {
|
|
|
|
eprintln!(" caused by {}", c);
|
|
|
|
}
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_main() -> Result<(), failure::Error> {
|
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
println!("cargo:rerun-if-changed=webidls/enabled");
|
|
|
|
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")?;
|
|
|
|
if entry.path().extension() == Some(OsStr::new("webidl")) {
|
|
|
|
println!("cargo:rerun-if-changed={}", entry.path().display());
|
2018-07-26 20:09:04 +03:00
|
|
|
source = source.add_file(entry.path())
|
|
|
|
.with_context(|_| format!("reading contents of file \"{}\"",
|
|
|
|
entry.path().display()))?;
|
2018-07-14 19:04:20 +03:00
|
|
|
}
|
2018-07-10 02:35:25 +03:00
|
|
|
}
|
|
|
|
|
2018-07-26 20:09:04 +03:00
|
|
|
let bindings = match wasm_bindgen_webidl::compile(&source.contents) {
|
|
|
|
Ok(bindings) => bindings,
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
wasm_bindgen_webidl::ErrorKind::ParsingWebIDLSourcePos(pos) => {
|
|
|
|
if let Some(pos) = source.resolve_offset(pos) {
|
|
|
|
let ctx = format!("compiling WebIDL into wasm-bindgen bindings in file \
|
|
|
|
\"{}\", line {} column {}", pos.filename, pos.line + 1, pos.col + 1);
|
|
|
|
return Err(e.context(ctx).into());
|
|
|
|
} else {
|
|
|
|
return Err(e.context("compiling WebIDL into wasm-bindgen bindings").into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(e.context("compiling WebIDL into wasm-bindgen bindings").into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
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");
|
|
|
|
let mut out_file = fs::File::create(&out_file_path)
|
2018-07-10 02:35:25 +03:00
|
|
|
.context("creating output bindings file")?;
|
|
|
|
out_file
|
|
|
|
.write_all(bindings.as_bytes())
|
|
|
|
.context("writing bindings to output file")?;
|
|
|
|
|
2018-07-30 02:07:19 +03:00
|
|
|
// run rustfmt on the generated file - really handy for debugging
|
|
|
|
//if ! process::Command::new("rustfmt").arg(&out_file_path).status()
|
|
|
|
// .context("running rustfmt")?.success() {
|
|
|
|
// return Err(format_err!("rustfmt failed to format {}", out_file_path.display()));
|
|
|
|
//}
|
|
|
|
|
2018-07-10 02:35:25 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-07-26 20:09:04 +03:00
|
|
|
|