Understanding control structures in CMake
The CMake language wouldn’t be complete without control structures! Like everything else, they are provided in the form of a command, and they come in three categories: conditional blocks, loops, and command definitions. Control structures are executed in scripts and during buildsystem generation for projects.
Conditional blocks
The only conditional block supported in CMake is the humble if()
command. All conditional blocks have to be closed with an endif()
command, and they may have any number of elseif()
commands and one optional else()
command in this order:
if(<condition>)
<commands>
elseif(<condition>) # optional block, can be repeated
<commands>
else() # optional block
<commands>
endif()
As in many other imperative languages, the if()
-endif()
block controls which sets of commands will be executed:
- If the
<condition>
expression specified in theif...