cmake-language.7.rst 17 KB

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