Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 1 | #!/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 Sapon | 4e1c65d | 2022-04-21 21:35:26 +0000 | [diff] [blame] | 16 | import os, subprocess, sys, shutil |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 17 | |
| 18 | from generator_lib import Generator, run_generator, FileRender |
| 19 | |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 20 | def get_git(): |
Aleksi Sapon | 4e1c65d | 2022-04-21 21:35:26 +0000 | [diff] [blame] | 21 | # 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 Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 27 | |
| 28 | |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 29 | def get_git_hash(dawn_dir): |
Loko Kung | 03ddfbb | 2022-05-03 00:28:53 +0000 | [diff] [blame] | 30 | try: |
| 31 | result = subprocess.run([get_git(), "rev-parse", "HEAD"], |
| 32 | stdout=subprocess.PIPE, |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 33 | cwd=dawn_dir) |
Loko Kung | 03ddfbb | 2022-05-03 00:28:53 +0000 | [diff] [blame] | 34 | if result.returncode == 0: |
| 35 | return result.stdout.decode("utf-8").strip() |
| 36 | except Exception: |
| 37 | return "" |
Loko Kung | 24ae176 | 2022-04-05 20:11:43 +0000 | [diff] [blame] | 38 | # 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 sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 40 | return "" |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 41 | |
| 42 | |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 43 | def get_git_head(dawn_dir): |
| 44 | return os.path.join(dawn_dir, ".git", "HEAD") |
Loko Kung | 24ae176 | 2022-04-05 20:11:43 +0000 | [diff] [blame] | 45 | |
| 46 | |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 47 | def git_exists(dawn_dir): |
| 48 | return os.path.exists(get_git_head(dawn_dir)) |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 49 | |
| 50 | |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 51 | def unpack_git_ref(packed, resolved): |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 52 | with open(packed) as fin: |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 53 | refs = fin.read().strip().split("\n") |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 54 | |
| 55 | # Strip comments |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 56 | refs = [ref.split(" ") for ref in refs if ref.strip()[0] != "#"] |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 57 | |
| 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 sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 61 | with open(resolved, "w") as fout: |
| 62 | fout.write(refs[0] + "\n") |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 63 | return True |
| 64 | return False |
| 65 | |
| 66 | |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 67 | def get_git_resolved_head(dawn_dir): |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 68 | result = subprocess.run( |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 69 | [get_git(), "rev-parse", "--symbolic-full-name", "HEAD"], |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 70 | stdout=subprocess.PIPE, |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 71 | cwd=dawn_dir) |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 72 | if result.returncode != 0: |
Aleksi Sapon | 4e1c65d | 2022-04-21 21:35:26 +0000 | [diff] [blame] | 73 | raise Exception("Failed to execute git rev-parse to resolve git head:", result.stdout) |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 74 | |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 75 | resolved = os.path.join(dawn_dir, ".git", |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 76 | result.stdout.decode("utf-8").strip()) |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 77 | |
| 78 | # Check a packed-refs file exists. If so, we need to potentially unpack and include it as a dep. |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 79 | packed = os.path.join(dawn_dir, ".git", "packed-refs") |
| 80 | if os.path.exists(packed) and unpack_git_ref(packed, resolved): |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 81 | return [packed, resolved] |
| 82 | |
| 83 | if not os.path.exists(resolved): |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 84 | raise Exception("Unable to resolve git HEAD hash file:", resolved) |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 85 | return [resolved] |
| 86 | |
| 87 | |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 88 | def 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 Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 96 | def compute_params(args): |
| 97 | return { |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 98 | "get_version": lambda: get_version(args), |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | class DawnVersionGenerator(Generator): |
| 103 | def get_description(self): |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 104 | return ( |
| 105 | "Generates version dependent Dawn code. Currently regenerated dependent on the version " |
| 106 | "header (if available), otherwise tries to use git hash.") |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 107 | |
| 108 | def add_commandline_arguments(self, parser): |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 109 | parser.add_argument( |
| 110 | "--dawn-dir", |
| 111 | required=True, |
| 112 | type=str, |
| 113 | help="The Dawn root directory path to use", |
| 114 | ) |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 115 | 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 Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 122 | |
| 123 | def get_dependencies(self, args): |
Loko Kung | 7d2b9d9 | 2022-06-22 04:19:43 +0000 | [diff] [blame] | 124 | 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 sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 130 | try: |
Loko Kung | 7223a5e | 2022-06-23 01:14:24 +0000 | [diff] [blame] | 131 | return [get_git_head(dawn_dir) |
| 132 | ] + get_git_resolved_head(dawn_dir) |
Aleksi Sapon | 4e1c65d | 2022-04-21 21:35:26 +0000 | [diff] [blame] | 133 | except Exception: |
Loko Kung | 7223a5e | 2022-06-23 01:14:24 +0000 | [diff] [blame] | 134 | return [] |
| 135 | return [] |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 136 | |
| 137 | def get_file_renders(self, args): |
| 138 | params = compute_params(args) |
| 139 | |
| 140 | return [ |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 141 | FileRender("dawn/common/Version.h", |
| 142 | "src/dawn/common/Version_autogen.h", [params]), |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 143 | ] |
| 144 | |
| 145 | |
dan sinclair | a12ddc6 | 2022-04-19 19:27:08 +0000 | [diff] [blame] | 146 | if __name__ == "__main__": |
Loko Kung | f578672 | 2022-03-31 05:09:04 +0000 | [diff] [blame] | 147 | sys.exit(run_generator(DawnVersionGenerator())) |