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
9 changes: 5 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- rust-capi
- rustjswasm
- cppjswasm
- cppjswasm-capi
- uitk-svelte
- uitk-webawesome
- site-react
Expand Down Expand Up @@ -89,23 +90,23 @@ jobs:
elif [[ "$RUNNER_OS" == "macOS" ]]; then
brew install clang-format
fi
if: matrix.template == 'cpp' || matrix.template == 'cpp-capi' || matrix.template == 'cppjswasm'
if: matrix.template == 'cpp' || matrix.template == 'cpp-capi' || matrix.template == 'cppjswasm' || matrix.template == 'cppjswasm-capi'

- name: Setup Node.js
uses: actions-ext/node/setup@8bcebee998923fcc26f5854b4fd05ea378b58262
with:
version: 22.x
pnpm_version: 11.11.0
js_folder: "."
if: matrix.template == 'js' || matrix.template == 'jupyter' || matrix.template == 'cppjswasm' || matrix.template == 'rustjswasm' || matrix.template == 'uitk-svelte' || matrix.template == 'uitk-webawesome' || matrix.template == 'site-react' || matrix.template == 'site-sveltekit' || matrix.template == 'site-webawesome'
if: matrix.template == 'js' || matrix.template == 'jupyter' || matrix.template == 'cppjswasm' || matrix.template == 'cppjswasm-capi' || matrix.template == 'rustjswasm' || matrix.template == 'uitk-svelte' || matrix.template == 'uitk-webawesome' || matrix.template == 'site-react' || matrix.template == 'site-sveltekit' || matrix.template == 'site-webawesome'

- name: Setup Rust
uses: actions-ext/rust/setup@af6343171cd1a86c4183da96de7eb96e15599662
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'
if: matrix.template == 'js' || matrix.template == 'jupyter' || matrix.template == 'cppjswasm' || matrix.template == 'cppjswasm-capi' || matrix.template == 'rustjswasm'

- name: Build Rust fixture
run: cargo build
Expand Down Expand Up @@ -134,4 +135,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 == 'cpp-capi' || 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 == 'cppjswasm-capi' || 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-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
.PHONY: gen-python gen-cpp gen-cpp-capi gen-js gen-jupyter gen-rust gen-rust-capi gen-rustjswasm gen-cppjswasm gen-cppjswasm-capi 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 @@ -87,6 +87,11 @@ gen-cppjswasm: ## regenerate the cppjswasm template from scratch
copier copy -w . ../python-template-cppjswasm --data-file examples/cppjswasm.yaml
cd ../python-template-cppjswasm && $(SED) 's#_src_path: .#_src_path: https://github.com/python-project-templates/base.git#g' ./.copier-answers.yaml

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

gen-uitk-svelte: ## regenerate the Svelte UI toolkit template from scratch
mkdir -p ../javascript-template-uitk-svelte && cd ../javascript-template-uitk-svelte && rm -rf ./* && rm -rf .copier-answers.yaml .github .gitignore .prettierignore .prettierrc .oxfmtrc.json .oxlintrc.json
copier copy -w . ../javascript-template-uitk-svelte --data-file examples/uitk-svelte.yaml
Expand All @@ -107,7 +112,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-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
.PHONY: test-python test-cpp test-cpp-capi test-js test-jupyter test-rust test-rust-capi test-rustjswasm test-cppjswasm-capi 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 @@ -178,6 +183,14 @@ test-cppjswasm:
cd ../python-template-cppjswasm && make checks
cd ../python-template-cppjswasm && make test

test-cppjswasm-capi:
cd ../python-template-cppjswasm-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-cppjswasm-capi && make develop
cd ../python-template-cppjswasm-capi && git add js/pnpm-lock.yaml && git commit -m "lockfile"
cd ../python-template-cppjswasm-capi && make lint
cd ../python-template-cppjswasm-capi && make checks
cd ../python-template-cppjswasm-capi && make test

test-uitk-svelte:
cd ../javascript-template-uitk-svelte && pnpm install
cd ../javascript-template-uitk-svelte && pnpm exec playwright install chromium
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ One selector chooses a template:
- `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/`.
- `Python / C++ + JavaScript + WebAssembly` generates a C++ package with Python and WebAssembly bindings from `python/cppjswasm/`. `add_c_abi` adds the same C ABI library for the Python extension.
- `JavaScript / UI Toolkit / Svelte` generates a Svelte package with shared design tokens, components, and a demo app from `javascript/uitk-svelte/`.
- `JavaScript / UI Toolkit / Lit + Web Awesome` generates Lit custom elements with Web Awesome, shared design tokens, and a demo app from `javascript/uitk-webawesome/`.
- `JavaScript / Site / React` generates a React site with Vite, Vitest, and Playwright from `javascript/site-react/`.
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 in ['cpp', 'rust'] }}"
when: "{{ add_extension in ['cpp', 'cppjswasm', 'rust'] }}"

python_version_primary:
type: str
Expand Down
13 changes: 13 additions & 0 deletions examples/cppjswasm-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: cppjswasm
email: 3105306+timkpaine@users.noreply.github.com
github: python-project-templates
project_description: A C++-JavaScript-Python project template with a C ABI library
project_name: python template cppjswasm capi
python_version_primary: '3.11'
team: Python Project Template Authors
1 change: 1 addition & 0 deletions examples/cppjswasm.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
26 changes: 26 additions & 0 deletions python/cppjswasm/README.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
[![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 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()`,
`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).
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,46 @@
#include <pybind11/pybind11.h>
#include <stdexcept>
#include <string>

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

namespace py = pybind11;

namespace {

// Owns an example in lib{{ module }} through the C ABI.
class Example {
public:
explicit Example(const std::string& value)
: handle({{ module }}_example_new(value.c_str())) {
if(handle == nullptr)
throw std::runtime_error("{{ module }}_example_new failed");
}
~Example() { {{ module }}_example_free(handle); }
Example(const Example&) = delete;
Example& operator=(const Example&) = delete;

std::string stuff() const { return {{ module }}_example_stuff(handle); }

void set_stuff(const std::string& value) {
if({{ module }}_example_set_stuff(handle, value.c_str()) != 0)
throw std::runtime_error("{{ module }}_example_set_stuff failed");
}

private:
{{ module }}_example* handle;
};

}

PYBIND11_MODULE(extension_cpp, m) {
m.doc() = "pybind11 extension module";

m.def("add", &{{ module }}_add, "A function that adds two numbers");

py::class_<Example>(m, "Example")
.def(py::init<const std::string&>())
.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() + ">"; });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "{{ module }}/{{ module }}.h"

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

struct {{ module }}_example {
Example value;
};

// The function names contain the module name, so clang-format would break these lines differently for each project.
// clang-format off

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

int32_t {{ module }}_add(int32_t i, int32_t j) {
return add(i, j);
}

// Exceptions must not cross the C boundary, so each function that allocates reports failure through its return value.
{{ module }}_example* {{ module }}_example_new(const char* value) {
if(value == nullptr)
return nullptr;
try {
return new {{ module }}_example{Example(value)};
} catch(...) {
return nullptr;
}
}

void {{ module }}_example_free({{ module }}_example* example) {
delete example;
}

const char* {{ module }}_example_stuff(const {{ module }}_example* example) {
return example->value.stuff.c_str();
}

int32_t {{ module }}_example_set_stuff({{ module }}_example* example, const char* value) {
if(value == nullptr)
return -1;
try {
example->value.stuff = value;
return 0;
} catch(...) {
return -1;
}
}

// clang-format on
8 changes: 5 additions & 3 deletions python/cppjswasm/js/build.mjs.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { node_modules_external } from "./tools/externals.mjs";
import fs from "fs";
import cpy from "cpy";

const EXTENSION = "../{{ module }}/extension";

const BUNDLES = [
{
entryPoints: ["src/ts/index.ts"],
Expand All @@ -25,7 +27,7 @@ async function build() {
}
}
}
fs.rmSync("../{{ module }}/extension", {
fs.rmSync(EXTENSION, {
recursive: true,
force: true,
});
Expand All @@ -45,8 +47,8 @@ async function build() {
await Promise.all(BUNDLES.map(bundle)).catch(() => process.exit(1));

// Copy servable assets to python extension (exclude esm/)
fs.mkdirSync("../{{ module }}/extension", { recursive: true });
await cpy("dist/**/*", "../{{ module }}/extension", {
fs.mkdirSync(EXTENSION, { recursive: true });
await cpy("dist/**/*", EXTENSION, {
filter: (file) =>
!file.relativePath.startsWith("esm/") &&
!file.relativePath.startsWith("dist/esm/"),
Expand Down
Loading
Loading