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:
5FindHg
6------
7
8Extract information from a mercurial working copy.
9
10The module defines the following variables:
11
12::
13
14   HG_EXECUTABLE - path to mercurial command line client (hg)
15   HG_FOUND - true if the command line client was found
16   HG_VERSION_STRING - the version of mercurial found
17
18.. versionadded:: 3.1
19  If the command line client executable is found the following macro is defined:
20
21::
22
23  HG_WC_INFO(<dir> <var-prefix>)
24
25Hg_WC_INFO extracts information of a mercurial working copy
26at a given location.  This macro defines the following variables:
27
28::
29
30  <var-prefix>_WC_CHANGESET - current changeset
31  <var-prefix>_WC_REVISION - current revision
32
33Example usage:
34
35::
36
37   find_package(Hg)
38   if(HG_FOUND)
39     message("hg found: ${HG_EXECUTABLE}")
40     HG_WC_INFO(${PROJECT_SOURCE_DIR} Project)
41     message("Current revision is ${Project_WC_REVISION}")
42     message("Current changeset is ${Project_WC_CHANGESET}")
43   endif()
44#]=======================================================================]
45
46find_program(HG_EXECUTABLE
47  NAMES hg
48  PATHS
49    [HKEY_LOCAL_MACHINE\\Software\\TortoiseHG]
50  PATH_SUFFIXES Mercurial
51  DOC "hg command line client"
52  )
53mark_as_advanced(HG_EXECUTABLE)
54
55if(HG_EXECUTABLE)
56  set(_saved_lc_all "$ENV{LC_ALL}")
57  set(ENV{LC_ALL} "C")
58
59  set(_saved_language "$ENV{LANGUAGE}")
60  set(ENV{LANGUAGE})
61
62  execute_process(COMMAND ${HG_EXECUTABLE} --version
63                  OUTPUT_VARIABLE hg_version
64                  ERROR_QUIET
65                  RESULT_VARIABLE hg_result
66                  OUTPUT_STRIP_TRAILING_WHITESPACE)
67
68  set(ENV{LC_ALL} ${_saved_lc_all})
69  set(ENV{LANGUAGE} ${_saved_language})
70
71  if(hg_result MATCHES "is not a valid Win32 application")
72    set_property(CACHE HG_EXECUTABLE PROPERTY VALUE "HG_EXECUTABLE-NOTFOUND")
73  endif()
74  if(hg_version MATCHES "^Mercurial Distributed SCM \\(version ([0-9][^)]*)\\)")
75    set(HG_VERSION_STRING "${CMAKE_MATCH_1}")
76  endif()
77  unset(hg_version)
78
79  macro(HG_WC_INFO dir prefix)
80    execute_process(COMMAND ${HG_EXECUTABLE} id -i -n
81      WORKING_DIRECTORY ${dir}
82      RESULT_VARIABLE hg_id_result
83      ERROR_VARIABLE hg_id_error
84      OUTPUT_VARIABLE ${prefix}_WC_DATA
85      OUTPUT_STRIP_TRAILING_WHITESPACE)
86    if(NOT ${hg_id_result} EQUAL 0)
87      message(SEND_ERROR "Command \"${HG_EXECUTBALE} id -n\" in directory ${dir} failed with output:\n${hg_id_error}")
88    endif()
89
90    string(REGEX REPLACE "([0-9a-f]+)\\+? [0-9]+\\+?" "\\1" ${prefix}_WC_CHANGESET ${${prefix}_WC_DATA})
91    string(REGEX REPLACE "[0-9a-f]+\\+? ([0-9]+)\\+?" "\\1" ${prefix}_WC_REVISION ${${prefix}_WC_DATA})
92  endmacro(HG_WC_INFO)
93endif()
94
95include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
96find_package_handle_standard_args(Hg
97                                  REQUIRED_VARS HG_EXECUTABLE
98                                  VERSION_VAR HG_VERSION_STRING)
99