From 103af00bd0d1fba388063a4702625546fd409d0c Mon Sep 17 00:00:00 2001
From: Adam Knapp <adam.knapp@ericsson.com>
Date: Wed, 21 Jul 2021 18:36:59 +0200
Subject: [PATCH] Add the commit id to the version printout (issue #561, epic
 &29)

---
 Makefile.cfg              |  3 +++
 common/.gitignore         |  1 +
 common/Makefile           | 22 +++++++++++++++++++++-
 common/version_internal.h |  3 +++
 compiler2/Makefile        |  2 +-
 compiler2/main.cc         |  4 ++++
 core/Makefile             |  2 +-
 core/Parallel_main.cc     |  8 ++++++--
 core/Single_main.cc       |  8 ++++++--
 9 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/Makefile.cfg b/Makefile.cfg
index 1af48ae1c..1f31fcda1 100644
--- a/Makefile.cfg
+++ b/Makefile.cfg
@@ -45,6 +45,9 @@ endif
 
 # Set these variables to fit your system:
 
+# Set it to 'yes' to display git commit id beside the version
+GIT_COMMIT_ID := yes
+
 # The target directory where the Test Executor will be installed to:
 TTCN3_DIR := ${TOP}/Install
 
diff --git a/common/.gitignore b/common/.gitignore
index 1a5ba15b2..2d1233e93 100644
--- a/common/.gitignore
+++ b/common/.gitignore
@@ -14,3 +14,4 @@ pattern_uni.output
 config_preproc_p.output
 config_preproc_p.tab.cc
 config_preproc_p.tab.hh
+git_version.c
diff --git a/common/Makefile b/common/Makefile
index f8989dec4..6c86b4e1b 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -26,6 +26,26 @@
 TOP := ..
 include $(TOP)/Makefile.cfg
 
+# Generating git_version.c according to GIT_COMMIT_ID
+ifeq ($(GIT_COMMIT_ID), yes)
+  $(shell echo -e "// this file was generated by make\n#include \"version_internal.h\"\n\nchar const *const GIT_COMMIT_ID = \"$$(git describe --always --dirty --match 'NOT A TAG')\";" > git_version.c.tmp; \
+  if [ ! -f git_version.c.tmp ]; then \
+    echo -e "// this file was generated by make\n#include \"version_internal.h\"\n\nchar const *const GIT_COMMIT_ID = \"\";" > git_version.c.tmp; \
+  fi; \
+  if diff -q git_version.c.tmp git_version.c >/dev/null 2>&1; then \
+    rm git_version.c.tmp; \
+  else \
+    mv git_version.c.tmp git_version.c; \
+  fi)
+else
+  $(shell echo -e "// this file was generated by make\n#include \"version_internal.h\"\n\nchar const *const GIT_COMMIT_ID = \"\";" > git_version.c.tmp; \
+  if diff -q git_version.c.tmp git_version.c >/dev/null 2>&1; then \
+    rm git_version.c.tmp; \
+  else \
+    mv git_version.c.tmp git_version.c; \
+  fi)
+endif
+
 TARGETS :=
 
 # The origin of GENERATED_SOURCES
@@ -35,7 +55,7 @@ GENERATED_SOURCES := pattern_la.cc pattern_p.cc pattern_uni.cc config_preproc_la
 
 # Sources in the CVS
 
-STATIC_SOURCES := memory.c new.cc userinfo.c path.c config_preproc.cc Quadruple.cc \
+STATIC_SOURCES := git_version.c memory.c new.cc userinfo.c path.c config_preproc.cc Quadruple.cc \
 		  Path2.cc ModuleVersion.cc JSON_Tokenizer.cc UnicharPattern.cc openssl_version.c
 
 ifndef MINGW
diff --git a/common/version_internal.h b/common/version_internal.h
index 1af7cd761..e334c9867 100644
--- a/common/version_internal.h
+++ b/common/version_internal.h
@@ -286,4 +286,7 @@
 /* For prefixing the above messages. Default value: empty string. */
 #define COMMENT_PREFIX
 
+/ * For include git commit id in version printouts */
+extern char const *const GIT_COMMIT_ID;
+
 #endif
diff --git a/compiler2/Makefile b/compiler2/Makefile
index ba9a9e4df..6f88256f8 100644
--- a/compiler2/Makefile
+++ b/compiler2/Makefile
@@ -121,7 +121,7 @@ ifdef REGEX_DIR
 endif
 
 COMPILER_COMMON_OBJECTS := $(COMMON_OBJECTS) \
-$(addprefix ../common/, new.o pattern_la.o pattern_p.o pattern_uni.o Quadruple.o \
+$(addprefix ../common/, git_version.o new.o pattern_la.o pattern_p.o pattern_uni.o Quadruple.o \
 ModuleVersion.o JSON_Tokenizer.o UnicharPattern.o openssl_version.o)
 
 ifeq ($(USAGE_STATS), yes)
diff --git a/compiler2/main.cc b/compiler2/main.cc
index 01e3c19d4..c191b41fd 100644
--- a/compiler2/main.cc
+++ b/compiler2/main.cc
@@ -977,6 +977,10 @@ int main(int argc, char *argv[])
 	  "Compiled with: " C_COMPILER_VERSION "\n", stderr);
 	  fputs("Using ", stderr);
 	  fputs(openssl_version_str(), stderr);
+	if (strlen(GIT_COMMIT_ID)) {
+	  fputs("\nCommit id: ", stderr);
+	  fputs(GIT_COMMIT_ID, stderr);
+	}
 	  fputs("\n\n" COPYRIGHT_STRING "\n\n", stderr);
 #ifdef LICENSE
     print_license_info();
diff --git a/core/Makefile b/core/Makefile
index 7620b928e..a9972a9cd 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -160,7 +160,7 @@ PROFMERGE_OBJECTS := ProfMerge_main.o ProfilerTools.profmerge.o \
 COMMON_OBJECTS := $(addprefix ../common/, memory.o pattern_la.o pattern_p.o \
 	            config_preproc.o config_preproc_la.o config_preproc_p.tab.o \
 	            path.o pattern_uni.o Quadruple.o NetworkHandler.o Path2.o \
-	            ModuleVersion.o JSON_Tokenizer.o UnicharPattern.o)
+	            ModuleVersion.o JSON_Tokenizer.o UnicharPattern.o git_version.o)
 
 ifeq ($(DEBUG), yes)
   COMMON_OBJECTS += ../common/new.o
diff --git a/core/Parallel_main.cc b/core/Parallel_main.cc
index ce627f92e..cce5b5eff 100644
--- a/core/Parallel_main.cc
+++ b/core/Parallel_main.cc
@@ -175,8 +175,12 @@ static boolean process_options(int argc, char *argv[], int& ret_val,
     fputs("TTCN-3 Host Controller (parallel mode)\n"
       "Version: " VERSION_STRING "\n"
       "Build date (Base Library): " __DATE__ " " __TIME__ "\n"
-      "Base Library was compiled with: " C_COMPILER_VERSION "\n\n"
-      COPYRIGHT_STRING "\n\n", stderr);
+      "Base Library was compiled with: " C_COMPILER_VERSION, stderr);
+    if (strlen(GIT_COMMIT_ID)) {
+	  fputs("\nCommit id: ", stderr);
+	  fputs(GIT_COMMIT_ID, stderr);
+	}
+    fputs("\n\n" COPYRIGHT_STRING "\n\n", stderr);
 #ifdef LICENSE
     print_license_info();
     putc('\n', stderr);
diff --git a/core/Single_main.cc b/core/Single_main.cc
index 1af929246..28913c056 100644
--- a/core/Single_main.cc
+++ b/core/Single_main.cc
@@ -185,8 +185,12 @@ int main(int argc, char *argv[])
     fputs("TTCN-3 Test Executor (single mode)\n"
       "Version: " VERSION_STRING "\n"
       "Build date (Base Library): " __DATE__ " " __TIME__ "\n"
-      "Base Library was compiled with: " C_COMPILER_VERSION "\n\n"
-      COPYRIGHT_STRING "\n\n", stderr);
+      "Base Library was compiled with: " C_COMPILER_VERSION, stderr);
+    if (strlen(GIT_COMMIT_ID)) {
+	  fputs("\nCommit id: ", stderr);
+	  fputs(GIT_COMMIT_ID, stderr);
+	}
+    fputs("\n\n" COPYRIGHT_STRING "\n\n", stderr);
 #ifdef LICENSE
     print_license_info();
     putc('\n', stderr);
-- 
GitLab