From: Jan Kiszka <[email protected]>

Generate a version string, either using the VERSION file for git-less
build or information obtained from the git tree. VERSION needs to be
updated on each release.

Signed-off-by: Jan Kiszka <[email protected]>
---

While testing a patch today and starring at the not-yet-working output,
the usual question came up: "What am I running?" This should provide a
better answer in the future. Machinery taken from Jailhouse.

 Makefile.am       | 30 ++++++++++++++++++++++++++++++
 VERSION           |  1 +
 gen_version_h     | 28 ++++++++++++++++++++++++++++
 main.c            |  3 ++-
 tools/bg_setenv.c |  8 ++++++++
 5 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 VERSION
 create mode 100755 gen_version_h

diff --git a/Makefile.am b/Makefile.am
index c7dcb10..bb7e7f9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -36,6 +36,19 @@ ARFLAGS = cr
 EXTRA_DIST = autogen.sh README LICENSE
 CLEANFILES =
 
+define filechk
+       $(AM_V_at)set -e;                       \
+       echo '  CHK      $@';                   \
+       mkdir -p $(dir $@);                     \
+       $(filechk_$(1)) < $< > [email protected];          \
+       if [ -r $@ ] && cmp -s $@ [email protected]; then  \
+               rm -f [email protected];                   \
+       else                                    \
+               echo '  UPD      $@';           \
+               mv -f [email protected] $@;                \
+       fi
+endef
+
 #
 # Static libraries
 #
@@ -152,6 +165,17 @@ efibootguard_DATA = $(efi_loadername)
 CLEANFILES += $(efi_objects) $(efi_solib) $(efi_loadername)
 EXTRA_DIST += $(efi_sources)
 
+define filechk_version
+       $(top_srcdir)/gen_version_h $(top_srcdir)/
+endef
+
+GEN_VERSION_H := $(top_builddir)/version.h
+
+$(GEN_VERSION_H): $(top_srcdir)/Makefile.in FORCE
+       $(call filechk,version)
+
+CLEANFILES += $(GEN_VERSION_H)
+
 $(top_builddir)/%.o: $(top_srcdir)/%.c
        @$(MKDIR_P) $(shell dirname $@)/
        $(AM_V_CC)$(GNUEFI_CC) $(efi_cppflags) $(efi_cflags) -c $< -o $@
@@ -168,6 +192,8 @@ $(top_builddir)/drivers/watchdog/%.o: 
$(top_srcdir)/drivers/watchdog/%.S
        @$(MKDIR_P) $(shell dirname $@)/
        $(AM_V_CC)$(GNUEFI_CC) $(efi_cppflags) $(efi_cflags) -c $< -o $@
 
+$(top_builddir)/main.o: $(GEN_VERSION_H)
+
 $(efi_solib): $(efi_objects)
        $(AM_V_CCLD)$(LD) $(efi_ldflags) $(efi_objects) \
                -o $@ -lefi -lgnuefi $(shell $(CC) $(CFLAGS) 
-print-libgcc-file-name); \
@@ -189,3 +215,7 @@ CLEANFILES += bg_printenv
 
 # Tests depend on libraries being built - start with "."
 SUBDIRS = . tools/tests
+
+FORCE:
+
+.PHONY: FORCE
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..085135e
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+v0.1
diff --git a/gen_version_h b/gen_version_h
new file mode 100755
index 0000000..f917fe0
--- /dev/null
+++ b/gen_version_h
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# EFI Boot Guard
+#
+# Copyright (c) Siemens AG, 2017
+#
+# Authors:
+#  Jan Kiszka <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.  See
+# the COPYING file in the top-level directory.
+#
+
+echo "/* Auto-generated - leave alone and don't commit! */"
+echo ""
+
+cd "$1" > /dev/null
+
+if ! git rev-parse 2>/dev/null; then
+       version="`cat VERSION`"
+else
+       describe="`git describe --long --dirty --match "v[0-9].[0-9]*"`"
+       version="`echo $describe | sed -e 's/\([^-]*\)-\(.*\)/\1 (\2)/'`"
+fi
+
+cd - > /dev/null
+
+echo "#define EFIBOOTGUARD_VERSION     \"$version\""
diff --git a/main.c b/main.c
index e796450..5bf38df 100644
--- a/main.c
+++ b/main.c
@@ -18,6 +18,7 @@
 #include <pci/header.h>
 #include <bootguard.h>
 #include <configuration.h>
+#include "version.h"
 
 extern const unsigned long init_array_start[];
 extern const unsigned long init_array_end[];
@@ -104,7 +105,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, 
EFI_SYSTEM_TABLE *system_table)
        this_image = image_handle;
        InitializeLib(this_image, system_table);
 
-       Print(L"\nEFI Boot Guard\n");
+       Print(L"\nEFI Boot Guard %s\n", L""EFIBOOTGUARD_VERSION);
 
        status =
            uefi_call_wrapper(BS->OpenProtocol, 6, this_image,
diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
index 6f7f8f9..4c25d9c 100644
--- a/tools/bg_setenv.c
+++ b/tools/bg_setenv.c
@@ -15,6 +15,9 @@
 #include "env_api.h"
 #include "ebgenv.h"
 #include "uservars.h"
+#include "version.h"
+
+#define VERSION_KEY    1000
 
 static char doc[] =
     "bg_setenv/bg_printenv - Environment tool for the EFI Boot Guard";
@@ -38,10 +41,12 @@ static struct argp_option options_setenv[] = {
     {"uservar", 'x', "KEY=VAL", 0, "Set user-defined string variable. For "
                                   "setting multiple variables, use this "
                                   "option multiple times."},
+    {"version", VERSION_KEY, 0, 0, "Print version"},
     {0}};
 
 static struct argp_option options_printenv[] = {
     {"verbose", 'v', 0, 0, "Be verbose"},
+    {"version", VERSION_KEY, 0, 0, "Print version"},
     {0}};
 
 struct arguments {
@@ -311,6 +316,9 @@ static error_t parse_opt(int key, char *arg, struct 
argp_state *state)
                /* Set user-defined variable(s) */
                set_uservars(arg);
                break;
+       case VERSION_KEY:
+               printf("EFI Boot Guard %s\n", EFIBOOTGUARD_VERSION);
+               exit(0);
        case ARGP_KEY_ARG:
                /* too many arguments - program terminates with call to
                 * argp_usage with non-zero return code */
-- 
2.12.3

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/dda6c143-ea76-210f-64dd-b6d87eac6918%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to