xref: /aosp_15_r20/external/mesa3d/src/vulkan/util/vk_icd_gen.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1# Copyright 2017 Intel Corporation
2#
3# Permission is hereby granted, free of charge, to any person obtaining a
4# copy of this software and associated documentation files (the
5# "Software"), to deal in the Software without restriction, including
6# without limitation the rights to use, copy, modify, merge, publish,
7# distribute, sub license, and/or sell copies of the Software, and to
8# permit persons to whom the Software is furnished to do so, subject to
9# the following conditions:
10#
11# The above copyright notice and this permission notice (including the
12# next paragraph) shall be included in all copies or substantial portions
13# of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
18# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
19# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23import argparse
24import json
25import re
26import xml.etree.ElementTree as et
27
28
29def get_xml_patch_version(xml_file):
30    xml = et.parse(xml_file)
31    for d in xml.findall('.types/type'):
32        if d.get('category', None) != 'define':
33            continue
34
35        name = d.find('.name')
36        if name.text != 'VK_HEADER_VERSION':
37            continue
38
39        return name.tail.strip()
40    assert False, f"Failed to find VK_HEADER_VERSION in {xml_file}"
41
42
43if __name__ == '__main__':
44    parser = argparse.ArgumentParser()
45    parser.add_argument('--api-version', required=True,
46                        help='Vulkan API version.')
47    parser.add_argument('--xml', required=False,
48                        help='Vulkan registry XML for patch version')
49    parser.add_argument('--lib-path', required=True,
50                        help='Path to installed library')
51    parser.add_argument('--out', required=False,
52                        help='Output json file.')
53    parser.add_argument('--use-backslash', action='store_true',
54                        help='Use backslash (Windows).')
55    args = parser.parse_args()
56
57    version = args.api_version
58    if args.xml:
59        re.match(r'\d+\.\d+', version)
60        version = version + '.' + get_xml_patch_version(args.xml)
61    else:
62        re.match(r'\d+\.\d+\.\d+', version)
63
64    lib_path = args.lib_path
65    if args.use_backslash:
66        lib_path = lib_path.replace('/', '\\')
67
68    json_data = {
69        'file_format_version': '1.0.0',
70        'ICD': {
71            'library_path': lib_path,
72            'api_version': version,
73        },
74    }
75
76    json_params = {
77        'indent': 4,
78        'sort_keys': True,
79        'separators': (',', ': '),
80    }
81
82    if args.out:
83        with open(args.out, 'w', encoding='utf-8') as f:
84            json.dump(json_data, f, **json_params)
85    else:
86        print(json.dumps(json_data, **json_params))
87