Skip to content
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
7 changes: 4 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- js
- jupyter
- rust
- rust-capi
- rustjswasm
- cppjswasm
- uitk-svelte
Expand Down Expand Up @@ -100,15 +101,15 @@ jobs:

- name: Setup Rust
uses: actions-ext/rust/setup@af6343171cd1a86c4183da96de7eb96e15599662
if: matrix.template == 'rust' || matrix.template == 'rustjswasm'
if: matrix.template == 'rust' || matrix.template == 'rust-capi' || matrix.template == 'rustjswasm'

- name: Install JavaScript dependencies
run: pnpm install
if: matrix.template == 'js' || matrix.template == 'jupyter' || matrix.template == 'cppjswasm' || matrix.template == 'rustjswasm'

- name: Build Rust fixture
run: cargo build
if: matrix.template == 'rust' || matrix.template == 'rustjswasm'
if: matrix.template == 'rust' || matrix.template == 'rust-capi' || matrix.template == 'rustjswasm'

- name: Generate template
run: make gen-${{ matrix.template }}
Expand All @@ -133,4 +134,4 @@ jobs:
run: python .github/scripts/check_sdist_artifacts.py ../python-template-${{ matrix.template }}
env:
HATCH_BUILD_NO_HOOKS: '1'
if: matrix.template == 'cpp' || matrix.template == 'cppjswasm' || matrix.template == 'rust' || matrix.template == 'rustjswasm'
if: matrix.template == 'cpp' || matrix.template == 'cppjswasm' || matrix.template == 'rust' || matrix.template == 'rust-capi' || matrix.template == 'rustjswasm'
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ update-action-pins: ## re-pin actions-ext references to each action repo's curr
check-action-pins: ## check whether any actions-ext pin is out of date
python3 .github/scripts/update_action_pins.py --check

.PHONY: gen-python gen-cpp gen-js gen-jupyter gen-rust gen-rustjswasm gen-cppjswasm gen-uitk-svelte gen-uitk-webawesome gen-site-react gen-site-sveltekit gen-site-webawesome
.PHONY: gen-python gen-cpp gen-js gen-jupyter gen-rust gen-rust-capi gen-rustjswasm gen-cppjswasm gen-uitk-svelte gen-uitk-webawesome gen-site-react gen-site-sveltekit gen-site-webawesome
gen-python: ## regenerate the python template from scratch
mkdir -p ../python-template && cd ../python-template && rm -rf ./* && rm -rf .copier-answers.yaml .gitignore .github .gitattributes
copier copy -w . ../python-template --data-file examples/python.yaml
Expand Down Expand Up @@ -67,6 +67,11 @@ gen-rust: ## regenerate the rust template from scratch
copier copy -w . ../python-template-rust --data-file examples/rust.yaml
cd ../python-template-rust && $(SED) 's#_src_path: .#_src_path: https://github.com/python-project-templates/base.git#g' ./.copier-answers.yaml

gen-rust-capi: ## regenerate the rust template with a C ABI library from scratch
mkdir -p ../python-template-rust-capi && cd ../python-template-rust-capi && rm -rf ./* && rm -rf .copier-answers.yaml .gitignore .github .gitattributes
copier copy -w . ../python-template-rust-capi --data-file examples/rust-capi.yaml
cd ../python-template-rust-capi && $(SED) 's#_src_path: .#_src_path: https://github.com/python-project-templates/base.git#g' ./.copier-answers.yaml

gen-rustjswasm: ## regenerate the rustjswasm template from scratch
mkdir -p ../python-template-rustjswasm && cd ../python-template-rustjswasm && rm -rf ./* && rm -rf .copier-answers.yaml .gitignore .github .gitattributes
copier copy -w . ../python-template-rustjswasm --data-file examples/rustjswasm.yaml
Expand Down Expand Up @@ -97,7 +102,7 @@ gen-site-webawesome: ## regenerate the Web Awesome site template from scratch
mkdir -p ../javascript-template-site-webawesome && cd ../javascript-template-site-webawesome && rm -rf ./* && rm -rf .copier-answers.yaml .github .gitignore .prettierignore .prettierrc .oxfmtrc.json .oxlintrc.json
copier copy -w . ../javascript-template-site-webawesome --data-file examples/site-webawesome.yaml

.PHONY: test-python test-cpp test-js test-jupyter test-rust test-rustjswasm test-cppjswasm test-uitk-svelte test-uitk-webawesome test-site-react test-site-sveltekit test-site-webawesome
.PHONY: test-python test-cpp test-js test-jupyter test-rust test-rust-capi test-rustjswasm test-cppjswasm test-uitk-svelte test-uitk-webawesome test-site-react test-site-sveltekit test-site-webawesome
test-python:
cd ../python-template && git config --global user.name "github-actions" && git config --global user.email "41898282+github-actions[bot]@users.noreply.github.c@example.com" && git init && git add . && git commit -m "initial commit"
cd ../python-template && make develop
Expand Down Expand Up @@ -136,6 +141,14 @@ test-rust:
cd ../python-template-rust && make checks
cd ../python-template-rust && make test

test-rust-capi:
cd ../python-template-rust-capi && git config --global user.name "github-actions" && git config --global user.email "41898282+github-actions[bot]@users.noreply.github.c@example.com" && git init && git add . && git commit -m "initial commit"
cd ../python-template-rust-capi && make develop
cd ../python-template-rust-capi && git add Cargo.lock rust/Cargo.lock rust/capi/include && git commit -m "lockfile and header"
cd ../python-template-rust-capi && make lint
cd ../python-template-rust-capi && make checks
cd ../python-template-rust-capi && make test

test-rustjswasm:
cd ../python-template-rustjswasm && git config --global user.name "github-actions" && git config --global user.email "41898282+github-actions[bot]@users.noreply.github.c@example.com" && git init && git add . && git commit -m "initial commit"
cd ../python-template-rustjswasm && make develop
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ One selector chooses a template:
- `Python / Cython` generates a Python package with compiled `.py` and `.pyx` modules from `python/cython/`.
- `Python / C++` generates a Python/C++ package with pybind11 bindings or standalone C++ code from `python/cpp/`.
- `Python / JavaScript` generates a Python package with a self-contained JavaScript frontend from `python/js/`.
- `Python / Rust` generates a Python package with Rust bindings via PyO3 from `python/rust/`.
- `Python / Rust` generates a Python package with Rust bindings via PyO3 from `python/rust/`. With `add_c_abi`, it also builds a C ABI library that the extension links against and wheels ship with its header.
- `Python / Jupyter Widget` generates a Python package with a Jupyter extension from `python/jupyter/`.
- `Python / Rust + JavaScript + WebAssembly` generates a Rust package with Python and WebAssembly bindings from `python/rustjswasm/`.
- `Python / C++ + JavaScript + WebAssembly` generates a C++ package with Python and WebAssembly bindings from `python/cppjswasm/`.
Expand Down
6 changes: 6 additions & 0 deletions copier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ add_pyodide:
help: Build and test Pyodide wheels
when: "{{ add_extension in ['cpp', 'rust', 'cppjswasm', 'rustjswasm'] }}"

add_c_abi:
type: bool
default: false
help: Build a C ABI library that the Python extension links against
when: "{{ add_extension == 'rust' }}"

python_version_primary:
type: str
default: 3.11
Expand Down
12 changes: 12 additions & 0 deletions examples/rust-capi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
add_docs: true
add_pyodide: false
add_wiki: true
add_c_abi: true
add_extension: rust
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
project_description: A Rust-Python project template with a C ABI library
project_name: python template rust capi
python_version_primary: '3.11'
team: Python Project Template Authors
1 change: 1 addition & 0 deletions examples/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
add_docs: true
add_pyodide: false
add_wiki: true
add_c_abi: false
add_extension: rust
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
5 changes: 4 additions & 1 deletion python/rust/.gitignore.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ vcpkg_installed/
# Rust
target
target-capi

{% if add_c_abi %}
# Copied from rust/capi/include by hatch-rs
{{ module }}/include/
{% endif %}
# Distribution / packaging
.Python
build/
Expand Down
9 changes: 7 additions & 2 deletions python/rust/Cargo.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ path = "rust/python/lib.rs"
crate-type = ["cdylib"]

[dependencies]
{{ module }} = { path = "./rust", version = "*" }
{{ module }} = { path = "./rust", version = "*"{% if add_c_abi %}, optional = true{% endif %} }
pyo3 = { version = "0.29.2", features = ["abi3", "extension-module", "multiple-pymethods"] }

{% if add_c_abi %}
[features]
default = ["direct"]
capi = []
direct = ["dep:{{ module }}"]
{% endif %}
[profile.release]
panic = 'abort'
lto = true
3 changes: 2 additions & 1 deletion python/rust/Makefile.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ lint-py: ## run python linter with ruff

lint-rs: ## run rust linter
make -C rust lint

{% if add_c_abi %} git diff --exit-code -- rust/capi/include/{{ module }}.h
{% endif %}
lint-docs: ## lint docs with mdformat and codespell
python -m mdformat --check README.md $(wildcard docs/src) {% if add_wiki %}docs/wiki/{% endif %}
python -m codespell_lib README.md $(wildcard docs/src) {% if add_wiki %}docs/wiki/{% endif %}
Expand Down
16 changes: 16 additions & 0 deletions python/rust/README.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
[![PyPI](https://img.shields.io/pypi/v/{{ project_name_formatted }}.svg)](https://pypi.python.org/pypi/{{ project_name_formatted }})

## Overview
{%- if add_c_abi %}

## C ABI

`rust/capi` builds `lib{{ module }}_capi`, a C ABI over the Rust crate with the
header `rust/capi/include/{{ module }}.h`. Wheels ship the library under
`{{ module }}/lib` and the header under `{{ module }}/include/{{ module }}`, and the
Python extension links against it rather than compiling the crate in. Other
projects build against the installed wheel with `{{ module }}.get_include()`,
`get_library_dirs()`, `get_libraries()`, and `get_prefix()`.

`HATCH_RS_LIBRARY_SOURCE` chooses where the library comes from at build time:
`bundled` (the default, for wheels), `external` (a prebuilt library found
through `HATCH_RS_LIBRARY_ROOT`, pkg-config, or the conda prefix), or `direct`
(compile the crate in).
{%- endif %}

> [!NOTE]
> This library was generated using [copier](https://copier.readthedocs.io/en/stable/) from the [Base Python Project Template repository](https://github.com/python-project-templates/base).
58 changes: 53 additions & 5 deletions python/rust/pyproject.toml.jinja
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build-system]
requires = [
"hatchling",
"hatch-rs>={% if add_pyodide %}0.3.0{% else %}0.1.4{% endif %}",
"hatch-rs>={% if add_c_abi %}0.5.0{% elif add_pyodide %}0.3.0{% else %}0.1.4{% endif %}",
]
build-backend = "hatchling.build"

Expand Down Expand Up @@ -46,7 +46,7 @@ develop = [
"check-dist",
"cibuildwheel",
"codespell",
"hatch-rs{% if add_pyodide %}>=0.3.0{% endif %}",
"hatch-rs{% if add_c_abi %}>=0.5.0{% elif add_pyodide %}>=0.3.0{% endif %}",
"hatchling",
"mdformat",
"mdformat-myst",
Expand Down Expand Up @@ -96,7 +96,12 @@ replace = 'version = "{new_version}"'
filename = "rust/Cargo.toml"
search = 'version = "{current_version}"'
replace = 'version = "{new_version}"'

{% if add_c_abi %}
[[tool.bumpversion.files]]
filename = "rust/capi/Cargo.toml"
search = 'version = "{current_version}"'
replace = 'version = "{new_version}"'
{% endif %}
[tool.cibuildwheel]
build = "cp{{ python_version_primary.replace('.', '') }}-*"
test-command = "pytest -vvv {project}/{{ module }}/tests"
Expand All @@ -112,11 +117,16 @@ rustup show
before-build = "cargo clean --manifest-path {package}/Cargo.toml && rm -f {package}/{{ module }}/*.so"
environment = {PATH="$HOME/.cargo/bin:$PATH", CARGO_TERM_COLOR="always"}
skip = "*i686* *musllinux*"

{% if add_c_abi -%}
# Keep the bundled C ABI library where the extension's rpath expects it.
repair-wheel-command = "auditwheel repair --exclude 'lib{{ module }}_capi.so*' -w {dest_dir} {wheel}"
{% endif %}
[tool.cibuildwheel.macos]
environment = {PATH="$HOME/.cargo/bin:$PATH", CARGO_TERM_COLOR="always", MACOS_DEPLOYMENT_TARGET=11.0}
archs = "arm64"

{% if add_c_abi -%}
repair-wheel-command = "delocate-wheel --require-archs {delocate_archs} --exclude {{ module }}_capi -w {dest_dir} -v {wheel}"
{% endif %}
[tool.cibuildwheel.windows]
environment = {PATH="$UserProfile\\.cargo\bin;$PATH", CARGO_TERM_COLOR="always"}
skip = "*win32 *arm64"
Expand All @@ -127,6 +137,10 @@ test-command = "python -m pytest -vvv --pyargs {{ module }}.tests"
test-extras = []
test-requires = ["pytest"]
xbuild-tools = ["cargo", "rustc", "rustup"]
{% if add_c_abi -%}
# Emscripten has no shared libraries to split; compile the library in.
environment = {HATCH_RS_LIBRARY_SOURCE="direct"}
{% endif -%}
{% endif %}
[tool.coverage.run]
branch = true
Expand All @@ -151,7 +165,37 @@ verbose = true
abi3 = true
module = "{{ module }}"
path = "."
{% if add_c_abi -%}
target-dir = "project"

# lib{{ module }}_capi: the C ABI library, shipped under {{ module }}/lib with its header.
[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "{{ module }}_capi"
manifest = "rust/capi/Cargo.toml"
destination = "{{ module }}/lib/{shared_library}"
expected-headers = ["rust/capi/include/{{ module }}.h"]
header-symbol-prefix = "{{ module }}_"
expected-abi-strings = ["{{ module | upper }}_ABI_VERSION"]
include-import-lib = true

[[tool.hatch.build.hooks.hatch-rs.artifacts.outputs]]
source = "rust/capi/include/{{ module }}.h"
destination = "{{ module }}/include/{{ module }}/{{ module }}.h"
install-scheme = "package"

# The PyO3 extension, linked against the library by default (library-source = bundled).
[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "{{ module }}"
manifest = "Cargo.toml"
destination = "{{ module }}/{python_extension_name}"
links = ["{{ module }}_capi"]

[tool.hatch.build.hooks.hatch-rs.artifacts.library-source-features]
bundled = ["capi"]
external = ["capi"]
static = ["direct"]
direct = ["direct"]
{% endif %}
[tool.hatch.build.targets.sdist]
packages = [
"{{ module }}",
Expand All @@ -173,6 +217,10 @@ artifacts = [
"{{ module }}/*.dll",
"{{ module }}/*.dylib",
"{{ module }}/*.so",
{%- if add_c_abi %}
"{{ module }}/include/**/*.h",
"{{ module }}/lib/*",
{%- endif %}
]
packages = [
"{{ module }}",
Expand Down
8 changes: 7 additions & 1 deletion python/rust/rust/Cargo.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ path = "src/lib.rs"
crate-type = ["rlib"]

[dependencies]

{% if add_c_abi %}
[workspace]
members = [
".",
"capi",
]
{% endif %}
43 changes: 43 additions & 0 deletions python/rust/rust/python/example/mod.rs.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
use pyo3::prelude::*;

{% if add_c_abi -%}
// The resolution switch picks the route at build time: `capi` calls through
// lib{{ module }}_capi (bundled or external), `direct` compiles the Rust crate in.
#[cfg(feature = "capi")]
use crate::capi::Example as BaseExample;
#[cfg(feature = "direct")]
use {{ module }}::Example as BaseExample;

#[cfg(feature = "capi")]
fn stuff(example: &BaseExample) -> String {
example.stuff()
}

#[cfg(feature = "direct")]
fn stuff(example: &BaseExample) -> String {
example.stuff.clone()
}


#[pyclass]
pub struct Example {
pub example: BaseExample,
}

#[pymethods]
impl Example {
#[new]
fn py_new(value: String) -> PyResult<Self> {
Ok(Example {
example: BaseExample::new(value),
})
}

fn __str__(&self) -> PyResult<String> {
Ok(stuff(&self.example))
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Example<{}>", stuff(&self.example)))
}
}
{%- else -%}
use {{ module }}::Example as BaseExample;


Expand Down Expand Up @@ -30,3 +72,4 @@ impl Example {
Ok(format!("Example<{}>", self.example.stuff))
}
}
{%- endif %}
4 changes: 4 additions & 0 deletions python/rust/rust/python/lib.rs.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use pyo3::prelude::*;

{% if add_c_abi -%}
#[cfg(feature = "capi")]
mod capi;
{% endif -%}
mod example;

pub use example::Example;
Expand Down
Loading
Loading