string.rst 13 KB

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