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

Use cargo rustc --crate-type cdylib on Rust nightly/dev channel #1020

Merged
merged 1 commit into from
Jul 20, 2022
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 @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add support for invoking with `python3 -m maturin` in [#1008](https://github.com/PyO3/maturin/pull/1008)
* Fix detection of optional dependencies when declaring `features` in `pyproject.toml` in [#1014](https://github.com/PyO3/maturin/pull/1014)
* Respect user specified Rust target in `maturin develop` in [#1016](https://github.com/PyO3/maturin/pull/1016)
* Use `cargo rustc --crate-type cdylib` on Rust nightly/dev channel in [#1020](https://github.com/PyO3/maturin/pull/1020)

## [0.13.0] - 2022-07-09

Expand Down
37 changes: 36 additions & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use std::str;
/// without `PYO3_NO_PYTHON` environment variable
const PYO3_ABI3_NO_PYTHON_VERSION: (u64, u64, u64) = (0, 16, 4);

/// crate types excluding `bin`, `cdylib` and `proc-macro`
const LIB_CRATE_TYPES: [&str; 4] = ["lib", "dylib", "rlib", "staticlib"];

/// Builds the rust crate into a native module (i.e. an .so or .dll) for a
/// specific python version. Returns a mapping from crate type (e.g. cdylib)
/// to artifact location.
Expand All @@ -23,14 +26,27 @@ pub fn compile(
bindings_crate: &BridgeModel,
) -> Result<Vec<HashMap<String, PathBuf>>> {
let root_pkg = context.cargo_metadata.root_package().unwrap();
let targets: Vec<_> = root_pkg
let mut targets: Vec<_> = root_pkg
.targets
.iter()
.filter(|target| match bindings_crate {
BridgeModel::Bin(_) => target.kind.contains(&"bin".to_string()),
_ => target.kind.contains(&"cdylib".to_string()),
})
.collect();
if targets.is_empty() && !bindings_crate.is_bin() {
// No `crate-type = ["cdylib"]` in `Cargo.toml`
// Let's try compile one of the target with `--crate-type cdylib`
let lib_target = root_pkg.targets.iter().find(|target| {
target
.kind
.iter()
.any(|k| LIB_CRATE_TYPES.contains(&k.as_str()))
});
if let Some(target) = lib_target {
targets.push(target);
}
}
if context.target.is_macos() && context.universal2 {
compile_universal2(context, python_interpreter, bindings_crate, &targets)
} else {
Expand Down Expand Up @@ -148,6 +164,25 @@ fn compile_target(
cargo_rustc.release = true;
}

// Add `--crate-type cdylib` if available
if binding_target
.kind
.iter()
.any(|k| LIB_CRATE_TYPES.contains(&k.as_str()))
{
if let Ok(channel) = rustc_version::version_meta().map(|x| x.channel) {
if matches!(
channel,
rustc_version::Channel::Nightly | rustc_version::Channel::Dev
) {
cargo_rustc
.unstable_flags
.push("unstable-options".to_string());
cargo_rustc.crate_type = vec!["cdylib".to_string()];
}
}
}

let mut rust_flags = env::var_os("RUSTFLAGS");

// We need to pass --bin / --lib
Expand Down