xref: /aosp_15_r20/external/angle/src/program_serialize_data_version.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*8975f5c5SAndroid Build Coastguard Worker#  Copyright 2022 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker#  Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker#  found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker
6*8975f5c5SAndroid Build Coastguard Worker# Generate ANGLEShaderProgramVersion.h with hash of files affecting data
7*8975f5c5SAndroid Build Coastguard Worker# used in serializing/deserializing shader programs.
8*8975f5c5SAndroid Build Coastguard Workerimport hashlib
9*8975f5c5SAndroid Build Coastguard Workerimport argparse
10*8975f5c5SAndroid Build Coastguard Workerimport shlex
11*8975f5c5SAndroid Build Coastguard Worker
12*8975f5c5SAndroid Build Coastguard Worker
13*8975f5c5SAndroid Build Coastguard Worker# Generate a hash from a list of files defined in angle_code_files
14*8975f5c5SAndroid Build Coastguard Workerdef GenerateHashOfAffectedFiles(angle_code_files):
15*8975f5c5SAndroid Build Coastguard Worker    hash_md5 = hashlib.md5()
16*8975f5c5SAndroid Build Coastguard Worker    for file in angle_code_files:
17*8975f5c5SAndroid Build Coastguard Worker        hash_md5.update(file.encode(encoding='utf-8'))
18*8975f5c5SAndroid Build Coastguard Worker        with open(file, 'r', encoding='utf-8') as f:
19*8975f5c5SAndroid Build Coastguard Worker            for chunk in iter(lambda: f.read(4096), ""):
20*8975f5c5SAndroid Build Coastguard Worker                hash_md5.update(chunk.encode())
21*8975f5c5SAndroid Build Coastguard Worker    return hash_md5.hexdigest(), hash_md5.digest_size
22*8975f5c5SAndroid Build Coastguard Worker
23*8975f5c5SAndroid Build Coastguard Worker
24*8975f5c5SAndroid Build Coastguard Worker# The script is expecting two mandatory input arguments:
25*8975f5c5SAndroid Build Coastguard Worker# 1) output_file: the header file where we will add two constants that indicate
26*8975f5c5SAndroid Build Coastguard Worker# the version of the code files that affect shader program compilations
27*8975f5c5SAndroid Build Coastguard Worker# 2) response_file_name: a file that includes a list of code files that affect
28*8975f5c5SAndroid Build Coastguard Worker# shader program compilations
29*8975f5c5SAndroid Build Coastguard Workerparser = argparse.ArgumentParser(description='Generate the file ANGLEShaderProgramVersion.h')
30*8975f5c5SAndroid Build Coastguard Workerparser.add_argument(
31*8975f5c5SAndroid Build Coastguard Worker    'output_file',
32*8975f5c5SAndroid Build Coastguard Worker    help='path (relative to build directory) to output file name, stores ANGLE_PROGRAM_VERSION and ANGLE_PROGRAM_VERSION_HASH_SIZE'
33*8975f5c5SAndroid Build Coastguard Worker)
34*8975f5c5SAndroid Build Coastguard Workerparser.add_argument(
35*8975f5c5SAndroid Build Coastguard Worker    'response_file_name',
36*8975f5c5SAndroid Build Coastguard Worker    help='path (relative to build directory) to response file name. The response file stores a list of program files that ANGLE_PROGRAM_VERSION hashes over. See https://gn.googlesource.com/gn/+/main/docs/reference.md#var_response_file_contents'
37*8975f5c5SAndroid Build Coastguard Worker)
38*8975f5c5SAndroid Build Coastguard Worker
39*8975f5c5SAndroid Build Coastguard Workerargs = parser.parse_args()
40*8975f5c5SAndroid Build Coastguard Worker
41*8975f5c5SAndroid Build Coastguard Workeroutput_file = args.output_file
42*8975f5c5SAndroid Build Coastguard Workerresponse_file_name = args.response_file_name
43*8975f5c5SAndroid Build Coastguard Worker
44*8975f5c5SAndroid Build Coastguard Workerwith open(response_file_name, "r") as input_files_for_hash_generation:
45*8975f5c5SAndroid Build Coastguard Worker    angle_code_files = shlex.split(input_files_for_hash_generation)
46*8975f5c5SAndroid Build Coastguard Workerdigest, digest_size = GenerateHashOfAffectedFiles(angle_code_files)
47*8975f5c5SAndroid Build Coastguard Workerhfile = open(output_file, 'w')
48*8975f5c5SAndroid Build Coastguard Workerhfile.write('#define ANGLE_PROGRAM_VERSION "%s"\n' % digest)
49*8975f5c5SAndroid Build Coastguard Workerhfile.write('#define ANGLE_PROGRAM_VERSION_HASH_SIZE %d\n' % digest_size)
50*8975f5c5SAndroid Build Coastguard Workerhfile.close()
51