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

Add support for building on Windows mingw platforms #677

Merged
merged 2 commits into from
Nov 16, 2021
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
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
3 changes: 2 additions & 1 deletion src/get_interpreter_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"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
"platform": platform.system().lower(),
"system": platform.system().lower(),
# We need this one for windows abi3 builds
"base_prefix": sys.base_prefix,
}
Expand Down
54 changes: 43 additions & 11 deletions src/python_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ 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 All @@ -301,12 +308,12 @@ fn fun_with_abiflags(
bridge: &BridgeModel,
) -> Result<String> {
if bridge != &BridgeModel::Cffi
&& target.get_python_os() != message.platform
&& target.get_python_os() != message.system
&& !is_cross_compiling(target)?
{
bail!(
"sys.platform in python, {}, and the rust target, {:?}, don't match ಠ_ಠ",
message.platform,
"platform.system() in python, {}, and the rust target, {:?}, don't match ಠ_ಠ",
message.system,
target,
)
}
Expand All @@ -322,11 +329,11 @@ fn fun_with_abiflags(
if message.interpreter == "pypy" {
// pypy does not specify abi flags
Ok("".to_string())
} else if message.platform == "windows" {
if message.abiflags.is_some() {
bail!("A python 3 interpreter on windows does not define abiflags in its sysconfig ಠ_ಠ")
} else {
} else if message.system == "windows" {
if matches!(message.abiflags.as_deref(), Some("") | None) {
Ok("".to_string())
} else {
bail!("A python 3 interpreter on windows does not define abiflags in its sysconfig ಠ_ಠ")
}
} else if let Some(ref abiflags) = message.abiflags {
if message.minor >= 8 {
Expand All @@ -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
Loading