Oscar Fuentes | ee99317 | 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 | 3ab0c4f | 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 | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 5 | |
| 6 | function(add_version_info_from_vcs VERS) |
Mehdi Amini | 3ab0c4f | 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 | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 11 | string(REPLACE "svn" "" result "${${VERS}}") |
Mehdi Amini | 3ab0c4f | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 12 | if( EXISTS "${SOURCE_DIR}/.svn" ) |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 13 | set(result "${result}svn") |
Oscar Fuentes | c54ca79 | 2010-10-22 17:16:26 +0000 | [diff] [blame] | 14 | # FindSubversion does not work with symlinks. See PR 8437 |
Mehdi Amini | 3ab0c4f | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 15 | if( NOT IS_SYMLINK "${SOURCE_DIR}" ) |
Oscar Fuentes | c54ca79 | 2010-10-22 17:16:26 +0000 | [diff] [blame] | 16 | find_package(Subversion) |
| 17 | endif() |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 18 | if( Subversion_FOUND ) |
Mehdi Amini | 3ab0c4f | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 19 | subversion_wc_info( ${SOURCE_DIR} Project ) |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 20 | if( Project_WC_REVISION ) |
Chandler Carruth | 78304a9 | 2011-12-10 10:04:38 +0000 | [diff] [blame] | 21 | set(SVN_REVISION ${Project_WC_REVISION} PARENT_SCOPE) |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 22 | set(result "${result}-r${Project_WC_REVISION}") |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 23 | endif() |
Chris Bieneman | 92f6aec | 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 | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 27 | endif() |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 28 | else() |
Tom Stellard | cc946df | 2017-03-02 22:05:13 +0000 | [diff] [blame] | 29 | find_program(git_executable NAMES git git.exe git.cmd) |
| 30 | |
| 31 | if( git_executable ) |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 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 |
Tim Northover | f3dc328 | 2017-05-02 16:37:37 +0000 | [diff] [blame] | 36 | OUTPUT_VARIABLE git_dir |
| 37 | ERROR_QUIET) |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 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 |
Tim Northover | 0bda850 | 2017-04-28 16:06:00 +0000 | [diff] [blame] | 49 | OUTPUT_VARIABLE git_output |
| 50 | ERROR_QUIET) |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 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 "") |
Geoff Berry | 73c7b05 | 2016-01-19 17:36:02 +0000 | [diff] [blame] | 70 | endif() |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 71 | endif() |
Tom Stellard | cc946df | 2017-03-02 22:05:13 +0000 | [diff] [blame] | 72 | |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 73 | # Get the git ref id |
| 74 | execute_process(COMMAND |
| 75 | ${git_executable} rev-parse --short HEAD |
Mehdi Amini | 3ab0c4f | 2016-04-16 07:33:14 +0000 | [diff] [blame] | 76 | WORKING_DIRECTORY ${SOURCE_DIR} |
Jim Grosbach | 0a2e27d | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 77 | TIMEOUT 5 |
| 78 | RESULT_VARIABLE git_result |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 79 | OUTPUT_VARIABLE git_ref_id |
Tom Stellard | cc946df | 2017-03-02 22:05:13 +0000 | [diff] [blame] | 80 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 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}") |
Jim Grosbach | 0a2e27d | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 85 | else() |
Peter Collingbourne | 8b69529 | 2017-04-13 01:26:12 +0000 | [diff] [blame] | 86 | set(result "${result}${git_svn_rev}") |
Jim Grosbach | 0a2e27d | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 87 | endif() |
Tom Stellard | cc946df | 2017-03-02 22:05:13 +0000 | [diff] [blame] | 88 | endif() |
Oscar Fuentes | 59d5145 | 2010-08-05 02:22:51 +0000 | [diff] [blame] | 89 | endif() |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 90 | endif() |
| 91 | set(${VERS} ${result} PARENT_SCOPE) |
| 92 | endfunction(add_version_info_from_vcs) |