refactor(build): use node to download .lib file on Windows
This commit is contained in:
parent
a93f9b6433
commit
d1d6a5b963
5 changed files with 58 additions and 70 deletions
2
.github/workflows/test.yaml
vendored
2
.github/workflows/test.yaml
vendored
|
@ -30,7 +30,7 @@ jobs:
|
||||||
|
|
||||||
- name: Set llvm path
|
- name: Set llvm path
|
||||||
if: matrix.os == 'windows-latest'
|
if: matrix.os == 'windows-latest'
|
||||||
uses: allenevans/set-env@v1.0.0
|
uses: allenevans/set-env@v1.1.0
|
||||||
with:
|
with:
|
||||||
LIBCLANG_PATH: 'C:\Program Files\LLVM\bin'
|
LIBCLANG_PATH: 'C:\Program Files\LLVM\bin'
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
[package]
|
[package]
|
||||||
name = "napi-build"
|
|
||||||
version = "0.2.1"
|
|
||||||
authors = ["LongYinan <lynweklm@gmail.com>"]
|
authors = ["LongYinan <lynweklm@gmail.com>"]
|
||||||
edition = "2018"
|
|
||||||
description = "N-API build support"
|
description = "N-API build support"
|
||||||
|
edition = "2018"
|
||||||
|
keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
|
||||||
|
license = "MIT"
|
||||||
|
name = "napi-build"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository = "https://github.com/napi-rs/napi-rs"
|
repository = "https://github.com/napi-rs/napi-rs"
|
||||||
license = "MIT"
|
version = "0.2.1"
|
||||||
keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cfg-if = "1.0"
|
cfg-if = "1.0"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
|
||||||
reqwest = { version = "0.10", features = ["native-tls", "blocking"] }
|
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
extern crate cfg_if;
|
extern crate cfg_if;
|
||||||
#[cfg(windows)]
|
|
||||||
extern crate reqwest;
|
|
||||||
|
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
|
@ -8,54 +6,57 @@ use cfg_if::cfg_if;
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(windows)] {
|
if #[cfg(windows)] {
|
||||||
use std::env::var;
|
use std::fs::{create_dir, metadata, write};
|
||||||
use std::fs::{File, create_dir};
|
|
||||||
use std::io::copy;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn download_node_lib() -> Vec<u8> {
|
||||||
|
let script = format!("
|
||||||
|
require('https').get('https://nodejs.org/dist/' + process.version + '/win-x64/node.lib', (res) => {{
|
||||||
|
res.pipe(process.stdout)
|
||||||
|
}})");
|
||||||
|
|
||||||
|
Command::new("node")
|
||||||
|
.arg("-e")
|
||||||
|
.arg(script)
|
||||||
|
.output()
|
||||||
|
.expect("Download node.lib failed")
|
||||||
|
.stdout
|
||||||
|
}
|
||||||
|
|
||||||
pub fn setup() {
|
pub fn setup() {
|
||||||
let node_full_version =
|
let node_full_version =
|
||||||
String::from_utf8(Command::new("node").arg("-v").output().unwrap().stdout).unwrap();
|
String::from_utf8(Command::new("node").arg("-v").output().unwrap().stdout).unwrap();
|
||||||
|
let trim_node_full_version = node_full_version.trim_end();
|
||||||
|
let mut node_lib_file_dir =
|
||||||
|
PathBuf::from(String::from_utf8(Command::new("node").arg("-e").arg("console.log(require('os').homedir())").output().unwrap().stdout).unwrap().trim_end().to_owned());
|
||||||
|
|
||||||
let dev_dir: PathBuf = [
|
node_lib_file_dir.push(".napi-rs");
|
||||||
&var("HOMEDRIVE").expect("Get env HOMEDRIVE failed"),
|
|
||||||
&var("HOMEPATH").expect("Get env HOMEDRIVE failed"),
|
|
||||||
".napi-rs"
|
|
||||||
].iter().collect();
|
|
||||||
|
|
||||||
match create_dir(&dev_dir) {
|
match create_dir(&node_lib_file_dir) {
|
||||||
Ok(_) => {},
|
Ok(_) => {},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if err.kind() != std::io::ErrorKind::AlreadyExists {
|
if err.kind() != std::io::ErrorKind::AlreadyExists {
|
||||||
panic!("create ~/.napi-rs folder failed: {}", err)
|
panic!("create {} folder failed: {}", node_lib_file_dir.to_str().unwrap(), err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
let node_lib_file_dir = dev_dir.join(format!("node-{}.lib", node_full_version.trim_end()));
|
let link_search_dir = node_lib_file_dir.clone();
|
||||||
if !node_lib_file_dir.exists() {
|
|
||||||
let lib_file_download_url = format!(
|
node_lib_file_dir.push(format!("node-{}.lib", trim_node_full_version));
|
||||||
"https://nodejs.org/dist/{}/win-x64/node.lib",
|
|
||||||
node_full_version
|
if let Err(_) = metadata(&node_lib_file_dir) {
|
||||||
);
|
let node_lib = download_node_lib();
|
||||||
let mut resp =
|
write(&node_lib_file_dir, &node_lib).expect(&format!("Could not save file to {}", node_lib_file_dir.to_str().unwrap()));
|
||||||
reqwest::blocking::get(&lib_file_download_url).expect("Download node.lib file failed");
|
|
||||||
let mut node_lib_file = File::create(&node_lib_file_dir).unwrap();
|
|
||||||
copy(&mut resp, &mut node_lib_file).expect("Save node.lib file failed");
|
|
||||||
}
|
}
|
||||||
println!(
|
println!(
|
||||||
"cargo:rustc-link-lib={}",
|
"cargo:rustc-link-lib={}",
|
||||||
&node_lib_file_dir.file_stem().unwrap().to_str().unwrap()
|
&node_lib_file_dir.file_stem().unwrap().to_str().unwrap()
|
||||||
);
|
);
|
||||||
println!("cargo:rustc-link-search={}", dev_dir.to_str().unwrap());
|
println!("cargo:rustc-link-search=native={}", link_search_dir.to_str().unwrap());
|
||||||
// Link `win_delay_load_hook.obj` for windows electron
|
// Link `win_delay_load_hook.obj` for windows electron
|
||||||
let node_runtime_env = "npm_config_runtime";
|
println!("cargo:rustc-cdylib-link-arg=delayimp.lib");
|
||||||
println!("cargo:rerun-if-env-changed={}", node_runtime_env);
|
println!("cargo:rustc-cdylib-link-arg=/DELAYLOAD:node.exe");
|
||||||
if var(node_runtime_env).map(|s| s == "electron") == Ok(true) {
|
|
||||||
println!("cargo:rustc-cdylib-link-arg=win_delay_load_hook.obj");
|
|
||||||
println!("cargo:rustc-cdylib-link-arg=delayimp.lib");
|
|
||||||
println!("cargo:rustc-cdylib-link-arg=/DELAYLOAD:node.exe");
|
|
||||||
}
|
|
||||||
setup_napi_feature();
|
setup_napi_feature();
|
||||||
}
|
}
|
||||||
} else if #[cfg(target_os = "macos")] {
|
} else if #[cfg(target_os = "macos")] {
|
||||||
|
|
|
@ -1,23 +1,16 @@
|
||||||
[package]
|
[package]
|
||||||
name = "napi-sys"
|
|
||||||
version = "0.4.7"
|
|
||||||
authors = ["LongYinan <lynweklm@gmail.com>"]
|
authors = ["LongYinan <lynweklm@gmail.com>"]
|
||||||
edition = "2018"
|
|
||||||
readme = "README.md"
|
|
||||||
description = "NodeJS N-API raw binding"
|
description = "NodeJS N-API raw binding"
|
||||||
repository = "https://github.com/napi-rs/napi-rs"
|
edition = "2018"
|
||||||
license = "MIT"
|
|
||||||
keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
|
|
||||||
include = ["src/**/*", "Cargo.toml", "build.rs", ".node-headers/**/*"]
|
include = ["src/**/*", "Cargo.toml", "build.rs", ".node-headers/**/*"]
|
||||||
|
keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
|
||||||
|
license = "MIT"
|
||||||
|
name = "napi-sys"
|
||||||
|
readme = "README.md"
|
||||||
|
repository = "https://github.com/napi-rs/napi-rs"
|
||||||
|
version = "0.4.7"
|
||||||
|
|
||||||
[target.'cfg(windows)'.build-dependencies]
|
[target.'cfg(windows)'.build-dependencies]
|
||||||
flate2 = "1.0"
|
|
||||||
reqwest = { version = "0.10", features = ["native-tls", "blocking"] }
|
|
||||||
tar = "0.4"
|
|
||||||
|
|
||||||
[target.x86_64-unknown-linux-gnu.build-dependencies]
|
|
||||||
flate2 = "1.0"
|
|
||||||
reqwest = { version = "0.10", features = ["native-tls", "blocking"] }
|
|
||||||
tar = "0.4"
|
tar = "0.4"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
|
|
31
sys/build.rs
31
sys/build.rs
|
@ -1,9 +1,5 @@
|
||||||
extern crate bindgen;
|
extern crate bindgen;
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
extern crate flate2;
|
|
||||||
extern crate glob;
|
extern crate glob;
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
extern crate reqwest;
|
|
||||||
extern crate semver;
|
extern crate semver;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
extern crate tar;
|
extern crate tar;
|
||||||
|
@ -40,12 +36,12 @@ fn main() {
|
||||||
|
|
||||||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||||
|
|
||||||
let mut sys_bindigs_path = PathBuf::from("src");
|
let mut sys_bindings_path = PathBuf::from("src");
|
||||||
sys_bindigs_path.push("bindings.h");
|
sys_bindings_path.push("bindings.h");
|
||||||
|
|
||||||
bindgen::Builder::default()
|
bindgen::Builder::default()
|
||||||
.derive_default(true)
|
.derive_default(true)
|
||||||
.header(sys_bindigs_path.to_str().unwrap().to_owned())
|
.header(sys_bindings_path.to_str().unwrap().to_owned())
|
||||||
.clang_arg(String::from("-I") + node_include_path.to_str().unwrap())
|
.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_).+")
|
||||||
|
@ -66,16 +62,17 @@ fn find_node_include_path(node_full_version: &str) -> PathBuf {
|
||||||
header_dist_path.push("include");
|
header_dist_path.push("include");
|
||||||
header_dist_path.push("node");
|
header_dist_path.push("node");
|
||||||
if !header_dist_path.exists() {
|
if !header_dist_path.exists() {
|
||||||
let header_file_download_url = String::from_utf8(
|
let script = r#"require('https').get(process.release.headersUrl, function (res) {
|
||||||
Command::new("node")
|
res.pipe(require('zlib').createUnzip()).pipe(process.stdout)
|
||||||
.args(vec!["-e", "console.log(process.release.headersUrl)"])
|
})"#;
|
||||||
.output()
|
|
||||||
.unwrap()
|
let tar_binary = Command::new("node")
|
||||||
.stdout,
|
.arg("-e")
|
||||||
)
|
.arg(script)
|
||||||
.unwrap();
|
.output()
|
||||||
let resp = reqwest::blocking::get(&header_file_download_url).expect("request failed");
|
.expect("Download headers file failed")
|
||||||
tar::Archive::new(flate2::read::GzDecoder::new(resp))
|
.stdout;
|
||||||
|
tar::Archive::new(tar_binary.as_slice())
|
||||||
.unpack(&unpack_path)
|
.unpack(&unpack_path)
|
||||||
.expect("Unpack headers file failed");
|
.expect("Unpack headers file failed");
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue