(lowercase) |
(new "End Commands" section) |
||
| Line 9: | Line 9: | ||
* while/endwhile | * while/endwhile | ||
* macro/endmacro | * macro/endmacro | ||
| − | * function/endfunction | + | * function/endfunction |
Use spaces for indenting, 2, 3 or 4 spaces preferably. Use the same amount | Use spaces for indenting, 2, 3 or 4 spaces preferably. Use the same amount | ||
| Line 27: | Line 27: | ||
In KDE the ''all-lowercase'' style is preferred. Mixing upper- and lowercase should not be done in KDE CMake files. | In KDE the ''all-lowercase'' style is preferred. Mixing upper- and lowercase should not be done in KDE CMake files. | ||
| + | |||
| + | == End commands == | ||
| + | |||
| + | To make the code easier to read, use empty commands for endforeach(), endif(), endfunction(), endmacro() and endwhile(). Also, use empty else() commands. | ||
| + | |||
| + | For example, do this: | ||
| + | <pre> | ||
| + | if(FOOVAR) | ||
| + | some_command(...) | ||
| + | else() | ||
| + | another_command(...) | ||
| + | endif() | ||
| + | </pre> | ||
| + | and not this: | ||
| + | <pre> | ||
| + | if(FOOVAR) | ||
| + | some_command(...) | ||
| + | else(FOOVAR) | ||
| + | another_command(...) | ||
| + | endif(FOOVAR) | ||
| + | </pre> | ||
==(Not) Using pkg-config== | ==(Not) Using pkg-config== | ||
This document describes the recommended coding style for CMake files in KDE, i.e. CMakeLists.txt files and *.cmake files.
Contents |
Indent all code correctly, i.e. the body of
Use spaces for indenting, 2, 3 or 4 spaces preferably. Use the same amount of spaces for indenting as is used in the rest of the file. Do not use tabs.
CMake commands are case-insensitive (only the commands, not the arguments or variable names). So all the following versions work:
add_executable(foo foo.c) ADD_EXECUTABLE(bar bar.c) Add_Executable(hello hello.c) aDd_ExEcUtAbLe(blub blub.c)
But this would be ugly.
In KDE the all-lowercase style is preferred. Mixing upper- and lowercase should not be done in KDE CMake files.
To make the code easier to read, use empty commands for endforeach(), endif(), endfunction(), endmacro() and endwhile(). Also, use empty else() commands.
For example, do this:
if(FOOVAR) some_command(...) else() another_command(...) endif()
and not this:
if(FOOVAR) some_command(...) else(FOOVAR) another_command(...) endif(FOOVAR)
You are free to use pkg-config in FindXXX.cmake modules, as long as the following conditions are met:
if(FOO_LIBRARY AND FOO_INCLUDE_DIR) set(FOO_FOUND TRUE) else() ... execute the whole find-logic endif()
should be removed, the find-logic should be executed always. These shortcuts can cause problems e.g. when the same file is used from multiple directories but e.g. with different required versions or components etc.