Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 1 | # Adds version control information to the variable VERS. For |
| 2 | # determining the Version Control System used (if any) it inspects the |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 3 | # existence of certain subdirectories under SOURCE_DIR (if provided as an |
| 4 | # extra argument, otherwise uses CMAKE_CURRENT_SOURCE_DIR). |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 5 | |
| 6 | function(add_version_info_from_vcs VERS) |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 7 | SET(SOURCE_DIR ${ARGV1}) |
| 8 | if("${SOURCE_DIR}" STREQUAL "") |
| 9 | SET(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 10 | endif() |
Chandler Carruth | 667f217 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 11 | string(REPLACE "svn" "" result "${${VERS}}") |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 12 | if( EXISTS "${SOURCE_DIR}/.svn" ) |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 13 | set(result "${result}svn") |
Oscar Fuentes | 26b1d20 | 2010-10-22 17:16:26 +0000 | [diff] [blame] | 14 | # FindSubversion does not work with symlinks. See PR 8437 |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 15 | if( NOT IS_SYMLINK "${SOURCE_DIR}" ) |
Oscar Fuentes | 26b1d20 | 2010-10-22 17:16:26 +0000 | [diff] [blame] | 16 | find_package(Subversion) |
| 17 | endif() |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 18 | if( Subversion_FOUND ) |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 19 | subversion_wc_info( ${SOURCE_DIR} Project ) |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 20 | if( Project_WC_REVISION ) |
Chandler Carruth | 7514da9 | 2011-12-10 10:04:38 +0000 | [diff] [blame] | 21 | set(SVN_REVISION ${Project_WC_REVISION} PARENT_SCOPE) |
Michael J. Spencer | ad39c92 | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 22 | set(result "${result}-r${Project_WC_REVISION}") |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 23 | endif() |
Chris Bieneman | 6bfebb7 | 2016-01-14 22:44:29 +0000 | [diff] [blame] | 24 | if( Project_WC_URL ) |
| 25 | set(LLVM_REPOSITORY ${Project_WC_URL} PARENT_SCOPE) |
| 26 | endif() |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 27 | endif() |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 28 | elseif( EXISTS ${SOURCE_DIR}/.git ) |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 29 | set(result "${result}git") |
Oscar Fuentes | fc21c3a | 2010-08-05 02:22:51 +0000 | [diff] [blame] | 30 | # Try to get a ref-id |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 31 | if( EXISTS ${SOURCE_DIR}/.git/svn ) |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 32 | find_program(git_executable NAMES git git.exe git.cmd) |
| 33 | if( git_executable ) |
| 34 | set(is_git_svn_rev_exact false) |
Geoff Berry | 5c6e076 | 2016-01-19 17:36:02 +0000 | [diff] [blame] | 35 | execute_process(COMMAND |
| 36 | ${git_executable} svn info |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 37 | WORKING_DIRECTORY ${SOURCE_DIR} |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 38 | TIMEOUT 5 |
| 39 | RESULT_VARIABLE git_result |
| 40 | OUTPUT_VARIABLE git_output) |
Michael J. Spencer | ad39c92 | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 41 | if( git_result EQUAL 0 ) |
Geoff Berry | 5c6e076 | 2016-01-19 17:36:02 +0000 | [diff] [blame] | 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}") |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 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 |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 54 | WORKING_DIRECTORY ${SOURCE_DIR} |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 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() |
Chandler Carruth | 667f217 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 63 | endif() |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 64 | else() |
| 65 | set(git_svn_rev "") |
Michael J. Spencer | ad39c92 | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 66 | endif() |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 67 | execute_process(COMMAND |
| 68 | ${git_executable} rev-parse --short HEAD |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 69 | WORKING_DIRECTORY ${SOURCE_DIR} |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 70 | TIMEOUT 5 |
| 71 | RESULT_VARIABLE git_result |
| 72 | OUTPUT_VARIABLE git_output) |
Mehdi Amini | 1d30fca | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 73 | |
Jim Grosbach | f7cbcf2 | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 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() |
Chris Bieneman | 6bfebb7 | 2016-01-14 22:44:29 +0000 | [diff] [blame] | 81 | |
Oscar Fuentes | fc21c3a | 2010-08-05 02:22:51 +0000 | [diff] [blame] | 82 | endif() |
| 83 | endif() |
Oscar Fuentes | 052c23c | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 84 | endif() |
| 85 | set(${VERS} ${result} PARENT_SCOPE) |
| 86 | endfunction(add_version_info_from_vcs) |