GetSVN.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # CMake project that writes Subversion revision information to a header.
  2. #
  3. # Input variables:
  4. # FIRST_SOURCE_DIR - First source directory
  5. # FIRST_NAME - The macro prefix for the first repository's info
  6. # SECOND_SOURCE_DIR - Second source directory (opt)
  7. # SECOND_NAME - The macro prefix for the second repository's info (opt)
  8. # HEADER_FILE - The header file to write
  9. #
  10. # The output header will contain macros FIRST_REPOSITORY and FIRST_REVISION,
  11. # and SECOND_REPOSITORY and SECOND_REVISION if requested, where "FIRST" and
  12. # "SECOND" are substituted with the names specified in the input variables.
  13. # Chop off cmake/modules/GetSVN.cmake
  14. get_filename_component(LLVM_DIR "${CMAKE_SCRIPT_MODE_FILE}" PATH)
  15. get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
  16. get_filename_component(LLVM_DIR "${LLVM_DIR}" PATH)
  17. # Handle strange terminals
  18. set(ENV{TERM} "dumb")
  19. macro(get_source_info_svn path revision repository)
  20. # If svn is a bat file, find_program(Subversion) doesn't find it.
  21. # Explicitly search for that here; Subversion_SVN_EXECUTABLE will override
  22. # the find_program call in FindSubversion.cmake.
  23. find_program(Subversion_SVN_EXECUTABLE NAMES svn svn.bat)
  24. # FindSubversion does not work with symlinks. See PR 8437
  25. if (NOT IS_SYMLINK "${path}")
  26. find_package(Subversion)
  27. endif()
  28. if (Subversion_FOUND)
  29. subversion_wc_info( ${path} Project )
  30. if (Project_WC_REVISION)
  31. set(${revision} ${Project_WC_REVISION} PARENT_SCOPE)
  32. endif()
  33. if (Project_WC_URL)
  34. set(${repository} ${Project_WC_URL} PARENT_SCOPE)
  35. endif()
  36. endif()
  37. endmacro()
  38. macro(get_source_info_git_svn path revision repository)
  39. find_program(git_executable NAMES git git.exe git.cmd)
  40. if (git_executable)
  41. execute_process(COMMAND ${git_executable} svn info
  42. WORKING_DIRECTORY ${path}
  43. TIMEOUT 5
  44. RESULT_VARIABLE git_result
  45. OUTPUT_VARIABLE git_output)
  46. if (git_result EQUAL 0)
  47. string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
  48. "\\2" git_svn_rev "${git_output}")
  49. set(${revision} ${git_svn_rev} PARENT_SCOPE)
  50. string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
  51. "\\2" git_url "${git_output}")
  52. set(${repository} ${git_url} PARENT_SCOPE)
  53. endif()
  54. endif()
  55. endmacro()
  56. macro(get_source_info_git path revision repository)
  57. find_program(git_executable NAMES git git.exe git.cmd)
  58. if (git_executable)
  59. execute_process(COMMAND ${git_executable} log -1 --pretty=format:%H
  60. WORKING_DIRECTORY ${path}
  61. TIMEOUT 5
  62. RESULT_VARIABLE git_result
  63. OUTPUT_VARIABLE git_output)
  64. if (git_result EQUAL 0)
  65. set(${revision} ${git_output} PARENT_SCOPE)
  66. endif()
  67. execute_process(COMMAND ${git_executable} remote -v
  68. WORKING_DIRECTORY ${path}
  69. TIMEOUT 5
  70. RESULT_VARIABLE git_result
  71. OUTPUT_VARIABLE git_output)
  72. if (git_result EQUAL 0)
  73. string(REGEX REPLACE "^(.*\n)?[^ \t]+[ \t]+([^ \t\n]+)[ \t]+\\(fetch\\).*"
  74. "\\2" git_url "${git_output}")
  75. set(${repository} "${git_url}" PARENT_SCOPE)
  76. endif()
  77. endif()
  78. endmacro()
  79. function(get_source_info path revision repository)
  80. if (EXISTS "${path}/.svn")
  81. get_source_info_svn("${path}" revision repository)
  82. elseif (EXISTS "${path}/.git/svn")
  83. get_source_info_git_svn("${path}" revision repository)
  84. elseif (EXISTS "${path}/.git")
  85. get_source_info_git("${path}" revision repository)
  86. endif()
  87. endfunction()
  88. function(append_info name path)
  89. get_source_info("${path}" revision repository)
  90. string(STRIP "${revision}" revision)
  91. string(STRIP "${repository}" repository)
  92. file(APPEND "${HEADER_FILE}.txt"
  93. "#define ${name}_REVISION \"${revision}\"\n")
  94. file(APPEND "${HEADER_FILE}.txt"
  95. "#define ${name}_REPOSITORY \"${repository}\"\n")
  96. endfunction()
  97. append_info(${FIRST_NAME} "${FIRST_SOURCE_DIR}")
  98. if(DEFINED SECOND_SOURCE_DIR)
  99. append_info(${SECOND_NAME} "${SECOND_SOURCE_DIR}")
  100. endif()
  101. # Copy the file only if it has changed.
  102. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
  103. "${HEADER_FILE}.txt" "${HEADER_FILE}")
  104. file(REMOVE "${HEADER_FILE}.txt")