python-cmaketools / python-cmaketools

Seamless integration of Cmake build system and Python setuptools
MIT License
22 stars 1 forks source link

Setuptools extensions for CMake: Seamless integration of Cmake build system to setuptools

This Python package provides an extension to setuptools to integrate CMake into setuptools workflow. CMake build tool is tasked to build/install a complete Python distribution package with binary extensions and necessary data files. Then, setuptools follows to package up the bundled files for binary distribution (bdist_wheel/bdist_egg/etc.) or the CMake source directory for source distribution (sdist).

Features

Usage Examples

You can experiment cmaketools with different Python/native interfaces availeble from following GitHub templates:

Package Authoring Guide

Source Directory Structure

The structure of the source directory and placements of CMakeLists.txt must adhere to the requirements below for cmaketools to detect the package structure correctly. Here are some key tips in structuring the source directory:

CMakeLists.txt Authoring Tips

First, to learn how to author CMake scripts, visit Official CMake Tutorial.

The CMake integration relies on CMake's ability to traverse directory hierarchies, i.e., to encapsulate the build process of each directory via its CMakeLists.txt script and traverse directries. Some script snippets are repetitive and reusable as described below.

Here are general tips:

setup() Arguments

cmaketools.setup() call wraps setuptools.setup() so to initialize CMakeBuilder and auto-generate setuptools.setup() arguments. As such, it accepts most of setuptools.setup() arguments with additional arguments to configure CMake build. Also, it may overwrite user-provided setuptools arguments in order to integrate CMake.

List of New Arguments for CMake

Keyword Type Description
cmake_path str path to cmake command (default auto-detected)
src_dir str Source directory (default "src")
ext_module_dirs str[] List of source directories defining external modules
ext_module_hint str Regex pattern to auto-detect external module directories
test_dir str Unit test directory (default "tests")
test_submodules str[] List of git submodules only used for testing
has_package_data bool Set False if project has no package_data (default True)
skip_configure bool Set True to configure cmake externally (default False)
config str Default CMake build type (default "Release")
generator str Default CMake --G argument
platform str Default CMake --platform argument
toolset str Default CMake --toolset argument
parallel int > 0 Default CMake --parallel argument
configure_opts str[] List of other default option arguments for CMake configure
build_opts str[] List of other default option arguments for CMake build
install_opts str[] List of other default option arguments for CMake install

Overriden setuptools arguments

build_ext Command Options for cmaketools-based setup.py

The build_ext command options are completely changed to accomodate CMake command-line options. Here is the output of python setup.py --help build_ext

Common commands: (see '--help-commands'  for more)
setup.py build will build the package underneath 'build/'
setup.py install will install the package

Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
--no-user-cfg ignore pydistutils.cfg in your home directory

Options for 'build_ext' command:
--cmake-path Name/path of the CMake executable to use, overriding
default auto-detection.
--build-lib (-b) directory for compiled extension modules
--inplace (-i) ignore build-lib and put compiled extensions into the
source directory alongside your pure Python modules
--force (-f) forcibly build everything (delete existing
CMakeCache.txt)
--cache (-C) Pre-load a CMake script to populate the cache.
--define (-D) Create or update a CMake CACHE entry (separated by
';')
--undef (-U) Remove matching entries from CMake CACHE.
--generator (-G) Specify a build system generator.
--toolset (-T) Toolset specification for the generator, if supported.
--platform (-A) Specify platform name if supported by generator.
--Wno-dev Suppress developer warnings.
--Wdev Enable developer warnings.
--Werror Make specified warnings into errors: dev or
deprecated.
--Wno-error Make specified warnings not errors.
--Wdeprecated Enable deprecated functionality warnings.
--Wno-deprecated Suppress deprecated functionality warnings.
--log-level Set the log level to one of: ERROR, WARNING, NOTICE,
STATUS, VERBOSE, DEBUG, TRACE
--log-context Enable the message() command outputting context
attached to each message.
--debug-trycompile Do not delete the try_compile() build tree. Only
useful on one try_compile() at a time.
--debug-output Put cmake in a debug mode.
--debug-find Put cmake find commands in a debug mode.
--trace Put cmake in trace mode.
--trace-expand Put cmake in trace mode with variables expanded.
--trace-format Put cmake in trace mode and sets the trace output
format.
--trace-source Put cmake in trace mode, but output only lines of a
specified file.
--trace-redirect Put cmake in trace mode and redirect trace output to a
file instead of stderr.
--warn-uninitialized Specify a build system generator.
--warn-unused-vars Warn about unused variables.
--no-warn-unused-cli Don’t warn about command line options.
--check-system-vars Find problems with variable usage in system files.
--parallel (-j) The maximum number of concurrent processes to use when
building.
--config For multi-configuration tools, choose this
configuration.
--clean-first Build target clean first, then build.
--verbose (-v) Enable verbose output - if supported - including the
build commands to be executed.
--strip Strip before installing.
--help-generator list available compilers

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help