Skip to main content

A library for parsing CMakePresets.json

Project description

CMakePresets 0.5.0

A Python library and CLI tool for working with CMakePresets.json configuration files in CMake projects.

About

CMakePresets is a utility that helps you inspect and work with CMake preset configurations. It provides both a Python API for programmatic access and a command-line interface for interactive use.

Features

  • Parse and analyze CMakePresets.json files
  • List all available presets of different types (configure, build, test, package, workflow)
  • Show detailed information about specific presets
  • Find related presets (e.g., build presets that use a specific configure preset)
  • Display inheritance relationships between presets
  • Output in different formats (rich tables, plain text, JSON)

Installation

pip install cmakepresets

CLI Usage

List all presets

cmakepresets --file CMakePresets.json list
cmakepresets --directory /path/to/project list

List specific types of presets

cmakepresets --file CMakePresets.json list --type configure

Show details of a specific preset

cmakepresets --file CMakePresets.json show my-preset
cmakepresets --file CMakePresets.json show my-preset --type configure

Show in JSON format

cmakepresets --file CMakePresets.json show my-preset --json

Find related presets

cmakepresets --file CMakePresets.json related my-configure-preset
cmakepresets --file CMakePresets.json related my-configure-preset --type build

Script-friendly output

cmakepresets --file CMakePresets.json related my-configure-preset --plain

Python API

>>> # Set up the test environment (only needed for doctest)
>>> import os
>>> import sys
>>> sys.path.insert(0, '.')
>>> # Create a proper test environment
>>> from tests.decorators import CMakePresets_json
>>>
>>> # Create test preset content
>>> preset_content = '''{
...   "version": 4,
...   "cmakeMinimumRequired": {"major": 3, "minor": 23, "patch": 0},
...   "configurePresets": [
...     {
...       "name": "base",
...       "generator": "Ninja",
...       "binaryDir": "${sourceDir}/build/${presetName}",
...       "hidden": true
...     },
...     {
...       "name": "my-config",
...       "inherits": "base",
...       "cacheVariables": {
...         "CMAKE_BUILD_TYPE": "Debug"
...       }
...     }
...   ],
...   "buildPresets": [
...     {
...       "name": "my-build",
...       "configurePreset": "my-config"
...     }
...   ]
... }'''
>>>
>>> # Set up the test environment
>>> patcher = CMakePresets_json(preset_content)
>>> test_env = patcher.__enter__()  # This creates a fake filesystem with CMakePresets.json

>>> ###################################################################
>>> # Now we can import and use CMakePresets normally as in real code #
>>> ###################################################################
>>> # Python API Examples
>>> ####
>>> from cmakepresets import CMakePresets
>>> from cmakepresets.constants import CONFIGURE, PACKAGE

>>> # Silence library debug output in doctest
>>> import logging
>>> import cmakepresets
>>> logging.getLogger(cmakepresets.__name__).setLevel(logging.WARNING)

>>> # Load presets from a file (uses the fake filesystem)
>>> presets = CMakePresets("CMakePresets.json")
>>> print(len(presets.configure_presets))
2


>>> # Or load from a project directory
>>> presets = CMakePresets(".")
>>> print(len(presets.build_presets))
1


>>> # Access preset collections
>>> configure_presets = presets.configure_presets
>>> # List names of all configure presets
>>> [preset["name"] for preset in configure_presets]
['base', 'my-config']


>>> # Get related prests to the configurePreset 'my-config'
>>> related = presets.find_related_presets("my-config")
>>> print(related)
{'build': [{'name': 'my-build', 'configurePreset': 'my-config'}], 'test': [], 'package': []}

>>> # Get related packagePrests to 'my-config'
>>> related = presets.find_related_presets("my-config", PACKAGE)
>>> print(len(related[PACKAGE]))
0


>>> # Get a specific preset by name
>>> my_config = presets.get_preset_by_name(CONFIGURE, "my-config")
>>> my_config["name"]
'my-config'

>>> # Get flattened preset with all inherited properties resolved
>>> flattened = presets.flatten_preset(CONFIGURE, "my-config")

>>> # Print the original preset
>>> print(my_config)
{'name': 'my-config', 'inherits': 'base', 'cacheVariables': {'CMAKE_BUILD_TYPE': 'Debug'}}

>>> # Compared to flattened
>>> print(flattened)
{'name': 'my-config', 'generator': 'Ninja', 'binaryDir': '${sourceDir}/build/${presetName}', 'cacheVariables': {'CMAKE_BUILD_TYPE': 'Debug'}}

>>> # Get flattened preset with "pseudo" resolved macros
>>> resolved = presets.resolve_macro_values(CONFIGURE, "my-config")
>>> print(resolved)
{'name': 'my-config', 'generator': 'Ninja', 'binaryDir': '/home/user/project/build/my-config', 'cacheVariables': {'CMAKE_BUILD_TYPE': 'Debug'}}


>>> # Clean up test environment (important to avoid resource leaks)
>>> patcher.__exit__(None, None, None)

Project details


Download files

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

Source Distribution

cmakepresets-0.5.0.tar.gz (115.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cmakepresets-0.5.0-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file cmakepresets-0.5.0.tar.gz.

File metadata

  • Download URL: cmakepresets-0.5.0.tar.gz
  • Upload date:
  • Size: 115.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.5

File hashes

Hashes for cmakepresets-0.5.0.tar.gz
Algorithm Hash digest
SHA256 65334cefd58dc5859f7a64768d532bb54efc75e59b3de5f5f87e561f741c33be
MD5 06aff48e47f06d69038d30e22c23c6b5
BLAKE2b-256 681666f5b88ae5a3cecbbdf56197631a2d38c942d7ad023729acf8a4a383f6f9

See more details on using hashes here.

File details

Details for the file cmakepresets-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cmakepresets-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3852d651a7934fa4f5d713a55ab623c3d7487a546f3519f7b7e6d4c3eaa07503
MD5 a524e42356f69e585af729379a322d3c
BLAKE2b-256 98985ae36d30bad21edcdfafa7d237b78cf1a781edc542c8b3b2a09d32c1a157

See more details on using hashes here.

Supported by

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