Updates to dawn_version_generator.

This CL fixes the incorrect `path` variable used when the HEAD lookup
fails, the proper path is now emitted.

The call to `get_gitResolvedHead` is wrapped in a try/except and an
exception is treated as if we are not in a git directory. This allows
the build to continue if `refs/head/main` doesn't exist in the local
check.

Change-Id: I49ae66cc5eac99d43387b58d28d356282b8f6783
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/87100
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Auto-Submit: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2022-04-19 19:27:08 +00:00 committed by Dawn LUCI CQ
parent 1d1fcdd005
commit a12ddc6ec6
1 changed files with 33 additions and 26 deletions

View File

@ -19,22 +19,22 @@ from generator_lib import Generator, run_generator, FileRender
def get_git(): def get_git():
return 'git.bat' if sys.platform == 'win32' else 'git' return "git.bat" if sys.platform == "win32" else "git"
def get_gitHash(dawnDir): def get_gitHash(dawnDir):
result = subprocess.run([get_git(), 'rev-parse', 'HEAD'], result = subprocess.run([get_git(), "rev-parse", "HEAD"],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
cwd=dawnDir) cwd=dawnDir)
if result.returncode == 0: if result.returncode == 0:
return result.stdout.decode('utf-8').strip() return result.stdout.decode("utf-8").strip()
# No hash was available (possibly) because the directory was not a git checkout. Dawn should # 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. # explicitly handle its absenece and disable features relying on the hash, i.e. caching.
return '' return ""
def get_gitHead(dawnDir): def get_gitHead(dawnDir):
return os.path.join(dawnDir, '.git', 'HEAD') return os.path.join(dawnDir, ".git", "HEAD")
def gitExists(dawnDir): def gitExists(dawnDir):
@ -43,71 +43,78 @@ def gitExists(dawnDir):
def unpackGitRef(packed, resolved): def unpackGitRef(packed, resolved):
with open(packed) as fin: with open(packed) as fin:
refs = fin.read().strip().split('\n') refs = fin.read().strip().split("\n")
# Strip comments # Strip comments
refs = [ref.split(' ') for ref in refs if ref.strip()[0] != '#'] refs = [ref.split(" ") for ref in refs if ref.strip()[0] != "#"]
# Parse results which are in the format [<gitHash>, <refFile>] from previous step. # Parse results which are in the format [<gitHash>, <refFile>] from previous step.
refs = [gitHash for (gitHash, refFile) in refs if refFile == resolved] refs = [gitHash for (gitHash, refFile) in refs if refFile == resolved]
if len(refs) == 1: if len(refs) == 1:
with open(resolved, 'w') as fout: with open(resolved, "w") as fout:
fout.write(refs[0] + '\n') fout.write(refs[0] + "\n")
return True return True
return False return False
def get_gitResolvedHead(dawnDir): def get_gitResolvedHead(dawnDir):
result = subprocess.run( result = subprocess.run(
[get_git(), 'rev-parse', '--symbolic-full-name', 'HEAD'], [get_git(), "rev-parse", "--symbolic-full-name", "HEAD"],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
cwd=dawnDir) cwd=dawnDir,
)
if result.returncode != 0: if result.returncode != 0:
raise Exception('Failed to execute git rev-parse to resolve git head.') raise Exception("Failed to execute git rev-parse to resolve git head.")
resolved = os.path.join(dawnDir, '.git', resolved = os.path.join(dawnDir, ".git",
result.stdout.decode('utf-8').strip()) 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. # 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') packed = os.path.join(dawnDir, ".git", "packed-refs")
if os.path.exists(packed) and unpackGitRef(packed, resolved): if os.path.exists(packed) and unpackGitRef(packed, resolved):
return [packed, resolved] return [packed, resolved]
if not os.path.exists(resolved): if not os.path.exists(resolved):
raise Exception('Unable to resolve git HEAD hash file:', path) raise Exception("Unable to resolve git HEAD hash file:", resolved)
return [resolved] return [resolved]
def compute_params(args): def compute_params(args):
return { return {
'get_gitHash': lambda: get_gitHash(os.path.abspath(args.dawn_dir)), "get_gitHash": lambda: get_gitHash(os.path.abspath(args.dawn_dir)),
} }
class DawnVersionGenerator(Generator): class DawnVersionGenerator(Generator):
def get_description(self): 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 git hash."
def add_commandline_arguments(self, parser): def add_commandline_arguments(self, parser):
parser.add_argument('--dawn-dir', parser.add_argument(
required=True, "--dawn-dir",
type=str, required=True,
help='The Dawn root directory path to use') type=str,
help="The Dawn root directory path to use",
)
def get_dependencies(self, args): def get_dependencies(self, args):
dawnDir = os.path.abspath(args.dawn_dir) dawnDir = os.path.abspath(args.dawn_dir)
if gitExists(dawnDir): if gitExists(dawnDir):
return [get_gitHead(dawnDir)] + get_gitResolvedHead(dawnDir) try:
return [get_gitHead(dawnDir)] + get_gitResolvedHead(dawnDir)
except Exception as e:
print(e)
return []
return [] return []
def get_file_renders(self, args): def get_file_renders(self, args):
params = compute_params(args) params = compute_params(args)
return [ return [
FileRender('dawn/common/Version.h', FileRender("dawn/common/Version.h",
'src/dawn/common/Version_autogen.h', [params]), "src/dawn/common/Version_autogen.h", [params]),
] ]
if __name__ == '__main__': if __name__ == "__main__":
sys.exit(run_generator(DawnVersionGenerator())) sys.exit(run_generator(DawnVersionGenerator()))