CMake Language Tools

0.6.13 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `cmake-format` to automatically reformat a CMakeLists.txt file. It creates a sample file, formats it in-place, and then prints the before and after content. This highlights the primary command-line interface of the `cmakelang` tools.

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)

view raw JSON →