From 5db1de3d3c4385927666f98f71d9cf5198fcdd51 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 20 Jan 2022 14:56:10 +0100 Subject: [PATCH] add GIT_HASH and TREXIO_USER_NAME variables to CMake --- CMakeLists.txt | 16 ++++++++++++++++ include/cmake_config.h.in | 2 ++ 2 files changed, 18 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bec4cd..7c2747f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,22 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/.git/config") else() message(FATAL_ERROR "EMACS not found. It is required to produce TREXIO source code from org-mode files.") endif() + # in case Git is not available, we default to "unknown" + set(GIT_HASH "unknown") + # find Git and if available set GIT_HASH variable + find_package(Git QUIET) + if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%H -n 1 + OUTPUT_VARIABLE GIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ERROR_QUIET) + endif() + # get the user name from the ${USER} env variable + set(TREXIO_USER_NAME $ENV{USER}) + # replace placeholders in the templace config.h.in file to produce config.h + # config.h is needed to insert TREXIO_PACKAGE_VERSION and TREXIO_GIT_HASH into trexio.h configure_file(${CMAKE_SOURCE_DIR}/include/cmake_config.h.in ${CMAKE_SOURCE_DIR}/include/config.h) endif() diff --git a/include/cmake_config.h.in b/include/cmake_config.h.in index 109829c..5bc20b3 100644 --- a/include/cmake_config.h.in +++ b/include/cmake_config.h.in @@ -3,3 +3,5 @@ #define VERSION_MAJOR @PROJECT_VERSION_MAJOR@ #define VERSION_MINOR @PROJECT_VERSION_MINOR@ #define VERSION_PATCH @PROJECT_VERSION_PATCH@ +#define GIT_HASH "@GIT_HASH@" +#define TREXIO_USER_NAME "@TREXIO_USER_NAME@"