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 |
| 3 | # existence of certain subdirectories under CMAKE_CURRENT_SOURCE_DIR. |
| 4 | |
| 5 | function(add_version_info_from_vcs VERS) |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 6 | string(REPLACE "svn" "" result "${${VERS}}") |
Oscar Fuentes | c54ca79 | 2010-10-22 17:16:26 +0000 | [diff] [blame] | 7 | if( EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.svn" ) |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 8 | set(result "${result}svn") |
Oscar Fuentes | c54ca79 | 2010-10-22 17:16:26 +0000 | [diff] [blame] | 9 | # FindSubversion does not work with symlinks. See PR 8437 |
| 10 | if( NOT IS_SYMLINK "${CMAKE_CURRENT_SOURCE_DIR}" ) |
| 11 | find_package(Subversion) |
| 12 | endif() |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 13 | if( Subversion_FOUND ) |
| 14 | subversion_wc_info( ${CMAKE_CURRENT_SOURCE_DIR} Project ) |
| 15 | if( Project_WC_REVISION ) |
Chandler Carruth | 78304a9 | 2011-12-10 10:04:38 +0000 | [diff] [blame] | 16 | set(SVN_REVISION ${Project_WC_REVISION} PARENT_SCOPE) |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 17 | set(result "${result}-r${Project_WC_REVISION}") |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 18 | endif() |
| 19 | endif() |
| 20 | elseif( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git ) |
| 21 | set(result "${result}git") |
Oscar Fuentes | 59d5145 | 2010-08-05 02:22:51 +0000 | [diff] [blame] | 22 | # Try to get a ref-id |
Jim Grosbach | 0a2e27d | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 23 | if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/svn ) |
| 24 | find_program(git_executable NAMES git git.exe git.cmd) |
| 25 | if( git_executable ) |
| 26 | set(is_git_svn_rev_exact false) |
| 27 | execute_process(COMMAND ${git_executable} svn log --limit=1 --oneline |
| 28 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 29 | TIMEOUT 5 |
| 30 | RESULT_VARIABLE git_result |
| 31 | OUTPUT_VARIABLE git_output) |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 32 | if( git_result EQUAL 0 ) |
Jim Grosbach | 0a2e27d | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 33 | string(REGEX MATCH r[0-9]+ git_svn_rev ${git_output}) |
| 34 | string(LENGTH "${git_svn_rev}" rev_length) |
| 35 | math(EXPR rev_length "${rev_length}-1") |
| 36 | string(SUBSTRING "${git_svn_rev}" 1 ${rev_length} git_svn_rev_number) |
| 37 | set(SVN_REVISION ${git_svn_rev_number} PARENT_SCOPE) |
| 38 | set(git_svn_rev "-svn-${git_svn_rev}") |
| 39 | |
| 40 | # Determine if the HEAD points directly at a subversion revision. |
| 41 | execute_process(COMMAND ${git_executable} svn find-rev HEAD |
| 42 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 43 | TIMEOUT 5 |
| 44 | RESULT_VARIABLE git_result |
| 45 | OUTPUT_VARIABLE git_output) |
| 46 | if( git_result EQUAL 0 ) |
| 47 | string(STRIP "${git_output}" git_head_svn_rev_number) |
| 48 | if( git_head_svn_rev_number EQUAL git_svn_rev_number ) |
| 49 | set(is_git_svn_rev_exact true) |
| 50 | endif() |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 51 | endif() |
Jim Grosbach | 0a2e27d | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 52 | else() |
| 53 | set(git_svn_rev "") |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 54 | endif() |
Jim Grosbach | 0a2e27d | 2012-12-10 19:03:37 +0000 | [diff] [blame] | 55 | execute_process(COMMAND |
| 56 | ${git_executable} rev-parse --short HEAD |
| 57 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 58 | TIMEOUT 5 |
| 59 | RESULT_VARIABLE git_result |
| 60 | OUTPUT_VARIABLE git_output) |
| 61 | if( git_result EQUAL 0 AND NOT is_git_svn_rev_exact ) |
| 62 | string(STRIP "${git_output}" git_ref_id) |
| 63 | set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE) |
| 64 | set(result "${result}${git_svn_rev}-${git_ref_id}") |
| 65 | else() |
| 66 | set(result "${result}${git_svn_rev}") |
| 67 | endif() |
Oscar Fuentes | 59d5145 | 2010-08-05 02:22:51 +0000 | [diff] [blame] | 68 | endif() |
| 69 | endif() |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 70 | endif() |
| 71 | set(${VERS} ${result} PARENT_SCOPE) |
| 72 | endfunction(add_version_info_from_vcs) |