From ff2ed51ca2078af63d934ed7c42316c671779dae Mon Sep 17 00:00:00 2001 From: messense Date: Mon, 11 Jul 2022 15:02:13 +0800 Subject: [PATCH] Use `cargo rustc --crate-type cdylib` on Rust nightly/dev --- Changelog.md | 1 + src/compile.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index efe8c34b8..3273ce58e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/src/compile.rs b/src/compile.rs index df766fc7d..413267bda 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -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. @@ -23,7 +26,7 @@ pub fn compile( bindings_crate: &BridgeModel, ) -> Result>> { 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 { @@ -31,6 +34,23 @@ pub fn compile( _ => 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() + .filter(|target| { + target + .kind + .iter() + .any(|k| LIB_CRATE_TYPES.contains(&k.as_str())) + }) + .next(); + 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 { @@ -148,6 +168,25 @@ fn compile_target( cargo_rustc.release = true; } + // Add `--crate-type cdylib` if avaiable + 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