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
5 changes: 3 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- python
- cython
- cpp
- cpp-capi
- js
- jupyter
- rust
Expand Down Expand Up @@ -88,7 +89,7 @@ jobs:
elif [[ "$RUNNER_OS" == "macOS" ]]; then
brew install clang-format
fi
if: matrix.template == 'cpp' || matrix.template == 'cppjswasm'
if: matrix.template == 'cpp' || matrix.template == 'cpp-capi' || matrix.template == 'cppjswasm'

- name: Setup Node.js
uses: actions-ext/node/setup@8bcebee998923fcc26f5854b4fd05ea378b58262
Expand Down Expand Up @@ -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 == 'rust-capi' || matrix.template == 'rustjswasm'
if: matrix.template == 'cpp' || matrix.template == 'cpp-capi' || matrix.template == 'cppjswasm' || matrix.template == 'rust' || matrix.template == 'rust-capi' || matrix.template == 'rustjswasm'
16 changes: 14 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-rust-capi 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-cpp-capi 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 All @@ -52,6 +52,11 @@ gen-cpp: ## regenerate the c++ template from scratch
copier copy -w . ../python-template-cpp --data-file examples/cpp.yaml
cd ../python-template-cpp && $(SED) 's#_src_path: .#_src_path: https://github.com/python-project-templates/base.git#g' ./.copier-answers.yaml

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

gen-js: ## regenerate the js template from scratch
mkdir -p ../python-template-js && cd ../python-template-js && rm -rf ./* && rm -rf .copier-answers.yaml .gitignore .github .gitattributes
copier copy -w . ../python-template-js --data-file examples/js.yaml
Expand Down Expand Up @@ -102,7 +107,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-rust-capi 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-cpp-capi 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 All @@ -117,6 +122,13 @@ test-cpp:
cd ../python-template-cpp && make checks
cd ../python-template-cpp && make test

test-cpp-capi:
cd ../python-template-cpp-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-cpp-capi && make develop
cd ../python-template-cpp-capi && make lint
cd ../python-template-cpp-capi && make checks
cd ../python-template-cpp-capi && make test

test-js:
cd ../python-template-js && 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-js && make develop
Expand Down
2 changes: 1 addition & 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/`.
- `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 / 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 Down
2 changes: 1 addition & 1 deletion copier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ add_c_abi:
type: bool
default: false
help: Build a C ABI library that the Python extension links against
when: "{{ add_extension == 'rust' }}"
when: "{{ add_extension in ['cpp', 'rust'] }}"

python_version_primary:
type: str
Expand Down
13 changes: 13 additions & 0 deletions examples/cpp-capi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
add_c_abi: true
add_docs: true
add_pyodide: false
add_vcpkg: false
add_wiki: true
add_extension: cpp
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
project_description: A C++-Python project template with a C ABI library
project_name: python template cpp capi
python_version_primary: '3.11'
team: Python Project Template Authors
1 change: 1 addition & 0 deletions examples/cpp.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
add_c_abi: false
add_docs: true
add_pyodide: false
add_vcpkg: true
Expand Down
25 changes: 25 additions & 0 deletions python/cpp/README.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@
[![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

`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
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()`.

`HATCH_CPP_LIBRARY_SOURCE` chooses where the library comes from at build time:
`bundled` (the default, for wheels), `external` (a prebuilt library found
through `HATCH_CPP_LIBRARY_ROOT`, pkg-config, or the conda prefix), or `direct`
(compile the library sources into the extension).

`cpp/CMakeLists.txt` builds and installs the library without Python, with a CMake
package config and a pkg-config file, for conda, vcpkg, and system packages:

```bash
cmake -S cpp -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
cmake --install build --prefix /path/to/prefix
```
{%- 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).
46 changes: 46 additions & 0 deletions python/cpp/cpp/{% if add_c_abi %}CMakeLists.txt{% endif %}.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Builds and installs lib{{ module }} without Python, for conda, vcpkg, and system packages.
cmake_minimum_required(VERSION 3.20)
project({{ module }} VERSION 0.1.0 LANGUAGES CXX)

include(CMakePackageConfigHelpers)

add_library({{ module }} SHARED {{ project_name_formatted }}/c_api.cpp {{ project_name_formatted }}/example.cpp)
add_library({{ module }}::{{ module }} ALIAS {{ module }})
target_compile_features({{ module }} PRIVATE cxx_std_17)
target_compile_definitions({{ module }} PRIVATE {{ module | upper }}_BUILD)
target_include_directories(
{{ module }}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../{{ module }}/include> $<INSTALL_INTERFACE:include>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
)
# SOVERSION is {{ module | upper }}_ABI_VERSION.
set_target_properties(
{{ module }}
PROPERTIES VERSION ${PROJECT_VERSION}
SOVERSION 1
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
INSTALL_NAME_DIR "@rpath"
)

install(
TARGETS {{ module }}
EXPORT {{ module }}Targets
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
install(DIRECTORY ../{{ module }}/include/ DESTINATION include)
install(EXPORT {{ module }}Targets NAMESPACE {{ module }}:: DESTINATION lib/cmake/{{ module }})

configure_package_config_file(
cmake/{{ module }}Config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/{{ module }}Config.cmake
INSTALL_DESTINATION lib/cmake/{{ module }}
)
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/{{ module }}ConfigVersion.cmake COMPATIBILITY SameMinorVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/{{ module }}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/{{ module }}ConfigVersion.cmake
DESTINATION lib/cmake/{{ module }}
)

configure_file(cmake/{{ module }}.pc.in ${CMAKE_CURRENT_BINARY_DIR}/{{ module }}.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/{{ module }}.pc DESTINATION lib/pkgconfig)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
prefix=${pcfiledir}/../..
libdir=${prefix}/lib
includedir=${prefix}/include

Name: {{ module }}
Description: {{ project_description }}
Version: @PROJECT_VERSION@
Libs: -L${libdir} -l{{ module }}
Cflags: -I${includedir}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/{{ module }}Targets.cmake")
check_required_components({{ module }})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <pybind11/pybind11.h>

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

PYBIND11_MODULE(extension, m) {
m.doc() = "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,7 @@
#include "{{ module }}/{{ module }}.h"

#include "{{ project_name_formatted }}/example.hpp"

uint32_t {{ module }}_abi_version(void) { return {{ module | upper }}_ABI_VERSION; }

int32_t {{ module }}_add(int32_t i, int32_t j) { return {{ module }}::add(i, j); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "{{ project_name_formatted }}/example.hpp"

namespace {{ module }} {

int add(int i, int j) { return i + j; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace {{ module }} {

int add(int i, int j);

}
51 changes: 46 additions & 5 deletions python/cpp/pyproject.toml.jinja
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build-system]
requires = [
"hatchling",
"hatch-cpp{% if add_pyodide %}>=0.5.0{% endif %}",
"hatch-cpp{% if add_c_abi %}>=0.6.0{% elif add_pyodide %}>=0.5.0{% endif %}",
"pybind11",
]
build-backend = "hatchling.build"
Expand Down Expand Up @@ -46,7 +46,7 @@ develop = [
"check-dist",
"cibuildwheel",
"codespell",
"hatch-cpp{% if add_pyodide %}>=0.5.0{% endif %}",
"hatch-cpp{% if add_c_abi %}>=0.6.0{% elif add_pyodide %}>=0.5.0{% endif %}",
"hatchling",
"mdformat",
"mdformat-myst",
Expand Down Expand Up @@ -82,7 +82,12 @@ replace = '__version__ = "{new_version}"'
filename = "pyproject.toml"
search = 'version = "{current_version}"'
replace = 'version = "{new_version}"'

{% if add_c_abi %}
[[tool.bumpversion.files]]
filename = "cpp/CMakeLists.txt"
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-*"
test-command = "pytest -vvv {project}/{{ module }}/tests"
Expand All @@ -92,11 +97,16 @@ test-extras = "develop"
manylinux-aarch64-image = "manylinux_2_28"
manylinux-x86_64-image = "manylinux_2_28"
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 }}.so' -w {dest_dir} {wheel}"
{% endif %}
[tool.cibuildwheel.macos]
environment = {MACOSX_DEPLOYMENT_TARGET="11.0"}
archs = "arm64"

{% if add_c_abi -%}
repair-wheel-command = "delocate-wheel --require-archs {delocate_archs} --exclude lib{{ module }}.dylib -w {dest_dir} -v {wheel}"
{% endif %}
[tool.cibuildwheel.windows]
archs = "AMD64"
skip = "*win32 *arm64"
Expand All @@ -106,6 +116,10 @@ skip = "*win32 *arm64"
test-command = "python -m pytest -vvv --pyargs {{ module }}.tests"
test-extras = []
test-requires = ["pytest"]
{% if add_c_abi -%}
# Emscripten has no shared libraries to split; compile the library in.
environment = {HATCH_CPP_LIBRARY_SOURCE="direct"}
{% endif -%}
{% endif %}
[tool.coverage.run]
branch = true
Expand All @@ -127,9 +141,33 @@ src = "/"

[tool.hatch.build.hooks.hatch-cpp]
verbose = true
{%- if add_c_abi %}

# lib{{ module }}: the C ABI library, shipped under {{ module }}/lib. Its header is in {{ module }}/include.
[[tool.hatch.build.hooks.hatch-cpp.libraries]]
name = "{{ module }}/lib/lib{{ module }}"
sources = ["cpp/{{ project_name_formatted }}/c_api.cpp", "cpp/{{ project_name_formatted }}/example.cpp"]
include-dirs = ["cpp", "{{ module }}/include"]
binding = "generic"
define-macros = ["{{ module | upper }}_BUILD"]
extra-compile-args-linux = ["-fvisibility=hidden"]
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"]

# The pybind11 extension, linked against the library by default (library-source = bundled).
[[tool.hatch.build.hooks.hatch-cpp.libraries]]
name = "{{ module }}/extension"
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"}
]
{%- endif %}

[tool.hatch.build.targets.sdist]
exclude = ["*.dll", "*.dylib", "*.so", "*.pyd"]
Expand All @@ -148,6 +186,9 @@ artifacts = [
"{{ module }}/*.dll",
"{{ module }}/*.dylib",
"{{ module }}/*.so",
{%- if add_c_abi %}
"{{ module }}/lib/*",
{%- endif %}
]
packages = [
"{{ module }}",
Expand Down
18 changes: 0 additions & 18 deletions python/cpp/{{module}}/__init__.py

This file was deleted.

Loading
Loading