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