|
29 | 29 | logger = None
|
30 | 30 |
|
31 | 31 |
|
32 |
| -def git(*args): |
| 32 | +def git(*args, cwd=None): |
33 | 33 | # Helper for running a Git command. Returns the rstrip()ed stdout output.
|
34 | 34 | # Called like git("diff"). Exits with SystemError (raised by sys.exit()) on
|
35 |
| - # errors. |
| 35 | + # errors. 'cwd' is the working directory to use (default: current |
| 36 | + # directory). |
36 | 37 |
|
37 | 38 | git_cmd = ("git",) + args
|
38 | 39 | git_cmd_s = " ".join(shlex.quote(word) for word in git_cmd) # For errors
|
39 | 40 |
|
40 | 41 | try:
|
41 | 42 | git_process = subprocess.Popen(
|
42 |
| - git_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 43 | + git_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) |
43 | 44 | except OSError as e:
|
44 | 45 | err("failed to run '{}': {}".format(git_cmd_s, e))
|
45 | 46 |
|
@@ -365,35 +366,22 @@ def check_no_undef_outside_kconfig(self, kconf):
|
365 | 366 | # - {, e.g. for Python scripts ("CONFIG_FOO_{}_BAR".format(...)")
|
366 | 367 | #
|
367 | 368 | # - *, meant for comments like '#endif /* CONFIG_FOO_* */
|
368 |
| - # |
369 |
| - # We skip doc/releases, which often references removed symbols. |
370 |
| - |
371 |
| - # Warning: Needs to work with both --perl-regexp and the 're' module |
372 |
| - regex = r"\bCONFIG_[A-Z0-9_]+\b(?!\s*##|[$@{*])" |
373 | 369 |
|
374 |
| - grep_cmd = ["git", "grep", "--line-number", "-I", "--null", |
375 |
| - "--perl-regexp", regex, |
376 |
| - "--", ":!doc/releases"] |
| 370 | + defined_syms = get_defined_syms(kconf) |
377 | 371 |
|
378 |
| - grep_process = subprocess.Popen(grep_cmd, |
379 |
| - stdout=subprocess.PIPE, |
380 |
| - stderr=subprocess.PIPE, |
381 |
| - cwd=ZEPHYR_BASE) |
| 372 | + # Maps each undefined symbol to a list <filename>:<linenr> strings |
| 373 | + undef_to_locs = collections.defaultdict(list) |
382 | 374 |
|
383 |
| - grep_stdout, grep_stderr = grep_process.communicate() |
384 |
| - # Fail if there's anything on stderr too, so that it doesn't get missed |
385 |
| - if grep_process.returncode or grep_stderr: |
386 |
| - self.error("'{}' failed with exit code {} (while searching for " |
387 |
| - "Kconfig symbol references)\n\nstdout:\n{}\n\n" |
388 |
| - "stderr:\n{}" |
389 |
| - .format(" ".join(grep_cmd), grep_process.returncode, |
390 |
| - grep_stdout, grep_stderr)) |
| 375 | + # Warning: Needs to work with both --perl-regexp and the 're' module |
| 376 | + regex = r"\bCONFIG_[A-Z0-9_]+\b(?!\s*##|[$@{*])" |
391 | 377 |
|
392 |
| - defined_syms = get_defined_syms(kconf) |
393 |
| - undef_to_locs = collections.defaultdict(list) |
| 378 | + # Skip doc/releases, which often references removed symbols |
| 379 | + grep_stdout = git("grep", "--line-number", "-I", "--null", |
| 380 | + "--perl-regexp", regex, "--", ":!/doc/releases", |
| 381 | + cwd=ZEPHYR_BASE) |
394 | 382 |
|
395 | 383 | # splitlines() supports various line terminators
|
396 |
| - for grep_line in grep_stdout.decode("utf-8").splitlines(): |
| 384 | + for grep_line in grep_stdout.splitlines(): |
397 | 385 | path, lineno, line = grep_line.split("\0")
|
398 | 386 |
|
399 | 387 | # Extract symbol references (might be more than one) within the
|
@@ -433,37 +421,24 @@ def check_no_undef_outside_kconfig(self, kconf):
|
433 | 421 |
|
434 | 422 |
|
435 | 423 | def get_defined_syms(kconf):
|
436 |
| - # Returns all defined Kconfig symbols. This is complicated by samples and |
437 |
| - # tests defining their own Kconfig trees. For those, just grep for |
438 |
| - # 'config FOO' to find definitions. Doing it "properly" with Kconfiglib is |
439 |
| - # still useful for the main tree, because some symbols are defined using |
440 |
| - # preprocessor macros. |
| 424 | + # Returns a set() with the names of all defined Kconfig symbols (with no |
| 425 | + # 'CONFIG_' prefix). This is complicated by samples and tests defining |
| 426 | + # their own Kconfig trees. For those, just grep for 'config FOO' to find |
| 427 | + # definitions. Doing it "properly" with Kconfiglib is still useful for the |
| 428 | + # main tree, because some symbols are defined using preprocessor macros. |
441 | 429 |
|
442 | 430 | # Warning: Needs to work with both --extended-regexp and the 're' module
|
443 |
| - regex = "^\s*(menu)?config\s*([A-Z0-9_]+)\s*(#|$)" |
444 |
| - |
445 |
| - grep_cmd = ["git", "grep", "-I", "-h", "--extended-regexp", regex, |
446 |
| - "--", ":samples", ":tests"] |
| 431 | + regex = r"^\s*(menu)?config\s*([A-Z0-9_]+)\s*(#|$)" |
447 | 432 |
|
448 |
| - grep_process = subprocess.Popen(grep_cmd, |
449 |
| - stdout=subprocess.PIPE, |
450 |
| - stderr=subprocess.PIPE, |
451 |
| - cwd=ZEPHYR_BASE) |
452 |
| - |
453 |
| - grep_stdout, grep_stderr = grep_process.communicate() |
454 |
| - # Fail if there's anything on stderr too, so that it doesn't get missed |
455 |
| - if grep_process.returncode or grep_stderr: |
456 |
| - self.error("'{}' failed with exit code {} (while searching for " |
457 |
| - "Kconfig symbol definitions)\n\nstdout:\n{}\n\n" |
458 |
| - "stderr:\n{}" |
459 |
| - .format(" ".join(grep_cmd), grep_process.returncode, |
460 |
| - grep_stdout, grep_stderr)) |
| 433 | + # Grep samples/ and tests/ for symbol definitions |
| 434 | + grep_stdout = git("grep", "-I", "-h", "--extended-regexp", regex, "--", |
| 435 | + ":samples", ":tests") |
461 | 436 |
|
| 437 | + # Start with the symbols from the main Kconfig tree in 'res' |
462 | 438 | res = set(sym.name for sym in kconf.unique_defined_syms)
|
463 |
| - |
464 |
| - for def_line in grep_stdout.decode("utf-8").splitlines(): |
| 439 | + # Add the grepped definitions from samples and tests |
| 440 | + for def_line in grep_stdout.splitlines(): |
465 | 441 | res.add(re.match(regex, def_line).group(2))
|
466 |
| - |
467 | 442 | return res
|
468 | 443 |
|
469 | 444 |
|
|
0 commit comments