Examples

Examples#

minimal

Bite-sized example using CMake and py-build-cmake to build a Python extension module in C. Includes a line-by-line explanation of the configuration file, and a discussion of the project’s file structure. This manual approach is not recommended for real-world projects, but the simplicity makes it well suited for an in-depth explanation of py-build-cmake.

https://github.com/tttapa/py-build-cmake/tree/main/examples/minimal
pybind11-project

A Python extension module written in C++, with bindings generated by the pybind11 library. This example is a good choice if you want to create a Python interface to C++ functions or classes. It includes some nice features such as automatic generation of PEP 561 stubs for type checking and autocompletion, unit tests using pytest, a configuration for debugging the C++ code, and example configurations for cross-compilation.

https://github.com/tttapa/py-build-cmake/tree/main/examples/pybind11-project
py-build-cmake-example

A full example that resembles a more complete, real-world Python project. It uses the Conan package manager for C++ dependencies, binds C++ code to Python using pybind11, generates typed stub files, and uses continuous integration (CI) to build and deploy the Wheel packages to PyPI. The GitHub actions CI workflow (cross-)compiles the package for all major platforms, including ARMv6 (Raspberry Pi), ARMv7 and AArch64.

https://github.com/tttapa/py-build-cmake-example
nanobind-project

Very similar to the pybind11-project example, but using the more modern (and more performant) nanobind library to generate the Python bindings instead of pybind11.

https://github.com/tttapa/py-build-cmake/tree/main/examples/nanobind-project
swig-project

Very similar to the pybind11-project and nanobind-project examples, but using the SWIG tool to generate the Python bindings instead of pybind11 or nanobind. Since SWIG targets many different programming languages, this may be a good choice if you want to reuse the same bindings for other languages.

https://github.com/tttapa/py-build-cmake/tree/main/examples/swig-project
minimal-program

Uses CMake and py-build-cmake to build a C++ program and packages it as a Python package. The program can then be installed using a single pip install command, and is automatically added to the PATH.

https://github.com/tttapa/py-build-cmake/tree/main/examples/minimal-program
minimal-debug-component

Largely the same as the minimal example, but uses py-build-cmake’s build_component backend to package the debugging symbols in a separate, optional package.

https://github.com/tttapa/py-build-cmake/tree/main/examples/minimal-debug-component

To see how py-build-cmake is used in real-world projects, have a look at the following packages:


GitHub Action workflow to build, test and deploy the generated Python package (from the tttapa/py-build-cmake-example repository).

GitHub Action workflow to build, test and deploy the generated Python package (from the tttapa/py-build-cmake-example repository).#