FindHg.cmake 3.0 KB

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