Respect CARGO_CFG_TARGET_ARCH env var

This commit is contained in:
adumbidiot 2020-10-30 20:00:16 -07:00
parent 5364458366
commit 7739cf5dc3

View file

@ -18,8 +18,9 @@ cfg_if! {
Ok(stdout_str.trim().trim_start_matches('v').to_string())
}
fn download_node_lib(dist_url: &str, version: &str) -> Vec<u8> {
let url = format!("{dist_url}/v{version}/win-x64/node.lib", dist_url = dist_url, version = version);
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);
let response = ureq::get(&url).call();
if let Some(error) = response.synthetic_error() {
@ -43,6 +44,24 @@ cfg_if! {
.or_else(|_| get_node_version())
.expect("Failed to determine nodejs version");
// 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");
println!("cargo:rerun-if-env-changed=NPM_CONFIG_DISTURL");
println!("cargo:rerun-if-env-changed=NPM_CONFIG_TARGET");
@ -65,7 +84,7 @@ cfg_if! {
node_lib_file_dir.push(format!("node-{}.lib", node_version));
if let Err(_) = metadata(&node_lib_file_dir) {
let node_lib = download_node_lib(&dist_url, &node_version);
let node_lib = download_node_lib(&dist_url, &node_version, &arch);
write(&node_lib_file_dir, &node_lib).expect(&format!("Could not save file to {}", node_lib_file_dir.to_str().unwrap()));
}
println!(
@ -73,7 +92,6 @@ cfg_if! {
&node_lib_file_dir.file_stem().unwrap().to_str().unwrap()
);
println!("cargo:rustc-link-search=native={}", link_search_dir.to_str().unwrap());
// Link `win_delay_load_hook.obj` for windows electron
println!("cargo:rustc-cdylib-link-arg=delayimp.lib");
println!("cargo:rustc-cdylib-link-arg=/DELAYLOAD:node.exe");
setup_napi_feature();