FindSubversion.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindSubversion
  5. --------------
  6. Finds a Subversion command-line client executable (``svn``) and provides
  7. commands for extracting information from a Subversion working copy:
  8. .. code-block:: cmake
  9. find_package(Subversion [<version>] [...])
  10. Result Variables
  11. ^^^^^^^^^^^^^^^^
  12. This module defines the following variables:
  13. ``Subversion_FOUND``
  14. Boolean indicating whether (the requested version of) Subversion command-line
  15. client is found. For backward compatibility, the ``SUBVERSION_FOUND``
  16. variable is also set to the same value.
  17. ``Subversion_VERSION``
  18. .. versionadded:: 4.2
  19. Version of the ``svn`` command-line client found.
  20. Cache Variables
  21. ^^^^^^^^^^^^^^^
  22. The following cache variables may also be set:
  23. ``Subversion_SVN_EXECUTABLE``
  24. Path to the ``svn`` command-line client.
  25. Commands
  26. ^^^^^^^^
  27. This module provides the following commands if the Subversion command-line
  28. client is found:
  29. .. command:: Subversion_WC_INFO
  30. Extracts information from a Subversion working copy located at a specified
  31. directory:
  32. .. code-block:: cmake
  33. Subversion_WC_INFO(<dir> <var-prefix> [IGNORE_SVN_FAILURE])
  34. This command defines the following variables if running Subversion's ``info``
  35. subcommand on ``<dir>`` succeeds; otherwise a ``SEND_ERROR`` message is
  36. generated:
  37. ``<var-prefix>_WC_URL``
  38. URL of the repository (at ``<dir>``).
  39. ``<var-prefix>_WC_ROOT``
  40. Root URL of the repository.
  41. ``<var-prefix>_WC_REVISION``
  42. Current revision.
  43. ``<var-prefix>_WC_LAST_CHANGED_AUTHOR``
  44. Author of last commit.
  45. ``<var-prefix>_WC_LAST_CHANGED_DATE``
  46. Date of last commit.
  47. ``<var-prefix>_WC_LAST_CHANGED_REV``
  48. Revision of last commit.
  49. ``<var-prefix>_WC_INFO``
  50. Output of the command ``svn info <dir>``
  51. The options are:
  52. ``IGNORE_SVN_FAILURE``
  53. .. versionadded:: 3.13
  54. When specified, errors from Subversion operation will not trigger a
  55. ``SEND_ERROR`` message. In case of an error, the ``<var-prefix>_*``
  56. variables remain undefined.
  57. .. command:: Subversion_WC_LOG
  58. Retrieves the log message of the base revision of a Subversion working copy at
  59. a given location:
  60. .. code-block:: cmake
  61. Subversion_WC_LOG(<dir> <var-prefix>)
  62. This command defines the following variable if running Subversion's ``log``
  63. subcommand on ``<dir>`` succeeds; otherwise a ``SEND_ERROR`` message is
  64. generated:
  65. ``<var-prefix>_LAST_CHANGED_LOG``
  66. Last log of the base revision of a Subversion working copy located at
  67. ``<dir>``.
  68. Deprecated Variables
  69. ^^^^^^^^^^^^^^^^^^^^
  70. The following variables are provided for backward compatibility:
  71. ``Subversion_VERSION_SVN``
  72. .. deprecated:: 4.2
  73. Use the ``Subversion_VERSION``.
  74. Version of the ``svn`` command-line client found.
  75. Examples
  76. ^^^^^^^^
  77. Examples: Finding Subversion
  78. """"""""""""""""""""""""""""
  79. Finding Subversion:
  80. .. code-block:: cmake
  81. find_package(Subversion)
  82. Or, finding Subversion and specifying a minimum required version:
  83. .. code-block:: cmake
  84. find_package(Subversion 1.4)
  85. Or, finding Subversion and making it required (if not found, processing stops
  86. with an error message):
  87. .. code-block:: cmake
  88. find_package(Subversion REQUIRED)
  89. Example: Using Subversion
  90. """""""""""""""""""""""""
  91. Finding Subversion and retrieving information about the current project's
  92. working copy:
  93. .. code-block:: cmake
  94. find_package(Subversion)
  95. if(Subversion_FOUND)
  96. Subversion_WC_INFO(${PROJECT_SOURCE_DIR} Project)
  97. message("Current revision is ${Project_WC_REVISION}")
  98. Subversion_WC_LOG(${PROJECT_SOURCE_DIR} Project)
  99. message("Last changed log is ${Project_LAST_CHANGED_LOG}")
  100. endif()
  101. #]=======================================================================]
  102. find_program(Subversion_SVN_EXECUTABLE svn
  103. PATHS
  104. [HKEY_LOCAL_MACHINE\\Software\\TortoiseSVN;Directory]/bin
  105. DOC "subversion command line client")
  106. mark_as_advanced(Subversion_SVN_EXECUTABLE)
  107. if(Subversion_SVN_EXECUTABLE)
  108. # the subversion commands should be executed with the C locale, otherwise
  109. # the message (which are parsed) may be translated, Alex
  110. set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
  111. set(ENV{LC_ALL} C)
  112. execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} --version
  113. OUTPUT_VARIABLE Subversion_VERSION_SVN
  114. ERROR_VARIABLE _Subversion_VERSION_STDERR
  115. RESULT_VARIABLE _Subversion_VERSION_RESULT
  116. OUTPUT_STRIP_TRAILING_WHITESPACE)
  117. # restore the previous LC_ALL
  118. set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
  119. if(_Subversion_VERSION_RESULT EQUAL 0)
  120. string(REGEX REPLACE "^(.*\n)?svn, version ([.0-9]+).*"
  121. "\\2" Subversion_VERSION_SVN "${Subversion_VERSION_SVN}")
  122. else()
  123. unset(Subversion_VERSION_SVN)
  124. if(_Subversion_VERSION_STDERR MATCHES "svn: error: The subversion command line tools are no longer provided by Xcode")
  125. set(Subversion_SVN_EXECUTABLE Subversion_SVN_EXECUTABLE-NOTFOUND)
  126. endif()
  127. endif()
  128. if(DEFINED Subversion_VERSION_SVN)
  129. set(Subversion_VERSION "${Subversion_VERSION_SVN}")
  130. else()
  131. unset(Subversion_VERSION)
  132. endif()
  133. macro(Subversion_WC_INFO dir prefix)
  134. cmake_parse_arguments(
  135. "Subversion_WC_INFO"
  136. "IGNORE_SVN_FAILURE"
  137. "" ""
  138. ${ARGN}
  139. )
  140. # the subversion commands should be executed with the C locale, otherwise
  141. # the message (which are parsed) may be translated, Alex
  142. set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
  143. set(ENV{LC_ALL} C)
  144. execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} info ${dir}
  145. OUTPUT_VARIABLE ${prefix}_WC_INFO
  146. ERROR_VARIABLE Subversion_svn_info_error
  147. RESULT_VARIABLE Subversion_svn_info_result
  148. OUTPUT_STRIP_TRAILING_WHITESPACE)
  149. if(${Subversion_svn_info_result} EQUAL 0)
  150. string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
  151. "\\2" ${prefix}_WC_URL "${${prefix}_WC_INFO}")
  152. string(REGEX REPLACE "^(.*\n)?Repository Root: ([^\n]+).*"
  153. "\\2" ${prefix}_WC_ROOT "${${prefix}_WC_INFO}")
  154. string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
  155. "\\2" ${prefix}_WC_REVISION "${${prefix}_WC_INFO}")
  156. string(REGEX REPLACE "^(.*\n)?Last Changed Author: ([^\n]+).*"
  157. "\\2" ${prefix}_WC_LAST_CHANGED_AUTHOR "${${prefix}_WC_INFO}")
  158. string(REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*"
  159. "\\2" ${prefix}_WC_LAST_CHANGED_REV "${${prefix}_WC_INFO}")
  160. string(REGEX REPLACE "^(.*\n)?Last Changed Date: ([^\n]+).*"
  161. "\\2" ${prefix}_WC_LAST_CHANGED_DATE "${${prefix}_WC_INFO}")
  162. elseif(NOT Subversion_WC_INFO_IGNORE_SVN_FAILURE)
  163. message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} info ${dir}\" failed with output:\n${Subversion_svn_info_error}")
  164. endif()
  165. # restore the previous LC_ALL
  166. set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
  167. endmacro()
  168. macro(Subversion_WC_LOG dir prefix)
  169. # This macro can block if the certificate is not signed:
  170. # svn ask you to accept the certificate and wait for your answer
  171. # This macro requires a svn server network access (Internet most of the time)
  172. # and can also be slow since it access the svn server
  173. execute_process(COMMAND
  174. ${Subversion_SVN_EXECUTABLE} --non-interactive log -r BASE ${dir}
  175. OUTPUT_VARIABLE ${prefix}_LAST_CHANGED_LOG
  176. ERROR_VARIABLE Subversion_svn_log_error
  177. RESULT_VARIABLE Subversion_svn_log_result
  178. OUTPUT_STRIP_TRAILING_WHITESPACE)
  179. if(NOT ${Subversion_svn_log_result} EQUAL 0)
  180. message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} log -r BASE ${dir}\" failed with output:\n${Subversion_svn_log_error}")
  181. endif()
  182. endmacro()
  183. endif()
  184. include(FindPackageHandleStandardArgs)
  185. find_package_handle_standard_args(Subversion REQUIRED_VARS Subversion_SVN_EXECUTABLE
  186. VERSION_VAR Subversion_VERSION)
  187. # for compatibility
  188. set(Subversion_SVN_FOUND ${Subversion_FOUND})