From 24a3fbd4e2b0068dd54a60e040bdbf5687061a25 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Sat, 29 Feb 2020 12:22:37 +0000 Subject: [PATCH] Check python and target arcitecture match in build.rs --- build.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/build.rs b/build.rs index 2195b7f1ef4..df7cbd767de 100644 --- a/build.rs +++ b/build.rs @@ -49,6 +49,7 @@ struct InterpreterConfig { /// Prefix used for determining the directory of libpython base_prefix: String, executable: String, + machine: String, } #[derive(Deserialize, Debug, Clone, PartialEq)] @@ -202,6 +203,7 @@ fn load_cross_compile_info() -> Result<(InterpreterConfig, HashMap Result { } } + check_target_architecture(&interpreter_config.machine)?; + let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some(); if !is_extension_module || cfg!(target_os = "windows") { println!("{}", get_rustc_link_lib(&interpreter_config).unwrap()); @@ -535,6 +540,33 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result { Ok(flags) } +fn check_target_architecture(python_machine: &str) -> Result<(), String> { + // Try to check whether the target architecture matches the python library + let target_arch = match env::var("CARGO_CFG_TARGET_ARCH") + .as_ref() + .map(|e| e.as_str()) + { + Ok("x86_64") => Some("64-bit"), + Ok("x86") => Some("32-bit"), + _ => None, // It might be possible to recognise other architectures, this will do for now. + }; + + let python_arch = match python_machine { + "AMD64" | "x86_64" => Some("64-bit"), + "i686" | "x86" => Some("32-bit"), + _ => None, // It might be possible to recognise other architectures, this will do for now. + }; + + match (target_arch, python_arch) { + // If we could recognise both, and they're different, fail. + (Some(t), Some(p)) if p != t => Err(format!( + "Your Rust target architecture ({}) does not match your python interpreter ({})", + t, p + )), + _ => Ok(()), + } +} + fn check_rustc_version() { let channel = Channel::read().expect("Failed to determine rustc channel"); if !channel.supports_features() {