1bf215546Sopenharmony_ci#!/usr/bin/env python3
2bf215546Sopenharmony_ci# Copyright © 2021-2021 Yonggang Luo
3bf215546Sopenharmony_ci
4bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a copy
5bf215546Sopenharmony_ci# of this software and associated documentation files (the "Software"), to deal
6bf215546Sopenharmony_ci# in the Software without restriction, including without limitation the rights
7bf215546Sopenharmony_ci# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8bf215546Sopenharmony_ci# copies of the Software, and to permit persons to whom the Software is
9bf215546Sopenharmony_ci# furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_ci# The above copyright notice and this permission notice shall be included in
12bf215546Sopenharmony_ci# all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci
14bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17bf215546Sopenharmony_ci# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19bf215546Sopenharmony_ci# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20bf215546Sopenharmony_ci# SOFTWARE.
21bf215546Sopenharmony_ci
22bf215546Sopenharmony_cigen_help = """Generates visual studio module definition file."""
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ciimport argparse
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci"""
27bf215546Sopenharmony_ciFor input template definition file
28bf215546Sopenharmony_ciFor gcc/x64,gcc/arm64,visual studio
29bf215546Sopenharmony_ci`wglMakeCurrent@8                     @357` => `wglMakeCurrent @357`
30bf215546Sopenharmony_ci`DrvCopyContext@12` => `DrvCopyContext`
31bf215546Sopenharmony_ci`stw_get_device` => `stw_get_device`
32bf215546Sopenharmony_ciFor gcc/x86,gcc/arm
33bf215546Sopenharmony_ci`wglMakeCurrent@8                     @357` => `wglMakeCurrent@8 @357 == wglMakeCurrent`
34bf215546Sopenharmony_ci`DrvCopyContext@12` => `DrvCopyContext@12 == DrvCopyContext`
35bf215546Sopenharmony_ci`stw_get_device` => `stw_get_device`
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci"""
38bf215546Sopenharmony_cidef gen_vs_module_def(in_file: str, out_file: str, compiler_id: str, cpu_family: str) -> None:
39bf215546Sopenharmony_ci    out_file_lines = ['EXPORTS']
40bf215546Sopenharmony_ci    with open(in_file, 'r', encoding='utf-8') as f:
41bf215546Sopenharmony_ci        lines = f.readlines()
42bf215546Sopenharmony_ci        for line in lines:
43bf215546Sopenharmony_ci            line = line.strip()
44bf215546Sopenharmony_ci            tokens = line.split(';')
45bf215546Sopenharmony_ci            if not tokens:
46bf215546Sopenharmony_ci                continue
47bf215546Sopenharmony_ci            def_infos = [x for x in tokens[0].split(' ') if len(x) > 0]
48bf215546Sopenharmony_ci            if not def_infos:
49bf215546Sopenharmony_ci                if line:
50bf215546Sopenharmony_ci                    out_file_lines.append('\t' + line)
51bf215546Sopenharmony_ci                else:
52bf215546Sopenharmony_ci                    out_file_lines.append('')
53bf215546Sopenharmony_ci                continue
54bf215546Sopenharmony_ci            name_infos = def_infos[0].split('@')
55bf215546Sopenharmony_ci            if not name_infos:
56bf215546Sopenharmony_ci                out_file_lines.append('\t;' + line)
57bf215546Sopenharmony_ci                continue
58bf215546Sopenharmony_ci            order_info = '' if len(def_infos) <= 1 else def_infos[1]
59bf215546Sopenharmony_ci            if def_infos[0] != name_infos[0] and \
60bf215546Sopenharmony_ci                (compiler_id == 'gcc') and (cpu_family not in {'x86_64', 'aarch64'}):
61bf215546Sopenharmony_ci                if order_info:
62bf215546Sopenharmony_ci                    out_file_lines.append('\t' + def_infos[0] + ' ' + order_info + ' == ' + name_infos[0])
63bf215546Sopenharmony_ci                else:
64bf215546Sopenharmony_ci                    out_file_lines.append('\t' + def_infos[0] + ' == ' + name_infos[0])
65bf215546Sopenharmony_ci            else:
66bf215546Sopenharmony_ci                if order_info:
67bf215546Sopenharmony_ci                    out_file_lines.append('\t' + name_infos[0] + ' ' + order_info)
68bf215546Sopenharmony_ci                else:
69bf215546Sopenharmony_ci                    out_file_lines.append('\t' + name_infos[0])
70bf215546Sopenharmony_ci    with open(out_file, 'wb') as f:
71bf215546Sopenharmony_ci        out_file_content = '\n'.join(out_file_lines) + '\n'
72bf215546Sopenharmony_ci        f.write(out_file_content.encode('utf-8'))
73bf215546Sopenharmony_ci'''
74bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/libgl-gdi/opengl32.def.in --out_file src/gallium/targets/libgl-gdi/opengl32.def --compiler_id gcc --cpu_family x86_64
75bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/libgl-gdi/opengl32.def.in --out_file src/gallium/targets/libgl-gdi/opengl32.mingw.def --compiler_id gcc --cpu_family x86
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/osmesa/osmesa.def.in --out_file src/gallium/targets/osmesa/osmesa.def --compiler_id gcc --cpu_family x86_64
78bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/osmesa/osmesa.def.in --out_file src/gallium/targets/osmesa/osmesa.mingw.def --compiler_id gcc --cpu_family x86
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/wgl/gallium_wgl.def.in --out_file src/gallium/targets/wgl/gallium_wgl.def --compiler_id gcc --cpu_family x86_64
81bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/wgl/gallium_wgl.def.in --out_file src/gallium/targets/wgl/gallium_wgl.mingw.def --compiler_id gcc --cpu_family x86
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/egl/main/egl.def.in --out_file src/egl/main/egl.def --compiler_id gcc --cpu_family x86_64
84bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/egl/main/egl.def.in --out_file src/egl/main/egl.mingw.def --compiler_id gcc --cpu_family x86
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/lavapipe/vulkan_lvp.def.in --out_file src/gallium/targets/lavapipe/vulkan_lvp.def --compiler_id gcc --cpu_family x86_64
87bf215546Sopenharmony_cipython ./bin/gen_vs_module_defs.py --in_file src/gallium/targets/lavapipe/vulkan_lvp.def.in --out_file src/gallium/targets/lavapipe/vulkan_lvp.mingw.def --compiler_id gcc --cpu_family x86
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci'''
90bf215546Sopenharmony_ciif __name__ == "__main__":
91bf215546Sopenharmony_ci    parser = argparse.ArgumentParser(description=gen_help)
92bf215546Sopenharmony_ci    parser.add_argument('--in_file', help='input template moudle definition file')
93bf215546Sopenharmony_ci    parser.add_argument('--out_file', help='output moudle definition file')
94bf215546Sopenharmony_ci    parser.add_argument('--compiler_id', help='compiler id')
95bf215546Sopenharmony_ci    parser.add_argument('--cpu_family', help='cpu family')
96bf215546Sopenharmony_ci    args = parser.parse_args()
97bf215546Sopenharmony_ci    # print(args)
98bf215546Sopenharmony_ci    gen_vs_module_def(args.in_file, args.out_file, args.compiler_id, args.cpu_family)
99