FindHg.cmake 2.9 KB

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