2020-02-18 22:09:17 +09:00
|
|
|
extern crate cfg_if;
|
|
|
|
|
2020-07-01 21:44:29 +09:00
|
|
|
use std::process::Command;
|
|
|
|
|
2020-02-18 22:09:17 +09:00
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(windows)] {
|
2020-10-09 22:37:14 +09:00
|
|
|
use std::fs::{create_dir, metadata, write};
|
2020-02-18 22:09:17 +09:00
|
|
|
use std::path::PathBuf;
|
2020-10-31 09:57:28 +09:00
|
|
|
use std::io::Read;
|
2020-02-18 22:09:17 +09:00
|
|
|
|
2020-10-31 09:57:28 +09:00
|
|
|
fn get_node_version() -> std::io::Result<String> {
|
|
|
|
let output = Command::new("node").arg("-v").output()?;
|
|
|
|
let stdout_str = String::from_utf8_lossy(&output.stdout);
|
|
|
|
|
|
|
|
// version should not have a leading "v" or trailing whitespace
|
|
|
|
Ok(stdout_str.trim().trim_start_matches('v').to_string())
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:00:16 +09:00
|
|
|
fn download_node_lib(dist_url: &str, version: &str, arch: &str) -> Vec<u8> {
|
|
|
|
// Assume windows since we know we are building on windows.
|
|
|
|
let url = format!("{dist_url}/v{version}/win-{arch}/node.lib", dist_url = dist_url, version = version, arch = arch);
|
2020-10-31 09:57:28 +09:00
|
|
|
|
|
|
|
let response = ureq::get(&url).call();
|
|
|
|
if let Some(error) = response.synthetic_error() {
|
|
|
|
panic!("Failed to download node.lib: {:#?}", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut reader = response.into_reader();
|
|
|
|
let mut bytes = vec![];
|
|
|
|
reader.read_to_end(&mut bytes).unwrap();
|
|
|
|
|
|
|
|
bytes
|
2020-10-09 22:37:14 +09:00
|
|
|
}
|
|
|
|
|
2020-02-18 22:09:17 +09:00
|
|
|
pub fn setup() {
|
2020-10-31 10:17:06 +09:00
|
|
|
// Assume nodejs if not specified.
|
|
|
|
let dist_url = std::env::var("NPM_CONFIG_DISTURL")
|
|
|
|
.unwrap_or("https://nodejs.org/dist".into());
|
|
|
|
|
|
|
|
// Try to get local nodejs version if not specified.
|
|
|
|
let node_version = std::env::var("NPM_CONFIG_TARGET")
|
|
|
|
.or_else(|_| get_node_version())
|
|
|
|
.expect("Failed to determine nodejs version");
|
|
|
|
|
2020-10-31 12:00:16 +09:00
|
|
|
// NPM also gives us an arch var, but let's trust cargo more.
|
|
|
|
// We translate from cargo's arch env format into npm/gyps's.
|
|
|
|
// See https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch for rust env values.
|
|
|
|
// Nodejs appears to follow `process.arch`.
|
|
|
|
// See https://nodejs.org/docs/latest/api/process.html#process_process_arch for npm env values.
|
|
|
|
let arch = std::env::var("CARGO_CFG_TARGET_ARCH")
|
|
|
|
.map(|arch| match arch.as_str() {
|
|
|
|
"x86" => "x86", // TODO: x86 appears to also be called ia32 in npm_config_arch sometimes. What is the right value?
|
|
|
|
"x86_64" => "x64",
|
|
|
|
"mips" => "mips",
|
|
|
|
"powerpc" => "ppc",
|
|
|
|
"powerpc64" => "ppc64",
|
|
|
|
"arm" => "arm",
|
|
|
|
"aarch64" => "arm64",
|
|
|
|
arch => panic!("Unknown Architecture: {}", arch),
|
|
|
|
})
|
|
|
|
.expect("Failed to determine target arch");
|
|
|
|
|
2020-10-31 10:17:06 +09:00
|
|
|
println!("cargo:rerun-if-env-changed=NPM_CONFIG_DISTURL");
|
|
|
|
println!("cargo:rerun-if-env-changed=NPM_CONFIG_TARGET");
|
2020-10-31 09:57:28 +09:00
|
|
|
|
2020-10-09 22:37:14 +09:00
|
|
|
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());
|
2020-09-20 01:28:02 +09:00
|
|
|
|
2020-10-09 22:37:14 +09:00
|
|
|
node_lib_file_dir.push(".napi-rs");
|
2020-09-20 01:28:02 +09:00
|
|
|
|
2020-10-09 22:37:14 +09:00
|
|
|
match create_dir(&node_lib_file_dir) {
|
2020-09-20 01:28:02 +09:00
|
|
|
Ok(_) => {},
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() != std::io::ErrorKind::AlreadyExists {
|
2020-10-09 22:37:14 +09:00
|
|
|
panic!("create {} folder failed: {}", node_lib_file_dir.to_str().unwrap(), err)
|
2020-09-20 01:28:02 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-09 22:37:14 +09:00
|
|
|
let link_search_dir = node_lib_file_dir.clone();
|
|
|
|
|
2020-10-31 09:57:28 +09:00
|
|
|
node_lib_file_dir.push(format!("node-{}.lib", node_version));
|
2020-10-09 22:37:14 +09:00
|
|
|
|
|
|
|
if let Err(_) = metadata(&node_lib_file_dir) {
|
2020-10-31 12:00:16 +09:00
|
|
|
let node_lib = download_node_lib(&dist_url, &node_version, &arch);
|
2020-10-09 22:37:14 +09:00
|
|
|
write(&node_lib_file_dir, &node_lib).expect(&format!("Could not save file to {}", node_lib_file_dir.to_str().unwrap()));
|
2020-02-18 22:09:17 +09:00
|
|
|
}
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-lib={}",
|
|
|
|
&node_lib_file_dir.file_stem().unwrap().to_str().unwrap()
|
|
|
|
);
|
2020-10-09 22:37:14 +09:00
|
|
|
println!("cargo:rustc-link-search=native={}", link_search_dir.to_str().unwrap());
|
|
|
|
println!("cargo:rustc-cdylib-link-arg=delayimp.lib");
|
|
|
|
println!("cargo:rustc-cdylib-link-arg=/DELAYLOAD:node.exe");
|
2020-07-01 21:44:29 +09:00
|
|
|
setup_napi_feature();
|
2020-02-18 22:09:17 +09:00
|
|
|
}
|
|
|
|
} else if #[cfg(target_os = "macos")] {
|
|
|
|
/// Set up the build environment by setting Cargo configuration variables.
|
|
|
|
pub fn setup() {
|
2020-08-26 18:58:42 +09:00
|
|
|
println!("cargo:rustc-cdylib-link-arg=-Wl");
|
2020-02-18 22:09:17 +09:00
|
|
|
println!("cargo:rustc-cdylib-link-arg=-undefined");
|
|
|
|
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
|
2020-07-01 21:44:29 +09:00
|
|
|
setup_napi_feature();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pub fn setup() {
|
|
|
|
setup_napi_feature();
|
2020-02-18 22:09:17 +09:00
|
|
|
}
|
2020-07-01 21:44:29 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 10:29:08 +09:00
|
|
|
pub fn setup_napi_feature() {
|
2020-07-01 21:44:29 +09:00
|
|
|
let napi_version = String::from_utf8(
|
|
|
|
Command::new("node")
|
|
|
|
.args(&["-e", "console.log(process.versions.napi)"])
|
|
|
|
.output()
|
|
|
|
.unwrap()
|
|
|
|
.stdout,
|
|
|
|
)
|
|
|
|
.expect("Get NAPI version failed");
|
|
|
|
|
|
|
|
let napi_version_number = napi_version.trim().parse::<u32>().unwrap();
|
|
|
|
|
2020-07-03 01:31:50 +09:00
|
|
|
if napi_version_number < 2 {
|
2020-07-01 21:44:29 +09:00
|
|
|
panic!("current napi version is too low");
|
|
|
|
}
|
|
|
|
|
2020-07-03 01:31:50 +09:00
|
|
|
if napi_version_number == 2 {
|
2020-07-01 21:44:29 +09:00
|
|
|
println!("cargo:rustc-cfg=napi{}", napi_version_number);
|
2020-02-18 22:09:17 +09:00
|
|
|
} else {
|
2020-07-03 01:31:50 +09:00
|
|
|
for version in 2..(napi_version_number + 1) {
|
2020-07-01 21:44:29 +09:00
|
|
|
println!("cargo:rustc-cfg=napi{}", version);
|
|
|
|
}
|
2020-02-18 22:09:17 +09:00
|
|
|
}
|
|
|
|
}
|