-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
config.rs
243 lines (232 loc) · 9.17 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use super::InterpreterKind;
use crate::target::{Arch, Os};
use crate::Target;
use anyhow::{format_err, Context, Result};
use fs_err as fs;
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::Path;
/// Wellknown Python interpreter sysconfig values
static WELLKNOWN_SYSCONFIG: Lazy<HashMap<Os, HashMap<Arch, Vec<InterpreterConfig>>>> =
Lazy::new(|| {
let mut sysconfig = HashMap::new();
// Linux
let sysconfig_linux = serde_json::from_slice(include_bytes!("sysconfig-linux.json"))
.expect("invalid sysconfig-linux.json");
sysconfig.insert(Os::Linux, sysconfig_linux);
// macOS
let sysconfig_macos = serde_json::from_slice(include_bytes!("sysconfig-macos.json"))
.expect("invalid sysconfig-macos.json");
sysconfig.insert(Os::Macos, sysconfig_macos);
// Windows
let sysconfig_windows = serde_json::from_slice(include_bytes!("sysconfig-windows.json"))
.expect("invalid sysconfig-windows.json");
sysconfig.insert(Os::Windows, sysconfig_windows);
// FreeBSD
let sysconfig_freebsd = serde_json::from_slice(include_bytes!("sysconfig-freebsd.json"))
.expect("invalid sysconfig-freebsd.json");
sysconfig.insert(Os::FreeBsd, sysconfig_freebsd);
// OpenBSD
let sysconfig_openbsd = serde_json::from_slice(include_bytes!("sysconfig-openbsd.json"))
.expect("invalid sysconfig-openbsd.json");
sysconfig.insert(Os::OpenBsd, sysconfig_openbsd);
// NetBSD
let sysconfig_netbsd = serde_json::from_slice(include_bytes!("sysconfig-netbsd.json"))
.expect("invalid sysconfig-netbsd.json");
sysconfig.insert(Os::NetBsd, sysconfig_netbsd);
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 pointer_width: 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
}
/// Construct a new InterpreterConfig from a pyo3 config file
pub fn from_pyo3_config(config_file: &Path, target: &Target) -> Result<Self> {
let config_file = fs::File::open(config_file)?;
let reader = BufReader::new(config_file);
let lines = reader.lines();
macro_rules! parse_value {
($variable:ident, $value:ident) => {
$variable = Some($value.trim().parse().context(format!(
concat!(
"failed to parse ",
stringify!($variable),
" from config value '{}'"
),
$value
))?)
};
}
let mut implementation = None;
let mut version = None;
let mut abiflags = None;
let mut ext_suffix = None;
let mut abi_tag = None;
let mut pointer_width = None;
for (i, line) in lines.enumerate() {
let line = line.context("failed to read line from config")?;
let (key, value) = line
.split_once('=')
.with_context(|| format!("expected key=value pair on line {}", i + 1))?;
match key {
"implementation" => parse_value!(implementation, value),
"version" => parse_value!(version, value),
"abiflags" => parse_value!(abiflags, value),
"ext_suffix" => parse_value!(ext_suffix, value),
"abi_tag" => parse_value!(abi_tag, value),
"pointer_width" => parse_value!(pointer_width, value),
_ => continue,
}
}
let version: String = version.context("missing value for version")?;
let (ver_major, ver_minor) = version
.split_once('.')
.context("Invalid python interpreter version")?;
let major = ver_major.parse::<usize>().with_context(|| {
format!(
"Invalid python interpreter major version '{}', expect a digit",
ver_major
)
})?;
let minor = ver_minor.parse::<usize>().with_context(|| {
format!(
"Invalid python interpreter minor version '{}', expect a digit",
ver_minor
)
})?;
let implementation = implementation.unwrap_or_else(|| "cpython".to_string());
let interpreter_kind = implementation.parse().map_err(|e| format_err!("{}", e))?;
let abi_tag = match interpreter_kind {
InterpreterKind::CPython => {
if (major, minor) >= (3, 8) {
abi_tag.unwrap_or_else(|| format!("{}{}", major, minor))
} else {
abi_tag.unwrap_or_else(|| format!("{}{}m", major, minor))
}
}
InterpreterKind::PyPy => abi_tag.unwrap_or_else(|| "pp73".to_string()),
};
let file_ext = if target.is_windows() { "pyd" } else { "so" };
let ext_suffix = if target.is_linux() || target.is_macos() {
// See https://github.com/pypa/auditwheel/issues/349
let target_env = if (major, minor) >= (3, 11) {
target.target_env().to_string()
} else {
"gnu".to_string()
};
match interpreter_kind {
InterpreterKind::CPython => ext_suffix.unwrap_or_else(|| {
// Eg: .cpython-38-x86_64-linux-gnu.so
format!(
".cpython-{}-{}-{}-{}.{}",
abi_tag,
target.get_python_arch(),
target.get_python_os(),
target_env,
file_ext,
)
}),
InterpreterKind::PyPy => ext_suffix.unwrap_or_else(|| {
// Eg: .pypy38-pp73-x86_64-linux-gnu.so
format!(
".pypy{}{}-{}-{}-{}-{}.{}",
major,
minor,
abi_tag,
target.get_python_arch(),
target.get_python_os(),
target_env,
file_ext,
)
}),
}
} else {
ext_suffix.context("missing value for ext_suffix")?
};
Ok(Self {
major,
minor,
interpreter_kind,
abiflags: abiflags.unwrap_or_default(),
ext_suffix,
abi_tag: Some(abi_tag),
pointer_width,
})
}
/// 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.pointer_width {
content.push_str(&format!("\npointer_width={}", pointer_width));
}
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");
}
}