xref: /optee_os/scripts/kconfig/kconfiglib/listnewconfig.py (revision c689edbb2550c76ae81dcecab717d82a564b2d7b)
1#!/usr/bin/env python3
2
3# Copyright (c) 2018-2019, Ulf Magnusson
4# SPDX-License-Identifier: ISC
5
6"""
7Lists all user-modifiable symbols that are not given a value in the
8configuration file. Usually, these are new symbols that have been added to the
9Kconfig files.
10
11The default configuration filename is '.config'. A different filename can be
12passed in the KCONFIG_CONFIG environment variable.
13"""
14from __future__ import print_function
15
16import argparse
17import sys
18
19from kconfiglib import Kconfig, BOOL, TRISTATE, INT, HEX, STRING, TRI_TO_STR
20
21
22def main():
23    parser = argparse.ArgumentParser(
24        formatter_class=argparse.RawDescriptionHelpFormatter,
25        description=__doc__)
26
27    parser.add_argument(
28        "--show-help", "-l",
29        action="store_true",
30        help="Show any help texts as well")
31
32    parser.add_argument(
33        "kconfig",
34        metavar="KCONFIG",
35        nargs="?",
36        default="Kconfig",
37        help="Top-level Kconfig file (default: Kconfig)")
38
39    args = parser.parse_args()
40
41    kconf = Kconfig(args.kconfig, suppress_traceback=True)
42    # Make it possible to filter this message out
43    print(kconf.load_config(), file=sys.stderr)
44
45    for sym in kconf.unique_defined_syms:
46        # Only show symbols that can be toggled. Choice symbols are a special
47        # case in that sym.assignable will be (2,) (length 1) for visible
48        # symbols in choices in y mode, but they can still be toggled by
49        # selecting some other symbol.
50        if sym.user_value is None and \
51           (len(sym.assignable) > 1 or
52            (sym.visibility and (sym.orig_type in (INT, HEX, STRING) or
53                                 sym.choice))):
54
55            # Don't reuse the 'config_string' format for bool/tristate symbols,
56            # to show n-valued symbols as 'CONFIG_FOO=n' instead of
57            # '# CONFIG_FOO is not set'. This matches the C tools.
58            if sym.orig_type in (BOOL, TRISTATE):
59                s = "{}{}={}\n".format(kconf.config_prefix, sym.name,
60                                       TRI_TO_STR[sym.tri_value])
61            else:
62                s = sym.config_string
63
64            print(s, end="")
65            if args.show_help:
66                for node in sym.nodes:
67                    if node.help is not None:
68                        # Indent by two spaces. textwrap.indent() is not
69                        # available in Python 2 (it's 3.3+).
70                        print("\n".join("  " + line
71                                        for line in node.help.split("\n")))
72                        break
73
74
75if __name__ == "__main__":
76    main()
77