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 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 elseif( EXISTS ${SOURCE_DIR}/.git ) 29 set(result "${result}git") 30 # Try to get a ref-id 31 if( EXISTS ${SOURCE_DIR}/.git/svn ) 32 find_program(git_executable NAMES git git.exe git.cmd) 33 if( git_executable ) 34 set(is_git_svn_rev_exact false) 35 execute_process(COMMAND 36 ${git_executable} svn info 37 WORKING_DIRECTORY ${SOURCE_DIR} 38 TIMEOUT 5 39 RESULT_VARIABLE git_result 40 OUTPUT_VARIABLE git_output) 41 if( git_result EQUAL 0 ) 42 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}") 49 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 54 WORKING_DIRECTORY ${SOURCE_DIR} 55 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() 63 endif() 64 else() 65 set(git_svn_rev "") 66 endif() 67 execute_process(COMMAND 68 ${git_executable} rev-parse --short HEAD 69 WORKING_DIRECTORY ${SOURCE_DIR} 70 TIMEOUT 5 71 RESULT_VARIABLE git_result 72 OUTPUT_VARIABLE git_output) 73 74 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() 81 82 endif() 83 endif() 84 endif() 85 set(${VERS} ${result} PARENT_SCOPE) 86endfunction(add_version_info_from_vcs) 87