FindHg.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindHg
  5. ------
  6. Finds the Mercurial command-line client executable (``hg``) and provides a
  7. command for extracting information from a Mercurial working copy:
  8. .. code-block:: cmake
  9. find_package(Hg [<version>] [...])
  10. Result Variables
  11. ^^^^^^^^^^^^^^^^
  12. This module defines the following variables:
  13. ``Hg_FOUND``
  14. .. versionadded:: 3.3
  15. Boolean indicating whether the (requested version of) Mercurial client was
  16. found.
  17. ``Hg_VERSION``
  18. .. versionadded:: 4.2
  19. The version of Mercurial found.
  20. Cache Variables
  21. ^^^^^^^^^^^^^^^
  22. The following cache variables may also be set:
  23. ``HG_EXECUTABLE``
  24. Absolute path to the Mercurial command-line client (``hg``).
  25. Commands
  26. ^^^^^^^^
  27. This module provides the following command when Mercurial client (``hg``) is
  28. found:
  29. .. command:: Hg_WC_INFO
  30. .. versionadded:: 3.1
  31. Extracts information of a Mercurial working copy:
  32. .. code-block:: cmake
  33. Hg_WC_INFO(<dir> <var-prefix>)
  34. This command defines the following variables if running Mercurial client on
  35. working copy located at a given location ``<dir>`` succeeds; otherwise a
  36. ``SEND_ERROR`` message is generated:
  37. ``<var-prefix>_WC_CHANGESET``
  38. Current changeset.
  39. ``<var-prefix>_WC_REVISION``
  40. Current revision.
  41. Deprecated Variables
  42. ^^^^^^^^^^^^^^^^^^^^
  43. The following variables are provided for backward compatibility:
  44. ``HG_FOUND``
  45. .. deprecated:: 4.2
  46. Use ``Hg_FOUND``, which has the same value.
  47. Boolean indicating whether the (requested version of) Mercurial client was
  48. found.
  49. ``HG_VERSION_STRING``
  50. .. deprecated:: 4.2
  51. Use ``Hg_VERSION``, which has the same value.
  52. The version of Mercurial found.
  53. Examples
  54. ^^^^^^^^
  55. Finding the Mercurial client and retrieving information about the current
  56. project's working copy:
  57. .. code-block:: cmake
  58. find_package(Hg)
  59. if(Hg_FOUND)
  60. Hg_WC_INFO(${PROJECT_SOURCE_DIR} Project)
  61. message("Current revision is ${Project_WC_REVISION}")
  62. message("Current changeset is ${Project_WC_CHANGESET}")
  63. endif()
  64. #]=======================================================================]
  65. find_program(HG_EXECUTABLE
  66. NAMES hg
  67. PATHS
  68. [HKEY_LOCAL_MACHINE\\Software\\TortoiseHG]
  69. PATH_SUFFIXES Mercurial
  70. DOC "hg command line client"
  71. )
  72. mark_as_advanced(HG_EXECUTABLE)
  73. if(HG_EXECUTABLE)
  74. set(_saved_lc_all "$ENV{LC_ALL}")
  75. set(ENV{LC_ALL} "C")
  76. set(_saved_language "$ENV{LANGUAGE}")
  77. set(ENV{LANGUAGE})
  78. execute_process(COMMAND ${HG_EXECUTABLE} --version
  79. OUTPUT_VARIABLE hg_version
  80. ERROR_QUIET
  81. RESULT_VARIABLE hg_result
  82. OUTPUT_STRIP_TRAILING_WHITESPACE)
  83. set(ENV{LC_ALL} ${_saved_lc_all})
  84. set(ENV{LANGUAGE} ${_saved_language})
  85. if(hg_result MATCHES "is not a valid Win32 application")
  86. set_property(CACHE HG_EXECUTABLE PROPERTY VALUE "HG_EXECUTABLE-NOTFOUND")
  87. endif()
  88. if(hg_version MATCHES "^Mercurial Distributed SCM \\(version ([0-9][^)]*)\\)")
  89. set(Hg_VERSION "${CMAKE_MATCH_1}")
  90. set(HG_VERSION_STRING "${Hg_VERSION}")
  91. endif()
  92. unset(hg_version)
  93. macro(HG_WC_INFO dir prefix)
  94. execute_process(COMMAND ${HG_EXECUTABLE} id -i -n
  95. WORKING_DIRECTORY ${dir}
  96. RESULT_VARIABLE hg_id_result
  97. ERROR_VARIABLE hg_id_error
  98. OUTPUT_VARIABLE ${prefix}_WC_DATA
  99. OUTPUT_STRIP_TRAILING_WHITESPACE)
  100. if(NOT ${hg_id_result} EQUAL 0)
  101. message(SEND_ERROR "Command \"${HG_EXECUTABLE} id -n\" in directory ${dir} failed with output:\n${hg_id_error}")
  102. endif()
  103. string(REGEX REPLACE "([0-9a-f]+)\\+? [0-9]+\\+?" "\\1" ${prefix}_WC_CHANGESET ${${prefix}_WC_DATA})
  104. string(REGEX REPLACE "[0-9a-f]+\\+? ([0-9]+)\\+?" "\\1" ${prefix}_WC_REVISION ${${prefix}_WC_DATA})
  105. endmacro()
  106. endif()
  107. include(FindPackageHandleStandardArgs)
  108. find_package_handle_standard_args(Hg
  109. REQUIRED_VARS HG_EXECUTABLE
  110. VERSION_VAR Hg_VERSION)