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 |
| 23 | find_program(git_executable NAMES git git.exe git.cmd) |
| 24 | if( git_executable ) |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 25 | set(is_git_svn_rev_exact false) |
| 26 | execute_process(COMMAND ${git_executable} svn log --limit=1 --oneline |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 27 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 28 | TIMEOUT 5 |
| 29 | RESULT_VARIABLE git_result |
| 30 | OUTPUT_VARIABLE git_output) |
Oscar Fuentes | 59d5145 | 2010-08-05 02:22:51 +0000 | [diff] [blame] | 31 | if( git_result EQUAL 0 ) |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 32 | string(REGEX MATCH r[0-9]+ git_svn_rev ${git_output}) |
| 33 | string(SUBSTRING "${git_svn_rev}" 1 -1 git_svn_rev_number) |
Chandler Carruth | 78304a9 | 2011-12-10 10:04:38 +0000 | [diff] [blame^] | 34 | set(SVN_REVISION ${git_svn_rev_number} PARENT_SCOPE) |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 35 | set(git_svn_rev "-svn-${git_svn_rev}") |
| 36 | |
| 37 | # Determine if the HEAD points directly at a subversion revision. |
| 38 | execute_process(COMMAND ${git_executable} svn find-rev HEAD |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 39 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 40 | TIMEOUT 5 |
| 41 | RESULT_VARIABLE git_result |
| 42 | OUTPUT_VARIABLE git_output) |
| 43 | if( git_result EQUAL 0 ) |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 44 | string(STRIP "${git_output}" git_head_svn_rev_number) |
| 45 | if( git_head_svn_rev_number EQUAL git_svn_rev_number ) |
| 46 | set(is_git_svn_rev_exact true) |
| 47 | endif() |
Michael J. Spencer | 3df9c6b | 2010-09-08 20:49:40 +0000 | [diff] [blame] | 48 | endif() |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 49 | else() |
| 50 | set(git_svn_rev "") |
| 51 | endif() |
| 52 | execute_process(COMMAND |
| 53 | ${git_executable} show-ref --abbrev --hash --head HEAD |
| 54 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 55 | TIMEOUT 5 |
| 56 | RESULT_VARIABLE git_result |
| 57 | OUTPUT_VARIABLE git_output) |
| 58 | if( git_result EQUAL 0 AND NOT is_git_svn_rev_exact ) |
| 59 | string(STRIP "${git_output}" git_ref_id) |
Chandler Carruth | 78304a9 | 2011-12-10 10:04:38 +0000 | [diff] [blame^] | 60 | set(GIT_COMMIT ${git_ref_id} PARENT_SCOPE) |
Chandler Carruth | 1f8bd30 | 2011-12-10 09:41:13 +0000 | [diff] [blame] | 61 | set(result "${result}${git_svn_rev}-${git_ref_id}") |
| 62 | else() |
| 63 | set(result "${result}${git_svn_rev}") |
Oscar Fuentes | 59d5145 | 2010-08-05 02:22:51 +0000 | [diff] [blame] | 64 | endif() |
| 65 | endif() |
Oscar Fuentes | ee99317 | 2010-08-03 17:28:09 +0000 | [diff] [blame] | 66 | endif() |
| 67 | set(${VERS} ${result} PARENT_SCOPE) |
| 68 | endfunction(add_version_info_from_vcs) |