FindSubversion.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindSubversion
  5. --------------
  6. Extract information from a subversion working copy
  7. The module defines the following variables:
  8. ::
  9. Subversion_SVN_EXECUTABLE - path to svn command line client
  10. Subversion_VERSION_SVN - version of svn command line client
  11. Subversion_FOUND - true if the command line client was found
  12. SUBVERSION_FOUND - same as Subversion_FOUND, set for compatibility reasons
  13. The minimum required version of Subversion can be specified using the
  14. standard syntax, e.g. ``find_package(Subversion 1.4)``.
  15. If the command line client executable is found two macros are defined:
  16. ::
  17. Subversion_WC_INFO(<dir> <var-prefix> [IGNORE_SVN_FAILURE])
  18. Subversion_WC_LOG(<dir> <var-prefix>)
  19. ``Subversion_WC_INFO`` extracts information of a subversion working copy at a
  20. given location. This macro defines the following variables if running
  21. Subversion's ``info`` command on ``<dir>`` succeeds; otherwise a
  22. ``SEND_ERROR`` message is generated.
  23. .. versionadded:: 3.13
  24. The error can be ignored by providing the
  25. ``IGNORE_SVN_FAILURE`` option, which causes these variables to remain
  26. undefined.
  27. ::
  28. <var-prefix>_WC_URL - url of the repository (at <dir>)
  29. <var-prefix>_WC_ROOT - root url of the repository
  30. <var-prefix>_WC_REVISION - current revision
  31. <var-prefix>_WC_LAST_CHANGED_AUTHOR - author of last commit
  32. <var-prefix>_WC_LAST_CHANGED_DATE - date of last commit
  33. <var-prefix>_WC_LAST_CHANGED_REV - revision of last commit
  34. <var-prefix>_WC_INFO - output of command `svn info <dir>'
  35. ``Subversion_WC_LOG`` retrieves the log message of the base revision of a
  36. subversion working copy at a given location. This macro defines the variable:
  37. ::
  38. <var-prefix>_LAST_CHANGED_LOG - last log of base revision
  39. Example usage:
  40. ::
  41. find_package(Subversion)
  42. if(SUBVERSION_FOUND)
  43. Subversion_WC_INFO(${PROJECT_SOURCE_DIR} Project)
  44. message("Current revision is ${Project_WC_REVISION}")
  45. Subversion_WC_LOG(${PROJECT_SOURCE_DIR} Project)
  46. message("Last changed log is ${Project_LAST_CHANGED_LOG}")
  47. endif()
  48. #]=======================================================================]
  49. find_program(Subversion_SVN_EXECUTABLE svn
  50. PATHS
  51. [HKEY_LOCAL_MACHINE\\Software\\TortoiseSVN;Directory]/bin
  52. DOC "subversion command line client")
  53. mark_as_advanced(Subversion_SVN_EXECUTABLE)
  54. if(Subversion_SVN_EXECUTABLE)
  55. # the subversion commands should be executed with the C locale, otherwise
  56. # the message (which are parsed) may be translated, Alex
  57. set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
  58. set(ENV{LC_ALL} C)
  59. execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} --version
  60. OUTPUT_VARIABLE Subversion_VERSION_SVN
  61. ERROR_VARIABLE _Subversion_VERSION_STDERR
  62. RESULT_VARIABLE _Subversion_VERSION_RESULT
  63. OUTPUT_STRIP_TRAILING_WHITESPACE)
  64. # restore the previous LC_ALL
  65. set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
  66. if(_Subversion_VERSION_RESULT EQUAL 0)
  67. string(REGEX REPLACE "^(.*\n)?svn, version ([.0-9]+).*"
  68. "\\2" Subversion_VERSION_SVN "${Subversion_VERSION_SVN}")
  69. else()
  70. unset(Subversion_VERSION_SVN)
  71. if(_Subversion_VERSION_STDERR MATCHES "svn: error: The subversion command line tools are no longer provided by Xcode")
  72. set(Subversion_SVN_EXECUTABLE Subversion_SVN_EXECUTABLE-NOTFOUND)
  73. endif()
  74. endif()
  75. macro(Subversion_WC_INFO dir prefix)
  76. cmake_parse_arguments(
  77. "Subversion_WC_INFO"
  78. "IGNORE_SVN_FAILURE"
  79. "" ""
  80. ${ARGN}
  81. )
  82. # the subversion commands should be executed with the C locale, otherwise
  83. # the message (which are parsed) may be translated, Alex
  84. set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
  85. set(ENV{LC_ALL} C)
  86. execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} info ${dir}
  87. OUTPUT_VARIABLE ${prefix}_WC_INFO
  88. ERROR_VARIABLE Subversion_svn_info_error
  89. RESULT_VARIABLE Subversion_svn_info_result
  90. OUTPUT_STRIP_TRAILING_WHITESPACE)
  91. if(${Subversion_svn_info_result} EQUAL 0)
  92. string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
  93. "\\2" ${prefix}_WC_URL "${${prefix}_WC_INFO}")
  94. string(REGEX REPLACE "^(.*\n)?Repository Root: ([^\n]+).*"
  95. "\\2" ${prefix}_WC_ROOT "${${prefix}_WC_INFO}")
  96. string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
  97. "\\2" ${prefix}_WC_REVISION "${${prefix}_WC_INFO}")
  98. string(REGEX REPLACE "^(.*\n)?Last Changed Author: ([^\n]+).*"
  99. "\\2" ${prefix}_WC_LAST_CHANGED_AUTHOR "${${prefix}_WC_INFO}")
  100. string(REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*"
  101. "\\2" ${prefix}_WC_LAST_CHANGED_REV "${${prefix}_WC_INFO}")
  102. string(REGEX REPLACE "^(.*\n)?Last Changed Date: ([^\n]+).*"
  103. "\\2" ${prefix}_WC_LAST_CHANGED_DATE "${${prefix}_WC_INFO}")
  104. elseif(NOT Subversion_WC_INFO_IGNORE_SVN_FAILURE)
  105. message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} info ${dir}\" failed with output:\n${Subversion_svn_info_error}")
  106. endif()
  107. # restore the previous LC_ALL
  108. set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
  109. endmacro()
  110. macro(Subversion_WC_LOG dir prefix)
  111. # This macro can block if the certificate is not signed:
  112. # svn ask you to accept the certificate and wait for your answer
  113. # This macro requires a svn server network access (Internet most of the time)
  114. # and can also be slow since it access the svn server
  115. execute_process(COMMAND
  116. ${Subversion_SVN_EXECUTABLE} --non-interactive log -r BASE ${dir}
  117. OUTPUT_VARIABLE ${prefix}_LAST_CHANGED_LOG
  118. ERROR_VARIABLE Subversion_svn_log_error
  119. RESULT_VARIABLE Subversion_svn_log_result
  120. OUTPUT_STRIP_TRAILING_WHITESPACE)
  121. if(NOT ${Subversion_svn_log_result} EQUAL 0)
  122. message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} log -r BASE ${dir}\" failed with output:\n${Subversion_svn_log_error}")
  123. endif()
  124. endmacro()
  125. endif()
  126. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  127. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Subversion REQUIRED_VARS Subversion_SVN_EXECUTABLE
  128. VERSION_VAR Subversion_VERSION_SVN )
  129. # for compatibility
  130. set(Subversion_FOUND ${SUBVERSION_FOUND})
  131. set(Subversion_SVN_FOUND ${SUBVERSION_FOUND})