2020-07-15 01:43:20 +09:00
|
|
|
extern crate bindgen;
|
|
|
|
extern crate glob;
|
|
|
|
extern crate semver;
|
2020-08-14 00:38:01 +09:00
|
|
|
#[cfg(target_os = "windows")]
|
2020-07-15 01:43:20 +09:00
|
|
|
extern crate tar;
|
|
|
|
|
|
|
|
use glob::glob;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
|
|
|
|
2020-08-24 20:47:31 +09:00
|
|
|
#[cfg(not(any(target_os = "windows", napidocsrs)))]
|
2020-07-15 01:43:20 +09:00
|
|
|
const NODE_PRINT_EXEC_PATH: &'static str = "console.log(process.execPath)";
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let node_full_version =
|
|
|
|
String::from_utf8(Command::new("node").arg("-v").output().unwrap().stdout).unwrap();
|
|
|
|
let node_version = semver::Version::parse(node_full_version.as_str().get(1..).unwrap()).unwrap();
|
|
|
|
|
|
|
|
let node_major_version = node_version.major;
|
|
|
|
|
|
|
|
println!("cargo:rerun-if-env-changed=NODE_INCLUDE_PATH");
|
|
|
|
for entry in glob("./src/**/*.*").unwrap() {
|
|
|
|
println!(
|
|
|
|
"cargo:rerun-if-changed={}",
|
|
|
|
entry.unwrap().to_str().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if node_major_version < 10 {
|
|
|
|
panic!("node version is too low")
|
|
|
|
}
|
|
|
|
|
|
|
|
let node_include_path = find_node_include_path(node_full_version.trim_end());
|
|
|
|
|
|
|
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
|
2020-10-09 22:37:14 +09:00
|
|
|
let mut sys_bindings_path = PathBuf::from("src");
|
|
|
|
sys_bindings_path.push("bindings.h");
|
2020-07-15 01:43:20 +09:00
|
|
|
|
|
|
|
bindgen::Builder::default()
|
2020-08-24 21:19:47 +09:00
|
|
|
.derive_default(true)
|
2020-10-09 22:37:14 +09:00
|
|
|
.header(sys_bindings_path.to_str().unwrap().to_owned())
|
2020-07-15 01:43:20 +09:00
|
|
|
.clang_arg(String::from("-I") + node_include_path.to_str().unwrap())
|
|
|
|
.rustified_enum("(napi_|uv_).+")
|
|
|
|
.whitelist_function("(napi_|uv_|extras_).+")
|
|
|
|
.whitelist_type("(napi_|uv_|extras_).+")
|
|
|
|
.generate()
|
|
|
|
.expect("Unable to generate napi bindings")
|
|
|
|
.write_to_file(out_path.join("bindings.rs"))
|
|
|
|
.expect("Unable to write napi bindings");
|
|
|
|
}
|
|
|
|
|
2020-08-24 20:47:31 +09:00
|
|
|
#[cfg(all(target_os = "windows", not(napidocsrs)))]
|
2020-07-15 01:43:20 +09:00
|
|
|
fn find_node_include_path(node_full_version: &str) -> PathBuf {
|
2020-08-03 13:21:22 +09:00
|
|
|
let mut out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
out_path.push(format!("node-headers-{}.tar.gz", node_full_version));
|
|
|
|
let mut header_dist_path = PathBuf::from(&PathBuf::from(&out_path).parent().unwrap());
|
2020-07-15 01:43:20 +09:00
|
|
|
let unpack_path = PathBuf::from(&header_dist_path);
|
|
|
|
header_dist_path.push(format!("node-{}", node_full_version));
|
|
|
|
header_dist_path.push("include");
|
|
|
|
header_dist_path.push("node");
|
|
|
|
if !header_dist_path.exists() {
|
2020-10-09 22:37:14 +09:00
|
|
|
let script = r#"require('https').get(process.release.headersUrl, function (res) {
|
|
|
|
res.pipe(require('zlib').createUnzip()).pipe(process.stdout)
|
|
|
|
})"#;
|
|
|
|
|
|
|
|
let tar_binary = Command::new("node")
|
|
|
|
.arg("-e")
|
|
|
|
.arg(script)
|
|
|
|
.output()
|
|
|
|
.expect("Download headers file failed")
|
|
|
|
.stdout;
|
|
|
|
tar::Archive::new(tar_binary.as_slice())
|
2020-07-15 01:43:20 +09:00
|
|
|
.unpack(&unpack_path)
|
|
|
|
.expect("Unpack headers file failed");
|
|
|
|
};
|
|
|
|
header_dist_path
|
|
|
|
}
|
|
|
|
|
2020-08-24 20:47:31 +09:00
|
|
|
#[cfg(napidocsrs)]
|
2020-08-14 00:38:01 +09:00
|
|
|
fn find_node_include_path(_node_full_version: &str) -> PathBuf {
|
|
|
|
let mut current = env::current_dir().unwrap();
|
|
|
|
current.push(".node-headers");
|
|
|
|
current
|
|
|
|
}
|
|
|
|
|
2020-08-24 20:47:31 +09:00
|
|
|
#[cfg(not(any(target_os = "windows", napidocsrs)))]
|
2020-07-15 01:43:20 +09:00
|
|
|
fn find_node_include_path(_node_full_version: &str) -> PathBuf {
|
|
|
|
let node_exec_path = String::from_utf8(
|
|
|
|
Command::new("node")
|
|
|
|
.arg("-e")
|
|
|
|
.arg(NODE_PRINT_EXEC_PATH)
|
|
|
|
.output()
|
|
|
|
.unwrap()
|
|
|
|
.stdout,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
PathBuf::from(node_exec_path)
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("include/node")
|
|
|
|
}
|