CMake Format Tool

0.6.13 · active · verified Thu Apr 16

The `cmake-format` tool is a Python-based command-line utility for automatically formatting CMake listfiles (e.g., `CMakeLists.txt`), improving consistency and readability. It is part of the broader `cmakelang` project, which provides the underlying formatting library. The current version is 0.6.13, and the project is actively maintained with several releases per year addressing features and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `cmake-format` command-line tool to format a CMakeLists.txt file in-place.

import os

# Create a dummy CMakeLists.txt with unformatted content
cmake_content = """
project(myproject)
  add_executable(myexec  main.cpp)

function(my_func arg1 arg2)
    message("Hello from my_func")
endfunction()
"""
with open("CMakeLists.txt", "w") as f:
    f.write(cmake_content)

print("Original CMakeLists.txt:\n---")
with open("CMakeLists.txt", "r") as f:
    print(f.read())
print("--- End Original\n")

# Format the file in-place using the CLI tool
# For a real application, consider subprocess.run for better control.
os.system("cmake-format -i CMakeLists.txt")

print("Formatted CMakeLists.txt:\n---")
with open("CMakeLists.txt", "r") as f:
    print(f.read())
print("--- End Formatted\n")

# Clean up
os.remove("CMakeLists.txt")

view raw JSON →