blob: 552fe77cdfb6890d43b34ff0fc478e29577951b7 [file] [log] [blame]
Oscar Fuentesee993172010-08-03 17:28:09 +00001# Adds version control information to the variable VERS. For
2# determining the Version Control System used (if any) it inspects the
Mehdi Amini3ab0c4f2016-04-16 07:33:14 +00003# existence of certain subdirectories under SOURCE_DIR (if provided as an
4# extra argument, otherwise uses CMAKE_CURRENT_SOURCE_DIR).
Oscar Fuentesee993172010-08-03 17:28:09 +00005
6function(add_version_info_from_vcs VERS)
Mehdi Amini3ab0c4f2016-04-16 07:33:14 +00007 SET(SOURCE_DIR ${ARGV1})
8 if("${SOURCE_DIR}" STREQUAL "")
9 SET(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
10 endif()
Chandler Carruth1f8bd302011-12-10 09:41:13 +000011 string(REPLACE "svn" "" result "${${VERS}}")
Mehdi Amini3ab0c4f2016-04-16 07:33:14 +000012 if( EXISTS "${SOURCE_DIR}/.svn" )
Oscar Fuentesee993172010-08-03 17:28:09 +000013 set(result "${result}svn")
Oscar Fuentesc54ca792010-10-22 17:16:26 +000014 # FindSubversion does not work with symlinks. See PR 8437
Mehdi Amini3ab0c4f2016-04-16 07:33:14 +000015 if( NOT IS_SYMLINK "${SOURCE_DIR}" )
Oscar Fuentesc54ca792010-10-22 17:16:26 +000016 find_package(Subversion)
17 endif()
Oscar Fuentesee993172010-08-03 17:28:09 +000018 if( Subversion_FOUND )
Mehdi Amini3ab0c4f2016-04-16 07:33:14 +000019 subversion_wc_info( ${SOURCE_DIR} Project )
Oscar Fuentesee993172010-08-03 17:28:09 +000020 if( Project_WC_REVISION )
Chandler Carruth78304a92011-12-10 10:04:38 +000021 set(SVN_REVISION ${Project_WC_REVISION} PARENT_SCOPE)
Michael J. Spencer3df9c6b2010-09-08 20:49:40 +000022 set(result "${result}-r${Project_WC_REVISION}")
Oscar Fuentesee993172010-08-03 17:28:09 +000023 endif()
Chris Bieneman92f6aec2016-01-14 22:44:29 +000024 if( Project_WC_URL )
25 set(LLVM_REPOSITORY ${Project_WC_URL} PARENT_SCOPE)
26 endif()
Oscar Fuentesee993172010-08-03 17:28:09 +000027 endif()
Peter Collingbourne8b695292017-04-13 01:26:12 +000028 else()
Tom Stellardcc946df2017-03-02 22:05:13 +000029 find_program(git_executable NAMES git git.exe git.cmd)
30
31 if( git_executable )
Peter Collingbourne8b695292017-04-13 01:26:12 +000032 # 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
Tim Northoverf3dc3282017-05-02 16:37:37 +000036 OUTPUT_VARIABLE git_dir
37 ERROR_QUIET)
Peter Collingbourne8b695292017-04-13 01:26:12 +000038 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
Tim Northover0bda8502017-04-28 16:06:00 +000049 OUTPUT_VARIABLE git_output
50 ERROR_QUIET)
Peter Collingbourne8b695292017-04-13 01:26:12 +000051 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 "")
Geoff Berry73c7b052016-01-19 17:36:02 +000070 endif()
Michael J. Spencer3df9c6b2010-09-08 20:49:40 +000071 endif()
Tom Stellardcc946df2017-03-02 22:05:13 +000072
Peter Collingbourne8b695292017-04-13 01:26:12 +000073 # Get the git ref id
74 execute_process(COMMAND
75 ${git_executable} rev-parse --short HEAD
Mehdi Amini3ab0c4f2016-04-16 07:33:14 +000076 WORKING_DIRECTORY ${SOURCE_DIR}
Jim Grosbach0a2e27d2012-12-10 19:03:37 +000077 TIMEOUT 5
78 RESULT_VARIABLE git_result
Peter Collingbourne8b695292017-04-13 01:26:12 +000079 OUTPUT_VARIABLE git_ref_id
Tom Stellardcc946df2017-03-02 22:05:13 +000080 OUTPUT_STRIP_TRAILING_WHITESPACE)
Peter Collingbourne8b695292017-04-13 01:26:12 +000081
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}")
Jim Grosbach0a2e27d2012-12-10 19:03:37 +000085 else()
Peter Collingbourne8b695292017-04-13 01:26:12 +000086 set(result "${result}${git_svn_rev}")
Jim Grosbach0a2e27d2012-12-10 19:03:37 +000087 endif()
Tom Stellardcc946df2017-03-02 22:05:13 +000088 endif()
Oscar Fuentes59d51452010-08-05 02:22:51 +000089 endif()
Oscar Fuentesee993172010-08-03 17:28:09 +000090 endif()
91 set(${VERS} ${result} PARENT_SCOPE)
92endfunction(add_version_info_from_vcs)