string.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. string
  2. ------
  3. String operations.
  4. Synopsis
  5. ^^^^^^^^
  6. .. parsed-literal::
  7. `Search and Replace`_
  8. string(`FIND`_ <string> <substring> <out-var> [...])
  9. string(`REPLACE`_ <match-string> <replace-string> <out-var> <input>...)
  10. `Regular Expressions`_
  11. string(`REGEX MATCH`_ <match-regex> <out-var> <input>...)
  12. string(`REGEX MATCHALL`_ <match-regex> <out-var> <input>...)
  13. string(`REGEX REPLACE`_ <match-regex> <replace-expr> <out-var> <input>...)
  14. `Manipulation`_
  15. string(`APPEND`_ <string-var> [<input>...])
  16. string(`PREPEND`_ <string-var> [<input>...])
  17. string(`CONCAT`_ <out-var> [<input>...])
  18. string(`JOIN`_ <glue> <out-var> [<input>...])
  19. string(`TOLOWER`_ <string> <out-var>)
  20. string(`TOUPPER`_ <string> <out-var>)
  21. string(`LENGTH`_ <string> <out-var>)
  22. string(`SUBSTRING`_ <string> <begin> <length> <out-var>)
  23. string(`STRIP`_ <string> <out-var>)
  24. string(`GENEX_STRIP`_ <string> <out-var>)
  25. string(`REPEAT`_ <string> <count> <out-var>)
  26. `Comparison`_
  27. string(`COMPARE`_ <op> <string1> <string2> <out-var>)
  28. `Hashing`_
  29. string(`\<HASH\> <HASH_>`_ <out-var> <input>)
  30. `Generation`_
  31. string(`ASCII`_ <number>... <out-var>)
  32. string(`CONFIGURE`_ <string> <out-var> [...])
  33. string(`MAKE_C_IDENTIFIER`_ <string> <out-var>)
  34. string(`RANDOM`_ [<option>...] <out-var>)
  35. string(`TIMESTAMP`_ <out-var> [<format string>] [UTC])
  36. string(`UUID`_ <out-var> ...)
  37. Search and Replace
  38. ^^^^^^^^^^^^^^^^^^
  39. .. _FIND:
  40. .. code-block:: cmake
  41. string(FIND <string> <substring> <output_variable> [REVERSE])
  42. Return the position where the given ``<substring>`` was found in
  43. the supplied ``<string>``. If the ``REVERSE`` flag was used, the command will
  44. search for the position of the last occurrence of the specified
  45. ``<substring>``. If the ``<substring>`` is not found, a position of -1 is
  46. returned.
  47. The ``string(FIND)`` subcommand treats all strings as ASCII-only characters.
  48. The index stored in ``<output_variable>`` will also be counted in bytes,
  49. so strings containing multi-byte characters may lead to unexpected results.
  50. .. _REPLACE:
  51. .. code-block:: cmake
  52. string(REPLACE <match_string>
  53. <replace_string> <output_variable>
  54. <input> [<input>...])
  55. Replace all occurrences of ``<match_string>`` in the ``<input>``
  56. with ``<replace_string>`` and store the result in the ``<output_variable>``.
  57. Regular Expressions
  58. ^^^^^^^^^^^^^^^^^^^
  59. .. _`REGEX MATCH`:
  60. .. code-block:: cmake
  61. string(REGEX MATCH <regular_expression>
  62. <output_variable> <input> [<input>...])
  63. Match the ``<regular_expression>`` once and store the match in the
  64. ``<output_variable>``.
  65. All ``<input>`` arguments are concatenated before matching.
  66. .. _`REGEX MATCHALL`:
  67. .. code-block:: cmake
  68. string(REGEX MATCHALL <regular_expression>
  69. <output_variable> <input> [<input>...])
  70. Match the ``<regular_expression>`` as many times as possible and store the
  71. matches in the ``<output_variable>`` as a list.
  72. All ``<input>`` arguments are concatenated before matching.
  73. .. _`REGEX REPLACE`:
  74. .. code-block:: cmake
  75. string(REGEX REPLACE <regular_expression>
  76. <replacement_expression> <output_variable>
  77. <input> [<input>...])
  78. Match the ``<regular_expression>`` as many times as possible and substitute
  79. the ``<replacement_expression>`` for the match in the output.
  80. All ``<input>`` arguments are concatenated before matching.
  81. The ``<replacement_expression>`` may refer to parenthesis-delimited
  82. subexpressions of the match using ``\1``, ``\2``, ..., ``\9``. Note that
  83. two backslashes (``\\1``) are required in CMake code to get a backslash
  84. through argument parsing.
  85. .. _`Regex Specification`:
  86. Regex Specification
  87. """""""""""""""""""
  88. The following characters have special meaning in regular expressions:
  89. ``^``
  90. Matches at beginning of input
  91. ``$``
  92. Matches at end of input
  93. ``.``
  94. Matches any single character
  95. ``\<char>``
  96. Matches the single character specified by ``<char>``. Use this to
  97. match special regex characters, e.g. ``\.`` for a literal ``.``
  98. or ``\\`` for a literal backslash ``\``. Escaping a non-special
  99. character is unnecessary but allowed, e.g. ``\a`` matches ``a``.
  100. ``[ ]``
  101. Matches any character(s) inside the brackets
  102. ``[^ ]``
  103. Matches any character(s) not inside the brackets
  104. ``-``
  105. Inside brackets, specifies an inclusive range between
  106. characters on either side e.g. ``[a-f]`` is ``[abcdef]``
  107. To match a literal ``-`` using brackets, make it the first
  108. or the last character e.g. ``[+*/-]`` matches basic
  109. mathematical operators.
  110. ``*``
  111. Matches preceding pattern zero or more times
  112. ``+``
  113. Matches preceding pattern one or more times
  114. ``?``
  115. Matches preceding pattern zero or once only
  116. ``|``
  117. Matches a pattern on either side of the ``|``
  118. ``()``
  119. Saves a matched subexpression, which can be referenced
  120. in the ``REGEX REPLACE`` operation. Additionally it is saved
  121. by all regular expression-related commands, including
  122. e.g. :command:`if(MATCHES)`, in the variables
  123. :variable:`CMAKE_MATCH_<n>` for ``<n>`` 0..9.
  124. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  125. has lower precedence than concatenation. This means that the regular
  126. expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
  127. expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
  128. CMake language :ref:`Escape Sequences` such as ``\t``, ``\r``, ``\n``,
  129. and ``\\`` may be used to construct literal tabs, carriage returns,
  130. newlines, and backslashes (respectively) to pass in a regex. For example:
  131. * The quoted argument ``"[ \t\r\n]"`` specifies a regex that matches
  132. any single whitespace character.
  133. * The quoted argument ``"[/\\]"`` specifies a regex that matches
  134. a single forward slash ``/`` or backslash ``\``.
  135. * The quoted argument ``"[A-Za-z0-9_]"`` specifies a regex that matches
  136. any single "word" character in the C locale.
  137. * The quoted argument ``"\\(\\a\\+b\\)"`` specifies a regex that matches
  138. the exact string ``(a+b)``. Each ``\\`` is parsed in a quoted argument
  139. as just ``\``, so the regex itself is actually ``\(\a\+\b\)``. This
  140. can alternatively be specified in a :ref:`bracket argument` without
  141. having to escape the backslashes, e.g. ``[[\(\a\+\b\)]]``.
  142. Manipulation
  143. ^^^^^^^^^^^^
  144. .. _APPEND:
  145. .. code-block:: cmake
  146. string(APPEND <string_variable> [<input>...])
  147. Append all the ``<input>`` arguments to the string.
  148. .. _PREPEND:
  149. .. code-block:: cmake
  150. string(PREPEND <string_variable> [<input>...])
  151. Prepend all the ``<input>`` arguments to the string.
  152. .. _CONCAT:
  153. .. code-block:: cmake
  154. string(CONCAT <output_variable> [<input>...])
  155. Concatenate all the ``<input>`` arguments together and store
  156. the result in the named ``<output_variable>``.
  157. .. _JOIN:
  158. .. code-block:: cmake
  159. string(JOIN <glue> <output_variable> [<input>...])
  160. Join all the ``<input>`` arguments together using the ``<glue>``
  161. string and store the result in the named ``<output_variable>``.
  162. To join a list's elements, prefer to use the ``JOIN`` operator
  163. from the :command:`list` command. This allows for the elements to have
  164. special characters like ``;`` in them.
  165. .. _TOLOWER:
  166. .. code-block:: cmake
  167. string(TOLOWER <string> <output_variable>)
  168. Convert ``<string>`` to lower characters.
  169. .. _TOUPPER:
  170. .. code-block:: cmake
  171. string(TOUPPER <string> <output_variable>)
  172. Convert ``<string>`` to upper characters.
  173. .. _LENGTH:
  174. .. code-block:: cmake
  175. string(LENGTH <string> <output_variable>)
  176. Store in an ``<output_variable>`` a given string's length in bytes.
  177. Note that this means if ``<string>`` contains multi-byte characters, the
  178. result stored in ``<output_variable>`` will *not* be the number of characters.
  179. .. _SUBSTRING:
  180. .. code-block:: cmake
  181. string(SUBSTRING <string> <begin> <length> <output_variable>)
  182. Store in an ``<output_variable>`` a substring of a given ``<string>``. If
  183. ``<length>`` is ``-1`` the remainder of the string starting at ``<begin>``
  184. will be returned. If ``<string>`` is shorter than ``<length>`` then the
  185. end of the string is used instead.
  186. Both ``<begin>`` and ``<length>`` are counted in bytes, so care must
  187. be exercised if ``<string>`` could contain multi-byte characters.
  188. .. note::
  189. CMake 3.1 and below reported an error if ``<length>`` pointed past
  190. the end of ``<string>``.
  191. .. _STRIP:
  192. .. code-block:: cmake
  193. string(STRIP <string> <output_variable>)
  194. Store in an ``<output_variable>`` a substring of a given ``<string>`` with
  195. leading and trailing spaces removed.
  196. .. _GENEX_STRIP:
  197. .. code-block:: cmake
  198. string(GENEX_STRIP <string> <output_variable>)
  199. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  200. from the input ``<string>`` and store the result in the ``<output_variable>``.
  201. .. _REPEAT:
  202. .. code-block:: cmake
  203. string(REPEAT <string> <count> <output_variable>)
  204. Produce the output string as the input ``<string>`` repeated ``<count>`` times.
  205. Comparison
  206. ^^^^^^^^^^
  207. .. _COMPARE:
  208. .. code-block:: cmake
  209. string(COMPARE LESS <string1> <string2> <output_variable>)
  210. string(COMPARE GREATER <string1> <string2> <output_variable>)
  211. string(COMPARE EQUAL <string1> <string2> <output_variable>)
  212. string(COMPARE NOTEQUAL <string1> <string2> <output_variable>)
  213. string(COMPARE LESS_EQUAL <string1> <string2> <output_variable>)
  214. string(COMPARE GREATER_EQUAL <string1> <string2> <output_variable>)
  215. Compare the strings and store true or false in the ``<output_variable>``.
  216. .. _`Supported Hash Algorithms`:
  217. Hashing
  218. ^^^^^^^
  219. .. _`HASH`:
  220. .. code-block:: cmake
  221. string(<HASH> <output_variable> <input>)
  222. Compute a cryptographic hash of the ``<input>`` string.
  223. The supported ``<HASH>`` algorithm names are:
  224. ``MD5``
  225. Message-Digest Algorithm 5, RFC 1321.
  226. ``SHA1``
  227. US Secure Hash Algorithm 1, RFC 3174.
  228. ``SHA224``
  229. US Secure Hash Algorithms, RFC 4634.
  230. ``SHA256``
  231. US Secure Hash Algorithms, RFC 4634.
  232. ``SHA384``
  233. US Secure Hash Algorithms, RFC 4634.
  234. ``SHA512``
  235. US Secure Hash Algorithms, RFC 4634.
  236. ``SHA3_224``
  237. Keccak SHA-3.
  238. ``SHA3_256``
  239. Keccak SHA-3.
  240. ``SHA3_384``
  241. Keccak SHA-3.
  242. ``SHA3_512``
  243. Keccak SHA-3.
  244. Generation
  245. ^^^^^^^^^^
  246. .. _ASCII:
  247. .. code-block:: cmake
  248. string(ASCII <number> [<number> ...] <output_variable>)
  249. Convert all numbers into corresponding ASCII characters.
  250. .. _CONFIGURE:
  251. .. code-block:: cmake
  252. string(CONFIGURE <string> <output_variable>
  253. [@ONLY] [ESCAPE_QUOTES])
  254. Transform a ``<string>`` like :command:`configure_file` transforms a file.
  255. .. _MAKE_C_IDENTIFIER:
  256. .. code-block:: cmake
  257. string(MAKE_C_IDENTIFIER <string> <output_variable>)
  258. Convert each non-alphanumeric character in the input ``<string>`` to an
  259. underscore and store the result in the ``<output_variable>``. If the first
  260. character of the ``<string>`` is a digit, an underscore will also be prepended
  261. to the result.
  262. .. _RANDOM:
  263. .. code-block:: cmake
  264. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  265. [RANDOM_SEED <seed>] <output_variable>)
  266. Return a random string of given ``<length>`` consisting of
  267. characters from the given ``<alphabet>``. Default length is 5 characters
  268. and default alphabet is all numbers and upper and lower case letters.
  269. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  270. random number generator.
  271. .. _TIMESTAMP:
  272. .. code-block:: cmake
  273. string(TIMESTAMP <output_variable> [<format_string>] [UTC])
  274. Write a string representation of the current date
  275. and/or time to the ``<output_variable>``.
  276. If the command is unable to obtain a timestamp, the ``<output_variable>``
  277. will be set to the empty string ``""``.
  278. The optional ``UTC`` flag requests the current date/time representation to
  279. be in Coordinated Universal Time (UTC) rather than local time.
  280. The optional ``<format_string>`` may contain the following format
  281. specifiers:
  282. ::
  283. %% A literal percent sign (%).
  284. %d The day of the current month (01-31).
  285. %H The hour on a 24-hour clock (00-23).
  286. %I The hour on a 12-hour clock (01-12).
  287. %j The day of the current year (001-366).
  288. %m The month of the current year (01-12).
  289. %b Abbreviated month name (e.g. Oct).
  290. %B Full month name (e.g. October).
  291. %M The minute of the current hour (00-59).
  292. %s Seconds since midnight (UTC) 1-Jan-1970 (UNIX time).
  293. %S The second of the current minute.
  294. 60 represents a leap second. (00-60)
  295. %U The week number of the current year (00-53).
  296. %w The day of the current week. 0 is Sunday. (0-6)
  297. %a Abbreviated weekday name (e.g. Fri).
  298. %A Full weekday name (e.g. Friday).
  299. %y The last two digits of the current year (00-99)
  300. %Y The current year.
  301. Unknown format specifiers will be ignored and copied to the output
  302. as-is.
  303. If no explicit ``<format_string>`` is given, it will default to:
  304. ::
  305. %Y-%m-%dT%H:%M:%S for local time.
  306. %Y-%m-%dT%H:%M:%SZ for UTC.
  307. .. note::
  308. If the ``SOURCE_DATE_EPOCH`` environment variable is set,
  309. its value will be used instead of the current time.
  310. See https://reproducible-builds.org/specs/source-date-epoch/ for details.
  311. .. _UUID:
  312. .. code-block:: cmake
  313. string(UUID <output_variable> NAMESPACE <namespace> NAME <name>
  314. TYPE <MD5|SHA1> [UPPER])
  315. Create a universally unique identifier (aka GUID) as per RFC4122
  316. based on the hash of the combined values of ``<namespace>``
  317. (which itself has to be a valid UUID) and ``<name>``.
  318. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  319. ``SHA1`` (Version 5 UUID).
  320. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  321. where each ``x`` represents a lower case hexadecimal character.
  322. Where required, an uppercase representation can be requested
  323. with the optional ``UPPER`` flag.