add a WP2::GitVersion string to example_utils.h

auto-determined by CMake or Makefile
(won't work with other build systems yet...)

Change-Id: Ifbd3dfe9fc570c48566b8e90116188b08099f9ab
Reviewed-on: https://chromium-review.googlesource.com/c/codecs/libwebp2/+/3100151
Reviewed-by: WebM Builds <builds@webmproject.org>
Reviewed-by: Pascal Massimino <skal@google.com>
Tested-by: WebM Builds <builds@webmproject.org>
Tested-by: Pascal Massimino <skal@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 492e78c..3bfcec6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -609,6 +609,17 @@
 
 # ##############################################################################
 
+execute_process(COMMAND git rev-parse --short HEAD
+                RESULT_VARIABLE GIT_HASH_RESULT
+                OUTPUT_VARIABLE GIT_HASH
+                OUTPUT_STRIP_TRAILING_WHITESPACE)
+if (${GIT_HASH_RESULT} STREQUAL "0")
+  message(STATUS "using [${GIT_HASH}] as git hash")
+  set(WP2_HAVE_GIT_VERSION TRUE)
+else()
+  set(WP2_HAVE_GIT_VERSION FALSE)
+endif()
+
 # Build the executables if asked for.
 if(WP2_BUILD_EXAMPLES)
   # utils
@@ -624,6 +635,10 @@
     ${CMAKE_CURRENT_SOURCE_DIR}/examples/example_utils.h
     ${CMAKE_CURRENT_SOURCE_DIR}/examples/stopwatch.h)
   target_link_libraries(example_utils imageio webp2)
+  if(WP2_HAVE_GIT_VERSION)
+    target_compile_definitions(example_utils
+                               PUBLIC WP2_GIT_VERSION=\"${GIT_HASH}\")
+  endif()
   if(WP2_DEFINITIONS)
     target_compile_definitions(example_utils PUBLIC ${WP2_DEFINITIONS})
   endif()
diff --git a/Makefile b/Makefile
index cb03acf..be276a4 100644
--- a/Makefile
+++ b/Makefile
@@ -28,7 +28,6 @@
 GTEST_FLAGS += -I$(GTEST_DIR)/googlemock/include
 GTEST_LIBS = -L$(GTEST_DIR)/lib -lgtest -lgtest_main
 
-
 # OPENGL setup
 EXTRA_FLAGS += -DWP2_HAVE_OPENGL
 ifeq ($(strip $(shell uname)), Darwin)
@@ -93,6 +92,12 @@
 EXTRA_FLAGS += -Wformat-security
 # EXTRA_FLAGS += -Wvla
 
+# fetch git hash
+GIT_HASH = $(shell git rev-parse --short HEAD)
+ifneq ($(GIT_HASH),)
+  EXTRA_FLAGS += -DWP2_GIT_VERSION=\"$(GIT_HASH)\"
+endif
+
 # SSE4.2-specific flags. If unsure about your compiler's defines, try:
 #    $(CC) -dM -E - < /dev/null | grep SSE
 ifeq ($(HAVE_SSE), 1)
@@ -106,7 +111,6 @@
 src/dsp/deblocking_filter_dsp.o: EXTRA_FLAGS += -mavx2
 endif
 
-
 # NEON-specific flags:
 # EXTRA_FLAGS += -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8
 # -> seems to make the overall lib slower: -fno-split-wide-types
diff --git a/examples/example_utils.cc b/examples/example_utils.cc
index 477b985..ddece7a 100644
--- a/examples/example_utils.cc
+++ b/examples/example_utils.cc
@@ -34,6 +34,12 @@
 
 namespace WP2 {
 
+#if defined(WP2_GIT_VERSION)
+const char* const GitVersion = WP2_GIT_VERSION;
+#else
+const char* const GitVersion = "0";
+#endif
+
 //------------------------------------------------------------------------------
 
 // Prints the distortion stored in 'values'.
@@ -481,6 +487,7 @@
   Add("-noasm", "disable all assembly optimizations");
 #endif
   Add("-version", "print version number and exit");
+  Add("-git", "print git hash if available");
   Add("-h / -help", "this help");
   Add("");
 }
@@ -534,8 +541,13 @@
   *must_stop = false;
   if (!strcmp(arg, "-version")) {
     const auto version = (uint32_t)WP2GetVersion();
-    printf("WebP2 version: %d.%d.%d\n", (version >> 16u) & 0xffu,
-           (version >> 8u) & 0xffu, (version >> 0u) & 0xffu);
+    printf("WebP2 version: %d.%d.%d [%s]\n", (version >> 16u) & 0xffu,
+           (version >> 8u) & 0xffu, (version >> 0u) & 0xffu,
+           GitVersion);
+    *must_stop = true;
+    return true;
+  } else if (!strcmp(arg, "-git")) {
+    printf("%s\n", GitVersion);
     *must_stop = true;
     return true;
   } else if (!strcmp(arg, "-noasm")) {
diff --git a/examples/example_utils.h b/examples/example_utils.h
index a86c835..a344619 100644
--- a/examples/example_utils.h
+++ b/examples/example_utils.h
@@ -44,6 +44,9 @@
 
 //------------------------------------------------------------------------------
 
+// Contains the hash of HEAD git version, or '0' if not available.
+extern const char* const GitVersion;
+
 // Parse numerical values (and update *error in case of error).
 int ExUtilGetInt(const char* const v, bool* const error, int base = 0);
 uint32_t ExUtilGetUInt(const char* const v, bool* const error, int base = 0);
diff --git a/tests/test_cmd.sh b/tests/test_cmd.sh
index 3f31a53..8800995 100755
--- a/tests/test_cmd.sh
+++ b/tests/test_cmd.sh
@@ -46,6 +46,7 @@
 
 "${CWP2}"
 "${CWP2}" -version
+"${CWP2}" -git
 "${CWP2}" -mem_traffic -h
 "${CWP2}" "${SRC_FILE3}"
 "${CWP2}" -- - < "${SRC_FILE3}"