blob: 96d61291f9d67ef575fb1adffc322c1f51be02a4 [file] [log] [blame]
Loko Kungf5786722022-03-31 05:09:04 +00001#!/usr/bin/env python3
2# Copyright 2022 The Dawn Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Aleksi Sapon4e1c65d2022-04-21 21:35:26 +000016import os, subprocess, sys, shutil
Loko Kungf5786722022-03-31 05:09:04 +000017
18from generator_lib import Generator, run_generator, FileRender
19
Loko Kungf5786722022-03-31 05:09:04 +000020def get_git():
Aleksi Sapon4e1c65d2022-04-21 21:35:26 +000021 # Will find git, git.exe, git.bat...
22 git_exec = shutil.which("git")
23 if not git_exec:
24 raise Exception("No git executable found")
25
26 return git_exec
Loko Kungf5786722022-03-31 05:09:04 +000027
28
Loko Kung7d2b9d92022-06-22 04:19:43 +000029def get_git_hash(dawn_dir):
Loko Kung03ddfbb2022-05-03 00:28:53 +000030 try:
31 result = subprocess.run([get_git(), "rev-parse", "HEAD"],
32 stdout=subprocess.PIPE,
Loko Kung7d2b9d92022-06-22 04:19:43 +000033 cwd=dawn_dir)
Loko Kung03ddfbb2022-05-03 00:28:53 +000034 if result.returncode == 0:
35 return result.stdout.decode("utf-8").strip()
36 except Exception:
37 return ""
Loko Kung24ae1762022-04-05 20:11:43 +000038 # No hash was available (possibly) because the directory was not a git checkout. Dawn should
39 # explicitly handle its absenece and disable features relying on the hash, i.e. caching.
dan sinclaira12ddc62022-04-19 19:27:08 +000040 return ""
Loko Kungf5786722022-03-31 05:09:04 +000041
42
Loko Kung7d2b9d92022-06-22 04:19:43 +000043def get_git_head(dawn_dir):
44 return os.path.join(dawn_dir, ".git", "HEAD")
Loko Kung24ae1762022-04-05 20:11:43 +000045
46
Loko Kung7d2b9d92022-06-22 04:19:43 +000047def git_exists(dawn_dir):
48 return os.path.exists(get_git_head(dawn_dir))
Loko Kungf5786722022-03-31 05:09:04 +000049
50
Loko Kung7d2b9d92022-06-22 04:19:43 +000051def unpack_git_ref(packed, resolved):
Loko Kungf5786722022-03-31 05:09:04 +000052 with open(packed) as fin:
dan sinclaira12ddc62022-04-19 19:27:08 +000053 refs = fin.read().strip().split("\n")
Loko Kungf5786722022-03-31 05:09:04 +000054
55 # Strip comments
dan sinclaira12ddc62022-04-19 19:27:08 +000056 refs = [ref.split(" ") for ref in refs if ref.strip()[0] != "#"]
Loko Kungf5786722022-03-31 05:09:04 +000057
58 # Parse results which are in the format [<gitHash>, <refFile>] from previous step.
59 refs = [gitHash for (gitHash, refFile) in refs if refFile == resolved]
60 if len(refs) == 1:
dan sinclaira12ddc62022-04-19 19:27:08 +000061 with open(resolved, "w") as fout:
62 fout.write(refs[0] + "\n")
Loko Kungf5786722022-03-31 05:09:04 +000063 return True
64 return False
65
66
Loko Kung7d2b9d92022-06-22 04:19:43 +000067def get_git_resolved_head(dawn_dir):
Loko Kungf5786722022-03-31 05:09:04 +000068 result = subprocess.run(
dan sinclaira12ddc62022-04-19 19:27:08 +000069 [get_git(), "rev-parse", "--symbolic-full-name", "HEAD"],
Loko Kungf5786722022-03-31 05:09:04 +000070 stdout=subprocess.PIPE,
Loko Kung7d2b9d92022-06-22 04:19:43 +000071 cwd=dawn_dir)
Loko Kungf5786722022-03-31 05:09:04 +000072 if result.returncode != 0:
Aleksi Sapon4e1c65d2022-04-21 21:35:26 +000073 raise Exception("Failed to execute git rev-parse to resolve git head:", result.stdout)
Loko Kungf5786722022-03-31 05:09:04 +000074
Loko Kung7d2b9d92022-06-22 04:19:43 +000075 resolved = os.path.join(dawn_dir, ".git",
dan sinclaira12ddc62022-04-19 19:27:08 +000076 result.stdout.decode("utf-8").strip())
Loko Kungf5786722022-03-31 05:09:04 +000077
78 # Check a packed-refs file exists. If so, we need to potentially unpack and include it as a dep.
Loko Kung7d2b9d92022-06-22 04:19:43 +000079 packed = os.path.join(dawn_dir, ".git", "packed-refs")
80 if os.path.exists(packed) and unpack_git_ref(packed, resolved):
Loko Kungf5786722022-03-31 05:09:04 +000081 return [packed, resolved]
82
83 if not os.path.exists(resolved):
dan sinclaira12ddc62022-04-19 19:27:08 +000084 raise Exception("Unable to resolve git HEAD hash file:", resolved)
Loko Kungf5786722022-03-31 05:09:04 +000085 return [resolved]
86
87
Loko Kung7d2b9d92022-06-22 04:19:43 +000088def get_version(args):
89 version_file = args.version_file
90 if version_file:
91 with open(version_file) as f:
92 return f.read()
93 return get_git_hash(os.path.abspath(args.dawn_dir))
94
95
Loko Kungf5786722022-03-31 05:09:04 +000096def compute_params(args):
97 return {
Loko Kung7d2b9d92022-06-22 04:19:43 +000098 "get_version": lambda: get_version(args),
Loko Kungf5786722022-03-31 05:09:04 +000099 }
100
101
102class DawnVersionGenerator(Generator):
103 def get_description(self):
Loko Kung7d2b9d92022-06-22 04:19:43 +0000104 return (
105 "Generates version dependent Dawn code. Currently regenerated dependent on the version "
106 "header (if available), otherwise tries to use git hash.")
Loko Kungf5786722022-03-31 05:09:04 +0000107
108 def add_commandline_arguments(self, parser):
dan sinclaira12ddc62022-04-19 19:27:08 +0000109 parser.add_argument(
110 "--dawn-dir",
111 required=True,
112 type=str,
113 help="The Dawn root directory path to use",
114 )
Loko Kung7d2b9d92022-06-22 04:19:43 +0000115 parser.add_argument(
116 "--version-file",
117 required=False,
118 type=str,
119 help=
120 ("Path to one-liner version string file used when git may not be present. "
121 "In general the version string is a git hash."))
Loko Kungf5786722022-03-31 05:09:04 +0000122
123 def get_dependencies(self, args):
Loko Kung7d2b9d92022-06-22 04:19:43 +0000124 dawn_dir = os.path.abspath(args.dawn_dir)
125 version_file = args.version_file
126
127 if version_file:
128 return [version_file]
129 if git_exists(dawn_dir):
dan sinclaira12ddc62022-04-19 19:27:08 +0000130 try:
Loko Kung7223a5e2022-06-23 01:14:24 +0000131 return [get_git_head(dawn_dir)
132 ] + get_git_resolved_head(dawn_dir)
Aleksi Sapon4e1c65d2022-04-21 21:35:26 +0000133 except Exception:
Loko Kung7223a5e2022-06-23 01:14:24 +0000134 return []
135 return []
Loko Kungf5786722022-03-31 05:09:04 +0000136
137 def get_file_renders(self, args):
138 params = compute_params(args)
139
140 return [
dan sinclaira12ddc62022-04-19 19:27:08 +0000141 FileRender("dawn/common/Version.h",
142 "src/dawn/common/Version_autogen.h", [params]),
Loko Kungf5786722022-03-31 05:09:04 +0000143 ]
144
145
dan sinclaira12ddc62022-04-19 19:27:08 +0000146if __name__ == "__main__":
Loko Kungf5786722022-03-31 05:09:04 +0000147 sys.exit(run_generator(DawnVersionGenerator()))