Add alternative method to use a version/git hash from a file.

- This is necessary for Chromium builds from tarballs where git is no longer available. This gives Chromium another option to create the version file when creating the tarball to accomplish the same thing.

Bug: dawn:549
Change-Id: Iffb4bf694b0df1306dd92939353422e5115346a7
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94043
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/build_overrides/dawn.gni b/build_overrides/dawn.gni
index 87e1ded..6440625 100644
--- a/build_overrides/dawn.gni
+++ b/build_overrides/dawn.gni
@@ -37,3 +37,7 @@
 dawn_vulkan_loader_dir = "//third_party/vulkan-deps/vulkan-loader/src"
 dawn_vulkan_validation_layers_dir =
     "//third_party/vulkan-deps/vulkan-validation-layers/src"
+
+# Optional path to a one-liner version file. Default is empty path indicating
+# that git should be used to figure out the version.
+dawn_version_file = ""
diff --git a/generator/dawn_version_generator.py b/generator/dawn_version_generator.py
index 120be49..c3c9547 100644
--- a/generator/dawn_version_generator.py
+++ b/generator/dawn_version_generator.py
@@ -26,11 +26,11 @@
     return git_exec
 
 
-def get_gitHash(dawnDir):
+def get_git_hash(dawn_dir):
     try:
         result = subprocess.run([get_git(), "rev-parse", "HEAD"],
                                 stdout=subprocess.PIPE,
-                                cwd=dawnDir)
+                                cwd=dawn_dir)
         if result.returncode == 0:
             return result.stdout.decode("utf-8").strip()
     except Exception:
@@ -40,15 +40,15 @@
     return ""
 
 
-def get_gitHead(dawnDir):
-    return os.path.join(dawnDir, ".git", "HEAD")
+def get_git_head(dawn_dir):
+    return os.path.join(dawn_dir, ".git", "HEAD")
 
 
-def gitExists(dawnDir):
-    return os.path.exists(get_gitHead(dawnDir))
+def git_exists(dawn_dir):
+    return os.path.exists(get_git_head(dawn_dir))
 
 
-def unpackGitRef(packed, resolved):
+def unpack_git_ref(packed, resolved):
     with open(packed) as fin:
         refs = fin.read().strip().split("\n")
 
@@ -64,20 +64,20 @@
     return False
 
 
-def get_gitResolvedHead(dawnDir):
+def get_git_resolved_head(dawn_dir):
     result = subprocess.run(
         [get_git(), "rev-parse", "--symbolic-full-name", "HEAD"],
         stdout=subprocess.PIPE,
-        cwd=dawnDir)
+        cwd=dawn_dir)
     if result.returncode != 0:
         raise Exception("Failed to execute git rev-parse to resolve git head:", result.stdout)
 
-    resolved = os.path.join(dawnDir, ".git",
+    resolved = os.path.join(dawn_dir, ".git",
                             result.stdout.decode("utf-8").strip())
 
     # Check a packed-refs file exists. If so, we need to potentially unpack and include it as a dep.
-    packed = os.path.join(dawnDir, ".git", "packed-refs")
-    if os.path.exists(packed) and unpackGitRef(packed, resolved):
+    packed = os.path.join(dawn_dir, ".git", "packed-refs")
+    if os.path.exists(packed) and unpack_git_ref(packed, resolved):
         return [packed, resolved]
 
     if not os.path.exists(resolved):
@@ -85,15 +85,25 @@
     return [resolved]
 
 
+def get_version(args):
+    version_file = args.version_file
+    if version_file:
+        with open(version_file) as f:
+            return f.read()
+    return get_git_hash(os.path.abspath(args.dawn_dir))
+
+
 def compute_params(args):
     return {
-        "get_gitHash": lambda: get_gitHash(os.path.abspath(args.dawn_dir)),
+        "get_version": lambda: get_version(args),
     }
 
 
 class DawnVersionGenerator(Generator):
     def get_description(self):
-        return "Generates version dependent Dawn code. Currently regenerated dependent on git hash."
+        return (
+            "Generates version dependent Dawn code. Currently regenerated dependent on the version "
+            "header (if available), otherwise tries to use git hash.")
 
     def add_commandline_arguments(self, parser):
         parser.add_argument(
@@ -102,15 +112,28 @@
             type=str,
             help="The Dawn root directory path to use",
         )
+        parser.add_argument(
+            "--version-file",
+            required=False,
+            type=str,
+            help=
+            ("Path to one-liner version string file used when git may not be present. "
+             "In general the version string is a git hash."))
 
     def get_dependencies(self, args):
-        dawnDir = os.path.abspath(args.dawn_dir)
-        if gitExists(dawnDir):
+        dawn_dir = os.path.abspath(args.dawn_dir)
+        version_file = args.version_file
+
+        if version_file:
+            return [version_file]
+        if git_exists(dawn_dir):
+            deps = []
             try:
-                return [get_gitHead(dawnDir)] + get_gitResolvedHead(dawnDir)
+                deps += [get_git_head(dawn_dir)
+                         ] + get_git_resolved_head(dawn_dir)
             except Exception:
-                return []
-        return []
+                return deps
+        return deps
 
     def get_file_renders(self, args):
         params = compute_params(args)
diff --git a/generator/templates/dawn/common/Version.h b/generator/templates/dawn/common/Version.h
index d8d5cdb..26e1986 100644
--- a/generator/templates/dawn/common/Version.h
+++ b/generator/templates/dawn/common/Version.h
@@ -19,7 +19,9 @@
 
 namespace dawn {
 
-static constexpr std::string_view kGitHash("{{get_gitHash()}}");
+// The version string should either be a valid git hash or empty.
+static constexpr std::string_view kDawnVersion("{{get_version()}}");
+static_assert(kDawnVersion.size() == 40 || kDawnVersion.size() == 0);
 
 } // namespace dawn
 
diff --git a/scripts/dawn_overrides_with_defaults.gni b/scripts/dawn_overrides_with_defaults.gni
index b4142ac..bbe79e1 100644
--- a/scripts/dawn_overrides_with_defaults.gni
+++ b/scripts/dawn_overrides_with_defaults.gni
@@ -85,3 +85,7 @@
 if (!defined(dawn_abseil_dir)) {
   dawn_abseil_dir = "//third_party/abseil-cpp"
 }
+
+if (!defined(dawn_version_file)) {
+  dawn_version_file = ""
+}
diff --git a/src/dawn/common/BUILD.gn b/src/dawn/common/BUILD.gn
index c843e86..02188ad 100644
--- a/src/dawn/common/BUILD.gn
+++ b/src/dawn/common/BUILD.gn
@@ -185,6 +185,16 @@
     "--dawn-dir",
     rebase_path("${dawn_root}", root_build_dir),
   ]
+
+  # We can use the explicit version file if it is generated instead of relying
+  # on the existence of git.
+  if (dawn_version_file != "") {
+    args += [
+      "--version-file",
+      rebase_path(dawn_version_file, root_build_dir),
+    ]
+  }
+
   outputs = [ "src/dawn/common/Version_autogen.h" ]
 }
 
diff --git a/src/dawn/native/Instance.cpp b/src/dawn/native/Instance.cpp
index 3d8cce9..3ea134c298 100644
--- a/src/dawn/native/Instance.cpp
+++ b/src/dawn/native/Instance.cpp
@@ -95,8 +95,8 @@
 }
 
 dawn::platform::CachingInterface* GetCachingInterface(dawn::platform::Platform* platform) {
-    if (platform != nullptr && dawn::kGitHash.size() > 0) {
-        return platform->GetCachingInterface(dawn::kGitHash.data(), dawn::kGitHash.size());
+    if (platform != nullptr && dawn::kDawnVersion.size() > 0) {
+        return platform->GetCachingInterface(dawn::kDawnVersion.data(), dawn::kDawnVersion.size());
     }
     return nullptr;
 }
diff --git a/src/dawn/tests/BUILD.gn b/src/dawn/tests/BUILD.gn
index 57bb666..ce07a94 100644
--- a/src/dawn/tests/BUILD.gn
+++ b/src/dawn/tests/BUILD.gn
@@ -252,7 +252,6 @@
     "unittests/SystemUtilsTests.cpp",
     "unittests/ToBackendTests.cpp",
     "unittests/TypedIntegerTests.cpp",
-    "unittests/VersionTests.cpp",
     "unittests/native/BlobTests.cpp",
     "unittests/native/CacheKeyTests.cpp",
     "unittests/native/CacheRequestTests.cpp",
diff --git a/src/dawn/tests/unittests/VersionTests.cpp b/src/dawn/tests/unittests/VersionTests.cpp
deleted file mode 100644
index ae7ea8b..0000000
--- a/src/dawn/tests/unittests/VersionTests.cpp
+++ /dev/null
@@ -1,32 +0,0 @@
-// Copyright 2022 The Dawn Authors
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include <string>
-
-#include "dawn/common/Version_autogen.h"
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-
-namespace dawn {
-namespace {
-
-using ::testing::SizeIs;
-
-TEST(VersionTests, GitCommitHashLength) {
-    // Git hashes should be 40 characters long.
-    EXPECT_THAT(std::string(kGitHash), SizeIs(40));
-}
-
-}  // namespace
-}  // namespace dawn