1.. cmake-manual-description: CMake Language Reference 2 3cmake-language(7) 4***************** 5 6.. only:: html 7 8 .. contents:: 9 10Organization 11============ 12 13CMake input files are written in the "CMake Language" in source files 14named ``CMakeLists.txt`` or ending in a ``.cmake`` file name extension. 15 16CMake Language source files in a project are organized into: 17 18* `Directories`_ (``CMakeLists.txt``), 19* `Scripts`_ (``<script>.cmake``), and 20* `Modules`_ (``<module>.cmake``). 21 22Directories 23----------- 24 25When CMake processes a project source tree, the entry point is 26a source file called ``CMakeLists.txt`` in the top-level source 27directory. This file may contain the entire build specification 28or use the :command:`add_subdirectory` command to add subdirectories 29to the build. Each subdirectory added by the command must also 30contain a ``CMakeLists.txt`` file as the entry point to that 31directory. For each source directory whose ``CMakeLists.txt`` file 32is processed CMake generates a corresponding directory in the build 33tree to act as the default working and output directory. 34 35Scripts 36------- 37 38An individual ``<script>.cmake`` source file may be processed 39in *script mode* by using the :manual:`cmake(1)` command-line tool 40with the ``-P`` option. Script mode simply runs the commands in 41the given CMake Language source file and does not generate a 42build system. It does not allow CMake commands that define build 43targets or actions. 44 45Modules 46------- 47 48CMake Language code in either `Directories`_ or `Scripts`_ may 49use the :command:`include` command to load a ``<module>.cmake`` 50source file in the scope of the including context. 51See the :manual:`cmake-modules(7)` manual page for documentation 52of modules included with the CMake distribution. 53Project source trees may also provide their own modules and 54specify their location(s) in the :variable:`CMAKE_MODULE_PATH` 55variable. 56 57Syntax 58====== 59 60.. _`CMake Language Encoding`: 61 62Encoding 63-------- 64 65A CMake Language source file may be written in 7-bit ASCII text for 66maximum portability across all supported platforms. Newlines may be 67encoded as either ``\n`` or ``\r\n`` but will be converted to ``\n`` 68as input files are read. 69 70Note that the implementation is 8-bit clean so source files may 71be encoded as UTF-8 on platforms with system APIs supporting this 72encoding. In addition, CMake 3.2 and above support source files 73encoded in UTF-8 on Windows (using UTF-16 to call system APIs). 74Furthermore, CMake 3.0 and above allow a leading UTF-8 75`Byte-Order Mark`_ in source files. 76 77.. _`Byte-Order Mark`: http://en.wikipedia.org/wiki/Byte_order_mark 78 79Source Files 80------------ 81 82A CMake Language source file consists of zero or more 83`Command Invocations`_ separated by newlines and optionally 84spaces and `Comments`_: 85 86.. raw:: latex 87 88 \begin{small} 89 90.. productionlist:: 91 file: `file_element`* 92 file_element: `command_invocation` `line_ending` | 93 : (`bracket_comment`|`space`)* `line_ending` 94 line_ending: `line_comment`? `newline` 95 space: <match '[ \t]+'> 96 newline: <match '\n'> 97 98.. raw:: latex 99 100 \end{small} 101 102Note that any source file line not inside `Command Arguments`_ or 103a `Bracket Comment`_ can end in a `Line Comment`_. 104 105.. _`Command Invocations`: 106 107Command Invocations 108------------------- 109 110A *command invocation* is a name followed by paren-enclosed arguments 111separated by whitespace: 112 113.. raw:: latex 114 115 \begin{small} 116 117.. productionlist:: 118 command_invocation: `space`* `identifier` `space`* '(' `arguments` ')' 119 identifier: <match '[A-Za-z_][A-Za-z0-9_]*'> 120 arguments: `argument`? `separated_arguments`* 121 separated_arguments: `separation`+ `argument`? | 122 : `separation`* '(' `arguments` ')' 123 separation: `space` | `line_ending` 124 125.. raw:: latex 126 127 \end{small} 128 129For example: 130 131.. code-block:: cmake 132 133 add_executable(hello world.c) 134 135Command names are case-insensitive. 136Nested unquoted parentheses in the arguments must balance. 137Each ``(`` or ``)`` is given to the command invocation as 138a literal `Unquoted Argument`_. This may be used in calls 139to the :command:`if` command to enclose conditions. 140For example: 141 142.. code-block:: cmake 143 144 if(FALSE AND (FALSE OR TRUE)) # evaluates to FALSE 145 146.. note:: 147 CMake versions prior to 3.0 require command name identifiers 148 to be at least 2 characters. 149 150 CMake versions prior to 2.8.12 silently accept an `Unquoted Argument`_ 151 or a `Quoted Argument`_ immediately following a `Quoted Argument`_ and 152 not separated by any whitespace. For compatibility, CMake 2.8.12 and 153 higher accept such code but produce a warning. 154 155Command Arguments 156----------------- 157 158There are three types of arguments within `Command Invocations`_: 159 160.. raw:: latex 161 162 \begin{small} 163 164.. productionlist:: 165 argument: `bracket_argument` | `quoted_argument` | `unquoted_argument` 166 167.. raw:: latex 168 169 \end{small} 170 171.. _`Bracket Argument`: 172 173Bracket Argument 174^^^^^^^^^^^^^^^^ 175 176A *bracket argument*, inspired by `Lua`_ long bracket syntax, 177encloses content between opening and closing "brackets" of the 178same length: 179 180.. raw:: latex 181 182 \begin{small} 183 184.. productionlist:: 185 bracket_argument: `bracket_open` `bracket_content` `bracket_close` 186 bracket_open: '[' '='* '[' 187 bracket_content: <any text not containing a `bracket_close` with 188 : the same number of '=' as the `bracket_open`> 189 bracket_close: ']' '='* ']' 190 191.. raw:: latex 192 193 \end{small} 194 195An opening bracket is written ``[`` followed by zero or more ``=`` followed 196by ``[``. The corresponding closing bracket is written ``]`` followed 197by the same number of ``=`` followed by ``]``. 198Brackets do not nest. A unique length may always be chosen 199for the opening and closing brackets to contain closing brackets 200of other lengths. 201 202Bracket argument content consists of all text between the opening 203and closing brackets, except that one newline immediately following 204the opening bracket, if any, is ignored. No evaluation of the 205enclosed content, such as `Escape Sequences`_ or `Variable References`_, 206is performed. A bracket argument is always given to the command 207invocation as exactly one argument. 208 209.. No code-block syntax highlighting in the following example 210 (long string literal not supported by our cmake.py) 211 212For example:: 213 214 message([=[ 215 This is the first line in a bracket argument with bracket length 1. 216 No \-escape sequences or ${variable} references are evaluated. 217 This is always one argument even though it contains a ; character. 218 The text does not end on a closing bracket of length 0 like ]]. 219 It does end in a closing bracket of length 1. 220 ]=]) 221 222.. note:: 223 CMake versions prior to 3.0 do not support bracket arguments. 224 They interpret the opening bracket as the start of an 225 `Unquoted Argument`_. 226 227.. _`Lua`: http://www.lua.org/ 228 229.. _`Quoted Argument`: 230 231Quoted Argument 232^^^^^^^^^^^^^^^ 233 234A *quoted argument* encloses content between opening and closing 235double-quote characters: 236 237.. raw:: latex 238 239 \begin{small} 240 241.. productionlist:: 242 quoted_argument: '"' `quoted_element`* '"' 243 quoted_element: <any character except '\' or '"'> | 244 : `escape_sequence` | 245 : `quoted_continuation` 246 quoted_continuation: '\' `newline` 247 248.. raw:: latex 249 250 \end{small} 251 252Quoted argument content consists of all text between opening and 253closing quotes. Both `Escape Sequences`_ and `Variable References`_ 254are evaluated. A quoted argument is always given to the command 255invocation as exactly one argument. 256 257.. No code-block syntax highlighting in the following example 258 (escape \" not supported by our cmake.py) 259 260For example: 261 262.. code-block:: cmake 263 264 message("This is a quoted argument containing multiple lines. 265 This is always one argument even though it contains a ; character. 266 Both \\-escape sequences and ${variable} references are evaluated. 267 The text does not end on an escaped double-quote like \". 268 It does end in an unescaped double quote. 269 ") 270 271.. No code-block syntax highlighting in the following example 272 (for conformity with the two above examples) 273 274The final ``\`` on any line ending in an odd number of backslashes 275is treated as a line continuation and ignored along with the 276immediately following newline character. For example: 277 278.. code-block:: cmake 279 280 message("\ 281 This is the first line of a quoted argument. \ 282 In fact it is the only line but since it is long \ 283 the source code uses line continuation.\ 284 ") 285 286.. note:: 287 CMake versions prior to 3.0 do not support continuation with ``\``. 288 They report errors in quoted arguments containing lines ending in 289 an odd number of ``\`` characters. 290 291.. _`Unquoted Argument`: 292 293Unquoted Argument 294^^^^^^^^^^^^^^^^^ 295 296An *unquoted argument* is not enclosed by any quoting syntax. 297It may not contain any whitespace, ``(``, ``)``, ``#``, ``"``, or ``\`` 298except when escaped by a backslash: 299 300.. raw:: latex 301 302 \begin{small} 303 304.. productionlist:: 305 unquoted_argument: `unquoted_element`+ | `unquoted_legacy` 306 unquoted_element: <any character except whitespace or one of '()#"\'> | 307 : `escape_sequence` 308 unquoted_legacy: <see note in text> 309 310.. raw:: latex 311 312 \end{small} 313 314Unquoted argument content consists of all text in a contiguous block 315of allowed or escaped characters. Both `Escape Sequences`_ and 316`Variable References`_ are evaluated. The resulting value is divided 317in the same way `Lists`_ divide into elements. Each non-empty element 318is given to the command invocation as an argument. Therefore an 319unquoted argument may be given to a command invocation as zero or 320more arguments. 321 322For example: 323 324.. code-block:: cmake 325 326 foreach(arg 327 NoSpace 328 Escaped\ Space 329 This;Divides;Into;Five;Arguments 330 Escaped\;Semicolon 331 ) 332 message("${arg}") 333 endforeach() 334 335.. note:: 336 To support legacy CMake code, unquoted arguments may also contain 337 double-quoted strings (``"..."``, possibly enclosing horizontal 338 whitespace), and make-style variable references (``$(MAKEVAR)``). 339 340 Unescaped double-quotes must balance, may not appear at the 341 beginning of an unquoted argument, and are treated as part of the 342 content. For example, the unquoted arguments ``-Da="b c"``, 343 ``-Da=$(v)``, and ``a" "b"c"d`` are each interpreted literally. 344 They may instead be written as quoted arguments ``"-Da=\"b c\""``, 345 ``"-Da=$(v)"``, and ``"a\" \"b\"c\"d"``, respectively. 346 347 Make-style references are treated literally as part of the content 348 and do not undergo variable expansion. They are treated as part 349 of a single argument (rather than as separate ``$``, ``(``, 350 ``MAKEVAR``, and ``)`` arguments). 351 352 The above "unquoted_legacy" production represents such arguments. 353 We do not recommend using legacy unquoted arguments in new code. 354 Instead use a `Quoted Argument`_ or a `Bracket Argument`_ to 355 represent the content. 356 357.. _`Escape Sequences`: 358 359Escape Sequences 360---------------- 361 362An *escape sequence* is a ``\`` followed by one character: 363 364.. raw:: latex 365 366 \begin{small} 367 368.. productionlist:: 369 escape_sequence: `escape_identity` | `escape_encoded` | `escape_semicolon` 370 escape_identity: '\' <match '[^A-Za-z0-9;]'> 371 escape_encoded: '\t' | '\r' | '\n' 372 escape_semicolon: '\;' 373 374.. raw:: latex 375 376 \end{small} 377 378A ``\`` followed by a non-alphanumeric character simply encodes the literal 379character without interpreting it as syntax. A ``\t``, ``\r``, or ``\n`` 380encodes a tab, carriage return, or newline character, respectively. A ``\;`` 381outside of any `Variable References`_ encodes itself but may be used in an 382`Unquoted Argument`_ to encode the ``;`` without dividing the argument 383value on it. A ``\;`` inside `Variable References`_ encodes the literal 384``;`` character. (See also policy :policy:`CMP0053` documentation for 385historical considerations.) 386 387.. _`Variable References`: 388 389Variable References 390------------------- 391 392A *variable reference* has the form ``${<variable>}`` and is 393evaluated inside a `Quoted Argument`_ or an `Unquoted Argument`_. 394A variable reference is replaced by the value of the variable, 395or by the empty string if the variable is not set. 396Variable references can nest and are evaluated from the 397inside out, e.g. ``${outer_${inner_variable}_variable}``. 398 399Literal variable references may consist of alphanumeric characters, 400the characters ``/_.+-``, and `Escape Sequences`_. Nested references 401may be used to evaluate variables of any name. See also policy 402:policy:`CMP0053` documentation for historical considerations and reasons why 403the ``$`` is also technically permitted but is discouraged. 404 405The `Variables`_ section documents the scope of variable names 406and how their values are set. 407 408An *environment variable reference* has the form ``$ENV{<variable>}``. 409See the `Environment Variables`_ section for more information. 410 411A *cache variable reference* has the form ``$CACHE{<variable>}``. 412See :variable:`CACHE` for more information. 413 414The :command:`if` command has a special condition syntax that 415allows for variable references in the short form ``<variable>`` 416instead of ``${<variable>}``. 417However, environment and cache variables always need to be 418referenced as ``$ENV{<variable>}`` or ``$CACHE{<variable>}``. 419 420Comments 421-------- 422 423A comment starts with a ``#`` character that is not inside a 424`Bracket Argument`_, `Quoted Argument`_, or escaped with ``\`` 425as part of an `Unquoted Argument`_. There are two types of 426comments: a `Bracket Comment`_ and a `Line Comment`_. 427 428.. _`Bracket Comment`: 429 430Bracket Comment 431^^^^^^^^^^^^^^^ 432 433A ``#`` immediately followed by a :token:`bracket_open` forms a 434*bracket comment* consisting of the entire bracket enclosure: 435 436.. raw:: latex 437 438 \begin{small} 439 440.. productionlist:: 441 bracket_comment: '#' `bracket_argument` 442 443.. raw:: latex 444 445 \end{small} 446 447For example: 448 449:: 450 451 #[[This is a bracket comment. 452 It runs until the close bracket.]] 453 message("First Argument\n" #[[Bracket Comment]] "Second Argument") 454 455.. note:: 456 CMake versions prior to 3.0 do not support bracket comments. 457 They interpret the opening ``#`` as the start of a `Line Comment`_. 458 459.. _`Line Comment`: 460 461Line Comment 462^^^^^^^^^^^^ 463 464A ``#`` not immediately followed by a :token:`bracket_open` forms a 465*line comment* that runs until the end of the line: 466 467.. raw:: latex 468 469 \begin{small} 470 471.. productionlist:: 472 line_comment: '#' <any text not starting in a `bracket_open` 473 : and not containing a `newline`> 474 475.. raw:: latex 476 477 \end{small} 478 479For example: 480 481.. code-block:: cmake 482 483 # This is a line comment. 484 message("First Argument\n" # This is a line comment :) 485 "Second Argument") # This is a line comment. 486 487Control Structures 488================== 489 490Conditional Blocks 491------------------ 492 493The :command:`if`/:command:`elseif`/:command:`else`/:command:`endif` 494commands delimit code blocks to be executed conditionally. 495 496Loops 497----- 498 499The :command:`foreach`/:command:`endforeach` and 500:command:`while`/:command:`endwhile` commands delimit code 501blocks to be executed in a loop. Inside such blocks the 502:command:`break` command may be used to terminate the loop 503early whereas the :command:`continue` command may be used 504to start with the next iteration immediately. 505 506Command Definitions 507------------------- 508 509The :command:`macro`/:command:`endmacro`, and 510:command:`function`/:command:`endfunction` commands delimit 511code blocks to be recorded for later invocation as commands. 512 513.. _`CMake Language Variables`: 514 515Variables 516========= 517 518Variables are the basic unit of storage in the CMake Language. 519Their values are always of string type, though some commands may 520interpret the strings as values of other types. 521The :command:`set` and :command:`unset` commands explicitly 522set or unset a variable, but other commands have semantics 523that modify variables as well. 524Variable names are case-sensitive and may consist of almost 525any text, but we recommend sticking to names consisting only 526of alphanumeric characters plus ``_`` and ``-``. 527 528Variables have dynamic scope. Each variable "set" or "unset" 529creates a binding in the current scope: 530 531Function Scope 532 `Command Definitions`_ created by the :command:`function` command 533 create commands that, when invoked, process the recorded commands 534 in a new variable binding scope. A variable "set" or "unset" 535 binds in this scope and is visible for the current function and 536 any nested calls within it, but not after the function returns. 537 538Directory Scope 539 Each of the `Directories`_ in a source tree has its own variable 540 bindings. Before processing the ``CMakeLists.txt`` file for a 541 directory, CMake copies all variable bindings currently defined 542 in the parent directory, if any, to initialize the new directory 543 scope. CMake `Scripts`_, when processed with ``cmake -P``, bind 544 variables in one "directory" scope. 545 546 A variable "set" or "unset" not inside a function call binds 547 to the current directory scope. 548 549Persistent Cache 550 CMake stores a separate set of "cache" variables, or "cache entries", 551 whose values persist across multiple runs within a project build 552 tree. Cache entries have an isolated binding scope modified only 553 by explicit request, such as by the ``CACHE`` option of the 554 :command:`set` and :command:`unset` commands. 555 556When evaluating `Variable References`_, CMake first searches the 557function call stack, if any, for a binding and then falls back 558to the binding in the current directory scope, if any. If a 559"set" binding is found, its value is used. If an "unset" binding 560is found, or no binding is found, CMake then searches for a 561cache entry. If a cache entry is found, its value is used. 562Otherwise, the variable reference evaluates to an empty string. 563The ``$CACHE{VAR}`` syntax can be used to do direct cache entry 564lookups. 565 566The :manual:`cmake-variables(7)` manual documents the many variables 567that are provided by CMake or have meaning to CMake when set 568by project code. 569 570.. include:: ID_RESERVE.txt 571 572.. _`CMake Language Environment Variables`: 573 574Environment Variables 575===================== 576 577Environment Variables are like ordinary `Variables`_, with the 578following differences: 579 580Scope 581 Environment variables have global scope in a CMake process. 582 They are never cached. 583 584References 585 `Variable References`_ have the form ``$ENV{<variable>}``. 586 587Initialization 588 Initial values of the CMake environment variables are those of 589 the calling process. 590 Values can be changed using the :command:`set` and :command:`unset` 591 commands. 592 These commands only affect the running CMake process, 593 not the system environment at large. 594 Changed values are not written back to the calling process, 595 and they are not seen by subsequent build or test processes. 596 597The :manual:`cmake-env-variables(7)` manual documents environment 598variables that have special meaning to CMake. 599 600.. _`CMake Language Lists`: 601 602Lists 603===== 604 605Although all values in CMake are stored as strings, a string 606may be treated as a list in certain contexts, such as during 607evaluation of an `Unquoted Argument`_. In such contexts, a string 608is divided into list elements by splitting on ``;`` characters not 609following an unequal number of ``[`` and ``]`` characters and not 610immediately preceded by a ``\``. The sequence ``\;`` does not 611divide a value but is replaced by ``;`` in the resulting element. 612 613A list of elements is represented as a string by concatenating 614the elements separated by ``;``. For example, the :command:`set` 615command stores multiple values into the destination variable 616as a list: 617 618.. code-block:: cmake 619 620 set(srcs a.c b.c c.c) # sets "srcs" to "a.c;b.c;c.c" 621 622Lists are meant for simple use cases such as a list of source 623files and should not be used for complex data processing tasks. 624Most commands that construct lists do not escape ``;`` characters 625in list elements, thus flattening nested lists: 626 627.. code-block:: cmake 628 629 set(x a "b;c") # sets "x" to "a;b;c", not "a;b\;c" 630