FindMercurial.cmake 2.32 KB
Newer Older
1 2 3
# - Extract information from a mercurial working copy

# Example usage:
4 5
#  find_package(Mercurial)
#  if(Mercurial_FOUND)
6
#    Mercurial_WC_IDENTIFY(${PROJECT_SOURCE_DIR} Project)
7 8
#    message("Current revision is ${Project_WC_REVISION}")
#    message("Mercurial version is ${Mercurial_VERSION_HG}")
9
#  endif()
10 11


12 13
set(Mercurial_FOUND FALSE)
set(Mercurial_HG_FOUND FALSE)
14

15
find_program(Mercurial_HG_EXECUTABLE hg
16
  DOC "mercurial command line client")
17
mark_as_advanced(Mercurial_HG_EXECUTABLE)
18

19 20 21
if(Mercurial_HG_EXECUTABLE)
  set(Mercurial_HG_FOUND TRUE)
  set(Mercurial_FOUND TRUE)
22

23
  macro(Mercurial_WC_IDENTIFY dir prefix)
24 25
    # the subversion commands should be executed with the C locale, otherwise
    # the message (which are parsed) may be translated, Alex
26 27 28
    set(_Mercurial_SAVED_LC_ALL "$ENV{LC_ALL}")
    set(ENV{LC_ALL} C)
    execute_process(COMMAND ${Mercurial_HG_EXECUTABLE} version
29 30
      WORKING_DIRECTORY ${dir}
      OUTPUT_VARIABLE Mercurial_VERSION_HG
31
      OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
32

33
    execute_process(COMMAND ${Mercurial_HG_EXECUTABLE} identify ${dir}
34
      OUTPUT_VARIABLE ${prefix}_WC_IDENTIFY
35 36 37
#      ERROR_VARIABLE Mercurial_hg_identify_error
#      RESULT_VARIABLE Mercurial_hg_identify_result
      OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
38

39
    execute_process(COMMAND ${Mercurial_HG_EXECUTABLE} status -m ${dir}
40
      OUTPUT_VARIABLE ${prefix}_WC_STATUS
41 42 43
#      ERROR_VARIABLE Mercurial_hg_status_error
#      RESULT_VARIABLE Mercurial_hg_status_result
      OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
44

45 46
    if(NOT ${Mercurial_hg_identify_result} EQUAL 0)
      message(SEND_ERROR "Command \"${Mercurial_HG_EXECUTABLE} identify ${dir}\" failed with output:\n${Mercurial_hg_identify_error}")
47
    else()
48

49
      string(REGEX REPLACE "^(.*\n)?Mercurial Distributed SCM [(]version ([.0-9]+).*"
50
        "\\2" Mercurial_VERSION_HG "${Mercurial_VERSION_HG}")
51
      string(REGEX REPLACE "^(.*\n)?([0-9a-f]+).*"
52 53
        "\\2" ${prefix}_WC_REVISION "${${prefix}_WC_IDENTIFY}")

54
    endif()
55 56

    # restore the previous LC_ALL
57
    set(ENV{LC_ALL} ${_Mercurial_SAVED_LC_ALL})
58

59
  endmacro()
60 61 62



63
endif()
64 65 66



67 68 69
if(NOT Mercurial_FOUND)
  if(NOT Mercurial_FIND_QUIETLY)
    message(STATUS "Mercurial was not found.")
70
  else()
71 72
    if(Mercurial_FIND_REQUIRED)
      message(FATAL_ERROR "Mercurial was not found.")
73 74 75
    endif()
  endif()
endif()