Skip to content
John Paul edited this page Jan 11, 2024 · 1 revision

Welcome to the pycmake!

Pycmake is a utility written in Python to facilitate calls to configure, build, install, and other cmake commands.

Purpose of pycmake

cmake is a cross-platform system to perform automated generation, in other words, cmake uses other available build systems such as ninja, make, etc. and creates the respective files to compile this project, which the build systems that cmake uses are called generators.

For example, if project A wants to compile for the Windows platform, cmake can use generators such as Visual Studio, Ninja and MinGW Makefiles and create the respective project files (a .vcxproj, .ninja or Makefile). Finally, it is compiled according to the generator.

There are certain scenarios where they create a build script to compile the project according to the framework, where you can use different ways such as Bash, Batch, Python, PowerShell and other scripting languages to automate. However, when writing the script to call cmake, using Python, for example:

import subprocess

with subprocess.Popen(['path/to/cmake', '-S', 'mysource', '-B', 'build', 'otherFlags', '...'], otherParams) as cmake:
 cmake.wait()
 if cmake.returncode != 0:
  # error handling

with subprocess.Popen(['path/to/cmake', '--build', 'build', 'otherFlags', '...'], otherParams) as cmake:
 cmake.wait()
 if cmake.returncode != 0:
  # error handling

with subprocess.Popen(['path/to/cmake', '--install', 'mybinaries', 'otherFlags', '...'], otherParams) as cmake:
 cmake.wait()
 if cmake.returncode != 0:
  # error handling

# Do other build stuff

Sometimes this call can be complex and difficult to read and understand what the line means. That's why we have pycmake, in which you can write what you need in a few lines:

import cmake

# Init cmake
cmake.cminit() # add options like logging.

instance = cmake.cmdefault() # get cmake instance.

configure = cmake.CMakeConfigureCommand(
 source_dir='source',
 build_dir='build',
 generator='Ninja',
 variables={
  'CMAKE_C_COMPILER': 'path/to/c_compiler',
  'CMAKE_CXX_COMPILER': 'path/to/cxx_compiler',
  'CMAKE_MAKE_PROGRAM': 'path/to/ninja'
 }
)

build = cmake.CMakeBuildCommand(
 build_path='build'
 max_jobs='4'
)

configure = cmake.CMakeInstallCommand(
 install_path='mybinaries'
)

instance.invoke(configure).invoke(build).invoke(install)

This is the perspective that pycmake will offer, for now this wiki will be under construction.

Clone this wiki locally