cmake-language.7.rst 17 KB

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