Debian Codegraph @

src/target/mod.rs

raw
1 use crate::build_options::TargetTriple;
2 use crate::cross_compile::is_cross_compiling;
3 use crate::python_interpreter::InterpreterKind;
4 use crate::python_interpreter::InterpreterKind::{CPython, GraalPy, PyPy};
5 use crate::PlatformTag;
6 use anyhow::{anyhow, bail, format_err, Result};
7 use platform_info::*;
8 use rustc_version::VersionMeta;
9 use serde::Deserialize;
10 use std::env;
11 use std::fmt;
12 use std::path::Path;
13 use std::path::PathBuf;
14 use std::process::Command;
15 use std::str;
16 use std::str::FromStr;
17 use target_lexicon::{Architecture, Environment, Triple};
18 use tracing::error;
19
20 mod legacy_py;
21 mod pypi_tags;
22
23 pub use pypi_tags::{is_arch_supported_by_pypi, validate_wheel_filename_for_pypi};
24
25 pub(crate) const RUST_1_64_0: semver::Version = semver::Version::new(1, 64, 0);
26
27 /// All supported operating system
28 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize)]
29 #[serde(rename_all = "lowercase")]
30 pub enum Os {
31 Linux,
32 Windows,
33 Macos,
34 Ios,
35 FreeBsd,
36 NetBsd,
37 OpenBsd,
38 Dragonfly,
39 Solaris,
40 Illumos,
41 Haiku,
42 Emscripten,
43 Wasi,
44 Aix,
45 Hurd,
46 }
47
48 impl fmt::Display for Os {
49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 match *self {
51 Os::Linux => write!(f, "Linux"),
52 Os::Windows => write!(f, "Windows"),
53 Os::Macos => write!(f, "macOS"),
54 Os::Ios => write!(f, "iOS"),
55 Os::FreeBsd => write!(f, "FreeBSD"),
56 Os::NetBsd => write!(f, "NetBSD"),
57 Os::OpenBsd => write!(f, "OpenBSD"),
58 Os::Dragonfly => write!(f, "DragonFly"),
59 Os::Solaris => write!(f, "Solaris"),
60 Os::Illumos => write!(f, "Illumos"),
61 Os::Haiku => write!(f, "Haiku"),
62 Os::Emscripten => write!(f, "Emscripten"),
63 Os::Wasi => write!(f, "Wasi"),
64 Os::Aix => write!(f, "AIX"),
65 Os::Hurd => write!(f, "Hurd"),
66 }
67 }
68 }
69
70 /// All supported CPU architectures
71 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize)]
72 #[serde(rename_all = "lowercase")]
73 pub enum Arch {
74 Aarch64,
75 Armv5teL,
76 Armv6L,
77 Armv7L,
78 #[serde(alias = "ppc")]
79 Powerpc,
80 #[serde(alias = "ppc64le")]
81 Powerpc64Le,
82 #[serde(alias = "ppc64")]
83 Powerpc64,
84 #[serde(alias = "i686")]
85 X86,
86 X86_64,
87 S390X,
88 Wasm32,
89 Riscv32,
90 Riscv64,
91 Mips64el,
92 Mips64,
93 Mipsel,
94 Mips,
95 Sparc64,
96 Sparcv9,
97 LoongArch64,
98 }
99
100 impl fmt::Display for Arch {
101 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102 match *self {
103 Arch::Aarch64 => write!(f, "aarch64"),
104 Arch::Armv5teL => write!(f, "armv5tel"),
105 Arch::Armv6L => write!(f, "armv6l"),
106 Arch::Armv7L => write!(f, "armv7l"),
107 Arch::Powerpc => write!(f, "ppc"),
108 Arch::Powerpc64Le => write!(f, "ppc64le"),
109 Arch::Powerpc64 => write!(f, "ppc64"),
110 Arch::X86 => write!(f, "i686"),
111 Arch::X86_64 => write!(f, "x86_64"),
112 Arch::S390X => write!(f, "s390x"),
113 Arch::Wasm32 => write!(f, "wasm32"),
114 Arch::Riscv32 => write!(f, "riscv32"),
115 Arch::Riscv64 => write!(f, "riscv64"),
116 Arch::Mips64el => write!(f, "mips64el"),
117 Arch::Mips64 => write!(f, "mips64"),
118 Arch::Mipsel => write!(f, "mipsel"),
119 Arch::Mips => write!(f, "mips"),
120 Arch::Sparc64 => write!(f, "sparc64"),
121 Arch::Sparcv9 => write!(f, "sparcv9"),
122 Arch::LoongArch64 => write!(f, "loongarch64"),
123 }
124 }
125 }
126
127 impl Arch {
128 /// Represents the hardware platform.
129 ///
130 /// This is the same as the native platform's `uname -m` output.
131 pub fn machine(&self) -> &'static str {
132 // See https://www.freebsd.org/cgi/man.cgi?query=arch&sektion=7&format=html
133 // MACHINE_ARCH vs MACHINE_CPUARCH vs MACHINE section
134 match self {
135 Arch::Aarch64 => "arm64",
136 Arch::Armv5teL | Arch::Armv6L | Arch::Armv7L => "arm",
137 Arch::Powerpc | Arch::Powerpc64Le | Arch::Powerpc64 => "powerpc",
138 Arch::X86 => "i386",
139 Arch::X86_64 => "amd64",
140 Arch::Riscv32 | Arch::Riscv64 => "riscv",
141 Arch::Mips64el | Arch::Mips64 | Arch::Mipsel | Arch::Mips => "mips",
142 // sparc64 is unsupported since FreeBSD 13.0
143 Arch::Sparc64 => "sparc64",
144 Arch::Sparcv9 => "sparcv9",
145 Arch::Wasm32 => "wasm32",
146 Arch::S390X => "s390x",
147 Arch::LoongArch64 => "loongarch64",
148 }
149 }
150 }
151
152 // Returns the set of supported architectures for each operating system
153 fn get_supported_architectures(os: &Os) -> Vec<Arch> {
154 match os {
155 Os::Linux => vec![
156 Arch::Aarch64,
157 Arch::Armv5teL,
158 Arch::Armv6L,
159 Arch::Armv7L,
160 Arch::Powerpc,
161 Arch::Powerpc64,
162 Arch::Powerpc64Le,
163 Arch::S390X,
164 Arch::X86,
165 Arch::X86_64,
166 Arch::Riscv32,
167 Arch::Riscv64,
168 Arch::Mips64el,
169 Arch::Mips64,
170 Arch::Mipsel,
171 Arch::Mips,
172 Arch::Sparc64,
173 Arch::LoongArch64,
174 ],
175 Os::Windows => vec![Arch::X86, Arch::X86_64, Arch::Aarch64],
176 Os::Macos => vec![Arch::Aarch64, Arch::X86_64],
177 Os::Ios => vec![Arch::Aarch64, Arch::X86_64],
178 Os::FreeBsd | Os::NetBsd => vec![
179 Arch::Aarch64,
180 Arch::Armv6L,
181 Arch::Armv7L,
182 Arch::Powerpc,
183 Arch::Powerpc64,
184 Arch::Powerpc64Le,
185 Arch::X86,
186 Arch::X86_64,
187 Arch::Riscv32,
188 Arch::Riscv64,
189 Arch::Mips64el,
190 Arch::Mipsel,
191 Arch::Sparc64,
192 ],
193 Os::OpenBsd => vec![
194 Arch::X86,
195 Arch::X86_64,
196 Arch::Aarch64,
197 Arch::Armv7L,
198 Arch::Powerpc,
199 Arch::Powerpc64,
200 Arch::Powerpc64Le,
201 Arch::Riscv32,
202 Arch::Riscv64,
203 Arch::Sparc64,
204 ],
205 Os::Dragonfly => vec![Arch::X86_64],
206 Os::Illumos => vec![Arch::X86_64],
207 Os::Haiku => vec![Arch::X86_64],
208 Os::Solaris => vec![Arch::X86_64, Arch::Sparc64, Arch::Sparcv9],
209 Os::Emscripten | Os::Wasi => vec![Arch::Wasm32],
210 Os::Aix => vec![Arch::Powerpc64],
211 Os::Hurd => vec![Arch::X86, Arch::X86_64],
212 }
213 }
214
215 /// The part of the current platform that is relevant when building wheels and is supported
216 #[derive(Debug, Clone, Eq, PartialEq)]
217 pub struct Target {
218 os: Os,
219 arch: Arch,
220 env: Environment,
221 triple: String,
222 cross_compiling: bool,
223 /// rustc version information
224 pub(crate) rustc_version: VersionMeta,
225 /// Is user specified `--target`
226 pub(crate) user_specified: bool,
227 }
228
229 impl fmt::Display for Target {
230 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
231 write!(f, "{}", self.triple)
232 }
233 }
234
235 impl Target {
236 /// Uses the given target triple or tries the guess the current target by using the one used
237 /// for compilation
238 ///
239 /// Fails if the target triple isn't supported
240 pub fn from_target_triple(target_triple: Option<&TargetTriple>) -> Result<Self> {
241 let rustc_version = rustc_version_meta()?;
242
243 let triple = match &target_triple {
244 None => rustc_version.host.to_string(),
245 Some(TargetTriple::Universal2) => "aarch64-apple-darwin".to_string(),
246 Some(TargetTriple::Regular(target_triple)) => target_triple.clone(),
247 };
248
249 Self::from_triple(rustc_version, &triple, target_triple.is_some())
250 }
251
252 /// Convert a Rust target triple to a [`Target`].
253 pub fn from_resolved_target_triple(target_triple: &str) -> Result<Self> {
254 let rustc_version = rustc_version_meta()?;
255
256 Self::from_triple(rustc_version, target_triple, true)
257 }
258
259 fn from_triple(
260 rustc_version: VersionMeta,
261 target_triple: &str,
262 user_specified: bool,
263 ) -> Result<Self> {
264 use target_lexicon::{
265 ArmArchitecture, Mips32Architecture, Mips64Architecture, OperatingSystem,
266 };
267 let platform = Triple::from_str(target_triple)
268 .map_err(|_| format_err!("Unknown target triple {}", target_triple))?;
269
270 let os = match platform.operating_system {
271 OperatingSystem::Linux => Os::Linux,
272 OperatingSystem::Windows => Os::Windows,
273 OperatingSystem::MacOSX(_) | OperatingSystem::Darwin(_) => Os::Macos,
274 OperatingSystem::IOS(_) => Os::Ios,
275 OperatingSystem::Netbsd => Os::NetBsd,
276 OperatingSystem::Freebsd => Os::FreeBsd,
277 OperatingSystem::Openbsd => Os::OpenBsd,
278 OperatingSystem::Dragonfly => Os::Dragonfly,
279 OperatingSystem::Solaris => Os::Solaris,
280 OperatingSystem::Illumos => Os::Illumos,
281 OperatingSystem::Haiku => Os::Haiku,
282 OperatingSystem::Emscripten => Os::Emscripten,
283 OperatingSystem::Wasi | OperatingSystem::WasiP1 | OperatingSystem::WasiP2 => Os::Wasi,
284 OperatingSystem::Aix => Os::Aix,
285 OperatingSystem::Hurd => Os::Hurd,
286 unsupported => bail!("The operating system {:?} is not supported", unsupported),
287 };
288
289 let arch = match platform.architecture {
290 Architecture::X86_64 | Architecture::X86_64h => Arch::X86_64,
291 Architecture::X86_32(_) => Arch::X86,
292 Architecture::Arm(arm_arch) => match arm_arch {
293 ArmArchitecture::Armv5te => Arch::Armv5teL,
294 ArmArchitecture::Arm | ArmArchitecture::Armv6 => Arch::Armv6L,
295 ArmArchitecture::Armv7 => Arch::Armv7L,
296 _ => bail!("The architecture {} is not supported", arm_arch),
297 },
298 Architecture::Aarch64(_) => Arch::Aarch64,
299 Architecture::Powerpc => Arch::Powerpc,
300 Architecture::Powerpc64 => Arch::Powerpc64,
301 Architecture::Powerpc64le => Arch::Powerpc64Le,
302 Architecture::S390x => Arch::S390X,
303 Architecture::Wasm32 => Arch::Wasm32,
304 Architecture::Riscv32(_) => Arch::Riscv32,
305 Architecture::Riscv64(_) => Arch::Riscv64,
306 Architecture::Mips64(mips64_arch) => match mips64_arch {
307 Mips64Architecture::Mips64el => Arch::Mips64el,
308 Mips64Architecture::Mips64 => Arch::Mips64,
309 _ => bail!("The architecture {} is not supported", mips64_arch),
310 },
311 Architecture::Mips32(mips32_arch) => match mips32_arch {
312 Mips32Architecture::Mipsel => Arch::Mipsel,
313 Mips32Architecture::Mips => Arch::Mips,
314 _ => bail!("The architecture {} is not supported", mips32_arch),
315 },
316 Architecture::Sparc64 => Arch::Sparc64,
317 Architecture::Sparcv9 => Arch::Sparcv9,
318 Architecture::LoongArch64 => Arch::LoongArch64,
319 unsupported => bail!("The architecture {} is not supported", unsupported),
320 };
321
322 if !get_supported_architectures(&os).contains(&arch) {
323 bail!("{} is not supported on {}", arch, os);
324 }
325
326 let mut target = Target {
327 os,
328 arch,
329 env: platform.environment,
330 triple: target_triple.to_string(),
331 rustc_version,
332 user_specified,
333 cross_compiling: false,
334 };
335 target.cross_compiling = is_cross_compiling(&target)?;
336 Ok(target)
337 }
338
339 /// Returns the platform architecture
340 pub fn get_platform_arch(&self) -> Result<String> {
341 if self.cross_compiling {
342 return Ok(self.arch.to_string());
343 }
344 let machine = PlatformInfo::new().map(|info| info.machine().to_string_lossy().into_owned());
345 let arch = match machine {
346 Ok(machine) => {
347 let linux32 = (machine == "x86_64" && self.arch != Arch::X86_64)
348 || (machine == "aarch64" && self.arch != Arch::Aarch64);
349 if linux32 {
350 // When running in Docker sometimes uname returns 64-bit architecture while the container is actually 32-bit,
351 // In this case we trust the architecture of rustc target
352 self.arch.to_string()
353 } else {
354 machine
355 }
356 }
357 Err(err) => {
358 error!("Failed to get machine architecture: {}", err);
359 self.arch.to_string()
360 }
361 };
362 Ok(arch)
363 }
364
365 /// Returns the platform release
366 pub fn get_platform_release(&self) -> Result<String> {
367 let os = self.os.to_string();
368 let os_version = env::var(format!("MATURIN_{}_VERSION", os.to_ascii_uppercase()));
369 let release = match os_version {
370 Ok(os_ver) => os_ver,
371 Err(_) => {
372 let info = PlatformInfo::new()
373 .map_err(|e| anyhow!("Failed to fetch platform information: {e}"))?;
374 info.release().to_string_lossy().into_owned()
375 }
376 };
377 let release = release.replace(['.', '-'], "_");
378 Ok(release)
379 }
380
381 /// Returns the name python uses in `platform.machine()` for this architecture.
382 pub fn get_python_arch(&self) -> &str {
383 match self.arch {
384 Arch::Aarch64 => "aarch64",
385 Arch::Armv5teL => "armv5tel",
386 Arch::Armv6L => "armv6l",
387 Arch::Armv7L => "armv7l",
388 Arch::Powerpc => "ppc",
389 Arch::Powerpc64Le => "powerpc64le",
390 Arch::Powerpc64 => "powerpc64",
391 Arch::X86 => "i386",
392 Arch::X86_64 => "x86_64",
393 Arch::S390X => "s390x",
394 Arch::Wasm32 => "wasm32",
395 Arch::Riscv32 => "riscv32",
396 Arch::Riscv64 => "riscv64",
397 // It's kinda surprising that Python doesn't include the `el` suffix
398 Arch::Mips64el | Arch::Mips64 => "mips64",
399 Arch::Mipsel | Arch::Mips => "mips",
400 Arch::Sparc64 => "sparc64",
401 Arch::Sparcv9 => "sparcv9",
402 Arch::LoongArch64 => "loongarch64",
403 }
404 }
405
406 /// Returns the extension architecture name python uses in `ext_suffix` for this architecture.
407 pub fn get_python_ext_arch(&self, python_impl: InterpreterKind) -> &str {
408 if matches!(
409 self.target_arch(),
410 Arch::Armv5teL | Arch::Armv6L | Arch::Armv7L
411 ) {
412 "arm"
413 } else if matches!(self.target_arch(), Arch::Powerpc64Le)
414 && python_impl == InterpreterKind::PyPy
415 {
416 "ppc_64"
417 } else if matches!(self.target_arch(), Arch::X86) && python_impl == InterpreterKind::PyPy {
418 "x86"
419 } else if matches!(self.target_arch(), Arch::Powerpc) {
420 "powerpc"
421 } else {
422 self.get_python_arch()
423 }
424 }
425
426 /// Returns the environment python uses in `ext_suffix` for this architecture.
427 pub fn get_python_target_env(
428 &self,
429 python_impl: InterpreterKind,
430 python_version: (usize, usize),
431 ) -> String {
432 match python_impl {
433 CPython => {
434 // For musl handling see https://github.com/pypa/auditwheel/issues/349
435 if matches!(self.target_arch(), Arch::Mips64 | Arch::Mips64el) && self.is_linux() {
436 "gnuabi64".to_string()
437 } else if python_version >= (3, 11) {
438 self.target_env().to_string()
439 } else {
440 self.target_env().to_string().replace("musl", "gnu")
441 }
442 }
443 PyPy | GraalPy => "gnu".to_string(),
444 }
445 }
446
447 /// Returns the name python uses in `sys.platform` for this os
448 pub fn get_python_os(&self) -> &str {
449 match self.os {
450 Os::Windows => "windows",
451 Os::Linux => "linux",
452 Os::Macos => "darwin",
453 Os::Ios => "darwin",
454 Os::FreeBsd => "freebsd",
455 Os::NetBsd => "netbsd",
456 Os::OpenBsd => "openbsd",
457 Os::Dragonfly => "dragonfly",
458 Os::Solaris => "sunos",
459 Os::Illumos => "sunos",
460 Os::Haiku => "haiku",
461 Os::Emscripten => "emscripten",
462 // This isn't real, there's no sys.platform here
463 Os::Wasi => "wasi",
464 Os::Aix => "aix",
465 Os::Hurd => "gnu",
466 }
467 }
468
469 /// Returns the oldest possible Manylinux tag for this architecture
470 pub fn get_minimum_manylinux_tag(&self) -> PlatformTag {
471 match self.arch {
472 Arch::Aarch64 | Arch::Armv7L | Arch::Powerpc64 | Arch::Powerpc64Le | Arch::S390X => {
473 PlatformTag::manylinux2014()
474 }
475 Arch::X86 | Arch::X86_64 => {
476 // rustc 1.64.0 bumps glibc requirement to 2.17
477 // see https://blog.rust-lang.org/2022/08/01/Increasing-glibc-kernel-requirements.html
478 if self.rustc_version.semver >= RUST_1_64_0 {
479 PlatformTag::manylinux2014()
480 } else {
481 PlatformTag::manylinux2010()
482 }
483 }
484 Arch::Riscv64 => PlatformTag::Manylinux {
485 major: 2,
486 minor: 31,
487 },
488 Arch::LoongArch64 => PlatformTag::Manylinux {
489 major: 2,
490 minor: 36,
491 },
492 Arch::Armv5teL
493 | Arch::Armv6L
494 | Arch::Wasm32
495 | Arch::Riscv32
496 | Arch::Mips64el
497 | Arch::Mips64
498 | Arch::Mipsel
499 | Arch::Mips
500 | Arch::Powerpc
501 | Arch::Sparc64
502 | Arch::Sparcv9 => PlatformTag::Linux,
503 }
504 }
505
506 /// Returns whether the platform is 64 bit or 32 bit
507 pub fn pointer_width(&self) -> usize {
508 match self.arch {
509 Arch::Aarch64
510 | Arch::Powerpc64
511 | Arch::Powerpc64Le
512 | Arch::X86_64
513 | Arch::S390X
514 | Arch::Riscv64
515 | Arch::Mips64el
516 | Arch::Mips64
517 | Arch::Sparc64
518 | Arch::Sparcv9
519 | Arch::LoongArch64 => 64,
520 Arch::Armv5teL
521 | Arch::Armv6L
522 | Arch::Armv7L
523 | Arch::X86
524 | Arch::Wasm32
525 | Arch::Mipsel
526 | Arch::Mips
527 | Arch::Riscv32
528 | Arch::Powerpc => 32,
529 }
530 }
531
532 /// Returns target triple as string
533 #[inline]
534 pub fn target_triple(&self) -> &str {
535 &self.triple
536 }
537
538 /// Returns host triple as string
539 #[inline]
540 pub fn host_triple(&self) -> &str {
541 &self.rustc_version.host
542 }
543
544 /// Returns true if the current platform is not windows
545 pub fn is_unix(&self) -> bool {
546 match self.os {
547 Os::Windows => false,
548 Os::Linux
549 | Os::Macos
550 | Os::Ios
551 | Os::FreeBsd
552 | Os::NetBsd
553 | Os::OpenBsd
554 | Os::Dragonfly
555 | Os::Solaris
556 | Os::Illumos
557 | Os::Haiku
558 | Os::Emscripten
559 | Os::Wasi
560 | Os::Aix
561 | Os::Hurd => true,
562 }
563 }
564
565 /// Returns target operating system
566 #[inline]
567 pub fn target_os(&self) -> Os {
568 self.os
569 }
570
571 /// Returns target architecture
572 #[inline]
573 pub fn target_arch(&self) -> Arch {
574 self.arch
575 }
576
577 /// Returns target environment
578 #[inline]
579 pub fn target_env(&self) -> Environment {
580 self.env
581 }
582
583 /// Returns true if the current platform is linux
584 #[inline]
585 pub fn is_linux(&self) -> bool {
586 self.os == Os::Linux
587 }
588
589 /// Returns true if the current platform is freebsd
590 #[inline]
591 pub fn is_freebsd(&self) -> bool {
592 self.os == Os::FreeBsd
593 }
594
595 /// Returns true if the current platform is mac os
596 #[inline]
597 pub fn is_macos(&self) -> bool {
598 self.os == Os::Macos
599 }
600
601 /// Returns true if the current platform is windows
602 #[inline]
603 pub fn is_windows(&self) -> bool {
604 self.os == Os::Windows
605 }
606
607 /// Returns true if the current environment is msvc
608 #[inline]
609 pub fn is_msvc(&self) -> bool {
610 self.env == Environment::Msvc
611 }
612
613 /// Returns true if the current platform is illumos
614 #[inline]
615 pub fn is_illumos(&self) -> bool {
616 self.os == Os::Illumos
617 }
618
619 /// Returns true if the current platform is haiku
620 #[inline]
621 pub fn is_haiku(&self) -> bool {
622 self.os == Os::Haiku
623 }
624
625 /// Returns true if the current platform is Emscripten
626 #[inline]
627 pub fn is_emscripten(&self) -> bool {
628 self.os == Os::Emscripten
629 }
630
631 /// Returns true if we're building a binary for wasm32-wasi
632 #[inline]
633 pub fn is_wasi(&self) -> bool {
634 self.os == Os::Wasi
635 }
636
637 /// Returns true if we're building a binary for GNU/Hurd
638 #[inline]
639 pub fn is_hurd(&self) -> bool {
640 self.os == Os::Hurd
641 }
642
643 /// Returns true if we're building a binary for AIX
644 #[inline]
645 pub fn is_aix(&self) -> bool {
646 self.os == Os::Aix
647 }
648
649 /// Returns true if the current platform's target env is Musl
650 #[inline]
651 pub fn is_musl_libc(&self) -> bool {
652 matches!(
653 self.env,
654 Environment::Musl
655 | Environment::Musleabi
656 | Environment::Musleabihf
657 | Environment::Muslabi64
658 )
659 }
660
661 /// Is cross compiling for this target
662 #[inline]
663 pub fn cross_compiling(&self) -> bool {
664 self.cross_compiling
665 }
666
667 /// Returns the path to the python executable inside a venv
668 pub fn get_venv_python(&self, venv_base: impl AsRef<Path>) -> PathBuf {
669 let python = if self.is_windows() {
670 "python.exe"
671 } else {
672 "python"
673 };
674 self.get_venv_bin_dir(venv_base).join(python)
675 }
676
677 /// Returns the directory where the binaries are stored inside a venv
678 pub fn get_venv_bin_dir(&self, venv_base: impl AsRef<Path>) -> PathBuf {
679 let venv = venv_base.as_ref();
680 if self.is_windows() {
681 let bin_dir = venv.join("Scripts");
682 if bin_dir.join("python.exe").exists() {
683 return bin_dir;
684 }
685 // Python installed via msys2 on Windows might produce a POSIX-like venv
686 // See https://github.com/PyO3/maturin/issues/1108
687 let bin_dir = venv.join("bin");
688 if bin_dir.join("python.exe").exists() {
689 return bin_dir;
690 }
691 // for conda environment
692 venv.to_path_buf()
693 } else {
694 venv.join("bin")
695 }
696 }
697
698 /// Returns the path to the python executable
699 ///
700 /// For windows it's always python.exe for unix it's first the venv's `python`
701 /// and then `python3`
702 pub fn get_python(&self) -> PathBuf {
703 if self.is_windows() {
704 PathBuf::from("python.exe")
705 } else if env::var_os("VIRTUAL_ENV").is_some() {
706 PathBuf::from("python")
707 } else {
708 PathBuf::from("python3")
709 }
710 }
711 }
712
713 fn rustc_version_meta() -> Result<VersionMeta> {
714 let meta = rustc_version::version_meta().map_err(|err| match err {
715 rustc_version::Error::CouldNotExecuteCommand(e)
716 if e.kind() == std::io::ErrorKind::NotFound =>
717 {
718 anyhow!(
719 "rustc, the rust compiler, is not installed or not in PATH. \
720 This package requires Rust and Cargo to compile extensions. \
721 Install it through the system's package manager or via https://rustup.rs/.",
722 )
723 }
724 err => anyhow!(err).context("Failed to run rustc to get the host target"),
725 })?;
726 Ok(meta)
727 }
728
729 pub(crate) fn detect_arch_from_python(python: &PathBuf, target: &Target) -> Option<TargetTriple> {
730 match Command::new(python)
731 .arg("-c")
732 .arg("import sysconfig; print(sysconfig.get_platform(), end='')")
733 .output()
734 {
735 Ok(output) if output.status.success() => {
736 let platform = String::from_utf8_lossy(&output.stdout);
737 if platform.contains("macos") {
738 if platform.contains("x86_64") && target.target_arch() != Arch::X86_64 {
739 return Some(TargetTriple::Regular("x86_64-apple-darwin".to_string()));
740 } else if platform.contains("arm64") && target.target_arch() != Arch::Aarch64 {
741 return Some(TargetTriple::Regular("aarch64-apple-darwin".to_string()));
742 }
743 }
744 }
745 _ => eprintln!("⚠️ Warning: Failed to determine python platform"),
746 }
747 None
748 }