summaryrefslogtreecommitdiff
path: root/chromium/third_party/dawn/generator/dawn_version_generator.py
blob: 96d61291f9d67ef575fb1adffc322c1f51be02a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/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.

import os, subprocess, sys, shutil

from generator_lib import Generator, run_generator, FileRender

def get_git():
    # 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


def get_git_hash(dawn_dir):
    try:
        result = subprocess.run([get_git(), "rev-parse", "HEAD"],
                                stdout=subprocess.PIPE,
                                cwd=dawn_dir)
        if result.returncode == 0:
            return result.stdout.decode("utf-8").strip()
    except Exception:
        return ""
    # 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.
    return ""


def get_git_head(dawn_dir):
    return os.path.join(dawn_dir, ".git", "HEAD")


def git_exists(dawn_dir):
    return os.path.exists(get_git_head(dawn_dir))


def unpack_git_ref(packed, resolved):
    with open(packed) as fin:
        refs = fin.read().strip().split("\n")

    # Strip comments
    refs = [ref.split(" ") for ref in refs if ref.strip()[0] != "#"]

    # 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:
        with open(resolved, "w") as fout:
            fout.write(refs[0] + "\n")
        return True
    return False


def get_git_resolved_head(dawn_dir):
    result = subprocess.run(
        [get_git(), "rev-parse", "--symbolic-full-name", "HEAD"],
        stdout=subprocess.PIPE,
        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(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(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):
        raise Exception("Unable to resolve git HEAD hash file:", resolved)
    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_version": lambda: get_version(args),
    }


class DawnVersionGenerator(Generator):
    def get_description(self):
        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(
            "--dawn-dir",
            required=True,
            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):
        dawn_dir = os.path.abspath(args.dawn_dir)
        version_file = args.version_file

        if version_file:
            return [version_file]
        if git_exists(dawn_dir):
            try:
                return [get_git_head(dawn_dir)
                        ] + get_git_resolved_head(dawn_dir)
            except Exception:
                return []
        return []

    def get_file_renders(self, args):
        params = compute_params(args)

        return [
            FileRender("dawn/common/Version.h",
                       "src/dawn/common/Version_autogen.h", [params]),
        ]


if __name__ == "__main__":
    sys.exit(run_generator(DawnVersionGenerator()))