string.rst 7.3 KB

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