From 901ec237be0f53f7ef1ff5d789c7c3a79bd98bc1 Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Fri, 6 Dec 2024 13:03:02 +0000
Subject: [PATCH] Version information are generated by CMakeLists.txt (+add a
 function to show version information.

---
 CMakeLists.txt                                | 24 ++++++++++--
 include/aidge/aidge.hpp                       |  1 +
 .../aidge/utils/sys_info/CoreVersionInfo.hpp  | 37 +++++++++++++++++++
 include/aidge/version.h.in                    | 11 ++++++
 python_binding/pybind_core.cpp                |  2 +
 .../utils/sys_info/pybind_CoreVersionInfo.cpp | 11 ++++++
 6 files changed, 82 insertions(+), 4 deletions(-)
 create mode 100644 include/aidge/utils/sys_info/CoreVersionInfo.hpp
 create mode 100644 include/aidge/version.h.in
 create mode 100644 python_binding/utils/sys_info/pybind_CoreVersionInfo.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3a1dc22b4..0d308bfc0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,17 +3,29 @@ set(CXX_STANDARD 14)
 
 file(STRINGS "${CMAKE_SOURCE_DIR}/version.txt" version)
 
+# Parse version.txt to retrieve Major, Minor and Path
+string(REGEX MATCH "([0-9]+\\.[0-9]+\\.[0-9]+)" _ MATCHES ${version})
+set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1})
+set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_2})
+set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_3})
+
+# Retrieve latest git commit
+execute_process(
+    COMMAND git rev-parse --short HEAD
+    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+    OUTPUT_VARIABLE GIT_COMMIT_HASH
+    OUTPUT_STRIP_TRAILING_WHITESPACE
+    ERROR_QUIET
+)
+
 project(aidge_core
         VERSION ${version}
         DESCRIPTION "Core algorithms for operators and graph of the AIDGE framework"
         LANGUAGES CXX)
-message(STATUS "Project name: ${CMAKE_PROJECT_NAME}")
-message(STATUS "Project version: ${version}")
-add_definitions(-DPROJECT_VERSION="${version}")
 
 message(STATUS "Project name: ${CMAKE_PROJECT_NAME}")
 message(STATUS "Project version: ${version}")
-
+message(STATUS "Latest git commit: ${GIT_COMMIT_HASH}")
 # helper for LSP users
 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
 
@@ -29,6 +41,10 @@ option(TEST "Enable tests" ON)
 option(COVERAGE "Enable coverage" OFF)
 option(ENABLE_ASAN "Enable ASan (AddressSanitizer) for runtime analysis of memory use (over/underflow, memory leak, ...)" OFF)
 
+configure_file(
+    "${CMAKE_CURRENT_SOURCE_DIR}/include/aidge/version.h.in"
+    "${CMAKE_CURRENT_SOURCE_DIR}/include/aidge/version.h"
+)
 
 ##############################################
 # Import utils CMakeLists
diff --git a/include/aidge/aidge.hpp b/include/aidge/aidge.hpp
index a06202696..2756d5ac7 100644
--- a/include/aidge/aidge.hpp
+++ b/include/aidge/aidge.hpp
@@ -11,6 +11,7 @@
 
 #ifndef AIDGE_IMPORTS_H_
 #define AIDGE_IMPORTS_H_
+#include "version.h"
 
 #include "aidge/backend/OperatorImpl.hpp"
 #include "aidge/backend/TensorImpl.hpp"
diff --git a/include/aidge/utils/sys_info/CoreVersionInfo.hpp b/include/aidge/utils/sys_info/CoreVersionInfo.hpp
new file mode 100644
index 000000000..e19ef4965
--- /dev/null
+++ b/include/aidge/utils/sys_info/CoreVersionInfo.hpp
@@ -0,0 +1,37 @@
+#ifndef AIDGE_UTILS_SYS_INFO_CORE_VERSION_INFO_H
+#define AIDGE_UTILS_SYS_INFO_CORE_VERSION_INFO_H
+
+#include "aidge/utils/Log.hpp"
+#include "aidge/version.h"
+
+namespace Aidge {
+
+constexpr inline const char * getCoreProjectVersion(){
+    return PROJECT_VERSION;
+}
+
+constexpr inline const char * getCoreGitHash(){
+    return PROJECT_GIT_HASH;
+}
+
+void showCoreVersion() {
+    Log::info("Aidge core: {} ({}), {} {}", getCoreProjectVersion(), getCoreGitHash(), __DATE__, __TIME__);
+        // Compiler version
+    #if defined(__clang__)
+    /* Clang/LLVM. ---------------------------------------------- */
+        Log::info("Clang/LLVM compiler version: {}.{}.{}\n", __clang_major__ , __clang_minor__, __clang_patchlevel__);
+    #elif defined(__ICC) || defined(__INTEL_COMPILER)
+    /* Intel ICC/ICPC. ------------------------------------------ */
+        Log::info("Intel ICC/ICPC compiler version: {}\n", __INTEL_COMPILER);
+    #elif defined(__GNUC__) || defined(__GNUG__)
+    /* GNU GCC/G++. --------------------------------------------- */
+        Log::info("GNU GCC/G++ compiler version: {}.{}.{}", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
+    #elif defined(_MSC_VER)
+    /* Microsoft Visual Studio. --------------------------------- */
+        Log::info("Microsoft Visual Studio compiler version: {}\n", _MSC_VER);
+    #else
+        Log::info("Unknown compiler\n");
+    #endif
+}
+}  // namespace Aidge
+#endif  // AIDGE_UTILS_SYS_INFO_CORE_VERSION_INFO_H
diff --git a/include/aidge/version.h.in b/include/aidge/version.h.in
new file mode 100644
index 000000000..4b876f630
--- /dev/null
+++ b/include/aidge/version.h.in
@@ -0,0 +1,11 @@
+#ifndef VERSION_H
+#define VERSION_H
+
+namespace Aidge {
+static constexpr const int PROJECT_VERSION_MAJOR = @PROJECT_VERSION_MAJOR@;
+static constexpr const int PROJECT_VERSION_MINOR = @PROJECT_VERSION_MINOR@;
+static constexpr const int PROJECT_VERSION_PATCH = @PROJECT_VERSION_PATCH@;
+static constexpr const char * PROJECT_VERSION = "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@";
+static constexpr const char * PROJECT_GIT_HASH = "@GIT_COMMIT_HASH@";
+}
+#endif // VERSION_H
diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp
index f572c024d..eccbebd2f 100644
--- a/python_binding/pybind_core.cpp
+++ b/python_binding/pybind_core.cpp
@@ -16,6 +16,7 @@
 namespace py = pybind11;
 
 namespace Aidge {
+void init_CoreSysInfo(py::module&);
 void init_Random(py::module&);
 void init_Data(py::module&);
 void init_Database(py::module&);
@@ -104,6 +105,7 @@ void init_TensorUtils(py::module&);
 void init_Filler(py::module&);
 
 void init_Aidge(py::module& m) {
+    init_CoreSysInfo(m);
     init_Random(m);
 
     init_Data(m);
diff --git a/python_binding/utils/sys_info/pybind_CoreVersionInfo.cpp b/python_binding/utils/sys_info/pybind_CoreVersionInfo.cpp
new file mode 100644
index 000000000..1e487aef4
--- /dev/null
+++ b/python_binding/utils/sys_info/pybind_CoreVersionInfo.cpp
@@ -0,0 +1,11 @@
+#include <pybind11/pybind11.h>
+#include "aidge/utils/sys_info/CoreVersionInfo.hpp"
+
+namespace py = pybind11;
+namespace Aidge {
+void init_CoreSysInfo(py::module& m){
+    m.def("show_core_version", &showCoreVersion);
+    m.def("get_core_project_version", &getCoreProjectVersion);
+    m.def("get_core_git_hash", &getCoreGitHash);
+}
+}
-- 
GitLab