From c8eef530abc63341f8198cefdd3b066cd8801af1 Mon Sep 17 00:00:00 2001 From: James Hunt Date: Fri, 2 Sep 2016 09:29:22 +0100 Subject: [PATCH] Version: Ensure "--version" and "version" display a commit id. git-archive(1), as used by github.com, allows files to be modified "on-the-fly" as the archive file is generated. By configuring git-archive to write the commit id to file "commit_id.fmt", the build can be convinced (using the mandatory "clean-local" rule) to generate file "commit_id" from "commit_id.fmt". The value of "commit_id" is then passed via the compiler to the runtime to allow the latter to display a commit id when run with either "--version" or "version". Signed-off-by: James Hunt --- .gitattributes | 2 ++ Makefile.am | 14 +++++++++++--- commit_id.fmt | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 .gitattributes create mode 100644 commit_id.fmt diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..275b8a6e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +commit_id.fmt export-subst +.gitattributes export-ignore diff --git a/Makefile.am b/Makefile.am index 887a0d86..6078634e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -25,14 +25,22 @@ AUTOMAKE_OPTIONS = parallel-tests CHECK_DEPS = +# Has a value if building in a git tree GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null) +# Has a value if building in a git-archive(1)-generated tree +# (see .gitattributes). +GENERATED_COMMIT = $(shell cat commit_id.fmt 2>/dev/null \| grep \'^[a-f0-9][a-f0-9]*\$\') + commit_id: $(AM_V_GEN)if [ "x$(GIT_COMMIT)" != "x" ]; then \ - echo "Writing commit id"; \ - echo $(GIT_COMMIT) > commit_id; \ + echo "Determining commit id from git"; \ + echo "$(GIT_COMMIT)" > commit_id; \ + elif [ "x$(GENERATED_COMMIT)" != "x" ]; then \ + echo "Determining commit id from generated value"; \ + echo "$(GENERATED_COMMIT)" > commit_id; \ else \ - echo "WARNING: cannot commit id"; \ + echo "WARNING: cannot determine actual commit id"; \ echo "0" > commit_id; \ fi diff --git a/commit_id.fmt b/commit_id.fmt new file mode 100644 index 00000000..6828f88d --- /dev/null +++ b/commit_id.fmt @@ -0,0 +1 @@ +$Format:%H$