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