|
9 | 9 | #
|
10 | 10 | # url_root: https://github.com/coderefinery/autocmake/raw/master/
|
11 | 11 | # fetch:
|
12 |
| -# - "%(url_root)modules/git_info/git_info_sub.cmake" |
13 | 12 | # - "%(url_root)modules/git_info/git_info.h.in"
|
14 | 13 |
|
15 |
| -# CMAKE_CURRENT_LIST_DIR is undefined in CMake 2.8.2 |
16 |
| -# see https://public.kitware.com/Bug/print_bug_page.php?bug_id=11675 |
17 |
| -# workaround: create CMAKE_CURRENT_LIST_DIR |
18 |
| -get_filename_component(CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_FILE} PATH) |
19 |
| -add_custom_command( |
20 |
| - OUTPUT ${PROJECT_BINARY_DIR}/git_info.h |
21 |
| - COMMAND ${CMAKE_COMMAND} -D_target_dir=${PROJECT_BINARY_DIR} -P git_info_sub.cmake |
22 |
| - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} |
| 14 | +function(generate_git_info_header) |
| 15 | + # _header_location: where the Git info header file should be generated |
| 16 | + # _header_name: the Git info header name, complete with extension (.h, .hpp, .hxx or whatever) |
| 17 | + if(${ARGC} EQUAL 2) |
| 18 | + set(_header_location ${ARGV0}) |
| 19 | + set(_header_name ${ARGV1}) |
| 20 | + elseif(${ARGC} EQUAL 0) |
| 21 | + set(_header_location ${PROJECT_BINARY_DIR}) |
| 22 | + set(_header_name git_info.h) |
| 23 | + else() |
| 24 | + message(FATAL_ERROR "generate_git_info_header function accepts either two or no arguments") |
| 25 | + endif() |
| 26 | + find_package(Git) |
| 27 | + |
| 28 | + set(_git_last_commit_hash "unknown") |
| 29 | + set(_git_last_commit_author "unknown") |
| 30 | + set(_git_last_commit_date "unknown") |
| 31 | + set(_git_branch "unknown") |
| 32 | + |
| 33 | + if(GIT_FOUND) |
| 34 | + execute_process( |
| 35 | + COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%h -n 1 |
| 36 | + OUTPUT_VARIABLE _git_last_commit_hash |
| 37 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 38 | + ERROR_QUIET |
| 39 | + ) |
| 40 | + |
| 41 | + execute_process( |
| 42 | + COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%aN -n 1 |
| 43 | + OUTPUT_VARIABLE _git_last_commit_author |
| 44 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 45 | + ERROR_QUIET |
| 46 | + ) |
| 47 | + |
| 48 | + execute_process( |
| 49 | + COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%ad -n 1 |
| 50 | + OUTPUT_VARIABLE _git_last_commit_date |
| 51 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 52 | + ERROR_QUIET |
| 53 | + ) |
| 54 | + |
| 55 | + execute_process( |
| 56 | + COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD |
| 57 | + OUTPUT_VARIABLE _git_branch |
| 58 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 59 | + ERROR_QUIET |
| 60 | + ) |
| 61 | + endif() |
| 62 | + |
| 63 | + configure_file( |
| 64 | + ${PROJECT_SOURCE_DIR}/cmake/downloaded/git_info.h.in |
| 65 | + ${_header_location}/${_header_name} |
| 66 | + @ONLY |
23 | 67 | )
|
24 | 68 |
|
25 |
| -add_custom_target( |
| 69 | + unset(_git_last_commit_hash) |
| 70 | + unset(_git_last_commit_author) |
| 71 | + unset(_git_last_commit_date) |
| 72 | + unset(_git_branch) |
| 73 | + |
| 74 | + add_custom_target( |
26 | 75 | git_info
|
27 |
| - ALL DEPENDS ${PROJECT_BINARY_DIR}/git_info.h |
| 76 | + ALL DEPENDS ${_header_location}/${_header_name} |
28 | 77 | )
|
| 78 | +endfunction() |
0 commit comments