• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2#
3# Copyright 2017 The ANGLE Project Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# run_code_generation.py:
8#   Runs ANGLE format table and other script code generation scripts.
9
10import hashlib
11import json
12import os
13import subprocess
14import sys
15import platform
16
17script_dir = sys.path[0]
18root_dir = os.path.abspath(os.path.join(script_dir, '..'))
19
20hash_dir = 'code_generation_hashes'
21
22# auto_script is a standard way for scripts to return their inputs and outputs.
23
24
25def get_child_script_dirname(script):
26    # All script names are relative to ANGLE's root
27    return os.path.dirname(os.path.abspath(os.path.join(root_dir, script)))
28
29
30# Replace all backslashes with forward slashes to be platform independent
31def clean_path_slashes(path):
32    return path.replace("\\", "/")
33
34
35# Takes a script file name which is relative to the code generation script's directory and
36# changes it to be relative to the angle root directory
37def rebase_script_path(script_path, relative_path):
38    return os.path.relpath(os.path.join(os.path.dirname(script_path), relative_path), root_dir)
39
40
41# Check if we need a module from vpython
42def get_executable_name(first_line):
43    binary = os.path.basename(first_line.strip().replace(' ', '/'))
44    if platform.system() == 'Windows':
45        if binary == 'python2':
46            return 'python.bat'
47        else:
48            return binary + '.bat'
49    else:
50        return binary
51
52
53def grab_from_script(script, param):
54    res = ''
55    f = open(os.path.basename(script), 'r')
56    exe = get_executable_name(f.readline())
57    try:
58        res = subprocess.check_output([exe, script, param]).decode().strip()
59    except Exception:
60        print('Error grabbing script output: %s, executable %s' % (script, exe))
61        raise
62    f.close()
63    if res == '':
64        return []
65    return [clean_path_slashes(rebase_script_path(script, name)) for name in res.split(',')]
66
67
68def auto_script(script):
69    # Set the CWD to the script directory.
70    os.chdir(get_child_script_dirname(script))
71    base_script = os.path.basename(script)
72    info = {
73        'inputs': grab_from_script(base_script, 'inputs'),
74        'outputs': grab_from_script(base_script, 'outputs')
75    }
76    # Reset the CWD to the root ANGLE directory.
77    os.chdir(root_dir)
78    return info
79
80
81generators = {
82    'ANGLE format':
83        'src/libANGLE/renderer/gen_angle_format_table.py',
84    'ANGLE load functions table':
85        'src/libANGLE/renderer/gen_load_functions_table.py',
86    'ANGLE load texture border functions table':
87        'src/libANGLE/renderer/gen_load_texture_border_functions_table.py',
88    'ANGLE shader preprocessor':
89        'src/compiler/preprocessor/generate_parser.py',
90    'ANGLE shader translator':
91        'src/compiler/translator/generate_parser.py',
92    'D3D11 blit shader selection':
93        'src/libANGLE/renderer/d3d/d3d11/gen_blit11helper.py',
94    'D3D11 format':
95        'src/libANGLE/renderer/d3d/d3d11/gen_texture_format_table.py',
96    'DXGI format':
97        'src/libANGLE/renderer/gen_dxgi_format_table.py',
98    'DXGI format support':
99        'src/libANGLE/renderer/gen_dxgi_support_tables.py',
100    'Emulated HLSL functions':
101        'src/compiler/translator/gen_emulated_builtin_function_tables.py',
102    'Extension files':
103        'src/libANGLE/gen_extensions.py',
104    'GL copy conversion table':
105        'src/libANGLE/gen_copy_conversion_table.py',
106    'GL CTS (dEQP) build files':
107        'scripts/gen_vk_gl_cts_build.py',
108    'GL/EGL/WGL loader':
109        'scripts/generate_loader.py',
110    'GL/EGL entry points':
111        'scripts/generate_entry_points.py',
112    'GLenum value to string map':
113        'scripts/gen_gl_enum_utils.py',
114    'GL format map':
115        'src/libANGLE/gen_format_map.py',
116    'Metal format table':
117        'src/libANGLE/renderer/metal/gen_mtl_format_table.py',
118    'Metal default shaders':
119        'src/libANGLE/renderer/metal/shaders/gen_mtl_internal_shaders.py',
120    'OpenGL dispatch table':
121        'src/libANGLE/renderer/gl/generate_gl_dispatch_table.py',
122    'overlay fonts':
123        'src/libANGLE/gen_overlay_fonts.py',
124    'overlay widgets':
125        'src/libANGLE/gen_overlay_widgets.py',
126    'packed enum':
127        'src/common/gen_packed_gl_enums.py',
128    'proc table':
129        'scripts/gen_proc_table.py',
130    'restricted traces':
131        'src/tests/restricted_traces/gen_restricted_traces.py',
132    'SPIR-V helpers':
133        'src/common/spirv/gen_spirv_builder_and_parser.py',
134    'Static builtins':
135        'src/compiler/translator/gen_builtin_symbols.py',
136    'Test spec JSON':
137        'infra/specs/generate_test_spec_json.py',
138    'uniform type':
139        'src/common/gen_uniform_type_table.py',
140    'Vulkan format':
141        'src/libANGLE/renderer/vulkan/gen_vk_format_table.py',
142    'Vulkan internal shader programs':
143        'src/libANGLE/renderer/vulkan/gen_vk_internal_shaders.py',
144    'Vulkan mandatory format support table':
145        'src/libANGLE/renderer/vulkan/gen_vk_mandatory_format_support_table.py',
146}
147
148
149def md5(fname):
150    hash_md5 = hashlib.md5()
151    with open(fname, "r") as f:
152        for chunk in iter(lambda: f.read(4096), ""):
153            hash_md5.update(chunk.encode())
154    return hash_md5.hexdigest()
155
156
157def get_hash_file_name(name):
158    return name.replace(' ', '_').replace('/', '_') + '.json'
159
160
161def any_hash_dirty(name, filenames, new_hashes, old_hashes):
162    found_dirty_hash = False
163
164    for fname in filenames:
165        if not os.path.isfile(fname):
166            print('File not found: "%s". Code gen dirty for %s' % (fname, name))
167            found_dirty_hash = True
168        else:
169            new_hashes[fname] = md5(fname)
170            if (not fname in old_hashes) or (old_hashes[fname] != new_hashes[fname]):
171                print('Hash for "%s" dirty for %s generator.' % (fname, name))
172                found_dirty_hash = True
173    return found_dirty_hash
174
175
176def any_old_hash_missing(all_new_hashes, all_old_hashes):
177    result = False
178    for file, old_hashes in all_old_hashes.items():
179        if file not in all_new_hashes:
180            print('"%s" does not exist. Code gen dirty.' % file)
181            result = True
182        else:
183            for name, _ in old_hashes.items():
184                if name not in all_new_hashes[file]:
185                    print('Hash for %s is missing from "%s". Code gen is dirty.' % (name, file))
186                    result = True
187    return result
188
189
190def update_output_hashes(script, outputs, new_hashes):
191    for output in outputs:
192        if not os.path.isfile(output):
193            print('Output is missing from %s: %s' % (script, output))
194            sys.exit(1)
195        new_hashes[output] = md5(output)
196
197
198def load_hashes():
199    hashes = {}
200    for file in os.listdir(hash_dir):
201        hash_fname = os.path.join(hash_dir, file)
202        with open(hash_fname) as hash_file:
203            try:
204                hashes[file] = json.load(hash_file)
205            except ValueError:
206                raise Exception("Could not decode JSON from %s" % file)
207    return hashes
208
209
210def main():
211    os.chdir(script_dir)
212
213    all_old_hashes = load_hashes()
214    all_new_hashes = {}
215    any_dirty = False
216
217    verify_only = False
218    if len(sys.argv) > 1 and sys.argv[1] == '--verify-no-dirty':
219        verify_only = True
220
221    for name, script in sorted(generators.items()):
222        info = auto_script(script)
223        fname = get_hash_file_name(name)
224        filenames = info['inputs'] + info['outputs'] + [script]
225        new_hashes = {}
226        if fname not in all_old_hashes:
227            all_old_hashes[fname] = {}
228        if any_hash_dirty(name, filenames, new_hashes, all_old_hashes[fname]):
229            any_dirty = True
230
231            if not verify_only:
232                print('Running ' + name + ' code generator')
233
234                # Set the CWD to the script directory.
235                os.chdir(get_child_script_dirname(script))
236
237                f = open(os.path.basename(script), "r")
238                if subprocess.call([get_executable_name(f.readline()),
239                                    os.path.basename(script)]) != 0:
240                    sys.exit(1)
241                f.close()
242
243        # Update the hash dictionary.
244        all_new_hashes[fname] = new_hashes
245
246    if any_old_hash_missing(all_new_hashes, all_old_hashes):
247        any_dirty = True
248
249    if verify_only:
250        sys.exit(any_dirty)
251
252    if any_dirty:
253        args = ['git.bat'] if os.name == 'nt' else ['git']
254        args += ['cl', 'format']
255        print('Calling git cl format')
256        if subprocess.call(args) != 0:
257            sys.exit(1)
258
259        # Update the output hashes again since they can be formatted.
260        for name, script in sorted(generators.items()):
261            info = auto_script(script)
262            fname = get_hash_file_name(name)
263            update_output_hashes(name, info['outputs'], all_new_hashes[fname])
264
265        os.chdir(script_dir)
266
267        for fname, new_hashes in all_new_hashes.items():
268            hash_fname = os.path.join(hash_dir, fname)
269            json.dump(
270                new_hashes,
271                open(hash_fname, "w"),
272                indent=2,
273                sort_keys=True,
274                separators=(',', ':\n    '))
275
276
277if __name__ == '__main__':
278    sys.exit(main())
279