| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 | .. cmake-manual-description: CMake Language Referencecmake-language(7)*****************.. only:: html   .. contents::Organization============CMake input files are written in the "CMake Language" in source filesnamed ``CMakeLists.txt`` or ending in a ``.cmake`` file name extension.CMake Language source files in a project are organized into:* `Directories`_ (``CMakeLists.txt``),* `Scripts`_ (``<script>.cmake``), and* `Modules`_ (``<module>.cmake``).Directories-----------When CMake processes a project source tree, the entry point isa source file called ``CMakeLists.txt`` in the top-level sourcedirectory.  This file may contain the entire build specificationor use the :command:`add_subdirectory` command to add subdirectoriesto the build.  Each subdirectory added by the command must alsocontain a ``CMakeLists.txt`` file as the entry point to thatdirectory.  For each source directory whose ``CMakeLists.txt`` fileis processed CMake generates a corresponding directory in the buildtree to act as the default working and output directory.Scripts-------An individual ``<script>.cmake`` source file may be processedin *script mode* by using the :manual:`cmake(1)` command-line toolwith the ``-P`` option.  Script mode simply runs the commands inthe given CMake Language source file and does not generate abuild system.  It does not allow CMake commands that define buildtargets or actions.Modules-------CMake Language code in either `Directories`_ or `Scripts`_ mayuse the :command:`include` command to load a ``<module>.cmake``source file in the scope of the including context.See the :manual:`cmake-modules(7)` manual page for documentationof modules included with the CMake distribution.Project source trees may also provide their own modules andspecify their location(s) in the :variable:`CMAKE_MODULE_PATH`variable.Syntax======.. _`CMake Language Encoding`:Encoding--------A CMake Language source file may be written in 7-bit ASCII text formaximum portability across all supported platforms.  Newlines may beencoded as either ``\n`` or ``\r\n`` but will be converted to ``\n``as input files are read.Note that the implementation is 8-bit clean so source files maybe encoded as UTF-8 on platforms with system APIs supporting thisencoding.  In addition, CMake 3.2 and above support source filesencoded in UTF-8 on Windows (using UTF-16 to call system APIs).Furthermore, CMake 3.0 and above allow a leading UTF-8`Byte-Order Mark`_ in source files... _`Byte-Order Mark`: http://en.wikipedia.org/wiki/Byte_order_markSource Files------------A CMake Language source file consists of zero or more`Command Invocations`_ separated by newlines and optionallyspaces and `Comments`_:.. raw:: latex   \begin{small}.. productionlist:: file: `file_element`* file_element: `command_invocation` `line_ending` |             : (`bracket_comment`|`space`)* `line_ending` line_ending: `line_comment`? `newline` space: <match '[ \t]+'> newline: <match '\n'>.. raw:: latex   \end{small}Note that any source file line not inside `Command Arguments`_ ora `Bracket Comment`_ can end in a `Line Comment`_... _`Command Invocations`:Command Invocations-------------------A *command invocation* is a name followed by paren-enclosed argumentsseparated by whitespace:.. raw:: latex   \begin{small}.. productionlist:: command_invocation: `space`* `identifier` `space`* '(' `arguments` ')' identifier: <match '[A-Za-z_][A-Za-z0-9_]*'> arguments: `argument`? `separated_arguments`* separated_arguments: `separation`+ `argument`? |                    : `separation`* '(' `arguments` ')' separation: `space` | `line_ending`.. raw:: latex   \end{small}For example:.. code-block:: cmake add_executable(hello world.c)Command names are case-insensitive.Nested unquoted parentheses in the arguments must balance.Each ``(`` or ``)`` is given to the command invocation asa literal `Unquoted Argument`_.  This may be used in callsto the :command:`if` command to enclose conditions.For example:.. code-block:: cmake if(FALSE AND (FALSE OR TRUE)) # evaluates to FALSE.. note:: CMake versions prior to 3.0 require command name identifiers to be at least 2 characters. CMake versions prior to 2.8.12 silently accept an `Unquoted Argument`_ or a `Quoted Argument`_ immediately following a `Quoted Argument`_ and not separated by any whitespace.  For compatibility, CMake 2.8.12 and higher accept such code but produce a warning.Command Arguments-----------------There are three types of arguments within `Command Invocations`_:.. raw:: latex   \begin{small}.. productionlist:: argument: `bracket_argument` | `quoted_argument` | `unquoted_argument`.. raw:: latex   \end{small}.. _`Bracket Argument`:Bracket Argument^^^^^^^^^^^^^^^^A *bracket argument*, inspired by `Lua`_ long bracket syntax,encloses content between opening and closing "brackets" of thesame length:.. raw:: latex   \begin{small}.. productionlist:: bracket_argument: `bracket_open` `bracket_content` `bracket_close` bracket_open: '[' '='* '[' bracket_content: <any text not containing a `bracket_close` with                :  the same number of '=' as the `bracket_open`> bracket_close: ']' '='* ']'.. raw:: latex   \end{small}An opening bracket is written ``[`` followed by zero or more ``=`` followedby ``[``.  The corresponding closing bracket is written ``]`` followedby the same number of ``=`` followed by ``]``.Brackets do not nest.  A unique length may always be chosenfor the opening and closing brackets to contain closing bracketsof other lengths.Bracket argument content consists of all text between the openingand closing brackets, except that one newline immediately followingthe opening bracket, if any, is ignored.  No evaluation of theenclosed content, such as `Escape Sequences`_ or `Variable References`_,is performed.  A bracket argument is always given to the commandinvocation as exactly one argument... No code-block syntax highlighting in the following example   (long string literal not supported by our cmake.py)For example:: message([=[ This is the first line in a bracket argument with bracket length 1. No \-escape sequences or ${variable} references are evaluated. This is always one argument even though it contains a ; character. The text does not end on a closing bracket of length 0 like ]]. It does end in a closing bracket of length 1. ]=]).. note:: CMake versions prior to 3.0 do not support bracket arguments. They interpret the opening bracket as the start of an `Unquoted Argument`_... _`Lua`: http://www.lua.org/.. _`Quoted Argument`:Quoted Argument^^^^^^^^^^^^^^^A *quoted argument* encloses content between opening and closingdouble-quote characters:.. raw:: latex   \begin{small}.. productionlist:: quoted_argument: '"' `quoted_element`* '"' quoted_element: <any character except '\' or '"'> |                 : `escape_sequence` |                 : `quoted_continuation` quoted_continuation: '\' `newline`.. raw:: latex   \end{small}Quoted argument content consists of all text between opening andclosing quotes.  Both `Escape Sequences`_ and `Variable References`_are evaluated.  A quoted argument is always given to the commandinvocation as exactly one argument... No code-block syntax highlighting in the following example   (escape \" not supported by our cmake.py)For example:.. code-block:: cmake  message("This is a quoted argument containing multiple lines.  This is always one argument even though it contains a ; character.  Both \\-escape sequences and ${variable} references are evaluated.  The text does not end on an escaped double-quote like \".  It does end in an unescaped double quote.  ").. No code-block syntax highlighting in the following example   (for conformity with the two above examples)The final ``\`` on any line ending in an odd number of backslashesis treated as a line continuation and ignored along with theimmediately following newline character.  For example:.. code-block:: cmake  message("\  This is the first line of a quoted argument. \  In fact it is the only line but since it is long \  the source code uses line continuation.\  ").. note:: CMake versions prior to 3.0 do not support continuation with ``\``. They report errors in quoted arguments containing lines ending in an odd number of ``\`` characters... _`Unquoted Argument`:Unquoted Argument^^^^^^^^^^^^^^^^^An *unquoted argument* is not enclosed by any quoting syntax.It may not contain any whitespace, ``(``, ``)``, ``#``, ``"``, or ``\``except when escaped by a backslash:.. raw:: latex   \begin{small}.. productionlist:: unquoted_argument: `unquoted_element`+ | `unquoted_legacy` unquoted_element: <any character except whitespace or one of '()#"\'> |                 : `escape_sequence` unquoted_legacy: <see note in text>.. raw:: latex   \end{small}Unquoted argument content consists of all text in a contiguous blockof allowed or escaped characters.  Both `Escape Sequences`_ and`Variable References`_ are evaluated.  The resulting value is dividedin the same way `Lists`_ divide into elements.  Each non-empty elementis given to the command invocation as an argument.  Therefore anunquoted argument may be given to a command invocation as zero ormore arguments.For example:.. code-block:: cmake foreach(arg     NoSpace     Escaped\ Space     This;Divides;Into;Five;Arguments     Escaped\;Semicolon     )   message("${arg}") endforeach().. note:: To support legacy CMake code, unquoted arguments may also contain double-quoted strings (``"..."``, possibly enclosing horizontal whitespace), and make-style variable references (``$(MAKEVAR)``). Unescaped double-quotes must balance, may not appear at the beginning of an unquoted argument, and are treated as part of the content.  For example, the unquoted arguments ``-Da="b c"``, ``-Da=$(v)``, and ``a" "b"c"d`` are each interpreted literally. They may instead be written as quoted arguments ``"-Da=\"b c\""``, ``"-Da=$(v)"``, and ``"a\" \"b\"c\"d"``, respectively. Make-style references are treated literally as part of the content and do not undergo variable expansion.  They are treated as part of a single argument (rather than as separate ``$``, ``(``, ``MAKEVAR``, and ``)`` arguments). The above "unquoted_legacy" production represents such arguments. We do not recommend using legacy unquoted arguments in new code. Instead use a `Quoted Argument`_ or a `Bracket Argument`_ to represent the content... _`Escape Sequences`:Escape Sequences----------------An *escape sequence* is a ``\`` followed by one character:.. raw:: latex   \begin{small}.. productionlist:: escape_sequence: `escape_identity` | `escape_encoded` | `escape_semicolon` escape_identity: '\' <match '[^A-Za-z0-9;]'> escape_encoded: '\t' | '\r' | '\n' escape_semicolon: '\;'.. raw:: latex   \end{small}A ``\`` followed by a non-alphanumeric character simply encodes the literalcharacter without interpreting it as syntax.  A ``\t``, ``\r``, or ``\n``encodes a tab, carriage return, or newline character, respectively. A ``\;``outside of any `Variable References`_  encodes itself but may be used in an`Unquoted Argument`_ to encode the ``;`` without dividing the argumentvalue on it.  A ``\;`` inside `Variable References`_ encodes the literal``;`` character.  (See also policy :policy:`CMP0053` documentation forhistorical considerations.).. _`Variable References`:Variable References-------------------A *variable reference* has the form ``${<variable>}`` and isevaluated inside a `Quoted Argument`_ or an `Unquoted Argument`_.A variable reference is replaced by the value of the variable,or by the empty string if the variable is not set.Variable references can nest and are evaluated from theinside out, e.g. ``${outer_${inner_variable}_variable}``.Literal variable references may consist of alphanumeric characters,the characters ``/_.+-``, and `Escape Sequences`_.  Nested referencesmay be used to evaluate variables of any name.  See also policy:policy:`CMP0053` documentation for historical considerations and reasons whythe ``$`` is also technically permitted but is discouraged.The `Variables`_ section documents the scope of variable namesand how their values are set.An *environment variable reference* has the form ``$ENV{<variable>}``.See the `Environment Variables`_ section for more information.A *cache variable reference* has the form ``$CACHE{<variable>}``.See :variable:`CACHE` for more information.The :command:`if` command has a special condition syntax thatallows for variable references in the short form ``<variable>``instead of ``${<variable>}``.However, environment and cache variables always need to bereferenced as ``$ENV{<variable>}`` or ``$CACHE{<variable>}``.Comments--------A comment starts with a ``#`` character that is not inside a`Bracket Argument`_, `Quoted Argument`_, or escaped with ``\``as part of an `Unquoted Argument`_.  There are two types ofcomments: a `Bracket Comment`_ and a `Line Comment`_... _`Bracket Comment`:Bracket Comment^^^^^^^^^^^^^^^A ``#`` immediately followed by a :token:`bracket_open` forms a*bracket comment* consisting of the entire bracket enclosure:.. raw:: latex   \begin{small}.. productionlist:: bracket_comment: '#' `bracket_argument`.. raw:: latex   \end{small}For example::: #[[This is a bracket comment. It runs until the close bracket.]] message("First Argument\n" #[[Bracket Comment]] "Second Argument").. note:: CMake versions prior to 3.0 do not support bracket comments. They interpret the opening ``#`` as the start of a `Line Comment`_... _`Line Comment`:Line Comment^^^^^^^^^^^^A ``#`` not immediately followed by a :token:`bracket_open` forms a*line comment* that runs until the end of the line:.. raw:: latex   \begin{small}.. productionlist:: line_comment: '#' <any text not starting in a `bracket_open`             :      and not containing a `newline`>.. raw:: latex   \end{small}For example:.. code-block:: cmake # This is a line comment. message("First Argument\n" # This is a line comment :)         "Second Argument") # This is a line comment.Control Structures==================Conditional Blocks------------------The :command:`if`/:command:`elseif`/:command:`else`/:command:`endif`commands delimit code blocks to be executed conditionally.Loops-----The :command:`foreach`/:command:`endforeach` and:command:`while`/:command:`endwhile` commands delimit codeblocks to be executed in a loop.  Inside such blocks the:command:`break` command may be used to terminate the loopearly whereas the :command:`continue` command may be usedto start with the next iteration immediately.Command Definitions-------------------The :command:`macro`/:command:`endmacro`, and:command:`function`/:command:`endfunction` commands delimitcode blocks to be recorded for later invocation as commands... _`CMake Language Variables`:Variables=========Variables are the basic unit of storage in the CMake Language.Their values are always of string type, though some commands mayinterpret the strings as values of other types.The :command:`set` and :command:`unset` commands explicitlyset or unset a variable, but other commands have semanticsthat modify variables as well.Variable names are case-sensitive and may consist of almostany text, but we recommend sticking to names consisting onlyof alphanumeric characters plus ``_`` and ``-``.Variables have dynamic scope.  Each variable "set" or "unset"creates a binding in the current scope:Function Scope `Command Definitions`_ created by the :command:`function` command create commands that, when invoked, process the recorded commands in a new variable binding scope.  A variable "set" or "unset" binds in this scope and is visible for the current function and any nested calls within it, but not after the function returns.Directory Scope Each of the `Directories`_ in a source tree has its own variable bindings.  Before processing the ``CMakeLists.txt`` file for a directory, CMake copies all variable bindings currently defined in the parent directory, if any, to initialize the new directory scope.  CMake `Scripts`_, when processed with ``cmake -P``, bind variables in one "directory" scope. A variable "set" or "unset" not inside a function call binds to the current directory scope.Persistent Cache CMake stores a separate set of "cache" variables, or "cache entries", whose values persist across multiple runs within a project build tree.  Cache entries have an isolated binding scope modified only by explicit request, such as by the ``CACHE`` option of the :command:`set` and :command:`unset` commands.When evaluating `Variable References`_, CMake first searches thefunction call stack, if any, for a binding and then falls backto the binding in the current directory scope, if any.  If a"set" binding is found, its value is used.  If an "unset" bindingis found, or no binding is found, CMake then searches for acache entry.  If a cache entry is found, its value is used.Otherwise, the variable reference evaluates to an empty string.The ``$CACHE{VAR}`` syntax can be used to do direct cache entrylookups.The :manual:`cmake-variables(7)` manual documents the many variablesthat are provided by CMake or have meaning to CMake when setby project code... include:: ID_RESERVE.txt.. _`CMake Language Environment Variables`:Environment Variables=====================Environment Variables are like ordinary `Variables`_, with thefollowing differences:Scope Environment variables have global scope in a CMake process. They are never cached.References `Variable References`_ have the form ``$ENV{<variable>}``.Initialization Initial values of the CMake environment variables are those of the calling process. Values can be changed using the :command:`set` and :command:`unset` commands. These commands only affect the running CMake process, not the system environment at large. Changed values are not written back to the calling process, and they are not seen by subsequent build or test processes.The :manual:`cmake-env-variables(7)` manual documents environmentvariables that have special meaning to CMake... _`CMake Language Lists`:Lists=====Although all values in CMake are stored as strings, a stringmay be treated as a list in certain contexts, such as duringevaluation of an `Unquoted Argument`_.  In such contexts, a stringis divided into list elements by splitting on ``;`` characters notfollowing an unequal number of ``[`` and ``]`` characters and notimmediately preceded by a ``\``.  The sequence ``\;`` does notdivide a value but is replaced by ``;`` in the resulting element.A list of elements is represented as a string by concatenatingthe elements separated by ``;``.  For example, the :command:`set`command stores multiple values into the destination variableas a list:.. code-block:: cmake set(srcs a.c b.c c.c) # sets "srcs" to "a.c;b.c;c.c"Lists are meant for simple use cases such as a list of sourcefiles and should not be used for complex data processing tasks.Most commands that construct lists do not escape ``;`` charactersin list elements, thus flattening nested lists:.. code-block:: cmake set(x a "b;c") # sets "x" to "a;b;c", not "a;b\;c"
 |