|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2021 Raspberry Pi (Trading) Ltd. |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: BSD-3-Clause |
| 6 | +# |
| 7 | +# |
| 8 | +# Script to scan the Raspberry Pi Pico SDK tree searching for CMake build defines |
| 9 | +# Outputs a tab separated file of the configuration item: |
| 10 | +# name location description type default group |
| 11 | +# |
| 12 | +# Usage: |
| 13 | +# |
| 14 | +# tools/extract_build_defines.py <root of repo> [output file] |
| 15 | +# |
| 16 | +# If not specified, output file will be `pico_build_defines.tsv` |
| 17 | + |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import re |
| 22 | +import csv |
| 23 | +import logging |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | +logging.basicConfig(level=logging.INFO) |
| 27 | + |
| 28 | +scandir = sys.argv[1] |
| 29 | +outfile = sys.argv[2] if len(sys.argv) > 2 else 'pico_build_defines.tsv' |
| 30 | + |
| 31 | +BUILD_DEFINE_RE = re.compile(r'#\s+PICO_BUILD_DEFINE:\s+(\w+),\s+([^,]+)(?:,\s+(.*))?$') |
| 32 | + |
| 33 | +all_configs = {} |
| 34 | +all_attrs = set() |
| 35 | +all_descriptions = {} |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +def ValidateAttrs(config_attrs, file_path, linenum): |
| 40 | + _type = config_attrs.get('type') |
| 41 | + |
| 42 | + # Validate attrs |
| 43 | + if _type == 'int': |
| 44 | + _min = _max = _default = None |
| 45 | + if config_attrs.get('min', None) is not None: |
| 46 | + value = config_attrs['min'] |
| 47 | + m = re.match(r'^(\d+)e(\d+)$', value.lower()) |
| 48 | + if m: |
| 49 | + _min = int(m.group(1)) * 10**int(m.group(2)) |
| 50 | + else: |
| 51 | + _min = int(value, 0) |
| 52 | + if config_attrs.get('max', None) is not None: |
| 53 | + value = config_attrs['max'] |
| 54 | + m = re.match(r'^(\d+)e(\d+)$', value.lower()) |
| 55 | + if m: |
| 56 | + _max = int(m.group(1)) * 10**int(m.group(2)) |
| 57 | + else: |
| 58 | + _max = int(value, 0) |
| 59 | + if config_attrs.get('default', None) is not None: |
| 60 | + if '/' not in config_attrs['default']: |
| 61 | + try: |
| 62 | + value = config_attrs['default'] |
| 63 | + m = re.match(r'^(\d+)e(\d+)$', value.lower()) |
| 64 | + if m: |
| 65 | + _default = int(m.group(1)) * 10**int(m.group(2)) |
| 66 | + else: |
| 67 | + _default = int(value, 0) |
| 68 | + except ValueError: |
| 69 | + pass |
| 70 | + if _min is not None and _max is not None: |
| 71 | + if _min > _max: |
| 72 | + raise Exception('{} at {}:{} has min {} > max {}'.format(config_name, file_path, linenum, config_attrs['min'], config_attrs['max'])) |
| 73 | + if _min is not None and _default is not None: |
| 74 | + if _min > _default: |
| 75 | + raise Exception('{} at {}:{} has min {} > default {}'.format(config_name, file_path, linenum, config_attrs['min'], config_attrs['default'])) |
| 76 | + if _default is not None and _max is not None: |
| 77 | + if _default > _max: |
| 78 | + raise Exception('{} at {}:{} has default {} > max {}'.format(config_name, file_path, linenum, config_attrs['default'], config_attrs['max'])) |
| 79 | + elif _type == 'bool': |
| 80 | + assert 'min' not in config_attrs |
| 81 | + assert 'max' not in config_attrs |
| 82 | + _default = config_attrs.get('default', None) |
| 83 | + if _default is not None: |
| 84 | + if '/' not in _default: |
| 85 | + if (_default.lower() != '0') and (config_attrs['default'].lower() != '1') and ( _default not in all_configs): |
| 86 | + logger.info('{} at {}:{} has non-integer default value "{}"'.format(config_name, file_path, linenum, config_attrs['default'])) |
| 87 | + |
| 88 | + elif _type == 'string': |
| 89 | + assert 'min' not in config_attrs |
| 90 | + assert 'max' not in config_attrs |
| 91 | + _default = config_attrs.get('default', None) |
| 92 | + elif _type == 'list': |
| 93 | + assert 'min' not in config_attrs |
| 94 | + assert 'max' not in config_attrs |
| 95 | + _default = config_attrs.get('default', None) |
| 96 | + else: |
| 97 | + raise Exception("Found unknown PICO_BUILD_DEFINE type {} at {}:{}".format(_type, file_path, linenum)) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +# Scan all CMakeLists.txt and .cmake files in the specific path, recursively. |
| 103 | + |
| 104 | +for dirpath, dirnames, filenames in os.walk(scandir): |
| 105 | + for filename in filenames: |
| 106 | + file_ext = os.path.splitext(filename)[1] |
| 107 | + if filename == 'CMakeLists.txt' or file_ext == '.cmake': |
| 108 | + file_path = os.path.join(dirpath, filename) |
| 109 | + |
| 110 | + with open(file_path, encoding="ISO-8859-1") as fh: |
| 111 | + linenum = 0 |
| 112 | + for line in fh.readlines(): |
| 113 | + linenum += 1 |
| 114 | + line = line.strip() |
| 115 | + m = BUILD_DEFINE_RE.match(line) |
| 116 | + if m: |
| 117 | + config_name = m.group(1) |
| 118 | + config_description = m.group(2) |
| 119 | + _attrs = m.group(3) |
| 120 | + # allow commas to appear inside brackets by converting them to and from NULL chars |
| 121 | + _attrs = re.sub(r'(\(.+\))', lambda m: m.group(1).replace(',', '\0'), _attrs) |
| 122 | + |
| 123 | + if '=' in config_description: |
| 124 | + raise Exception("For {} at {}:{} the description was set to '{}' - has the description field been omitted?".format(config_name, file_path, linenum, config_description)) |
| 125 | + if config_description in all_descriptions: |
| 126 | + raise Exception("Found description {} at {}:{} but it was already used at {}:{}".format(config_description, file_path, linenum, os.path.join(scandir, all_descriptions[config_description]['filename']), all_descriptions[config_description]['line_number'])) |
| 127 | + else: |
| 128 | + all_descriptions[config_description] = {'config_name': config_name, 'filename': os.path.relpath(file_path, scandir), 'line_number': linenum} |
| 129 | + |
| 130 | + config_attrs = {} |
| 131 | + prev = None |
| 132 | + # Handle case where attr value contains a comma |
| 133 | + for item in _attrs.split(','): |
| 134 | + if "=" not in item: |
| 135 | + assert(prev) |
| 136 | + item = prev + "," + item |
| 137 | + try: |
| 138 | + k, v = (i.strip() for i in item.split('=')) |
| 139 | + except ValueError: |
| 140 | + raise Exception('{} at {}:{} has malformed value {}'.format(config_name, file_path, linenum, item)) |
| 141 | + config_attrs[k] = v.replace('\0', ',') |
| 142 | + all_attrs.add(k) |
| 143 | + prev = item |
| 144 | + #print(file_path, config_name, config_attrs) |
| 145 | + |
| 146 | + if 'group' not in config_attrs: |
| 147 | + raise Exception('{} at {}:{} has no group attribute'.format(config_name, file_path, linenum)) |
| 148 | + |
| 149 | + #print(file_path, config_name, config_attrs) |
| 150 | + if config_name in all_configs: |
| 151 | + raise Exception("Found {} at {}:{} but it was already declared at {}:{}".format(config_name, file_path, linenum, os.path.join(scandir, all_configs[config_name]['filename']), all_configs[config_name]['line_number'])) |
| 152 | + else: |
| 153 | + all_configs[config_name] = {'attrs': config_attrs, 'filename': os.path.relpath(file_path, scandir), 'line_number': linenum, 'description': config_description} |
| 154 | + |
| 155 | + |
| 156 | +for config_name, config_obj in all_configs.items(): |
| 157 | + file_path = os.path.join(scandir, config_obj['filename']) |
| 158 | + linenum = config_obj['line_number'] |
| 159 | + |
| 160 | + ValidateAttrs(config_obj['attrs'], file_path, linenum) |
| 161 | + |
| 162 | +with open(outfile, 'w', newline='') as csvfile: |
| 163 | + fieldnames = ('name', 'location', 'description', 'type') + tuple(sorted(all_attrs - set(['type']))) |
| 164 | + writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore', dialect='excel-tab') |
| 165 | + |
| 166 | + writer.writeheader() |
| 167 | + for config_name, config_obj in sorted(all_configs.items()): |
| 168 | + writer.writerow({'name': config_name, 'location': '/{}:{}'.format(config_obj['filename'], config_obj['line_number']), 'description': config_obj['description'], **config_obj['attrs']}) |
0 commit comments