Debian Codegraph @

src/python_interpreter/config.rs

raw
1 use super::{
2 InterpreterKind, MAXIMUM_PYPY_MINOR, MAXIMUM_PYTHON_MINOR, MINIMUM_PYPY_MINOR,
3 MINIMUM_PYTHON_MINOR,
4 };
5 use crate::target::{Arch, Os};
6 use crate::Target;
7 use anyhow::{format_err, Context, Result};
8 use fs_err as fs;
9 use serde::Deserialize;
10 use std::fmt::Write as _;
11 use std::io::{BufRead, BufReader};
12 use std::path::Path;
13
14 const PYPY_ABI_TAG: &str = "pp73";
15
16 fn graalpy_version_for_python_version(major: usize, minor: usize) -> Option<(usize, usize)> {
17 match (major, minor) {
18 (3, 10) => Some((24, 0)),
19 (3, 11) => Some((24, 2)),
20 // Since 25.0, GraalPy should only change the major release number for feature releases.
21 // Additionally, it promises that only the autumn (oddly-numbered) releases are
22 // allowed to break ABI compatibility, so only those can change the Python version.
23 // The even-numbered releases will report the ABI version of the previous release.
24 // So assuming that GraalPy doesn't fall terribly behind on updating Python version,
25 // the version used in the ABI should follow this formula
26 (3, 12..) => Some((25 + (minor - 12) * 2, 0)),
27 (_, _) => None,
28 }
29 }
30
31 /// Some of the sysconfigdata of Python interpreter we care about
32 #[derive(Debug, Clone, Deserialize, Eq, PartialEq)]
33 pub struct InterpreterConfig {
34 /// Python's major version
35 pub major: usize,
36 /// Python's minor version
37 pub minor: usize,
38 /// cpython, pypy, or graalpy
39 #[serde(rename = "interpreter")]
40 pub interpreter_kind: InterpreterKind,
41 /// For linux and mac, this contains the value of the abiflags, e.g. "m"
42 /// for python3.7m or "dm" for python3.6dm.
43 ///
44 /// * Since python3.8, the value is empty
45 /// * Since python3.13, the value is "t" for free-threaded builds.
46 /// * On Windows, the value was always None.
47 ///
48 /// See PEP 261 and PEP 393 for details
49 pub abiflags: String,
50 /// Suffix to use for extension modules as given by sysconfig.
51 pub ext_suffix: String,
52 /// Pointer width
53 pub pointer_width: Option<usize>,
54 /// Is GIL disabled, i.e. free-threaded build
55 pub gil_disabled: bool,
56 }
57
58 impl InterpreterConfig {
59 /// Lookup a wellknown sysconfig for a given Python interpreter
60 pub fn lookup_one(
61 target: &Target,
62 python_impl: InterpreterKind,
63 python_version: (usize, usize),
64 abiflags: &str,
65 ) -> Option<Self> {
66 use InterpreterKind::*;
67
68 let (major, minor) = python_version;
69 if major < 3 {
70 // Python 2 is not supported
71 return None;
72 }
73 let python_ext_arch = target.get_python_ext_arch(python_impl);
74 let target_env = target.get_python_target_env(python_impl, python_version);
75 let gil_disabled = abiflags == "t";
76 match (target.target_os(), python_impl) {
77 (Os::Linux, CPython) => {
78 let abiflags = if python_version < (3, 8) {
79 "m".to_string()
80 } else {
81 abiflags.to_string()
82 };
83 let ldversion = format!("{major}{minor}{abiflags}");
84 let ext_suffix =
85 format!(".cpython-{ldversion}-{python_ext_arch}-linux-{target_env}.so");
86 Some(Self {
87 major,
88 minor,
89 interpreter_kind: CPython,
90 abiflags,
91 ext_suffix,
92 pointer_width: Some(target.pointer_width()),
93 gil_disabled,
94 })
95 }
96 (Os::Linux, PyPy) => {
97 let abi_tag = format!("pypy{major}{minor}-{PYPY_ABI_TAG}");
98 let ext_suffix = format!(".{abi_tag}-{python_ext_arch}-linux-{target_env}.so");
99 Some(Self {
100 major,
101 minor,
102 interpreter_kind: PyPy,
103 abiflags: String::new(),
104 ext_suffix,
105 pointer_width: Some(target.pointer_width()),
106 gil_disabled,
107 })
108 }
109 (Os::Linux, GraalPy) => {
110 let (graalpy_major, graalpy_minor) =
111 graalpy_version_for_python_version(major, minor)?;
112 let ext_suffix = format!(".graalpy{graalpy_major}{graalpy_minor}-{major}{minor}-native-{python_ext_arch}-linux.so");
113 Some(Self {
114 major,
115 minor,
116 interpreter_kind: GraalPy,
117 abiflags: String::new(),
118 ext_suffix,
119 pointer_width: Some(target.pointer_width()),
120 gil_disabled,
121 })
122 }
123 (Os::Macos, CPython) => {
124 let abiflags = if python_version < (3, 8) {
125 "m".to_string()
126 } else {
127 abiflags.to_string()
128 };
129 let ldversion = format!("{major}{minor}{abiflags}");
130 let ext_suffix = format!(".cpython-{ldversion}-darwin.so");
131 Some(Self {
132 major,
133 minor,
134 interpreter_kind: CPython,
135 abiflags,
136 ext_suffix,
137 pointer_width: Some(target.pointer_width()),
138 gil_disabled,
139 })
140 }
141 (Os::Macos, PyPy) => {
142 let ext_suffix = format!(".pypy{major}{minor}-{PYPY_ABI_TAG}-darwin.so");
143 Some(Self {
144 major,
145 minor,
146 interpreter_kind: PyPy,
147 abiflags: String::new(),
148 ext_suffix,
149 pointer_width: Some(target.pointer_width()),
150 gil_disabled,
151 })
152 }
153 (Os::Macos, GraalPy) => {
154 let (graalpy_major, graalpy_minor) =
155 graalpy_version_for_python_version(major, minor)?;
156 let ext_suffix = format!(".graalpy{graalpy_major}{graalpy_minor}-{major}{minor}-native-{python_ext_arch}-darwin.so");
157 Some(Self {
158 major,
159 minor,
160 interpreter_kind: GraalPy,
161 abiflags: String::new(),
162 ext_suffix,
163 pointer_width: Some(target.pointer_width()),
164 gil_disabled,
165 })
166 }
167 (Os::Windows, CPython) => {
168 let abiflags = if python_version < (3, 8) {
169 "m".to_string()
170 } else {
171 abiflags.to_string()
172 };
173 let ext_suffix = if python_version < (3, 8) {
174 ".pyd".to_string()
175 } else {
176 let platform = match target.target_arch() {
177 Arch::Aarch64 => "win_arm64",
178 Arch::X86 => "win32",
179 Arch::X86_64 => "win_amd64",
180 _ => return None,
181 };
182 format!(".cp{major}{minor}{abiflags}-{platform}.pyd")
183 };
184 Some(Self {
185 major,
186 minor,
187 interpreter_kind: CPython,
188 abiflags,
189 ext_suffix,
190 pointer_width: Some(target.pointer_width()),
191 gil_disabled,
192 })
193 }
194 (Os::Windows, PyPy) => {
195 if target.target_arch() != Arch::X86_64 {
196 // PyPy on Windows only supports x86_64
197 return None;
198 }
199 let ext_suffix = format!(".pypy{major}{minor}-{PYPY_ABI_TAG}-win_amd64.pyd");
200 Some(Self {
201 major,
202 minor,
203 interpreter_kind: PyPy,
204 abiflags: String::new(),
205 ext_suffix,
206 pointer_width: Some(target.pointer_width()),
207 gil_disabled,
208 })
209 }
210 (Os::FreeBsd, CPython) => {
211 let (abiflags, ext_suffix) = if python_version < (3, 8) {
212 ("m".to_string(), ".so".to_string())
213 } else {
214 (
215 abiflags.to_string(),
216 format!(".cpython-{major}{minor}{abiflags}.so"),
217 )
218 };
219 Some(Self {
220 major,
221 minor,
222 interpreter_kind: CPython,
223 abiflags,
224 ext_suffix,
225 pointer_width: Some(target.pointer_width()),
226 gil_disabled,
227 })
228 }
229 (Os::NetBsd, CPython) => {
230 let ext_suffix = ".so".to_string();
231 Some(Self {
232 major,
233 minor,
234 interpreter_kind: CPython,
235 abiflags: String::new(),
236 ext_suffix,
237 pointer_width: Some(target.pointer_width()),
238 gil_disabled,
239 })
240 }
241 (Os::OpenBsd, CPython) => {
242 let ldversion = format!("{major}{minor}");
243 let ext_suffix = format!(".cpython-{ldversion}{abiflags}.so");
244 Some(Self {
245 major,
246 minor,
247 interpreter_kind: CPython,
248 abiflags: String::new(),
249 ext_suffix,
250 pointer_width: Some(target.pointer_width()),
251 gil_disabled,
252 })
253 }
254 (Os::Emscripten, CPython) => {
255 let ldversion = format!("{major}{minor}");
256 let ext_suffix = format!(".cpython-{ldversion}-{python_ext_arch}-emscripten.so");
257 Some(Self {
258 major,
259 minor,
260 interpreter_kind: CPython,
261 abiflags: String::new(),
262 ext_suffix,
263 pointer_width: Some(target.pointer_width()),
264 gil_disabled,
265 })
266 }
267 (_, _) => None,
268 }
269 }
270
271 /// Lookup wellknown sysconfigs for a given target
272 pub fn lookup_target(target: &Target) -> Vec<Self> {
273 let mut configs = Vec::new();
274 for (python_impl, min_minor_ver, max_minor_ver) in [
275 (
276 InterpreterKind::CPython,
277 MINIMUM_PYTHON_MINOR,
278 MAXIMUM_PYTHON_MINOR,
279 ),
280 (
281 InterpreterKind::PyPy,
282 MINIMUM_PYPY_MINOR,
283 MAXIMUM_PYPY_MINOR,
284 ),
285 ] {
286 for minor in min_minor_ver..=max_minor_ver {
287 if let Some(config) = Self::lookup_one(target, python_impl, (3, minor), "") {
288 configs.push(config);
289 }
290 }
291 for minor in 13..=max_minor_ver {
292 if let Some(config) = Self::lookup_one(target, python_impl, (3, minor), "t") {
293 configs.push(config);
294 }
295 }
296 }
297 configs
298 }
299
300 /// Construct a new InterpreterConfig from a pyo3 config file
301 pub fn from_pyo3_config(config_file: &Path, target: &Target) -> Result<Self> {
302 let config_file = fs::File::open(config_file)?;
303 let reader = BufReader::new(config_file);
304 let lines = reader.lines();
305
306 macro_rules! parse_value {
307 ($variable:ident, $value:ident) => {
308 $variable = Some($value.trim().parse().context(format!(
309 concat!(
310 "failed to parse ",
311 stringify!($variable),
312 " from config value '{}'"
313 ),
314 $value
315 ))?)
316 };
317 }
318
319 let mut implementation = None;
320 let mut version = None;
321 let mut abiflags = None;
322 let mut ext_suffix = None;
323 let mut abi_tag = None;
324 let mut pointer_width = None;
325 let mut build_flags: Option<String> = None;
326
327 for (i, line) in lines.enumerate() {
328 let line = line.context("failed to read line from config")?;
329 let (key, value) = line
330 .split_once('=')
331 .with_context(|| format!("expected key=value pair on line {}", i + 1))?;
332 match key {
333 "implementation" => parse_value!(implementation, value),
334 "version" => parse_value!(version, value),
335 "abiflags" => parse_value!(abiflags, value),
336 "ext_suffix" => parse_value!(ext_suffix, value),
337 "abi_tag" => parse_value!(abi_tag, value),
338 "pointer_width" => parse_value!(pointer_width, value),
339 "build_flags" => parse_value!(build_flags, value),
340 _ => continue,
341 }
342 }
343 let version: String = version.context("missing value for version")?;
344 let (ver_major, ver_minor) = version
345 .split_once('.')
346 .context("Invalid python interpreter version")?;
347 let major = ver_major.parse::<usize>().with_context(|| {
348 format!("Invalid python interpreter major version '{ver_major}', expect a digit")
349 })?;
350 let minor = ver_minor.parse::<usize>().with_context(|| {
351 format!("Invalid python interpreter minor version '{ver_minor}', expect a digit")
352 })?;
353 let implementation = implementation.unwrap_or_else(|| "cpython".to_string());
354 let interpreter_kind = implementation.parse().map_err(|e| format_err!("{}", e))?;
355 let abi_tag = match interpreter_kind {
356 InterpreterKind::CPython => {
357 if (major, minor) >= (3, 8) {
358 abi_tag.unwrap_or_else(|| format!("{major}{minor}"))
359 } else {
360 abi_tag.unwrap_or_else(|| format!("{major}{minor}m"))
361 }
362 }
363 InterpreterKind::PyPy => abi_tag.unwrap_or_else(|| PYPY_ABI_TAG.to_string()),
364 InterpreterKind::GraalPy => abi_tag.unwrap_or_else(|| {
365 let (graalpy_major, graalpy_minor) =
366 graalpy_version_for_python_version(major, minor).unwrap_or((23, 0));
367 format!("graalpy{graalpy_major}{graalpy_minor}_{major}{minor}_native")
368 }),
369 };
370 let file_ext = if target.is_windows() { "pyd" } else { "so" };
371 let ext_suffix = if target.is_linux() || target.is_macos() || target.is_hurd() {
372 let target_env = target.get_python_target_env(interpreter_kind, (major, minor));
373 match interpreter_kind {
374 InterpreterKind::CPython => ext_suffix.unwrap_or_else(|| {
375 // Eg: .cpython-38-x86_64-linux-gnu.so
376 format!(
377 ".cpython-{}-{}-{}-{}.{}",
378 abi_tag,
379 target.get_python_ext_arch(interpreter_kind),
380 target.get_python_os(),
381 target_env,
382 file_ext,
383 )
384 }),
385 InterpreterKind::PyPy => ext_suffix.unwrap_or_else(|| {
386 // Eg: .pypy38-pp73-x86_64-linux-gnu.so
387 format!(
388 ".pypy{}{}-{}-{}-{}-{}.{}",
389 major,
390 minor,
391 abi_tag,
392 target.get_python_ext_arch(interpreter_kind),
393 target.get_python_os(),
394 target_env,
395 file_ext,
396 )
397 }),
398 InterpreterKind::GraalPy => ext_suffix.unwrap_or_else(|| {
399 // e.g. .graalpy230-310-native-x86_64-linux.so
400 format!(
401 ".{}-{}-{}.{}",
402 abi_tag.replace('_', "-"),
403 target.get_python_ext_arch(interpreter_kind),
404 target.get_python_os(),
405 file_ext,
406 )
407 }),
408 }
409 } else if target.is_emscripten() && matches!(interpreter_kind, InterpreterKind::CPython) {
410 ext_suffix.unwrap_or_else(|| {
411 format!(
412 ".cpython-{}-{}-{}.{}",
413 abi_tag,
414 target.get_python_ext_arch(interpreter_kind),
415 target.get_python_os(),
416 file_ext
417 )
418 })
419 } else {
420 ext_suffix.context("missing value for ext_suffix")?
421 };
422 let gil_disabled = build_flags
423 .map(|flags| flags.contains("Py_GIL_DISABLED"))
424 .unwrap_or(false);
425 Ok(Self {
426 major,
427 minor,
428 interpreter_kind,
429 abiflags: abiflags.unwrap_or_default(),
430 ext_suffix,
431 pointer_width,
432 gil_disabled,
433 })
434 }
435
436 /// Generate pyo3 config file content
437 pub fn pyo3_config_file(&self) -> String {
438 let build_flags = if self.gil_disabled {
439 "Py_GIL_DISABLED"
440 } else {
441 ""
442 };
443 let mut content = format!(
444 r#"implementation={implementation}
445 version={major}.{minor}
446 shared=true
447 abi3=false
448 build_flags={build_flags}
449 suppress_build_script_link_lines=false"#,
450 implementation = self.interpreter_kind,
451 major = self.major,
452 minor = self.minor,
453 );
454 if let Some(pointer_width) = self.pointer_width {
455 write!(content, "\npointer_width={pointer_width}").unwrap();
456 }
457 content
458 }
459 }
460
461 #[cfg(test)]
462 mod test {
463 use super::*;
464 use expect_test::expect;
465 use pretty_assertions::assert_eq;
466
467 #[test]
468 fn test_well_known_sysconfigs_linux() {
469 // CPython
470 let sysconfig = InterpreterConfig::lookup_one(
471 &Target::from_resolved_target_triple("x86_64-unknown-linux-gnu").unwrap(),
472 InterpreterKind::CPython,
473 (3, 10),
474 "",
475 )
476 .unwrap();
477 assert_eq!(sysconfig.ext_suffix, ".cpython-310-x86_64-linux-gnu.so");
478
479 let sysconfig = InterpreterConfig::lookup_one(
480 &Target::from_resolved_target_triple("i686-unknown-linux-gnu").unwrap(),
481 InterpreterKind::CPython,
482 (3, 10),
483 "",
484 )
485 .unwrap();
486 assert_eq!(sysconfig.ext_suffix, ".cpython-310-i386-linux-gnu.so");
487
488 let sysconfig = InterpreterConfig::lookup_one(
489 &Target::from_resolved_target_triple("aarch64-unknown-linux-gnu").unwrap(),
490 InterpreterKind::CPython,
491 (3, 10),
492 "",
493 )
494 .unwrap();
495 assert_eq!(sysconfig.ext_suffix, ".cpython-310-aarch64-linux-gnu.so");
496
497 let sysconfig = InterpreterConfig::lookup_one(
498 &Target::from_resolved_target_triple("armv7-unknown-linux-gnueabihf").unwrap(),
499 InterpreterKind::CPython,
500 (3, 10),
501 "",
502 )
503 .unwrap();
504 assert_eq!(sysconfig.ext_suffix, ".cpython-310-arm-linux-gnueabihf.so");
505
506 let sysconfig = InterpreterConfig::lookup_one(
507 &Target::from_resolved_target_triple("arm-unknown-linux-gnueabihf").unwrap(),
508 InterpreterKind::CPython,
509 (3, 10),
510 "",
511 )
512 .unwrap();
513 assert_eq!(sysconfig.ext_suffix, ".cpython-310-arm-linux-gnueabihf.so");
514
515 let sysconfig = InterpreterConfig::lookup_one(
516 &Target::from_resolved_target_triple("powerpc64le-unknown-linux-gnu").unwrap(),
517 InterpreterKind::CPython,
518 (3, 10),
519 "",
520 )
521 .unwrap();
522 assert_eq!(
523 sysconfig.ext_suffix,
524 ".cpython-310-powerpc64le-linux-gnu.so"
525 );
526
527 let sysconfig = InterpreterConfig::lookup_one(
528 &Target::from_resolved_target_triple("powerpc-unknown-linux-gnu").unwrap(),
529 InterpreterKind::CPython,
530 (3, 10),
531 "",
532 )
533 .unwrap();
534 assert_eq!(sysconfig.ext_suffix, ".cpython-310-powerpc-linux-gnu.so");
535
536 let sysconfig = InterpreterConfig::lookup_one(
537 &Target::from_resolved_target_triple("mips64-unknown-linux-gnu").unwrap(),
538 InterpreterKind::CPython,
539 (3, 10),
540 "",
541 )
542 .unwrap();
543 assert_eq!(
544 sysconfig.ext_suffix,
545 ".cpython-310-mips64-linux-gnuabi64.so"
546 );
547
548 let sysconfig = InterpreterConfig::lookup_one(
549 &Target::from_resolved_target_triple("mips-unknown-linux-gnu").unwrap(),
550 InterpreterKind::CPython,
551 (3, 10),
552 "",
553 )
554 .unwrap();
555 assert_eq!(sysconfig.ext_suffix, ".cpython-310-mips-linux-gnu.so");
556
557 let sysconfig = InterpreterConfig::lookup_one(
558 &Target::from_resolved_target_triple("s390x-unknown-linux-gnu").unwrap(),
559 InterpreterKind::CPython,
560 (3, 10),
561 "",
562 )
563 .unwrap();
564 assert_eq!(sysconfig.ext_suffix, ".cpython-310-s390x-linux-gnu.so");
565
566 // PyPy
567 let sysconfig = InterpreterConfig::lookup_one(
568 &Target::from_resolved_target_triple("x86_64-unknown-linux-gnu").unwrap(),
569 InterpreterKind::PyPy,
570 (3, 9),
571 "",
572 )
573 .unwrap();
574 assert_eq!(sysconfig.abiflags, "");
575 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-x86_64-linux-gnu.so");
576
577 let sysconfig = InterpreterConfig::lookup_one(
578 &Target::from_resolved_target_triple("i686-unknown-linux-gnu").unwrap(),
579 InterpreterKind::PyPy,
580 (3, 9),
581 "",
582 )
583 .unwrap();
584 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-x86-linux-gnu.so");
585
586 let sysconfig = InterpreterConfig::lookup_one(
587 &Target::from_resolved_target_triple("aarch64-unknown-linux-gnu").unwrap(),
588 InterpreterKind::PyPy,
589 (3, 9),
590 "",
591 )
592 .unwrap();
593 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-aarch64-linux-gnu.so");
594
595 let sysconfig = InterpreterConfig::lookup_one(
596 &Target::from_resolved_target_triple("armv7-unknown-linux-gnueabihf").unwrap(),
597 InterpreterKind::PyPy,
598 (3, 9),
599 "",
600 )
601 .unwrap();
602 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-arm-linux-gnu.so");
603
604 let sysconfig = InterpreterConfig::lookup_one(
605 &Target::from_resolved_target_triple("arm-unknown-linux-gnueabihf").unwrap(),
606 InterpreterKind::PyPy,
607 (3, 9),
608 "",
609 )
610 .unwrap();
611 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-arm-linux-gnu.so");
612
613 let sysconfig = InterpreterConfig::lookup_one(
614 &Target::from_resolved_target_triple("powerpc64le-unknown-linux-gnu").unwrap(),
615 InterpreterKind::PyPy,
616 (3, 9),
617 "",
618 )
619 .unwrap();
620 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-ppc_64-linux-gnu.so");
621
622 let sysconfig = InterpreterConfig::lookup_one(
623 &Target::from_resolved_target_triple("s390x-unknown-linux-gnu").unwrap(),
624 InterpreterKind::PyPy,
625 (3, 9),
626 "",
627 )
628 .unwrap();
629 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-s390x-linux-gnu.so");
630 }
631
632 #[test]
633 fn test_well_known_sysconfigs_macos() {
634 // CPython
635 let sysconfig = InterpreterConfig::lookup_one(
636 &Target::from_resolved_target_triple("x86_64-apple-darwin").unwrap(),
637 InterpreterKind::CPython,
638 (3, 10),
639 "",
640 )
641 .unwrap();
642 assert_eq!(sysconfig.ext_suffix, ".cpython-310-darwin.so");
643
644 let sysconfig = InterpreterConfig::lookup_one(
645 &Target::from_resolved_target_triple("aarch64-apple-darwin").unwrap(),
646 InterpreterKind::CPython,
647 (3, 10),
648 "",
649 )
650 .unwrap();
651 assert_eq!(sysconfig.ext_suffix, ".cpython-310-darwin.so");
652
653 let sysconfig = InterpreterConfig::lookup_one(
654 &Target::from_resolved_target_triple("x86_64-apple-darwin").unwrap(),
655 InterpreterKind::CPython,
656 (3, 7),
657 "",
658 )
659 .unwrap();
660 assert_eq!(sysconfig.abiflags, "m");
661 assert_eq!(sysconfig.ext_suffix, ".cpython-37m-darwin.so");
662
663 // PyPy
664 let sysconfig = InterpreterConfig::lookup_one(
665 &Target::from_resolved_target_triple("x86_64-apple-darwin").unwrap(),
666 InterpreterKind::PyPy,
667 (3, 9),
668 "",
669 )
670 .unwrap();
671 assert_eq!(sysconfig.abiflags, "");
672 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-darwin.so");
673
674 let sysconfig = InterpreterConfig::lookup_one(
675 &Target::from_resolved_target_triple("aarch64-apple-darwin").unwrap(),
676 InterpreterKind::PyPy,
677 (3, 9),
678 "",
679 )
680 .unwrap();
681 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-darwin.so");
682 }
683
684 #[test]
685 fn test_well_known_sysconfigs_windows() {
686 // CPython
687 let sysconfig = InterpreterConfig::lookup_one(
688 &Target::from_resolved_target_triple("x86_64-pc-windows-msvc").unwrap(),
689 InterpreterKind::CPython,
690 (3, 10),
691 "",
692 )
693 .unwrap();
694 assert_eq!(sysconfig.ext_suffix, ".cp310-win_amd64.pyd");
695
696 let sysconfig = InterpreterConfig::lookup_one(
697 &Target::from_resolved_target_triple("i686-pc-windows-msvc").unwrap(),
698 InterpreterKind::CPython,
699 (3, 10),
700 "",
701 )
702 .unwrap();
703 assert_eq!(sysconfig.ext_suffix, ".cp310-win32.pyd");
704
705 let sysconfig = InterpreterConfig::lookup_one(
706 &Target::from_resolved_target_triple("aarch64-pc-windows-msvc").unwrap(),
707 InterpreterKind::CPython,
708 (3, 10),
709 "",
710 )
711 .unwrap();
712 assert_eq!(sysconfig.ext_suffix, ".cp310-win_arm64.pyd");
713
714 // PyPy
715 let sysconfig = InterpreterConfig::lookup_one(
716 &Target::from_resolved_target_triple("x86_64-pc-windows-msvc").unwrap(),
717 InterpreterKind::PyPy,
718 (3, 9),
719 "",
720 )
721 .unwrap();
722 assert_eq!(sysconfig.ext_suffix, ".pypy39-pp73-win_amd64.pyd");
723 }
724
725 #[test]
726 fn test_well_known_sysconfigs_freebsd() {
727 // CPython
728 let sysconfig = InterpreterConfig::lookup_one(
729 &Target::from_resolved_target_triple("x86_64-unknown-freebsd").unwrap(),
730 InterpreterKind::CPython,
731 (3, 7),
732 "",
733 )
734 .unwrap();
735 assert_eq!(sysconfig.abiflags, "m");
736 assert_eq!(sysconfig.ext_suffix, ".so");
737
738 let sysconfig = InterpreterConfig::lookup_one(
739 &Target::from_resolved_target_triple("x86_64-unknown-freebsd").unwrap(),
740 InterpreterKind::CPython,
741 (3, 10),
742 "",
743 )
744 .unwrap();
745 assert_eq!(sysconfig.abiflags, "");
746 assert_eq!(sysconfig.ext_suffix, ".cpython-310.so");
747
748 let sysconfig = InterpreterConfig::lookup_one(
749 &Target::from_resolved_target_triple("i686-unknown-freebsd").unwrap(),
750 InterpreterKind::CPython,
751 (3, 10),
752 "",
753 )
754 .unwrap();
755 assert_eq!(sysconfig.ext_suffix, ".cpython-310.so");
756
757 let sysconfig = InterpreterConfig::lookup_one(
758 &Target::from_resolved_target_triple("aarch64-unknown-freebsd").unwrap(),
759 InterpreterKind::CPython,
760 (3, 10),
761 "",
762 )
763 .unwrap();
764 assert_eq!(sysconfig.ext_suffix, ".cpython-310.so");
765
766 let sysconfig = InterpreterConfig::lookup_one(
767 &Target::from_resolved_target_triple("armv7-unknown-freebsd").unwrap(),
768 InterpreterKind::CPython,
769 (3, 10),
770 "",
771 )
772 .unwrap();
773 assert_eq!(sysconfig.ext_suffix, ".cpython-310.so");
774 }
775
776 #[test]
777 fn test_well_known_sysconfigs_netbsd() {
778 // CPython
779 let sysconfig = InterpreterConfig::lookup_one(
780 &Target::from_resolved_target_triple("x86_64-unknown-netbsd").unwrap(),
781 InterpreterKind::CPython,
782 (3, 7),
783 "",
784 )
785 .unwrap();
786 assert_eq!(sysconfig.abiflags, "");
787 assert_eq!(sysconfig.ext_suffix, ".so");
788
789 let sysconfig = InterpreterConfig::lookup_one(
790 &Target::from_resolved_target_triple("x86_64-unknown-netbsd").unwrap(),
791 InterpreterKind::CPython,
792 (3, 10),
793 "",
794 )
795 .unwrap();
796 assert_eq!(sysconfig.ext_suffix, ".so");
797 }
798
799 #[test]
800 fn test_well_known_sysconfigs_openbsd() {
801 // CPython
802 let sysconfig = InterpreterConfig::lookup_one(
803 &Target::from_resolved_target_triple("x86_64-unknown-openbsd").unwrap(),
804 InterpreterKind::CPython,
805 (3, 10),
806 "",
807 )
808 .unwrap();
809 assert_eq!(sysconfig.ext_suffix, ".cpython-310.so");
810
811 let sysconfig = InterpreterConfig::lookup_one(
812 &Target::from_resolved_target_triple("i686-unknown-openbsd").unwrap(),
813 InterpreterKind::CPython,
814 (3, 10),
815 "",
816 )
817 .unwrap();
818 assert_eq!(sysconfig.ext_suffix, ".cpython-310.so");
819
820 let sysconfig = InterpreterConfig::lookup_one(
821 &Target::from_resolved_target_triple("aarch64-unknown-openbsd").unwrap(),
822 InterpreterKind::CPython,
823 (3, 10),
824 "",
825 )
826 .unwrap();
827 assert_eq!(sysconfig.ext_suffix, ".cpython-310.so");
828 }
829
830 #[test]
831 fn test_well_known_sysconfigs_emscripten() {
832 // CPython
833 let sysconfig = InterpreterConfig::lookup_one(
834 &Target::from_resolved_target_triple("wasm32-unknown-emscripten").unwrap(),
835 InterpreterKind::CPython,
836 (3, 10),
837 "",
838 )
839 .unwrap();
840 assert_eq!(sysconfig.abiflags, "");
841 assert_eq!(sysconfig.ext_suffix, ".cpython-310-wasm32-emscripten.so");
842 }
843
844 #[test]
845 fn test_pyo3_config_file() {
846 let sysconfig = InterpreterConfig::lookup_one(
847 &Target::from_resolved_target_triple("x86_64-unknown-linux-gnu").unwrap(),
848 InterpreterKind::CPython,
849 (3, 10),
850 "",
851 )
852 .unwrap();
853 let config_file = sysconfig.pyo3_config_file();
854 let expected = expect![[r#"
855 implementation=CPython
856 version=3.10
857 shared=true
858 abi3=false
859 build_flags=
860 suppress_build_script_link_lines=false
861 pointer_width=64"#]];
862 expected.assert_eq(&config_file);
863 }
864
865 #[test]
866 fn test_pyo3_config_file_free_threaded_python_3_13() {
867 let sysconfig = InterpreterConfig::lookup_one(
868 &Target::from_resolved_target_triple("x86_64-unknown-linux-gnu").unwrap(),
869 InterpreterKind::CPython,
870 (3, 13),
871 "t",
872 )
873 .unwrap();
874 assert_eq!(sysconfig.ext_suffix, ".cpython-313t-x86_64-linux-gnu.so");
875 let config_file = sysconfig.pyo3_config_file();
876 let expected = expect![[r#"
877 implementation=CPython
878 version=3.13
879 shared=true
880 abi3=false
881 build_flags=Py_GIL_DISABLED
882 suppress_build_script_link_lines=false
883 pointer_width=64"#]];
884 expected.assert_eq(&config_file);
885 }
886
887 #[test]
888 fn test_pyo3_config_file_musl_python_3_11() {
889 let sysconfig = InterpreterConfig::lookup_one(
890 &Target::from_resolved_target_triple("x86_64-unknown-linux-musl").unwrap(),
891 InterpreterKind::CPython,
892 (3, 11),
893 "",
894 )
895 .unwrap();
896 assert_eq!(sysconfig.ext_suffix, ".cpython-311-x86_64-linux-musl.so");
897 let config_file = sysconfig.pyo3_config_file();
898 let expected = expect![[r#"
899 implementation=CPython
900 version=3.11
901 shared=true
902 abi3=false
903 build_flags=
904 suppress_build_script_link_lines=false
905 pointer_width=64"#]];
906 expected.assert_eq(&config_file);
907 }
908 }