Setuptools extension to build and package CMake projects

0.6.1 · active · verified Sun Apr 12

cmake-build-extension is a Setuptools extension designed to simplify the integration of C++ projects based on CMake with Python packaging tools. It enables configuring and building CMake projects directly from `setup.py`, installing their artifacts into the resulting Python package, and passing custom CMake options. The library also provides a context manager for reliable CPython module imports across operating systems. It is currently at version 0.6.1, with releases occurring on an as-needed basis for features and bug fixes.

Warnings

Install

Imports

Quickstart

To use `cmake-build-extension`, you typically define `CMakeExtension` objects within the `ext_modules` list in your `setup.py` and register `BuildExtension` in `cmdclass`. The `CMakeExtension` points to your CMake project and specifies installation details. Ensure your `CMakeLists.txt` is configured to build and install the necessary artifacts.

from setuptools import setup, find_packages
from cmake_build_extension import BuildExtension, CMakeExtension

# --- Minimal Example setup.py ---
# This assumes a C++ project structured like:
# myproject/
# |-- CMakeLists.txt
# |-- src/my_cpp_lib.cpp
# |-- python/
#     `-- my_python_package/
#         `-- __init__.py

setup(
    name="my-hybrid-package",
    version="0.1.0",
    packages=find_packages(where="python"),
    package_dir={'': "python"},
    ext_modules=[
        CMakeExtension(
            name="MyProject", # Corresponds to a CMake target or project name
            install_prefix="my_python_package", # Where to install CMake artifacts within the Python package
            cmake_configure_options=[
                "-DBUILD_SHARED_LIBS=ON",
                # Additional CMake options can be passed here
            ],
            # Optional: Specify a CMake component to install
            # cmake_install_component="my_component",
        ),
    ],
    cmdclass=dict(build_ext=BuildExtension),
    zip_safe=False, # Often required for packages containing C extensions
)

view raw JSON →