Skip to main content

Setuptools extension to build and package CMake projects

Project description

cmake-build-extension

Version Python versions Status Format License Python CI/CD

Setuptools extension to build and package CMake projects.

A project MyProject with C++ files that can be compiled with CMake (here MyClass.{h,cpp}) and a Python package (here my_python_package) can be structured as follows:

myproject/
|-- CMakeLists.txt
|-- include
|   `-- MyClass.h
|-- python
|   `-- my_python_package
|       `-- __init__.py
|-- setup.py
`-- src
    `-- MyClass.cpp

The C++ and Python could be independent, even though this hybrid setup usually applies to project that want to provide Python bindings of the C++ libraries.

This extension enables the following:

  • Streamlines the interaction of Python packaging and CMake.
  • Enables building and packaging a generic CMake project through setuptools.
  • Simplifies distributing and consuming C++ / Python hybrid projects.

The following lines of the setup.py script add a build_ext step to the pipeline that compiles and installs the CMake project in the root of the resulting Python package:

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

setup(
    name="my-python-package",
    packages=find_packages("python"),
    package_dir={'': "python"},
    # ...
    ext_modules=[
        CMakeExtension(name="MyProject",
                       install_prefix="my_python_package",
                       cmake_configure_options=[
                           "-DBUILD_SHARED_LIBS:BOOL=OFF",
                       ]),
    ],
    cmdclass=dict(build_ext=BuildExtension),
)

Note: If CMake also exports the targets, downstream projects can import them by adding the directory of the installed Python package to CMAKE_PREFIX_PATH.

The resulting Python install tree will be something similar to what follows:

site-packages/
`-- my_python_package
    |-- __init__.py
    `-- lib
        |-- cmake
        |   |-- MyProject.cmake
        |   |-- MyProjectTargets.cmake
        |   `-- MyProjectVersion.cmake
        `-- libMyProject.so

This way, if the C++ project also includes Python bindings, they can be generated by your favourite tool and handled with CMake. This is often convenient since CMake typically has powerful modules that simplify the building process, especially for big projects.

Check out examples/swig for a minimal working example.

Usage

Once both CMake project and setup.py are correctly configured, the following commands can be used:

# Install with pip
pip install .
pip install --editable .

# Install with setup.py
python setup.py install .
python setup.py develop .

# Install with pip passing custom CMake options
pip install --global-option="build_ext" --global-option="-DBAR=Foo;VAR=TRUE" .

# Install with setup.py passing custom CMake options
python setup.py install build_ext -D"BAR=Foo;VAR=TRUE"

# Create wheel with pip
pip wheel -w dist .

# Create wheel with setup.py
python setup.py bdist_wheel

# Create wheel with setup.py passing custom CMake options
python setup.py bdist_wheel build_ext -D"BAR=Foo;VAR=TRUE"

If the Python project is compliant with PEP517 and PEP518, pip will use an isolated environment. Note that CMake's find_package will still find resources from the filesystem and it will not be isolated.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cmake-build-extension-0.2.dev2.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

cmake_build_extension-0.2.dev2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file cmake-build-extension-0.2.dev2.tar.gz.

File metadata

  • Download URL: cmake-build-extension-0.2.dev2.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for cmake-build-extension-0.2.dev2.tar.gz
Algorithm Hash digest
SHA256 cabf55b18267060ef649054665b0cf02dbaec6005507946b50cdf1fbc5c7d62a
MD5 67929ad723cc421e26bd93d6375ad7aa
BLAKE2b-256 fc436629d9af8c8793dd580c22858031653fd7e3a6e80168f5132268d241b65c

See more details on using hashes here.

File details

Details for the file cmake_build_extension-0.2.dev2-py3-none-any.whl.

File metadata

  • Download URL: cmake_build_extension-0.2.dev2-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for cmake_build_extension-0.2.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 fde7251a544e1866b1bbca4771dc5ace7f316c1890fa671fcb024ed9e71a57c2
MD5 811a17c1e18aad6a51aa30d8481cf089
BLAKE2b-256 89a27bfc48f13ddda2de521493dcdcd02e4f1a2c3b72066d1c58add8b9312660

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page