• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2
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    if 'vpython' in first_line:
44        return 'vpython.bat' if platform.system() == 'Windows' else 'vpython'
45    return 'python'
46
47
48def grab_from_script(script, param):
49    res = ''
50    f = open(os.path.basename(script), "r")
51    res = subprocess.check_output([get_executable_name(f.readline()), script, param]).strip()
52    f.close()
53    if res == '':
54        return []
55    return [clean_path_slashes(rebase_script_path(script, name)) for name in res.split(',')]
56
57
58def auto_script(script):
59    # Set the CWD to the script directory.
60    os.chdir(get_child_script_dirname(script))
61    base_script = os.path.basename(script)
62    info = {
63        'inputs': grab_from_script(base_script, 'inputs'),
64        'outputs': grab_from_script(base_script, 'outputs')
65    }
66    # Reset the CWD to the root ANGLE directory.
67    os.chdir(root_dir)
68    return info
69
70
71generators = {
72    'ANGLE format':
73        'src/libANGLE/renderer/gen_angle_format_table.py',
74    'ANGLE load functions table':
75        'src/libANGLE/renderer/gen_load_functions_table.py',
76    'D3D11 blit shader selection':
77        'src/libANGLE/renderer/d3d/d3d11/gen_blit11helper.py',
78    'D3D11 format':
79        'src/libANGLE/renderer/d3d/d3d11/gen_texture_format_table.py',
80    'DXGI format':
81        'src/libANGLE/renderer/d3d/d3d11/gen_dxgi_format_table.py',
82    'DXGI format support':
83        'src/libANGLE/renderer/d3d/d3d11/gen_dxgi_support_tables.py',
84    'GL copy conversion table':
85        'src/libANGLE/gen_copy_conversion_table.py',
86    'GL/EGL/WGL loader':
87        'scripts/generate_loader.py',
88    'GL/EGL entry points':
89        'scripts/generate_entry_points.py',
90    'GLenum value to string map':
91        'scripts/gen_gl_enum_utils.py',
92    'GL format map':
93        'src/libANGLE/gen_format_map.py',
94    'uniform type':
95        'src/common/gen_uniform_type_table.py',
96    'OpenGL dispatch table':
97        'src/libANGLE/renderer/gl/generate_gl_dispatch_table.py',
98    'packed enum':
99        'src/common/gen_packed_gl_enums.py',
100    'proc table':
101        'scripts/gen_proc_table.py',
102    'Vulkan format':
103        'src/libANGLE/renderer/vulkan/gen_vk_format_table.py',
104    'Vulkan mandatory format support table':
105        'src/libANGLE/renderer/vulkan/gen_vk_mandatory_format_support_table.py',
106    'Vulkan internal shader programs':
107        'src/libANGLE/renderer/vulkan/gen_vk_internal_shaders.py',
108    'Emulated HLSL functions':
109        'src/compiler/translator/gen_emulated_builtin_function_tables.py',
110    'ESSL static builtins':
111        'src/compiler/translator/gen_builtin_symbols.py',
112}
113
114
115def md5(fname):
116    hash_md5 = hashlib.md5()
117    with open(fname, "r") as f:
118        for chunk in iter(lambda: f.read(4096), b""):
119            hash_md5.update(chunk)
120    return hash_md5.hexdigest()
121
122
123def get_hash_file_name(name):
124    return name.replace(' ', '_').replace('/', '_') + '.json'
125
126
127def any_hash_dirty(name, filenames, new_hashes, old_hashes):
128    found_dirty_hash = False
129
130    for fname in filenames:
131        if not os.path.isfile(fname):
132            print('File not found: "%s". Code gen dirty for %s' % (fname, name))
133            found_dirty_hash = True
134        else:
135            new_hashes[fname] = md5(fname)
136            if (not fname in old_hashes) or (old_hashes[fname] != new_hashes[fname]):
137                print('Hash for "%s" dirty for %s generator.' % (fname, name))
138                found_dirty_hash = True
139    return found_dirty_hash
140
141
142def any_old_hash_missing(all_new_hashes, all_old_hashes):
143    result = False
144    for file, old_hashes in all_old_hashes.iteritems():
145        if file not in all_new_hashes:
146            print('"%s" does not exist. Code gen dirty.' % file)
147            result = True
148        else:
149            for name, _ in old_hashes.iteritems():
150                if name not in all_new_hashes[file]:
151                    print('Hash for %s is missing from "%s". Code gen is dirty.' % (name, file))
152                    result = True
153    return result
154
155
156def update_output_hashes(script, outputs, new_hashes):
157    for output in outputs:
158        if not os.path.isfile(output):
159            print('Output is missing from %s: %s' % (script, output))
160            sys.exit(1)
161        new_hashes[output] = md5(output)
162
163
164def load_hashes():
165    hashes = {}
166    for file in os.listdir(hash_dir):
167        hash_fname = os.path.join(hash_dir, file)
168        with open(hash_fname) as hash_file:
169            hashes[file] = json.load(open(hash_fname))
170    return hashes
171
172
173def main():
174    os.chdir(script_dir)
175
176    all_old_hashes = load_hashes()
177    all_new_hashes = {}
178    any_dirty = False
179
180    verify_only = False
181    if len(sys.argv) > 1 and sys.argv[1] == '--verify-no-dirty':
182        verify_only = True
183
184    for name, script in sorted(generators.iteritems()):
185        info = auto_script(script)
186        fname = get_hash_file_name(name)
187        filenames = info['inputs'] + info['outputs'] + [script]
188        new_hashes = {}
189        if fname not in all_old_hashes:
190            all_old_hashes[fname] = {}
191        if any_hash_dirty(name, filenames, new_hashes, all_old_hashes[fname]):
192            any_dirty = True
193
194            if not verify_only:
195                # Set the CWD to the script directory.
196                os.chdir(get_child_script_dirname(script))
197
198                print('Running ' + name + ' code generator')
199
200                f = open(os.path.basename(script), "r")
201                if subprocess.call([get_executable_name(f.readline()),
202                                    os.path.basename(script)]) != 0:
203                    sys.exit(1)
204                f.close()
205
206        # Update the hash dictionary.
207        all_new_hashes[fname] = new_hashes
208
209    if any_old_hash_missing(all_new_hashes, all_old_hashes):
210        any_dirty = True
211
212    if verify_only:
213        sys.exit(any_dirty)
214
215    if any_dirty:
216        args = ['git.bat'] if os.name == 'nt' else ['git']
217        # The diff can be so large the arguments to clang-format can break the Windows command
218        # line length limits. Work around this by calling git cl format with --full.
219        args += ['cl', 'format', '--full']
220        print('Calling git cl format')
221        subprocess.call(args)
222
223        # Update the output hashes again since they can be formatted.
224        for name, script in sorted(generators.iteritems()):
225            info = auto_script(script)
226            fname = get_hash_file_name(name)
227            update_output_hashes(name, info['outputs'], all_new_hashes[fname])
228
229        os.chdir(script_dir)
230
231        for fname, new_hashes in all_new_hashes.iteritems():
232            hash_fname = os.path.join(hash_dir, fname)
233            json.dump(
234                new_hashes,
235                open(hash_fname, "w"),
236                indent=2,
237                sort_keys=True,
238                separators=(',', ':\n    '))
239
240
241if __name__ == '__main__':
242    sys.exit(main())
243