Bump actions/checkout from 4.1.3 to 4.1.4
[glslang.git] / gen_extension_headers.py
blob0638720a074cb33a36d0f1ae1ba69ab8a8f43685
1 #!/usr/bin/env python3
3 # Copyright (c) 2020 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 import glob
18 import sys
19 import os
21 def generate_main(glsl_files, output_header_file):
22 # Write commit ID to output header file
23 with open(output_header_file, "w") as header_file:
24 # Copyright Notice
25 header_string = '/***************************************************************************\n'
26 header_string += ' *\n'
27 header_string += ' * Copyright (c) 2015-2021 The Khronos Group Inc.\n'
28 header_string += ' * Copyright (c) 2015-2021 Valve Corporation\n'
29 header_string += ' * Copyright (c) 2015-2021 LunarG, Inc.\n'
30 header_string += ' * Copyright (c) 2015-2021 Google Inc.\n'
31 header_string += ' * Copyright (c) 2021 Advanced Micro Devices, Inc.All rights reserved.\n'
32 header_string += ' *\n'
33 header_string += ' ****************************************************************************/\n'
34 header_string += '#pragma once\n\n'
35 header_string += '#ifndef _INTRINSIC_EXTENSION_HEADER_H_\n'
36 header_string += '#define _INTRINSIC_EXTENSION_HEADER_H_\n\n'
37 header_file.write(header_string)
39 symbol_name_list = []
41 for i in glsl_files:
42 glsl_contents = open(i,"r").read()
44 filename = os.path.basename(i)
45 symbol_name = filename.split(".")[0]
46 symbol_name_list.append(symbol_name)
47 header_name = symbol_name + ".h"
48 header_str = 'std::string %s_GLSL = R"(\n%s\n)";\n' % (symbol_name, glsl_contents)
49 header_str += '\n'
50 header_file.write(header_str)
52 contents = ''
53 contents += '\n'
54 contents += 'std::string getIntrinsic(const char* const* shaders, int n) {\n'
55 contents += '\tstd::string shaderString = "";\n';
57 contents += '\tfor (int i = 0; i < n; i++) {\n'
59 for symbol_name in symbol_name_list:
60 contents += '\t\tif (strstr(shaders[i], "%s") != nullptr) {\n' % (symbol_name)
61 contents += '\t\t shaderString.append(%s_GLSL);\n' % (symbol_name)
62 contents += '\t\t}\n'
64 contents += '\t}\n'
65 contents += '\treturn shaderString;\n';
66 contents += '}\n'
68 contents += '\n#endif\n'
69 header_file.write(contents)
71 def main():
72 if len(sys.argv) < 2:
73 raise Exception("Invalid number of arguments")
75 i = 0
76 while i < len(sys.argv):
77 opt = sys.argv[i]
78 i = i + 1
80 if opt == "-i" or opt == "-o":
81 if i == len(sys.argv):
82 raise Exception("Expected path after {}".format(opt))
83 val = sys.argv[i]
84 i = i + 1
85 if (opt == "-i"):
86 input_dir = val
87 elif (opt == "-o"):
88 output_file = val
89 else:
90 raise Exception("Unknown flag {}".format(opt))
92 glsl_files = glob.glob(input_dir + '/*.glsl')
94 # Generate main header
95 generate_main(glsl_files, output_file)
97 if __name__ == '__main__':
98 main()