Formatting CMakeLists.txt with Clang-Format
Asked Answered
H

2

13

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.

Hooey answered 30/5, 2017 at 11:56 Comment(2)
Probably not. It'll parse the file as a cpp file. The indentation is probably because it expects that statements will be divided by semicolons, so it probably interprets everything as a big long line and it indents it to show you that is a continued statement. You can try setting the switch AlignAfterOpenBracket to false. I'm not sure this is the one influencing the indenting but you'll still have problems with ifs and fors, because they don't follow a C like syntax. clang.llvm.org/docs/ClangFormatStyleOptions.htmlViafore
So, clang-format v. 8.0.1 will do stuff to a CMakeLists.txt file. It seems slightly smart, in that it will take a comment and remove the space after the #. Is there any way to control what's done with these files?Carbolize
H
7
  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)
Hooey answered 29/11, 2018 at 1:4 Comment(0)
K
0

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;
Knapp answered 31/3, 2023 at 20:47 Comment(4)
I'm afraid the code # // clang-format off does not work on the CMakeLists.txt. At least not for me.Plebs
i DON'T know what you're talking about but i was talking about TURNING THE FORMATTER OFFKnapp
That is what I am saying. I added # // clang-format off on my CMakeLists.txt, as you mentioned, and the code still got formatted.Plebs
all I know is at some point in time my solution for CMAKE files 100% worked numerous times!!!Knapp

© 2022 - 2024 — McMap. All rights reserved.