Skip to main content

A library for parsing CMakePresets.json

Project description

CMakePresets 0.4.1

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

>>> # 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.4.1.tar.gz (96.5 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.4.1-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cmakepresets-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b9d90bb854b1e782b3b754e7803bfa5daba063367d028b6e9582244faa16eabb
MD5 729990d1f561a68d5020d45854f6b5a6
BLAKE2b-256 d9e9489de54f2faa9607ed9a759aebec85105ca468b5d127f2cae7b096bd0707

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cmakepresets-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bd4b163e97a078be22e9ec923e7418a227c3a58cc0b296aa09c43fe383cbec78
MD5 f16566ddbf4f09b60c5a076e65b2f145
BLAKE2b-256 0fb9474592d64f564112aa10dbcf958569a32752002994d3499323749658a9f1

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