summaryrefslogtreecommitdiff
path: root/examples/allyesconfig.py
blob: b9210d3936b9c21fcee9af2f70bff306add1880e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Works like 'make allyesconfig'. Verified by the test suite to generate output
# identical to 'make allyesconfig', for all ARCHES.
#
# This example is implemented a bit differently from allnoconfig.py to
# demonstrate some other possibilities.
#
# In theory, we need to handle choices in two different modes:
#
#   y: One symbol is y, the rest are n
#   m: Any number of symbols are m, the rest are n
#
# Only tristate choices can be in m mode.
#
# In practice, no m mode choices appear for allyesconfig as of 4.14, as
# expected, but we still handle them here for completeness. Here's a convoluted
# example of how you might get an m-mode choice even during allyesconfig:
#
#   choice
#           tristate "weird choice"
#           depends on m
#
#   ...
#
#   endchoice
#
#
# Usage:
#
#   $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/allyesconfig.py

from kconfiglib import Kconfig, Choice, STR_TO_TRI
import sys

def all_choices(node):
    """
    Returns all choices in the menu tree rooted at 'node'. See
    print_tree_iter.py for an example of how the menu tree can be walked
    iteratively.

    (I was thinking of making a list of choices available directly in the API,
    but I'm not sure it will always be needed internally, and I'm trying to
    spam the API with less seldom-used stuff compared to Kconfiglib 1.)
    """
    res = []

    while node:
        if isinstance(node.item, Choice):
            res.append(node.item)

        if node.list:
            res.extend(all_choices(node.list))

        node = node.next

    return res

kconf = Kconfig(sys.argv[1])

non_choice_syms = [sym for sym in kconf.defined_syms if not sym.choice]
choices = all_choices(kconf.top_node)  # All choices in the configuration

while True:
    changed = False

    for sym in non_choice_syms:
        # Set the symbol to the highest assignable value, unless it already has
        # that value. sym.assignable[-1] gives the last element in assignable.
        if sym.assignable and sym.tri_value < sym.assignable[-1]:
            sym.set_value(sym.assignable[-1])
            changed = True

    for choice in choices:
        # Same logic as above for choices
        if choice.assignable and choice.tri_value < choice.assignable[-1]:
            choice.set_value(choice.assignable[-1])
            changed = True

            if choice.tri_value == 1:
                # For m-mode choices, set all choice symbols to m
                for sym in choice.syms:
                    sym.set_value(1)

    # Do multiple passes until we longer manage to raise any symbols or
    # choices, like in allnoconfig.py
    if not changed:
        break

kconf.write_config(".config")