Merge pull request #3046 from amdrexu/bugfix
[platform/upstream/glslang.git] / gen_extension_headers.py
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2020 Google Inc.
4 #
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
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
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.\r
16 \r
17 import glob\r
18 import sys\r
19 import os\r
20 \r
21 def generate_main(glsl_files, output_header_file):\r
22     # Write commit ID to output header file\r
23     with open(output_header_file, "w") as header_file:\r
24         # Copyright Notice\r
25         header_string =  '/***************************************************************************\n'\r
26         header_string += ' *\n'\r
27         header_string += ' * Copyright (c) 2015-2021 The Khronos Group Inc.\n'\r
28         header_string += ' * Copyright (c) 2015-2021 Valve Corporation\n'\r
29         header_string += ' * Copyright (c) 2015-2021 LunarG, Inc.\n'\r
30         header_string += ' * Copyright (c) 2015-2021 Google Inc.\n'\r
31         header_string += ' * Copyright (c) 2021 Advanced Micro Devices, Inc.All rights reserved.\n'\r
32         header_string += ' *\n'\r
33         header_string += ' ****************************************************************************/\n'\r
34         header_string += '#pragma once\n\n'\r
35         header_string += '#ifndef _INTRINSIC_EXTENSION_HEADER_H_\n'\r
36         header_string += '#define _INTRINSIC_EXTENSION_HEADER_H_\n\n'\r
37         header_file.write(header_string)\r
38 \r
39         symbol_name_list = []\r
40 \r
41         for i in glsl_files:\r
42             glsl_contents = open(i,"r").read()\r
43 \r
44             filename = os.path.basename(i)\r
45             symbol_name = filename.split(".")[0]\r
46             symbol_name_list.append(symbol_name)\r
47             header_name = symbol_name + ".h"\r
48             header_str = 'std::string %s_GLSL = R"(\n%s\n)";\n' % (symbol_name, glsl_contents)\r
49             header_str += '\n'\r
50             header_file.write(header_str)\r
51 \r
52         contents = ''\r
53         contents += '\n'
54         contents += 'std::string getIntrinsic(const char* const* shaders, int n) {\n'
55         contents += '\tstd::string shaderString = "";\n';
56
57         contents += '\tfor (int i = 0; i < n; i++) {\n'
58
59         for symbol_name in symbol_name_list:\r
60             contents += '\t\tif (strstr(shaders[i], "%s") != NULL) {\n'   % (symbol_name)
61             contents += '\t\t    shaderString.append(%s_GLSL);\n' % (symbol_name)
62             contents += '\t\t}\n'\r
63
64         contents += '\t}\n'
65         contents += '\treturn shaderString;\n';
66         contents += '}\n'\r
67 \r
68         contents += '\n#endif\n'\r
69         header_file.write(contents)\r
70 \r
71 def main():\r
72     if len(sys.argv) < 2:
73         raise Exception("Invalid number of arguments")\r
74 \r
75     i = 0\r
76     while i < len(sys.argv):
77         opt = sys.argv[i]
78         i = i + 1
79
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))\r
91 \r
92     glsl_files = glob.glob(input_dir + '/*.glsl')\r
93 \r
94     # Generate main header\r
95     generate_main(glsl_files, output_file)\r
96 \r
97 if __name__ == '__main__':\r
98     main()