From cbf5b42d615229981549dd5cd1761581f742d9ed Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 26 Jun 2018 16:31:42 +0800 Subject: [PATCH] build: use cargo to build napi-rs instead of napi.js --- Cargo.toml | 1 + build.rs | 48 +++++++++++++++++++++++++++++++----------- src/sys/node8.rs | 2 ++ test_module/Cargo.toml | 2 +- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4fe331a2..953cb601 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ futures = "0.1.17" bindgen = "0.32.1" cc = "1.0" glob = "0.2.11" +semver = "0.9" diff --git a/build.rs b/build.rs index ef75f02f..c1e2a459 100644 --- a/build.rs +++ b/build.rs @@ -1,23 +1,47 @@ extern crate bindgen; extern crate cc; extern crate glob; +extern crate semver; use glob::glob; use std::env; +use std::path::{Path, PathBuf}; +use std::process::Command; -fn expect_env(key: &str) -> String { - let value = env::var(key); - if value.is_err() { - eprintln!("{} environment variable is not defined.", key); - eprintln!("Make sure you're running cargo via the `napi` wrapper script to assign correct environment variables and options."); - std::process::exit(1); - }; - value.unwrap() +fn find_it

(exe_name: P) -> Option + where P: AsRef, +{ + env::var_os("PATH").and_then(|paths| { + env::split_paths(&paths).filter_map(|dir| { + let full_path = dir.join(&exe_name); + if full_path.is_file() { + Some(full_path) + } else { + None + } + }).next() + }) } fn main() { - let node_include_path = expect_env("NODE_INCLUDE_PATH"); - let node_major_version = expect_env("NODE_MAJOR_VERSION"); + let node_include_path = find_it("node") + .expect("can not find executable node") + .parent().unwrap() + .parent().unwrap() + .join("include/node"); + let node_version = semver::Version::parse( + String::from_utf8(Command::new("node") + .arg("-v") + .output() + .unwrap().stdout + ) + .unwrap() + .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/sys/**/*.*").unwrap() { @@ -27,13 +51,13 @@ fn main() { ); } - // Activate the "node8" or "node9" feature for compatibility with + // Activate the "node8" or "nodestable" feature for compatibility with // different versions of Node.js/N-API. println!("cargo:rustc-cfg=node{}", node_major_version); bindgen::Builder::default() .header("src/sys/bindings.h") - .clang_arg(String::from("-I") + &node_include_path) + .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_).+") diff --git a/src/sys/node8.rs b/src/sys/node8.rs index 298650e0..38ecf4c9 100644 --- a/src/sys/node8.rs +++ b/src/sys/node8.rs @@ -17,6 +17,7 @@ pub enum Status { EscapeCalledTwice, HandleScopeMismatch, StringContainsNull, + CallbackScopeMismatch } impl From for Status { @@ -39,6 +40,7 @@ impl From for Status { napi_cancelled => Cancelled, napi_escape_called_twice => EscapeCalledTwice, napi_handle_scope_mismatch => HandleScopeMismatch, + napi_callback_scope_mismatch => CallbackScopeMismatch, } } } diff --git a/test_module/Cargo.toml b/test_module/Cargo.toml index 6a3e8389..f6fde11c 100644 --- a/test_module/Cargo.toml +++ b/test_module/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "test-module" version = "0.1.0" -authors = ["Nathan Sobo "] +authors = ["LongYinan "] [lib] crate-type = ["dylib"]