Use a simpler and more reliable command for converting from HEAD to
[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(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(SUBSTRING "${git_svn_rev}" 1 -1 git_svn_rev_number)
34         set(SVN_REVISION ${git_svn_rev_number} PARENT_SCOPE)
35         set(git_svn_rev "-svn-${git_svn_rev}")
36
37         # Determine if the HEAD points directly at a subversion revision.
38         execute_process(COMMAND ${git_executable} svn find-rev HEAD
39                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
40                         TIMEOUT 5
41                         RESULT_VARIABLE git_result
42                         OUTPUT_VARIABLE git_output)
43         if( git_result EQUAL 0 )
44           string(STRIP "${git_output}" git_head_svn_rev_number)
45           if( git_head_svn_rev_number EQUAL git_svn_rev_number )
46             set(is_git_svn_rev_exact true)
47           endif()
48         endif()
49       else()
50         set(git_svn_rev "")
51       endif()
52       execute_process(COMMAND
53                       ${git_executable} rev-parse --short HEAD
54                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
55                       TIMEOUT 5
56                       RESULT_VARIABLE git_result
57                       OUTPUT_VARIABLE git_output)
58       if( git_result EQUAL 0 AND NOT is_git_svn_rev_exact )
59         string(STRIP "${git_output}" git_ref_id)
60         set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE)
61         set(result "${result}${git_svn_rev}-${git_ref_id}")
62       else()
63         set(result "${result}${git_svn_rev}")
64       endif()
65     endif()
66   endif()
67   set(${VERS} ${result} PARENT_SCOPE)
68 endfunction(add_version_info_from_vcs)