FindSubversion.cmake 5.8 KB

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