Skip to content

Commit

Permalink
Merge pull request #179 from robertodr/master
Browse files Browse the repository at this point in the history
Use only Git commands in `git_info`
  • Loading branch information
bast committed Nov 18, 2016
2 parents 8ba73f5 + 310add5 commit caf8d63
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 77 deletions.
74 changes: 63 additions & 11 deletions modules/git_info/git_info.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,72 @@
#
# url_root: https://github.com/coderefinery/autocmake/raw/master/
# fetch:
# - "%(url_root)modules/git_info/git_info_sub.cmake"
# - "%(url_root)modules/git_info/git_info.h.in"

# CMAKE_CURRENT_LIST_DIR is undefined in CMake 2.8.2
# see https://public.kitware.com/Bug/print_bug_page.php?bug_id=11675
# workaround: create CMAKE_CURRENT_LIST_DIR
get_filename_component(CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/git_info.h
COMMAND ${CMAKE_COMMAND} -D_target_dir=${PROJECT_BINARY_DIR} -P git_info_sub.cmake
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
get_filename_component(_current_dir ${CMAKE_CURRENT_LIST_FILE} PATH)

function(generate_git_info_header)
# _header_location: where the Git info header file should be generated
# _header_name: the Git info header name, complete with extension (.h, .hpp, .hxx or whatever)
if(${ARGC} EQUAL 2)
set(_header_location ${ARGV0})
set(_header_name ${ARGV1})
elseif(${ARGC} EQUAL 0)
set(_header_location ${PROJECT_BINARY_DIR})
set(_header_name git_info.h)
else()
message(FATAL_ERROR "generate_git_info_header function accepts either two or no arguments")
endif()
find_package(Git)

set(_git_last_commit_hash "unknown")
set(_git_last_commit_author "unknown")
set(_git_last_commit_date "unknown")
set(_git_branch "unknown")

if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%h -n 1
OUTPUT_VARIABLE _git_last_commit_hash
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%aN -n 1
OUTPUT_VARIABLE _git_last_commit_author
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%ad -n 1
OUTPUT_VARIABLE _git_last_commit_date
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
OUTPUT_VARIABLE _git_branch
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()

configure_file(
${_current_dir}/git_info.h.in
${_header_location}/${_header_name}
@ONLY
)

add_custom_target(
unset(_git_last_commit_hash)
unset(_git_last_commit_author)
unset(_git_last_commit_date)
unset(_git_branch)

add_custom_target(
git_info
ALL DEPENDS ${PROJECT_BINARY_DIR}/git_info.h
ALL DEPENDS ${_header_location}/${_header_name}
)
endfunction()
64 changes: 0 additions & 64 deletions modules/git_info/git_info_sub.cmake

This file was deleted.

4 changes: 2 additions & 2 deletions test/fc_git_info/cmake/autocmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ modules:
- source: ../../../modules/fc.cmake
- default_build_paths:
- source: ../../../modules/default_build_paths.cmake
- src:
- source: ../../../modules/src.cmake
- git_info:
- source: ../../../modules/git_info/git_info.cmake
- src:
- source: ../../../modules/src.cmake
1 change: 1 addition & 0 deletions test/fc_git_info/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
generate_git_info_header()
include_directories(${PROJECT_BINARY_DIR})

add_executable(example example.F90)

0 comments on commit caf8d63

Please sign in to comment.