From 6d6d030bf7a13fcf70ba2346b578efd40c4b8b59 Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Mon, 29 Mar 2021 08:47:11 +0300 Subject: [PATCH 1/3] Remove unused cross-compile environment variables PYO3_CROSS_VERSION was renamed to PYO3_CROSS_PYTHON_VERSION. PYO3_CROSS_INCLUDE_DIR was removed in https://github.com/PyO3/pyo3/pull/1521 --- build.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/build.rs b/build.rs index 08c30996..1e2e3e82 100644 --- a/build.rs +++ b/build.rs @@ -144,8 +144,6 @@ impl CrossCompileConfig { fn cross_compiling() -> Result> { if env::var_os("PYO3_CROSS").is_none() && env::var_os("PYO3_CROSS_LIB_DIR").is_none() - && env::var_os("PYO3_CROSS_INCLUDE_DIR").is_none() - && env::var_os("PYO3_CROSS_VERSION").is_none() && env::var_os("PYO3_CROSS_PYTHON_VERSION").is_none() { let target = env::var("TARGET")?; @@ -489,7 +487,7 @@ fn windows_hardcoded_cross_compile( ) { (Some(major), Some(minor), None) => (major, minor), _ => bail!( - "Expected major.minor version (e.g. 3.9) for PYO3_CROSS_VERSION, got `{}`", + "Expected major.minor version (e.g. 3.9) for PYO3_CROSS_PYTHON_VERSION, got `{}`", version ), } From f91f795fc7a95b550ed3bdcf6aae97b2f1eab9d1 Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Mon, 29 Mar 2021 09:00:57 +0300 Subject: [PATCH 2/3] Clean up CARGO_CFG_TARGET_FAMILY variable parsing Match the target OS family strings exactly and provide better error messages when CARGO_CFG_TARGET_FAMILY is not set (e.g. on wasm32). --- build.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/build.rs b/build.rs index 1e2e3e82..40a65e75 100644 --- a/build.rs +++ b/build.rs @@ -516,13 +516,15 @@ fn windows_hardcoded_cross_compile( fn load_cross_compile_info( cross_compile_config: CrossCompileConfig, ) -> Result<(InterpreterConfig, BuildFlags)> { - let target_family = env::var("CARGO_CFG_TARGET_FAMILY")?; - // Because compiling for windows on linux still includes the unix target family - if target_family == "unix" { + match env::var("CARGO_CFG_TARGET_FAMILY") { // Configure for unix platforms using the sysconfigdata file - load_cross_compile_from_sysconfigdata(cross_compile_config) - } else { - windows_hardcoded_cross_compile(cross_compile_config) + Ok(os) if os == "unix" => load_cross_compile_from_sysconfigdata(cross_compile_config), + // Use hardcoded interpreter config when targeting Windows + Ok(os) if os == "windows" => windows_hardcoded_cross_compile(cross_compile_config), + // ??? + Ok(os) => Err(format!("Unsupported target OS family: {}", os).into()), + // wasm32-wasi / bare metal ? + Err(_) => Err("CARGO_CFG_TARGET_FAMILY is not set when cross-compiling".into()), } } From ccaabe3ba5276db6db0e73eb3cca3b81ee554cbe Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Mon, 29 Mar 2021 11:27:35 +0300 Subject: [PATCH 3/3] Apply suggestions from code review: env::var() -> env::var_os() and fall back to load_cross_compile_from_sysconfigdata() when unset. Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com> --- build.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index 40a65e75..df07592b 100644 --- a/build.rs +++ b/build.rs @@ -516,15 +516,18 @@ fn windows_hardcoded_cross_compile( fn load_cross_compile_info( cross_compile_config: CrossCompileConfig, ) -> Result<(InterpreterConfig, BuildFlags)> { - match env::var("CARGO_CFG_TARGET_FAMILY") { + match env::var_os("CARGO_CFG_TARGET_FAMILY") { // Configure for unix platforms using the sysconfigdata file - Ok(os) if os == "unix" => load_cross_compile_from_sysconfigdata(cross_compile_config), + Some(os) if os == "unix" => load_cross_compile_from_sysconfigdata(cross_compile_config), // Use hardcoded interpreter config when targeting Windows - Ok(os) if os == "windows" => windows_hardcoded_cross_compile(cross_compile_config), - // ??? - Ok(os) => Err(format!("Unsupported target OS family: {}", os).into()), - // wasm32-wasi / bare metal ? - Err(_) => Err("CARGO_CFG_TARGET_FAMILY is not set when cross-compiling".into()), + Some(os) if os == "windows" => windows_hardcoded_cross_compile(cross_compile_config), + // Waiting for users to tell us what they expect on their target platform + Some(os) => bail!( + "Unsupported target OS family for cross-compilation: {:?}", + os + ), + // Unknown os family - try to do something useful + None => load_cross_compile_from_sysconfigdata(cross_compile_config), } }