diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 16698fdfced660f7624a1678cd5912573c32e76f..8700f59a58bc2a0aa50b4c93295eaf194342494b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,8 +3,7 @@ image: gitlab-registry.mpcdf.mpg.de/mpcdf/module-image check-recipes: before_script: - module purge - - module load cmake/3.22 - - module load doxygen + - module load cmake/3.22 doxygen git - module load intel/21.5.0 - cmake --version @@ -37,4 +36,6 @@ check-recipes: - cmake -S 05_configure_file -B 05_build - cmake --build 05_build - - 05_build/version-info \ No newline at end of file + - 05_build/version-info + + - cmake -S 06_git_configure -B 06_build \ No newline at end of file diff --git a/06_git_configure/CMakeLists.txt b/06_git_configure/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..18cb96e11c56be02141ac1b95a04894904cf70fa --- /dev/null +++ b/06_git_configure/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.20) + +project(06_git_configure + LANGUAGES CXX) + +set(GIT_HASH "unknown") + +find_package(Git QUIET) +if (GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%H + OUTPUT_VARIABLE GIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + ) +endif () + +message(STATUS "Git hash: ${GIT_HASH}") + diff --git a/06_git_configure/README.md b/06_git_configure/README.md new file mode 100644 index 0000000000000000000000000000000000000000..2d25dae6a50227326451b0b5375a1964ec44c2f4 --- /dev/null +++ b/06_git_configure/README.md @@ -0,0 +1,9 @@ +# How to get the current git hash at configure time? + +With [find_package](https://cmake.org/cmake/help/latest/command/find_package.html) +we can locate the git executable followed by +[execute_process](https://cmake.org/cmake/help/latest/command/execute_process.html) +to get the current commit hash. The result is stored in a CMake variable. The git hash is +updated whenever CMake is run, i.e. at configure time. +This can be combined with [How to pass information from CMake to your code?](../05_configure_file) +to expose the git hash to the source code. \ No newline at end of file diff --git a/README.md b/README.md index 4fc4778a24cade617cc1da5d4cd31df7bc7cd6cf..315c99b2400016468feec9f9331035f2f345ea09 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ we run them in our CI. * [How to create custom targets to build the documentation, etc?](04_custom_targets) * [Local and cache variables?](https://cliutils.gitlab.io/modern-cmake/chapters/basics/variables.html) * [How to pass information from CMake to your code?](05_configure_file) + * [How to get the current git hash at configure time?](06_git_configure) ## Why CMake? * Automatically search for programs, libraries, and header files