cmake-language.7.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. .. cmake-manual-description: CMake Language Reference
  2. cmake-language(7)
  3. *****************
  4. .. only:: html or latex
  5. .. contents::
  6. Organization
  7. ============
  8. CMake input files are written in the "CMake Language" in source files
  9. named ``CMakeLists.txt`` or ending in a ``.cmake`` file name extension.
  10. CMake Language source files in a project are organized into:
  11. * `Directories`_ (``CMakeLists.txt``),
  12. * `Scripts`_ (``<script>.cmake``), and
  13. * `Modules`_ (``<module>.cmake``).
  14. Directories
  15. -----------
  16. When CMake processes a project source tree, the entry point is
  17. a source file called ``CMakeLists.txt`` in the top-level source
  18. directory. This file may contain the entire build specification
  19. or use the :command:`add_subdirectory` command to add subdirectories
  20. to the build. Each subdirectory added by the command must also
  21. contain a ``CMakeLists.txt`` file as the entry point to that
  22. directory. For each source directory whose ``CMakeLists.txt`` file
  23. is processed CMake generates a corresponding directory in the build
  24. tree to act as the default working and output directory.
  25. Scripts
  26. -------
  27. An individual ``<script>.cmake`` source file may be processed
  28. in *script mode* by using the :manual:`cmake(1)` command-line tool
  29. with the ``-P`` option. Script mode simply runs the commands in
  30. the given CMake Language source file and does not generate a
  31. build system. It does not allow CMake commands that define build
  32. targets or actions.
  33. Modules
  34. -------
  35. CMake Language code in either `Directories`_ or `Scripts`_ may
  36. use the :command:`include` command to load a ``<module>.cmake``
  37. source file in the scope of the including context.
  38. See the :manual:`cmake-modules(7)` manual page for documentation
  39. of modules included with the CMake distribution.
  40. Project source trees may also provide their own modules and
  41. specify their location(s) in the :variable:`CMAKE_MODULE_PATH`
  42. variable.
  43. Syntax
  44. ======
  45. Encoding
  46. --------
  47. A CMake Language source file must be written in 7-bit ASCII text
  48. to be portable across all supported platforms. Newlines may be
  49. encoded as either ``\n`` or ``\r\n`` but will be converted to ``\n``
  50. as input files are read.
  51. Note that the implementation is 8-bit clean so source files may
  52. be encoded as UTF-8 on platforms with system APIs supporting this
  53. encoding. Furthermore, CMake 3.0 and above allow a leading UTF-8
  54. `Byte-Order Mark`_ in source files.
  55. .. _`Byte-Order Mark`: http://en.wikipedia.org/wiki/Byte_order_mark
  56. Source Files
  57. ------------
  58. A CMake Language source file consists of zero or more
  59. `Command Invocations`_ separated by newlines and optionally
  60. spaces and `Comments`_:
  61. .. productionlist::
  62. file: `file_element`*
  63. file_element: `command_invocation` `line_ending` |
  64. : (`bracket_comment`|`space`)* `line_ending`
  65. line_ending: `line_comment`? `newline`
  66. space: <match '[ \t]+'>
  67. newline: <match '\n'>
  68. Note that any source file line not inside `Command Arguments`_ or
  69. a `Bracket Comment`_ can end in a `Line Comment`_.
  70. .. _`Command Invocations`:
  71. Command Invocations
  72. -------------------
  73. A *command invocation* is a name followed by paren-enclosed arguments
  74. separated by whitespace:
  75. .. productionlist::
  76. command_invocation: `space`* `identifier` `space`* '(' `arguments` ')'
  77. identifier: <match '[A-Za-z_][A-Za-z0-9_]*'>
  78. arguments: `argument`? `separated_arguments`*
  79. separated_arguments: `separation`+ `argument`? |
  80. : `separation`* '(' `arguments` ')'
  81. separation: `space` | `line_ending`
  82. For example:
  83. .. code-block:: cmake
  84. add_executable(hello world.c)
  85. Command names are case-insensitive.
  86. Nested unquoted parentheses in the arguments must balance.
  87. Each ``(`` or ``)`` is given to the command invocation as
  88. a literal `Unquoted Argument`_. This may be used in calls
  89. to the :command:`if` command to enclose conditions.
  90. For example:
  91. .. code-block:: cmake
  92. if(FALSE AND (FALSE OR TRUE)) # evaluates to FALSE
  93. .. note::
  94. CMake versions prior to 3.0 require command name identifiers
  95. to be at least 2 characters.
  96. CMake versions prior to 2.8.12 silently accept an `Unquoted Argument`_
  97. or a `Quoted Argument`_ immediately following a `Quoted Argument`_ and
  98. not separated by any whitespace. For compatibility, CMake 2.8.12 and
  99. higher accept such code but produce a warning.
  100. Command Arguments
  101. -----------------
  102. There are three types of arguments within `Command Invocations`_:
  103. .. productionlist::
  104. argument: `bracket_argument` | `quoted_argument` | `unquoted_argument`
  105. .. _`Bracket Argument`:
  106. Bracket Argument
  107. ^^^^^^^^^^^^^^^^
  108. A *bracket argument*, inspired by `Lua`_ long bracket syntax,
  109. encloses content between opening and closing "brackets" of the
  110. same length:
  111. .. productionlist::
  112. bracket_argument: `bracket_open` `bracket_content` `bracket_close`
  113. bracket_open: '[' '='{len} '['
  114. bracket_content: <any text not containing a `bracket_close`
  115. : of the same {len} as the `bracket_open`>
  116. bracket_close: ']' '='{len} ']'
  117. An opening bracket of length *len >= 0* is written ``[`` followed
  118. by *len* ``=`` followed by ``[`` and the corresponding closing
  119. bracket is written ``]`` followed by *len* ``=`` followed by ``]``.
  120. Brackets do not nest. A unique length may always be chosen
  121. for the opening and closing brackets to contain closing brackets
  122. of other lengths.
  123. Bracket argument content consists of all text between the opening
  124. and closing brackets, except that one newline immediately following
  125. the opening bracket, if any, is ignored. No evaluation of the
  126. enclosed content, such as `Escape Sequences`_ or `Variable References`_,
  127. is performed. A bracket argument is always given to the command
  128. invocation as exactly one argument.
  129. For example:
  130. .. code-block:: cmake
  131. message([=[
  132. This is the first line in a bracket argument with bracket length 1.
  133. No \-escape sequences or ${variable} references are evaluated.
  134. This is always one argument even though it contains a ; character.
  135. The text does not end on a closing bracket of length 0 like ]].
  136. It does end in a closing bracket of length 1.
  137. ]=])
  138. .. note::
  139. CMake versions prior to 3.0 do not support bracket arguments.
  140. They interpret the opening bracket as the start of an
  141. `Unquoted Argument`_.
  142. .. _`Lua`: http://www.lua.org/
  143. .. _`Quoted Argument`:
  144. Quoted Argument
  145. ^^^^^^^^^^^^^^^
  146. A *quoted argument* encloses content between opening and closing
  147. double-quote characters:
  148. .. productionlist::
  149. quoted_argument: '"' `quoted_element`* '"'
  150. quoted_element: <any character except '\' or '"'> |
  151. : `escape_sequence` |
  152. : `quoted_continuation`
  153. quoted_continuation: '\' `newline`
  154. Quoted argument content consists of all text between opening and
  155. closing quotes. Both `Escape Sequences`_ and `Variable References`_
  156. are evaluated. A quoted argument is always given to the command
  157. invocation as exactly one argument.
  158. For example:
  159. .. code-block:: cmake
  160. message("This is a quoted argument containing multiple lines.
  161. This is always one argument even though it contains a ; character.
  162. Both \\-escape sequences and ${variable} references are evaluated.
  163. The text does not end on an escaped double-quote like \".
  164. It does end in an unescaped double quote.
  165. ")
  166. The final ``\`` on any line ending in an odd number of backslashes
  167. is treated as a line continuation and ignored along with the
  168. immediately following newline character. For example:
  169. .. code-block:: cmake
  170. message("\
  171. This is the first line of a quoted argument. \
  172. In fact it is the only line but since it is long \
  173. the source code uses line continuation.\
  174. ")
  175. .. note::
  176. CMake versions prior to 3.0 do not support continuation with ``\``.
  177. They report errors in quoted arguments containing lines ending in
  178. an odd number of ``\`` characters.
  179. .. _`Unquoted Argument`:
  180. Unquoted Argument
  181. ^^^^^^^^^^^^^^^^^
  182. An *unquoted argument* is not enclosed by any quoting syntax.
  183. It may not contain any whitespace, ``(``, ``)``, ``#``, ``"``, or ``\``
  184. except when escaped by a backslash:
  185. .. productionlist::
  186. unquoted_argument: `unquoted_element`+ | `unquoted_legacy`
  187. unquoted_element: <any character except whitespace or one of '()#"\'> |
  188. : `escape_sequence`
  189. unquoted_legacy: <see note in text>
  190. Unquoted argument content consists of all text in a contiguous block
  191. of allowed or escaped characters. Both `Escape Sequences`_ and
  192. `Variable References`_ are evaluated. The resulting value is divided
  193. in the same way `Lists`_ divide into elements. Each non-empty element
  194. is given to the command invocation as an argument. Therefore an
  195. unquoted argument may be given to a command invocation as zero or
  196. more arguments.
  197. For example:
  198. .. code-block:: cmake
  199. foreach(arg
  200. NoSpace
  201. Escaped\ Space
  202. This;Divides;Into;Five;Arguments
  203. Escaped\;Semicolon
  204. )
  205. message("${arg}")
  206. endforeach()
  207. .. note::
  208. To support legacy CMake code, unquoted arguments may also contain
  209. double-quoted strings (``"..."``, possibly enclosing horizontal
  210. whitespace), and make-style variable references (``$(MAKEVAR)``).
  211. Unescaped double-quotes must balance, may not appear at the
  212. beginning of an unquoted argument, and are treated as part of the
  213. content. For example, the unquoted arguments ``-Da="b c"``,
  214. ``-Da=$(v)``, and ``a" "b"c"d`` are each interpreted literally.
  215. The above "unquoted_legacy" production represents such arguments.
  216. We do not recommend using legacy unquoted arguments in new code.
  217. Instead use a `Quoted Argument`_ or a `Bracket Argument`_ to
  218. represent the content.
  219. Escape Sequences
  220. ----------------
  221. An *escape sequence* is a ``\`` followed by one character:
  222. .. productionlist::
  223. escape_sequence: `escape_identity` | `escape_encoded` | `escape_semicolon`
  224. escape_identity: '\(' | '\)' | '\#' | '\"' | '\ ' |
  225. : '\\' | '\$' | '\@' | '\^'
  226. escape_encoded: '\t' | '\r' | '\n'
  227. escape_semicolon: '\;'
  228. A ``\`` followed by one of ``()#" \#@^`` simply encodes the literal
  229. character without interpreting it as syntax. A ``\t``, ``\r``, or ``\n``
  230. encodes a tab, carriage return, or newline character, respectively.
  231. A ``\;`` encodes itself but may be used in an `Unquoted Argument`_
  232. to encode the ``;`` without dividing the argument value on it.
  233. Variable References
  234. -------------------
  235. A *variable reference* has the form ``${variable_name}`` and is
  236. evaluated inside a `Quoted Argument`_ or an `Unquoted Argument`_.
  237. A variable reference is replaced by the value of the variable,
  238. or by the empty string if the variable is not set.
  239. Variable references can nest and are evaluated from the
  240. inside out, e.g. ``${outer_${inner_variable}_variable}``.
  241. The `Variables`_ section documents the scope of variable names
  242. and how their values are set.
  243. An *environment variable reference* has the form ``$ENV{VAR}`` and
  244. is evaluated in the same contexts as a normal variable reference.
  245. Comments
  246. --------
  247. A comment starts with a ``#`` character that is not inside a
  248. `Bracket Argument`_, `Quoted Argument`_, or escaped with ``\``
  249. as part of an `Unquoted Argument`_. There are two types of
  250. comments: a `Bracket Comment`_ and a `Line Comment`_.
  251. .. _`Bracket Comment`:
  252. Bracket Comment
  253. ^^^^^^^^^^^^^^^
  254. A ``#`` immediately followed by a `Bracket Argument`_ forms a
  255. *bracket comment* consisting of the entire bracket enclosure:
  256. .. productionlist::
  257. bracket_comment: '#' `bracket_argument`
  258. For example:
  259. .. code-block:: cmake
  260. #[[This is a bracket comment.
  261. It runs until the close bracket.]]
  262. message("First Argument\n" #[[Bracket Comment]] "Second Argument")
  263. .. note::
  264. CMake versions prior to 3.0 do not support bracket comments.
  265. They interpret the opening ``#`` as the start of a `Line Comment`_.
  266. .. _`Line Comment`:
  267. Line Comment
  268. ^^^^^^^^^^^^
  269. A ``#`` not immediately followed by a `Bracket Argument`_ forms a
  270. *line comment* that runs until the end of the line:
  271. .. productionlist::
  272. line_comment: '#' <any text not starting in a `bracket_argument`
  273. : and not containing a `newline`>
  274. For example:
  275. .. code-block:: cmake
  276. # This is a line comment.
  277. message("First Argument\n" # This is a line comment :)
  278. "Second Argument") # This is a line comment.
  279. Control Structures
  280. ==================
  281. Conditional Blocks
  282. ------------------
  283. The :command:`if`/:command:`elseif`/:command:`else`/:command:`endif`
  284. commands delimit code blocks to be executed conditionally.
  285. Loops
  286. -----
  287. The :command:`foreach`/:command:`endforeach` and
  288. :command:`while`/:command:`endwhile` commands delimit code
  289. blocks to be executed in a loop. The :command:`break` command
  290. may be used inside such blocks to terminate the loop early.
  291. Command Definitions
  292. -------------------
  293. The :command:`macro`/:command:`endmacro`, and
  294. :command:`function`/:command:`endfunction` commands delimit
  295. code blocks to be recorded for later invocation as commands.
  296. Variables
  297. =========
  298. Variables are the basic unit of storage in the CMake Language.
  299. Their values are always of string type, though some commands may
  300. interpret the strings as values of other types.
  301. The :command:`set` and :command:`unset` commands explicitly
  302. set or unset a variable, but other commands have semantics
  303. that modify variables as well.
  304. Variable names are case-sensitive and may consist of almost
  305. any text, but we recommend sticking to names consisting only
  306. of alphanumeric characters plus ``_`` and ``-``.
  307. Variables have dynamic scope. Each variable "set" or "unset"
  308. creates a binding in the current scope:
  309. Function Scope
  310. `Command Definitions`_ created by the :command:`function` command
  311. create commands that, when invoked, process the recorded commands
  312. in a new variable binding scope. A variable "set" or "unset"
  313. binds in this scope and is visible for the current function and
  314. any nested calls, but not after the function returns.
  315. Directory Scope
  316. Each of the `Directories`_ in a source tree has its own variable
  317. bindings. Before processing the ``CMakeLists.txt`` file for a
  318. directory, CMake copies all variable bindings currently defined
  319. in the parent directory, if any, to initialize the new directory
  320. scope. CMake `Scripts`_, when processed with ``cmake -P``, bind
  321. variables in one "directory" scope.
  322. A variable "set" or "unset" not inside a function call binds
  323. to the current directory scope.
  324. Persistent Cache
  325. CMake stores a separate set of "cache" variables, or "cache entries",
  326. whose values persist across multiple runs within a project build
  327. tree. Cache entries have an isolated binding scope modified only
  328. by explicit request, such as by the ``CACHE`` option of the
  329. :command:`set` and :command:`unset` commands.
  330. When evaluating `Variable References`_, CMake first searches the
  331. function call stack, if any, for a binding and then falls back
  332. to the binding in the current directory scope, if any. If a
  333. "set" binding is found, its value is used. If an "unset" binding
  334. is found, or no binding is found, CMake then searches for a
  335. cache entry. If a cache entry is found, its value is used.
  336. Otherwise, the variable reference evaluates to an empty string.
  337. The :manual:`cmake-variables(7)` manual documents many variables
  338. that are provided by CMake or have meaning to CMake when set
  339. by project code.
  340. Lists
  341. =====
  342. Although all values in CMake are stored as strings, a string
  343. may be treated as a list in certain contexts, such as during
  344. evaluation of an `Unquoted Argument`_. In such contexts, a string
  345. is divided into list elements by splitting on ``;`` characters not
  346. following an unequal number of ``[`` and ``]`` characters and not
  347. immediately preceded by a ``\``. The sequence ``\;`` does not
  348. divide a value but is replaced by ``;`` in the resulting element.
  349. A list of elements is represented as a string by concatenating
  350. the elements separated by ``;``. For example, the :command:`set`
  351. command stores multiple values into the destination variable
  352. as a list:
  353. .. code-block:: cmake
  354. set(srcs a.c b.c c.c) # sets "srcs" to "a.c;b.c;c.c"
  355. Lists are meant for simple use cases such as a list of source
  356. files and should not be used for complex data processing tasks.
  357. Most commands that construct lists do not escape ``;`` characters
  358. in list elements, thus flattening nested lists:
  359. .. code-block:: cmake
  360. set(x a "b;c") # sets "x" to "a;b;c", not "a;b\;c"