FindSubversion.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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>)
  28. # Subversion_WC_LOG(<dir> <var-prefix>)
  29. #
  30. # Subversion_WC_INFO extracts information of a subversion working copy
  31. # at a given location. This macro defines the following variables:
  32. #
  33. # ::
  34. #
  35. # <var-prefix>_WC_URL - url of the repository (at <dir>)
  36. # <var-prefix>_WC_ROOT - root url of the repository
  37. # <var-prefix>_WC_REVISION - current revision
  38. # <var-prefix>_WC_LAST_CHANGED_AUTHOR - author of last commit
  39. # <var-prefix>_WC_LAST_CHANGED_DATE - date of last commit
  40. # <var-prefix>_WC_LAST_CHANGED_REV - revision of last commit
  41. # <var-prefix>_WC_INFO - output of command `svn info <dir>'
  42. #
  43. # Subversion_WC_LOG retrieves the log message of the base revision of a
  44. # subversion working copy at a given location. This macro defines the
  45. # variable:
  46. #
  47. # ::
  48. #
  49. # <var-prefix>_LAST_CHANGED_LOG - last log of base revision
  50. #
  51. # Example usage:
  52. #
  53. # ::
  54. #
  55. # find_package(Subversion)
  56. # if(SUBVERSION_FOUND)
  57. # Subversion_WC_INFO(${PROJECT_SOURCE_DIR} Project)
  58. # message("Current revision is ${Project_WC_REVISION}")
  59. # Subversion_WC_LOG(${PROJECT_SOURCE_DIR} Project)
  60. # message("Last changed log is ${Project_LAST_CHANGED_LOG}")
  61. # endif()
  62. find_program(Subversion_SVN_EXECUTABLE svn
  63. PATHS
  64. [HKEY_LOCAL_MACHINE\\Software\\TortoiseSVN;Directory]/bin
  65. DOC "subversion command line client")
  66. mark_as_advanced(Subversion_SVN_EXECUTABLE)
  67. if(Subversion_SVN_EXECUTABLE)
  68. # the subversion commands should be executed with the C locale, otherwise
  69. # the message (which are parsed) may be translated, Alex
  70. set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
  71. set(ENV{LC_ALL} C)
  72. execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} --version
  73. OUTPUT_VARIABLE Subversion_VERSION_SVN
  74. OUTPUT_STRIP_TRAILING_WHITESPACE)
  75. # restore the previous LC_ALL
  76. set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
  77. string(REGEX REPLACE "^(.*\n)?svn, version ([.0-9]+).*"
  78. "\\2" Subversion_VERSION_SVN "${Subversion_VERSION_SVN}")
  79. macro(Subversion_WC_INFO dir prefix)
  80. # the subversion commands should be executed with the C locale, otherwise
  81. # the message (which are parsed) may be translated, Alex
  82. set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
  83. set(ENV{LC_ALL} C)
  84. execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} info ${dir}
  85. OUTPUT_VARIABLE ${prefix}_WC_INFO
  86. ERROR_VARIABLE Subversion_svn_info_error
  87. RESULT_VARIABLE Subversion_svn_info_result
  88. OUTPUT_STRIP_TRAILING_WHITESPACE)
  89. if(NOT ${Subversion_svn_info_result} EQUAL 0)
  90. message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} info ${dir}\" failed with output:\n${Subversion_svn_info_error}")
  91. else()
  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. endif()
  105. # restore the previous LC_ALL
  106. set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
  107. endmacro()
  108. macro(Subversion_WC_LOG dir prefix)
  109. # This macro can block if the certificate is not signed:
  110. # svn ask you to accept the certificate and wait for your answer
  111. # This macro requires a svn server network access (Internet most of the time)
  112. # and can also be slow since it access the svn server
  113. execute_process(COMMAND
  114. ${Subversion_SVN_EXECUTABLE} --non-interactive log -r BASE ${dir}
  115. OUTPUT_VARIABLE ${prefix}_LAST_CHANGED_LOG
  116. ERROR_VARIABLE Subversion_svn_log_error
  117. RESULT_VARIABLE Subversion_svn_log_result
  118. OUTPUT_STRIP_TRAILING_WHITESPACE)
  119. if(NOT ${Subversion_svn_log_result} EQUAL 0)
  120. message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} log -r BASE ${dir}\" failed with output:\n${Subversion_svn_log_error}")
  121. endif()
  122. endmacro()
  123. endif()
  124. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  125. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Subversion REQUIRED_VARS Subversion_SVN_EXECUTABLE
  126. VERSION_VAR Subversion_VERSION_SVN )
  127. # for compatibility
  128. set(Subversion_FOUND ${SUBVERSION_FOUND})
  129. set(Subversion_SVN_FOUND ${SUBVERSION_FOUND})