Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # VK |
| 4 | # |
| 5 | # Copyright (C) 2015 LunarG, Inc. |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | # copy of this software and associated documentation files (the "Software"), |
| 9 | # to deal in the Software without restriction, including without limitation |
| 10 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | # and/or sell copies of the Software, and to permit persons to whom the |
| 12 | # Software is furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included |
| 15 | # in all copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | # DEALINGS IN THE SOFTWARE. |
| 24 | |
| 25 | import argparse |
| 26 | import os |
| 27 | import sys |
| 28 | import vulkan |
| 29 | import platform |
| 30 | |
| 31 | # vk_layer_documentation_generate.py overview |
| 32 | # This script is intended to generate documentation based on vulkan layers |
| 33 | # It parses known validation layer headers for details of the validation checks |
| 34 | # It parses validation layer source files for specific code where checks are implemented |
| 35 | # structs in a human-readable txt format, as well as utility functions |
| 36 | # to print enum values as strings |
| 37 | |
| 38 | # NOTE : Initially the script is performing validation of a hand-written document |
| 39 | # Right now it does 3 checks: |
| 40 | # 1. Verify ENUM codes declared in source are documented |
| 41 | # 2. Verify ENUM codes in document are declared in source |
| 42 | # 3. Verify API function names in document are in the actual API header (vulkan.py) |
| 43 | # Currently script will flag errors in all of these cases |
| 44 | |
| 45 | # TODO : Need a formal specification of the syntax for doc generation |
| 46 | # Initially, these are the basics: |
| 47 | # 1. Validation checks have unique ENUM values defined in validation layer header |
| 48 | # 2. ENUM includes comments for 1-line overview of check and more detailed description |
| 49 | # 3. Actual code implementing checks includes ENUM value in callback |
| 50 | # 4. Code to test checks should include reference to ENUM |
| 51 | |
| 52 | |
| 53 | # TODO : Need list of known validation layers to use as default input |
| 54 | # Just a couple of flat lists right now, but may need to make this input file |
| 55 | # or at least a more dynamic data structure |
| 56 | layer_inputs = { 'draw_state' : {'header' : 'layers/draw_state.h', |
| 57 | 'source' : 'layers/draw_state.cpp', |
| 58 | 'generated' : False, |
| 59 | 'error_enum' : 'DRAW_STATE_ERROR'}, |
| 60 | 'mem_tracker' : {'header' : 'layers/mem_tracker.h', |
| 61 | 'source' : 'layers/mem_tracker.cpp', |
| 62 | 'generated' : False, |
| 63 | 'error_enum' : 'MEM_TRACK_ERROR'}, |
| 64 | 'shader_checker' : {'header' : 'layers/shader_checker.h', |
| 65 | 'source' : 'layers/shader_checker.cpp', |
| 66 | 'generated' : False, |
| 67 | 'error_enum' : 'SHADER_CHECKER_ERROR'}, |
| 68 | 'threading' : {'header' : 'layers/threading.h', |
| 69 | 'source' : 'dbuild/layers/threading.cpp', |
| 70 | 'generated' : True, |
| 71 | 'error_enum' : 'THREADING_CHECKER_ERROR'}, |
| 72 | 'object_track' : {'header' : 'layers/object_track.h', |
| 73 | 'source' : 'dbuild/layers/object_track.cpp', |
| 74 | 'generated' : True, |
| 75 | 'error_enum' : 'OBJECT_TRACK_ERROR',}, |
Tobin Ehlis | b5fc4fb | 2015-09-03 09:50:06 -0600 | [diff] [blame] | 76 | 'device_limits' : {'header' : 'layers/device_limits.h', |
Tobin Ehlis | ad8c446 | 2015-09-21 15:20:28 -0600 | [diff] [blame] | 77 | 'source' : 'layers/device_limits.cpp', |
| 78 | 'generated' : False, |
| 79 | 'error_enum' : 'DEV_LIMITS_ERROR',}, |
| 80 | 'image' : {'header' : 'layers/image.h', |
| 81 | 'source' : 'layers/image.cpp', |
| 82 | 'generated' : False, |
| 83 | 'error_enum' : 'IMAGE_ERROR',}, |
Ian Elliott | b0f474c | 2015-09-25 15:50:55 -0600 | [diff] [blame] | 84 | 'swapchain' : {'header' : 'layers/swapchain.h', |
| 85 | 'source' : 'layers/swapchain.cpp', |
| 86 | 'generated' : False, |
| 87 | 'error_enum' : 'SWAPCHAIN_ERROR',}, |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | builtin_headers = [layer_inputs[ln]['header'] for ln in layer_inputs] |
| 91 | builtin_source = [layer_inputs[ln]['source'] for ln in layer_inputs] |
| 92 | |
| 93 | # List of extensions in layers that are included in documentation, but not in vulkan.py API set |
| 94 | layer_extension_functions = ['objTrackGetObjects', 'objTrackGetObjectsOfType'] |
| 95 | |
| 96 | def handle_args(): |
| 97 | parser = argparse.ArgumentParser(description='Generate layer documenation from source.') |
| 98 | parser.add_argument('--in_headers', required=False, default=builtin_headers, help='The input layer header files from which code will be generated.') |
| 99 | parser.add_argument('--in_source', required=False, default=builtin_source, help='The input layer source files from which code will be generated.') |
| 100 | parser.add_argument('--layer_doc', required=False, default='layers/vk_validation_layer_details.md', help='Existing layer document to be validated against actual layers.') |
| 101 | parser.add_argument('--validate', action='store_true', default=False, help='Validate that there are no mismatches between layer documentation and source. This includes cross-checking the validation checks, and making sure documented Vulkan API calls exist.') |
| 102 | parser.add_argument('--print_structs', action='store_true', default=False, help='Primarily a debug option that prints out internal data structs used to generate layer docs.') |
| 103 | parser.add_argument('--print_doc_checks', action='store_true', default=False, help='Primarily a debug option that prints out all of the checks that are documented.') |
| 104 | return parser.parse_args() |
| 105 | |
| 106 | # Little helper class for coloring cmd line output |
| 107 | class bcolors: |
| 108 | |
| 109 | def __init__(self): |
| 110 | self.GREEN = '\033[0;32m' |
| 111 | self.RED = '\033[0;31m' |
| 112 | self.ENDC = '\033[0m' |
| 113 | if 'Linux' != platform.system(): |
| 114 | self.GREEN = '' |
| 115 | self.RED = '' |
| 116 | self.ENDC = '' |
| 117 | |
| 118 | def green(self): |
| 119 | return self.GREEN |
| 120 | |
| 121 | def red(self): |
| 122 | return self.RED |
| 123 | |
| 124 | def endc(self): |
| 125 | return self.ENDC |
| 126 | |
| 127 | # Class to parse the layer source code and store details in internal data structs |
| 128 | class LayerParser: |
| 129 | def __init__(self, header_file_list, source_file_list): |
| 130 | self.header_files = header_file_list |
| 131 | self.source_files = source_file_list |
| 132 | self.layer_dict = {} |
| 133 | self.api_dict = {} |
| 134 | |
| 135 | # Parse layer header files into internal dict data structs |
| 136 | def parse(self): |
| 137 | # For each header file, parse details into dicts |
| 138 | # TODO : Should have a global dict element to track overall list of checks |
| 139 | store_enum = False |
| 140 | for hf in self.header_files: |
| 141 | layer_name = os.path.basename(hf).split('.')[0] |
| 142 | self.layer_dict[layer_name] = {} # initialize a new dict for this layer |
| 143 | self.layer_dict[layer_name]['CHECKS'] = [] # enum of checks is stored in a list |
| 144 | #print('Parsing header file %s as layer name %s' % (hf, layer_name)) |
| 145 | with open(hf) as f: |
| 146 | for line in f: |
| 147 | if True in [line.strip().startswith(comment) for comment in ['//', '/*']]: |
| 148 | #print("Skipping comment line: %s" % line) |
| 149 | # For now skipping lines starting w/ comment, may use these to capture |
| 150 | # documentation in the future |
| 151 | continue |
| 152 | |
| 153 | # Find enums |
| 154 | if store_enum: |
| 155 | if '}' in line: # we're done with enum definition |
| 156 | store_enum = False |
| 157 | continue |
| 158 | # grab the enum name as a unique check |
| 159 | if ',' in line: |
| 160 | # TODO : When documentation for a check is contained in the source, |
| 161 | # this is where we should also capture that documentation so that |
| 162 | # it can then be transformed into desired doc format |
| 163 | enum_name = line.split(',')[0].strip() |
| 164 | # Flag an error if we have already seen this enum |
| 165 | if enum_name in self.layer_dict[layer_name]['CHECKS']: |
| 166 | print('ERROR : % layer has duplicate error enum: %s' % (layer_name, enum_name)) |
| 167 | self.layer_dict[layer_name]['CHECKS'].append(enum_name) |
| 168 | # If the line includes 'typedef', 'enum', and the expected enum name, start capturing enums |
| 169 | if False not in [ex in line for ex in ['typedef', 'enum', layer_inputs[layer_name]['error_enum']]]: |
| 170 | store_enum = True |
| 171 | |
| 172 | # For each source file, parse into dicts |
| 173 | for sf in self.source_files: |
| 174 | #print('Parsing source file %s' % sf) |
| 175 | pass |
| 176 | # TODO : In the source file we want to see where checks actually occur |
| 177 | # Need to build function tree of checks so that we know all of the |
| 178 | # checks that occur under a top-level Vulkan API call |
| 179 | # Eventually in the validation we can flag ENUMs that aren't being |
| 180 | # used in the source, and we can document source code lines as well |
| 181 | # as Vulkan API calls where each specific ENUM check is made |
| 182 | |
| 183 | def print_structs(self): |
| 184 | print('This is where I print the data structs') |
| 185 | for layer in self.layer_dict: |
| 186 | print('Layer %s has %i checks:\n%s' % (layer, len(self.layer_dict[layer]['CHECKS'])-1, "\n\t".join(self.layer_dict[layer]['CHECKS']))) |
| 187 | |
| 188 | # Class to parse hand-written md layer documentation into a dict and then validate its contents |
| 189 | class LayerDoc: |
| 190 | def __init__(self, source_file): |
| 191 | self.layer_doc_filename = source_file |
| 192 | self.txt_color = bcolors() |
| 193 | # Main data struct to store info from layer doc |
| 194 | self.layer_doc_dict = {} |
| 195 | # Comprehensive list of all validation checks recorded in doc |
| 196 | self.enum_list = [] |
| 197 | |
| 198 | # Parse the contents of doc into data struct |
| 199 | def parse(self): |
| 200 | layer_name = 'INIT' |
| 201 | parse_layer_details = False |
| 202 | detail_trigger = '| Check | ' |
| 203 | parse_pending_work = False |
| 204 | pending_trigger = ' Pending Work' |
| 205 | parse_overview = False |
| 206 | overview_trigger = ' Overview' |
| 207 | enum_prefix = '' |
| 208 | |
| 209 | with open(self.layer_doc_filename) as f: |
| 210 | for line in f: |
| 211 | if parse_pending_work: |
| 212 | if '.' in line and line.strip()[0].isdigit(): |
| 213 | todo_item = line.split('.')[1].strip() |
| 214 | self.layer_doc_dict[layer_name]['pending'].append(todo_item) |
| 215 | if pending_trigger in line and '##' in line: |
| 216 | parse_layer_details = False |
| 217 | parse_pending_work = True |
| 218 | parse_overview = False |
| 219 | self.layer_doc_dict[layer_name]['pending'] = [] |
| 220 | if parse_layer_details: |
| 221 | # Grab details but skip the fomat line with a bunch of '-' chars |
| 222 | if '|' in line and line.count('-') < 20: |
| 223 | detail_sections = line.split('|') |
| 224 | #print("Details elements from line %s: %s" % (line, detail_sections)) |
| 225 | check_name = '%s%s' % (enum_prefix, detail_sections[3].strip()) |
| 226 | if '_NA' in check_name: |
| 227 | # TODO : Should clean up these NA checks in the doc, skipping them for now |
| 228 | continue |
| 229 | self.enum_list.append(check_name) |
| 230 | self.layer_doc_dict[layer_name][check_name] = {} |
| 231 | self.layer_doc_dict[layer_name][check_name]['summary_txt'] = detail_sections[1].strip() |
| 232 | self.layer_doc_dict[layer_name][check_name]['details_txt'] = detail_sections[2].strip() |
| 233 | self.layer_doc_dict[layer_name][check_name]['api_list'] = detail_sections[4].split() |
| 234 | self.layer_doc_dict[layer_name][check_name]['tests'] = detail_sections[5].split() |
| 235 | self.layer_doc_dict[layer_name][check_name]['notes'] = detail_sections[6].strip() |
| 236 | # strip any unwanted commas from api and test names |
| 237 | self.layer_doc_dict[layer_name][check_name]['api_list'] = [a.strip(',') for a in self.layer_doc_dict[layer_name][check_name]['api_list']] |
| 238 | self.layer_doc_dict[layer_name][check_name]['tests'] = [a.strip(',') for a in self.layer_doc_dict[layer_name][check_name]['tests']] |
| 239 | # Trigger details parsing when we have table header |
| 240 | if detail_trigger in line: |
| 241 | parse_layer_details = True |
| 242 | parse_pending_work = False |
| 243 | parse_overview = False |
| 244 | enum_txt = line.split('|')[3] |
| 245 | if '*' in enum_txt: |
| 246 | enum_prefix = enum_txt.split()[-1].strip('*').strip() |
| 247 | #print('prefix: %s' % enum_prefix) |
| 248 | if parse_overview: |
| 249 | self.layer_doc_dict[layer_name]['overview'] += line |
| 250 | if overview_trigger in line and '##' in line: |
| 251 | parse_layer_details = False |
| 252 | parse_pending_work = False |
| 253 | parse_overview = True |
| 254 | layer_name = line.split()[1] |
| 255 | self.layer_doc_dict[layer_name] = {} |
| 256 | self.layer_doc_dict[layer_name]['overview'] = '' |
| 257 | |
| 258 | # Verify that checks and api references in layer doc match reality |
| 259 | # Report API calls from doc that are not found in API |
| 260 | # Report checks from doc that are not in actual layers |
| 261 | # Report checks from layers that are not captured in doc |
| 262 | def validate(self, layer_dict): |
| 263 | # Count number of errors found and return it |
| 264 | errors_found = 0 |
| 265 | # First we'll go through the doc datastructures and flag any issues |
| 266 | for chk in self.enum_list: |
| 267 | doc_layer_found = False |
| 268 | for real_layer in layer_dict: |
| 269 | if chk in layer_dict[real_layer]['CHECKS']: |
| 270 | #print('Found actual layer check %s in doc' % (chk)) |
| 271 | doc_layer_found = True |
| 272 | continue |
| 273 | if not doc_layer_found: |
| 274 | print(self.txt_color.red() + 'Actual layers do not contain documented check: %s' % (chk) + self.txt_color.endc()) |
| 275 | errors_found += 1 |
| 276 | # Now go through API names in doc and verify they're real |
| 277 | # First we're going to transform proto names from vulkan.py into single list |
| 278 | core_api_names = [p.name for p in vulkan.core.protos] |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 279 | wsi_s_names = [p.name for p in vulkan.ext_khr_swapchain.protos] |
| 280 | wsi_ds_names = [p.name for p in vulkan.ext_khr_device_swapchain.protos] |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 281 | dbg_rpt_names = [p.name for p in vulkan.debug_report_lunarg.protos] |
| 282 | dbg_mrk_names = [p.name for p in vulkan.debug_marker_lunarg.protos] |
| 283 | api_names = core_api_names + wsi_s_names + wsi_ds_names + dbg_rpt_names + dbg_mrk_names |
| 284 | for ln in self.layer_doc_dict: |
| 285 | for chk in self.layer_doc_dict[ln]: |
| 286 | if chk in ['overview', 'pending']: |
| 287 | continue |
| 288 | for api in self.layer_doc_dict[ln][chk]['api_list']: |
| 289 | if api[2:] not in api_names and api not in layer_extension_functions: |
| 290 | print(self.txt_color.red() + 'Doc references invalid function: %s' % (api) + self.txt_color.endc()) |
| 291 | errors_found += 1 |
| 292 | # Now go through all of the actual checks in the layers and make sure they're covered in the doc |
| 293 | for ln in layer_dict: |
| 294 | for chk in layer_dict[ln]['CHECKS']: |
| 295 | if chk not in self.enum_list: |
| 296 | print(self.txt_color.red() + 'Doc is missing check: %s' % (chk) + self.txt_color.endc()) |
| 297 | errors_found += 1 |
| 298 | |
| 299 | return errors_found |
| 300 | |
| 301 | # Print all of the checks captured in the doc |
| 302 | def print_checks(self): |
| 303 | print('Checks captured in doc:\n%s' % ('\n\t'.join(self.enum_list))) |
| 304 | |
| 305 | def main(argv=None): |
| 306 | # Parse args |
| 307 | opts = handle_args() |
| 308 | # Create parser for layer files |
| 309 | layer_parser = LayerParser(opts.in_headers, opts.in_source) |
| 310 | # Parse files into internal data structs |
| 311 | layer_parser.parse() |
| 312 | |
| 313 | # Generate requested types of output |
| 314 | if opts.print_structs: # Print details of internal data structs |
| 315 | layer_parser.print_structs() |
| 316 | |
| 317 | layer_doc = LayerDoc(opts.layer_doc) |
| 318 | layer_doc.parse() |
| 319 | if opts.print_doc_checks: |
| 320 | layer_doc.print_checks() |
| 321 | |
| 322 | if opts.validate: |
| 323 | num_errors = layer_doc.validate(layer_parser.layer_dict) |
| 324 | if (0 == num_errors): |
| 325 | txt_color = bcolors() |
| 326 | print(txt_color.green() + 'No mismatches found between %s and implementation' % (os.path.basename(opts.layer_doc)) + txt_color.endc()) |
| 327 | else: |
| 328 | return num_errors |
| 329 | return 0 |
| 330 | |
| 331 | if __name__ == "__main__": |
| 332 | sys.exit(main()) |
| 333 | |