From 7d2b9d9e8344b54677ce7079a2bb16066b60dfda Mon Sep 17 00:00:00 2001 From: Loko Kung Date: Wed, 22 Jun 2022 04:19:43 +0000 Subject: [PATCH] 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 Commit-Queue: Loko Kung Kokoro: Kokoro --- build_overrides/dawn.gni | 4 ++ generator/dawn_version_generator.py | 61 ++++++++++++++++------- generator/templates/dawn/common/Version.h | 4 +- scripts/dawn_overrides_with_defaults.gni | 4 ++ src/dawn/common/BUILD.gn | 10 ++++ src/dawn/native/Instance.cpp | 4 +- src/dawn/tests/BUILD.gn | 1 - src/dawn/tests/unittests/VersionTests.cpp | 32 ------------ 8 files changed, 65 insertions(+), 55 deletions(-) delete mode 100644 src/dawn/tests/unittests/VersionTests.cpp diff --git a/build_overrides/dawn.gni b/build_overrides/dawn.gni index 87e1ded268..6440625c53 100644 --- a/build_overrides/dawn.gni +++ b/build_overrides/dawn.gni @@ -37,3 +37,7 @@ dawn_swiftshader_dir = "//third_party/swiftshader" 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 120be495bd..c3c95475e9 100644 --- a/generator/dawn_version_generator.py +++ b/generator/dawn_version_generator.py @@ -26,11 +26,11 @@ def get_git(): 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 @@ def get_gitHash(dawnDir): 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 @@ def unpackGitRef(packed, resolved): 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 @@ def get_gitResolvedHead(dawnDir): 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 @@ class DawnVersionGenerator(Generator): 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 d8d5cdbb29..26e19867d7 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 b4142ac6eb..bbe79e18c4 100644 --- a/scripts/dawn_overrides_with_defaults.gni +++ b/scripts/dawn_overrides_with_defaults.gni @@ -85,3 +85,7 @@ if (!defined(dawn_vulkan_validation_layers_dir)) { 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 c843e86b6f..02188adc67 100644 --- a/src/dawn/common/BUILD.gn +++ b/src/dawn/common/BUILD.gn @@ -185,6 +185,16 @@ dawn_generator("dawn_version_gen") { "--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 3d8cce91e9..3ea134c298 100644 --- a/src/dawn/native/Instance.cpp +++ b/src/dawn/native/Instance.cpp @@ -95,8 +95,8 @@ BackendsBitset GetEnabledBackends() { } 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 57bb666be1..ce07a94d3b 100644 --- a/src/dawn/tests/BUILD.gn +++ b/src/dawn/tests/BUILD.gn @@ -252,7 +252,6 @@ dawn_test("dawn_unittests") { "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 ae7ea8b3fb..0000000000 --- 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 - -#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