From 09b18c67c60d4e221737616a742d56f321bbbe9e Mon Sep 17 00:00:00 2001
From: Sebastian Eibl <sebastian.eibl@mpcdf.mpg.de>
Date: Mon, 18 Jul 2022 16:43:05 +0200
Subject: [PATCH] added 06_git_configure

---
 .gitlab-ci.yml                  |  7 ++++---
 06_git_configure/CMakeLists.txt | 20 ++++++++++++++++++++
 06_git_configure/README.md      |  9 +++++++++
 README.md                       |  1 +
 4 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 06_git_configure/CMakeLists.txt
 create mode 100644 06_git_configure/README.md

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 16698fd..8700f59 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 0000000..18cb96e
--- /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 0000000..2d25dae
--- /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 4fc4778..315c99b 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
-- 
GitLab