2022-03-31 05:09:04 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# 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.
|
|
|
|
|
2022-04-21 21:35:26 +00:00
|
|
|
import os, subprocess, sys, shutil
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
from generator_lib import Generator, run_generator, FileRender
|
|
|
|
|
|
|
|
def get_git():
|
2022-04-21 21:35:26 +00:00
|
|
|
# Will find git, git.exe, git.bat...
|
|
|
|
git_exec = shutil.which("git")
|
|
|
|
if not git_exec:
|
|
|
|
raise Exception("No git executable found")
|
|
|
|
|
|
|
|
return git_exec
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
|
2022-06-22 04:19:43 +00:00
|
|
|
def get_git_hash(dawn_dir):
|
2022-05-03 00:28:53 +00:00
|
|
|
try:
|
|
|
|
result = subprocess.run([get_git(), "rev-parse", "HEAD"],
|
|
|
|
stdout=subprocess.PIPE,
|
2022-06-22 04:19:43 +00:00
|
|
|
cwd=dawn_dir)
|
2022-05-03 00:28:53 +00:00
|
|
|
if result.returncode == 0:
|
|
|
|
return result.stdout.decode("utf-8").strip()
|
|
|
|
except Exception:
|
|
|
|
return ""
|
2022-04-05 20:11:43 +00:00
|
|
|
# No hash was available (possibly) because the directory was not a git checkout. Dawn should
|
|
|
|
# explicitly handle its absenece and disable features relying on the hash, i.e. caching.
|
2022-04-19 19:27:08 +00:00
|
|
|
return ""
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
|
2022-06-22 04:19:43 +00:00
|
|
|
def get_git_head(dawn_dir):
|
|
|
|
return os.path.join(dawn_dir, ".git", "HEAD")
|
2022-04-05 20:11:43 +00:00
|
|
|
|
|
|
|
|
2022-06-22 04:19:43 +00:00
|
|
|
def git_exists(dawn_dir):
|
|
|
|
return os.path.exists(get_git_head(dawn_dir))
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
|
2022-06-22 04:19:43 +00:00
|
|
|
def unpack_git_ref(packed, resolved):
|
2022-03-31 05:09:04 +00:00
|
|
|
with open(packed) as fin:
|
2022-04-19 19:27:08 +00:00
|
|
|
refs = fin.read().strip().split("\n")
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
# Strip comments
|
2022-04-19 19:27:08 +00:00
|
|
|
refs = [ref.split(" ") for ref in refs if ref.strip()[0] != "#"]
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
# Parse results which are in the format [<gitHash>, <refFile>] from previous step.
|
|
|
|
refs = [gitHash for (gitHash, refFile) in refs if refFile == resolved]
|
|
|
|
if len(refs) == 1:
|
2022-04-19 19:27:08 +00:00
|
|
|
with open(resolved, "w") as fout:
|
|
|
|
fout.write(refs[0] + "\n")
|
2022-03-31 05:09:04 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-06-22 04:19:43 +00:00
|
|
|
def get_git_resolved_head(dawn_dir):
|
2022-03-31 05:09:04 +00:00
|
|
|
result = subprocess.run(
|
2022-04-19 19:27:08 +00:00
|
|
|
[get_git(), "rev-parse", "--symbolic-full-name", "HEAD"],
|
2022-03-31 05:09:04 +00:00
|
|
|
stdout=subprocess.PIPE,
|
2022-06-22 04:19:43 +00:00
|
|
|
cwd=dawn_dir)
|
2022-03-31 05:09:04 +00:00
|
|
|
if result.returncode != 0:
|
2022-04-21 21:35:26 +00:00
|
|
|
raise Exception("Failed to execute git rev-parse to resolve git head:", result.stdout)
|
2022-03-31 05:09:04 +00:00
|
|
|
|
2022-06-22 04:19:43 +00:00
|
|
|
resolved = os.path.join(dawn_dir, ".git",
|
2022-04-19 19:27:08 +00:00
|
|
|
result.stdout.decode("utf-8").strip())
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
# Check a packed-refs file exists. If so, we need to potentially unpack and include it as a dep.
|
2022-06-22 04:19:43 +00:00
|
|
|
packed = os.path.join(dawn_dir, ".git", "packed-refs")
|
|
|
|
if os.path.exists(packed) and unpack_git_ref(packed, resolved):
|
2022-03-31 05:09:04 +00:00
|
|
|
return [packed, resolved]
|
|
|
|
|
|
|
|
if not os.path.exists(resolved):
|
2022-04-19 19:27:08 +00:00
|
|
|
raise Exception("Unable to resolve git HEAD hash file:", resolved)
|
2022-03-31 05:09:04 +00:00
|
|
|
return [resolved]
|
|
|
|
|
|
|
|
|
2022-06-22 04:19:43 +00:00
|
|
|
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))
|
|
|
|
|
|
|
|
|
2022-03-31 05:09:04 +00:00
|
|
|
def compute_params(args):
|
|
|
|
return {
|
2022-06-22 04:19:43 +00:00
|
|
|
"get_version": lambda: get_version(args),
|
2022-03-31 05:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DawnVersionGenerator(Generator):
|
|
|
|
def get_description(self):
|
2022-06-22 04:19:43 +00:00
|
|
|
return (
|
|
|
|
"Generates version dependent Dawn code. Currently regenerated dependent on the version "
|
|
|
|
"header (if available), otherwise tries to use git hash.")
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
def add_commandline_arguments(self, parser):
|
2022-04-19 19:27:08 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--dawn-dir",
|
|
|
|
required=True,
|
|
|
|
type=str,
|
|
|
|
help="The Dawn root directory path to use",
|
|
|
|
)
|
2022-06-22 04:19:43 +00:00
|
|
|
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."))
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
def get_dependencies(self, args):
|
2022-06-22 04:19:43 +00:00
|
|
|
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 = []
|
2022-04-19 19:27:08 +00:00
|
|
|
try:
|
2022-06-22 04:19:43 +00:00
|
|
|
deps += [get_git_head(dawn_dir)
|
|
|
|
] + get_git_resolved_head(dawn_dir)
|
2022-04-21 21:35:26 +00:00
|
|
|
except Exception:
|
2022-06-22 04:19:43 +00:00
|
|
|
return deps
|
|
|
|
return deps
|
2022-03-31 05:09:04 +00:00
|
|
|
|
|
|
|
def get_file_renders(self, args):
|
|
|
|
params = compute_params(args)
|
|
|
|
|
|
|
|
return [
|
2022-04-19 19:27:08 +00:00
|
|
|
FileRender("dawn/common/Version.h",
|
|
|
|
"src/dawn/common/Version_autogen.h", [params]),
|
2022-03-31 05:09:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-04-19 19:27:08 +00:00
|
|
|
if __name__ == "__main__":
|
2022-03-31 05:09:04 +00:00
|
|
|
sys.exit(run_generator(DawnVersionGenerator()))
|