Cmake basics

From wikinotes

General Notes

Command types & availability

cmake has 3x different types of files:

  • Projects (CMakeLists.txt) defines how to build a project
  • Scripts (*.cmake) any file included in a CMakeLists.txt to introduce functions/variables
  • CTests (*.cmake files run using ctest executable)


Each type of file has it's own set of available commands.

  • script commands can be used anywhere.
  • project commands can only be used in CMakeLists.txt
  • test commands can only be used in *.cmake files run by ctest)


NOTE:

The complete cmake command documentation is available here: https://cmake.org/cmake/help/v3.11/manual/cmake-commands.7.html#cmake-commands-7

Function-Calling Syntax

cmake commands may make use of keyword-arguments. Like python, the order in which they are used does not matter.

install(
   TARGETS       fileA.txt fileB.txt fileC.txt
   DESTINATION   relative/path/
)
 
# is roughly analogous to this in python:
install(
    TARGETS     = [ 'fileA.txt', 'fileB.txt', 'fileC.txt' ],
    DESTINATION = 'relative/path/',
)

Basics

print

MESSAGE( "message to print for the user" )                  # print message to user

math

MATH( EXPR x "3 + 3" )                                      # saves result of '3 + 3' to variable x

comments

# LINE COMMENT
 
#[[
MULTILINE
COMMENT
]]