Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting CMakeLists.txt with Clang-Format

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.

like image 591
Unapiedra Avatar asked May 30 '17 11:05

Unapiedra


1 Answers

  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)
like image 187
Unapiedra Avatar answered Oct 10 '22 21:10

Unapiedra