2021-04-14 15:05:07 +09:00
|
|
|
#![allow(clippy::expect_fun_call)]
|
2020-11-25 18:42:14 +09:00
|
|
|
use std::env;
|
2020-10-31 12:58:28 +09:00
|
|
|
use std::fs::{metadata, write};
|
2020-10-31 12:11:10 +09:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-06-01 00:21:56 +09:00
|
|
|
fn copy_node_lib(arch: &str) -> Vec<u8> {
|
|
|
|
match arch {
|
|
|
|
"x64" => include_bytes!("libs/node-x64.lib").to_vec(),
|
|
|
|
"x86" => include_bytes!("libs/node-x86.lib").to_vec(),
|
|
|
|
"arm64" => include_bytes!("libs/node-arm64.lib").to_vec(),
|
|
|
|
_ => unreachable!(),
|
2020-10-31 12:11:10 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn setup() {
|
2020-11-25 18:42:14 +09:00
|
|
|
let out_dir = env::var("OUT_DIR").expect("OUT_DIR is not set");
|
2020-10-31 12:58:28 +09:00
|
|
|
|
2020-10-31 12:11:10 +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.
|
2020-11-10 12:09:25 +09:00
|
|
|
// For windows, we only support `['ia32', 'x64', 'arm64']`
|
|
|
|
// https://github.com/nodejs/node-gyp/blob/master/lib/install.js#L301
|
2020-11-25 18:42:14 +09:00
|
|
|
let arch = env::var("CARGO_CFG_TARGET_ARCH")
|
2020-10-31 12:11:10 +09:00
|
|
|
.map(|arch| match arch.as_str() {
|
2020-11-10 12:09:25 +09:00
|
|
|
"x86" => "x86",
|
2020-10-31 12:11:10 +09:00
|
|
|
"x86_64" => "x64",
|
2020-11-10 12:09:25 +09:00
|
|
|
// https://github.com/nodejs/node/issues/25998
|
|
|
|
// actually not supported for now
|
2021-06-01 00:21:56 +09:00
|
|
|
// but we can get it from https://unofficial-builds.nodejs.org/download/release
|
|
|
|
// just set the `NPM_CONFIG_DISTURL` to `https://unofficial-builds.nodejs.org/download/release`
|
2020-10-31 12:11:10 +09:00
|
|
|
"aarch64" => "arm64",
|
2020-11-10 12:09:25 +09:00
|
|
|
arch => panic!("Unsupported CPU Architecture: {}", arch),
|
2020-10-31 12:11:10 +09:00
|
|
|
})
|
|
|
|
.expect("Failed to determine target arch");
|
|
|
|
|
2020-10-31 12:58:28 +09:00
|
|
|
let mut node_lib_file_path = PathBuf::from(out_dir);
|
|
|
|
let link_search_dir = node_lib_file_path.clone();
|
|
|
|
|
2021-06-01 00:21:56 +09:00
|
|
|
// Encode arch to detect and require node.lib.
|
|
|
|
let node_lib_file_name = format!("node-{arch}.lib", arch = arch,);
|
2020-10-31 12:58:28 +09:00
|
|
|
node_lib_file_path.push(&node_lib_file_name);
|
2020-10-31 12:11:10 +09:00
|
|
|
|
2020-10-31 12:58:28 +09:00
|
|
|
// If file does not exist, download it.
|
|
|
|
if metadata(&node_lib_file_path).is_err() {
|
2021-07-30 13:54:59 +09:00
|
|
|
let node_lib = copy_node_lib(arch);
|
2020-10-31 12:58:28 +09:00
|
|
|
|
|
|
|
write(&node_lib_file_path, &node_lib).expect(&format!(
|
2020-10-31 12:11:10 +09:00
|
|
|
"Could not save file to {}",
|
2020-10-31 12:58:28 +09:00
|
|
|
node_lib_file_path.to_str().unwrap()
|
2020-10-31 12:11:10 +09:00
|
|
|
));
|
|
|
|
}
|
2020-10-31 12:58:28 +09:00
|
|
|
|
2020-10-31 12:11:10 +09:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-lib={}",
|
2020-10-31 12:58:28 +09:00
|
|
|
node_lib_file_path.file_stem().unwrap().to_str().unwrap()
|
2020-10-31 12:11:10 +09:00
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
2020-10-31 12:58:28 +09:00
|
|
|
link_search_dir.display()
|
2020-10-31 12:11:10 +09:00
|
|
|
);
|
2020-10-15 21:16:52 +09:00
|
|
|
// Link `win_delay_load_hook.obj`
|
|
|
|
// Needed for electron, but okay for other environments
|
|
|
|
// https://github.com/neon-bindings/neon/pull/627
|
2020-10-31 12:11:10 +09:00
|
|
|
println!("cargo:rustc-cdylib-link-arg=delayimp.lib");
|
|
|
|
println!("cargo:rustc-cdylib-link-arg=/DELAYLOAD:node.exe");
|
|
|
|
}
|