Formatting CMakeLists.txt with Clang-Format

13.4k views Asked by At

Is there a way to get clang-format to correctly format a CMake file?

I have a .clang-format file 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 indents many lines. The longer the file, the more levels of indentation I get.

Questions:

  1. Is there a way to get clang-format to recognize a CMakeLists.txt as a different language than Cpp?
  2. Does clang-format have the capabilities for me to add rules for the CMake language?
  3. Does an alternative to clang-format exist 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 output: Same as input. Or maybe no space between command and parenthesis.

2

There are 2 answers

0
Unapiedra On BEST ANSWER
  1. A related question: Is there any utility that can reformat a cmake file

  2. Clang-format cannot do this, but an alternative exists now: https://github.com/cheshirekow/cmake_format

Example -- Bad 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
cmake-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)
0
messed loser On

the following is a solution for the "biggest problem" that's mentioned in this question regarding erroneous "levels of indentation" being unwanted/unexpected

Formatting CMakeLists.txt is NOT what Clang-Format is intended for (so should be turned off since CMakeLists.txt is NOT a C++/C file). if you DON'T turn Clang-Format off on 'text/configuration/data' files (and stuff like that), it's possible that Clang-Format will cause "breaking changes" so that cmake "errors out" [fails with an error]. the build being broken by clang-format has happened to me many times when my IDE sends any file i "click & save" on (including NON-C++/C files) to clang-format for Formatting!

Put the "code below" in any 'CMakeLists.txt' file that should NOT be formatted; but put the "code below" at the very top of the file (so the entire file is turned off):

# // clang-format off

the '#' is necessary so that CMake ignores the custom style option for clang-format. the '#' is a CMake comment (which causes CMake to ignore the entire line). however for that input file, clang-format will understand the custom style option (which turns Clang-format off for that particular file)

Disabling Formatting on a Piece of Code https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code

note: the quote & "code block" below is from the clang-format documentation linked to in the external resource above

Clang-format understands also special comments that switch formatting in a delimited range.

 int formatted_code;
 // clang-format off
     void    unformatted_code  ;
 // clang-format on
 void formatted_code_again;