Skip to content

Commit

Permalink
Try #1275:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Nov 17, 2022
2 parents 2618bb8 + c3d0fc5 commit 77cdeb7
Show file tree
Hide file tree
Showing 32 changed files with 1,686 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .config/nextest.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[profile.default]
# Terminate slow tests after 5 minutes
slow-timeout = { period = "60s", terminate-after = 5 }

[[profile.default.overrides]]
# See https://nexte.st/book/threads-required.html
filter = 'test(/_uniffi_/)'
threads-required = "num-cpus"
135 changes: 132 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cargo-options = "0.5.2"
cargo-zigbuild = "0.14.1"
cargo-xwin = { version = "0.12.2", default-features = false }
cbindgen = { version = "0.24.2", default-features = false }
uniffi_bindgen = "0.21.0"
flate2 = "1.0.18"
goblin = "0.6.0"
human-panic = { version = "1.0.3", optional = true }
Expand Down
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add support for 32-bit x86 FreeBSD target in [#1254](https://github.com/PyO3/maturin/pull/1254)
* Add `[tool.maturin.include]` and `[tool.maturin.exclude]` and deprecate `[tool.maturin.sdist-include]` [#1255](https://github.com/PyO3/maturin/pull/1255)
* Ignore sdist tar ball instead of error out in [#1259](https://github.com/PyO3/maturin/pull/1259)
* Add support for [`uniffi`](https://github.com/mozilla/uniffi-rs) bindings in [#1275](https://github.com/PyO3/maturin/pull/1275)

## [0.13.7] - 2022-10-29

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ _formerly pyo3-pack_
[![Maturin User Guide](https://img.shields.io/badge/user-guide-brightgreen?logo=readthedocs&style=flat-square)](https://maturin.rs)
[![Chat on Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg?logo=gitter&style=flat-square)](https://gitter.im/PyO3/Lobby)

Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages.
Build and publish crates with pyo3, rust-cpython, cffi and uniffi bindings as well as rust binaries as python packages.

This project is meant as a zero configuration replacement for [setuptools-rust](https://github.com/PyO3/setuptools-rust) and [milksnake](https://github.com/getsentry/milksnake).
It supports building wheels for python 3.5+ on windows, linux, mac and freebsd, can upload them to [pypi](https://pypi.org/) and has basic pypy support.
Expand Down
69 changes: 64 additions & 5 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::build_options::CargoOptions;
use crate::compile::warn_missing_py_init;
use crate::module_writer::{
add_data, write_bin, write_bindings_module, write_cffi_module, write_python_part,
write_wasm_launcher, WheelWriter,
write_uniffi_module, write_wasm_launcher, WheelWriter,
};
use crate::project_layout::ProjectLayout;
use crate::python_interpreter::InterpreterKind;
Expand All @@ -29,8 +29,6 @@ use std::path::{Path, PathBuf};
/// The way the rust code is used in the wheel
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BridgeModel {
/// A native module with c bindings, i.e. `#[no_mangle] extern "C" <some item>`
Cffi,
/// A rust binary to be shipped a python package
/// The String is the name of the bindings
/// providing crate, e.g. pyo3, the number is the minimum minor python version
Expand All @@ -42,6 +40,10 @@ pub enum BridgeModel {
/// for all cpython versions (pypy still needs multiple versions).
/// The numbers are the minimum major and minor version
BindingsAbi3(u8, u8),
/// A native module with c bindings, i.e. `#[no_mangle] extern "C" <some item>`
Cffi,
/// A native module generated from uniffi
UniFfi,
}

impl BridgeModel {
Expand Down Expand Up @@ -71,11 +73,12 @@ impl BridgeModel {
impl Display for BridgeModel {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
BridgeModel::Cffi => write!(f, "cffi"),
BridgeModel::Bin(Some((name, _))) => write!(f, "{} bin", name),
BridgeModel::Bin(None) => write!(f, "bin"),
BridgeModel::Bindings(name, _) => write!(f, "{}", name),
BridgeModel::BindingsAbi3(..) => write!(f, "pyo3"),
BridgeModel::Cffi => write!(f, "cffi"),
BridgeModel::UniFfi => write!(f, "uniffi"),
}
}
}
Expand Down Expand Up @@ -199,7 +202,6 @@ impl BuildContext {
.context("Failed to create the target directory for the wheels")?;

let wheels = match &self.bridge {
BridgeModel::Cffi => self.build_cffi_wheel()?,
BridgeModel::Bin(None) => self.build_bin_wheel(None)?,
BridgeModel::Bin(Some(..)) => self.build_bin_wheels(&self.interpreter)?,
BridgeModel::Bindings(..) => self.build_binding_wheels(&self.interpreter)?,
Expand Down Expand Up @@ -240,6 +242,8 @@ impl BuildContext {
}
built_wheels
}
BridgeModel::Cffi => self.build_cffi_wheel()?,
BridgeModel::UniFfi => self.build_uniffi_wheel()?,
};

Ok(wheels)
Expand Down Expand Up @@ -751,6 +755,61 @@ impl BuildContext {
Ok(wheels)
}

fn write_uniffi_wheel(
&self,
artifact: BuildArtifact,
platform_tags: &[PlatformTag],
ext_libs: Vec<Library>,
) -> Result<BuiltWheelMetadata> {
let (tag, tags) = self
.target
.get_universal_tags(platform_tags, self.universal2)?;

let mut writer = WheelWriter::new(
&tag,
&self.out,
&self.metadata21,
&tags,
self.excludes(Format::Wheel)?,
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

write_uniffi_module(
&mut writer,
&self.project_layout,
self.manifest_path.parent().unwrap(),
&self.target_dir,
&self.module_name,
&artifact.path,
self.target.target_os(),
self.editable,
self.pyproject_toml.as_ref(),
)?;

self.add_pth(&mut writer)?;
add_data(&mut writer, self.project_layout.data.as_deref())?;
let wheel_path = writer.finish()?;
Ok((wheel_path, "py3".to_string()))
}

/// Builds a wheel with uniffi bindings
pub fn build_uniffi_wheel(&self) -> Result<Vec<BuiltWheelMetadata>> {
let mut wheels = Vec::new();
let artifact = self.compile_cdylib(None, None)?;
let (policy, external_libs) = self.auditwheel(&artifact, &self.platform_tag, None)?;
let platform_tags = if self.platform_tag.is_empty() {
vec![policy.platform_tag()]
} else {
self.platform_tag.clone()
};
let (wheel_path, tag) = self.write_uniffi_wheel(artifact, &platform_tags, external_libs)?;

println!("📦 Built wheel to {}", wheel_path.display());
wheels.push((wheel_path, tag));

Ok(wheels)
}

fn write_bin_wheel(
&self,
python_interpreter: Option<&PythonInterpreter>,
Expand Down
Loading

0 comments on commit 77cdeb7

Please sign in to comment.