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