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