Skip to content

Commit

Permalink
Use cargo rustc --crate-type cdylib on Rust nightly/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jul 20, 2022
1 parent cdec6aa commit ff2ed51
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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
41 changes: 40 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,31 @@ 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()
.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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ff2ed51

Please sign in to comment.