Skip to content

Commit

Permalink
Merge pull request #779 from davidhewitt/check-architecture
Browse files Browse the repository at this point in the history
Check python and target arcitecture match in build.rs
  • Loading branch information
kngwyu authored Mar 3, 2020
2 parents ebb528d + 24a3fbd commit 09e36f2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -202,6 +203,7 @@ fn load_cross_compile_info() -> Result<(InterpreterConfig, HashMap<String, Strin
ld_version: "".to_string(),
base_prefix: "".to_string(),
executable: "".to_string(),
machine: "".to_string(),
};

Ok((intepreter_config, fix_config_map(config_map)))
Expand Down Expand Up @@ -479,6 +481,7 @@ print(json.dumps({
"base_prefix": base_prefix,
"shared": PYPY or bool(sysconfig.get_config_var('Py_ENABLE_SHARED')),
"executable": sys.executable,
"machine": platform.machine()
}))
"#;
let json = run_python_script(interpreter, script)?;
Expand All @@ -495,6 +498,8 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String, String> {
}
}

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());
Expand Down Expand Up @@ -535,6 +540,33 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String, String> {
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() {
Expand Down

0 comments on commit 09e36f2

Please sign in to comment.