list.rst 11 KB

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