build: use cargo to build napi-rs instead of napi.js

This commit is contained in:
LongYinan 2018-06-26 16:31:42 +08:00
parent 7fc461aa28
commit cbf5b42d61
No known key found for this signature in database
GPG key ID: D05299933DBE44D3
4 changed files with 40 additions and 13 deletions

View file

@ -11,3 +11,4 @@ futures = "0.1.17"
bindgen = "0.32.1" bindgen = "0.32.1"
cc = "1.0" cc = "1.0"
glob = "0.2.11" glob = "0.2.11"
semver = "0.9"

View file

@ -1,23 +1,47 @@
extern crate bindgen; extern crate bindgen;
extern crate cc; extern crate cc;
extern crate glob; extern crate glob;
extern crate semver;
use glob::glob; use glob::glob;
use std::env; use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
fn expect_env(key: &str) -> String { fn find_it<P>(exe_name: P) -> Option<PathBuf>
let value = env::var(key); where P: AsRef<Path>,
if value.is_err() { {
eprintln!("{} environment variable is not defined.", key); env::var_os("PATH").and_then(|paths| {
eprintln!("Make sure you're running cargo via the `napi` wrapper script to assign correct environment variables and options."); env::split_paths(&paths).filter_map(|dir| {
std::process::exit(1); let full_path = dir.join(&exe_name);
}; if full_path.is_file() {
value.unwrap() Some(full_path)
} else {
None
}
}).next()
})
} }
fn main() { fn main() {
let node_include_path = expect_env("NODE_INCLUDE_PATH"); let node_include_path = find_it("node")
let node_major_version = expect_env("NODE_MAJOR_VERSION"); .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"); println!("cargo:rerun-if-env-changed=NODE_INCLUDE_PATH");
for entry in glob("./src/sys/**/*.*").unwrap() { 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. // different versions of Node.js/N-API.
println!("cargo:rustc-cfg=node{}", node_major_version); println!("cargo:rustc-cfg=node{}", node_major_version);
bindgen::Builder::default() bindgen::Builder::default()
.header("src/sys/bindings.h") .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_).+") .rustified_enum("(napi_|uv_).+")
.whitelist_function("(napi_|uv_|extras_).+") .whitelist_function("(napi_|uv_|extras_).+")
.whitelist_type("(napi_|uv_|extras_).+") .whitelist_type("(napi_|uv_|extras_).+")

View file

@ -17,6 +17,7 @@ pub enum Status {
EscapeCalledTwice, EscapeCalledTwice,
HandleScopeMismatch, HandleScopeMismatch,
StringContainsNull, StringContainsNull,
CallbackScopeMismatch
} }
impl From<napi_status> for Status { impl From<napi_status> for Status {
@ -39,6 +40,7 @@ impl From<napi_status> for Status {
napi_cancelled => Cancelled, napi_cancelled => Cancelled,
napi_escape_called_twice => EscapeCalledTwice, napi_escape_called_twice => EscapeCalledTwice,
napi_handle_scope_mismatch => HandleScopeMismatch, napi_handle_scope_mismatch => HandleScopeMismatch,
napi_callback_scope_mismatch => CallbackScopeMismatch,
} }
} }
} }

View file

@ -1,7 +1,7 @@
[package] [package]
name = "test-module" name = "test-module"
version = "0.1.0" version = "0.1.0"
authors = ["Nathan Sobo <nathan@github.com>"] authors = ["LongYinan <lynweklm@gmail.com>"]
[lib] [lib]
crate-type = ["dylib"] crate-type = ["dylib"]