blob: 844ff33f4e4246d7d7c67bb9af7908a1483a1a62 [file] [log] [blame]
# Copyright 2017 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Runs ANGLE format table and other script code generation scripts.
import hashlib
import json
import os
import subprocess
import sys
import platform
script_dir = sys.path[0]
root_dir = os.path.abspath(os.path.join(script_dir, '..'))
hash_dir = 'code_generation_hashes'
# auto_script is a standard way for scripts to return their inputs and outputs.
def get_child_script_dirname(script):
# All script names are relative to ANGLE's root
return os.path.dirname(os.path.abspath(os.path.join(root_dir, script)))
# Replace all backslashes with forward slashes to be platform independent
def clean_path_slashes(path):
return path.replace("\\", "/")
# Takes a script file name which is relative to the code generation script's directory and
# changes it to be relative to the angle root directory
def rebase_script_path(script_path, relative_path):
return os.path.relpath(os.path.join(os.path.dirname(script_path), relative_path), root_dir)
# Check if we need a module from vpython
def get_executable_name(first_line):
binary = os.path.basename(first_line.strip().replace(' ', '/'))
if platform.system() == 'Windows':
if binary == 'python2':
return 'python.bat'
return binary + '.bat'
return binary
def grab_from_script(script, param):
res = ''
f = open(os.path.basename(script), 'r')
exe = get_executable_name(f.readline())
res = subprocess.check_output([exe, script, param]).decode().strip()
except Exception:
print('Error grabbing script output: %s, executable %s' % (script, exe))
if res == '':
return []
return [clean_path_slashes(rebase_script_path(script, name)) for name in res.split(',')]
def auto_script(script):
# Set the CWD to the script directory.
base_script = os.path.basename(script)
info = {
'inputs': grab_from_script(base_script, 'inputs'),
'outputs': grab_from_script(base_script, 'outputs')
# Reset the CWD to the root ANGLE directory.
return info
generators = {
'ANGLE format':
'ANGLE load functions table':
'ANGLE shader preprocessor':
'ANGLE shader translator':
'D3D11 blit shader selection':
'D3D11 format':
'DXGI format':
'DXGI format support':
'Emulated HLSL functions':
'GL copy conversion table':
'GL CTS (dEQP) build files':
'GL/EGL/WGL loader':
'GL/EGL entry points':
'GLenum value to string map':
'GL format map':
'Metal format table':
'Metal default shaders':
'OpenGL dispatch table':
'overlay fonts':
'overlay widgets':
'packed enum':
'proc table':
'restricted traces':
'SPIR-V helpers':
'Static builtins':
'uniform type':
'Vulkan format':
'Vulkan internal shader programs':
'Vulkan mandatory format support table':
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "r") as f:
for chunk in iter(lambda:, ""):
return hash_md5.hexdigest()
def get_hash_file_name(name):
return name.replace(' ', '_').replace('/', '_') + '.json'
def any_hash_dirty(name, filenames, new_hashes, old_hashes):
found_dirty_hash = False
for fname in filenames:
if not os.path.isfile(fname):
print('File not found: "%s". Code gen dirty for %s' % (fname, name))
found_dirty_hash = True
new_hashes[fname] = md5(fname)
if (not fname in old_hashes) or (old_hashes[fname] != new_hashes[fname]):
print('Hash for "%s" dirty for %s generator.' % (fname, name))
found_dirty_hash = True
return found_dirty_hash
def any_old_hash_missing(all_new_hashes, all_old_hashes):
result = False
for file, old_hashes in all_old_hashes.items():
if file not in all_new_hashes:
print('"%s" does not exist. Code gen dirty.' % file)
result = True
for name, _ in old_hashes.items():
if name not in all_new_hashes[file]:
print('Hash for %s is missing from "%s". Code gen is dirty.' % (name, file))
result = True
return result
def update_output_hashes(script, outputs, new_hashes):
for output in outputs:
if not os.path.isfile(output):
print('Output is missing from %s: %s' % (script, output))
new_hashes[output] = md5(output)
def load_hashes():
hashes = {}
for file in os.listdir(hash_dir):
hash_fname = os.path.join(hash_dir, file)
with open(hash_fname) as hash_file:
hashes[file] = json.load(hash_file)
except ValueError:
raise Exception("Could not decode JSON from %s" % file)
return hashes
def main():
all_old_hashes = load_hashes()
all_new_hashes = {}
any_dirty = False
verify_only = False
if len(sys.argv) > 1 and sys.argv[1] == '--verify-no-dirty':
verify_only = True
for name, script in sorted(generators.items()):
info = auto_script(script)
fname = get_hash_file_name(name)
filenames = info['inputs'] + info['outputs'] + [script]
new_hashes = {}
if fname not in all_old_hashes:
all_old_hashes[fname] = {}
if any_hash_dirty(name, filenames, new_hashes, all_old_hashes[fname]):
any_dirty = True
if not verify_only:
print('Running ' + name + ' code generator')
# Set the CWD to the script directory.
f = open(os.path.basename(script), "r")
os.path.basename(script)]) != 0:
# Update the hash dictionary.
all_new_hashes[fname] = new_hashes
if any_old_hash_missing(all_new_hashes, all_old_hashes):
any_dirty = True
if verify_only:
if any_dirty:
args = ['git.bat'] if == 'nt' else ['git']
args += ['cl', 'format']
print('Calling git cl format')
if != 0:
# Update the output hashes again since they can be formatted.
for name, script in sorted(generators.items()):
info = auto_script(script)
fname = get_hash_file_name(name)
update_output_hashes(name, info['outputs'], all_new_hashes[fname])
for fname, new_hashes in all_new_hashes.items():
hash_fname = os.path.join(hash_dir, fname)
open(hash_fname, "w"),
separators=(',', ':\n '))
if __name__ == '__main__':