string.rst 13 KB

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