string.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 <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\>`_ <out-var> <input>)
  29. `Generation`_
  30. string(`ASCII`_ <number>... <out-var>)
  31. string(`HEX`_ <string> <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. `JSON`_
  38. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  39. {`GET <JSON GET_>`_ | `TYPE <JSON TYPE_>`_ | `LENGTH <JSON LENGTH_>`_ | `REMOVE <JSON REMOVE_>`_}
  40. <json-string> <member|index> [<member|index> ...])
  41. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  42. `MEMBER <JSON MEMBER_>`_ <json-string>
  43. [<member|index> ...] <index>)
  44. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  45. `SET <JSON SET_>`_ <json-string>
  46. <member|index> [<member|index> ...] <value>)
  47. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  48. `EQUAL <JSON EQUAL_>`_ <json-string1> <json-string2>)
  49. Search and Replace
  50. ^^^^^^^^^^^^^^^^^^
  51. Search and Replace With Plain Strings
  52. """""""""""""""""""""""""""""""""""""
  53. .. signature::
  54. string(FIND <string> <substring> <output_variable> [REVERSE])
  55. Return the position where the given ``<substring>`` was found in
  56. the supplied ``<string>``. If the ``REVERSE`` flag was used, the command
  57. will search for the position of the last occurrence of the specified
  58. ``<substring>``. If the ``<substring>`` is not found, a position of -1 is
  59. returned.
  60. The ``string(FIND)`` subcommand treats all strings as ASCII-only characters.
  61. The index stored in ``<output_variable>`` will also be counted in bytes,
  62. so strings containing multi-byte characters may lead to unexpected results.
  63. .. signature::
  64. string(REPLACE <match_string>
  65. <replace_string> <output_variable>
  66. <input> [<input>...])
  67. Replace all occurrences of ``<match_string>`` in the ``<input>``
  68. with ``<replace_string>`` and store the result in the ``<output_variable>``.
  69. Search and Replace With Regular Expressions
  70. """""""""""""""""""""""""""""""""""""""""""
  71. .. signature::
  72. string(REGEX MATCH <regular_expression>
  73. <output_variable> <input> [<input>...])
  74. Match the ``<regular_expression>`` once and store the match in the
  75. ``<output_variable>``.
  76. All ``<input>`` arguments are concatenated before matching.
  77. Regular expressions are specified in the subsection just below.
  78. .. signature::
  79. string(REGEX MATCHALL <regular_expression>
  80. <output_variable> <input> [<input>...])
  81. Match the ``<regular_expression>`` as many times as possible and store the
  82. matches in the ``<output_variable>`` as a list.
  83. All ``<input>`` arguments are concatenated before matching.
  84. .. signature::
  85. string(REGEX REPLACE <regular_expression>
  86. <replacement_expression> <output_variable>
  87. <input> [<input>...])
  88. Match the ``<regular_expression>`` as many times as possible and substitute
  89. the ``<replacement_expression>`` for the match in the output.
  90. All ``<input>`` arguments are concatenated before matching.
  91. The ``<replacement_expression>`` may refer to parenthesis-delimited
  92. subexpressions of the match using ``\1``, ``\2``, ..., ``\9``. Note that
  93. two backslashes (``\\1``) are required in CMake code to get a backslash
  94. through argument parsing.
  95. .. versionchanged:: 4.1
  96. The ``^`` anchor now matches only at the beginning of the input
  97. string instead of the beginning of each repeated search.
  98. See policy :policy:`CMP0186`.
  99. The replacement expression may contain references to subexpressions that
  100. didn't match anything. Previously, such references triggered an error.
  101. .. _`Regex Specification`:
  102. Regex Specification
  103. """""""""""""""""""
  104. The following characters have special meaning in regular expressions:
  105. ``^``
  106. Matches at beginning of input
  107. ``$``
  108. Matches at end of input
  109. ``.``
  110. Matches any single character
  111. ``\<char>``
  112. Matches the single character specified by ``<char>``. Use this to
  113. match special regex characters, e.g. ``\.`` for a literal ``.``
  114. or ``\\`` for a literal backslash ``\``. Escaping a non-special
  115. character is unnecessary but allowed, e.g. ``\a`` matches ``a``.
  116. ``[ ]``
  117. Matches any character(s) inside the brackets.
  118. To match a literal ``]``, make it the first character, e.g., ``[]ab]``.
  119. ``[^ ]``
  120. Matches any character(s) not inside the brackets.
  121. To not match a literal ``]``, make it the first character, e.g., ``[^]ab]``.
  122. ``-``
  123. Inside brackets, specifies an inclusive range between characters on
  124. either side, e.g., ``[a-f]`` is ``[abcdef]``.
  125. To match a literal ``-`` using brackets, make it the first or the last
  126. character, e.g., ``[+*/-]`` matches basic mathematical operators.
  127. ``*``
  128. Matches preceding pattern zero or more times
  129. ``+``
  130. Matches preceding pattern one or more times
  131. ``?``
  132. Matches preceding pattern zero or once only
  133. ``|``
  134. Matches a pattern on either side of the ``|``
  135. ``()``
  136. Saves a matched subexpression, which can be referenced
  137. in the ``REGEX REPLACE`` operation.
  138. .. versionadded:: 3.9
  139. All regular expression-related commands, including e.g.
  140. :command:`if(MATCHES)`, save subgroup matches in the variables
  141. :variable:`CMAKE_MATCH_<n>` for ``<n>`` 0..9.
  142. .. noqa: spellcheck off
  143. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  144. has lower precedence than concatenation. This means that the regular
  145. expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
  146. expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
  147. .. noqa: spellcheck on
  148. CMake language :ref:`Escape Sequences` such as ``\t``, ``\r``, ``\n``,
  149. and ``\\`` may be used to construct literal tabs, carriage returns,
  150. newlines, and backslashes (respectively) to pass in a regex. For example:
  151. * The quoted argument ``"[ \t\r\n]"`` specifies a regex that matches
  152. any single whitespace character.
  153. * The quoted argument ``"[/\\]"`` specifies a regex that matches
  154. a single forward slash ``/`` or backslash ``\``.
  155. * The quoted argument ``"[A-Za-z0-9_]"`` specifies a regex that matches
  156. any single "word" character in the C locale.
  157. * The quoted argument ``"\\(\\a\\+b\\)"`` specifies a regex that matches
  158. the exact string ``(a+b)``. Each ``\\`` is parsed in a quoted argument
  159. as just ``\``, so the regex itself is actually ``\(\a\+\b\)``. This
  160. can alternatively be specified in a :ref:`bracket argument` without
  161. having to escape the backslashes, e.g. ``[[\(\a\+\b\)]]``.
  162. Manipulation
  163. ^^^^^^^^^^^^
  164. .. signature::
  165. string(APPEND <string_variable> [<input>...])
  166. .. versionadded:: 3.4
  167. Append all the ``<input>`` arguments to the string.
  168. .. signature::
  169. string(PREPEND <string_variable> [<input>...])
  170. .. versionadded:: 3.10
  171. Prepend all the ``<input>`` arguments to the string.
  172. .. signature::
  173. string(CONCAT <output_variable> [<input>...])
  174. Concatenate all the ``<input>`` arguments together and store
  175. the result in the named ``<output_variable>``.
  176. .. signature::
  177. string(JOIN <glue> <output_variable> [<input>...])
  178. .. versionadded:: 3.12
  179. Join all the ``<input>`` arguments together using the ``<glue>``
  180. string and store the result in the named ``<output_variable>``.
  181. To join a list's elements, prefer to use the ``JOIN`` operator
  182. from the :command:`list` command. This allows for the elements to have
  183. special characters like ``;`` in them.
  184. .. signature::
  185. string(TOLOWER <string> <output_variable>)
  186. Convert ``<string>`` to lower characters.
  187. .. signature::
  188. string(TOUPPER <string> <output_variable>)
  189. Convert ``<string>`` to upper characters.
  190. .. signature::
  191. string(LENGTH <string> <output_variable>)
  192. Store in an ``<output_variable>`` a given string's length in bytes.
  193. Note that this means if ``<string>`` contains multi-byte characters,
  194. the result stored in ``<output_variable>`` will *not* be
  195. the number of characters.
  196. .. signature::
  197. string(SUBSTRING <string> <begin> <length> <output_variable>)
  198. Store in an ``<output_variable>`` a substring of a given ``<string>``. If
  199. ``<length>`` is ``-1`` the remainder of the string starting at ``<begin>``
  200. will be returned.
  201. .. versionchanged:: 3.2
  202. If ``<string>`` is shorter than ``<length>``
  203. then the end of the string is used instead.
  204. Previous versions of CMake reported an error in this case.
  205. Both ``<begin>`` and ``<length>`` are counted in bytes, so care must
  206. be exercised if ``<string>`` could contain multi-byte characters.
  207. .. signature::
  208. string(STRIP <string> <output_variable>)
  209. Store in an ``<output_variable>`` a substring of a given ``<string>``
  210. with leading and trailing spaces removed.
  211. .. signature::
  212. string(GENEX_STRIP <string> <output_variable>)
  213. .. versionadded:: 3.1
  214. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  215. from the input ``<string>`` and store the result
  216. in the ``<output_variable>``.
  217. .. signature::
  218. string(REPEAT <string> <count> <output_variable>)
  219. .. versionadded:: 3.15
  220. Produce the output string as the input ``<string>``
  221. repeated ``<count>`` times.
  222. Comparison
  223. ^^^^^^^^^^
  224. .. _COMPARE:
  225. .. signature::
  226. string(COMPARE LESS <string1> <string2> <output_variable>)
  227. string(COMPARE GREATER <string1> <string2> <output_variable>)
  228. string(COMPARE EQUAL <string1> <string2> <output_variable>)
  229. string(COMPARE NOTEQUAL <string1> <string2> <output_variable>)
  230. string(COMPARE LESS_EQUAL <string1> <string2> <output_variable>)
  231. string(COMPARE GREATER_EQUAL <string1> <string2> <output_variable>)
  232. Compare the strings and store true or false in the ``<output_variable>``.
  233. .. versionadded:: 3.7
  234. Added the ``LESS_EQUAL`` and ``GREATER_EQUAL`` options.
  235. .. _`Supported Hash Algorithms`:
  236. Hashing
  237. ^^^^^^^
  238. .. signature::
  239. string(<HASH> <output_variable> <input>)
  240. :target: <HASH>
  241. Compute a cryptographic hash of the ``<input>`` string.
  242. The supported ``<HASH>`` algorithm names are:
  243. ``MD5``
  244. Message-Digest Algorithm 5, RFC 1321.
  245. ``SHA1``
  246. US Secure Hash Algorithm 1, RFC 3174.
  247. ``SHA224``
  248. US Secure Hash Algorithms, RFC 4634.
  249. ``SHA256``
  250. US Secure Hash Algorithms, RFC 4634.
  251. ``SHA384``
  252. US Secure Hash Algorithms, RFC 4634.
  253. ``SHA512``
  254. US Secure Hash Algorithms, RFC 4634.
  255. ``SHA3_224``
  256. Keccak SHA-3.
  257. ``SHA3_256``
  258. Keccak SHA-3.
  259. ``SHA3_384``
  260. Keccak SHA-3.
  261. ``SHA3_512``
  262. Keccak SHA-3.
  263. .. versionadded:: 3.8
  264. Added the ``SHA3_*`` hash algorithms.
  265. Generation
  266. ^^^^^^^^^^
  267. .. signature::
  268. string(ASCII <number> [<number> ...] <output_variable>)
  269. Convert all numbers into corresponding ASCII characters.
  270. .. signature::
  271. string(HEX <string> <output_variable>)
  272. .. versionadded:: 3.18
  273. Convert each byte in the input ``<string>`` to its hexadecimal representation
  274. and store the concatenated hex digits in the ``<output_variable>``.
  275. Letters in the output (``a`` through ``f``) are in lowercase.
  276. .. signature::
  277. string(CONFIGURE <string> <output_variable>
  278. [@ONLY] [ESCAPE_QUOTES])
  279. Transform a ``<string>`` like :command:`configure_file` transforms a file.
  280. .. signature::
  281. string(MAKE_C_IDENTIFIER <string> <output_variable>)
  282. Convert each non-alphanumeric character in the input ``<string>`` to an
  283. underscore and store the result in the ``<output_variable>``. If the first
  284. character of the ``<string>`` is a digit, an underscore will also be
  285. prepended to the result.
  286. .. signature::
  287. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  288. [RANDOM_SEED <seed>] <output_variable>)
  289. Return a random string of given ``<length>`` consisting of
  290. characters from the given ``<alphabet>``. Default length is 5 characters
  291. and default alphabet is all numbers and upper and lower case letters.
  292. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  293. random number generator.
  294. .. signature::
  295. string(TIMESTAMP <output_variable> [<format_string>] [UTC])
  296. Write a string representation of the current date
  297. and/or time to the ``<output_variable>``.
  298. If the command is unable to obtain a timestamp, the ``<output_variable>``
  299. will be set to the empty string ``""``.
  300. The optional ``UTC`` flag requests the current date/time representation to
  301. be in Coordinated Universal Time (UTC) rather than local time.
  302. The optional ``<format_string>`` may contain the following format
  303. specifiers:
  304. ``%%``
  305. .. versionadded:: 3.8
  306. A literal percent sign (%).
  307. ``%d``
  308. The day of the current month (01-31).
  309. ``%H``
  310. The hour on a 24-hour clock (00-23).
  311. ``%I``
  312. The hour on a 12-hour clock (01-12).
  313. ``%j``
  314. The day of the current year (001-366).
  315. ``%m``
  316. The month of the current year (01-12).
  317. ``%b``
  318. .. versionadded:: 3.7
  319. Abbreviated month name (e.g. Oct).
  320. ``%B``
  321. .. versionadded:: 3.10
  322. Full month name (e.g. October).
  323. ``%M``
  324. The minute of the current hour (00-59).
  325. ``%s``
  326. .. versionadded:: 3.6
  327. Seconds since midnight (UTC) 1-Jan-1970 (UNIX time).
  328. ``%S``
  329. The second of the current minute. 60 represents a leap second. (00-60)
  330. ``%f``
  331. .. versionadded:: 3.23
  332. The microsecond of the current second (000000-999999).
  333. ``%U``
  334. The week number of the current year (00-53).
  335. ``%V``
  336. .. versionadded:: 3.22
  337. The ISO 8601 week number of the current year (01-53).
  338. ``%w``
  339. The day of the current week. 0 is Sunday. (0-6)
  340. ``%a``
  341. .. versionadded:: 3.7
  342. Abbreviated weekday name (e.g. Fri).
  343. ``%A``
  344. .. versionadded:: 3.10
  345. Full weekday name (e.g. Friday).
  346. ``%y``
  347. The last two digits of the current year (00-99).
  348. ``%Y``
  349. The current year.
  350. ``%z``
  351. .. versionadded:: 3.26
  352. The offset of the time zone from UTC, in hours and minutes,
  353. with format ``+hhmm`` or ``-hhmm``.
  354. ``%Z``
  355. .. versionadded:: 3.26
  356. The time zone name.
  357. Unknown format specifiers will be ignored and copied to the output
  358. as-is.
  359. If no explicit ``<format_string>`` is given, it will default to:
  360. * ``%Y-%m-%dT%H:%M:%S`` for local time.
  361. * ``%Y-%m-%dT%H:%M:%SZ`` for UTC.
  362. .. versionadded:: 3.8
  363. If the ``SOURCE_DATE_EPOCH`` environment variable is set,
  364. its value will be used instead of the current time.
  365. See https://reproducible-builds.org/specs/source-date-epoch/ for details.
  366. .. signature::
  367. string(UUID <output_variable> NAMESPACE <namespace> NAME <name>
  368. TYPE <MD5|SHA1> [UPPER])
  369. .. versionadded:: 3.1
  370. Create a universally unique identifier (aka GUID) as per RFC4122
  371. based on the hash of the combined values of ``<namespace>``
  372. (which itself has to be a valid UUID) and ``<name>``.
  373. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  374. ``SHA1`` (Version 5 UUID).
  375. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  376. where each ``x`` represents a lower case hexadecimal character.
  377. Where required, an uppercase representation can be requested
  378. with the optional ``UPPER`` flag.
  379. .. _JSON:
  380. JSON
  381. ^^^^
  382. .. versionadded:: 3.19
  383. Functionality for querying a JSON string.
  384. .. note::
  385. In each of the following JSON-related subcommands, if the optional
  386. ``ERROR_VARIABLE`` argument is given, errors will be reported in
  387. ``<error-variable>`` and the ``<out-var>`` will be set to
  388. ``<member|index>-[<member|index>...]-NOTFOUND`` with the path elements
  389. up to the point where the error occurred, or just ``NOTFOUND`` if there
  390. is no relevant path. If an error occurs but the ``ERROR_VARIABLE``
  391. option is not present, a fatal error message is generated. If no error
  392. occurs, the ``<error-variable>`` will be set to ``NOTFOUND``.
  393. In the following subcommands, the ``<json-string>`` argument should
  394. be written as a :ref:`Quoted Argument` to ensure the entire JSON
  395. string is passed as a single argument even if it contains semicolons.
  396. .. signature::
  397. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  398. GET <json-string> <member|index> [<member|index> ...])
  399. :target: JSON GET
  400. Get an element from ``<json-string>`` at the location given
  401. by the list of ``<member|index>`` arguments.
  402. Array and object elements will be returned as a JSON string.
  403. Boolean elements will be returned as ``ON`` or ``OFF``.
  404. Null elements will be returned as an empty string.
  405. Number and string types will be returned as strings.
  406. .. signature::
  407. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  408. TYPE <json-string> <member|index> [<member|index> ...])
  409. :target: JSON TYPE
  410. Get the type of an element in ``<json-string>`` at the location
  411. given by the list of ``<member|index>`` arguments. The ``<out-var>``
  412. will be set to one of ``NULL``, ``NUMBER``, ``STRING``, ``BOOLEAN``,
  413. ``ARRAY``, or ``OBJECT``.
  414. .. signature::
  415. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  416. MEMBER <json-string>
  417. [<member|index> ...] <index>)
  418. :target: JSON MEMBER
  419. Get the name of the ``<index>``-th member in ``<json-string>``
  420. at the location given by the list of ``<member|index>`` arguments.
  421. Requires an element of object type.
  422. .. signature::
  423. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  424. LENGTH <json-string> [<member|index> ...])
  425. :target: JSON LENGTH
  426. Get the length of an element in ``<json-string>`` at the location
  427. given by the list of ``<member|index>`` arguments.
  428. Requires an element of array or object type.
  429. .. signature::
  430. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  431. REMOVE <json-string> <member|index> [<member|index> ...])
  432. :target: JSON REMOVE
  433. Remove an element from ``<json-string>`` at the location
  434. given by the list of ``<member|index>`` arguments. The JSON string
  435. without the removed element will be stored in ``<out-var>``.
  436. .. signature::
  437. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  438. SET <json-string> <member|index> [<member|index> ...] <value>)
  439. :target: JSON SET
  440. Set an element in ``<json-string>`` at the location
  441. given by the list of ``<member|index>`` arguments to ``<value>``.
  442. The contents of ``<value>`` should be valid JSON.
  443. If ``<json-string>`` is an array, ``<value>`` can be appended to the end of
  444. the array by using a number greater or equal to the array length as the
  445. ``<member|index>`` argument.
  446. .. signature::
  447. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  448. EQUAL <json-string1> <json-string2>)
  449. :target: JSON EQUAL
  450. Compare the two JSON objects given by ``<json-string1>``
  451. and ``<json-string2>`` for equality. The contents of ``<json-string1>``
  452. and ``<json-string2>`` should be valid JSON. The ``<out-var>``
  453. will be set to a true value if the JSON objects are considered equal,
  454. or a false value otherwise.