Skip to content
Draft
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
2 changes: 2 additions & 0 deletions guide/src/distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ Then pass `--zig` to maturin `build` or `publish` commands to use it, for exampl
maturin build --release --target aarch64-unknown-linux-gnu --zig
```

For macOS targets, maturin passes the deployment target to zig. It uses the same version as the wheel tag: `MACOSX_DEPLOYMENT_TARGET`, or `macos-deployment-target` from `pyproject.toml`, or rustc's default.

### Cross-compile to Windows

Pyo3 0.16.5 added an experimental feature `generate-import-lib` enables the user to cross compile
Expand Down
46 changes: 26 additions & 20 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ fn create_build_command(
}
_ => target_triple.to_string(),
}
} else if target.is_macos() {
let (deployment_target, _) = macos_deployment_target(context, target_triple);
format!("{target_triple}.{deployment_target}")
} else {
target_triple.to_string()
};
Expand Down Expand Up @@ -963,32 +966,35 @@ fn configure_pyo3_env(
&& target.is_macos()
&& env::var_os("MACOSX_DEPLOYMENT_TARGET").is_none()
{
let target_config = context
.project
.pyproject_toml
.as_ref()
.and_then(|x| x.target_config(target_triple));
let deployment_target = if let Some(deployment_target) = target_config
.as_ref()
.and_then(|config| config.macos_deployment_target.as_ref())
{
eprintln!(
"💻 Using `MACOSX_DEPLOYMENT_TARGET={deployment_target}` for {target_triple} by configuration"
);
deployment_target.clone()
} else {
let (major, minor) = rustc_macosx_target_version(target_triple);
eprintln!(
"💻 Using `MACOSX_DEPLOYMENT_TARGET={major}.{minor}` for {target_triple} by default"
);
format!("{major}.{minor}")
};
let (deployment_target, source) = macos_deployment_target(context, target_triple);
eprintln!(
"💻 Using `MACOSX_DEPLOYMENT_TARGET={deployment_target}` for {target_triple} by {source}"
);
build_command.env("MACOSX_DEPLOYMENT_TARGET", deployment_target);
}

Ok(())
}

fn macos_deployment_target(context: &BuildContext, target_triple: &str) -> (String, &'static str) {
if let Ok(deployment_target) = env::var("MACOSX_DEPLOYMENT_TARGET") {
return (deployment_target, "environment");
}
let target_config = context
.project
.pyproject_toml
.as_ref()
.and_then(|x| x.target_config(target_triple));
if let Some(deployment_target) = target_config
.as_ref()
.and_then(|config| config.macos_deployment_target.as_ref())
{
return (deployment_target.clone(), "configuration");
}
let (major, minor) = rustc_macosx_target_version(target_triple);
(format!("{major}.{minor}"), "default")
}

fn compile_target(
context: &BuildContext,
mut build_command: Command,
Expand Down
Loading