diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 18d5016..119347b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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 diff --git a/README.md b/README.md index 4b9d3cf..35caee0 100644 --- a/README.md +++ b/README.md @@ -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/`. @@ -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. diff --git a/copier.yaml b/copier.yaml index 4284dcc..7d55635 100644 --- a/copier.yaml +++ b/copier.yaml @@ -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 @@ -87,14 +88,22 @@ 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": @@ -102,6 +111,13 @@ python_version_primary: 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 diff --git a/examples/cpp-capi.yaml b/examples/cpp-capi.yaml index 375c538..121c81e 100644 --- a/examples/cpp-capi.yaml +++ b/examples/cpp-capi.yaml @@ -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 diff --git a/examples/cpp.yaml b/examples/cpp.yaml index ec773f2..0eef71a 100644 --- a/examples/cpp.yaml +++ b/examples/cpp.yaml @@ -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 diff --git a/examples/cppjswasm-capi.yaml b/examples/cppjswasm-capi.yaml index 81060cd..5e0e4a1 100644 --- a/examples/cppjswasm-capi.yaml +++ b/examples/cppjswasm-capi.yaml @@ -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 diff --git a/examples/cppjswasm.yaml b/examples/cppjswasm.yaml index d59f072..56c0eb6 100644 --- a/examples/cppjswasm.yaml +++ b/examples/cppjswasm.yaml @@ -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 diff --git a/examples/rust-capi.yaml b/examples/rust-capi.yaml index 358f46d..425dd11 100644 --- a/examples/rust-capi.yaml +++ b/examples/rust-capi.yaml @@ -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 diff --git a/examples/rust.yaml b/examples/rust.yaml index 7954264..14bc873 100644 --- a/examples/rust.yaml +++ b/examples/rust.yaml @@ -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 diff --git a/examples/rustjswasm.yaml b/examples/rustjswasm.yaml index 4924a25..0834057 100644 --- a/examples/rustjswasm.yaml +++ b/examples/rustjswasm.yaml @@ -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 diff --git a/native-wheel-matrix.yaml.jinja b/native-wheel-matrix.yaml.jinja new file mode 100644 index 0000000..e30997f --- /dev/null +++ b/native-wheel-matrix.yaml.jinja @@ -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 %} diff --git a/python/cpp/.github/workflows/build.yaml.jinja b/python/cpp/.github/workflows/build.yaml.jinja index ddeb20d..a67377d 100644 --- a/python/cpp/.github/workflows/build.yaml.jinja +++ b/python/cpp/.github/workflows/build.yaml.jinja @@ -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 @@ -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: @@ -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: @@ -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 diff --git a/python/cpp/README.md.jinja b/python/cpp/README.md.jinja index 38818d1..404e808 100644 --- a/python/cpp/README.md.jinja +++ b/python/cpp/README.md.jinja @@ -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()`. diff --git a/python/cpp/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja b/python/cpp/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja index 69c5d70..d3a5eb0 100644 --- a/python/cpp/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja +++ b/python/cpp/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja @@ -1,9 +1,12 @@ +{% if use_abi3 -%} +#include +{% else -%} #include - +{% 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"); } diff --git a/python/cpp/cpp/{% if use_abi3 %}abi3_wheel.py{% endif %}.jinja b/python/cpp/cpp/{% if use_abi3 %}abi3_wheel.py{% endif %}.jinja new file mode 100644 index 0000000..a9e34b4 --- /dev/null +++ b/python/cpp/cpp/{% if use_abi3 %}abi3_wheel.py{% endif %}.jinja @@ -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}" diff --git a/python/cpp/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.hpp{% endif %} b/python/cpp/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.hpp{% endif %} deleted file mode 100644 index 2ae782a..0000000 --- a/python/cpp/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.hpp{% endif %} +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -namespace py = pybind11; - -int add(int i, int j); - -PYBIND11_MODULE(extension, m) { - m.doc() = "pybind11 example plugin"; - - m.def("add", &add, "A function that adds two numbers"); -} diff --git a/python/cpp/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.hpp{% endif %}.jinja b/python/cpp/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.hpp{% endif %}.jinja new file mode 100644 index 0000000..2481839 --- /dev/null +++ b/python/cpp/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.hpp{% endif %}.jinja @@ -0,0 +1,15 @@ +#pragma once +{% if use_abi3 -%} +#include +{% else -%} +#include +{% 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"); +} diff --git a/python/cpp/pyproject.toml.jinja b/python/cpp/pyproject.toml.jinja index 7b1e451..b5961b1 100644 --- a/python/cpp/pyproject.toml.jinja +++ b/python/cpp/pyproject.toml.jinja @@ -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" @@ -51,7 +51,7 @@ develop = [ "mdformat", "mdformat-myst", "mdformat-tables>=1", - "pybind11", + "{{ 'nanobind' if use_abi3 else 'pybind11' }}", "pytest", "pytest-cov", "ruff", @@ -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" @@ -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 = [ diff --git a/python/cppjswasm/.github/workflows/build.yaml.jinja b/python/cppjswasm/.github/workflows/build.yaml.jinja index 5396762..93c5c1f 100644 --- a/python/cppjswasm/.github/workflows/build.yaml.jinja +++ b/python/cppjswasm/.github/workflows/build.yaml.jinja @@ -25,39 +25,10 @@ permissions: jobs: build: - name: build ({% raw %}${{ matrix.name }}{% endraw %}) - runs-on: {% raw %}${{ matrix.os }}{% endraw %} - - strategy: - matrix: - include: - - name: ubuntu-24.04 - dist_path: dist - os: ubuntu-24.04 - python: "{{ python_version_primary }}" - target: native - - name: ubuntu-24.04-arm - dist_path: dist - os: ubuntu-24.04-arm - python: "{{ python_version_primary }}" - target: native - - name: macos-26 - dist_path: dist - os: macos-26 - python: "{{ python_version_primary }}" - target: native - - name: windows-2025 - dist_path: dist - os: windows-2025 - python: "{{ python_version_primary }}" - target: native - {% if add_pyodide -%} - - name: pyodide - dist_path: dist/pyodide - os: ubuntu-24.04 - python: "3.14" - target: pyodide - {% endif %} + name: build ({% raw %}${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %}) + runs-on: {% raw %}${{ matrix.platform.os }}{% endraw %} + +{% include 'native-wheel-matrix.yaml.jinja' %} steps: - name: Checkout @@ -68,7 +39,7 @@ jobs: - name: Setup Python uses: actions-ext/python/setup@d8fa97566b70553350d6745e8b0a62db2e3c9cae with: - version: {% raw %}${{ matrix.python }}{% endraw %} + version: {% raw %}${{ matrix.python.version }}{% endraw %} - name: Setup Node.js uses: actions-ext/node/setup@8bcebee998923fcc26f5854b4fd05ea378b58262 @@ -94,11 +65,11 @@ jobs: - name: Lint run: make lint - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Checks run: make checks - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Test run: make coverage @@ -111,15 +82,15 @@ jobs: - name: Upload test results (Python) uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: {% raw %}test-results-${{ matrix.name }}{% endraw %} + name: {% raw %}test-results-${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %} path: junit.xml - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Publish test results uses: EnricoMi/publish-unit-test-result-action@da5b7be67623cd59bbbd1a0ce4f022050a55605f # v2.26.0 with: files: '**/junit.xml' - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Upload coverage uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1 @@ -134,14 +105,13 @@ jobs: - name: Build distributions (Linux x86_64) run: | rm -rf dist - make dist-py-sdist make dist-py-wheel make dist-check env: - CIBW_BUILD: "cp{{ python_version_primary.replace('.', '') }}-manylinux*" + CIBW_BUILD: {% raw %}"${{ matrix.python.cibuildwheel }}-manylinux*"{% endraw %} CIBW_ARCHS_LINUX: native CIBW_BUILD_VERBOSITY: 3 - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Build distributions (Linux ARM64) run: | @@ -149,27 +119,33 @@ jobs: make dist-py-wheel make dist-check env: - CIBW_BUILD: "cp{{ python_version_primary.replace('.', '') }}-manylinux*" + CIBW_BUILD: {% raw %}"${{ matrix.python.cibuildwheel }}-manylinux*"{% endraw %} CIBW_ARCHS_LINUX: native CIBW_BUILD_VERBOSITY: 3 - if: matrix.name == 'ubuntu-24.04-arm' + if: matrix.platform.name == 'ubuntu-24.04-arm' - name: Build distributions (macOS) run: | rm -rf dist make dist-py-wheel env: - CIBW_BUILD: "cp{{ python_version_primary.replace('.', '') }}-macos*" + CIBW_BUILD: {% raw %}"${{ matrix.python.cibuildwheel }}-macos*"{% endraw %} CIBW_BUILD_VERBOSITY: 3 - if: matrix.name == 'macos-26' + if: matrix.platform.name == 'macos-26' - name: Build distributions (Windows) run: | if (Test-Path dist) { Remove-Item dist -Recurse -Force } make dist-py-wheel env: - CIBW_BUILD: "cp{{ python_version_primary.replace('.', '') }}-win_amd64" - if: matrix.name == 'windows-2025' + 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 @@ -181,10 +157,10 @@ jobs: uses: actions-ext/python/test-sdist@d8fa97566b70553350d6745e8b0a62db2e3c9cae with: module: {{module}} - if: matrix.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 with: - name: {% raw %}dist-${{ matrix.name }}{% endraw %} + name: {% raw %}dist-${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %} path: {% raw %}${{ matrix.dist_path }}{% endraw %} diff --git a/python/cppjswasm/README.md.jinja b/python/cppjswasm/README.md.jinja index 6cba8fb..ce4e001 100644 --- a/python/cppjswasm/README.md.jinja +++ b/python/cppjswasm/README.md.jinja @@ -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 JavaScript build compiles the C++ sources with Emscripten and does not use the C ABI. The tests compile a C program against the installed package. Other projects build against the installed wheel with `{{ module }}.get_include()`, diff --git a/python/cppjswasm/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja b/python/cppjswasm/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja index 01cd66d..c5dca94 100644 --- a/python/cppjswasm/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja +++ b/python/cppjswasm/cpp/{% if add_c_abi %}python{% endif %}/extension.cpp.jinja @@ -1,10 +1,15 @@ +{% if use_abi3 -%} +#include +#include +{% else -%} #include +{% endif -%} #include #include #include "{{ module }}/{{ module }}.h" -namespace py = pybind11; +namespace py = {{ "nanobind" if use_abi3 else "pybind11" }}; namespace { @@ -33,14 +38,14 @@ private: } -PYBIND11_MODULE(extension_cpp, m) { - m.doc() = "pybind11 extension module"; +{{ "NB_MODULE" if use_abi3 else "PYBIND11_MODULE" }}(extension_cpp, m) { + m.doc() = "{{ "nanobind" if use_abi3 else "pybind11" }} extension module"; m.def("add", &{{ module }}_add, "A function that adds two numbers"); py::class_(m, "Example") .def(py::init()) - .def_property("stuff", &Example::stuff, &Example::set_stuff) + .{{ "def_prop_rw" if use_abi3 else "def_property" }}("stuff", &Example::stuff, &Example::set_stuff) .def("__str__", [](const Example& e) { return e.stuff(); }) .def("__repr__", [](const Example& e) { return std::string("Example<") + e.stuff() + ">"; }); } diff --git a/python/cppjswasm/cpp/{% if use_abi3 %}abi3_wheel.py{% endif %}.jinja b/python/cppjswasm/cpp/{% if use_abi3 %}abi3_wheel.py{% endif %}.jinja new file mode 100644 index 0000000..a9e34b4 --- /dev/null +++ b/python/cppjswasm/cpp/{% if use_abi3 %}abi3_wheel.py{% endif %}.jinja @@ -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}" diff --git a/python/cppjswasm/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.cpp{% endif %}.jinja b/python/cppjswasm/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.cpp{% endif %}.jinja index 00a26be..bcfb52d 100644 --- a/python/cppjswasm/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.cpp{% endif %}.jinja +++ b/python/cppjswasm/cpp/{{project_name_formatted}}/{% if not add_c_abi %}extension.cpp{% endif %}.jinja @@ -1,18 +1,22 @@ +{% if use_abi3 -%} +#include +#include +{% else -%} #include #include - +{% endif %} #include "{{ project_name_formatted }}/example.hpp" -namespace py = pybind11; +namespace py = {{ "nanobind" if use_abi3 else "pybind11" }}; -PYBIND11_MODULE(extension_cpp, m) { - m.doc() = "pybind11 extension module"; +{{ "NB_MODULE" if use_abi3 else "PYBIND11_MODULE" }}(extension_cpp, m) { + m.doc() = "{{ "nanobind" if use_abi3 else "pybind11" }} extension module"; m.def("add", &add, "A function that adds two numbers"); py::class_(m, "Example") .def(py::init()) - .def_readwrite("stuff", &Example::stuff) + .{{ "def_rw" if use_abi3 else "def_readwrite" }}("stuff", &Example::stuff) .def("__str__", [](const Example& e) { return e.stuff; }) .def("__repr__", [](const Example& e) { return std::string("Example<") + e.stuff + ">"; }); } diff --git a/python/cppjswasm/pyproject.toml.jinja b/python/cppjswasm/pyproject.toml.jinja index 09c1b5f..669fb03 100644 --- a/python/cppjswasm/pyproject.toml.jinja +++ b/python/cppjswasm/pyproject.toml.jinja @@ -3,7 +3,7 @@ requires = [ "hatchling", "hatch-cpp{% if add_c_abi %}>=0.6.0{% elif add_pyodide %}>=0.5.0{% endif %}", "hatch-js", - "pybind11", + "{{ 'nanobind' if use_abi3 else 'pybind11' }}", ] build-backend = "hatchling.build" @@ -51,7 +51,7 @@ develop = [ "hatch-cpp{% if add_c_abi %}>=0.6.0{% elif add_pyodide %}>=0.5.0{% endif %}", "hatch-js", "hatchling", - "pybind11", + "{{ 'nanobind' if use_abi3 else 'pybind11' }}", "mdformat", "mdformat-myst", "mdformat-tables>=1", @@ -101,7 +101,7 @@ search = "VERSION {current_version}" replace = "VERSION {new_version}" {% endif %} [tool.cibuildwheel] -build = "cp{{ python_version_primary.replace('.', '') }}-*" +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" @@ -184,20 +184,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_cpp" +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_cpp", sources = ["cpp/{{ project_name_formatted }}/example.cpp", "cpp/{{ project_name_formatted }}/extension.cpp"], include-dirs = ["cpp"], binding="pybind11"} -] +sources = ["cpp/{{ project_name_formatted }}/example.cpp", "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 = [ diff --git a/python/rust/.github/workflows/build.yaml.jinja b/python/rust/.github/workflows/build.yaml.jinja index 2d0e40f..0c0fce7 100644 --- a/python/rust/.github/workflows/build.yaml.jinja +++ b/python/rust/.github/workflows/build.yaml.jinja @@ -25,39 +25,13 @@ permissions: jobs: build: - name: build ({% raw %}${{ matrix.name }}{% endraw %}) - runs-on: {% raw %}${{ matrix.os }}{% endraw %} - - strategy: - matrix: - include: - - name: ubuntu-24.04 - dist_path: dist - os: ubuntu-24.04 - python: "{{ python_version_primary }}" - target: native - - name: ubuntu-24.04-arm - dist_path: dist - os: ubuntu-24.04-arm - python: "{{ python_version_primary }}" - target: native - - name: macos-26 - dist_path: dist - os: macos-26 - python: "{{ python_version_primary }}" - target: native - - name: windows-2025 - dist_path: dist - os: windows-2025 - python: "{{ python_version_primary }}" - target: native - {% if add_pyodide -%} - - name: pyodide - dist_path: dist/pyodide - os: ubuntu-24.04 - python: "3.14" - target: pyodide - {% endif %} + name: build ({% raw %}${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %}) + runs-on: {% raw %}${{ matrix.platform.os }}{% endraw %} + +{% include 'native-wheel-matrix.yaml.jinja' %} + + env: + CIBW_BUILD: {% raw %}"${{ matrix.python.cibuildwheel }}-*"{% endraw %} steps: - name: Checkout @@ -68,7 +42,7 @@ jobs: - name: Setup Python uses: actions-ext/python/setup@d8fa97566b70553350d6745e8b0a62db2e3c9cae with: - version: {% raw %}${{ matrix.python }}{% endraw %} + version: {% raw %}${{ matrix.python.version }}{% endraw %} - name: Setup Rust uses: actions-ext/rust/setup@af6343171cd1a86c4183da96de7eb96e15599662 @@ -83,7 +57,7 @@ jobs: - name: Checks run: make checks - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Build run: make build @@ -100,7 +74,7 @@ jobs: - name: Upload test results (Python) uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: {% raw %}test-results-${{ matrix.name }}{% endraw %} + name: {% raw %}test-results-${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %} path: '**/junit.xml' if: always() && matrix.target == 'native' @@ -108,7 +82,7 @@ jobs: uses: EnricoMi/publish-unit-test-result-action@da5b7be67623cd59bbbd1a0ce4f022050a55605f # v2.26.0 with: files: '**/junit.xml' - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Upload coverage uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1 @@ -126,14 +100,12 @@ jobs: - name: Build distributions (Linux x86_64) run: | rm -rf dist - make dist-rs - make dist-py-sdist make dist-py-wheel make dist-check shell: bash env: CIBW_ARCHS_LINUX: native - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Build distributions (Linux ARM64) run: | @@ -143,7 +115,7 @@ jobs: shell: bash env: CIBW_ARCHS_LINUX: native - if: matrix.name == 'ubuntu-24.04-arm' + if: matrix.platform.name == 'ubuntu-24.04-arm' - name: Build distributions (macOS/Windows) run: | @@ -155,6 +127,13 @@ jobs: CIBW_ENVIRONMENT_MACOS: MACOSX_DEPLOYMENT_TARGET=11.0 if: matrix.target == 'native' && runner.os != 'Linux' + - name: Build source distributions + run: | + make dist-rs + 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: @@ -165,10 +144,10 @@ jobs: uses: actions-ext/python/test-sdist@d8fa97566b70553350d6745e8b0a62db2e3c9cae with: module: {{module}} - if: matrix.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 with: - name: {% raw %}dist-${{ matrix.name }}{% endraw %} + name: {% raw %}dist-${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %} path: {% raw %}${{ matrix.dist_path }}{% endraw %} diff --git a/python/rust/Cargo.toml.jinja b/python/rust/Cargo.toml.jinja index 5eace10..7ce6f2a 100644 --- a/python/rust/Cargo.toml.jinja +++ b/python/rust/Cargo.toml.jinja @@ -11,7 +11,7 @@ crate-type = ["cdylib"] [dependencies] {{ module }} = { path = "./rust", version = "*"{% if add_c_abi %}, optional = true{% endif %} } -pyo3 = { version = "0.30.1", features = ["abi3", "multiple-pymethods"] } +pyo3 = { version = "0.30.1", features = [{% if use_abi3 %}"abi3", {% endif %}"multiple-pymethods"] } {% if add_c_abi %} [features] default = ["direct"] diff --git a/python/rust/pyproject.toml.jinja b/python/rust/pyproject.toml.jinja index 42cecca..98b068d 100644 --- a/python/rust/pyproject.toml.jinja +++ b/python/rust/pyproject.toml.jinja @@ -103,7 +103,7 @@ search = 'version = "{current_version}"' replace = 'version = "{new_version}"' {% endif %} [tool.cibuildwheel] -build = "cp{{ python_version_primary.replace('.', '') }}-*" +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" @@ -165,7 +165,7 @@ src = "/" [tool.hatch.build.hooks.hatch-rs] verbose = true -abi3 = true +abi3 = {{ use_abi3 | lower }} module = "{{ module }}" path = "." {% if not add_c_abi -%} diff --git a/python/rustjswasm/.github/workflows/build.yaml.jinja b/python/rustjswasm/.github/workflows/build.yaml.jinja index b3d9fb7..4945285 100644 --- a/python/rustjswasm/.github/workflows/build.yaml.jinja +++ b/python/rustjswasm/.github/workflows/build.yaml.jinja @@ -25,39 +25,13 @@ permissions: jobs: build: - name: build ({% raw %}${{ matrix.name }}{% endraw %}) - runs-on: {% raw %}${{ matrix.os }}{% endraw %} - - strategy: - matrix: - include: - - name: ubuntu-24.04 - dist_path: dist - os: ubuntu-24.04 - python: "{{ python_version_primary }}" - target: native - - name: ubuntu-24.04-arm - dist_path: dist - os: ubuntu-24.04-arm - python: "{{ python_version_primary }}" - target: native - - name: macos-26 - dist_path: dist - os: macos-26 - python: "{{ python_version_primary }}" - target: native - - name: windows-2025 - dist_path: dist - os: windows-2025 - python: "{{ python_version_primary }}" - target: native - {% if add_pyodide -%} - - name: pyodide - dist_path: dist/pyodide - os: ubuntu-24.04 - python: "3.14" - target: pyodide - {% endif %} + name: build ({% raw %}${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %}) + runs-on: {% raw %}${{ matrix.platform.os }}{% endraw %} + +{% include 'native-wheel-matrix.yaml.jinja' %} + + env: + CIBW_BUILD: {% raw %}"${{ matrix.python.cibuildwheel }}-*"{% endraw %} steps: - name: Checkout @@ -68,7 +42,7 @@ jobs: - name: Setup Python uses: actions-ext/python/setup@d8fa97566b70553350d6745e8b0a62db2e3c9cae with: - version: {% raw %}${{ matrix.python }}{% endraw %} + version: {% raw %}${{ matrix.python.version }}{% endraw %} - name: Setup Rust uses: actions-ext/rust/setup@af6343171cd1a86c4183da96de7eb96e15599662 @@ -85,11 +59,11 @@ jobs: - name: Lint run: make lint - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Checks run: make checks - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Build run: make build @@ -106,15 +80,15 @@ jobs: - name: Upload test results (Python) uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: {% raw %}test-results-${{ matrix.name }}{% endraw %} + name: {% raw %}test-results-${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %} path: '**/junit.xml' - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Publish test results uses: EnricoMi/publish-unit-test-result-action@da5b7be67623cd59bbbd1a0ce4f022050a55605f # v2.26.0 with: files: '**/junit.xml' - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Upload coverage uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1 @@ -132,14 +106,12 @@ jobs: - name: Build distributions (Linux x86_64) run: | rm -rf dist - make dist-rs - make dist-py-sdist make dist-py-wheel make dist-check shell: bash env: CIBW_ARCHS_LINUX: native - if: matrix.name == 'ubuntu-24.04' + if: matrix.platform.name == 'ubuntu-24.04' - name: Build distributions (Linux ARM64) run: | @@ -149,7 +121,7 @@ jobs: shell: bash env: CIBW_ARCHS_LINUX: native - if: matrix.name == 'ubuntu-24.04-arm' + if: matrix.platform.name == 'ubuntu-24.04-arm' - name: Build distributions (macOS/Windows) run: | @@ -161,6 +133,13 @@ jobs: CIBW_ENVIRONMENT_MACOS: MACOSX_DEPLOYMENT_TARGET=11.0 if: matrix.target == 'native' && runner.os != 'Linux' + - name: Build source distributions + run: | + make dist-rs + 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: @@ -171,10 +150,10 @@ jobs: uses: actions-ext/python/test-sdist@d8fa97566b70553350d6745e8b0a62db2e3c9cae with: module: {{module}} - if: matrix.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 with: - name: {% raw %}dist-${{ matrix.name }}{% endraw %} + name: {% raw %}dist-${{ matrix.platform.name }}-${{ matrix.python.version }}{% endraw %} path: {% raw %}${{ matrix.dist_path }}{% endraw %} diff --git a/python/rustjswasm/Cargo.toml.jinja b/python/rustjswasm/Cargo.toml.jinja index 1284605..999c9f9 100644 --- a/python/rustjswasm/Cargo.toml.jinja +++ b/python/rustjswasm/Cargo.toml.jinja @@ -11,7 +11,7 @@ crate-type = ["cdylib"] [dependencies] {{ module }} = { path = "./rust", version = "*" } -pyo3 = { version = "0.30.1", features = ["abi3", "multiple-pymethods"] } +pyo3 = { version = "0.30.1", features = [{% if use_abi3 %}"abi3", {% endif %}"multiple-pymethods"] } [profile.release] panic = 'abort' diff --git a/python/rustjswasm/pyproject.toml.jinja b/python/rustjswasm/pyproject.toml.jinja index 3d5e06f..b73e68b 100644 --- a/python/rustjswasm/pyproject.toml.jinja +++ b/python/rustjswasm/pyproject.toml.jinja @@ -110,7 +110,7 @@ search = 'version = "{current_version}"' replace = 'version = "{new_version}"' [tool.cibuildwheel] -build = "cp{{ python_version_primary.replace('.', '') }}-*" +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" @@ -174,7 +174,7 @@ targets = [ [tool.hatch.build.hooks.hatch-rs] env = { PYO3_BUILD_EXTENSION_MODULE = "1" } verbose = true -abi3 = true +abi3 = {{ use_abi3 | lower }} module = "{{ module }}" path = "."