From 7739cf5dc3e81cd8cc894d5fba63d3bc9ac721b7 Mon Sep 17 00:00:00 2001 From: adumbidiot Date: Fri, 30 Oct 2020 20:00:16 -0700 Subject: [PATCH] Respect CARGO_CFG_TARGET_ARCH env var --- build/src/lib.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/build/src/lib.rs b/build/src/lib.rs index 29534b4c..f24578f3 100644 --- a/build/src/lib.rs +++ b/build/src/lib.rs @@ -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 { - 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 { + // 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();