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 'ANGLE shader preprocessor': 77 'src/compiler/preprocessor/generate_parser.py', 78 'ANGLE shader translator': 79 'src/compiler/translator/generate_parser.py', 80 'D3D11 blit shader selection': 81 'src/libANGLE/renderer/d3d/d3d11/gen_blit11helper.py', 82 'D3D11 format': 83 'src/libANGLE/renderer/d3d/d3d11/gen_texture_format_table.py', 84 'DXGI format': 85 'src/libANGLE/renderer/d3d/d3d11/gen_dxgi_format_table.py', 86 'DXGI format support': 87 'src/libANGLE/renderer/d3d/d3d11/gen_dxgi_support_tables.py', 88 'Emulated HLSL functions': 89 'src/compiler/translator/gen_emulated_builtin_function_tables.py', 90 'GL copy conversion table': 91 'src/libANGLE/gen_copy_conversion_table.py', 92 'GL CTS (dEQP) build files': 93 'scripts/gen_vk_gl_cts_build.py', 94 'GL/EGL/WGL loader': 95 'scripts/generate_loader.py', 96 'GL/EGL entry points': 97 'scripts/generate_entry_points.py', 98 'GLenum value to string map': 99 'scripts/gen_gl_enum_utils.py', 100 'GL format map': 101 'src/libANGLE/gen_format_map.py', 102 'Metal format table': 103 'src/libANGLE/renderer/metal/gen_mtl_format_table.py', 104 'Metal default shaders': 105 'src/libANGLE/renderer/metal/shaders/gen_mtl_internal_shaders.py', 106 'OpenGL dispatch table': 107 'src/libANGLE/renderer/gl/generate_gl_dispatch_table.py', 108 'overlay fonts': 109 'src/libANGLE/gen_overlay_fonts.py', 110 'overlay widgets': 111 'src/libANGLE/gen_overlay_widgets.py', 112 'packed enum': 113 'src/common/gen_packed_gl_enums.py', 114 'proc table': 115 'scripts/gen_proc_table.py', 116 'restricted traces': 117 'src/tests/perf_tests/restricted_traces/gen_restricted_traces.py', 118 'Static builtins': 119 'src/compiler/translator/gen_builtin_symbols.py', 120 'uniform type': 121 'src/common/gen_uniform_type_table.py', 122 'Vulkan format': 123 'src/libANGLE/renderer/vulkan/gen_vk_format_table.py', 124 'Vulkan mandatory format support table': 125 'src/libANGLE/renderer/vulkan/gen_vk_mandatory_format_support_table.py', 126 'Vulkan internal shader programs': 127 'src/libANGLE/renderer/vulkan/gen_vk_internal_shaders.py', 128} 129 130 131def md5(fname): 132 hash_md5 = hashlib.md5() 133 with open(fname, "r") as f: 134 for chunk in iter(lambda: f.read(4096), b""): 135 hash_md5.update(chunk) 136 return hash_md5.hexdigest() 137 138 139def get_hash_file_name(name): 140 return name.replace(' ', '_').replace('/', '_') + '.json' 141 142 143def any_hash_dirty(name, filenames, new_hashes, old_hashes): 144 found_dirty_hash = False 145 146 for fname in filenames: 147 if not os.path.isfile(fname): 148 print('File not found: "%s". Code gen dirty for %s' % (fname, name)) 149 found_dirty_hash = True 150 else: 151 new_hashes[fname] = md5(fname) 152 if (not fname in old_hashes) or (old_hashes[fname] != new_hashes[fname]): 153 print('Hash for "%s" dirty for %s generator.' % (fname, name)) 154 found_dirty_hash = True 155 return found_dirty_hash 156 157 158def any_old_hash_missing(all_new_hashes, all_old_hashes): 159 result = False 160 for file, old_hashes in all_old_hashes.iteritems(): 161 if file not in all_new_hashes: 162 print('"%s" does not exist. Code gen dirty.' % file) 163 result = True 164 else: 165 for name, _ in old_hashes.iteritems(): 166 if name not in all_new_hashes[file]: 167 print('Hash for %s is missing from "%s". Code gen is dirty.' % (name, file)) 168 result = True 169 return result 170 171 172def update_output_hashes(script, outputs, new_hashes): 173 for output in outputs: 174 if not os.path.isfile(output): 175 print('Output is missing from %s: %s' % (script, output)) 176 sys.exit(1) 177 new_hashes[output] = md5(output) 178 179 180def load_hashes(): 181 hashes = {} 182 for file in os.listdir(hash_dir): 183 hash_fname = os.path.join(hash_dir, file) 184 with open(hash_fname) as hash_file: 185 hashes[file] = json.load(open(hash_fname)) 186 return hashes 187 188 189def main(): 190 os.chdir(script_dir) 191 192 all_old_hashes = load_hashes() 193 all_new_hashes = {} 194 any_dirty = False 195 196 verify_only = False 197 if len(sys.argv) > 1 and sys.argv[1] == '--verify-no-dirty': 198 verify_only = True 199 200 for name, script in sorted(generators.iteritems()): 201 info = auto_script(script) 202 fname = get_hash_file_name(name) 203 filenames = info['inputs'] + info['outputs'] + [script] 204 new_hashes = {} 205 if fname not in all_old_hashes: 206 all_old_hashes[fname] = {} 207 if any_hash_dirty(name, filenames, new_hashes, all_old_hashes[fname]): 208 any_dirty = True 209 210 if not verify_only: 211 # Set the CWD to the script directory. 212 os.chdir(get_child_script_dirname(script)) 213 214 print('Running ' + name + ' code generator') 215 216 f = open(os.path.basename(script), "r") 217 if subprocess.call([get_executable_name(f.readline()), 218 os.path.basename(script)]) != 0: 219 sys.exit(1) 220 f.close() 221 222 # Update the hash dictionary. 223 all_new_hashes[fname] = new_hashes 224 225 if any_old_hash_missing(all_new_hashes, all_old_hashes): 226 any_dirty = True 227 228 if verify_only: 229 sys.exit(any_dirty) 230 231 if any_dirty: 232 args = ['git.bat'] if os.name == 'nt' else ['git'] 233 args += ['cl', 'format'] 234 print('Calling git cl format') 235 if subprocess.call(args) != 0: 236 sys.exit(1) 237 238 # Update the output hashes again since they can be formatted. 239 for name, script in sorted(generators.iteritems()): 240 info = auto_script(script) 241 fname = get_hash_file_name(name) 242 update_output_hashes(name, info['outputs'], all_new_hashes[fname]) 243 244 os.chdir(script_dir) 245 246 for fname, new_hashes in all_new_hashes.iteritems(): 247 hash_fname = os.path.join(hash_dir, fname) 248 json.dump( 249 new_hashes, 250 open(hash_fname, "w"), 251 indent=2, 252 sort_keys=True, 253 separators=(',', ':\n ')) 254 255 256if __name__ == '__main__': 257 sys.exit(main()) 258