CMake Language Tools
The `cmakelang` project provides Quality Assurance (QA) and other language tools for CMake. It includes command-line tools like `cmake-annotate` for generating HTML, `cmake-format` for source code formatting, `cmake-lint` for checking common issues, and `ctest-to` for parsing CTest output. The project, currently at version 0.6.13, is actively maintained with a regular release cadence. While the PyPI package is now `cmakelang`, the GitHub repository is still named `cmake_format`.
Warnings
- breaking The project's Python package name officially changed from `cmake-format` to `cmakelang` around version 0.6.12/0.6.13. The old `cmake-format` PyPI package is now a transitional, empty package that depends on `cmakelang`.
- breaking Configuration file datastructures were overhauled in v0.5.0. While legacy configuration files (without sections) are still supported, they may be deprecated in the future, and default formatting for some commands (e.g., `set()`) changed due to parser logic updates.
- gotcha Optional features like YAML configuration file support (`pyyaml`) or HTML annotation (`jinja2`) require additional, non-default dependencies.
- gotcha `cmake-format` relies on user-provided specifications in its configuration file to correctly understand and format custom CMake commands (macros/functions). Without these, formatting of custom commands may be suboptimal or incorrect.
- gotcha When running `cmake-format --dump-config`, the tool will attempt to load any automatically detected configuration files in the current or parent directories. If these files are corrupt or un-parsable, `dump-config` may fail.
- deprecated The GitHub repository is still named `cmake_format`, even though the PyPI package and project name are `cmakelang`. The documentation indicates the repository may be moved in the future.
Install
-
pip install cmakelang -
pip install cmakelang[YAML] -
pip install cmakelang[html-gen]
Imports
- cmake-format (command-line tool)
Run `cmake-format <file>` from your shell.
Quickstart
import os
cmake_content_before = '''
cmake_minimum_required(VERSION 3.0)
project(MyProject)
set(SOURCES
main.cpp
anotherfile.cpp
thirdfile.cpp)
add_executable(MyProject ${SOURCES})
'''
cmake_file_path = "CMakeLists.txt"
with open(cmake_file_path, "w") as f:
f.write(cmake_content_before)
print("--- CMakeLists.txt (Before Formatting) ---")
print(cmake_content_before)
# Run cmake-format in-place
print("\nRunning cmake-format...")
# For quickstart, using os.system for simplicity, in production consider subprocess.run
exit_code = os.system(f"cmake-format -i {cmake_file_path}")
if exit_code == 0:
with open(cmake_file_path, "r") as f:
cmake_content_after = f.read()
print("\n--- CMakeLists.txt (After Formatting) ---")
print(cmake_content_after)
print("\nFormatting successful!")
else:
print(f"\nError: cmake-format exited with code {exit_code}")
# Clean up
os.remove(cmake_file_path)