From 45a4a0022f9468512ead82253e714e40b8b5b995 Mon Sep 17 00:00:00 2001 From: Roberto Di Remigio Date: Wed, 16 Nov 2016 11:42:06 +0100 Subject: [PATCH 1/3] Use only Git commands in git_info --- modules/git_info/git_info.cmake | 70 +++++++++++++++++++++++------ modules/git_info/git_info_sub.cmake | 64 -------------------------- 2 files changed, 57 insertions(+), 77 deletions(-) delete mode 100644 modules/git_info/git_info_sub.cmake diff --git a/modules/git_info/git_info.cmake b/modules/git_info/git_info.cmake index f33895c..8d33c20 100644 --- a/modules/git_info/git_info.cmake +++ b/modules/git_info/git_info.cmake @@ -5,22 +5,66 @@ # Git repository version and status information # to the program output. # -# autocmake.cfg configuration:: +# autocmake.yml configuration:: # -# fetch: https://github.com/scisoft/autocmake/raw/master/modules/git_info/git_info_sub.cmake -# https://github.com/scisoft/autocmake/raw/master/modules/git_info/git_info.h.in +# 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} +# _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) +function(generate_git_info_header _header_location _header_name) + 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( + ${PROJECT_SOURCE_DIR}/cmake/downloaded/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() diff --git a/modules/git_info/git_info_sub.cmake b/modules/git_info/git_info_sub.cmake deleted file mode 100644 index de2a6ad..0000000 --- a/modules/git_info/git_info_sub.cmake +++ /dev/null @@ -1,64 +0,0 @@ -#.rst: -# -# Creates git_info.h in the build directory. -# This file can be included in sources to print -# Git status information to the program output -# for reproducibility reasons. - -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} rev-list --max-count=1 --abbrev-commit HEAD - OUTPUT_VARIABLE _git_last_commit_hash - ERROR_QUIET - ) - - if(_git_last_commit_hash) - string(STRIP ${_git_last_commit_hash} _git_last_commit_hash) - endif() - - execute_process( - COMMAND ${GIT_EXECUTABLE} log --max-count=1 HEAD - OUTPUT_VARIABLE _git_last_commit - ERROR_QUIET - ) - - if(_git_last_commit) - string(REGEX MATCH "Author: +(.+) <.*@" temp "${_git_last_commit}") - set(_git_last_commit_author ${CMAKE_MATCH_1}) - string(STRIP ${_git_last_commit_author} _git_last_commit_author) - string(REGEX MATCH "Date: +(.+[0-9][0-9][0-9][0-9] [+-][0-9][0-9][0-9][0-9])" temp "${_git_last_commit}") - set(_git_last_commit_date ${CMAKE_MATCH_1}) - endif() - - execute_process( - COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD - OUTPUT_VARIABLE _git_branch - ERROR_QUIET - ) - - if(_git_branch) - string(STRIP ${_git_branch} _git_branch) - endif() -endif() - -# 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) -configure_file( - ${CMAKE_CURRENT_LIST_DIR}/git_info.h.in - ${_target_dir}/git_info.h - @ONLY - ) - -unset(_git_last_commit_hash) -unset(_git_last_commit_author) -unset(_git_last_commit_date) -unset(_git_branch) From 4efb2431614826ac3f5456130f5c71208ee182fa Mon Sep 17 00:00:00 2001 From: Roberto Di Remigio Date: Wed, 16 Nov 2016 15:31:57 +0100 Subject: [PATCH 2/3] Fine-tune and fix tests --- modules/git_info/git_info.cmake | 18 +++++++++++++----- test/fc_git_info/cmake/autocmake.yml | 4 ++-- test/fc_git_info/src/CMakeLists.txt | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/git_info/git_info.cmake b/modules/git_info/git_info.cmake index e8f85a4..f499ddc 100644 --- a/modules/git_info/git_info.cmake +++ b/modules/git_info/git_info.cmake @@ -9,12 +9,20 @@ # # 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" -# _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) -function(generate_git_info_header _header_location _header_name) +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") @@ -65,6 +73,6 @@ function(generate_git_info_header _header_location _header_name) add_custom_target( git_info - ALL DEPENDS ${PROJECT_BINARY_DIR}/git_info.h + ALL DEPENDS ${_header_location}/${_header_name} ) endfunction() diff --git a/test/fc_git_info/cmake/autocmake.yml b/test/fc_git_info/cmake/autocmake.yml index a8d32dd..af54162 100644 --- a/test/fc_git_info/cmake/autocmake.yml +++ b/test/fc_git_info/cmake/autocmake.yml @@ -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 diff --git a/test/fc_git_info/src/CMakeLists.txt b/test/fc_git_info/src/CMakeLists.txt index 2080ed2..5b9ac21 100644 --- a/test/fc_git_info/src/CMakeLists.txt +++ b/test/fc_git_info/src/CMakeLists.txt @@ -1,3 +1,4 @@ +generate_git_info_header() include_directories(${PROJECT_BINARY_DIR}) add_executable(example example.F90) From 310add559b1636a29abc4d72122d0443875b1fd9 Mon Sep 17 00:00:00 2001 From: Roberto Di Remigio Date: Thu, 17 Nov 2016 19:51:17 +0100 Subject: [PATCH 3/3] Path to git_info.h.in is no longer hardcoded --- modules/git_info/git_info.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/git_info/git_info.cmake b/modules/git_info/git_info.cmake index f499ddc..cc292e5 100644 --- a/modules/git_info/git_info.cmake +++ b/modules/git_info/git_info.cmake @@ -11,6 +11,8 @@ # fetch: # - "%(url_root)modules/git_info/git_info.h.in" +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) @@ -61,7 +63,7 @@ function(generate_git_info_header) endif() configure_file( - ${PROJECT_SOURCE_DIR}/cmake/downloaded/git_info.h.in + ${_current_dir}/git_info.h.in ${_header_location}/${_header_name} @ONLY )