From c7a7d0adef949234163c08c2040fd92920b309a4 Mon Sep 17 00:00:00 2001 From: phl0 Date: Tue, 23 May 2017 10:07:44 +0200 Subject: [PATCH] Add compilation time stamp and GitID to binaries --- DMRGateway.cpp | 4 +++- DMRGateway.vcxproj | 8 +++++++- Makefile | 13 +++++++++++-- prebuild.cmd | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 prebuild.cmd diff --git a/DMRGateway.cpp b/DMRGateway.cpp index 50fcdde..63f28d6 100644 --- a/DMRGateway.cpp +++ b/DMRGateway.cpp @@ -25,6 +25,7 @@ #include "Thread.h" #include "Voice.h" #include "Log.h" +#include "GitVersion.h" #include #include @@ -77,7 +78,7 @@ int main(int argc, char** argv) for (int currentArg = 1; currentArg < argc; ++currentArg) { std::string arg = argv[currentArg]; if ((arg == "-v") || (arg == "--version")) { - ::fprintf(stdout, "DMRGateway version %s\n", VERSION); + ::fprintf(stdout, "DMRGateway version %s git #%.7s\n", VERSION, gitversion); return 0; } else if (arg.substr(0,1) == "-") { ::fprintf(stderr, "Usage: DMRGateway [-v|--version] [filename]\n"); @@ -231,6 +232,7 @@ int CDMRGateway::run() LogInfo(HEADER4); LogMessage("DMRGateway-%s is starting", VERSION); + LogMessage("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion); ret = createMMDVM(); if (!ret) diff --git a/DMRGateway.vcxproj b/DMRGateway.vcxproj index 06458c7..1b02149 100644 --- a/DMRGateway.vcxproj +++ b/DMRGateway.vcxproj @@ -108,6 +108,12 @@ true wsock32.lib;%(AdditionalDependencies) + + "$(ProjectDir)prebuild.cmd" $(ProjectDir) + + + prebuild.cmd generates GitVersion.h from git refs heads master + @@ -217,4 +223,4 @@ - \ No newline at end of file + diff --git a/Makefile b/Makefile index f0283c1..35730c2 100644 --- a/Makefile +++ b/Makefile @@ -10,11 +10,20 @@ OBJECTS = BPTC19696.o Conf.o CRC.o DMRData.o DMREmbeddedData.o DMREMB.o DMRFullL all: DMRGateway -DMRGateway: $(OBJECTS) +DMRGateway: GitVersion.h $(OBJECTS) $(CXX) $(OBJECTS) $(CFLAGS) $(LIBS) -o DMRGateway %.o: %.cpp $(CXX) $(CFLAGS) -c -o $@ $< clean: - $(RM) DMRGateway *.o *.d *.bak *~ + $(RM) DMRGateway *.o *.d *.bak *~ GitVersion.h + +# Export the current git version if the index file exists, else 000... +GitVersion.h: +ifneq ("$(wildcard .git/index)","") + echo "const char *gitversion = \"$(shell git rev-parse HEAD)\";" > $@ +else + echo "const char *gitversion = \"0000000000000000000000000000000000000000\";" > $@ +endif + diff --git a/prebuild.cmd b/prebuild.cmd new file mode 100644 index 0000000..2dc5f9d --- /dev/null +++ b/prebuild.cmd @@ -0,0 +1,38 @@ +@echo off +REM This pre-build file is for MSVS VC++. It parses the git master hash and +REM converts it into GitVersion.h for compiling into builds. [George M1GEO] + +cd %1 +setlocal enabledelayedexpansion +set HEADFILE=.git\HEAD +set HASHFILE=0 +if exist %HEADFILE% ( + for /F "tokens=4 delims=/:" %%a in ('type %HEADFILE%') do set HEADBRANCH=%%a + set HASHFILE=.git\refs\heads\!HEADBRANCH! + echo Found Git HEAD file: %HEADFILE% + echo Git HEAD branch: !HEADBRANCH! + echo Git HASH file: !HASHFILE! + call :USEHASH +) else ( + echo No head file :( + call :USENULL +) + +goto :EOF + +:USENULL +set GITHASH=0000000000000000000000000000000000000000 +goto :WRITEGITVERSIONHEADER + +:USEHASH +for /f %%i in ('type !HASHFILE!') do set GITHASH=%%i +goto :WRITEGITVERSIONHEADER + +:WRITEGITVERSIONHEADER +echo // File contains Git commit ID SHA1 present at buildtime (prebuild.cmd) > GitVersion.h +echo const char *gitversion = "%GITHASH%"; >> GitVersion.h +echo Current Git HASH: %GITHASH% +goto :FINISHED + +:FINISHED +echo GitVersion.h written...