Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools for upper/lower case consistency in CMake source

CMake commands are valid in lower, upper, and mixed case. Mixing all of those together in one file however reduces the readability of the CMake code.

Is there a tool for automatically correcting this kind of stylistic inconsistencies?

like image 394
Nico Schlömer Avatar asked Apr 03 '14 13:04

Nico Schlömer


People also ask

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is CMakeLists txt?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.

How do you clean after CMake?

Simply delete the CMakeFiles/ directory inside your build directory. rm -rf CMakeFiles/ cmake --build . This causes CMake to rerun, and build system files are regenerated. Your build will also start from scratch.

How do I use CMake variables?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .


2 Answers

The answer by steveire links to the right resources, but let me explain explicitly in case those links vanish.

CMake command are case insensitive but lower case is recommended according to CMake developer Brad King in 2012:

Ancient CMake versions required upper-case commands. Later command names became case-insensitive. Now the preferred style is lower-case.

The shell code that allowed to convert my project CMakeLists.txt file to lower case was inspired by the code behind the links of steveire's answer:

cmake --help-command-list \
    | while read c; do
        echo 's/\([^a-zA-Z_]\|^\)'"$c"'\(\s*\)(/\1'"$c"'\2(/gI'
    done > convert.sed
git ls-files -z -- '*CMakeLists.txt' | xargs -0 sed -i -f convert.sed

It has the following improvements:

  • grep -v "cmake version" is not required anymore because it seems that the --help-command-list output does not contain that anymore
  • the original regex contained a \b which made it not match any thing with GNU sed 4.2.2
  • instead, the cmake command must be preceded by a non-letter/underscore or the start of the line
  • instead of converting the cmake command to all upper case, make the regex case insensitive. This will also find and convert mixed-case occurrences like Find_Package.
  • there are no parts specific to the cmake codebase. Modify to fit yours. This simple construct was enough for mine.
like image 166
josch Avatar answered Oct 20 '22 23:10

josch


Adapt this to your needs:

http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=77543bd

See also:

http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=9db31162

like image 23
steveire Avatar answered Oct 20 '22 22:10

steveire