1
0

string.rst 11 KB

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