string.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. string
  2. ------
  3. String operations.
  4. ::
  5. string(REGEX MATCH <regular_expression>
  6. <output variable> <input> [<input>...])
  7. string(REGEX MATCHALL <regular_expression>
  8. <output variable> <input> [<input>...])
  9. string(REGEX REPLACE <regular_expression>
  10. <replace_expression> <output variable>
  11. <input> [<input>...])
  12. string(REPLACE <match_string>
  13. <replace_string> <output variable>
  14. <input> [<input>...])
  15. string(CONCAT <output variable> [<input>...])
  16. string(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512>
  17. <output variable> <input>)
  18. string(COMPARE EQUAL <string1> <string2> <output variable>)
  19. string(COMPARE NOTEQUAL <string1> <string2> <output variable>)
  20. string(COMPARE LESS <string1> <string2> <output variable>)
  21. string(COMPARE GREATER <string1> <string2> <output variable>)
  22. string(ASCII <number> [<number> ...] <output variable>)
  23. string(CONFIGURE <string1> <output variable>
  24. [@ONLY] [ESCAPE_QUOTES])
  25. string(TOUPPER <string1> <output variable>)
  26. string(TOLOWER <string1> <output variable>)
  27. string(LENGTH <string> <output variable>)
  28. string(SUBSTRING <string> <begin> <length> <output variable>)
  29. string(STRIP <string> <output variable>)
  30. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  31. [RANDOM_SEED <seed>] <output variable>)
  32. string(FIND <string> <substring> <output variable> [REVERSE])
  33. string(TIMESTAMP <output variable> [<format string>] [UTC])
  34. string(MAKE_C_IDENTIFIER <input string> <output variable>)
  35. string(GENEX_STRIP <input string> <output variable>)
  36. string(UUID <output variable> NAMESPACE <namespace> NAME <name>
  37. TYPE <MD5|SHA1> [UPPER])
  38. ``REGEX MATCH`` will match the regular expression once and store the match
  39. in the output variable.
  40. ``REGEX MATCHALL`` will match the regular expression as many times as
  41. possible and store the matches in the output variable as a list.
  42. ``REGEX REPLACE`` will match the regular expression as many times as
  43. possible and substitute the replacement expression for the match in
  44. the output. The replace expression may refer to paren-delimited
  45. subexpressions of the match using \1, \2, ..., \9. Note that two
  46. backslashes (\\1) are required in CMake code to get a backslash
  47. through argument parsing.
  48. ``REPLACE`` will replace all occurrences of ``match_string`` in the input
  49. with ``replace_string`` and store the result in the output.
  50. ``CONCAT`` will concatenate all the input arguments together and store
  51. the result in the named output variable.
  52. ``MD5``, ``SHA1``, ``SHA224``, ``SHA256``, ``SHA384``, and ``SHA512`` will
  53. compute a cryptographic hash of the input string.
  54. ``COMPARE EQUAL``/``COMPARE NOTEQUAL``/``COMPARE LESS/GREATER`` will
  55. compare the strings and store true or false in the output variable.
  56. ``ASCII`` will convert all numbers into corresponding ASCII characters.
  57. ``CONFIGURE`` will transform a string like :command:`configure_file`
  58. transforms a file.
  59. ``TOUPPER``/``TOLOWER`` will convert string to upper/lower characters.
  60. ``LENGTH`` will return a given string's length.
  61. ``SUBSTRING`` will return a substring of a given string. If length is -1
  62. the remainder of the string starting at begin will be returned.
  63. If string is shorter than length then end of string is used instead.
  64. .. note::
  65. CMake 3.1 and below reported an error if length pointed past
  66. the end of string.
  67. ``STRIP`` will return a substring of a given string with leading and
  68. trailing spaces removed.
  69. ``RANDOM`` will return a random string of given length consisting of
  70. characters from the given alphabet. Default length is 5 characters
  71. and default alphabet is all numbers and upper and lower case letters.
  72. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  73. random number generator.
  74. ``FIND`` will return the position where the given substring was found in
  75. the supplied string. If the ``REVERSE`` flag was used, the command will
  76. search for the position of the last occurrence of the specified
  77. substring.
  78. The following characters have special meaning in regular expressions:
  79. ::
  80. ^ Matches at beginning of input
  81. $ Matches at end of input
  82. . Matches any single character
  83. [ ] Matches any character(s) inside the brackets
  84. [^ ] Matches any character(s) not inside the brackets
  85. - Inside brackets, specifies an inclusive range between
  86. characters on either side e.g. [a-f] is [abcdef]
  87. To match a literal - using brackets, make it the first
  88. or the last character e.g. [+*/-] matches basic
  89. mathematical operators.
  90. * Matches preceding pattern zero or more times
  91. + Matches preceding pattern one or more times
  92. ? Matches preceding pattern zero or once only
  93. | Matches a pattern on either side of the |
  94. () Saves a matched subexpression, which can be referenced
  95. in the REGEX REPLACE operation. Additionally it is saved
  96. by all regular expression-related commands, including
  97. e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).
  98. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  99. has lower precedence than concatenation. This means that the regular
  100. expression "^ab+d$" matches "abbd" but not "ababd", and the regular
  101. expression "^(ab|cd)$" matches "ab" but not "abd".
  102. ``TIMESTAMP`` will write a string representation of the current date
  103. and/or time to the output variable.
  104. Should the command be unable to obtain a timestamp the output variable
  105. will be set to the empty string "".
  106. The optional ``UTC`` flag requests the current date/time representation to
  107. be in Coordinated Universal Time (UTC) rather than local time.
  108. The optional ``<format string>`` may contain the following format
  109. specifiers:
  110. ::
  111. %d The day of the current month (01-31).
  112. %H The hour on a 24-hour clock (00-23).
  113. %I The hour on a 12-hour clock (01-12).
  114. %j The day of the current year (001-366).
  115. %m The month of the current year (01-12).
  116. %M The minute of the current hour (00-59).
  117. %S The second of the current minute.
  118. 60 represents a leap second. (00-60)
  119. %U The week number of the current year (00-53).
  120. %w The day of the current week. 0 is Sunday. (0-6)
  121. %y The last two digits of the current year (00-99)
  122. %Y The current year.
  123. Unknown format specifiers will be ignored and copied to the output
  124. as-is.
  125. If no explicit ``<format string>`` is given it will default to:
  126. ::
  127. %Y-%m-%dT%H:%M:%S for local time.
  128. %Y-%m-%dT%H:%M:%SZ for UTC.
  129. ``MAKE_C_IDENTIFIER`` will write a string which can be used as an
  130. identifier in C.
  131. ``GENEX_STRIP`` will strip any
  132. :manual:`generator expressions <cmake-generator-expressions(7)>` from the
  133. ``input string`` and store the result in the ``output variable``.
  134. ``UUID`` creates a univerally unique identifier (aka GUID) as per RFC4122
  135. based on the hash of the combined values of ``<namespace>``
  136. (which itself has to be a valid UUID) and ``<name>``.
  137. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  138. ``SHA1`` (Version 5 UUID).
  139. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  140. where each `x` represents a lower case hexadecimal character.
  141. Where required an uppercase representation can be requested
  142. with the optional ``UPPER`` flag.