FindHg.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #.rst:
  2. # FindHg
  3. # ------
  4. #
  5. # Extract information from a mercurial working copy.
  6. #
  7. # The module defines the following variables:
  8. #
  9. # ::
  10. #
  11. # HG_EXECUTABLE - path to mercurial command line client (hg)
  12. # HG_FOUND - true if the command line client was found
  13. # HG_VERSION_STRING - the version of mercurial found
  14. #
  15. # If the command line client executable is found the following macro is defined:
  16. #
  17. # ::
  18. #
  19. # HG_WC_INFO(<dir> <var-prefix>)
  20. #
  21. # Hg_WC_INFO extracts information of a mercurial working copy
  22. # at a given location. This macro defines the following variables:
  23. #
  24. # ::
  25. #
  26. # <var-prefix>_WC_CHANGESET - current changeset
  27. # <var-prefix>_WC_REVISION - current revision
  28. #
  29. # Example usage:
  30. #
  31. # ::
  32. #
  33. # find_package(Hg)
  34. # if(HG_FOUND)
  35. # message("hg found: ${HG_EXECUTABLE}")
  36. # HG_WC_INFO(${PROJECT_SOURCE_DIR} Project)
  37. # message("Current revision is ${Project_WC_REVISION}")
  38. # message("Current changeset is ${Project_WC_CHANGESET}")
  39. # endif()
  40. #=============================================================================
  41. # Copyright 2010-2012 Kitware, Inc.
  42. # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
  43. # Copyright 2014 Matthaeus G. Chajdas
  44. #
  45. # Distributed under the OSI-approved BSD License (the "License");
  46. # see accompanying file Copyright.txt for details.
  47. #
  48. # This software is distributed WITHOUT ANY WARRANTY; without even the
  49. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  50. # See the License for more information.
  51. #=============================================================================
  52. # (To distribute this file outside of CMake, substitute the full
  53. # License text for the above reference.)
  54. find_program(HG_EXECUTABLE
  55. NAMES hg
  56. PATHS
  57. [HKEY_LOCAL_MACHINE\\Software\\TortoiseHG]
  58. PATH_SUFFIXES Mercurial
  59. DOC "hg command line client"
  60. )
  61. mark_as_advanced(HG_EXECUTABLE)
  62. if(HG_EXECUTABLE)
  63. set(_saved_lc_all "$ENV{LC_ALL}")
  64. set(ENV{LC_ALL} "C")
  65. set(_saved_language "$ENV{LANGUAGE}")
  66. set(ENV{LANGUAGE})
  67. execute_process(COMMAND ${HG_EXECUTABLE} --version
  68. OUTPUT_VARIABLE hg_version
  69. ERROR_QUIET
  70. RESULT_VARIABLE hg_result
  71. OUTPUT_STRIP_TRAILING_WHITESPACE)
  72. set(ENV{LC_ALL} ${_saved_lc_all})
  73. set(ENV{LANGUAGE} ${_saved_language})
  74. if(hg_result MATCHES "is not a valid Win32 application")
  75. set_property(CACHE HG_EXECUTABLE PROPERTY VALUE "HG_EXECUTABLE-NOTFOUND")
  76. endif()
  77. if(hg_version MATCHES "^Mercurial Distributed SCM \\(version ([0-9][^)]*)\\)")
  78. set(HG_VERSION_STRING "${CMAKE_MATCH_1}")
  79. endif()
  80. unset(hg_version)
  81. macro(HG_WC_INFO dir prefix)
  82. execute_process(COMMAND ${HG_EXECUTABLE} id -i -n
  83. WORKING_DIRECTORY ${dir}
  84. RESULT_VARIABLE hg_id_result
  85. ERROR_VARIABLE hg_id_error
  86. OUTPUT_VARIABLE ${prefix}_WC_DATA
  87. OUTPUT_STRIP_TRAILING_WHITESPACE)
  88. if(NOT ${hg_id_result} EQUAL 0)
  89. message(SEND_ERROR "Command \"${HG_EXECUTBALE} id -n\" in directory ${dir} failed with output:\n${hg_id_error}")
  90. endif()
  91. string(REGEX REPLACE "([0-9a-f]+)\\+? [0-9]+\\+?" "\\1" ${prefix}_WC_CHANGESET ${${prefix}_WC_DATA})
  92. string(REGEX REPLACE "[0-9a-f]+\\+? ([0-9]+)\\+?" "\\1" ${prefix}_WC_REVISION ${${prefix}_WC_DATA})
  93. endmacro(HG_WC_INFO)
  94. endif()
  95. # Handle the QUIETLY and REQUIRED arguments and set HG_FOUND to TRUE if
  96. # all listed variables are TRUE
  97. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  98. find_package_handle_standard_args(Hg
  99. REQUIRED_VARS HG_EXECUTABLE
  100. VERSION_VAR HG_VERSION_STRING)