VersionFromVCS.cmake 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Adds version control information to the variable VERS. For
  2. # determining the Version Control System used (if any) it inspects the
  3. # existence of certain subdirectories under SOURCE_DIR (if provided as an
  4. # extra argument, otherwise uses CMAKE_CURRENT_SOURCE_DIR).
  5. function(get_source_info_svn path revision repository)
  6. # If svn is a bat file, find_program(Subversion) doesn't find it.
  7. # Explicitly search for that here; Subversion_SVN_EXECUTABLE will override
  8. # the find_program call in FindSubversion.cmake.
  9. find_program(Subversion_SVN_EXECUTABLE NAMES svn svn.bat)
  10. find_package(Subversion)
  11. # Subversion module does not work with symlinks, see PR8437.
  12. get_filename_component(realpath ${path} REALPATH)
  13. if(Subversion_FOUND)
  14. subversion_wc_info(${realpath} Project)
  15. if(Project_WC_REVISION)
  16. set(${revision} ${Project_WC_REVISION} PARENT_SCOPE)
  17. endif()
  18. if(Project_WC_URL)
  19. set(${repository} ${Project_WC_URL} PARENT_SCOPE)
  20. endif()
  21. endif()
  22. endfunction()
  23. function(get_source_info_git path revision repository)
  24. find_package(Git)
  25. if(GIT_FOUND)
  26. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
  27. WORKING_DIRECTORY ${path}
  28. RESULT_VARIABLE git_result
  29. OUTPUT_VARIABLE git_output
  30. ERROR_QUIET)
  31. if(git_result EQUAL 0)
  32. string(STRIP "${git_output}" git_output)
  33. get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
  34. if(EXISTS "${git_dir}/svn/refs")
  35. execute_process(COMMAND ${GIT_EXECUTABLE} svn info
  36. WORKING_DIRECTORY ${path}
  37. RESULT_VARIABLE git_result
  38. OUTPUT_VARIABLE git_output)
  39. if(git_result EQUAL 0)
  40. string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
  41. "\\2" git_svn_rev "${git_output}")
  42. set(${revision} ${git_svn_rev} PARENT_SCOPE)
  43. string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
  44. "\\2" git_url "${git_output}")
  45. set(${repository} ${git_url} PARENT_SCOPE)
  46. endif()
  47. else()
  48. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
  49. WORKING_DIRECTORY ${path}
  50. RESULT_VARIABLE git_result
  51. OUTPUT_VARIABLE git_output)
  52. if(git_result EQUAL 0)
  53. string(STRIP "${git_output}" git_output)
  54. set(${revision} ${git_output} PARENT_SCOPE)
  55. endif()
  56. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref --symbolic-full-name @{upstream}
  57. WORKING_DIRECTORY ${path}
  58. RESULT_VARIABLE git_result
  59. OUTPUT_VARIABLE git_output
  60. ERROR_QUIET)
  61. if(git_result EQUAL 0)
  62. string(REPLACE "/" ";" branch ${git_output})
  63. list(GET branch 0 remote)
  64. else()
  65. set(remote "origin")
  66. endif()
  67. execute_process(COMMAND ${GIT_EXECUTABLE} remote get-url ${remote}
  68. WORKING_DIRECTORY ${path}
  69. RESULT_VARIABLE git_result
  70. OUTPUT_VARIABLE git_output
  71. ERROR_QUIET)
  72. if(git_result EQUAL 0)
  73. string(STRIP "${git_output}" git_output)
  74. set(${repository} ${git_output} PARENT_SCOPE)
  75. else()
  76. set(${repository} ${path} PARENT_SCOPE)
  77. endif()
  78. endif()
  79. endif()
  80. endif()
  81. endfunction()
  82. function(get_source_info path revision repository)
  83. if(EXISTS "${path}/.svn")
  84. get_source_info_svn("${path}" revision_info repository_info)
  85. else()
  86. get_source_info_git("${path}" revision_info repository_info)
  87. endif()
  88. set(${repository} "${repository_info}" PARENT_SCOPE)
  89. set(${revision} "${revision_info}" PARENT_SCOPE)
  90. endfunction()