FindSubversion.cmake 6.21 KB
Newer Older
1 2 3
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#[=======================================================================[.rst:
FindSubversion
--------------

Extract information from a subversion working copy

The module defines the following variables:

::

  Subversion_SVN_EXECUTABLE - path to svn command line client
  Subversion_VERSION_SVN - version of svn command line client
  Subversion_FOUND - true if the command line client was found
  SUBVERSION_FOUND - same as Subversion_FOUND, set for compatibility reasons



The minimum required version of Subversion can be specified using the
standard syntax, e.g. ``find_package(Subversion 1.4)``.

If the command line client executable is found two macros are defined:

::

  Subversion_WC_INFO(<dir> <var-prefix> [IGNORE_SVN_FAILURE])
  Subversion_WC_LOG(<dir> <var-prefix>)

``Subversion_WC_INFO`` extracts information of a subversion working copy at a
given location.  This macro defines the following variables if running
Subversion's ``info`` command on ``<dir>`` succeeds; otherwise a
``SEND_ERROR`` message is generated. The error can be ignored by providing the
``IGNORE_SVN_FAILURE`` option, which causes these variables to remain
undefined.

::

  <var-prefix>_WC_URL - url of the repository (at <dir>)
  <var-prefix>_WC_ROOT - root url of the repository
  <var-prefix>_WC_REVISION - current revision
  <var-prefix>_WC_LAST_CHANGED_AUTHOR - author of last commit
  <var-prefix>_WC_LAST_CHANGED_DATE - date of last commit
  <var-prefix>_WC_LAST_CHANGED_REV - revision of last commit
  <var-prefix>_WC_INFO - output of command `svn info <dir>'

``Subversion_WC_LOG`` retrieves the log message of the base revision of a
subversion working copy at a given location.  This macro defines the variable:

::

  <var-prefix>_LAST_CHANGED_LOG - last log of base revision

Example usage:

::

  find_package(Subversion)
  if(SUBVERSION_FOUND)
    Subversion_WC_INFO(${PROJECT_SOURCE_DIR} Project)
    message("Current revision is ${Project_WC_REVISION}")
    Subversion_WC_LOG(${PROJECT_SOURCE_DIR} Project)
    message("Last changed log is ${Project_LAST_CHANGED_LOG}")
  endif()
#]=======================================================================]
67

68
find_program(Subversion_SVN_EXECUTABLE svn
69 70
  PATHS
    [HKEY_LOCAL_MACHINE\\Software\\TortoiseSVN;Directory]/bin
71
  DOC "subversion command line client")
72
mark_as_advanced(Subversion_SVN_EXECUTABLE)
73

74
if(Subversion_SVN_EXECUTABLE)
75 76
  # the subversion commands should be executed with the C locale, otherwise
  # the message (which are parsed) may be translated, Alex
77 78
  set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
  set(ENV{LC_ALL} C)
79

80
  execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} --version
81
    OUTPUT_VARIABLE Subversion_VERSION_SVN
82 83
    ERROR_VARIABLE _Subversion_VERSION_STDERR
    RESULT_VARIABLE _Subversion_VERSION_RESULT
84 85
    OUTPUT_STRIP_TRAILING_WHITESPACE)

86
  # restore the previous LC_ALL
87
  set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
88

89 90 91 92 93 94 95 96 97
  if(_Subversion_VERSION_RESULT EQUAL 0)
    string(REGEX REPLACE "^(.*\n)?svn, version ([.0-9]+).*"
      "\\2" Subversion_VERSION_SVN "${Subversion_VERSION_SVN}")
  else()
    unset(Subversion_VERSION_SVN)
    if(_Subversion_VERSION_STDERR MATCHES "svn: error: The subversion command line tools are no longer provided by Xcode")
      set(Subversion_SVN_EXECUTABLE Subversion_SVN_EXECUTABLE-NOTFOUND)
    endif()
  endif()
98

99
  macro(Subversion_WC_INFO dir prefix)
100 101 102 103 104 105 106 107

    cmake_parse_arguments(
      "Subversion_WC_INFO"
      "IGNORE_SVN_FAILURE"
      "" ""
      ${ARGN}
    )

Brad King's avatar
Brad King committed
108 109
    # the subversion commands should be executed with the C locale, otherwise
    # the message (which are parsed) may be translated, Alex
110 111
    set(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
    set(ENV{LC_ALL} C)
Brad King's avatar
Brad King committed
112

113
    execute_process(COMMAND ${Subversion_SVN_EXECUTABLE} info ${dir}
114 115 116 117 118
      OUTPUT_VARIABLE ${prefix}_WC_INFO
      ERROR_VARIABLE Subversion_svn_info_error
      RESULT_VARIABLE Subversion_svn_info_result
      OUTPUT_STRIP_TRAILING_WHITESPACE)

119
    if(${Subversion_svn_info_result} EQUAL 0)
120
      string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
121
        "\\2" ${prefix}_WC_URL "${${prefix}_WC_INFO}")
122
      string(REGEX REPLACE "^(.*\n)?Repository Root: ([^\n]+).*"
123
        "\\2" ${prefix}_WC_ROOT "${${prefix}_WC_INFO}")
124
      string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
125
        "\\2" ${prefix}_WC_REVISION "${${prefix}_WC_INFO}")
126
      string(REGEX REPLACE "^(.*\n)?Last Changed Author: ([^\n]+).*"
127
        "\\2" ${prefix}_WC_LAST_CHANGED_AUTHOR "${${prefix}_WC_INFO}")
128
      string(REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*"
129
        "\\2" ${prefix}_WC_LAST_CHANGED_REV "${${prefix}_WC_INFO}")
130
      string(REGEX REPLACE "^(.*\n)?Last Changed Date: ([^\n]+).*"
131
        "\\2" ${prefix}_WC_LAST_CHANGED_DATE "${${prefix}_WC_INFO}")
132 133
    elseif(NOT Subversion_WC_INFO_IGNORE_SVN_FAILURE)
      message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} info ${dir}\" failed with output:\n${Subversion_svn_info_error}")
134
    endif()
135

Brad King's avatar
Brad King committed
136
    # restore the previous LC_ALL
137
    set(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})
Brad King's avatar
Brad King committed
138

139
  endmacro()
Brad King's avatar
Brad King committed
140

141
  macro(Subversion_WC_LOG dir prefix)
Brad King's avatar
Brad King committed
142 143 144 145
    # This macro can block if the certificate is not signed:
    # svn ask you to accept the certificate and wait for your answer
    # This macro requires a svn server network access (Internet most of the time)
    # and can also be slow since it access the svn server
146
    execute_process(COMMAND
147
      ${Subversion_SVN_EXECUTABLE} --non-interactive log -r BASE ${dir}
Brad King's avatar
Brad King committed
148
      OUTPUT_VARIABLE ${prefix}_LAST_CHANGED_LOG
149 150 151 152
      ERROR_VARIABLE Subversion_svn_log_error
      RESULT_VARIABLE Subversion_svn_log_result
      OUTPUT_STRIP_TRAILING_WHITESPACE)

153 154
    if(NOT ${Subversion_svn_log_result} EQUAL 0)
      message(SEND_ERROR "Command \"${Subversion_SVN_EXECUTABLE} log -r BASE ${dir}\" failed with output:\n${Subversion_svn_log_error}")
155 156
    endif()
  endmacro()
157

158
endif()
159

160
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
161 162
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Subversion REQUIRED_VARS Subversion_SVN_EXECUTABLE
                                             VERSION_VAR Subversion_VERSION_SVN )
163 164

# for compatibility
165 166
set(Subversion_FOUND ${SUBVERSION_FOUND})
set(Subversion_SVN_FOUND ${SUBVERSION_FOUND})