Skip to content

Commit

Permalink
Get wheel platform tag from sysconfig.get_platform()
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Nov 15, 2021
1 parent a294ee7 commit c5a122d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix false positive missing pyinit warning on arm64 macOS in [#673](https://github.com/PyO3/maturin/pull/673)
* Build without rustls on arm64 Windows by nsait-linaro in [#674](https://github.com/PyO3/maturin/pull/674)
* Publish Windows arm64 wheels to PyPI by nsait-linaro in [#675](https://github.com/PyO3/maturin/pull/675)
* Add support for building on Windows mingw platforms in [#677](https://github.com/PyO3/maturin/pull/677)
* Allow building for non-abi3 pypy wheels when the abi3 feature is enabled in [#678](https://github.com/PyO3/maturin/pull/678)
* Add support for cross compiling to different operating systems in [#680](https://github.com/PyO3/maturin/pull/680)

Expand Down
2 changes: 2 additions & 0 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ pub fn find_interpreter(
interpreter_kind,
abi_tag,
libs_dir: PathBuf::from(cross_lib_dir),
platform: None,
}];
}
}
Expand Down Expand Up @@ -618,6 +619,7 @@ pub fn find_interpreter(
interpreter_kind: InterpreterKind::CPython,
abi_tag: None,
libs_dir: PathBuf::from(manual_base_prefix),
platform: None,
}])
} else {
let interp = interpreter
Expand Down
1 change: 1 addition & 0 deletions src/get_interpreter_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"interpreter": platform.python_implementation().lower(),
"ext_suffix": ext_suffix,
"abi_tag": (sysconfig.get_config_var("SOABI") or "-").split("-")[1] or None,
"platform": sysconfig.get_platform(),
# This one isn't technically necessary, but still very useful for sanity checks
"system": platform.system().lower(),
# We need this one for windows abi3 builds
Expand Down
40 changes: 36 additions & 4 deletions src/python_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ struct IntepreterMetadataMessage {
abiflags: Option<String>,
interpreter: String,
ext_suffix: Option<String>,
// comes from `sysconfig.get_platform()`
platform: String,
// comes from `platform.system()`
system: String,
abi_tag: Option<String>,
base_prefix: String,
Expand Down Expand Up @@ -287,6 +290,10 @@ pub struct PythonInterpreter {
pub abi_tag: Option<String>,
/// We need this value for windows abi3 linking
pub libs_dir: PathBuf,
/// Comes from `sysconfig.get_platform()`
///
/// Note that this can be `None` when cross compiling
pub platform: Option<String>,
}

/// Returns the abiflags that are assembled through the message, with some
Expand Down Expand Up @@ -350,9 +357,17 @@ impl PythonInterpreter {
///
/// If abi3 is true, cpython wheels use the generic abi3 with the given version as minimum
pub fn get_tag(&self, platform_tag: PlatformTag, universal2: bool) -> String {
let platform = if self.target.is_windows() {
// Restrict `sysconfig.get_platform()` usage to Windows only for now
// so we don't need to deal with macOS deployment target
self.platform
.clone()
.unwrap_or_else(|| self.target.get_platform_tag(platform_tag, universal2))
} else {
self.target.get_platform_tag(platform_tag, universal2)
};
match self.interpreter_kind {
InterpreterKind::CPython => {
let platform = self.target.get_platform_tag(platform_tag, universal2);
if self.target.is_unix() {
format!(
"cp{major}{minor}-cp{major}{minor}{abiflags}-{platform}",
Expand All @@ -372,7 +387,6 @@ impl PythonInterpreter {
}
}
InterpreterKind::PyPy => {
let platform = self.target.get_platform_tag(platform_tag, universal2);
// pypy uses its version as part of the ABI, e.g.
// pypy 3.7 7.3 => numpy-1.20.1-pp37-pypy37_pp73-manylinux2010_x86_64.whl
format!(
Expand Down Expand Up @@ -471,8 +485,8 @@ impl PythonInterpreter {
match message.interpreter.as_str() {
"cpython" => interpreter = InterpreterKind::CPython,
"pypy" => interpreter = InterpreterKind::PyPy,
_ => {
bail!("Invalid interpreter");
other => {
bail!("Unsupported interpreter {}", other);
}
};

Expand All @@ -481,6 +495,23 @@ impl PythonInterpreter {
executable.as_ref().display()
))?;

let platform = if message.platform.starts_with("macosx") {
// macOS contains system version in platform name so need special handle
let mut parts = message.platform.splitn(3, '-');
let err_ctx = "Invalid platform string from `sysconfig.get_platform()`";
let prefix = parts.next().context(err_ctx)?;
let base_version = parts.next().context(err_ctx)?;
let suffix = parts.next().context(err_ctx)?;
let (major_ver, minor_ver) = base_version.split_once('.').context(err_ctx)?;
let major_ver: u64 = major_ver.parse()?;
let minor_ver = if major_ver > 10 { "0" } else { minor_ver };
// FIXME: We should also take MACOSX_DEPLOYMENT_TARGET env var into account
format!("{}_{}_{}_{}", prefix, major_ver, minor_ver, suffix)
} else {
message.platform
};
let platform = platform.to_lowercase().replace('-', "_").replace('.', "_");

Ok(Some(PythonInterpreter {
major: message.major,
minor: message.minor,
Expand All @@ -493,6 +524,7 @@ impl PythonInterpreter {
interpreter_kind: interpreter,
abi_tag: message.abi_tag,
libs_dir: PathBuf::from(message.base_prefix).join("libs"),
platform: Some(platform),
}))
}

Expand Down

0 comments on commit c5a122d

Please sign in to comment.