-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #896 from messense/well-known-sysconfig
Add hardcoded well-known sysconfigs for effortless cross compiling
- Loading branch information
Showing
11 changed files
with
807 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use super::InterpreterKind; | ||
use crate::target::{Arch, Os}; | ||
use once_cell::sync::Lazy; | ||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
|
||
/// Wellknown Python interpreter sysconfig values | ||
static WELLKNOWN_SYSCONFIG: Lazy<HashMap<Os, HashMap<Arch, Vec<InterpreterConfig>>>> = | ||
Lazy::new(|| { | ||
let mut sysconfig = HashMap::new(); | ||
let sysconfig_linux = serde_json::from_slice(include_bytes!("sysconfig-linux.json")) | ||
.expect("invalid sysconfig-linux.json"); | ||
sysconfig.insert(Os::Linux, sysconfig_linux); | ||
let sysconfig_macos = serde_json::from_slice(include_bytes!("sysconfig-macos.json")) | ||
.expect("invalid sysconfig-macos.json"); | ||
sysconfig.insert(Os::Macos, sysconfig_macos); | ||
sysconfig | ||
}); | ||
|
||
/// Some of the sysconfigdata of Python interpreter we care about | ||
#[derive(Debug, Clone, Deserialize, Eq, PartialEq)] | ||
pub struct InterpreterConfig { | ||
/// Python's major version | ||
pub major: usize, | ||
/// Python's minor version | ||
pub minor: usize, | ||
/// cpython or pypy | ||
#[serde(rename = "interpreter")] | ||
pub interpreter_kind: InterpreterKind, | ||
/// For linux and mac, this contains the value of the abiflags, e.g. "m" | ||
/// for python3.7m or "dm" for python3.6dm. Since python3.8, the value is | ||
/// empty. On windows, the value was always None. | ||
/// | ||
/// See PEP 261 and PEP 393 for details | ||
pub abiflags: String, | ||
/// Suffix to use for extension modules as given by sysconfig. | ||
pub ext_suffix: String, | ||
/// Part of sysconfig's SOABI specifying {major}{minor}{abiflags} | ||
/// | ||
/// Note that this always `None` on windows | ||
pub abi_tag: Option<String>, | ||
/// Pointer width | ||
pub calcsize_pointer: Option<usize>, | ||
} | ||
|
||
impl InterpreterConfig { | ||
/// Lookup a wellknown sysconfig for a given Python interpreter | ||
pub fn lookup( | ||
os: Os, | ||
arch: Arch, | ||
python_impl: InterpreterKind, | ||
python_version: (usize, usize), | ||
) -> Option<&'static Self> { | ||
let (major, minor) = python_version; | ||
if let Some(os_sysconfigs) = WELLKNOWN_SYSCONFIG.get(&os) { | ||
if let Some(sysconfigs) = os_sysconfigs.get(&arch) { | ||
return sysconfigs.iter().find(|s| { | ||
s.interpreter_kind == python_impl && s.major == major && s.minor == minor | ||
}); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Generate pyo3 config file content | ||
pub fn pyo3_config_file(&self) -> String { | ||
let mut content = format!( | ||
r#"implementation={implementation} | ||
version={major}.{minor} | ||
shared=true | ||
abi3=false | ||
build_flags=WITH_THREAD | ||
suppress_build_script_link_lines=false"#, | ||
implementation = self.interpreter_kind, | ||
major = self.major, | ||
minor = self.minor, | ||
); | ||
if let Some(pointer_width) = self.calcsize_pointer { | ||
content.push_str(&format!("\npointer_width={}", pointer_width * 8)); | ||
} | ||
content | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_load_sysconfig() { | ||
let linux_sysconfig = WELLKNOWN_SYSCONFIG.get(&Os::Linux).unwrap(); | ||
assert!(linux_sysconfig.contains_key(&Arch::X86_64)); | ||
} | ||
|
||
#[test] | ||
fn test_pyo3_config_file() { | ||
let sysconfig = | ||
InterpreterConfig::lookup(Os::Linux, Arch::X86_64, InterpreterKind::CPython, (3, 10)) | ||
.unwrap(); | ||
let config_file = sysconfig.pyo3_config_file(); | ||
assert_eq!(config_file, "implementation=CPython\nversion=3.10\nshared=true\nabi3=false\nbuild_flags=WITH_THREAD\nsuppress_build_script_link_lines=false\npointer_width=64"); | ||
} | ||
} |
Oops, something went wrong.