From e906920d26c00c90aafce23b4339a3cfb2cf9fc9 Mon Sep 17 00:00:00 2001
From: Christian Willner <34183939+vaeng@users.noreply.github.com>
Date: Mon, 9 Oct 2023 09:42:37 +0200
Subject: [PATCH] feat(project-template): add VersionInfo.h generation (#40)

---
 cmake/set_version_numbers.cmake  | 9 +++++++++
 cmake/version_info_template.h.in | 9 +++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 cmake/version_info_template.h.in

diff --git a/cmake/set_version_numbers.cmake b/cmake/set_version_numbers.cmake
index f6c1c9f..333afd4 100644
--- a/cmake/set_version_numbers.cmake
+++ b/cmake/set_version_numbers.cmake
@@ -35,3 +35,12 @@ endif( ${PROJECT_NAME}_BUILDVERSION )
 
 set(${PROJECT_NAME}_FULL_LIBRARY_VERSION ${${PROJECT_NAME}_SOVERSION}.${${PROJECT_NAME}_PATCH_VERSION})
 
+# The following generates a cpp header file that can be used to access the CMAKE version info.
+# The "VersionInfo.h" is available in the project's build directory and included in the build.
+
+string(REGEX REPLACE "^0" "" ${PROJECT_NAME}_MAJOR_VERSION_INT ${${PROJECT_NAME}_MAJOR_VERSION})
+string(REGEX REPLACE "^0" "" ${PROJECT_NAME}_MINOR_VERSION_INT ${${PROJECT_NAME}_MINOR_VERSION})
+string(REGEX REPLACE "^0" "" ${PROJECT_NAME}_PATCH_VERSION_INT ${${PROJECT_NAME}_PATCH_VERSION})
+
+configure_file(cmake/version_info_template.h.in "${PROJECT_BINARY_DIR}/generated/VersionInfo.h")
+include_directories(${PROJECT_BINARY_DIR}/generated)
diff --git a/cmake/version_info_template.h.in b/cmake/version_info_template.h.in
new file mode 100644
index 0000000..7659c54
--- /dev/null
+++ b/cmake/version_info_template.h.in
@@ -0,0 +1,9 @@
+#pragma once
+
+namespace ChimeraTK::VersionInfo {
+
+  const int major{${${PROJECT_NAME}_MAJOR_VERSION_INT}};
+  const int minor{${${PROJECT_NAME}_MINOR_VERSION_INT}};
+  const int applicationPatch{${${PROJECT_NAME}_PATCH_VERSION_INT}};
+
+} // namespace ChimeraTK::VersionInfo
-- 
GitLab