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>
This commit is contained in:
Loko Kung 2022-06-22 04:19:43 +00:00 committed by Dawn LUCI CQ
parent ef62b58cf9
commit 7d2b9d9e83
8 changed files with 65 additions and 55 deletions

View File

@ -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 = ""

View File

@ -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)

View File

@ -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

View File

@ -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 = ""
}

View File

@ -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" ]
}

View File

@ -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;
}

View File

@ -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",

View File

@ -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