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 CMAKE_CURRENT_SOURCE_DIR.
4
5function(add_version_info_from_vcs VERS)
6  string(REPLACE "svn" "" result "${${VERS}}")
7  if( EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn" )
8    set(result "${result}svn")
9    # FindSubversion does not work with symlinks. See PR 8437
10    if( NOT IS_SYMLINK "${CMAKE_CURRENT_SOURCE_DIR}" )
11      find_package(Subversion)
12    endif()
13    if( Subversion_FOUND )
14      subversion_wc_info( ${CMAKE_CURRENT_SOURCE_DIR} Project )
15      if( Project_WC_REVISION )
16        set(SVN_REVISION ${Project_WC_REVISION} PARENT_SCOPE)
17        set(result "${result}-r${Project_WC_REVISION}")
18      endif()
19    endif()
20  elseif( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git )
21    set(result "${result}git")
22    # Try to get a ref-id
23    find_program(git_executable NAMES git git.exe git.cmd)
24    if( git_executable )
25      set(is_git_svn_rev_exact false)
26      execute_process(COMMAND ${git_executable} svn log --limit=1 --oneline
27                      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
28                      TIMEOUT 5
29                      RESULT_VARIABLE git_result
30                      OUTPUT_VARIABLE git_output)
31      if( git_result EQUAL 0 )
32        string(REGEX MATCH r[0-9]+ git_svn_rev ${git_output})
33        string(LENGTH "${git_svn_rev}" rev_length)
34        math(EXPR rev_length "${rev_length}-1")
35        string(SUBSTRING "${git_svn_rev}" 1 ${rev_length} git_svn_rev_number)
36        set(SVN_REVISION ${git_svn_rev_number} PARENT_SCOPE)
37        set(git_svn_rev "-svn-${git_svn_rev}")
38
39        # Determine if the HEAD points directly at a subversion revision.
40        execute_process(COMMAND ${git_executable} svn find-rev HEAD
41                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
42                        TIMEOUT 5
43                        RESULT_VARIABLE git_result
44                        OUTPUT_VARIABLE git_output)
45        if( git_result EQUAL 0 )
46          string(STRIP "${git_output}" git_head_svn_rev_number)
47          if( git_head_svn_rev_number EQUAL git_svn_rev_number )
48            set(is_git_svn_rev_exact true)
49          endif()
50        endif()
51      else()
52        set(git_svn_rev "")
53      endif()
54      execute_process(COMMAND
55                      ${git_executable} rev-parse --short HEAD
56                      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
57                      TIMEOUT 5
58                      RESULT_VARIABLE git_result
59                      OUTPUT_VARIABLE git_output)
60      if( git_result EQUAL 0 AND NOT is_git_svn_rev_exact )
61        string(STRIP "${git_output}" git_ref_id)
62        set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE)
63        set(result "${result}${git_svn_rev}-${git_ref_id}")
64      else()
65        set(result "${result}${git_svn_rev}")
66      endif()
67    endif()
68  endif()
69  set(${VERS} ${result} PARENT_SCOPE)
70endfunction(add_version_info_from_vcs)
71