Valentin Rothberg | 4b6fda0 | 2015-05-13 10:40:52 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 2 | |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 3 | """Find Kconfig symbols that are referenced but not defined.""" |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 4 | |
Valentin Rothberg | c745566 | 2015-06-01 16:00:20 +0200 | [diff] [blame] | 5 | # (c) 2014-2015 Valentin Rothberg <valentinrothberg@gmail.com> |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 6 | # (c) 2014 Stefan Hengelein <stefan.hengelein@fau.de> |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 7 | # |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 8 | # Licensed under the terms of the GNU GPL License version 2 |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 9 | |
| 10 | |
| 11 | import os |
| 12 | import re |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 13 | import signal |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 14 | import sys |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 15 | from multiprocessing import Pool, cpu_count |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 16 | from optparse import OptionParser |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 17 | from subprocess import Popen, PIPE, STDOUT |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 18 | |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 19 | |
| 20 | # regex expressions |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 21 | OPERATORS = r"&|\(|\)|\||\!" |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 22 | FEATURE = r"(?:\w*[A-Z0-9]\w*){2,}" |
| 23 | DEF = r"^\s*(?:menu){,1}config\s+(" + FEATURE + r")\s*" |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 24 | EXPR = r"(?:" + OPERATORS + r"|\s|" + FEATURE + r")+" |
Valentin Rothberg | 0bd38ae | 2015-07-27 12:33:05 +0200 | [diff] [blame] | 25 | DEFAULT = r"default\s+.*?(?:if\s.+){,1}" |
| 26 | STMT = r"^\s*(?:if|select|depends\s+on|(?:" + DEFAULT + r"))\s+" + EXPR |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 27 | SOURCE_FEATURE = r"(?:\W|\b)+[D]{,1}CONFIG_(" + FEATURE + r")" |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 28 | |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 29 | # regex objects |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 30 | REGEX_FILE_KCONFIG = re.compile(r".*Kconfig[\.\w+\-]*$") |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 31 | REGEX_FEATURE = re.compile(r'(?!\B)' + FEATURE + r'(?!\B)') |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 32 | REGEX_SOURCE_FEATURE = re.compile(SOURCE_FEATURE) |
| 33 | REGEX_KCONFIG_DEF = re.compile(DEF) |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 34 | REGEX_KCONFIG_EXPR = re.compile(EXPR) |
| 35 | REGEX_KCONFIG_STMT = re.compile(STMT) |
| 36 | REGEX_KCONFIG_HELP = re.compile(r"^\s+(help|---help---)\s*$") |
| 37 | REGEX_FILTER_FEATURES = re.compile(r"[A-Za-z0-9]$") |
Valentin Rothberg | 0bd38ae | 2015-07-27 12:33:05 +0200 | [diff] [blame] | 38 | REGEX_NUMERIC = re.compile(r"0[xX][0-9a-fA-F]+|[0-9]+") |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 39 | REGEX_QUOTES = re.compile("(\"(.*?)\")") |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 40 | |
| 41 | |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 42 | def parse_options(): |
| 43 | """The user interface of this module.""" |
| 44 | usage = "%prog [options]\n\n" \ |
| 45 | "Run this tool to detect Kconfig symbols that are referenced but " \ |
| 46 | "not defined in\nKconfig. The output of this tool has the " \ |
| 47 | "format \'Undefined symbol\\tFile list\'\n\n" \ |
| 48 | "If no option is specified, %prog will default to check your\n" \ |
| 49 | "current tree. Please note that specifying commits will " \ |
| 50 | "\'git reset --hard\'\nyour current tree! You may save " \ |
| 51 | "uncommitted changes to avoid losing data." |
| 52 | |
| 53 | parser = OptionParser(usage=usage) |
| 54 | |
| 55 | parser.add_option('-c', '--commit', dest='commit', action='store', |
| 56 | default="", |
| 57 | help="Check if the specified commit (hash) introduces " |
| 58 | "undefined Kconfig symbols.") |
| 59 | |
| 60 | parser.add_option('-d', '--diff', dest='diff', action='store', |
| 61 | default="", |
| 62 | help="Diff undefined symbols between two commits. The " |
| 63 | "input format bases on Git log's " |
| 64 | "\'commmit1..commit2\'.") |
| 65 | |
Valentin Rothberg | a42fa92 | 2015-06-01 16:00:19 +0200 | [diff] [blame] | 66 | parser.add_option('-f', '--find', dest='find', action='store_true', |
| 67 | default=False, |
| 68 | help="Find and show commits that may cause symbols to be " |
| 69 | "missing. Required to run with --diff.") |
| 70 | |
Valentin Rothberg | cf132e4 | 2015-04-29 16:58:27 +0200 | [diff] [blame] | 71 | parser.add_option('-i', '--ignore', dest='ignore', action='store', |
| 72 | default="", |
| 73 | help="Ignore files matching this pattern. Note that " |
| 74 | "the pattern needs to be a Python regex. To " |
| 75 | "ignore defconfigs, specify -i '.*defconfig'.") |
| 76 | |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 77 | parser.add_option('', '--force', dest='force', action='store_true', |
| 78 | default=False, |
| 79 | help="Reset current Git tree even when it's dirty.") |
| 80 | |
| 81 | (opts, _) = parser.parse_args() |
| 82 | |
| 83 | if opts.commit and opts.diff: |
| 84 | sys.exit("Please specify only one option at once.") |
| 85 | |
| 86 | if opts.diff and not re.match(r"^[\w\-\.]+\.\.[\w\-\.]+$", opts.diff): |
| 87 | sys.exit("Please specify valid input in the following format: " |
| 88 | "\'commmit1..commit2\'") |
| 89 | |
| 90 | if opts.commit or opts.diff: |
| 91 | if not opts.force and tree_is_dirty(): |
| 92 | sys.exit("The current Git tree is dirty (see 'git status'). " |
| 93 | "Running this script may\ndelete important data since it " |
| 94 | "calls 'git reset --hard' for some performance\nreasons. " |
| 95 | " Please run this script in a clean Git tree or pass " |
| 96 | "'--force' if you\nwant to ignore this warning and " |
| 97 | "continue.") |
| 98 | |
Valentin Rothberg | a42fa92 | 2015-06-01 16:00:19 +0200 | [diff] [blame] | 99 | if opts.commit: |
| 100 | opts.find = False |
| 101 | |
Valentin Rothberg | cf132e4 | 2015-04-29 16:58:27 +0200 | [diff] [blame] | 102 | if opts.ignore: |
| 103 | try: |
| 104 | re.match(opts.ignore, "this/is/just/a/test.c") |
| 105 | except: |
| 106 | sys.exit("Please specify a valid Python regex.") |
| 107 | |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 108 | return opts |
| 109 | |
| 110 | |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 111 | def main(): |
| 112 | """Main function of this module.""" |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 113 | opts = parse_options() |
| 114 | |
| 115 | if opts.commit or opts.diff: |
| 116 | head = get_head() |
| 117 | |
| 118 | # get commit range |
| 119 | commit_a = None |
| 120 | commit_b = None |
| 121 | if opts.commit: |
| 122 | commit_a = opts.commit + "~" |
| 123 | commit_b = opts.commit |
| 124 | elif opts.diff: |
| 125 | split = opts.diff.split("..") |
| 126 | commit_a = split[0] |
| 127 | commit_b = split[1] |
| 128 | undefined_a = {} |
| 129 | undefined_b = {} |
| 130 | |
| 131 | # get undefined items before the commit |
| 132 | execute("git reset --hard %s" % commit_a) |
Valentin Rothberg | cf132e4 | 2015-04-29 16:58:27 +0200 | [diff] [blame] | 133 | undefined_a = check_symbols(opts.ignore) |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 134 | |
| 135 | # get undefined items for the commit |
| 136 | execute("git reset --hard %s" % commit_b) |
Valentin Rothberg | cf132e4 | 2015-04-29 16:58:27 +0200 | [diff] [blame] | 137 | undefined_b = check_symbols(opts.ignore) |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 138 | |
| 139 | # report cases that are present for the commit but not before |
Valentin Rothberg | e9533ae | 2015-03-23 18:40:49 +0100 | [diff] [blame] | 140 | for feature in sorted(undefined_b): |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 141 | # feature has not been undefined before |
| 142 | if not feature in undefined_a: |
Valentin Rothberg | e9533ae | 2015-03-23 18:40:49 +0100 | [diff] [blame] | 143 | files = sorted(undefined_b.get(feature)) |
Valentin Rothberg | c745566 | 2015-06-01 16:00:20 +0200 | [diff] [blame] | 144 | print "%s\t%s" % (yel(feature), ", ".join(files)) |
Valentin Rothberg | a42fa92 | 2015-06-01 16:00:19 +0200 | [diff] [blame] | 145 | if opts.find: |
| 146 | commits = find_commits(feature, opts.diff) |
Valentin Rothberg | c745566 | 2015-06-01 16:00:20 +0200 | [diff] [blame] | 147 | print red(commits) |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 148 | # check if there are new files that reference the undefined feature |
| 149 | else: |
Valentin Rothberg | e9533ae | 2015-03-23 18:40:49 +0100 | [diff] [blame] | 150 | files = sorted(undefined_b.get(feature) - |
| 151 | undefined_a.get(feature)) |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 152 | if files: |
Valentin Rothberg | c745566 | 2015-06-01 16:00:20 +0200 | [diff] [blame] | 153 | print "%s\t%s" % (yel(feature), ", ".join(files)) |
Valentin Rothberg | a42fa92 | 2015-06-01 16:00:19 +0200 | [diff] [blame] | 154 | if opts.find: |
| 155 | commits = find_commits(feature, opts.diff) |
Valentin Rothberg | c745566 | 2015-06-01 16:00:20 +0200 | [diff] [blame] | 156 | print red(commits) |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 157 | |
| 158 | # reset to head |
| 159 | execute("git reset --hard %s" % head) |
| 160 | |
| 161 | # default to check the entire tree |
| 162 | else: |
Valentin Rothberg | cf132e4 | 2015-04-29 16:58:27 +0200 | [diff] [blame] | 163 | undefined = check_symbols(opts.ignore) |
Valentin Rothberg | e9533ae | 2015-03-23 18:40:49 +0100 | [diff] [blame] | 164 | for feature in sorted(undefined): |
| 165 | files = sorted(undefined.get(feature)) |
Valentin Rothberg | c745566 | 2015-06-01 16:00:20 +0200 | [diff] [blame] | 166 | print "%s\t%s" % (yel(feature), ", ".join(files)) |
| 167 | |
| 168 | |
| 169 | def yel(string): |
| 170 | """ |
| 171 | Color %string yellow. |
| 172 | """ |
| 173 | return "\033[33m%s\033[0m" % string |
| 174 | |
| 175 | |
| 176 | def red(string): |
| 177 | """ |
| 178 | Color %string red. |
| 179 | """ |
| 180 | return "\033[31m%s\033[0m" % string |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 181 | |
| 182 | |
| 183 | def execute(cmd): |
| 184 | """Execute %cmd and return stdout. Exit in case of error.""" |
| 185 | pop = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True) |
| 186 | (stdout, _) = pop.communicate() # wait until finished |
| 187 | if pop.returncode != 0: |
| 188 | sys.exit(stdout) |
| 189 | return stdout |
| 190 | |
| 191 | |
Valentin Rothberg | a42fa92 | 2015-06-01 16:00:19 +0200 | [diff] [blame] | 192 | def find_commits(symbol, diff): |
| 193 | """Find commits changing %symbol in the given range of %diff.""" |
| 194 | commits = execute("git log --pretty=oneline --abbrev-commit -G %s %s" |
| 195 | % (symbol, diff)) |
| 196 | return commits |
| 197 | |
| 198 | |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 199 | def tree_is_dirty(): |
| 200 | """Return true if the current working tree is dirty (i.e., if any file has |
| 201 | been added, deleted, modified, renamed or copied but not committed).""" |
| 202 | stdout = execute("git status --porcelain") |
| 203 | for line in stdout: |
| 204 | if re.findall(r"[URMADC]{1}", line[:2]): |
| 205 | return True |
| 206 | return False |
| 207 | |
| 208 | |
| 209 | def get_head(): |
| 210 | """Return commit hash of current HEAD.""" |
| 211 | stdout = execute("git rev-parse HEAD") |
| 212 | return stdout.strip('\n') |
| 213 | |
| 214 | |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 215 | def partition(lst, size): |
| 216 | """Partition list @lst into eveni-sized lists of size @size.""" |
| 217 | return [lst[i::size] for i in xrange(size)] |
| 218 | |
| 219 | |
| 220 | def init_worker(): |
| 221 | """Set signal handler to ignore SIGINT.""" |
| 222 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 223 | |
| 224 | |
Valentin Rothberg | cf132e4 | 2015-04-29 16:58:27 +0200 | [diff] [blame] | 225 | def check_symbols(ignore): |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 226 | """Find undefined Kconfig symbols and return a dict with the symbol as key |
Valentin Rothberg | cf132e4 | 2015-04-29 16:58:27 +0200 | [diff] [blame] | 227 | and a list of referencing files as value. Files matching %ignore are not |
| 228 | checked for undefined symbols.""" |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 229 | pool = Pool(cpu_count(), init_worker) |
| 230 | try: |
| 231 | return check_symbols_helper(pool, ignore) |
| 232 | except KeyboardInterrupt: |
| 233 | pool.terminate() |
| 234 | pool.join() |
| 235 | sys.exit(1) |
| 236 | |
| 237 | |
| 238 | def check_symbols_helper(pool, ignore): |
| 239 | """Helper method for check_symbols(). Used to catch keyboard interrupts in |
| 240 | check_symbols() in order to properly terminate running worker processes.""" |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 241 | source_files = [] |
| 242 | kconfig_files = [] |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 243 | defined_features = [] |
| 244 | referenced_features = dict() # {file: [features]} |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 245 | |
| 246 | # use 'git ls-files' to get the worklist |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 247 | stdout = execute("git ls-files") |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 248 | if len(stdout) > 0 and stdout[-1] == "\n": |
| 249 | stdout = stdout[:-1] |
| 250 | |
| 251 | for gitfile in stdout.rsplit("\n"): |
Valentin Rothberg | 208d511 | 2015-02-25 15:15:23 +0100 | [diff] [blame] | 252 | if ".git" in gitfile or "ChangeLog" in gitfile or \ |
| 253 | ".log" in gitfile or os.path.isdir(gitfile) or \ |
| 254 | gitfile.startswith("tools/"): |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 255 | continue |
| 256 | if REGEX_FILE_KCONFIG.match(gitfile): |
| 257 | kconfig_files.append(gitfile) |
| 258 | else: |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 259 | if ignore and not re.match(ignore, gitfile): |
| 260 | continue |
| 261 | # add source files that do not match the ignore pattern |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 262 | source_files.append(gitfile) |
| 263 | |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 264 | # parse source files |
| 265 | arglist = partition(source_files, cpu_count()) |
| 266 | for res in pool.map(parse_source_files, arglist): |
| 267 | referenced_features.update(res) |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 268 | |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 269 | |
| 270 | # parse kconfig files |
| 271 | arglist = [] |
| 272 | for part in partition(kconfig_files, cpu_count()): |
| 273 | arglist.append((part, ignore)) |
| 274 | for res in pool.map(parse_kconfig_files, arglist): |
| 275 | defined_features.extend(res[0]) |
| 276 | referenced_features.update(res[1]) |
| 277 | defined_features = set(defined_features) |
| 278 | |
| 279 | # inverse mapping of referenced_features to dict(feature: [files]) |
| 280 | inv_map = dict() |
| 281 | for _file, features in referenced_features.iteritems(): |
| 282 | for feature in features: |
| 283 | inv_map[feature] = inv_map.get(feature, set()) |
| 284 | inv_map[feature].add(_file) |
| 285 | referenced_features = inv_map |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 286 | |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 287 | undefined = {} # {feature: [files]} |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 288 | for feature in sorted(referenced_features): |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 289 | # filter some false positives |
| 290 | if feature == "FOO" or feature == "BAR" or \ |
| 291 | feature == "FOO_BAR" or feature == "XXX": |
| 292 | continue |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 293 | if feature not in defined_features: |
| 294 | if feature.endswith("_MODULE"): |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 295 | # avoid false positives for kernel modules |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 296 | if feature[:-len("_MODULE")] in defined_features: |
| 297 | continue |
Valentin Rothberg | b1a3f24 | 2015-03-16 12:16:14 +0100 | [diff] [blame] | 298 | undefined[feature] = referenced_features.get(feature) |
| 299 | return undefined |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 300 | |
| 301 | |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 302 | def parse_source_files(source_files): |
| 303 | """Parse each source file in @source_files and return dictionary with source |
| 304 | files as keys and lists of references Kconfig symbols as values.""" |
| 305 | referenced_features = dict() |
| 306 | for sfile in source_files: |
| 307 | referenced_features[sfile] = parse_source_file(sfile) |
| 308 | return referenced_features |
| 309 | |
| 310 | |
| 311 | def parse_source_file(sfile): |
| 312 | """Parse @sfile and return a list of referenced Kconfig features.""" |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 313 | lines = [] |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 314 | references = [] |
| 315 | |
| 316 | if not os.path.exists(sfile): |
| 317 | return references |
| 318 | |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 319 | with open(sfile, "r") as stream: |
| 320 | lines = stream.readlines() |
| 321 | |
| 322 | for line in lines: |
| 323 | if not "CONFIG_" in line: |
| 324 | continue |
| 325 | features = REGEX_SOURCE_FEATURE.findall(line) |
| 326 | for feature in features: |
| 327 | if not REGEX_FILTER_FEATURES.search(feature): |
| 328 | continue |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 329 | references.append(feature) |
| 330 | |
| 331 | return references |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 332 | |
| 333 | |
| 334 | def get_features_in_line(line): |
| 335 | """Return mentioned Kconfig features in @line.""" |
| 336 | return REGEX_FEATURE.findall(line) |
| 337 | |
| 338 | |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 339 | def parse_kconfig_files(args): |
| 340 | """Parse kconfig files and return tuple of defined and references Kconfig |
| 341 | symbols. Note, @args is a tuple of a list of files and the @ignore |
| 342 | pattern.""" |
| 343 | kconfig_files = args[0] |
| 344 | ignore = args[1] |
| 345 | defined_features = [] |
| 346 | referenced_features = dict() |
| 347 | |
| 348 | for kfile in kconfig_files: |
| 349 | defined, references = parse_kconfig_file(kfile) |
| 350 | defined_features.extend(defined) |
| 351 | if ignore and re.match(ignore, kfile): |
| 352 | # do not collect references for files that match the ignore pattern |
| 353 | continue |
| 354 | referenced_features[kfile] = references |
| 355 | return (defined_features, referenced_features) |
| 356 | |
| 357 | |
| 358 | def parse_kconfig_file(kfile): |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 359 | """Parse @kfile and update feature definitions and references.""" |
| 360 | lines = [] |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 361 | defined = [] |
| 362 | references = [] |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 363 | skip = False |
| 364 | |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 365 | if not os.path.exists(kfile): |
| 366 | return defined, references |
| 367 | |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 368 | with open(kfile, "r") as stream: |
| 369 | lines = stream.readlines() |
| 370 | |
| 371 | for i in range(len(lines)): |
| 372 | line = lines[i] |
| 373 | line = line.strip('\n') |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 374 | line = line.split("#")[0] # ignore comments |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 375 | |
| 376 | if REGEX_KCONFIG_DEF.match(line): |
| 377 | feature_def = REGEX_KCONFIG_DEF.findall(line) |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 378 | defined.append(feature_def[0]) |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 379 | skip = False |
| 380 | elif REGEX_KCONFIG_HELP.match(line): |
| 381 | skip = True |
| 382 | elif skip: |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 383 | # ignore content of help messages |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 384 | pass |
| 385 | elif REGEX_KCONFIG_STMT.match(line): |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 386 | line = REGEX_QUOTES.sub("", line) |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 387 | features = get_features_in_line(line) |
Valentin Rothberg | cc641d5 | 2014-11-08 20:56:35 +0100 | [diff] [blame] | 388 | # multi-line statements |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 389 | while line.endswith("\\"): |
| 390 | i += 1 |
| 391 | line = lines[i] |
| 392 | line = line.strip('\n') |
| 393 | features.extend(get_features_in_line(line)) |
| 394 | for feature in set(features): |
Valentin Rothberg | 0bd38ae | 2015-07-27 12:33:05 +0200 | [diff] [blame] | 395 | if REGEX_NUMERIC.match(feature): |
| 396 | # ignore numeric values |
| 397 | continue |
Valentin Rothberg | e2042a8 | 2015-10-15 10:37:47 +0200 | [diff] [blame^] | 398 | references.append(feature) |
| 399 | |
| 400 | return defined, references |
Valentin Rothberg | 24fe1f0 | 2014-09-27 16:30:45 +0200 | [diff] [blame] | 401 | |
| 402 | |
| 403 | if __name__ == "__main__": |
| 404 | main() |