Is there any utility that can reformat a cmake file

13k views Asked by At

I have relatively complex cmake files and I am wondering if there is any utility that can reformat a cmakeList.txt and beautify it for easier reading?

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

Is there any such tools?

I am using notepad++ for editing, but if there is any other tools, I can switch.

4

There are 4 answers

3
user3667089 On BEST ANSWER

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

1
Blabdouze On

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.

0
AMA On

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.

0
alfC On

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.