Path: blob/main_old/scripts/run_code_generation.py
1693 views
#!/usr/bin/python31#2# Copyright 2017 The ANGLE Project Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5#6# run_code_generation.py:7# Runs ANGLE format table and other script code generation scripts.89import hashlib10import json11import os12import subprocess13import sys14import platform1516script_dir = sys.path[0]17root_dir = os.path.abspath(os.path.join(script_dir, '..'))1819hash_dir = 'code_generation_hashes'2021# auto_script is a standard way for scripts to return their inputs and outputs.222324def get_child_script_dirname(script):25# All script names are relative to ANGLE's root26return os.path.dirname(os.path.abspath(os.path.join(root_dir, script)))272829# Replace all backslashes with forward slashes to be platform independent30def clean_path_slashes(path):31return path.replace("\\", "/")323334# Takes a script file name which is relative to the code generation script's directory and35# changes it to be relative to the angle root directory36def rebase_script_path(script_path, relative_path):37return os.path.relpath(os.path.join(os.path.dirname(script_path), relative_path), root_dir)383940# Check if we need a module from vpython41def get_executable_name(first_line):42binary = os.path.basename(first_line.strip().replace(' ', '/'))43if platform.system() == 'Windows':44if binary == 'python2':45return 'python.bat'46else:47return binary + '.bat'48else:49return binary505152def grab_from_script(script, param):53res = ''54f = open(os.path.basename(script), 'r')55exe = get_executable_name(f.readline())56try:57res = subprocess.check_output([exe, script, param]).decode().strip()58except Exception:59print('Error grabbing script output: %s, executable %s' % (script, exe))60raise61f.close()62if res == '':63return []64return [clean_path_slashes(rebase_script_path(script, name)) for name in res.split(',')]656667def auto_script(script):68# Set the CWD to the script directory.69os.chdir(get_child_script_dirname(script))70base_script = os.path.basename(script)71info = {72'inputs': grab_from_script(base_script, 'inputs'),73'outputs': grab_from_script(base_script, 'outputs')74}75# Reset the CWD to the root ANGLE directory.76os.chdir(root_dir)77return info787980generators = {81'ANGLE format':82'src/libANGLE/renderer/gen_angle_format_table.py',83'ANGLE load functions table':84'src/libANGLE/renderer/gen_load_functions_table.py',85'ANGLE load texture border functions table':86'src/libANGLE/renderer/gen_load_texture_border_functions_table.py',87'ANGLE shader preprocessor':88'src/compiler/preprocessor/generate_parser.py',89'ANGLE shader translator':90'src/compiler/translator/generate_parser.py',91'D3D11 blit shader selection':92'src/libANGLE/renderer/d3d/d3d11/gen_blit11helper.py',93'D3D11 format':94'src/libANGLE/renderer/d3d/d3d11/gen_texture_format_table.py',95'DXGI format':96'src/libANGLE/renderer/gen_dxgi_format_table.py',97'DXGI format support':98'src/libANGLE/renderer/gen_dxgi_support_tables.py',99'Emulated HLSL functions':100'src/compiler/translator/gen_emulated_builtin_function_tables.py',101'GL copy conversion table':102'src/libANGLE/gen_copy_conversion_table.py',103'GL CTS (dEQP) build files':104'scripts/gen_vk_gl_cts_build.py',105'GL/EGL/WGL loader':106'scripts/generate_loader.py',107'GL/EGL entry points':108'scripts/generate_entry_points.py',109'GLenum value to string map':110'scripts/gen_gl_enum_utils.py',111'GL format map':112'src/libANGLE/gen_format_map.py',113'Metal format table':114'src/libANGLE/renderer/metal/gen_mtl_format_table.py',115'Metal default shaders':116'src/libANGLE/renderer/metal/shaders/gen_mtl_internal_shaders.py',117'OpenGL dispatch table':118'src/libANGLE/renderer/gl/generate_gl_dispatch_table.py',119'overlay fonts':120'src/libANGLE/gen_overlay_fonts.py',121'overlay widgets':122'src/libANGLE/gen_overlay_widgets.py',123'packed enum':124'src/common/gen_packed_gl_enums.py',125'proc table':126'scripts/gen_proc_table.py',127'restricted traces':128'src/tests/restricted_traces/gen_restricted_traces.py',129'SPIR-V helpers':130'src/common/spirv/gen_spirv_builder_and_parser.py',131'Static builtins':132'src/compiler/translator/gen_builtin_symbols.py',133'Test spec JSON':134'infra/specs/generate_test_spec_json.py',135'uniform type':136'src/common/gen_uniform_type_table.py',137'Vulkan format':138'src/libANGLE/renderer/vulkan/gen_vk_format_table.py',139'Vulkan internal shader programs':140'src/libANGLE/renderer/vulkan/gen_vk_internal_shaders.py',141'Vulkan mandatory format support table':142'src/libANGLE/renderer/vulkan/gen_vk_mandatory_format_support_table.py',143}144145146def md5(fname):147hash_md5 = hashlib.md5()148with open(fname, "r") as f:149for chunk in iter(lambda: f.read(4096), ""):150hash_md5.update(chunk.encode())151return hash_md5.hexdigest()152153154def get_hash_file_name(name):155return name.replace(' ', '_').replace('/', '_') + '.json'156157158def any_hash_dirty(name, filenames, new_hashes, old_hashes):159found_dirty_hash = False160161for fname in filenames:162if not os.path.isfile(fname):163print('File not found: "%s". Code gen dirty for %s' % (fname, name))164found_dirty_hash = True165else:166new_hashes[fname] = md5(fname)167if (not fname in old_hashes) or (old_hashes[fname] != new_hashes[fname]):168print('Hash for "%s" dirty for %s generator.' % (fname, name))169found_dirty_hash = True170return found_dirty_hash171172173def any_old_hash_missing(all_new_hashes, all_old_hashes):174result = False175for file, old_hashes in all_old_hashes.items():176if file not in all_new_hashes:177print('"%s" does not exist. Code gen dirty.' % file)178result = True179else:180for name, _ in old_hashes.items():181if name not in all_new_hashes[file]:182print('Hash for %s is missing from "%s". Code gen is dirty.' % (name, file))183result = True184return result185186187def update_output_hashes(script, outputs, new_hashes):188for output in outputs:189if not os.path.isfile(output):190print('Output is missing from %s: %s' % (script, output))191sys.exit(1)192new_hashes[output] = md5(output)193194195def load_hashes():196hashes = {}197for file in os.listdir(hash_dir):198hash_fname = os.path.join(hash_dir, file)199with open(hash_fname) as hash_file:200try:201hashes[file] = json.load(hash_file)202except ValueError:203raise Exception("Could not decode JSON from %s" % file)204return hashes205206207def main():208os.chdir(script_dir)209210all_old_hashes = load_hashes()211all_new_hashes = {}212any_dirty = False213214verify_only = False215if len(sys.argv) > 1 and sys.argv[1] == '--verify-no-dirty':216verify_only = True217218for name, script in sorted(generators.items()):219info = auto_script(script)220fname = get_hash_file_name(name)221filenames = info['inputs'] + info['outputs'] + [script]222new_hashes = {}223if fname not in all_old_hashes:224all_old_hashes[fname] = {}225if any_hash_dirty(name, filenames, new_hashes, all_old_hashes[fname]):226any_dirty = True227228if not verify_only:229print('Running ' + name + ' code generator')230231# Set the CWD to the script directory.232os.chdir(get_child_script_dirname(script))233234f = open(os.path.basename(script), "r")235if subprocess.call([get_executable_name(f.readline()),236os.path.basename(script)]) != 0:237sys.exit(1)238f.close()239240# Update the hash dictionary.241all_new_hashes[fname] = new_hashes242243if any_old_hash_missing(all_new_hashes, all_old_hashes):244any_dirty = True245246if verify_only:247sys.exit(any_dirty)248249if any_dirty:250args = ['git.bat'] if os.name == 'nt' else ['git']251args += ['cl', 'format']252print('Calling git cl format')253if subprocess.call(args) != 0:254sys.exit(1)255256# Update the output hashes again since they can be formatted.257for name, script in sorted(generators.items()):258info = auto_script(script)259fname = get_hash_file_name(name)260update_output_hashes(name, info['outputs'], all_new_hashes[fname])261262os.chdir(script_dir)263264for fname, new_hashes in all_new_hashes.items():265hash_fname = os.path.join(hash_dir, fname)266json.dump(267new_hashes,268open(hash_fname, "w"),269indent=2,270sort_keys=True,271separators=(',', ':\n '))272273274if __name__ == '__main__':275sys.exit(main())276277278