• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright (C) 2016 The ANGLE Project Authors.
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#     https://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.
16
17"""Generate copies of the Vulkan layers JSON files, with no paths, forcing
18Vulkan to use the default search path to look for layers."""
19
20from __future__ import print_function
21
22import argparse
23import glob
24import json
25import os
26import platform
27import sys
28
29
30def glob_slash(dirname):
31    """Like regular glob but replaces \ with / in returned paths."""
32    return [s.replace('\\', '/') for s in glob.glob(dirname)]
33
34
35def main():
36    parser = argparse.ArgumentParser(description=__doc__)
37    parser.add_argument('--icd', action='store_true')
38    parser.add_argument('source_dir')
39    parser.add_argument('target_dir')
40    parser.add_argument('version_header', help='path to vulkan_core.h')
41    parser.add_argument('json_files', nargs='*')
42    args = parser.parse_args()
43
44    source_dir = args.source_dir
45    target_dir = args.target_dir
46
47    json_files = [j for j in args.json_files if j.endswith('.json')]
48    json_in_files = [j for j in args.json_files if j.endswith('.json.in')]
49
50    data_key = 'ICD' if args.icd else 'layer'
51
52    if not os.path.isdir(source_dir):
53        print(source_dir + ' is not a directory.', file=sys.stderr)
54        return 1
55
56    if not os.path.exists(target_dir):
57        os.makedirs(target_dir)
58
59    # Copy the *.json files from source dir to target dir
60    if (set(glob_slash(os.path.join(source_dir, '*.json'))) != set(json_files)):
61        print(glob.glob(os.path.join(source_dir, '*.json')))
62        print('.json list in gn file is out-of-date', file=sys.stderr)
63        return 1
64
65    for json_fname in json_files:
66        if not json_fname.endswith('.json'):
67            continue
68        with open(json_fname) as infile:
69            data = json.load(infile)
70
71        # Update the path.
72        if not data_key in data:
73            raise Exception(
74                "Could not find '%s' key in %s" % (data_key, json_fname))
75
76        # The standard validation layer has no library path.
77        if 'library_path' in data[data_key]:
78            prev_name = os.path.basename(data[data_key]['library_path'])
79            data[data_key]['library_path'] = prev_name
80
81        target_fname = os.path.join(target_dir, os.path.basename(json_fname))
82        with open(target_fname, 'wb') as outfile:
83            json.dump(data, outfile)
84
85    # Get the Vulkan version from the vulkan_core.h file
86    vk_header_filename = args.version_header
87    vk_version = None
88    with open(vk_header_filename) as vk_header_file:
89        for line in vk_header_file:
90            if line.startswith('#define VK_HEADER_VERSION'):
91                vk_version = line.split()[-1]
92                break
93    if not vk_version:
94        print('failed to extract vk_version', file=sys.stderr)
95        return 1
96
97    # Set json file prefix and suffix for generating files, default to Linux.
98    relative_path_prefix = '../lib'
99    file_type_suffix = '.so'
100    if platform.system() == 'Windows':
101        relative_path_prefix = r'..\\'  # json-escaped, hence two backslashes.
102        file_type_suffix = '.dll'
103
104    # For each *.json.in template files in source dir generate actual json file
105    # in target dir
106    if (set(glob_slash(os.path.join(source_dir, '*.json.in'))) !=
107            set(json_in_files)):
108        print('.json.in list in gn file is out-of-date', file=sys.stderr)
109        return 1
110    for json_in_name in json_in_files:
111        if not json_in_name.endswith('.json.in'):
112            continue
113        json_in_fname = os.path.basename(json_in_name)
114        layer_name = json_in_fname[:-len('.json.in')]
115        layer_lib_name = layer_name + file_type_suffix
116        json_out_fname = os.path.join(target_dir, json_in_fname[:-len('.in')])
117        with open(json_out_fname,'w') as json_out_file, \
118             open(json_in_name) as infile:
119            for line in infile:
120                line = line.replace('@RELATIVE_LAYER_BINARY@',
121                                    relative_path_prefix + layer_lib_name)
122                line = line.replace('@VK_VERSION@', '1.1.' + vk_version)
123                json_out_file.write(line)
124
125if __name__ == '__main__':
126    sys.exit(main())
127