string.rst 12 KB

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