xref: /netbsd-src/external/apache2/llvm/dist/llvm/cmake/modules/VersionFromVCS.cmake (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
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
6function(get_source_info_svn path revision repository)
7  # If svn is a bat file, find_program(Subversion) doesn't find it.
8  # Explicitly search for that here; Subversion_SVN_EXECUTABLE will override
9  # the find_program call in FindSubversion.cmake.
10  find_program(Subversion_SVN_EXECUTABLE NAMES svn svn.bat)
11  find_package(Subversion)
12
13  # Subversion module does not work with symlinks, see PR8437.
14  get_filename_component(realpath ${path} REALPATH)
15  if(Subversion_FOUND)
16    subversion_wc_info(${realpath} Project)
17    if(Project_WC_REVISION)
18      set(${revision} ${Project_WC_REVISION} PARENT_SCOPE)
19    endif()
20    if(Project_WC_URL)
21      set(${repository} ${Project_WC_URL} PARENT_SCOPE)
22    endif()
23  endif()
24endfunction()
25
26function(get_source_info_git path revision repository)
27  find_package(Git)
28  if(GIT_FOUND)
29    execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir
30      WORKING_DIRECTORY ${path}
31      RESULT_VARIABLE git_result
32      OUTPUT_VARIABLE git_output
33      ERROR_QUIET)
34    if(git_result EQUAL 0)
35      string(STRIP "${git_output}" git_output)
36      get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path})
37      if(EXISTS "${git_dir}/svn/refs")
38        execute_process(COMMAND ${GIT_EXECUTABLE} svn info
39          WORKING_DIRECTORY ${path}
40          RESULT_VARIABLE git_result
41          OUTPUT_VARIABLE git_output)
42        if(git_result EQUAL 0)
43          string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
44            "\\2" git_svn_rev "${git_output}")
45          set(${revision} ${git_svn_rev} PARENT_SCOPE)
46          string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
47            "\\2" git_url "${git_output}")
48          set(${repository} ${git_url} PARENT_SCOPE)
49        endif()
50      else()
51        execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
52          WORKING_DIRECTORY ${path}
53          RESULT_VARIABLE git_result
54          OUTPUT_VARIABLE git_output)
55        if(git_result EQUAL 0)
56          string(STRIP "${git_output}" git_output)
57          set(${revision} ${git_output} PARENT_SCOPE)
58        endif()
59        execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref --symbolic-full-name @{upstream}
60          WORKING_DIRECTORY ${path}
61          RESULT_VARIABLE git_result
62          OUTPUT_VARIABLE git_output
63          ERROR_QUIET)
64        if(git_result EQUAL 0)
65          string(REPLACE "/" ";" branch ${git_output})
66          list(GET branch 0 remote)
67        else()
68          set(remote "origin")
69        endif()
70        execute_process(COMMAND ${GIT_EXECUTABLE} remote get-url ${remote}
71          WORKING_DIRECTORY ${path}
72          RESULT_VARIABLE git_result
73          OUTPUT_VARIABLE git_output
74          ERROR_QUIET)
75        if(git_result EQUAL 0)
76          string(STRIP "${git_output}" git_output)
77          set(${repository} ${git_output} PARENT_SCOPE)
78        else()
79          set(${repository} ${path} PARENT_SCOPE)
80        endif()
81      endif()
82    endif()
83  endif()
84endfunction()
85
86function(get_source_info path revision repository)
87  if(EXISTS "${path}/.svn")
88    get_source_info_svn("${path}" revision_info repository_info)
89  else()
90    get_source_info_git("${path}" revision_info repository_info)
91  endif()
92  set(${repository} "${repository_info}" PARENT_SCOPE)
93  set(${revision} "${revision_info}" PARENT_SCOPE)
94endfunction()
95