list.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. list
  2. ----
  3. List operations.
  4. Synopsis
  5. ^^^^^^^^
  6. .. parsed-literal::
  7. `Reading`_
  8. list(`LENGTH`_ <list> <out-var>)
  9. list(`GET`_ <list> <element index> [<index> ...] <out-var>)
  10. list(`JOIN`_ <list> <glue> <out-var>)
  11. list(`SUBLIST`_ <list> <begin> <length> <out-var>)
  12. `Search`_
  13. list(`FIND`_ <list> <value> <out-var>)
  14. `Modification`_
  15. list(`APPEND`_ <list> [<element>...])
  16. list(`FILTER`_ <list> {INCLUDE | EXCLUDE} REGEX <regex>)
  17. list(`INSERT`_ <list> <index> [<element>...])
  18. list(`POP_BACK`_ <list> [<out-var>...])
  19. list(`POP_FRONT`_ <list> [<out-var>...])
  20. list(`PREPEND`_ <list> [<element>...])
  21. list(`REMOVE_ITEM`_ <list> <value>...)
  22. list(`REMOVE_AT`_ <list> <index>...)
  23. list(`REMOVE_DUPLICATES`_ <list>)
  24. list(`TRANSFORM`_ <list> <ACTION> [...])
  25. `Ordering`_
  26. list(`REVERSE`_ <list>)
  27. list(`SORT`_ <list> [...])
  28. Introduction
  29. ^^^^^^^^^^^^
  30. The list subcommands ``APPEND``, ``INSERT``, ``FILTER``, ``PREPEND``,
  31. ``POP_BACK``, ``POP_FRONT``, ``REMOVE_AT``, ``REMOVE_ITEM``,
  32. ``REMOVE_DUPLICATES``, ``REVERSE`` and ``SORT`` may create
  33. new values for the list within the current CMake variable scope. Similar to
  34. the :command:`set` command, the LIST command creates new variable values in
  35. the current scope, even if the list itself is actually defined in a parent
  36. scope. To propagate the results of these operations upwards, use
  37. :command:`set` with ``PARENT_SCOPE``, :command:`set` with
  38. ``CACHE INTERNAL``, or some other means of value propagation.
  39. .. note::
  40. A list in cmake is a ``;`` separated group of strings. To create a
  41. list the set command can be used. For example, ``set(var a b c d e)``
  42. creates a list with ``a;b;c;d;e``, and ``set(var "a b c d e")`` creates a
  43. string or a list with one item in it. (Note macro arguments are not
  44. variables, and therefore cannot be used in LIST commands.)
  45. .. note::
  46. When specifying index values, if ``<element index>`` is 0 or greater, it
  47. is indexed from the beginning of the list, with 0 representing the
  48. first list element. If ``<element index>`` is -1 or lesser, it is indexed
  49. from the end of the list, with -1 representing the last list element.
  50. Be careful when counting with negative indices: they do not start from
  51. 0. -0 is equivalent to 0, the first list element.
  52. Reading
  53. ^^^^^^^
  54. .. _LENGTH:
  55. .. code-block:: cmake
  56. list(LENGTH <list> <output variable>)
  57. Returns the list's length.
  58. .. _GET:
  59. .. code-block:: cmake
  60. list(GET <list> <element index> [<element index> ...] <output variable>)
  61. Returns the list of elements specified by indices from the list.
  62. .. _JOIN:
  63. .. code-block:: cmake
  64. list(JOIN <list> <glue> <output variable>)
  65. .. versionadded:: 3.12
  66. Returns a string joining all list's elements using the glue string.
  67. To join multiple strings, which are not part of a list, use ``JOIN`` operator
  68. from :command:`string` command.
  69. .. _SUBLIST:
  70. .. code-block:: cmake
  71. list(SUBLIST <list> <begin> <length> <output variable>)
  72. .. versionadded:: 3.12
  73. Returns a sublist of the given list.
  74. If ``<length>`` is 0, an empty list will be returned.
  75. If ``<length>`` is -1 or the list is smaller than ``<begin>+<length>`` then
  76. the remaining elements of the list starting at ``<begin>`` will be returned.
  77. Search
  78. ^^^^^^
  79. .. _FIND:
  80. .. code-block:: cmake
  81. list(FIND <list> <value> <output variable>)
  82. Returns the index of the element specified in the list or -1
  83. if it wasn't found.
  84. Modification
  85. ^^^^^^^^^^^^
  86. .. _APPEND:
  87. .. code-block:: cmake
  88. list(APPEND <list> [<element> ...])
  89. Appends elements to the list. If no variable named ``<list>`` exists in the
  90. current scope its value is treated as empty and the elements are appended to
  91. that empty list.
  92. .. _FILTER:
  93. .. code-block:: cmake
  94. list(FILTER <list> <INCLUDE|EXCLUDE> REGEX <regular_expression>)
  95. .. versionadded:: 3.6
  96. Includes or removes items from the list that match the mode's pattern.
  97. In ``REGEX`` mode, items will be matched against the given regular expression.
  98. For more information on regular expressions look under
  99. :ref:`string(REGEX) <Regex Specification>`.
  100. .. _INSERT:
  101. .. code-block:: cmake
  102. list(INSERT <list> <element_index> <element> [<element> ...])
  103. Inserts elements to the list to the specified index. It is an
  104. error to specify an out-of-range index. Valid indexes are 0 to `N`
  105. where `N` is the length of the list, inclusive. An empty list
  106. has length 0. If no variable named ``<list>`` exists in the
  107. current scope its value is treated as empty and the elements are
  108. inserted in that empty list.
  109. .. _POP_BACK:
  110. .. code-block:: cmake
  111. list(POP_BACK <list> [<out-var>...])
  112. .. versionadded:: 3.15
  113. If no variable name is given, removes exactly one element. Otherwise,
  114. with `N` variable names provided, assign the last `N` elements' values
  115. to the given variables and then remove the last `N` values from
  116. ``<list>``.
  117. .. _POP_FRONT:
  118. .. code-block:: cmake
  119. list(POP_FRONT <list> [<out-var>...])
  120. .. versionadded:: 3.15
  121. If no variable name is given, removes exactly one element. Otherwise,
  122. with `N` variable names provided, assign the first `N` elements' values
  123. to the given variables and then remove the first `N` values from
  124. ``<list>``.
  125. .. _PREPEND:
  126. .. code-block:: cmake
  127. list(PREPEND <list> [<element> ...])
  128. .. versionadded:: 3.15
  129. Insert elements to the 0th position in the list. If no variable named
  130. ``<list>`` exists in the current scope its value is treated as empty and
  131. the elements are prepended to that empty list.
  132. .. _REMOVE_ITEM:
  133. .. code-block:: cmake
  134. list(REMOVE_ITEM <list> <value> [<value> ...])
  135. Removes all instances of the given items from the list.
  136. .. _REMOVE_AT:
  137. .. code-block:: cmake
  138. list(REMOVE_AT <list> <index> [<index> ...])
  139. Removes items at given indices from the list.
  140. .. _REMOVE_DUPLICATES:
  141. .. code-block:: cmake
  142. list(REMOVE_DUPLICATES <list>)
  143. Removes duplicated items in the list. The relative order of items is preserved,
  144. but if duplicates are encountered, only the first instance is preserved.
  145. .. _TRANSFORM:
  146. .. code-block:: cmake
  147. list(TRANSFORM <list> <ACTION> [<SELECTOR>]
  148. [OUTPUT_VARIABLE <output variable>])
  149. .. versionadded:: 3.12
  150. Transforms the list by applying an action to all or, by specifying a
  151. ``<SELECTOR>``, to the selected elements of the list, storing the result
  152. in-place or in the specified output variable.
  153. .. note::
  154. The ``TRANSFORM`` sub-command does not change the number of elements in the
  155. list. If a ``<SELECTOR>`` is specified, only some elements will be changed,
  156. the other ones will remain the same as before the transformation.
  157. ``<ACTION>`` specifies the action to apply to the elements of the list.
  158. The actions have exactly the same semantics as sub-commands of the
  159. :command:`string` command. ``<ACTION>`` must be one of the following:
  160. ``APPEND``, ``PREPEND``: Append, prepend specified value to each element of
  161. the list.
  162. .. code-block:: cmake
  163. list(TRANSFORM <list> <APPEND|PREPEND> <value> ...)
  164. ``TOUPPER``, ``TOLOWER``: Convert each element of the list to upper, lower
  165. characters.
  166. .. code-block:: cmake
  167. list(TRANSFORM <list> <TOLOWER|TOUPPER> ...)
  168. ``STRIP``: Remove leading and trailing spaces from each element of the
  169. list.
  170. .. code-block:: cmake
  171. list(TRANSFORM <list> STRIP ...)
  172. ``GENEX_STRIP``: Strip any
  173. :manual:`generator expressions <cmake-generator-expressions(7)>` from each
  174. element of the list.
  175. .. code-block:: cmake
  176. list(TRANSFORM <list> GENEX_STRIP ...)
  177. ``REPLACE``: Match the regular expression as many times as possible and
  178. substitute the replacement expression for the match for each element
  179. of the list
  180. (Same semantic as ``REGEX REPLACE`` from :command:`string` command).
  181. .. code-block:: cmake
  182. list(TRANSFORM <list> REPLACE <regular_expression>
  183. <replace_expression> ...)
  184. ``<SELECTOR>`` determines which elements of the list will be transformed.
  185. Only one type of selector can be specified at a time. When given,
  186. ``<SELECTOR>`` must be one of the following:
  187. ``AT``: Specify a list of indexes.
  188. .. code-block:: cmake
  189. list(TRANSFORM <list> <ACTION> AT <index> [<index> ...] ...)
  190. ``FOR``: Specify a range with, optionally, an increment used to iterate over
  191. the range.
  192. .. code-block:: cmake
  193. list(TRANSFORM <list> <ACTION> FOR <start> <stop> [<step>] ...)
  194. ``REGEX``: Specify a regular expression. Only elements matching the regular
  195. expression will be transformed.
  196. .. code-block:: cmake
  197. list(TRANSFORM <list> <ACTION> REGEX <regular_expression> ...)
  198. Ordering
  199. ^^^^^^^^
  200. .. _REVERSE:
  201. .. code-block:: cmake
  202. list(REVERSE <list>)
  203. Reverses the contents of the list in-place.
  204. .. _SORT:
  205. .. code-block:: cmake
  206. list(SORT <list> [COMPARE <compare>] [CASE <case>] [ORDER <order>])
  207. Sorts the list in-place alphabetically.
  208. .. versionadded:: 3.13
  209. Added the ``COMPARE``, ``CASE``, and ``ORDER`` options.
  210. .. versionadded:: 3.18
  211. Added the ``COMPARE NATURAL`` option.
  212. Use the ``COMPARE`` keyword to select the comparison method for sorting.
  213. The ``<compare>`` option should be one of:
  214. * ``STRING``: Sorts a list of strings alphabetically. This is the
  215. default behavior if the ``COMPARE`` option is not given.
  216. * ``FILE_BASENAME``: Sorts a list of pathnames of files by their basenames.
  217. * ``NATURAL``: Sorts a list of strings using natural order
  218. (see ``strverscmp(3)`` manual), i.e. such that contiguous digits
  219. are compared as whole numbers.
  220. For example: the following list `10.0 1.1 2.1 8.0 2.0 3.1`
  221. will be sorted as `1.1 2.0 2.1 3.1 8.0 10.0` if the ``NATURAL``
  222. comparison is selected where it will be sorted as
  223. `1.1 10.0 2.0 2.1 3.1 8.0` with the ``STRING`` comparison.
  224. Use the ``CASE`` keyword to select a case sensitive or case insensitive
  225. sort mode. The ``<case>`` option should be one of:
  226. * ``SENSITIVE``: List items are sorted in a case-sensitive manner. This is
  227. the default behavior if the ``CASE`` option is not given.
  228. * ``INSENSITIVE``: List items are sorted case insensitively. The order of
  229. items which differ only by upper/lowercase is not specified.
  230. To control the sort order, the ``ORDER`` keyword can be given.
  231. The ``<order>`` option should be one of:
  232. * ``ASCENDING``: Sorts the list in ascending order. This is the default
  233. behavior when the ``ORDER`` option is not given.
  234. * ``DESCENDING``: Sorts the list in descending order.