FindSubversion.cmake 5.8 KB

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