string.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. Additionally it is saved
  136. by all regular expression-related commands, including
  137. e.g. :command:`if(MATCHES)`, in the variables
  138. :variable:`CMAKE_MATCH_<n>` for ``<n>`` 0..9.
  139. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  140. has lower precedence than concatenation. This means that the regular
  141. expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
  142. expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
  143. CMake language :ref:`Escape Sequences` such as ``\t``, ``\r``, ``\n``,
  144. and ``\\`` may be used to construct literal tabs, carriage returns,
  145. newlines, and backslashes (respectively) to pass in a regex. For example:
  146. * The quoted argument ``"[ \t\r\n]"`` specifies a regex that matches
  147. any single whitespace character.
  148. * The quoted argument ``"[/\\]"`` specifies a regex that matches
  149. a single forward slash ``/`` or backslash ``\``.
  150. * The quoted argument ``"[A-Za-z0-9_]"`` specifies a regex that matches
  151. any single "word" character in the C locale.
  152. * The quoted argument ``"\\(\\a\\+b\\)"`` specifies a regex that matches
  153. the exact string ``(a+b)``. Each ``\\`` is parsed in a quoted argument
  154. as just ``\``, so the regex itself is actually ``\(\a\+\b\)``. This
  155. can alternatively be specified in a :ref:`bracket argument` without
  156. having to escape the backslashes, e.g. ``[[\(\a\+\b\)]]``.
  157. Manipulation
  158. ^^^^^^^^^^^^
  159. .. _APPEND:
  160. .. code-block:: cmake
  161. string(APPEND <string_variable> [<input>...])
  162. Append all the ``<input>`` arguments to the string.
  163. .. _PREPEND:
  164. .. code-block:: cmake
  165. string(PREPEND <string_variable> [<input>...])
  166. Prepend all the ``<input>`` arguments to the string.
  167. .. _CONCAT:
  168. .. code-block:: cmake
  169. string(CONCAT <output_variable> [<input>...])
  170. Concatenate all the ``<input>`` arguments together and store
  171. the result in the named ``<output_variable>``.
  172. .. _JOIN:
  173. .. code-block:: cmake
  174. string(JOIN <glue> <output_variable> [<input>...])
  175. Join all the ``<input>`` arguments together using the ``<glue>``
  176. string and store the result in the named ``<output_variable>``.
  177. To join a list's elements, prefer to use the ``JOIN`` operator
  178. from the :command:`list` command. This allows for the elements to have
  179. special characters like ``;`` in them.
  180. .. _TOLOWER:
  181. .. code-block:: cmake
  182. string(TOLOWER <string> <output_variable>)
  183. Convert ``<string>`` to lower characters.
  184. .. _TOUPPER:
  185. .. code-block:: cmake
  186. string(TOUPPER <string> <output_variable>)
  187. Convert ``<string>`` to upper characters.
  188. .. _LENGTH:
  189. .. code-block:: cmake
  190. string(LENGTH <string> <output_variable>)
  191. Store in an ``<output_variable>`` a given string's length in bytes.
  192. Note that this means if ``<string>`` contains multi-byte characters, the
  193. result stored in ``<output_variable>`` will *not* be the number of characters.
  194. .. _SUBSTRING:
  195. .. code-block:: cmake
  196. string(SUBSTRING <string> <begin> <length> <output_variable>)
  197. Store in an ``<output_variable>`` a substring of a given ``<string>``. If
  198. ``<length>`` is ``-1`` the remainder of the string starting at ``<begin>``
  199. will be returned. If ``<string>`` is shorter than ``<length>`` then the
  200. end of the string is used instead.
  201. Both ``<begin>`` and ``<length>`` are counted in bytes, so care must
  202. be exercised if ``<string>`` could contain multi-byte characters.
  203. .. note::
  204. CMake 3.1 and below reported an error if ``<length>`` pointed past
  205. the end of ``<string>``.
  206. .. _STRIP:
  207. .. code-block:: cmake
  208. string(STRIP <string> <output_variable>)
  209. Store in an ``<output_variable>`` a substring of a given ``<string>`` with
  210. leading and trailing spaces removed.
  211. .. _GENEX_STRIP:
  212. .. code-block:: cmake
  213. string(GENEX_STRIP <string> <output_variable>)
  214. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  215. from the input ``<string>`` and store the result in the ``<output_variable>``.
  216. .. _REPEAT:
  217. .. code-block:: cmake
  218. string(REPEAT <string> <count> <output_variable>)
  219. Produce the output string as the input ``<string>`` repeated ``<count>`` times.
  220. Comparison
  221. ^^^^^^^^^^
  222. .. _COMPARE:
  223. .. code-block:: cmake
  224. string(COMPARE LESS <string1> <string2> <output_variable>)
  225. string(COMPARE GREATER <string1> <string2> <output_variable>)
  226. string(COMPARE EQUAL <string1> <string2> <output_variable>)
  227. string(COMPARE NOTEQUAL <string1> <string2> <output_variable>)
  228. string(COMPARE LESS_EQUAL <string1> <string2> <output_variable>)
  229. string(COMPARE GREATER_EQUAL <string1> <string2> <output_variable>)
  230. Compare the strings and store true or false in the ``<output_variable>``.
  231. .. _`Supported Hash Algorithms`:
  232. Hashing
  233. ^^^^^^^
  234. .. _`HASH`:
  235. .. code-block:: cmake
  236. string(<HASH> <output_variable> <input>)
  237. Compute a cryptographic hash of the ``<input>`` string.
  238. The supported ``<HASH>`` algorithm names are:
  239. ``MD5``
  240. Message-Digest Algorithm 5, RFC 1321.
  241. ``SHA1``
  242. US Secure Hash Algorithm 1, RFC 3174.
  243. ``SHA224``
  244. US Secure Hash Algorithms, RFC 4634.
  245. ``SHA256``
  246. US Secure Hash Algorithms, RFC 4634.
  247. ``SHA384``
  248. US Secure Hash Algorithms, RFC 4634.
  249. ``SHA512``
  250. US Secure Hash Algorithms, RFC 4634.
  251. ``SHA3_224``
  252. Keccak SHA-3.
  253. ``SHA3_256``
  254. Keccak SHA-3.
  255. ``SHA3_384``
  256. Keccak SHA-3.
  257. ``SHA3_512``
  258. Keccak SHA-3.
  259. Generation
  260. ^^^^^^^^^^
  261. .. _ASCII:
  262. .. code-block:: cmake
  263. string(ASCII <number> [<number> ...] <output_variable>)
  264. Convert all numbers into corresponding ASCII characters.
  265. .. _HEX:
  266. .. code-block:: cmake
  267. string(HEX <string> <output_variable>)
  268. Convert each byte in the input ``<string>`` to its hexadecimal representation
  269. and store the concatenated hex digits in the ``<output_variable>``. Letters in
  270. the output (``a`` through ``f``) are in lowercase.
  271. .. _CONFIGURE:
  272. .. code-block:: cmake
  273. string(CONFIGURE <string> <output_variable>
  274. [@ONLY] [ESCAPE_QUOTES])
  275. Transform a ``<string>`` like :command:`configure_file` transforms a file.
  276. .. _MAKE_C_IDENTIFIER:
  277. .. code-block:: cmake
  278. string(MAKE_C_IDENTIFIER <string> <output_variable>)
  279. Convert each non-alphanumeric character in the input ``<string>`` to an
  280. underscore and store the result in the ``<output_variable>``. If the first
  281. character of the ``<string>`` is a digit, an underscore will also be prepended
  282. to the result.
  283. .. _RANDOM:
  284. .. code-block:: cmake
  285. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  286. [RANDOM_SEED <seed>] <output_variable>)
  287. Return a random string of given ``<length>`` consisting of
  288. characters from the given ``<alphabet>``. Default length is 5 characters
  289. and default alphabet is all numbers and upper and lower case letters.
  290. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  291. random number generator.
  292. .. _TIMESTAMP:
  293. .. code-block:: cmake
  294. string(TIMESTAMP <output_variable> [<format_string>] [UTC])
  295. Write a string representation of the current date
  296. and/or time to the ``<output_variable>``.
  297. If the command is unable to obtain a timestamp, the ``<output_variable>``
  298. will be set to the empty string ``""``.
  299. The optional ``UTC`` flag requests the current date/time representation to
  300. be in Coordinated Universal Time (UTC) rather than local time.
  301. The optional ``<format_string>`` may contain the following format
  302. specifiers:
  303. ::
  304. %% A literal percent sign (%).
  305. %d The day of the current month (01-31).
  306. %H The hour on a 24-hour clock (00-23).
  307. %I The hour on a 12-hour clock (01-12).
  308. %j The day of the current year (001-366).
  309. %m The month of the current year (01-12).
  310. %b Abbreviated month name (e.g. Oct).
  311. %B Full month name (e.g. October).
  312. %M The minute of the current hour (00-59).
  313. %s Seconds since midnight (UTC) 1-Jan-1970 (UNIX time).
  314. %S The second of the current minute.
  315. 60 represents a leap second. (00-60)
  316. %U The week number of the current year (00-53).
  317. %w The day of the current week. 0 is Sunday. (0-6)
  318. %a Abbreviated weekday name (e.g. Fri).
  319. %A Full weekday name (e.g. Friday).
  320. %y The last two digits of the current year (00-99)
  321. %Y The current year.
  322. Unknown format specifiers will be ignored and copied to the output
  323. as-is.
  324. If no explicit ``<format_string>`` is given, it will default to:
  325. ::
  326. %Y-%m-%dT%H:%M:%S for local time.
  327. %Y-%m-%dT%H:%M:%SZ for UTC.
  328. .. note::
  329. If the ``SOURCE_DATE_EPOCH`` environment variable is set,
  330. its value will be used instead of the current time.
  331. See https://reproducible-builds.org/specs/source-date-epoch/ for details.
  332. .. _UUID:
  333. .. code-block:: cmake
  334. string(UUID <output_variable> NAMESPACE <namespace> NAME <name>
  335. TYPE <MD5|SHA1> [UPPER])
  336. Create a universally unique identifier (aka GUID) as per RFC4122
  337. based on the hash of the combined values of ``<namespace>``
  338. (which itself has to be a valid UUID) and ``<name>``.
  339. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  340. ``SHA1`` (Version 5 UUID).
  341. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  342. where each ``x`` represents a lower case hexadecimal character.
  343. Where required, an uppercase representation can be requested
  344. with the optional ``UPPER`` flag.
  345. JSON
  346. ^^^^
  347. .. _JSON:
  348. Functionality for querying a JSON string
  349. .. _GET:
  350. .. code-block:: cmake
  351. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  352. GET <json-string> <member|index> [<member|index> ...])
  353. Get an element from ``<json-string>`` at the location given
  354. by the list of ``<member|index>`` arguments.
  355. Array and object elements will be returned as a JSON string.
  356. Boolean elements will be returned as ``ON`` or ``OFF``.
  357. Null elements will be returned as an empty string.
  358. Number and string types will be returned as strings.
  359. .. _TYPE:
  360. .. code-block:: cmake
  361. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  362. TYPE <json-string> <member|index> [<member|index> ...])
  363. Get the type of an element in ``<json-string>`` at the location
  364. given by the list of ``<member|index>`` arguments. The ``<out-var>``
  365. will be set to one of ``NULL``, ``NUMBER``, ``STRING``, ``BOOLEAN``,
  366. ``ARRAY``, or ``OBJECT``.
  367. .. _MEMBER:
  368. .. code-block:: cmake
  369. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  370. MEMBER <json-string>
  371. [<member|index> ...] <index>)
  372. Get the name of the ``<index>``:th member in ``<json-string>`` at the location
  373. given by the list of ``<member|index>`` arguments.
  374. Requires an element of object type.
  375. .. _JSONLENGTH:
  376. .. code-block:: cmake
  377. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  378. LENGTH <json-string> <member|index> [<member|index> ...])
  379. Get the length of an element in ``<json-string>`` at the location
  380. given by the list of ``<member|index>`` arguments.
  381. Required an element of array or object type.
  382. .. _REMOVE:
  383. .. code-block:: cmake
  384. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  385. REMOVE <json-string> <member|index> [<member|index> ...])
  386. Remove an element from ``<json-string>`` at the location
  387. given by the list of ``<member|index>`` arguments. The JSON string
  388. without the removed element will we written in ``<out-var>``.
  389. .. _SET:
  390. .. code-block:: cmake
  391. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  392. SET <json-string> <member|index> [<member|index> ...] <value>)
  393. Set an element in ``<json-string>`` at the location
  394. given by the list of ``<member|index>`` arguments to ``<value>``.
  395. The contents of ``<value>`` should be valid JSON.
  396. .. _EQUAL:
  397. .. code-block:: cmake
  398. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  399. EQUAL <json-string1> <json-string2>)
  400. Compare the two JSON objects given by ``<json-string1>`` and ``<json-string2>``
  401. for equality
  402. If the optional ``ERROR_VARIABLE`` argument is given errors will be
  403. reported in ``<error-variable>``. If no error occurs the ``<error-variable>``
  404. will be set to ``NOTFOUND``. If ``ERROR_VARIABLE`` is not set a CMake error
  405. will be issued.
  406. When an error occurs the ``<out-var>`` will be set to
  407. ``<member|index>-[<member|index>...]-NOTFOUND`` with the path elements up to
  408. the point where the error occurred.