Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross building fixes #2204

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions src/python_interpreter/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,8 @@ impl InterpreterConfig {
// Python 2 is not supported
return None;
}
let python_arch = if matches!(target.target_arch(), Arch::Armv6L | Arch::Armv7L) {
"arm"
} else if matches!(target.target_arch(), Arch::Powerpc64Le) && python_impl == PyPy {
"ppc_64"
} else if matches!(target.target_arch(), Arch::X86) && python_impl == PyPy {
"x86"
} else {
target.get_python_arch()
};
// See https://github.com/pypa/auditwheel/issues/349
let target_env = match python_impl {
CPython => {
if python_version >= (3, 11) {
target.target_env().to_string()
} else {
target.target_env().to_string().replace("musl", "gnu")
}
}
PyPy | GraalPy => "gnu".to_string(),
};
let python_ext_arch = target.get_python_ext_arch(python_impl);
let target_env = target.get_python_target_env(python_impl, python_version);
match (target.target_os(), python_impl) {
(Os::Linux, CPython) => {
let abiflags = if python_version < (3, 8) {
Expand All @@ -77,7 +59,7 @@ impl InterpreterConfig {
let ldversion = format!("{}{}{}", major, minor, abiflags);
let ext_suffix = format!(
".cpython-{}-{}-linux-{}.so",
ldversion, python_arch, target_env
ldversion, python_ext_arch, target_env
);
Some(Self {
major,
Expand All @@ -90,7 +72,8 @@ impl InterpreterConfig {
}
(Os::Linux, PyPy) => {
let abi_tag = format!("pypy{}{}-{}", major, minor, PYPY_ABI_TAG);
let ext_suffix = format!(".{}-{}-linux-{}.so", abi_tag, python_arch, target_env);
let ext_suffix =
format!(".{}-{}-linux-{}.so", abi_tag, python_ext_arch, target_env);
Some(Self {
major,
minor,
Expand Down Expand Up @@ -204,7 +187,8 @@ impl InterpreterConfig {
}
(Os::Emscripten, CPython) => {
let ldversion = format!("{}{}", major, minor);
let ext_suffix = format!(".cpython-{}-{}-emscripten.so", ldversion, python_arch);
let ext_suffix =
format!(".cpython-{}-{}-emscripten.so", ldversion, python_ext_arch);
Some(Self {
major,
minor,
Expand Down Expand Up @@ -300,19 +284,14 @@ impl InterpreterConfig {
};
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 {
target.target_env().to_string().replace("musl", "gnu")
};
let target_env = target.get_python_target_env(interpreter_kind, (major, minor));
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_ext_arch(interpreter_kind),
target.get_python_os(),
target_env,
file_ext,
Expand All @@ -325,7 +304,7 @@ impl InterpreterConfig {
major,
minor,
abi_tag,
target.get_python_arch(),
target.get_python_ext_arch(interpreter_kind),
target.get_python_os(),
target_env,
file_ext,
Expand All @@ -336,7 +315,7 @@ impl InterpreterConfig {
format!(
".{}-{}-{}.{}",
abi_tag.replace('_', "-"),
target.get_python_arch(),
target.get_python_ext_arch(interpreter_kind),
target.get_python_os(),
file_ext,
)
Expand All @@ -347,7 +326,7 @@ impl InterpreterConfig {
format!(
".cpython-{}-{}-{}.{}",
abi_tag,
target.get_python_arch(),
target.get_python_ext_arch(interpreter_kind),
target.get_python_os(),
file_ext
)
Expand Down Expand Up @@ -445,6 +424,33 @@ mod test {
".cpython-310-powerpc64le-linux-gnu.so"
);

let sysconfig = InterpreterConfig::lookup_one(
&Target::from_target_triple(Some("powerpc-unknown-linux-gnu".to_string())).unwrap(),
InterpreterKind::CPython,
(3, 10),
)
.unwrap();
assert_eq!(sysconfig.ext_suffix, ".cpython-310-powerpc-linux-gnu.so");

let sysconfig = InterpreterConfig::lookup_one(
&Target::from_target_triple(Some("mips64-unknown-linux-gnu".to_string())).unwrap(),
InterpreterKind::CPython,
(3, 10),
)
.unwrap();
assert_eq!(
sysconfig.ext_suffix,
".cpython-310-mips64-linux-gnuabi64.so"
);

let sysconfig = InterpreterConfig::lookup_one(
&Target::from_target_triple(Some("mips-unknown-linux-gnu".to_string())).unwrap(),
InterpreterKind::CPython,
(3, 10),
)
.unwrap();
assert_eq!(sysconfig.ext_suffix, ".cpython-310-mips-linux-gnu.so");

let sysconfig = InterpreterConfig::lookup_one(
&Target::from_target_triple(Some("s390x-unknown-linux-gnu".to_string())).unwrap(),
InterpreterKind::CPython,
Expand Down
40 changes: 40 additions & 0 deletions src/target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::cross_compile::is_cross_compiling;
use crate::python_interpreter::InterpreterKind;
use crate::python_interpreter::InterpreterKind::{CPython, GraalPy, PyPy};
use crate::PlatformTag;
use anyhow::{anyhow, bail, format_err, Result};
use platform_info::*;
Expand Down Expand Up @@ -368,6 +370,44 @@ impl Target {
}
}

/// Returns the extension architecture name python uses in `ext_suffix` for this architecture.
pub fn get_python_ext_arch(&self, python_impl: InterpreterKind) -> &str {
if matches!(self.target_arch(), Arch::Armv6L | Arch::Armv7L) {
"arm"
} else if matches!(self.target_arch(), Arch::Powerpc64Le)
&& python_impl == InterpreterKind::PyPy
{
"ppc_64"
} else if matches!(self.target_arch(), Arch::X86) && python_impl == InterpreterKind::PyPy {
"x86"
} else if matches!(self.target_arch(), Arch::Powerpc) {
"powerpc"
} else {
self.get_python_arch()
}
}

/// Returns the environment python uses in `ext_suffix` for this architecture.
pub fn get_python_target_env(
&self,
python_impl: InterpreterKind,
python_version: (usize, usize),
) -> String {
match python_impl {
CPython => {
// For musl handling see https://github.com/pypa/auditwheel/issues/349
if matches!(self.target_arch(), Arch::Mips64 | Arch::Mips64el) && self.is_linux() {
"gnuabi64".to_string()
} else if python_version >= (3, 11) {
self.target_env().to_string()
} else {
self.target_env().to_string().replace("musl", "gnu")
}
}
PyPy | GraalPy => "gnu".to_string(),
}
}

/// Returns the name python uses in `sys.platform` for this os
pub fn get_python_os(&self) -> &str {
match self.os {
Expand Down
Loading