string.rst 14 KB

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