blob: 8d56b66fa4781afd0dfbbdfb1bfdbf5688983fc1 [file] [log] [blame]
Oscar Fuentes052c23c2010-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 Amini1d30fca2016-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 Fuentes052c23c2010-08-03 17:28:09 +00005
6function(add_version_info_from_vcs VERS)
Mehdi Amini1d30fca2016-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 Carruth667f2172011-12-10 09:41:13 +000011 string(REPLACE "svn" "" result "${${VERS}}")
Mehdi Amini1d30fca2016-04-16 07:33:14 +000012 if( EXISTS "${SOURCE_DIR}/.svn" )
Oscar Fuentes052c23c2010-08-03 17:28:09 +000013 set(result "${result}svn")
Oscar Fuentes26b1d202010-10-22 17:16:26 +000014 # FindSubversion does not work with symlinks. See PR 8437
Mehdi Amini1d30fca2016-04-16 07:33:14 +000015 if( NOT IS_SYMLINK "${SOURCE_DIR}" )
Oscar Fuentes26b1d202010-10-22 17:16:26 +000016 find_package(Subversion)
17 endif()
Oscar Fuentes052c23c2010-08-03 17:28:09 +000018 if( Subversion_FOUND )
Mehdi Amini1d30fca2016-04-16 07:33:14 +000019 subversion_wc_info( ${SOURCE_DIR} Project )
Oscar Fuentes052c23c2010-08-03 17:28:09 +000020 if( Project_WC_REVISION )
Chandler Carruth7514da92011-12-10 10:04:38 +000021 set(SVN_REVISION ${Project_WC_REVISION} PARENT_SCOPE)
Michael J. Spencerad39c922010-09-08 20:49:40 +000022 set(result "${result}-r${Project_WC_REVISION}")
Oscar Fuentes052c23c2010-08-03 17:28:09 +000023 endif()
Chris Bieneman6bfebb72016-01-14 22:44:29 +000024 if( Project_WC_URL )
25 set(LLVM_REPOSITORY ${Project_WC_URL} PARENT_SCOPE)
26 endif()
Oscar Fuentes052c23c2010-08-03 17:28:09 +000027 endif()
Mehdi Amini1d30fca2016-04-16 07:33:14 +000028 elseif( EXISTS ${SOURCE_DIR}/.git )
Oscar Fuentes052c23c2010-08-03 17:28:09 +000029 set(result "${result}git")
Oscar Fuentesfc21c3a2010-08-05 02:22:51 +000030 # Try to get a ref-id
Mehdi Amini1d30fca2016-04-16 07:33:14 +000031 if( EXISTS ${SOURCE_DIR}/.git/svn )
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000032 find_program(git_executable NAMES git git.exe git.cmd)
33 if( git_executable )
34 set(is_git_svn_rev_exact false)
Geoff Berry5c6e0762016-01-19 17:36:02 +000035 execute_process(COMMAND
36 ${git_executable} svn info
Mehdi Amini1d30fca2016-04-16 07:33:14 +000037 WORKING_DIRECTORY ${SOURCE_DIR}
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000038 TIMEOUT 5
39 RESULT_VARIABLE git_result
40 OUTPUT_VARIABLE git_output)
Michael J. Spencerad39c922010-09-08 20:49:40 +000041 if( git_result EQUAL 0 )
Geoff Berry5c6e0762016-01-19 17:36:02 +000042 string(REGEX MATCH "URL: ([^ \n]*)" svn_url ${git_output})
43 if(svn_url)
44 set(LLVM_REPOSITORY ${CMAKE_MATCH_1} PARENT_SCOPE)
45 endif()
46
47 string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
48 "\\2" git_svn_rev_number "${git_output}")
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000049 set(SVN_REVISION ${git_svn_rev_number} PARENT_SCOPE)
50 set(git_svn_rev "-svn-${git_svn_rev}")
51
52 # Determine if the HEAD points directly at a subversion revision.
53 execute_process(COMMAND ${git_executable} svn find-rev HEAD
Mehdi Amini1d30fca2016-04-16 07:33:14 +000054 WORKING_DIRECTORY ${SOURCE_DIR}
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000055 TIMEOUT 5
56 RESULT_VARIABLE git_result
57 OUTPUT_VARIABLE git_output)
58 if( git_result EQUAL 0 )
59 string(STRIP "${git_output}" git_head_svn_rev_number)
60 if( git_head_svn_rev_number EQUAL git_svn_rev_number )
61 set(is_git_svn_rev_exact true)
62 endif()
Chandler Carruth667f2172011-12-10 09:41:13 +000063 endif()
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000064 else()
65 set(git_svn_rev "")
Michael J. Spencerad39c922010-09-08 20:49:40 +000066 endif()
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000067 execute_process(COMMAND
68 ${git_executable} rev-parse --short HEAD
Mehdi Amini1d30fca2016-04-16 07:33:14 +000069 WORKING_DIRECTORY ${SOURCE_DIR}
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000070 TIMEOUT 5
71 RESULT_VARIABLE git_result
72 OUTPUT_VARIABLE git_output)
Mehdi Amini1d30fca2016-04-16 07:33:14 +000073
Jim Grosbachf7cbcf22012-12-10 19:03:37 +000074 if( git_result EQUAL 0 AND NOT is_git_svn_rev_exact )
75 string(STRIP "${git_output}" git_ref_id)
76 set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE)
77 set(result "${result}${git_svn_rev}-${git_ref_id}")
78 else()
79 set(result "${result}${git_svn_rev}")
80 endif()
Chris Bieneman6bfebb72016-01-14 22:44:29 +000081
Oscar Fuentesfc21c3a2010-08-05 02:22:51 +000082 endif()
83 endif()
Oscar Fuentes052c23c2010-08-03 17:28:09 +000084 endif()
85 set(${VERS} ${result} PARENT_SCOPE)
86endfunction(add_version_info_from_vcs)