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
23 changes: 23 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,26 @@ jobs:
env:
HATCH_BUILD_NO_HOOKS: '1'
if: matrix.template == 'cpp' || matrix.template == 'cpp-capi' || matrix.template == 'cppjswasm' || matrix.template == 'cppjswasm-capi' || matrix.template == 'rust' || matrix.template == 'rust-capi' || matrix.template == 'rustjswasm'

- name: Test alternate native wheel ABI
if: startsWith(matrix.template, 'cpp') || startsWith(matrix.template, 'rust')
shell: bash
env:
TEMPLATE: ${{ matrix.template }}
SKIP_HATCH_JS: '1'
run: |
if [[ "$TEMPLATE" == cpp* ]]; then
abi3=true
tag=abi3
test_python=3.13
else
abi3=false
tag=cp312
test_python=3.12
fi
project="$RUNNER_TEMP/abi-test"
copier copy --defaults --vcs-ref=HEAD . "$project" --data-file "examples/$TEMPLATE.yaml" \
-d project_name=abi_test -d python_version_primary=3.12 -d use_abi3="$abi3" -d add_vcpkg=false
uv build --python 3.12 "$project"
wheel=("$project"/dist/*-cp312-"$tag"-*.whl)
uv run --isolated --no-project --python "$test_python" --with "${wheel[0]}" --with pytest pytest --pyargs abi_test
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ One selector chooses a template:

- `Python / Pure` generates a pure Python package from `python/pure/`.
- `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/`. With `add_c_abi`, it also builds a C ABI library that the extension links against and wheels ship with its header.
- `Python / C++` generates a Python/C++ package with Python bindings or standalone C++ code from `python/cpp/`. With `add_c_abi`, it also builds a C ABI library that the extension links against and wheels ship with its header.
- `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/`. 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/`.
Expand All @@ -29,3 +29,5 @@ One selector chooses a template:
- `JavaScript / Site / React` generates a React site with Vite, Vitest, and Playwright from `javascript/site-react/`.
- `JavaScript / Site / SvelteKit` generates a SvelteKit site with static or Node deployment from `javascript/site-sveltekit/`.
- `JavaScript / Site / Web Awesome` generates a Vite and TypeScript site using Web Awesome components from `javascript/site-webawesome/`.

For the Rust and C++ templates, including their WebAssembly variants, `use_abi3` selects the native Python wheel ABI. Rust defaults to ABI3. C++ defaults to version-specific pybind11 wheels and uses nanobind for ABI3 (Python 3.12+). ABI3 builds one wheel per platform at `python_version_primary`. Version-specific builds produce a wheel for each CPython minor from that version through 3.14. Pyodide builds target their own runtime.
18 changes: 17 additions & 1 deletion copier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ _answers_file: .copier-answers.yaml
_exclude:
- name-slug.jinja
- module-slug.jinja
- native-wheel-matrix.yaml.jinja

project_name:
type: str
Expand Down Expand Up @@ -87,21 +88,36 @@ add_c_abi:
help: Build a C ABI library that the Python extension links against
when: "{{ add_extension in ['cpp', 'cppjswasm', 'rust'] }}"

use_abi3:
type: bool
default: "{{ add_extension in ['rust', 'rustjswasm'] }}"
help: Build native wheels for the CPython stable ABI (C++ uses nanobind and requires Python 3.12+)
when: "{{ add_extension in ['cpp', 'cppjswasm', 'rust', 'rustjswasm'] }}"

python_version_primary:
type: str
default: 3.11
default: "{{ '3.12' if use_abi3 and add_extension in ['cpp', 'cppjswasm'] else '3.11' }}"
choices:
"3.10":
value: "3.10"
validator: "{% if use_abi3 and add_extension in ['cpp', 'cppjswasm'] %}C++ ABI3 requires Python 3.12 or newer{% endif %}"
"3.11":
value: "3.11"
validator: "{% if use_abi3 and add_extension in ['cpp', 'cppjswasm'] %}C++ ABI3 requires Python 3.12 or newer{% endif %}"
"3.12":
value: "3.12"
"3.13":
value: "3.13"
help: Primary version of Python to use in CI/CD
when: "{{ add_extension not in ['uitk-svelte', 'uitk-webawesome', 'site-react', 'site-sveltekit', 'site-webawesome'] }}"

native_python_versions:
type: yaml
default: >-
{{ [python_version_primary] if use_abi3 else
['3.10', '3.11', '3.12', '3.13', '3.14'] | select('ge', python_version_primary) | list }}
when: false

add_docs:
type: bool
default: false
Expand Down
1 change: 1 addition & 0 deletions examples/cpp-capi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_docs: true
add_pyodide: false
add_vcpkg: false
add_wiki: true
use_abi3: false
add_extension: cpp
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
1 change: 1 addition & 0 deletions examples/cpp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_docs: true
add_pyodide: false
add_vcpkg: true
add_wiki: true
use_abi3: false
add_extension: cpp
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
1 change: 1 addition & 0 deletions examples/cppjswasm-capi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_docs: true
add_pyodide: false
add_vcpkg: false
add_wiki: true
use_abi3: false
add_extension: cppjswasm
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
1 change: 1 addition & 0 deletions examples/cppjswasm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_docs: true
add_pyodide: false
add_vcpkg: true
add_wiki: true
use_abi3: false
add_extension: cppjswasm
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
1 change: 1 addition & 0 deletions examples/rust-capi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_docs: true
add_pyodide: false
add_wiki: true
add_c_abi: true
use_abi3: true
add_extension: rust
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
1 change: 1 addition & 0 deletions examples/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_docs: true
add_pyodide: false
add_wiki: true
add_c_abi: false
use_abi3: true
add_extension: rust
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
1 change: 1 addition & 0 deletions examples/rustjswasm.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
use_abi3: true
add_extension: rustjswasm
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
Expand Down
31 changes: 31 additions & 0 deletions native-wheel-matrix.yaml.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
strategy:
matrix:
platform:
- name: ubuntu-24.04
os: ubuntu-24.04
- name: ubuntu-24.04-arm
os: ubuntu-24.04-arm
- name: macos-26
os: macos-26
- name: windows-2025
os: windows-2025
python:
{%- for version in native_python_versions %}
- version: "{{ version }}"
cibuildwheel: "cp{{ version.replace('.', '') }}"
primary: {{ (version == python_version_primary) | lower }}
{%- endfor %}
include:
- target: native
dist_path: dist
{%- if add_pyodide %}
- platform:
name: pyodide
os: ubuntu-24.04
python:
version: "3.14"
cibuildwheel: "cp314"
primary: false
target: pyodide
dist_path: dist/pyodide
{%- endif %}
54 changes: 8 additions & 46 deletions python/cpp/.github/workflows/build.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,7 @@ jobs:
name: build ({% raw %}${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %})
runs-on: {% raw %}${{ matrix.platform.os }}{% endraw %}

strategy:
matrix:
platform:
- name: ubuntu-24.04
os: ubuntu-24.04
- name: ubuntu-24.04-arm
os: ubuntu-24.04-arm
- name: macos-26
os: macos-26
- name: windows-2025
os: windows-2025
python:
{% if python_version_primary == "3.10" -%}
- version: "3.10"
cibuildwheel: "cp310"
primary: true
{% endif -%}
{% if python_version_primary in ["3.10", "3.11"] -%}
- version: "3.11"
cibuildwheel: "cp311"
primary: {{ (python_version_primary == "3.11") | lower }}
{% endif -%}
{% if python_version_primary in ["3.10", "3.11", "3.12"] -%}
- version: "3.12"
cibuildwheel: "cp312"
primary: {{ (python_version_primary == "3.12") | lower }}
{% endif -%}
- version: "3.13"
cibuildwheel: "cp313"
primary: {{ (python_version_primary == "3.13") | lower }}
include:
- target: native
dist_path: dist
{% if add_pyodide -%}
- platform:
name: pyodide
os: ubuntu-24.04
python:
version: "3.14"
cibuildwheel: "cp314"
primary: false
target: pyodide
dist_path: dist/pyodide
{% endif %}
{% include 'native-wheel-matrix.yaml.jinja' %}

steps:
- name: Checkout
Expand Down Expand Up @@ -138,7 +95,6 @@ jobs:
- name: Build distributions (Linux x86_64)
run: |
rm -rf dist
make dist-py-sdist
make dist-py-wheel
make dist-check
env:
Expand Down Expand Up @@ -175,6 +131,12 @@ jobs:
CIBW_BUILD: {% raw %}"${{ matrix.python.cibuildwheel }}-win_amd64"{% endraw %}
if: matrix.platform.name == 'windows-2025'

- name: Build source distributions
run: |
make dist-py-sdist
make dist-check
if: matrix.platform.name == 'ubuntu-24.04' && matrix.python.primary

- name: Test wheel
uses: actions-ext/python/test-wheel@d8fa97566b70553350d6745e8b0a62db2e3c9cae
with:
Expand All @@ -185,7 +147,7 @@ jobs:
uses: actions-ext/python/test-sdist@d8fa97566b70553350d6745e8b0a62db2e3c9cae
with:
module: {{module}}
if: matrix.platform.name == 'ubuntu-24.04'
if: matrix.platform.name == 'ubuntu-24.04' && matrix.python.primary

- name: Upload distributions
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
Expand Down
2 changes: 1 addition & 1 deletion python/cpp/README.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

`cpp/{{ project_name_formatted }}` builds `lib{{ module }}`, a C ABI over the C++ code, declared in
`{{ module }}/include/{{ module }}/{{ module }}.h`. Wheels ship the library under `{{ module }}/lib` and
the header under `{{ module }}/include/{{ module }}`, and the pybind11 extension in `cpp/python` links
the header under `{{ module }}/include/{{ module }}`, and the Python extension in `cpp/python` links
against the library. The tests compile a C program against the installed package. Other
projects build against the installed wheel with `{{ module }}.get_include()`,
`get_library_dirs()`, `get_libraries()`, and `get_prefix()`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{% if use_abi3 -%}
#include <nanobind/nanobind.h>
{% else -%}
#include <pybind11/pybind11.h>

{% endif %}
#include "{{ module }}/{{ module }}.h"

PYBIND11_MODULE(extension, m) {
m.doc() = "pybind11 example plugin";
{{ "NB_MODULE" if use_abi3 else "PYBIND11_MODULE" }}(extension, m) {
m.doc() = "{{ "nanobind" if use_abi3 else "pybind11" }} example plugin";

m.def("add", &{{ module }}_add, "A function that adds two numbers");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from hatchling.builders.hooks.plugin.interface import BuildHookInterface


class Abi3WheelHook(BuildHookInterface):
def initialize(self, version, build_data):
# hatch-cpp tags for the build interpreter and counts bundled C libraries as Python extensions.
_, _, platform = build_data["tag"].split("-")
if not platform.startswith("pyemscripten_"):
build_data["tag"] = f"cp{{ python_version_primary.replace('.', '') }}-abi3-{platform}"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
{% if use_abi3 -%}
#include <nanobind/nanobind.h>
{% else -%}
#include <pybind11/pybind11.h>
{% endif %}
namespace py = {{ "nanobind" if use_abi3 else "pybind11" }};

int add(int i, int j);

{{ "NB_MODULE" if use_abi3 else "PYBIND11_MODULE" }}(extension, m) {
m.doc() = "{{ "nanobind" if use_abi3 else "pybind11" }} example plugin";

m.def("add", &add, "A function that adds two numbers");
}
23 changes: 15 additions & 8 deletions python/cpp/pyproject.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
requires = [
"hatchling",
"hatch-cpp{% if add_c_abi %}>=0.6.0{% elif add_pyodide %}>=0.5.0{% endif %}",
"pybind11",
"{{ 'nanobind' if use_abi3 else 'pybind11' }}",
]
build-backend = "hatchling.build"

Expand Down Expand Up @@ -51,7 +51,7 @@ develop = [
"mdformat",
"mdformat-myst",
"mdformat-tables>=1",
"pybind11",
"{{ 'nanobind' if use_abi3 else 'pybind11' }}",
"pytest",
"pytest-cov",
"ruff",
Expand Down Expand Up @@ -89,7 +89,7 @@ search = "VERSION {current_version}"
replace = "VERSION {new_version}"
{% endif %}
[tool.cibuildwheel]
build = "{% if python_version_primary == '3.10' %}cp310-* {% endif %}{% if python_version_primary in ['3.10', '3.11'] %}cp311-* {% endif %}{% if python_version_primary in ['3.10', '3.11', '3.12'] %}cp312-* {% endif %}cp313-*"
build = "{% for version in native_python_versions %}cp{{ version.replace('.', '') }}-*{{ ' ' if not loop.last }}{% endfor %}"
test-command = "pytest -vvv {project}/{{ module }}/tests"
test-extras = "develop"

Expand Down Expand Up @@ -158,20 +158,27 @@ extra-compile-args-darwin = ["-fvisibility=hidden"]
expected-headers = ["{{ module }}/include/{{ module }}/{{ module }}.h"]
header-symbol-prefix = "{{ module }}_"
expected-abi-strings = ["{{ module | upper }}_ABI_VERSION 1"]
{%- endif %}

# The pybind11 extension, linked against the library by default (library-source = bundled).
[[tool.hatch.build.hooks.hatch-cpp.libraries]]
name = "{{ module }}/extension"
binding = "{{ 'nanobind' if use_abi3 else 'pybind11' }}"
{% if use_abi3 -%}
py-limited-api = "cp{{ python_version_primary.replace('.', '') }}"
{% endif -%}
{% if add_c_abi -%}
sources = ["cpp/python/extension.cpp"]
include-dirs = ["{{ module }}/include"]
binding = "pybind11"
depends = ["{{ module }}/lib/lib{{ module }}"]
{%- else %}
libraries = [
{name = "{{ module }}/extension", sources = ["cpp/{{ project_name_formatted }}/extension.cpp"], include-dirs = ["cpp"], binding="pybind11"}
]
sources = ["cpp/{{ project_name_formatted }}/extension.cpp"]
include-dirs = ["cpp"]
{%- endif %}

{% if use_abi3 -%}
[tool.hatch.build.targets.wheel.hooks.custom]
path = "cpp/abi3_wheel.py"
{% endif %}
[tool.hatch.build.targets.sdist]
exclude = ["*.dll", "*.dylib", "*.so", "*.pyd"]
packages = [
Expand Down
Loading
Loading