1#!/usr/bin/env python3
2
3# Copyright (c) 2018-2023 Nordic Semiconductor ASA and Ulf Magnusson
4# Originally modified from:
5# https://github.com/ulfalizer/Kconfiglib/blob/master/examples/merge_config.py
6
7# SPDX-License-Identifier: ISC
8
9# Writes/updates the zephyr/.config configuration file by merging configuration
10# files passed as arguments, e.g. board *_defconfig and application prj.conf
11# files.
12#
13# When fragments haven't changed, zephyr/.config is both the input and the
14# output, which just updates it. This is handled in the CMake files.
15#
16# Also does various checks (most via Kconfiglib warnings).
17
18import argparse
19import os
20import re
21import sys
22import textwrap
23
24# Zephyr doesn't use tristate symbols. They're supported here just to make the
25# script a bit more generic.
26from kconfiglib import (
27    AND,
28    BOOL,
29    OR,
30    TRI_TO_STR,
31    TRISTATE,
32    Kconfig,
33    expr_str,
34    expr_value,
35    split_expr,
36)
37
38
39def main():
40    args = parse_args()
41
42    if args.zephyr_base:
43        os.environ['ZEPHYR_BASE'] = args.zephyr_base
44
45    print("Parsing " + args.kconfig_file)
46    kconf = Kconfig(args.kconfig_file, warn_to_stderr=False,
47                    suppress_traceback=True)
48
49    if args.handwritten_input_configs:
50        # Warn for assignments to undefined symbols, but only for handwritten
51        # fragments, to avoid warnings-turned-errors when using an old
52        # configuration file together with updated Kconfig files
53        kconf.warn_assign_undef = True
54
55        # prj.conf may override settings from the board configuration, so
56        # disable warnings about symbols being assigned more than once
57        kconf.warn_assign_override = False
58        kconf.warn_assign_redun = False
59
60    if args.forced_input_configs:
61        # Do not warn on a redundant config.
62        # The reason is that a regular .config will be followed by the forced
63        # config which under normal circumstances should be identical to the
64        # configured setting.
65        # Only if user has modified to a value that gets overruled by the forced
66        # a warning shall be issued.
67        kconf.warn_assign_redun = False
68
69    # Load files
70    print(kconf.load_config(args.configs_in[0]))
71    for config in args.configs_in[1:]:
72        # replace=False creates a merged configuration
73        print(kconf.load_config(config, replace=False))
74
75    if args.handwritten_input_configs:
76        # Check that there are no assignments to promptless symbols, which
77        # have no effect.
78        #
79        # This only makes sense when loading handwritten fragments and not when
80        # loading zephyr/.config, because zephyr/.config is configuration
81        # output and also assigns promptless symbols.
82        check_no_promptless_assign(kconf)
83
84        # Print warnings for symbols that didn't get the assigned value. Only
85        # do this for handwritten input too, to avoid likely unhelpful warnings
86        # when using an old configuration and updating Kconfig files.
87        check_assigned_sym_values(kconf)
88        check_assigned_choice_values(kconf)
89
90    if kconf.syms.get('WARN_DEPRECATED', kconf.y).tri_value == 2:
91        check_deprecated(kconf)
92
93    if kconf.syms.get('WARN_EXPERIMENTAL', kconf.y).tri_value == 2:
94        check_experimental(kconf)
95
96    # Hack: Force all symbols to be evaluated, to catch warnings generated
97    # during evaluation. Wait till the end to write the actual output files, so
98    # that we don't generate any output if there are warnings-turned-errors.
99    #
100    # Kconfiglib caches calculated symbol values internally, so this is still
101    # fast.
102    kconf.write_config(os.devnull)
103
104    warn_only = r"warning:.*set more than once."
105
106    if kconf.warnings:
107        if args.forced_input_configs:
108            error_out = False
109        else:
110            error_out = True
111
112        # Put a blank line between warnings to make them easier to read
113        for warning in kconf.warnings:
114            print("\n" + warning, file=sys.stderr)
115
116            if not error_out and not re.search(warn_only, warning):
117                # The warning is not a warn_only, fail the Kconfig.
118                error_out = True
119
120        # Turn all warnings into errors, so that e.g. assignments to undefined
121        # Kconfig symbols become errors.
122        #
123        # A warning is generated by this script whenever a symbol gets a
124        # different value than the one it was assigned. Keep that one as just a
125        # warning for now.
126        if error_out:
127            err("Aborting due to Kconfig warnings")
128
129    # Write the merged configuration and the C header
130    print(kconf.write_config(args.config_out))
131    print(kconf.write_autoconf(args.header_out))
132
133    # Write the list of parsed Kconfig files to a file
134    write_kconfig_filenames(kconf, args.kconfig_list_out)
135
136
137def check_no_promptless_assign(kconf):
138    # Checks that no promptless symbols are assigned
139
140    for sym in kconf.unique_defined_syms:
141        if sym.user_value is not None and promptless(sym):
142            err(f"""\
143{sym.name_and_loc} is assigned in a configuration file, but is not directly
144user-configurable (has no prompt). It gets its value indirectly from other
145symbols. """ + SYM_INFO_HINT.format(sym))
146
147
148def check_assigned_sym_values(kconf):
149    # Verifies that the values assigned to symbols "took" (matches the value
150    # the symbols actually got), printing warnings otherwise. Choice symbols
151    # are checked separately, in check_assigned_choice_values().
152
153    for sym in kconf.unique_defined_syms:
154        if sym.choice:
155            continue
156
157        user_value = sym.user_value
158        if user_value is None:
159            continue
160
161        # Tristate values are represented as 0, 1, 2. Having them as "n", "m",
162        # "y" is more convenient here, so convert.
163        if sym.type in (BOOL, TRISTATE):
164            user_value = TRI_TO_STR[user_value]
165
166        if user_value != sym.str_value:
167            msg = f"{sym.name_and_loc} was assigned the value '{user_value}'" \
168                  f" but got the value '{sym.str_value}'. "
169
170            # List any unsatisfied 'depends on' dependencies in the warning
171            mdeps = missing_deps(sym)
172            if mdeps:
173                expr_strs = []
174                for expr in mdeps:
175                    estr = expr_str(expr)
176                    if isinstance(expr, tuple):
177                        # Add () around dependencies that aren't plain symbols.
178                        # Gives '(FOO || BAR) (=n)' instead of
179                        # 'FOO || BAR (=n)', which might be clearer.
180                        estr = f"({estr})"
181                    expr_strs.append(f"{estr} "
182                                     f"(={TRI_TO_STR[expr_value(expr)]})")
183
184                msg += "Check these unsatisfied dependencies: " + \
185                    ", ".join(expr_strs) + ". "
186
187            warn(msg + SYM_INFO_HINT.format(sym))
188
189
190def missing_deps(sym):
191    # check_assigned_sym_values() helper for finding unsatisfied dependencies.
192    #
193    # Given direct dependencies
194    #
195    #     depends on <expr> && <expr> && ... && <expr>
196    #
197    # on 'sym' (which can also come from e.g. a surrounding 'if'), returns a
198    # list of all <expr>s with a value less than the value 'sym' was assigned
199    # ("less" instead of "not equal" just to be general and handle tristates,
200    # even though Zephyr doesn't use them).
201    #
202    # For string/int/hex symbols, just looks for <expr> = n.
203    #
204    # Note that <expr>s can be something more complicated than just a symbol,
205    # like 'FOO || BAR' or 'FOO = "string"'.
206
207    deps = split_expr(sym.direct_dep, AND)
208
209    if sym.type in (BOOL, TRISTATE):
210        return [dep for dep in deps if expr_value(dep) < sym.user_value]
211    # string/int/hex
212    return [dep for dep in deps if expr_value(dep) == 0]
213
214
215def check_assigned_choice_values(kconf):
216    # Verifies that any choice symbols that were selected (by setting them to
217    # y) ended up as the selection, printing warnings otherwise.
218    #
219    # We check choice symbols separately to avoid warnings when two different
220    # choice symbols within the same choice are set to y. This might happen if
221    # a choice selection from a board defconfig is overridden in a prj.conf,
222    # for example. The last choice symbol set to y becomes the selection (and
223    # all other choice symbols get the value n).
224    #
225    # Without special-casing choices, we'd detect that the first symbol set to
226    # y ended up as n, and print a spurious warning.
227
228    for choice in kconf.unique_choices:
229        if choice.user_selection and \
230           choice.user_selection is not choice.selection:
231
232            warn(f"""\
233The choice symbol {choice.user_selection.name_and_loc} was selected (set =y),
234but {choice.selection.name_and_loc if choice.selection else "no symbol"} ended
235up as the choice selection. """ + SYM_INFO_HINT.format(choice.user_selection))
236
237
238# Hint on where to find symbol information. Used like
239# SYM_INFO_HINT.format(sym).
240SYM_INFO_HINT = """\
241See http://docs.zephyrproject.org/latest/kconfig.html#CONFIG_{0.name} and/or
242look up {0.name} in the menuconfig/guiconfig interface. The Application
243Development Primer, Setting Configuration Values, and Kconfig - Tips and Best
244Practices sections of the manual might be helpful too.\
245"""
246
247
248def check_deprecated(kconf):
249    deprecated = kconf.syms.get('DEPRECATED')
250    dep_expr = kconf.n if deprecated is None else deprecated.rev_dep
251
252    if dep_expr is not kconf.n:
253        selectors = [s for s in split_expr(dep_expr, OR) if expr_value(s) == 2]
254        for selector in selectors:
255            selector_name = split_expr(selector, AND)[0].name
256            warn(f'Deprecated symbol {selector_name} is enabled.')
257
258
259def check_experimental(kconf):
260    experimental = kconf.syms.get('EXPERIMENTAL')
261    dep_expr = kconf.n if experimental is None else experimental.rev_dep
262
263    if dep_expr is not kconf.n:
264        selectors = [s for s in split_expr(dep_expr, OR) if expr_value(s) == 2]
265        for selector in selectors:
266            selector_name = split_expr(selector, AND)[0].name
267            warn(f'Experimental symbol {selector_name} is enabled.')
268
269
270def promptless(sym):
271    # Returns True if 'sym' has no prompt. Since the symbol might be defined in
272    # multiple locations, we need to check all locations.
273
274    return not any(node.prompt for node in sym.nodes)
275
276
277def write_kconfig_filenames(kconf, kconfig_list_path):
278    # Writes a sorted list with the absolute paths of all parsed Kconfig files
279    # to 'kconfig_list_path'. The paths are realpath()'d, and duplicates are
280    # removed. This file is used by CMake to look for changed Kconfig files. It
281    # needs to be deterministic.
282
283    with open(kconfig_list_path, 'w') as out:
284        for path in sorted({os.path.realpath(os.path.join(kconf.srctree, path))
285                            for path in kconf.kconfig_filenames}):
286            print(path, file=out)
287
288
289def parse_args():
290    parser = argparse.ArgumentParser(allow_abbrev=False)
291
292    parser.add_argument("--handwritten-input-configs",
293                        action="store_true",
294                        help="Assume the input configuration fragments are "
295                             "handwritten fragments and do additional checks "
296                             "on them, like no promptless symbols being "
297                             "assigned")
298    parser.add_argument("--forced-input-configs",
299                        action="store_true",
300                        help="Indicate the input configuration files are "
301                             "followed by an forced configuration file."
302                             "The forced configuration is used to forcefully "
303                             "set specific configuration settings to a "
304                             "pre-defined value and thereby remove any user "
305                             " adjustments.")
306    parser.add_argument("--zephyr-base",
307                        help="Path to current Zephyr installation")
308    parser.add_argument("kconfig_file",
309                        help="Top-level Kconfig file")
310    parser.add_argument("config_out",
311                        help="Output configuration file")
312    parser.add_argument("header_out",
313                        help="Output header file")
314    parser.add_argument("kconfig_list_out",
315                        help="Output file for list of parsed Kconfig files")
316    parser.add_argument("configs_in",
317                        nargs="+",
318                        help="Input configuration fragments. Will be merged "
319                             "together.")
320
321    return parser.parse_args()
322
323
324def warn(msg):
325    # Use a large fill() width to try to avoid linebreaks in the symbol
326    # reference link, and add some extra newlines to set the message off from
327    # surrounding text (this usually gets printed as part of spammy CMake
328    # output)
329    print("\n" + textwrap.fill("warning: " + msg, 100) + "\n", file=sys.stderr)
330
331
332def err(msg):
333    sys.exit("\n" + textwrap.fill("error: " + msg, 100) + "\n")
334
335
336if __name__ == "__main__":
337    main()
338