string.rst 18 KB

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