Formatting CMakeLists.txt with Clang-Format
Is there a way to get the clang format to format the CMake file correctly?
I have a file .clang-format
with Language: Cpp
and BasedOnStyle: Google
. No other language is specified.
Ideally I would like to customize the style, however the biggest problem right now is that clang-format is indenting many lines. The longer the file, the more indentation levels I get.
Questions:
- Is there a way to get clang-format to be recognized
CMakeLists.txt
as a different language than Cpp? - Does the clang format have the ability for me to add rules for the CMake language?
- Is there an alternative to clang format in this context?
Example
Input
cmake_minimum_required (VERSION 3.2)
project(HELLO)
add_executable (helloDemo demo.cxx demo_b.cxx)
add_executable (goodByeDemo goodbye.cxx goodbye_b.cxx)
Actual output
cmake_minimum_required(VERSION 3.2) project(HELLO)
add_executable(helloDemo demo.cxx demo_b.cxx)
add_executable(goodByeDemo goodbye.cxx goodbye_b.cxx)
Expected Result: Same as input. Or maybe there are no spaces between commands and parentheses.
source to share
-
Related question: is there a utility that can reformat the cmake file
-
Clang format cannot do this, but an alternative exists now: https://github.com/cheshirekow/cmake_format
Example - invalid input:
cmake_minimum_required(VERSION 3.2) project(HELLO)
add_executable(helloDemo demo.cxx demo_b.cxx)
add_executable(goodByeDemo goodbye.cxx goodbye_b.cxx)
Command:
pip install --user cmake_format # Or sudo to install system-wide
clang-format -i CMakeLists.txt
Resulting output:
cmake_minimum_required(VERSION 3.2)
project(HELLO)
add_executable(helloDemo demo.cxx demo_b.cxx)
add_executable(goodByeDemo goodbye.cxx goodbye_b.cxx)
source to share