At the request of Michael Spencer, make the VCS version detection logic
[oota-llvm.git] / cmake / modules / VersionFromVCS.cmake
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
5 function(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(result "${result}-r${Project_WC_REVISION}")
17       endif()
18     endif()
19   elseif( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git )
20     set(result "${result}git")
21     # Try to get a ref-id
22     find_program(git_executable NAMES git git.exe git.cmd)
23     if( git_executable )
24       set(is_git_svn_rev_exact false)
25       execute_process(COMMAND ${git_executable} svn log --limit=1 --oneline
26                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
27                       TIMEOUT 5
28                       RESULT_VARIABLE git_result
29                       OUTPUT_VARIABLE git_output)
30       if( git_result EQUAL 0 )
31         string(REGEX MATCH r[0-9]+ git_svn_rev ${git_output})
32         string(SUBSTRING "${git_svn_rev}" 1 -1 git_svn_rev_number)
33         set(git_svn_rev "-svn-${git_svn_rev}")
34
35         # Determine if the HEAD points directly at a subversion revision.
36         execute_process(COMMAND ${git_executable} svn find-rev HEAD
37                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
38                         TIMEOUT 5
39                         RESULT_VARIABLE git_result
40                         OUTPUT_VARIABLE git_output)
41         if( git_result EQUAL 0 )
42           string(STRIP "${git_output}" git_head_svn_rev_number)
43           if( git_head_svn_rev_number EQUAL git_svn_rev_number )
44             set(is_git_svn_rev_exact true)
45           endif()
46         endif()
47       else()
48         set(git_svn_rev "")
49       endif()
50       execute_process(COMMAND
51                       ${git_executable} show-ref --abbrev --hash --head HEAD
52                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
53                       TIMEOUT 5
54                       RESULT_VARIABLE git_result
55                       OUTPUT_VARIABLE git_output)
56       if( git_result EQUAL 0 AND NOT is_git_svn_rev_exact )
57         string(STRIP "${git_output}" git_ref_id)
58         set(result "${result}${git_svn_rev}-${git_ref_id}")
59       else()
60         set(result "${result}${git_svn_rev}")
61       endif()
62     endif()
63   endif()
64   set(${VERS} ${result} PARENT_SCOPE)
65 endfunction(add_version_info_from_vcs)