blob: 552fe77cdfb6890d43b34ff0fc478e29577951b7 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001# 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(add_version_info_from_vcs VERS)
7 SET(SOURCE_DIR ${ARGV1})
8 if("${SOURCE_DIR}" STREQUAL "")
9 SET(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
10 endif()
11 string(REPLACE "svn" "" result "${${VERS}}")
12 if( EXISTS "${SOURCE_DIR}/.svn" )
13 set(result "${result}svn")
14 # FindSubversion does not work with symlinks. See PR 8437
15 if( NOT IS_SYMLINK "${SOURCE_DIR}" )
16 find_package(Subversion)
17 endif()
18 if( Subversion_FOUND )
19 subversion_wc_info( ${SOURCE_DIR} Project )
20 if( Project_WC_REVISION )
21 set(SVN_REVISION ${Project_WC_REVISION} PARENT_SCOPE)
22 set(result "${result}-r${Project_WC_REVISION}")
23 endif()
24 if( Project_WC_URL )
25 set(LLVM_REPOSITORY ${Project_WC_URL} PARENT_SCOPE)
26 endif()
27 endif()
28 else()
29 find_program(git_executable NAMES git git.exe git.cmd)
30
31 if( git_executable )
32 # Run from a subdirectory to force git to print an absoute path.
33 execute_process(COMMAND ${git_executable} rev-parse --git-dir
34 WORKING_DIRECTORY ${SOURCE_DIR}/cmake
35 RESULT_VARIABLE git_result
36 OUTPUT_VARIABLE git_dir
37 ERROR_QUIET)
38 if(git_result EQUAL 0)
39 # Try to get a ref-id
40 string(STRIP "${git_dir}" git_dir)
41 set(result "${result}git")
42 if( EXISTS ${git_dir}/svn )
43 # Get the repository URL
44 execute_process(COMMAND
45 ${git_executable} svn info
46 WORKING_DIRECTORY ${SOURCE_DIR}
47 TIMEOUT 5
48 RESULT_VARIABLE git_result
49 OUTPUT_VARIABLE git_output
50 ERROR_QUIET)
51 if( git_result EQUAL 0 )
52 string(REGEX MATCH "URL: ([^ \n]*)" svn_url ${git_output})
53 if(svn_url)
54 set(LLVM_REPOSITORY ${CMAKE_MATCH_1} PARENT_SCOPE)
55 endif()
56 endif()
57
58 # Get the svn revision number for this git commit if one exists.
59 execute_process(COMMAND ${git_executable} svn find-rev HEAD
60 WORKING_DIRECTORY ${SOURCE_DIR}
61 TIMEOUT 5
62 RESULT_VARIABLE git_result
63 OUTPUT_VARIABLE git_head_svn_rev_number
64 OUTPUT_STRIP_TRAILING_WHITESPACE)
65 if( git_result EQUAL 0 AND git_output)
66 set(SVN_REVISION ${git_head_svn_rev_number} PARENT_SCOPE)
67 set(git_svn_rev "-svn-${git_head_svn_rev_number}")
68 else()
69 set(git_svn_rev "")
70 endif()
71 endif()
72
73 # Get the git ref id
74 execute_process(COMMAND
75 ${git_executable} rev-parse --short HEAD
76 WORKING_DIRECTORY ${SOURCE_DIR}
77 TIMEOUT 5
78 RESULT_VARIABLE git_result
79 OUTPUT_VARIABLE git_ref_id
80 OUTPUT_STRIP_TRAILING_WHITESPACE)
81
82 if( git_result EQUAL 0 )
83 set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE)
84 set(result "${result}${git_svn_rev}-${git_ref_id}")
85 else()
86 set(result "${result}${git_svn_rev}")
87 endif()
88 endif()
89 endif()
90 endif()
91 set(${VERS} ${result} PARENT_SCOPE)
92endfunction(add_version_info_from_vcs)