3 # Copyright (C) 2016 The ANGLE Project Authors.
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 # https://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 """Generate copies of the Vulkan layers JSON files, with no paths, forcing
18 Vulkan to use the default search path to look for layers."""
20 from __future__ import print_function
30 def glob_slash(dirname):
31 """Like regular glob but replaces \ with / in returned paths."""
32 return [s.replace('\\', '/') for s in glob.glob(dirname)]
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()
44 source_dir = args.source_dir
45 target_dir = args.target_dir
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')]
50 data_key = 'ICD' if args.icd else 'layer'
52 if not os.path.isdir(source_dir):
53 print(source_dir + ' is not a directory.', file=sys.stderr)
56 if not os.path.exists(target_dir):
57 os.makedirs(target_dir)
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)
65 for json_fname in json_files:
66 if not json_fname.endswith('.json'):
68 with open(json_fname) as infile:
69 data = json.load(infile)
72 if not data_key in data:
74 "Could not find '%s' key in %s" % (data_key, json_fname))
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
81 target_fname = os.path.join(target_dir, os.path.basename(json_fname))
82 with open(target_fname, 'w') as outfile:
83 json.dump(data, outfile)
85 # Get the Vulkan version from the vulkan_core.h file
86 vk_header_filename = args.version_header
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]
94 print('failed to extract vk_version', file=sys.stderr)
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'
104 # For each *.json.in template files in source dir generate actual json file
106 if (set(glob_slash(os.path.join(source_dir, '*.json.in'))) !=
108 print('.json.in list in gn file is out-of-date', file=sys.stderr)
110 for json_in_name in json_in_files:
111 if not json_in_name.endswith('.json.in'):
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:
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)
125 if __name__ == '__main__':