File: generate_rules.py

package info (click to toggle)
0ad 0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 171,928 kB
  • sloc: cpp: 194,011; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,695; perl: 1,575; java: 533; xml: 415; php: 192; makefile: 99
file content (94 lines) | stat: -rw-r--r-- 3,646 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
#
# Copyright (C) 2024 Wildfire Games.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import argparse
import json
import os
import re

from merge_rules import merge_rules


def is_descriptor_indexing_define(define):
    return define["name"] == "USE_DESCRIPTOR_INDEXING"


def generate_rules(logs):
    re_error = re.compile(
        r"ERROR:\s+Program \'spirv/(.*?)\' with required defines not found\.(.*?)"
        r"ERROR:\s+Failed to load shader 'spirv/(.*?)'",
        re.DOTALL | re.MULTILINE,
    )
    re_define = re.compile(r'ERROR:\s+"(.*?)": "(.*?)"')
    rules = {}
    for error in re_error.findall(logs):
        if error[0] != error[2]:
            raise ValueError("Unexpected error format.")
        program_name = error[0]
        if program_name not in rules:
            rules[program_name] = {"name": program_name, "combinations": []}
        combination = []
        for define in re_define.findall(error[1]):
            name = define[0]
            value = define[1]
            # We have to avoid building all debug modes because it will cost x4
            # of SPIR-V shaders size.
            if name == "RENDER_DEBUG_MODE":
                value = "RENDER_DEBUG_MODE_NONE"
            combination.append({"name": name, "value": value})
        rules[program_name]["combinations"].append(combination)
        if any(is_descriptor_indexing_define(define) for define in combination):
            paired_combination = list(
                filter(lambda define: not is_descriptor_indexing_define(define), combination)
            )
        else:
            paired_combination = [*combination, {"name": "USE_DESCRIPTOR_INDEXING", "value": "1"}]
        rules[program_name]["combinations"].append(paired_combination)
    # Raw logs might contain duplicated values.
    return merge_rules(rules, {})


def run():
    parser = argparse.ArgumentParser()
    parser.add_argument("output_rules_path", help="a path to output rules")
    parser.add_argument(
        "input_paths", help="a paths to input files with logs", nargs=argparse.REMAINDER
    )
    args = parser.parse_args()

    if not args.input_paths or any(
        not os.path.isfile(input_path) for input_path in args.input_paths
    ):
        raise ValueError("Invalid input files.")

    rules = {}
    for input_path in args.input_paths:
        with open(input_path) as handle:
            input_logs = handle.read()
        rules = merge_rules(rules, generate_rules(input_logs))

    with open(args.output_rules_path, "w") as handle:
        json.dump(rules, handle, sort_keys=True)


if __name__ == "__main__":
    run()