How can I automatically format a CMake file?
Asked Answered
T

5

26

I have relatively complex CMake files and I am wondering if there is a way to automatically reformat a CMakeLists.txt and beautify it for easier reading.

Some simple tricks would do the jobs, such as indenting if, else, endif sections, and so on.

How can I do this?

Tunisia answered 3/1, 2017 at 15:4 Comment(1)
please check out this link for the CMake editors support overview. Some of them support indentation, but I am not sure if auto-formatting is available. I am also interested if it exists.Phosphorite
R
19

Someone recently wrote a CMake autoformatter called cmake_format. Still under development but should be good enough for basic CMake files.

Retroact answered 23/2, 2018 at 18:20 Comment(3)
@CarloWood, Maybe it was in 2018, but in 2020 it feels like cmake_format is the tool of choice, it's easy to use, and it just worksLoyola
I personally like the parallels of cmake-format with clang-format, makes things easierPhalanx
Now 2024 and cmake-format hasn't see any activity for the past 4 years. I found gersemi and am considering it for a project.Talanian
U
3

In case it is not clear from the other answers and comments. In Ubuntu (Debian?) there is a tool called cmake-format, which seems to be what is called cmake_format in other answers.

And it worked very well for me.

$ apt search cmake-format
Sorting... Done
Full Text Search... Done
cmake-format/kinetic,kinetic,now 0.6.13-2 all [installed]
  source code formatter for cmake listfiles
$ cmake-format --help
usage: 
cmake-format [-h]
             [--dump-config {yaml,json,python} | -i | -o OUTFILE_PATH]
             [-c CONFIG_FILE]
             infilepath [infilepath ...]

Parse cmake listfiles and format them nicely.
...

The specification file is quite difficult to understand but I liked the default formatting anyway. The best part is that it seems to normalize the capitalization of keywords which is what drives me crazy.

Utoaztecan answered 14/1, 2023 at 4:4 Comment(0)
P
2

I searched for a code beautifier supporting CMake, and I did not find any.

  • There is this thread on CMake mailing lists, which does not provide a good solution
  • This mailing list thread provides no answer too (some use emacs mode)
  • I checked most of the major code beautifiers using UniversalIndentGUI on Ubuntu. None of the provided beautifiers from the long list are working with CMake out of the box.

Looks like implementing CMake support in an existing beautifier, or writing a custom one is a great idea for a nice code project.

Phosphorite answered 3/1, 2017 at 16:2 Comment(0)
P
2

Take a look at Atom and its plugin language-cmake (you can install easily with Atom package manager).

Once both are installed, open your CMakeLists.txt and go to Edit > Lines > Auto Indent.

Psephology answered 3/1, 2017 at 16:24 Comment(1)
github.com/textmate/cmake.tmbundle is the source that inspired the atom plugin github.com/lucas-clemente/language-cmake . It's a nice feature of the atom, but changing editors is a non-starter for me personally.Isotron
C
0

In 2024, the answer to this question is gersemi. It is quick and opinionated, just like a formatter should be in my opinion.

Cmake-format seems to be abandoned.

Catima answered 14/7 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.