1# Copyright (c) 2011-2019, Ulf Magnusson
2# SPDX-License-Identifier: ISC
3
4"""
5Overview
6========
7
8Kconfiglib is a Python 2/3 library for scripting and extracting information
9from Kconfig (https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt)
10configuration systems.
11
12See the homepage at https://github.com/ulfalizer/Kconfiglib for a longer
13overview.
14
15Since Kconfiglib 12.0.0, the library version is available in
16kconfiglib.VERSION, which is a (<major>, <minor>, <patch>) tuple, e.g.
17(12, 0, 0).
18
19
20Using Kconfiglib on the Linux kernel with the Makefile targets
21==============================================================
22
23For the Linux kernel, a handy interface is provided by the
24scripts/kconfig/Makefile patch, which can be applied with either 'git am' or
25the 'patch' utility:
26
27  $ wget -qO- https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch | git am
28  $ wget -qO- https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch | patch -p1
29
30Warning: Not passing -p1 to patch will cause the wrong file to be patched.
31
32Please tell me if the patch does not apply. It should be trivial to apply
33manually, as it's just a block of text that needs to be inserted near the other
34*conf: targets in scripts/kconfig/Makefile.
35
36Look further down for a motivation for the Makefile patch and for instructions
37on how you can use Kconfiglib without it.
38
39If you do not wish to install Kconfiglib via pip, the Makefile patch is set up
40so that you can also just clone Kconfiglib into the kernel root:
41
42  $ git clone git://github.com/ulfalizer/Kconfiglib.git
43  $ git am Kconfiglib/makefile.patch  (or 'patch -p1 < Kconfiglib/makefile.patch')
44
45Warning: The directory name Kconfiglib/ is significant in this case, because
46it's added to PYTHONPATH by the new targets in makefile.patch.
47
48The targets added by the Makefile patch are described in the following
49sections.
50
51
52make kmenuconfig
53----------------
54
55This target runs the curses menuconfig interface with Python 3. As of
56Kconfiglib 12.2.0, both Python 2 and Python 3 are supported (previously, only
57Python 3 was supported, so this was a backport).
58
59
60make guiconfig
61--------------
62
63This target runs the Tkinter menuconfig interface. Both Python 2 and Python 3
64are supported. To change the Python interpreter used, pass
65PYTHONCMD=<executable> to 'make'. The default is 'python'.
66
67
68make [ARCH=<arch>] iscriptconfig
69--------------------------------
70
71This target gives an interactive Python prompt where a Kconfig instance has
72been preloaded and is available in 'kconf'. To change the Python interpreter
73used, pass PYTHONCMD=<executable> to 'make'. The default is 'python'.
74
75To get a feel for the API, try evaluating and printing the symbols in
76kconf.defined_syms, and explore the MenuNode menu tree starting at
77kconf.top_node by following 'next' and 'list' pointers.
78
79The item contained in a menu node is found in MenuNode.item (note that this can
80be one of the constants kconfiglib.MENU and kconfiglib.COMMENT), and all
81symbols and choices have a 'nodes' attribute containing their menu nodes
82(usually only one). Printing a menu node will print its item, in Kconfig
83format.
84
85If you want to look up a symbol by name, use the kconf.syms dictionary.
86
87
88make scriptconfig SCRIPT=<script> [SCRIPT_ARG=<arg>]
89----------------------------------------------------
90
91This target runs the Python script given by the SCRIPT parameter on the
92configuration. sys.argv[1] holds the name of the top-level Kconfig file
93(currently always "Kconfig" in practice), and sys.argv[2] holds the SCRIPT_ARG
94argument, if given.
95
96See the examples/ subdirectory for example scripts.
97
98
99make dumpvarsconfig
100-------------------
101
102This target prints a list of all environment variables referenced from the
103Kconfig files, together with their values. See the
104Kconfiglib/examples/dumpvars.py script.
105
106Only environment variables that are referenced via the Kconfig preprocessor
107$(FOO) syntax are included. The preprocessor was added in Linux 4.18.
108
109
110Using Kconfiglib without the Makefile targets
111=============================================
112
113The make targets are only needed to pick up environment variables exported from
114the Kbuild makefiles and referenced inside Kconfig files, via e.g.
115'source "arch/$(SRCARCH)/Kconfig" and commands run via '$(shell,...)'.
116
117These variables are referenced as of writing (Linux 4.18), together with sample
118values:
119
120  srctree          (.)
121  ARCH             (x86)
122  SRCARCH          (x86)
123  KERNELVERSION    (4.18.0)
124  CC               (gcc)
125  HOSTCC           (gcc)
126  HOSTCXX          (g++)
127  CC_VERSION_TEXT  (gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0)
128
129Older kernels only reference ARCH, SRCARCH, and KERNELVERSION.
130
131If your kernel is recent enough (4.18+), you can get a list of referenced
132environment variables via 'make dumpvarsconfig' (see above). Note that this
133command is added by the Makefile patch.
134
135To run Kconfiglib without the Makefile patch, set the environment variables
136manually:
137
138  $ srctree=. ARCH=x86 SRCARCH=x86 KERNELVERSION=`make kernelversion` ... python(3)
139  >>> import kconfiglib
140  >>> kconf = kconfiglib.Kconfig()  # filename defaults to "Kconfig"
141
142Search the top-level Makefile for "Additional ARCH settings" to see other
143possibilities for ARCH and SRCARCH.
144
145
146Intro to symbol values
147======================
148
149Kconfiglib has the same assignment semantics as the C implementation.
150
151Any symbol can be assigned a value by the user (via Kconfig.load_config() or
152Symbol.set_value()), but this user value is only respected if the symbol is
153visible, which corresponds to it (currently) being visible in the menuconfig
154interface.
155
156For symbols with prompts, the visibility of the symbol is determined by the
157condition on the prompt. Symbols without prompts are never visible, so setting
158a user value on them is pointless. A warning will be printed by default if
159Symbol.set_value() is called on a promptless symbol. Assignments to promptless
160symbols are normal within a .config file, so no similar warning will be printed
161by load_config().
162
163Dependencies from parents and 'if'/'depends on' are propagated to properties,
164including prompts, so these two configurations are logically equivalent:
165
166(1)
167
168  menu "menu"
169      depends on A
170
171  if B
172
173  config FOO
174      tristate "foo" if D
175      default y
176      depends on C
177
178  endif
179
180  endmenu
181
182(2)
183
184  menu "menu"
185      depends on A
186
187  config FOO
188      tristate "foo" if A && B && C && D
189      default y if A && B && C
190
191  endmenu
192
193In this example, A && B && C && D (the prompt condition) needs to be non-n for
194FOO to be visible (assignable). If its value is m, the symbol can only be
195assigned the value m: The visibility sets an upper bound on the value that can
196be assigned by the user, and any higher user value will be truncated down.
197
198'default' properties are independent of the visibility, though a 'default' will
199often get the same condition as the prompt due to dependency propagation.
200'default' properties are used if the symbol is not visible or has no user
201value.
202
203Symbols with no user value (or that have a user value but are not visible) and
204no (active) 'default' default to n for bool/tristate symbols, and to the empty
205string for other symbol types.
206
207'select' works similarly to symbol visibility, but sets a lower bound on the
208value of the symbol. The lower bound is determined by the value of the
209select*ing* symbol. 'select' does not respect visibility, so non-visible
210symbols can be forced to a particular (minimum) value by a select as well.
211
212For non-bool/tristate symbols, it only matters whether the visibility is n or
213non-n: m visibility acts the same as y visibility.
214
215Conditions on 'default' and 'select' work in mostly intuitive ways. If the
216condition is n, the 'default' or 'select' is disabled. If it is m, the
217'default' or 'select' value (the value of the selecting symbol) is truncated
218down to m.
219
220When writing a configuration with Kconfig.write_config(), only symbols that are
221visible, have an (active) default, or are selected will get written out (note
222that this includes all symbols that would accept user values). Kconfiglib
223matches the .config format produced by the C implementations down to the
224character. This eases testing.
225
226For a visible bool/tristate symbol FOO with value n, this line is written to
227.config:
228
229    # CONFIG_FOO is not set
230
231The point is to remember the user n selection (which might differ from the
232default value the symbol would get), while at the same sticking to the rule
233that undefined corresponds to n (.config uses Makefile format, making the line
234above a comment). When the .config file is read back in, this line will be
235treated the same as the following assignment:
236
237    CONFIG_FOO=n
238
239In Kconfiglib, the set of (currently) assignable values for a bool/tristate
240symbol appear in Symbol.assignable. For other symbol types, just check if
241sym.visibility is non-0 (non-n) to see whether the user value will have an
242effect.
243
244
245Intro to the menu tree
246======================
247
248The menu structure, as seen in e.g. menuconfig, is represented by a tree of
249MenuNode objects. The top node of the configuration corresponds to an implicit
250top-level menu, the title of which is shown at the top in the standard
251menuconfig interface. (The title is also available in Kconfig.mainmenu_text in
252Kconfiglib.)
253
254The top node is found in Kconfig.top_node. From there, you can visit child menu
255nodes by following the 'list' pointer, and any following menu nodes by
256following the 'next' pointer. Usually, a non-None 'list' pointer indicates a
257menu or Choice, but menu nodes for symbols can sometimes have a non-None 'list'
258pointer too due to submenus created implicitly from dependencies.
259
260MenuNode.item is either a Symbol or a Choice object, or one of the constants
261MENU and COMMENT. The prompt of the menu node can be found in MenuNode.prompt,
262which also holds the title for menus and comments. For Symbol and Choice,
263MenuNode.help holds the help text (if any, otherwise None).
264
265Most symbols will only have a single menu node. A symbol defined in multiple
266locations will have one menu node for each location. The list of menu nodes for
267a Symbol or Choice can be found in the Symbol/Choice.nodes attribute.
268
269Note that prompts and help texts for symbols and choices are stored in their
270menu node(s) rather than in the Symbol or Choice objects themselves. This makes
271it possible to define a symbol in multiple locations with a different prompt or
272help text in each location. To get the help text or prompt for a symbol with a
273single menu node, do sym.nodes[0].help and sym.nodes[0].prompt, respectively.
274The prompt is a (text, condition) tuple, where condition determines the
275visibility (see 'Intro to expressions' below).
276
277This organization mirrors the C implementation. MenuNode is called
278'struct menu' there, but I thought "menu" was a confusing name.
279
280It is possible to give a Choice a name and define it in multiple locations,
281hence why Choice.nodes is also a list.
282
283As a convenience, the properties added at a particular definition location are
284available on the MenuNode itself, in e.g. MenuNode.defaults. This is helpful
285when generating documentation, so that symbols/choices defined in multiple
286locations can be shown with the correct properties at each location.
287
288
289Intro to expressions
290====================
291
292Expressions can be evaluated with the expr_value() function and printed with
293the expr_str() function (these are used internally as well). Evaluating an
294expression always yields a tristate value, where n, m, and y are represented as
2950, 1, and 2, respectively.
296
297The following table should help you figure out how expressions are represented.
298A, B, C, ... are symbols (Symbol instances), NOT is the kconfiglib.NOT
299constant, etc.
300
301Expression            Representation
302----------            --------------
303A                     A
304"A"                   A (constant symbol)
305!A                    (NOT, A)
306A && B                (AND, A, B)
307A && B && C           (AND, A, (AND, B, C))
308A || B                (OR, A, B)
309A || (B && C && D)    (OR, A, (AND, B, (AND, C, D)))
310A = B                 (EQUAL, A, B)
311A != "foo"            (UNEQUAL, A, foo (constant symbol))
312A && B = C && D       (AND, A, (AND, (EQUAL, B, C), D))
313n                     Kconfig.n (constant symbol)
314m                     Kconfig.m (constant symbol)
315y                     Kconfig.y (constant symbol)
316"y"                   Kconfig.y (constant symbol)
317
318Strings like "foo" in 'default "foo"' or 'depends on SYM = "foo"' are
319represented as constant symbols, so the only values that appear in expressions
320are symbols***. This mirrors the C implementation.
321
322***For choice symbols, the parent Choice will appear in expressions as well,
323but it's usually invisible as the value interfaces of Symbol and Choice are
324identical. This mirrors the C implementation and makes different choice modes
325"just work".
326
327Manual evaluation examples:
328
329  - The value of A && B is min(A.tri_value, B.tri_value)
330
331  - The value of A || B is max(A.tri_value, B.tri_value)
332
333  - The value of !A is 2 - A.tri_value
334
335  - The value of A = B is 2 (y) if A.str_value == B.str_value, and 0 (n)
336    otherwise. Note that str_value is used here instead of tri_value.
337
338    For constant (as well as undefined) symbols, str_value matches the name of
339    the symbol. This mirrors the C implementation and explains why
340    'depends on SYM = "foo"' above works as expected.
341
342n/m/y are automatically converted to the corresponding constant symbols
343"n"/"m"/"y" (Kconfig.n/m/y) during parsing.
344
345Kconfig.const_syms is a dictionary like Kconfig.syms but for constant symbols.
346
347If a condition is missing (e.g., <cond> when the 'if <cond>' is removed from
348'default A if <cond>'), it is actually Kconfig.y. The standard __str__()
349functions just avoid printing 'if y' conditions to give cleaner output.
350
351
352Kconfig extensions
353==================
354
355Kconfiglib includes a couple of Kconfig extensions:
356
357'source' with relative path
358---------------------------
359
360The 'rsource' statement sources Kconfig files with a path relative to directory
361of the Kconfig file containing the 'rsource' statement, instead of relative to
362the project root.
363
364Consider following directory tree:
365
366  Project
367  +--Kconfig
368  |
369  +--src
370     +--Kconfig
371     |
372     +--SubSystem1
373        +--Kconfig
374        |
375        +--ModuleA
376           +--Kconfig
377
378In this example, assume that src/SubSystem1/Kconfig wants to source
379src/SubSystem1/ModuleA/Kconfig.
380
381With 'source', this statement would be used:
382
383  source "src/SubSystem1/ModuleA/Kconfig"
384
385With 'rsource', this turns into
386
387  rsource "ModuleA/Kconfig"
388
389If an absolute path is given to 'rsource', it acts the same as 'source'.
390
391'rsource' can be used to create "position-independent" Kconfig trees that can
392be moved around freely.
393
394
395Globbing 'source'
396-----------------
397
398'source' and 'rsource' accept glob patterns, sourcing all matching Kconfig
399files. They require at least one matching file, raising a KconfigError
400otherwise.
401
402For example, the following statement might source sub1/foofoofoo and
403sub2/foobarfoo:
404
405  source "sub[12]/foo*foo"
406
407The glob patterns accepted are the same as for the standard glob.glob()
408function.
409
410Two additional statements are provided for cases where it's acceptable for a
411pattern to match no files: 'osource' and 'orsource' (the o is for "optional").
412
413For example, the following statements will be no-ops if neither "foo" nor any
414files matching "bar*" exist:
415
416  osource "foo"
417  osource "bar*"
418
419'orsource' does a relative optional source.
420
421'source' and 'osource' are analogous to 'include' and '-include' in Make.
422
423
424Generalized def_* keywords
425--------------------------
426
427def_int, def_hex, and def_string are available in addition to def_bool and
428def_tristate, allowing int, hex, and string symbols to be given a type and a
429default at the same time.
430
431
432Extra optional warnings
433-----------------------
434
435Some optional warnings can be controlled via environment variables:
436
437  - KCONFIG_WARN_UNDEF: If set to 'y', warnings will be generated for all
438    references to undefined symbols within Kconfig files. The only gotcha is
439    that all hex literals must be prefixed with "0x" or "0X", to make it
440    possible to distinguish them from symbol references.
441
442    Some projects (e.g. the Linux kernel) use multiple Kconfig trees with many
443    shared Kconfig files, leading to some safe undefined symbol references.
444    KCONFIG_WARN_UNDEF is useful in projects that only have a single Kconfig
445    tree though.
446
447    KCONFIG_STRICT is an older alias for this environment variable, supported
448    for backwards compatibility.
449
450  - KCONFIG_WARN_UNDEF_ASSIGN: If set to 'y', warnings will be generated for
451    all assignments to undefined symbols within .config files. By default, no
452    such warnings are generated.
453
454    This warning can also be enabled/disabled via the Kconfig.warn_assign_undef
455    variable.
456
457
458Preprocessor user functions defined in Python
459---------------------------------------------
460
461Preprocessor functions can be defined in Python, which makes it simple to
462integrate information from existing Python tools into Kconfig (e.g. to have
463Kconfig symbols depend on hardware information stored in some other format).
464
465Putting a Python module named kconfigfunctions(.py) anywhere in sys.path will
466cause it to be imported by Kconfiglib (in Kconfig.__init__()). Note that
467sys.path can be customized via PYTHONPATH, and includes the directory of the
468module being run by default, as well as installation directories.
469
470If the KCONFIG_FUNCTIONS environment variable is set, it gives a different
471module name to use instead of 'kconfigfunctions'.
472
473The imported module is expected to define a global dictionary named 'functions'
474that maps function names to Python functions, as follows:
475
476  def my_fn(kconf, name, arg_1, arg_2, ...):
477      # kconf:
478      #   Kconfig instance
479      #
480      # name:
481      #   Name of the user-defined function ("my-fn"). Think argv[0].
482      #
483      # arg_1, arg_2, ...:
484      #   Arguments passed to the function from Kconfig (strings)
485      #
486      # Returns a string to be substituted as the result of calling the
487      # function
488      ...
489
490  def my_other_fn(kconf, name, arg_1, arg_2, ...):
491      ...
492
493  functions = {
494      "my-fn":       (my_fn,       <min.args>, <max.args>/None),
495      "my-other-fn": (my_other_fn, <min.args>, <max.args>/None),
496      ...
497  }
498
499  ...
500
501<min.args> and <max.args> are the minimum and maximum number of arguments
502expected by the function (excluding the implicit 'name' argument). If
503<max.args> is None, there is no upper limit to the number of arguments. Passing
504an invalid number of arguments will generate a KconfigError exception.
505
506Functions can access the current parsing location as kconf.filename/linenr.
507Accessing other fields of the Kconfig object is not safe. See the warning
508below.
509
510Keep in mind that for a variable defined like 'foo = $(fn)', 'fn' will be
511called only when 'foo' is expanded. If 'fn' uses the parsing location and the
512intent is to use the location of the assignment, you want 'foo := $(fn)'
513instead, which calls the function immediately.
514
515Once defined, user functions can be called from Kconfig in the same way as
516other preprocessor functions:
517
518    config FOO
519        ...
520        depends on $(my-fn,arg1,arg2)
521
522If my_fn() returns "n", this will result in
523
524    config FOO
525        ...
526        depends on n
527
528Warning
529*******
530
531User-defined preprocessor functions are called as they're encountered at parse
532time, before all Kconfig files have been processed, and before the menu tree
533has been finalized. There are no guarantees that accessing Kconfig symbols or
534the menu tree via the 'kconf' parameter will work, and it could potentially
535lead to a crash.
536
537Preferably, user-defined functions should be stateless.
538
539
540Feedback
541========
542
543Send bug reports, suggestions, and questions to ulfalizer a.t Google's email
544service, or open a ticket on the GitHub page.
545"""
546import errno
547import importlib
548import os
549import re
550import sys
551
552# Get rid of some attribute lookups. These are obvious in context.
553from glob import iglob
554from os.path import dirname, exists, expandvars, islink, join, realpath
555
556
557VERSION = (14, 1, 0)
558
559
560# File layout:
561#
562# Public classes
563# Public functions
564# Internal functions
565# Global constants
566
567# Line length: 79 columns
568
569
570#
571# Public classes
572#
573
574
575class Kconfig(object):
576    """
577    Represents a Kconfig configuration, e.g. for x86 or ARM. This is the set of
578    symbols, choices, and menu nodes appearing in the configuration. Creating
579    any number of Kconfig objects (including for different architectures) is
580    safe. Kconfiglib doesn't keep any global state.
581
582    The following attributes are available. They should be treated as
583    read-only, and some are implemented through @property magic.
584
585    syms:
586      A dictionary with all symbols in the configuration, indexed by name. Also
587      includes all symbols that are referenced in expressions but never
588      defined, except for constant (quoted) symbols.
589
590      Undefined symbols can be recognized by Symbol.nodes being empty -- see
591      the 'Intro to the menu tree' section in the module docstring.
592
593    const_syms:
594      A dictionary like 'syms' for constant (quoted) symbols
595
596    named_choices:
597      A dictionary like 'syms' for named choices (choice FOO)
598
599    defined_syms:
600      A list with all defined symbols, in the same order as they appear in the
601      Kconfig files. Symbols defined in multiple locations appear multiple
602      times.
603
604      Note: You probably want to use 'unique_defined_syms' instead. This
605      attribute is mostly maintained for backwards compatibility.
606
607    unique_defined_syms:
608      A list like 'defined_syms', but with duplicates removed. Just the first
609      instance is kept for symbols defined in multiple locations. Kconfig order
610      is preserved otherwise.
611
612      Using this attribute instead of 'defined_syms' can save work, and
613      automatically gives reasonable behavior when writing configuration output
614      (symbols defined in multiple locations only generate output once, while
615      still preserving Kconfig order for readability).
616
617    choices:
618      A list with all choices, in the same order as they appear in the Kconfig
619      files.
620
621      Note: You probably want to use 'unique_choices' instead. This attribute
622      is mostly maintained for backwards compatibility.
623
624    unique_choices:
625      Analogous to 'unique_defined_syms', for choices. Named choices can have
626      multiple definition locations.
627
628    menus:
629      A list with all menus, in the same order as they appear in the Kconfig
630      files
631
632    comments:
633      A list with all comments, in the same order as they appear in the Kconfig
634      files
635
636    kconfig_filenames:
637      A list with the filenames of all Kconfig files included in the
638      configuration, relative to $srctree (or relative to the current directory
639      if $srctree isn't set), except absolute paths (e.g.
640      'source "/foo/Kconfig"') are kept as-is.
641
642      The files are listed in the order they are source'd, starting with the
643      top-level Kconfig file. If a file is source'd multiple times, it will
644      appear multiple times. Use set() to get unique filenames.
645
646      Note that Kconfig.sync_deps() already indirectly catches any file
647      modifications that change configuration output.
648
649    env_vars:
650      A set() with the names of all environment variables referenced in the
651      Kconfig files.
652
653      Only environment variables referenced with the preprocessor $(FOO) syntax
654      will be registered. The older $FOO syntax is only supported for backwards
655      compatibility.
656
657      Also note that $(FOO) won't be registered unless the environment variable
658      $FOO is actually set. If it isn't, $(FOO) is an expansion of an unset
659      preprocessor variable (which gives the empty string).
660
661      Another gotcha is that environment variables referenced in the values of
662      recursively expanded preprocessor variables (those defined with =) will
663      only be registered if the variable is actually used (expanded) somewhere.
664
665      The note from the 'kconfig_filenames' documentation applies here too.
666
667    n/m/y:
668      The predefined constant symbols n/m/y. Also available in const_syms.
669
670    modules:
671      The Symbol instance for the modules symbol. Currently hardcoded to
672      MODULES, which is backwards compatible. Kconfiglib will warn if
673      'option modules' is set on some other symbol. Tell me if you need proper
674      'option modules' support.
675
676      'modules' is never None. If the MODULES symbol is not explicitly defined,
677      its tri_value will be 0 (n), as expected.
678
679      A simple way to enable modules is to do 'kconf.modules.set_value(2)'
680      (provided the MODULES symbol is defined and visible). Modules are
681      disabled by default in the kernel Kconfig files as of writing, though
682      nearly all defconfig files enable them (with 'CONFIG_MODULES=y').
683
684    defconfig_list:
685      The Symbol instance for the 'option defconfig_list' symbol, or None if no
686      defconfig_list symbol exists. The defconfig filename derived from this
687      symbol can be found in Kconfig.defconfig_filename.
688
689    defconfig_filename:
690      The filename given by the defconfig_list symbol. This is taken from the
691      first 'default' with a satisfied condition where the specified file
692      exists (can be opened for reading). If a defconfig file foo/defconfig is
693      not found and $srctree was set when the Kconfig was created,
694      $srctree/foo/defconfig is looked up as well.
695
696      'defconfig_filename' is None if either no defconfig_list symbol exists,
697      or if the defconfig_list symbol has no 'default' with a satisfied
698      condition that specifies a file that exists.
699
700      Gotcha: scripts/kconfig/Makefile might pass --defconfig=<defconfig> to
701      scripts/kconfig/conf when running e.g. 'make defconfig'. This option
702      overrides the defconfig_list symbol, meaning defconfig_filename might not
703      always match what 'make defconfig' would use.
704
705    top_node:
706      The menu node (see the MenuNode class) of the implicit top-level menu.
707      Acts as the root of the menu tree.
708
709    mainmenu_text:
710      The prompt (title) of the top menu (top_node). Defaults to "Main menu".
711      Can be changed with the 'mainmenu' statement (see kconfig-language.txt).
712
713    variables:
714      A dictionary with all preprocessor variables, indexed by name. See the
715      Variable class.
716
717    warn:
718      Set this variable to True/False to enable/disable warnings. See
719      Kconfig.__init__().
720
721      When 'warn' is False, the values of the other warning-related variables
722      are ignored.
723
724      This variable as well as the other warn* variables can be read to check
725      the current warning settings.
726
727    warn_to_stderr:
728      Set this variable to True/False to enable/disable warnings on stderr. See
729      Kconfig.__init__().
730
731    warn_assign_undef:
732      Set this variable to True to generate warnings for assignments to
733      undefined symbols in configuration files.
734
735      This variable is False by default unless the KCONFIG_WARN_UNDEF_ASSIGN
736      environment variable was set to 'y' when the Kconfig instance was
737      created.
738
739    warn_assign_override:
740      Set this variable to True to generate warnings for multiple assignments
741      to the same symbol in configuration files, where the assignments set
742      different values (e.g. CONFIG_FOO=m followed by CONFIG_FOO=y, where the
743      last value would get used).
744
745      This variable is True by default. Disabling it might be useful when
746      merging configurations.
747
748    warn_assign_redun:
749      Like warn_assign_override, but for multiple assignments setting a symbol
750      to the same value.
751
752      This variable is True by default. Disabling it might be useful when
753      merging configurations.
754
755    warnings:
756      A list of strings containing all warnings that have been generated, for
757      cases where more flexibility is needed.
758
759      See the 'warn_to_stderr' parameter to Kconfig.__init__() and the
760      Kconfig.warn_to_stderr variable as well. Note that warnings still get
761      added to Kconfig.warnings when 'warn_to_stderr' is True.
762
763      Just as for warnings printed to stderr, only warnings that are enabled
764      will get added to Kconfig.warnings. See the various Kconfig.warn*
765      variables.
766
767    missing_syms:
768      A list with (name, value) tuples for all assignments to undefined symbols
769      within the most recently loaded .config file(s). 'name' is the symbol
770      name without the 'CONFIG_' prefix. 'value' is a string that gives the
771      right-hand side of the assignment verbatim.
772
773      See Kconfig.load_config() as well.
774
775    srctree:
776      The value the $srctree environment variable had when the Kconfig instance
777      was created, or the empty string if $srctree wasn't set. This gives nice
778      behavior with os.path.join(), which treats "" as the current directory,
779      without adding "./".
780
781      Kconfig files are looked up relative to $srctree (unless absolute paths
782      are used), and .config files are looked up relative to $srctree if they
783      are not found in the current directory. This is used to support
784      out-of-tree builds. The C tools use this environment variable in the same
785      way.
786
787      Changing $srctree after creating the Kconfig instance has no effect. Only
788      the value when the configuration is loaded matters. This avoids surprises
789      if multiple configurations are loaded with different values for $srctree.
790
791    config_prefix:
792      The value the CONFIG_ environment variable had when the Kconfig instance
793      was created, or "CONFIG_" if CONFIG_ wasn't set. This is the prefix used
794      (and expected) on symbol names in .config files and C headers. Used in
795      the same way in the C tools.
796
797    config_header:
798      The value the KCONFIG_CONFIG_HEADER environment variable had when the
799      Kconfig instance was created, or the empty string if
800      KCONFIG_CONFIG_HEADER wasn't set. This string is inserted verbatim at the
801      beginning of configuration files. See write_config().
802
803    header_header:
804      The value the KCONFIG_AUTOHEADER_HEADER environment variable had when the
805      Kconfig instance was created, or the empty string if
806      KCONFIG_AUTOHEADER_HEADER wasn't set. This string is inserted verbatim at
807      the beginning of header files. See write_autoconf().
808
809    filename/linenr:
810      The current parsing location, for use in Python preprocessor functions.
811      See the module docstring.
812    """
813    __slots__ = (
814        "_encoding",
815        "_functions",
816        "_set_match",
817        "_srctree_prefix",
818        "_unset_match",
819        "_warn_assign_no_prompt",
820        "choices",
821        "comments",
822        "config_header",
823        "config_prefix",
824        "const_syms",
825        "defconfig_list",
826        "defined_syms",
827        "env_vars",
828        "header_header",
829        "kconfig_filenames",
830        "m",
831        "menus",
832        "missing_syms",
833        "modules",
834        "n",
835        "named_choices",
836        "srctree",
837        "syms",
838        "top_node",
839        "unique_choices",
840        "unique_defined_syms",
841        "variables",
842        "warn",
843        "warn_assign_override",
844        "warn_assign_redun",
845        "warn_assign_undef",
846        "warn_to_stderr",
847        "warnings",
848        "y",
849
850        # Parsing-related
851        "_parsing_kconfigs",
852        "_readline",
853        "filename",
854        "linenr",
855        "_include_path",
856        "_filestack",
857        "_line",
858        "_tokens",
859        "_tokens_i",
860        "_reuse_tokens",
861    )
862
863    #
864    # Public interface
865    #
866
867    def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
868                 encoding="utf-8", suppress_traceback=False):
869        """
870        Creates a new Kconfig object by parsing Kconfig files.
871        Note that Kconfig files are not the same as .config files (which store
872        configuration symbol values).
873
874        See the module docstring for some environment variables that influence
875        default warning settings (KCONFIG_WARN_UNDEF and
876        KCONFIG_WARN_UNDEF_ASSIGN).
877
878        Raises KconfigError on syntax/semantic errors, and OSError or (possibly
879        a subclass of) IOError on IO errors ('errno', 'strerror', and
880        'filename' are available). Note that IOError is an alias for OSError on
881        Python 3, so it's enough to catch OSError there. If you need Python 2/3
882        compatibility, it's easiest to catch EnvironmentError, which is a
883        common base class of OSError/IOError on Python 2 and an alias for
884        OSError on Python 3.
885
886        filename (default: "Kconfig"):
887          The Kconfig file to load. For the Linux kernel, you'll want "Kconfig"
888          from the top-level directory, as environment variables will make sure
889          the right Kconfig is included from there (arch/$SRCARCH/Kconfig as of
890          writing).
891
892          If $srctree is set, 'filename' will be looked up relative to it.
893          $srctree is also used to look up source'd files within Kconfig files.
894          See the class documentation.
895
896          If you are using Kconfiglib via 'make scriptconfig', the filename of
897          the base base Kconfig file will be in sys.argv[1]. It's currently
898          always "Kconfig" in practice.
899
900        warn (default: True):
901          True if warnings related to this configuration should be generated.
902          This can be changed later by setting Kconfig.warn to True/False. It
903          is provided as a constructor argument since warnings might be
904          generated during parsing.
905
906          See the other Kconfig.warn_* variables as well, which enable or
907          suppress certain warnings when warnings are enabled.
908
909          All generated warnings are added to the Kconfig.warnings list. See
910          the class documentation.
911
912        warn_to_stderr (default: True):
913          True if warnings should be printed to stderr in addition to being
914          added to Kconfig.warnings.
915
916          This can be changed later by setting Kconfig.warn_to_stderr to
917          True/False.
918
919        encoding (default: "utf-8"):
920          The encoding to use when reading and writing files, and when decoding
921          output from commands run via $(shell). If None, the encoding
922          specified in the current locale will be used.
923
924          The "utf-8" default avoids exceptions on systems that are configured
925          to use the C locale, which implies an ASCII encoding.
926
927          This parameter has no effect on Python 2, due to implementation
928          issues (regular strings turning into Unicode strings, which are
929          distinct in Python 2). Python 2 doesn't decode regular strings
930          anyway.
931
932          Related PEP: https://www.python.org/dev/peps/pep-0538/
933
934        suppress_traceback (default: False):
935          Helper for tools. When True, any EnvironmentError or KconfigError
936          generated during parsing is caught, the exception message is printed
937          to stderr together with the command name, and sys.exit(1) is called
938          (which generates SystemExit).
939
940          This hides the Python traceback for "expected" errors like syntax
941          errors in Kconfig files.
942
943          Other exceptions besides EnvironmentError and KconfigError are still
944          propagated when suppress_traceback is True.
945        """
946        try:
947            self._init(filename, warn, warn_to_stderr, encoding)
948        except (EnvironmentError, KconfigError) as e:
949            if suppress_traceback:
950                cmd = sys.argv[0]  # Empty string if missing
951                if cmd:
952                    cmd += ": "
953                # Some long exception messages have extra newlines for better
954                # formatting when reported as an unhandled exception. Strip
955                # them here.
956                sys.exit(cmd + str(e).strip())
957            raise
958
959    def _init(self, filename, warn, warn_to_stderr, encoding):
960        # See __init__()
961
962        self._encoding = encoding
963
964        self.srctree = os.getenv("srctree", "")
965        # A prefix we can reliably strip from glob() results to get a filename
966        # relative to $srctree. relpath() can cause issues for symlinks,
967        # because it assumes symlink/../foo is the same as foo/.
968        self._srctree_prefix = realpath(self.srctree) + os.sep
969
970        self.warn = warn
971        self.warn_to_stderr = warn_to_stderr
972        self.warn_assign_undef = os.getenv("KCONFIG_WARN_UNDEF_ASSIGN") == "y"
973        self.warn_assign_override = True
974        self.warn_assign_redun = True
975        self._warn_assign_no_prompt = True
976
977        self.warnings = []
978
979        self.config_prefix = os.getenv("CONFIG_", "CONFIG_")
980        # Regular expressions for parsing .config files
981        self._set_match = _re_match(self.config_prefix + r"([^=]+)=(.*)")
982        self._unset_match = _re_match(r"# {}([^ ]+) is not set".format(
983            self.config_prefix))
984
985        self.config_header = os.getenv("KCONFIG_CONFIG_HEADER", "")
986        self.header_header = os.getenv("KCONFIG_AUTOHEADER_HEADER", "")
987
988        self.syms = {}
989        self.const_syms = {}
990        self.defined_syms = []
991        self.missing_syms = []
992        self.named_choices = {}
993        self.choices = []
994        self.menus = []
995        self.comments = []
996
997        for nmy in "n", "m", "y":
998            sym = Symbol()
999            sym.kconfig = self
1000            sym.name = nmy
1001            sym.is_constant = True
1002            sym.orig_type = TRISTATE
1003            sym._cached_tri_val = STR_TO_TRI[nmy]
1004
1005            self.const_syms[nmy] = sym
1006
1007        self.n = self.const_syms["n"]
1008        self.m = self.const_syms["m"]
1009        self.y = self.const_syms["y"]
1010
1011        # Make n/m/y well-formed symbols
1012        for nmy in "n", "m", "y":
1013            sym = self.const_syms[nmy]
1014            sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
1015
1016        # Maps preprocessor variables names to Variable instances
1017        self.variables = {}
1018
1019        # Predefined preprocessor functions, with min/max number of arguments
1020        self._functions = {
1021            "info":       (_info_fn,       1, 1),
1022            "error-if":   (_error_if_fn,   2, 2),
1023            "filename":   (_filename_fn,   0, 0),
1024            "lineno":     (_lineno_fn,     0, 0),
1025            "shell":      (_shell_fn,      1, 1),
1026            "warning-if": (_warning_if_fn, 2, 2),
1027        }
1028
1029        # Add any user-defined preprocessor functions
1030        try:
1031            self._functions.update(
1032                importlib.import_module(
1033                    os.getenv("KCONFIG_FUNCTIONS", "kconfigfunctions")
1034                ).functions)
1035        except ImportError:
1036            pass
1037
1038        # This determines whether previously unseen symbols are registered.
1039        # They shouldn't be if we parse expressions after parsing, as part of
1040        # Kconfig.eval_string().
1041        self._parsing_kconfigs = True
1042
1043        self.modules = self._lookup_sym("MODULES")
1044        self.defconfig_list = None
1045
1046        self.top_node = MenuNode()
1047        self.top_node.kconfig = self
1048        self.top_node.item = MENU
1049        self.top_node.is_menuconfig = True
1050        self.top_node.visibility = self.y
1051        self.top_node.prompt = ("Main menu", self.y)
1052        self.top_node.parent = None
1053        self.top_node.dep = self.y
1054        self.top_node.filename = filename
1055        self.top_node.linenr = 1
1056        self.top_node.include_path = ()
1057
1058        # Parse the Kconfig files
1059
1060        # Not used internally. Provided as a convenience.
1061        self.kconfig_filenames = [filename]
1062        self.env_vars = set()
1063
1064        # Keeps track of the location in the parent Kconfig files. Kconfig
1065        # files usually source other Kconfig files. See _enter_file().
1066        self._filestack = []
1067        self._include_path = ()
1068
1069        # The current parsing location
1070        self.filename = filename
1071        self.linenr = 0
1072
1073        # Used to avoid retokenizing lines when we discover that they're not
1074        # part of the construct currently being parsed. This is kinda like an
1075        # unget operation.
1076        self._reuse_tokens = False
1077
1078        # Open the top-level Kconfig file. Store the readline() method directly
1079        # as a small optimization.
1080        self._readline = self._open(join(self.srctree, filename), "r").readline
1081
1082        try:
1083            # Parse the Kconfig files. Returns the last node, which we
1084            # terminate with '.next = None'.
1085            self._parse_block(None, self.top_node, self.top_node).next = None
1086            self.top_node.list = self.top_node.next
1087            self.top_node.next = None
1088        except UnicodeDecodeError as e:
1089            _decoding_error(e, self.filename)
1090
1091        # Close the top-level Kconfig file. __self__ fetches the 'file' object
1092        # for the method.
1093        self._readline.__self__.close()
1094
1095        self._parsing_kconfigs = False
1096
1097        # Do various menu tree post-processing
1098        self._finalize_node(self.top_node, self.y)
1099
1100        self.unique_defined_syms = _ordered_unique(self.defined_syms)
1101        self.unique_choices = _ordered_unique(self.choices)
1102
1103        # Do sanity checks. Some of these depend on everything being finalized.
1104        self._check_sym_sanity()
1105        self._check_choice_sanity()
1106
1107        # KCONFIG_STRICT is an older alias for KCONFIG_WARN_UNDEF, supported
1108        # for backwards compatibility
1109        if os.getenv("KCONFIG_WARN_UNDEF") == "y" or \
1110           os.getenv("KCONFIG_STRICT") == "y":
1111
1112            self._check_undef_syms()
1113
1114        # Build Symbol._dependents for all symbols and choices
1115        self._build_dep()
1116
1117        # Check for dependency loops
1118        check_dep_loop_sym = _check_dep_loop_sym  # Micro-optimization
1119        for sym in self.unique_defined_syms:
1120            check_dep_loop_sym(sym, False)
1121
1122        # Add extra dependencies from choices to choice symbols that get
1123        # awkward during dependency loop detection
1124        self._add_choice_deps()
1125
1126    @property
1127    def mainmenu_text(self):
1128        """
1129        See the class documentation.
1130        """
1131        return self.top_node.prompt[0]
1132
1133    @property
1134    def defconfig_filename(self):
1135        """
1136        See the class documentation.
1137        """
1138        if self.defconfig_list:
1139            for filename, cond in self.defconfig_list.defaults:
1140                if expr_value(cond):
1141                    try:
1142                        with self._open_config(filename.str_value) as f:
1143                            return f.name
1144                    except EnvironmentError:
1145                        continue
1146
1147        return None
1148
1149    def load_config(self, filename=None, replace=True, verbose=None):
1150        """
1151        Loads symbol values from a file in the .config format. Equivalent to
1152        calling Symbol.set_value() to set each of the values.
1153
1154        "# CONFIG_FOO is not set" within a .config file sets the user value of
1155        FOO to n. The C tools work the same way.
1156
1157        For each symbol, the Symbol.user_value attribute holds the value the
1158        symbol was assigned in the .config file (if any). The user value might
1159        differ from Symbol.str/tri_value if there are unsatisfied dependencies.
1160
1161        Calling this function also updates the Kconfig.missing_syms attribute
1162        with a list of all assignments to undefined symbols within the
1163        configuration file. Kconfig.missing_syms is cleared if 'replace' is
1164        True, and appended to otherwise. See the documentation for
1165        Kconfig.missing_syms as well.
1166
1167        See the Kconfig.__init__() docstring for raised exceptions
1168        (OSError/IOError). KconfigError is never raised here.
1169
1170        filename (default: None):
1171          Path to load configuration from (a string). Respects $srctree if set
1172          (see the class documentation).
1173
1174          If 'filename' is None (the default), the configuration file to load
1175          (if any) is calculated automatically, giving the behavior you'd
1176          usually want:
1177
1178            1. If the KCONFIG_CONFIG environment variable is set, it gives the
1179               path to the configuration file to load. Otherwise, ".config" is
1180               used. See standard_config_filename().
1181
1182            2. If the path from (1.) doesn't exist, the configuration file
1183               given by kconf.defconfig_filename is loaded instead, which is
1184               derived from the 'option defconfig_list' symbol.
1185
1186            3. If (1.) and (2.) fail to find a configuration file to load, no
1187               configuration file is loaded, and symbols retain their current
1188               values (e.g., their default values). This is not an error.
1189
1190           See the return value as well.
1191
1192        replace (default: True):
1193          If True, all existing user values will be cleared before loading the
1194          .config. Pass False to merge configurations.
1195
1196        verbose (default: None):
1197          Limited backwards compatibility to prevent crashes. A warning is
1198          printed if anything but None is passed.
1199
1200          Prior to Kconfiglib 12.0.0, this option enabled printing of messages
1201          to stdout when 'filename' was None. A message is (always) returned
1202          now instead, which is more flexible.
1203
1204          Will probably be removed in some future version.
1205
1206        Returns a string with a message saying which file got loaded (or
1207        possibly that no file got loaded, when 'filename' is None). This is
1208        meant to reduce boilerplate in tools, which can do e.g.
1209        print(kconf.load_config()). The returned message distinguishes between
1210        loading (replace == True) and merging (replace == False).
1211        """
1212        if verbose is not None:
1213            _warn_verbose_deprecated("load_config")
1214
1215        msg = None
1216        if filename is None:
1217            filename = standard_config_filename()
1218            if not exists(filename) and \
1219               not exists(join(self.srctree, filename)):
1220                defconfig = self.defconfig_filename
1221                if defconfig is None:
1222                    return "Using default symbol values (no '{}')" \
1223                           .format(filename)
1224
1225                msg = " default configuration '{}' (no '{}')" \
1226                      .format(defconfig, filename)
1227                filename = defconfig
1228
1229        if not msg:
1230            msg = " configuration '{}'".format(filename)
1231
1232        # Disable the warning about assigning to symbols without prompts. This
1233        # is normal and expected within a .config file.
1234        self._warn_assign_no_prompt = False
1235
1236        # This stub only exists to make sure _warn_assign_no_prompt gets
1237        # reenabled
1238        try:
1239            self._load_config(filename, replace)
1240        except UnicodeDecodeError as e:
1241            _decoding_error(e, filename)
1242        finally:
1243            self._warn_assign_no_prompt = True
1244
1245        return ("Loaded" if replace else "Merged") + msg
1246
1247    def _load_config(self, filename, replace):
1248        with self._open_config(filename) as f:
1249            if replace:
1250                self.missing_syms = []
1251
1252                # If we're replacing the configuration, keep track of which
1253                # symbols and choices got set so that we can unset the rest
1254                # later. This avoids invalidating everything and is faster.
1255                # Another benefit is that invalidation must be rock solid for
1256                # it to work, making it a good test.
1257
1258                for sym in self.unique_defined_syms:
1259                    sym._was_set = False
1260
1261                for choice in self.unique_choices:
1262                    choice._was_set = False
1263
1264            # Small optimizations
1265            set_match = self._set_match
1266            unset_match = self._unset_match
1267            get_sym = self.syms.get
1268
1269            for linenr, line in enumerate(f, 1):
1270                # The C tools ignore trailing whitespace
1271                line = line.rstrip()
1272
1273                match = set_match(line)
1274                if match:
1275                    name, val = match.groups()
1276                    sym = get_sym(name)
1277                    if not sym or not sym.nodes:
1278                        self._undef_assign(name, val, filename, linenr)
1279                        continue
1280
1281                    if sym.orig_type in _BOOL_TRISTATE:
1282                        # The C implementation only checks the first character
1283                        # to the right of '=', for whatever reason
1284                        if not (sym.orig_type is BOOL
1285                                and val.startswith(("y", "n")) or
1286                                sym.orig_type is TRISTATE
1287                                and val.startswith(("y", "m", "n"))):
1288                            self._warn("'{}' is not a valid value for the {} "
1289                                       "symbol {}. Assignment ignored."
1290                                       .format(val, TYPE_TO_STR[sym.orig_type],
1291                                               sym.name_and_loc),
1292                                       filename, linenr)
1293                            continue
1294
1295                        val = val[0]
1296
1297                        if sym.choice and val != "n":
1298                            # During .config loading, we infer the mode of the
1299                            # choice from the kind of values that are assigned
1300                            # to the choice symbols
1301
1302                            prev_mode = sym.choice.user_value
1303                            if prev_mode is not None and \
1304                               TRI_TO_STR[prev_mode] != val:
1305
1306                                self._warn("both m and y assigned to symbols "
1307                                           "within the same choice",
1308                                           filename, linenr)
1309
1310                            # Set the choice's mode
1311                            sym.choice.set_value(val)
1312
1313                    elif sym.orig_type is STRING:
1314                        match = _conf_string_match(val)
1315                        if not match:
1316                            self._warn("malformed string literal in "
1317                                       "assignment to {}. Assignment ignored."
1318                                       .format(sym.name_and_loc),
1319                                       filename, linenr)
1320                            continue
1321
1322                        val = unescape(match.group(1))
1323
1324                else:
1325                    match = unset_match(line)
1326                    if not match:
1327                        # Print a warning for lines that match neither
1328                        # set_match() nor unset_match() and that are not blank
1329                        # lines or comments. 'line' has already been
1330                        # rstrip()'d, so blank lines show up as "" here.
1331                        if line and not line.lstrip().startswith("#"):
1332                            self._warn("ignoring malformed line '{}'"
1333                                       .format(line),
1334                                       filename, linenr)
1335
1336                        continue
1337
1338                    name = match.group(1)
1339                    sym = get_sym(name)
1340                    if not sym or not sym.nodes:
1341                        self._undef_assign(name, "n", filename, linenr)
1342                        continue
1343
1344                    if sym.orig_type not in _BOOL_TRISTATE:
1345                        continue
1346
1347                    val = "n"
1348
1349                # Done parsing the assignment. Set the value.
1350
1351                if sym._was_set:
1352                    self._assigned_twice(sym, val, filename, linenr)
1353
1354                sym.set_value(val)
1355
1356        if replace:
1357            # If we're replacing the configuration, unset the symbols that
1358            # didn't get set
1359
1360            for sym in self.unique_defined_syms:
1361                if not sym._was_set:
1362                    sym.unset_value()
1363
1364            for choice in self.unique_choices:
1365                if not choice._was_set:
1366                    choice.unset_value()
1367
1368    def _undef_assign(self, name, val, filename, linenr):
1369        # Called for assignments to undefined symbols during .config loading
1370
1371        self.missing_syms.append((name, val))
1372        if self.warn_assign_undef:
1373            self._warn(
1374                "attempt to assign the value '{}' to the undefined symbol {}"
1375                .format(val, name), filename, linenr)
1376
1377    def _assigned_twice(self, sym, new_val, filename, linenr):
1378        # Called when a symbol is assigned more than once in a .config file
1379
1380        # Use strings for bool/tristate user values in the warning
1381        if sym.orig_type in _BOOL_TRISTATE:
1382            user_val = TRI_TO_STR[sym.user_value]
1383        else:
1384            user_val = sym.user_value
1385
1386        msg = '{} set more than once. Old value "{}", new value "{}".'.format(
1387            sym.name_and_loc, user_val, new_val)
1388
1389        if user_val == new_val:
1390            if self.warn_assign_redun:
1391                self._warn(msg, filename, linenr)
1392        elif self.warn_assign_override:
1393            self._warn(msg, filename, linenr)
1394
1395    def load_allconfig(self, filename):
1396        """
1397        Helper for all*config. Loads (merges) the configuration file specified
1398        by KCONFIG_ALLCONFIG, if any. See Documentation/kbuild/kconfig.txt in
1399        the Linux kernel.
1400
1401        Disables warnings for duplicated assignments within configuration files
1402        for the duration of the call
1403        (kconf.warn_assign_override/warn_assign_redun = False), and restores
1404        the previous warning settings at the end. The KCONFIG_ALLCONFIG
1405        configuration file is expected to override symbols.
1406
1407        Exits with sys.exit() (which raises a SystemExit exception) and prints
1408        an error to stderr if KCONFIG_ALLCONFIG is set but the configuration
1409        file can't be opened.
1410
1411        filename:
1412          Command-specific configuration filename - "allyes.config",
1413          "allno.config", etc.
1414        """
1415        load_allconfig(self, filename)
1416
1417    def write_autoconf(self, filename=None, header=None):
1418        r"""
1419        Writes out symbol values as a C header file, matching the format used
1420        by include/generated/autoconf.h in the kernel.
1421
1422        The ordering of the #defines matches the one generated by
1423        write_config(). The order in the C implementation depends on the hash
1424        table implementation as of writing, and so won't match.
1425
1426        If 'filename' exists and its contents is identical to what would get
1427        written out, it is left untouched. This avoids updating file metadata
1428        like the modification time and possibly triggering redundant work in
1429        build tools.
1430
1431        filename (default: None):
1432          Path to write header to.
1433
1434          If None (the default), the path in the environment variable
1435          KCONFIG_AUTOHEADER is used if set, and "include/generated/autoconf.h"
1436          otherwise. This is compatible with the C tools.
1437
1438        header (default: None):
1439          Text inserted verbatim at the beginning of the file. You would
1440          usually want it enclosed in '/* */' to make it a C comment, and
1441          include a trailing newline.
1442
1443          If None (the default), the value of the environment variable
1444          KCONFIG_AUTOHEADER_HEADER had when the Kconfig instance was created
1445          will be used if it was set, and no header otherwise. See the
1446          Kconfig.header_header attribute.
1447
1448        Returns a string with a message saying that the header got saved, or
1449        that there were no changes to it. This is meant to reduce boilerplate
1450        in tools, which can do e.g. print(kconf.write_autoconf()).
1451        """
1452        if filename is None:
1453            filename = os.getenv("KCONFIG_AUTOHEADER",
1454                                 "include/generated/autoconf.h")
1455
1456        if self._write_if_changed(filename, self._autoconf_contents(header)):
1457            return "Kconfig header saved to '{}'".format(filename)
1458        return "No change to Kconfig header in '{}'".format(filename)
1459
1460    def _autoconf_contents(self, header):
1461        # write_autoconf() helper. Returns the contents to write as a string,
1462        # with 'header' or KCONFIG_AUTOHEADER_HEADER at the beginning.
1463
1464        if header is None:
1465            header = self.header_header
1466
1467        chunks = [header]  # "".join()ed later
1468        add = chunks.append
1469
1470        for sym in self.unique_defined_syms:
1471            # _write_to_conf is determined when the value is calculated. This
1472            # is a hidden function call due to property magic.
1473            #
1474            # Note: In client code, you can check if sym.config_string is empty
1475            # instead, to avoid accessing the internal _write_to_conf variable
1476            # (though it's likely to keep working).
1477            val = sym.str_value
1478            if not sym._write_to_conf:
1479                continue
1480
1481            if sym.orig_type in _BOOL_TRISTATE:
1482                if val == "y":
1483                    add("#define {}{} 1\n"
1484                        .format(self.config_prefix, sym.name))
1485                elif val == "m":
1486                    add("#define {}{}_MODULE 1\n"
1487                        .format(self.config_prefix, sym.name))
1488
1489            elif sym.orig_type is STRING:
1490                add('#define {}{} "{}"\n'
1491                    .format(self.config_prefix, sym.name, escape(val)))
1492
1493            else:  # sym.orig_type in _INT_HEX:
1494                if sym.orig_type is HEX and \
1495                   not val.startswith(("0x", "0X")):
1496                    val = "0x" + val
1497
1498                add("#define {}{} {}\n"
1499                    .format(self.config_prefix, sym.name, val))
1500
1501        return "".join(chunks)
1502
1503    def write_config(self, filename=None, header=None, save_old=True,
1504                     verbose=None):
1505        r"""
1506        Writes out symbol values in the .config format. The format matches the
1507        C implementation, including ordering.
1508
1509        Symbols appear in the same order in generated .config files as they do
1510        in the Kconfig files. For symbols defined in multiple locations, a
1511        single assignment is written out corresponding to the first location
1512        where the symbol is defined.
1513
1514        See the 'Intro to symbol values' section in the module docstring to
1515        understand which symbols get written out.
1516
1517        If 'filename' exists and its contents is identical to what would get
1518        written out, it is left untouched. This avoids updating file metadata
1519        like the modification time and possibly triggering redundant work in
1520        build tools.
1521
1522        See the Kconfig.__init__() docstring for raised exceptions
1523        (OSError/IOError). KconfigError is never raised here.
1524
1525        filename (default: None):
1526          Path to write configuration to (a string).
1527
1528          If None (the default), the path in the environment variable
1529          KCONFIG_CONFIG is used if set, and ".config" otherwise. See
1530          standard_config_filename().
1531
1532        header (default: None):
1533          Text inserted verbatim at the beginning of the file. You would
1534          usually want each line to start with '#' to make it a comment, and
1535          include a trailing newline.
1536
1537          if None (the default), the value of the environment variable
1538          KCONFIG_CONFIG_HEADER had when the Kconfig instance was created will
1539          be used if it was set, and no header otherwise. See the
1540          Kconfig.config_header attribute.
1541
1542        save_old (default: True):
1543          If True and <filename> already exists, a copy of it will be saved to
1544          <filename>.old in the same directory before the new configuration is
1545          written.
1546
1547          Errors are silently ignored if <filename>.old cannot be written (e.g.
1548          due to permissions errors).
1549
1550        verbose (default: None):
1551          Limited backwards compatibility to prevent crashes. A warning is
1552          printed if anything but None is passed.
1553
1554          Prior to Kconfiglib 12.0.0, this option enabled printing of messages
1555          to stdout when 'filename' was None. A message is (always) returned
1556          now instead, which is more flexible.
1557
1558          Will probably be removed in some future version.
1559
1560        Returns a string with a message saying which file got saved. This is
1561        meant to reduce boilerplate in tools, which can do e.g.
1562        print(kconf.write_config()).
1563        """
1564        if verbose is not None:
1565            _warn_verbose_deprecated("write_config")
1566
1567        if filename is None:
1568            filename = standard_config_filename()
1569
1570        contents = self._config_contents(header)
1571        if self._contents_eq(filename, contents):
1572            return "No change to configuration in '{}'".format(filename)
1573
1574        if save_old:
1575            _save_old(filename)
1576
1577        with self._open(filename, "w") as f:
1578            f.write(contents)
1579
1580        return "Configuration saved to '{}'".format(filename)
1581
1582    def _config_contents(self, header):
1583        # write_config() helper. Returns the contents to write as a string,
1584        # with 'header' or KCONFIG_CONFIG_HEADER at the beginning.
1585        #
1586        # More memory friendly would be to 'yield' the strings and
1587        # "".join(_config_contents()), but it was a bit slower on my system.
1588
1589        # node_iter() was used here before commit 3aea9f7 ("Add '# end of
1590        # <menu>' after menus in .config"). Those comments get tricky to
1591        # implement with it.
1592
1593        for sym in self.unique_defined_syms:
1594            sym._visited = False
1595
1596        if header is None:
1597            header = self.config_header
1598
1599        chunks = [header]  # "".join()ed later
1600        add = chunks.append
1601
1602        # Did we just print an '# end of ...' comment?
1603        after_end_comment = False
1604
1605        node = self.top_node
1606        while 1:
1607            # Jump to the next node with an iterative tree walk
1608            if node.list:
1609                node = node.list
1610            elif node.next:
1611                node = node.next
1612            else:
1613                while node.parent:
1614                    node = node.parent
1615
1616                    # Add a comment when leaving visible menus
1617                    if node.item is MENU and expr_value(node.dep) and \
1618                       expr_value(node.visibility) and \
1619                       node is not self.top_node:
1620                        add("# end of {}\n".format(node.prompt[0]))
1621                        after_end_comment = True
1622
1623                    if node.next:
1624                        node = node.next
1625                        break
1626                else:
1627                    # No more nodes
1628                    return "".join(chunks)
1629
1630            # Generate configuration output for the node
1631
1632            item = node.item
1633
1634            if item.__class__ is Symbol:
1635                if item._visited:
1636                    continue
1637                item._visited = True
1638
1639                conf_string = item.config_string
1640                if not conf_string:
1641                    continue
1642
1643                if after_end_comment:
1644                    # Add a blank line before the first symbol printed after an
1645                    # '# end of ...' comment
1646                    after_end_comment = False
1647                    add("\n")
1648                add(conf_string)
1649
1650            elif expr_value(node.dep) and \
1651                 ((item is MENU and expr_value(node.visibility)) or
1652                  item is COMMENT):
1653
1654                add("\n#\n# {}\n#\n".format(node.prompt[0]))
1655                after_end_comment = False
1656
1657    def write_min_config(self, filename, header=None):
1658        """
1659        Writes out a "minimal" configuration file, omitting symbols whose value
1660        matches their default value. The format matches the one produced by
1661        'make savedefconfig'.
1662
1663        The resulting configuration file is incomplete, but a complete
1664        configuration can be derived from it by loading it. Minimal
1665        configuration files can serve as a more manageable configuration format
1666        compared to a "full" .config file, especially when configurations files
1667        are merged or edited by hand.
1668
1669        See the Kconfig.__init__() docstring for raised exceptions
1670        (OSError/IOError). KconfigError is never raised here.
1671
1672        filename:
1673          Path to write minimal configuration to.
1674
1675        header (default: None):
1676          Text inserted verbatim at the beginning of the file. You would
1677          usually want each line to start with '#' to make it a comment, and
1678          include a final terminating newline.
1679
1680          if None (the default), the value of the environment variable
1681          KCONFIG_CONFIG_HEADER had when the Kconfig instance was created will
1682          be used if it was set, and no header otherwise. See the
1683          Kconfig.config_header attribute.
1684
1685        Returns a string with a message saying the minimal configuration got
1686        saved, or that there were no changes to it. This is meant to reduce
1687        boilerplate in tools, which can do e.g.
1688        print(kconf.write_min_config()).
1689        """
1690        if self._write_if_changed(filename, self._min_config_contents(header)):
1691            return "Minimal configuration saved to '{}'".format(filename)
1692        return "No change to minimal configuration in '{}'".format(filename)
1693
1694    def _min_config_contents(self, header):
1695        # write_min_config() helper. Returns the contents to write as a string,
1696        # with 'header' or KCONFIG_CONFIG_HEADER at the beginning.
1697
1698        if header is None:
1699            header = self.config_header
1700
1701        chunks = [header]  # "".join()ed later
1702        add = chunks.append
1703
1704        for sym in self.unique_defined_syms:
1705            # Skip symbols that cannot be changed. Only check
1706            # non-choice symbols, as selects don't affect choice
1707            # symbols.
1708            if not sym.choice and \
1709               sym.visibility <= expr_value(sym.rev_dep):
1710                continue
1711
1712            # Skip symbols whose value matches their default
1713            if sym.str_value == sym._str_default():
1714                continue
1715
1716            # Skip symbols that would be selected by default in a
1717            # choice, unless the choice is optional or the symbol type
1718            # isn't bool (it might be possible to set the choice mode
1719            # to n or the symbol to m in those cases).
1720            if sym.choice and \
1721               not sym.choice.is_optional and \
1722               sym.choice._selection_from_defaults() is sym and \
1723               sym.orig_type is BOOL and \
1724               sym.tri_value == 2:
1725                continue
1726
1727            add(sym.config_string)
1728
1729        return "".join(chunks)
1730
1731    def sync_deps(self, path):
1732        """
1733        Creates or updates a directory structure that can be used to avoid
1734        doing a full rebuild whenever the configuration is changed, mirroring
1735        include/config/ in the kernel.
1736
1737        This function is intended to be called during each build, before
1738        compiling source files that depend on configuration symbols.
1739
1740        See the Kconfig.__init__() docstring for raised exceptions
1741        (OSError/IOError). KconfigError is never raised here.
1742
1743        path:
1744          Path to directory
1745
1746        sync_deps(path) does the following:
1747
1748          1. If the directory <path> does not exist, it is created.
1749
1750          2. If <path>/auto.conf exists, old symbol values are loaded from it,
1751             which are then compared against the current symbol values. If a
1752             symbol has changed value (would generate different output in
1753             autoconf.h compared to before), the change is signaled by
1754             touch'ing a file corresponding to the symbol.
1755
1756             The first time sync_deps() is run on a directory, <path>/auto.conf
1757             won't exist, and no old symbol values will be available. This
1758             logically has the same effect as updating the entire
1759             configuration.
1760
1761             The path to a symbol's file is calculated from the symbol's name
1762             by replacing all '_' with '/' and appending '.h'. For example, the
1763             symbol FOO_BAR_BAZ gets the file <path>/foo/bar/baz.h, and FOO
1764             gets the file <path>/foo.h.
1765
1766             This scheme matches the C tools. The point is to avoid having a
1767             single directory with a huge number of files, which the underlying
1768             filesystem might not handle well.
1769
1770          3. A new auto.conf with the current symbol values is written, to keep
1771             track of them for the next build.
1772
1773             If auto.conf exists and its contents is identical to what would
1774             get written out, it is left untouched. This avoids updating file
1775             metadata like the modification time and possibly triggering
1776             redundant work in build tools.
1777
1778
1779        The last piece of the puzzle is knowing what symbols each source file
1780        depends on. Knowing that, dependencies can be added from source files
1781        to the files corresponding to the symbols they depends on. The source
1782        file will then get recompiled (only) when the symbol value changes
1783        (provided sync_deps() is run first during each build).
1784
1785        The tool in the kernel that extracts symbol dependencies from source
1786        files is scripts/basic/fixdep.c. Missing symbol files also correspond
1787        to "not changed", which fixdep deals with by using the $(wildcard) Make
1788        function when adding symbol prerequisites to source files.
1789
1790        In case you need a different scheme for your project, the sync_deps()
1791        implementation can be used as a template.
1792        """
1793        if not exists(path):
1794            os.mkdir(path, 0o755)
1795
1796        # Load old values from auto.conf, if any
1797        self._load_old_vals(path)
1798
1799        for sym in self.unique_defined_syms:
1800            # _write_to_conf is determined when the value is calculated. This
1801            # is a hidden function call due to property magic.
1802            #
1803            # Note: In client code, you can check if sym.config_string is empty
1804            # instead, to avoid accessing the internal _write_to_conf variable
1805            # (though it's likely to keep working).
1806            val = sym.str_value
1807
1808            # n tristate values do not get written to auto.conf and autoconf.h,
1809            # making a missing symbol logically equivalent to n
1810
1811            if sym._write_to_conf:
1812                if sym._old_val is None and \
1813                   sym.orig_type in _BOOL_TRISTATE and \
1814                   val == "n":
1815                    # No old value (the symbol was missing or n), new value n.
1816                    # No change.
1817                    continue
1818
1819                if val == sym._old_val:
1820                    # New value matches old. No change.
1821                    continue
1822
1823            elif sym._old_val is None:
1824                # The symbol wouldn't appear in autoconf.h (because
1825                # _write_to_conf is false), and it wouldn't have appeared in
1826                # autoconf.h previously either (because it didn't appear in
1827                # auto.conf). No change.
1828                continue
1829
1830            # 'sym' has a new value. Flag it.
1831            _touch_dep_file(path, sym.name)
1832
1833        # Remember the current values as the "new old" values.
1834        #
1835        # This call could go anywhere after the call to _load_old_vals(), but
1836        # putting it last means _sync_deps() can be safely rerun if it fails
1837        # before this point.
1838        self._write_old_vals(path)
1839
1840    def _load_old_vals(self, path):
1841        # Loads old symbol values from auto.conf into a dedicated
1842        # Symbol._old_val field. Mirrors load_config().
1843        #
1844        # The extra field could be avoided with some trickery involving dumping
1845        # symbol values and restoring them later, but this is simpler and
1846        # faster. The C tools also use a dedicated field for this purpose.
1847
1848        for sym in self.unique_defined_syms:
1849            sym._old_val = None
1850
1851        try:
1852            auto_conf = self._open(join(path, "auto.conf"), "r")
1853        except EnvironmentError as e:
1854            if e.errno == errno.ENOENT:
1855                # No old values
1856                return
1857            raise
1858
1859        with auto_conf as f:
1860            for line in f:
1861                match = self._set_match(line)
1862                if not match:
1863                    # We only expect CONFIG_FOO=... (and possibly a header
1864                    # comment) in auto.conf
1865                    continue
1866
1867                name, val = match.groups()
1868                if name in self.syms:
1869                    sym = self.syms[name]
1870
1871                    if sym.orig_type is STRING:
1872                        match = _conf_string_match(val)
1873                        if not match:
1874                            continue
1875                        val = unescape(match.group(1))
1876
1877                    self.syms[name]._old_val = val
1878                else:
1879                    # Flag that the symbol no longer exists, in
1880                    # case something still depends on it
1881                    _touch_dep_file(path, name)
1882
1883    def _write_old_vals(self, path):
1884        # Helper for writing auto.conf. Basically just a simplified
1885        # write_config() that doesn't write any comments (including
1886        # '# CONFIG_FOO is not set' comments). The format matches the C
1887        # implementation, though the ordering is arbitrary there (depends on
1888        # the hash table implementation).
1889        #
1890        # A separate helper function is neater than complicating write_config()
1891        # by passing a flag to it, plus we only need to look at symbols here.
1892
1893        self._write_if_changed(
1894            os.path.join(path, "auto.conf"),
1895            self._old_vals_contents())
1896
1897    def _old_vals_contents(self):
1898        # _write_old_vals() helper. Returns the contents to write as a string.
1899
1900        # Temporary list instead of generator makes this a bit faster
1901        return "".join([
1902            sym.config_string for sym in self.unique_defined_syms
1903                if not (sym.orig_type in _BOOL_TRISTATE and not sym.tri_value)
1904        ])
1905
1906    def node_iter(self, unique_syms=False):
1907        """
1908        Returns a generator for iterating through all MenuNode's in the Kconfig
1909        tree. The iteration is done in Kconfig definition order (each node is
1910        visited before its children, and the children of a node are visited
1911        before the next node).
1912
1913        The Kconfig.top_node menu node is skipped. It contains an implicit menu
1914        that holds the top-level items.
1915
1916        As an example, the following code will produce a list equal to
1917        Kconfig.defined_syms:
1918
1919          defined_syms = [node.item for node in kconf.node_iter()
1920                          if isinstance(node.item, Symbol)]
1921
1922        unique_syms (default: False):
1923          If True, only the first MenuNode will be included for symbols defined
1924          in multiple locations.
1925
1926          Using kconf.node_iter(True) in the example above would give a list
1927          equal to unique_defined_syms.
1928        """
1929        if unique_syms:
1930            for sym in self.unique_defined_syms:
1931                sym._visited = False
1932
1933        node = self.top_node
1934        while 1:
1935            # Jump to the next node with an iterative tree walk
1936            if node.list:
1937                node = node.list
1938            elif node.next:
1939                node = node.next
1940            else:
1941                while node.parent:
1942                    node = node.parent
1943                    if node.next:
1944                        node = node.next
1945                        break
1946                else:
1947                    # No more nodes
1948                    return
1949
1950            if unique_syms and node.item.__class__ is Symbol:
1951                if node.item._visited:
1952                    continue
1953                node.item._visited = True
1954
1955            yield node
1956
1957    def eval_string(self, s):
1958        """
1959        Returns the tristate value of the expression 's', represented as 0, 1,
1960        and 2 for n, m, and y, respectively. Raises KconfigError on syntax
1961        errors. Warns if undefined symbols are referenced.
1962
1963        As an example, if FOO and BAR are tristate symbols at least one of
1964        which has the value y, then eval_string("y && (FOO || BAR)") returns
1965        2 (y).
1966
1967        To get the string value of non-bool/tristate symbols, use
1968        Symbol.str_value. eval_string() always returns a tristate value, and
1969        all non-bool/tristate symbols have the tristate value 0 (n).
1970
1971        The expression parsing is consistent with how parsing works for
1972        conditional ('if ...') expressions in the configuration, and matches
1973        the C implementation. m is rewritten to 'm && MODULES', so
1974        eval_string("m") will return 0 (n) unless modules are enabled.
1975        """
1976        # The parser is optimized to be fast when parsing Kconfig files (where
1977        # an expression can never appear at the beginning of a line). We have
1978        # to monkey-patch things a bit here to reuse it.
1979
1980        self.filename = None
1981
1982        self._tokens = self._tokenize("if " + s)
1983        # Strip "if " to avoid giving confusing error messages
1984        self._line = s
1985        self._tokens_i = 1  # Skip the 'if' token
1986
1987        return expr_value(self._expect_expr_and_eol())
1988
1989    def unset_values(self):
1990        """
1991        Removes any user values from all symbols, as if Kconfig.load_config()
1992        or Symbol.set_value() had never been called.
1993        """
1994        self._warn_assign_no_prompt = False
1995        try:
1996            # set_value() already rejects undefined symbols, and they don't
1997            # need to be invalidated (because their value never changes), so we
1998            # can just iterate over defined symbols
1999            for sym in self.unique_defined_syms:
2000                sym.unset_value()
2001
2002            for choice in self.unique_choices:
2003                choice.unset_value()
2004        finally:
2005            self._warn_assign_no_prompt = True
2006
2007    def enable_warnings(self):
2008        """
2009        Do 'Kconfig.warn = True' instead. Maintained for backwards
2010        compatibility.
2011        """
2012        self.warn = True
2013
2014    def disable_warnings(self):
2015        """
2016        Do 'Kconfig.warn = False' instead. Maintained for backwards
2017        compatibility.
2018        """
2019        self.warn = False
2020
2021    def enable_stderr_warnings(self):
2022        """
2023        Do 'Kconfig.warn_to_stderr = True' instead. Maintained for backwards
2024        compatibility.
2025        """
2026        self.warn_to_stderr = True
2027
2028    def disable_stderr_warnings(self):
2029        """
2030        Do 'Kconfig.warn_to_stderr = False' instead. Maintained for backwards
2031        compatibility.
2032        """
2033        self.warn_to_stderr = False
2034
2035    def enable_undef_warnings(self):
2036        """
2037        Do 'Kconfig.warn_assign_undef = True' instead. Maintained for backwards
2038        compatibility.
2039        """
2040        self.warn_assign_undef = True
2041
2042    def disable_undef_warnings(self):
2043        """
2044        Do 'Kconfig.warn_assign_undef = False' instead. Maintained for
2045        backwards compatibility.
2046        """
2047        self.warn_assign_undef = False
2048
2049    def enable_override_warnings(self):
2050        """
2051        Do 'Kconfig.warn_assign_override = True' instead. Maintained for
2052        backwards compatibility.
2053        """
2054        self.warn_assign_override = True
2055
2056    def disable_override_warnings(self):
2057        """
2058        Do 'Kconfig.warn_assign_override = False' instead. Maintained for
2059        backwards compatibility.
2060        """
2061        self.warn_assign_override = False
2062
2063    def enable_redun_warnings(self):
2064        """
2065        Do 'Kconfig.warn_assign_redun = True' instead. Maintained for backwards
2066        compatibility.
2067        """
2068        self.warn_assign_redun = True
2069
2070    def disable_redun_warnings(self):
2071        """
2072        Do 'Kconfig.warn_assign_redun = False' instead. Maintained for
2073        backwards compatibility.
2074        """
2075        self.warn_assign_redun = False
2076
2077    def __repr__(self):
2078        """
2079        Returns a string with information about the Kconfig object when it is
2080        evaluated on e.g. the interactive Python prompt.
2081        """
2082        def status(flag):
2083            return "enabled" if flag else "disabled"
2084
2085        return "<{}>".format(", ".join((
2086            "configuration with {} symbols".format(len(self.syms)),
2087            'main menu prompt "{}"'.format(self.mainmenu_text),
2088            "srctree is current directory" if not self.srctree else
2089                'srctree "{}"'.format(self.srctree),
2090            'config symbol prefix "{}"'.format(self.config_prefix),
2091            "warnings " + status(self.warn),
2092            "printing of warnings to stderr " + status(self.warn_to_stderr),
2093            "undef. symbol assignment warnings " +
2094                status(self.warn_assign_undef),
2095            "overriding symbol assignment warnings " +
2096                status(self.warn_assign_override),
2097            "redundant symbol assignment warnings " +
2098                status(self.warn_assign_redun)
2099        )))
2100
2101    #
2102    # Private methods
2103    #
2104
2105
2106    #
2107    # File reading
2108    #
2109
2110    def _open_config(self, filename):
2111        # Opens a .config file. First tries to open 'filename', then
2112        # '$srctree/filename' if $srctree was set when the configuration was
2113        # loaded.
2114
2115        try:
2116            return self._open(filename, "r")
2117        except EnvironmentError as e:
2118            # This will try opening the same file twice if $srctree is unset,
2119            # but it's not a big deal
2120            try:
2121                return self._open(join(self.srctree, filename), "r")
2122            except EnvironmentError as e2:
2123                # This is needed for Python 3, because e2 is deleted after
2124                # the try block:
2125                #
2126                # https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
2127                e = e2
2128
2129            raise _KconfigIOError(
2130                e, "Could not open '{}' ({}: {}). Check that the $srctree "
2131                   "environment variable ({}) is set correctly."
2132                   .format(filename, errno.errorcode[e.errno], e.strerror,
2133                           "set to '{}'".format(self.srctree) if self.srctree
2134                               else "unset or blank"))
2135
2136    def _enter_file(self, filename):
2137        # Jumps to the beginning of a sourced Kconfig file, saving the previous
2138        # position and file object.
2139        #
2140        # filename:
2141        #   Absolute path to file
2142
2143        # Path relative to $srctree, stored in e.g. self.filename (which makes
2144        # it indirectly show up in MenuNode.filename). Equals 'filename' for
2145        # absolute paths passed to 'source'.
2146        if filename.startswith(self._srctree_prefix):
2147            # Relative path (or a redundant absolute path to within $srctree,
2148            # but it's probably fine to reduce those too)
2149            rel_filename = filename[len(self._srctree_prefix):]
2150        else:
2151            # Absolute path
2152            rel_filename = filename
2153
2154        self.kconfig_filenames.append(rel_filename)
2155
2156        # The parent Kconfig files are represented as a list of
2157        # (<include path>, <Python 'file' object for Kconfig file>) tuples.
2158        #
2159        # <include path> is immutable and holds a *tuple* of
2160        # (<filename>, <linenr>) tuples, giving the locations of the 'source'
2161        # statements in the parent Kconfig files. The current include path is
2162        # also available in Kconfig._include_path.
2163        #
2164        # The point of this redundant setup is to allow Kconfig._include_path
2165        # to be assigned directly to MenuNode.include_path without having to
2166        # copy it, sharing it wherever possible.
2167
2168        # Save include path and 'file' object (via its 'readline' function)
2169        # before entering the file
2170        self._filestack.append((self._include_path, self._readline))
2171
2172        # _include_path is a tuple, so this rebinds the variable instead of
2173        # doing in-place modification
2174        self._include_path += ((self.filename, self.linenr),)
2175
2176        # Check for recursive 'source'
2177        for name, _ in self._include_path:
2178            if name == rel_filename:
2179                raise KconfigError(
2180                    "\n{}:{}: recursive 'source' of '{}' detected. Check that "
2181                    "environment variables are set correctly.\n"
2182                    "Include path:\n{}"
2183                    .format(self.filename, self.linenr, rel_filename,
2184                            "\n".join("{}:{}".format(name, linenr)
2185                                      for name, linenr in self._include_path)))
2186
2187        try:
2188            self._readline = self._open(filename, "r").readline
2189        except EnvironmentError as e:
2190            # We already know that the file exists
2191            raise _KconfigIOError(
2192                e, "{}:{}: Could not open '{}' (in '{}') ({}: {})"
2193                   .format(self.filename, self.linenr, filename,
2194                           self._line.strip(),
2195                           errno.errorcode[e.errno], e.strerror))
2196
2197        self.filename = rel_filename
2198        self.linenr = 0
2199
2200    def _leave_file(self):
2201        # Returns from a Kconfig file to the file that sourced it. See
2202        # _enter_file().
2203
2204        # Restore location from parent Kconfig file
2205        self.filename, self.linenr = self._include_path[-1]
2206        # Restore include path and 'file' object
2207        self._readline.__self__.close()  # __self__ fetches the 'file' object
2208        self._include_path, self._readline = self._filestack.pop()
2209
2210    def _next_line(self):
2211        # Fetches and tokenizes the next line from the current Kconfig file.
2212        # Returns False at EOF and True otherwise.
2213
2214        # We might already have tokens from parsing a line and discovering that
2215        # it's part of a different construct
2216        if self._reuse_tokens:
2217            self._reuse_tokens = False
2218            # self._tokens_i is known to be 1 here, because _parse_props()
2219            # leaves it like that when it can't recognize a line (or parses a
2220            # help text)
2221            return True
2222
2223        # readline() returns '' over and over at EOF, which we rely on for help
2224        # texts at the end of files (see _line_after_help())
2225        line = self._readline()
2226        if not line:
2227            return False
2228        self.linenr += 1
2229
2230        # Handle line joining
2231        while line.endswith("\\\n"):
2232            line = line[:-2] + self._readline()
2233            self.linenr += 1
2234
2235        self._tokens = self._tokenize(line)
2236        # Initialize to 1 instead of 0 to factor out code from _parse_block()
2237        # and _parse_props(). They immediately fetch self._tokens[0].
2238        self._tokens_i = 1
2239
2240        return True
2241
2242    def _line_after_help(self, line):
2243        # Tokenizes a line after a help text. This case is special in that the
2244        # line has already been fetched (to discover that it isn't part of the
2245        # help text).
2246        #
2247        # An earlier version used a _saved_line variable instead that was
2248        # checked in _next_line(). This special-casing gets rid of it and makes
2249        # _reuse_tokens alone sufficient to handle unget.
2250
2251        # Handle line joining
2252        while line.endswith("\\\n"):
2253            line = line[:-2] + self._readline()
2254            self.linenr += 1
2255
2256        self._tokens = self._tokenize(line)
2257        self._reuse_tokens = True
2258
2259    def _write_if_changed(self, filename, contents):
2260        # Writes 'contents' into 'filename', but only if it differs from the
2261        # current contents of the file.
2262        #
2263        # Another variant would be write a temporary file on the same
2264        # filesystem, compare the files, and rename() the temporary file if it
2265        # differs, but it breaks stuff like write_config("/dev/null"), which is
2266        # used out there to force evaluation-related warnings to be generated.
2267        # This simple version is pretty failsafe and portable.
2268        #
2269        # Returns True if the file has changed and is updated, and False
2270        # otherwise.
2271
2272        if self._contents_eq(filename, contents):
2273            return False
2274        with self._open(filename, "w") as f:
2275            f.write(contents)
2276        return True
2277
2278    def _contents_eq(self, filename, contents):
2279        # Returns True if the contents of 'filename' is 'contents' (a string),
2280        # and False otherwise (including if 'filename' can't be opened/read)
2281
2282        try:
2283            with self._open(filename, "r") as f:
2284                # Robust re. things like encoding and line endings (mmap()
2285                # trickery isn't)
2286                return f.read(len(contents) + 1) == contents
2287        except EnvironmentError:
2288            # If the error here would prevent writing the file as well, we'll
2289            # notice it later
2290            return False
2291
2292    #
2293    # Tokenization
2294    #
2295
2296    def _lookup_sym(self, name):
2297        # Fetches the symbol 'name' from the symbol table, creating and
2298        # registering it if it does not exist. If '_parsing_kconfigs' is False,
2299        # it means we're in eval_string(), and new symbols won't be registered.
2300
2301        if name in self.syms:
2302            return self.syms[name]
2303
2304        sym = Symbol()
2305        sym.kconfig = self
2306        sym.name = name
2307        sym.is_constant = False
2308        sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
2309
2310        if self._parsing_kconfigs:
2311            self.syms[name] = sym
2312        else:
2313            self._warn("no symbol {} in configuration".format(name))
2314
2315        return sym
2316
2317    def _lookup_const_sym(self, name):
2318        # Like _lookup_sym(), for constant (quoted) symbols
2319
2320        if name in self.const_syms:
2321            return self.const_syms[name]
2322
2323        sym = Symbol()
2324        sym.kconfig = self
2325        sym.name = name
2326        sym.is_constant = True
2327        sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
2328
2329        if self._parsing_kconfigs:
2330            self.const_syms[name] = sym
2331
2332        return sym
2333
2334    def _tokenize(self, s):
2335        # Parses 's', returning a None-terminated list of tokens. Registers any
2336        # new symbols encountered with _lookup(_const)_sym().
2337        #
2338        # Tries to be reasonably speedy by processing chunks of text via
2339        # regexes and string operations where possible. This is the biggest
2340        # hotspot during parsing.
2341        #
2342        # It might be possible to rewrite this to 'yield' tokens instead,
2343        # working across multiple lines. Lookback and compatibility with old
2344        # janky versions of the C tools complicate things though.
2345
2346        self._line = s  # Used for error reporting
2347
2348        # Initial token on the line
2349        match = _command_match(s)
2350        if not match:
2351            if s.isspace() or s.lstrip().startswith("#"):
2352                return (None,)
2353            self._parse_error("unknown token at start of line")
2354
2355        # Tricky implementation detail: While parsing a token, 'token' refers
2356        # to the previous token. See _STRING_LEX for why this is needed.
2357        token = _get_keyword(match.group(1))
2358        if not token:
2359            # Backwards compatibility with old versions of the C tools, which
2360            # (accidentally) accepted stuff like "--help--" and "-help---".
2361            # This was fixed in the C tools by commit c2264564 ("kconfig: warn
2362            # of unhandled characters in Kconfig commands"), committed in July
2363            # 2015, but it seems people still run Kconfiglib on older kernels.
2364            if s.strip(" \t\n-") == "help":
2365                return (_T_HELP, None)
2366
2367            # If the first token is not a keyword (and not a weird help token),
2368            # we have a preprocessor variable assignment (or a bare macro on a
2369            # line)
2370            self._parse_assignment(s)
2371            return (None,)
2372
2373        tokens = [token]
2374        # The current index in the string being tokenized
2375        i = match.end()
2376
2377        # Main tokenization loop (for tokens past the first one)
2378        while i < len(s):
2379            # Test for an identifier/keyword first. This is the most common
2380            # case.
2381            match = _id_keyword_match(s, i)
2382            if match:
2383                # We have an identifier or keyword
2384
2385                # Check what it is. lookup_sym() will take care of allocating
2386                # new symbols for us the first time we see them. Note that
2387                # 'token' still refers to the previous token.
2388
2389                name = match.group(1)
2390                keyword = _get_keyword(name)
2391                if keyword:
2392                    # It's a keyword
2393                    token = keyword
2394                    # Jump past it
2395                    i = match.end()
2396
2397                elif token not in _STRING_LEX:
2398                    # It's a non-const symbol, except we translate n, m, and y
2399                    # into the corresponding constant symbols, like the C
2400                    # implementation
2401
2402                    if "$" in name:
2403                        # Macro expansion within symbol name
2404                        name, s, i = self._expand_name(s, i)
2405                    else:
2406                        i = match.end()
2407
2408                    token = self.const_syms[name] if name in STR_TO_TRI else \
2409                        self._lookup_sym(name)
2410
2411                else:
2412                    # It's a case of missing quotes. For example, the
2413                    # following is accepted:
2414                    #
2415                    #   menu unquoted_title
2416                    #
2417                    #   config A
2418                    #       tristate unquoted_prompt
2419                    #
2420                    #   endmenu
2421                    #
2422                    # Named choices ('choice FOO') also end up here.
2423
2424                    if token is not _T_CHOICE:
2425                        self._warn("style: quotes recommended around '{}' in '{}'"
2426                                   .format(name, self._line.strip()),
2427                                   self.filename, self.linenr)
2428
2429                    token = name
2430                    i = match.end()
2431
2432            else:
2433                # Neither a keyword nor a non-const symbol
2434
2435                # We always strip whitespace after tokens, so it is safe to
2436                # assume that s[i] is the start of a token here.
2437                c = s[i]
2438
2439                if c in "\"'":
2440                    if "$" not in s and "\\" not in s:
2441                        # Fast path for lines without $ and \. Find the
2442                        # matching quote.
2443                        end_i = s.find(c, i + 1) + 1
2444                        if not end_i:
2445                            self._parse_error("unterminated string")
2446                        val = s[i + 1:end_i - 1]
2447                        i = end_i
2448                    else:
2449                        # Slow path
2450                        s, end_i = self._expand_str(s, i)
2451
2452                        # os.path.expandvars() and the $UNAME_RELEASE replace()
2453                        # is a backwards compatibility hack, which should be
2454                        # reasonably safe as expandvars() leaves references to
2455                        # undefined env. vars. as is.
2456                        #
2457                        # The preprocessor functionality changed how
2458                        # environment variables are referenced, to $(FOO).
2459                        val = expandvars(s[i + 1:end_i - 1]
2460                                         .replace("$UNAME_RELEASE",
2461                                                  _UNAME_RELEASE))
2462
2463                        i = end_i
2464
2465                    # This is the only place where we don't survive with a
2466                    # single token of lookback: 'option env="FOO"' does not
2467                    # refer to a constant symbol named "FOO".
2468                    token = \
2469                        val if token in _STRING_LEX or tokens[0] is _T_OPTION \
2470                        else self._lookup_const_sym(val)
2471
2472                elif s.startswith("&&", i):
2473                    token = _T_AND
2474                    i += 2
2475
2476                elif s.startswith("||", i):
2477                    token = _T_OR
2478                    i += 2
2479
2480                elif c == "=":
2481                    token = _T_EQUAL
2482                    i += 1
2483
2484                elif s.startswith("!=", i):
2485                    token = _T_UNEQUAL
2486                    i += 2
2487
2488                elif c == "!":
2489                    token = _T_NOT
2490                    i += 1
2491
2492                elif c == "(":
2493                    token = _T_OPEN_PAREN
2494                    i += 1
2495
2496                elif c == ")":
2497                    token = _T_CLOSE_PAREN
2498                    i += 1
2499
2500                elif c == "#":
2501                    break
2502
2503
2504                # Very rare
2505
2506                elif s.startswith("<=", i):
2507                    token = _T_LESS_EQUAL
2508                    i += 2
2509
2510                elif c == "<":
2511                    token = _T_LESS
2512                    i += 1
2513
2514                elif s.startswith(">=", i):
2515                    token = _T_GREATER_EQUAL
2516                    i += 2
2517
2518                elif c == ">":
2519                    token = _T_GREATER
2520                    i += 1
2521
2522
2523                else:
2524                    self._parse_error("unknown tokens in line")
2525
2526
2527                # Skip trailing whitespace
2528                while i < len(s) and s[i].isspace():
2529                    i += 1
2530
2531
2532            # Add the token
2533            tokens.append(token)
2534
2535        # None-terminating the token list makes token fetching simpler/faster
2536        tokens.append(None)
2537
2538        return tokens
2539
2540    # Helpers for syntax checking and token fetching. See the
2541    # 'Intro to expressions' section for what a constant symbol is.
2542    #
2543    # More of these could be added, but the single-use cases are inlined as an
2544    # optimization.
2545
2546    def _expect_sym(self):
2547        token = self._tokens[self._tokens_i]
2548        self._tokens_i += 1
2549
2550        if token.__class__ is not Symbol:
2551            self._parse_error("expected symbol")
2552
2553        return token
2554
2555    def _expect_nonconst_sym(self):
2556        # Used for 'select' and 'imply' only. We know the token indices.
2557
2558        token = self._tokens[1]
2559        self._tokens_i = 2
2560
2561        if token.__class__ is not Symbol or token.is_constant:
2562            self._parse_error("expected nonconstant symbol")
2563
2564        return token
2565
2566    def _expect_str_and_eol(self):
2567        token = self._tokens[self._tokens_i]
2568        self._tokens_i += 1
2569
2570        if token.__class__ is not str:
2571            self._parse_error("expected string")
2572
2573        if self._tokens[self._tokens_i] is not None:
2574            self._trailing_tokens_error()
2575
2576        return token
2577
2578    def _expect_expr_and_eol(self):
2579        expr = self._parse_expr(True)
2580
2581        if self._tokens[self._tokens_i] is not None:
2582            self._trailing_tokens_error()
2583
2584        return expr
2585
2586    def _check_token(self, token):
2587        # If the next token is 'token', removes it and returns True
2588
2589        if self._tokens[self._tokens_i] is token:
2590            self._tokens_i += 1
2591            return True
2592        return False
2593
2594    #
2595    # Preprocessor logic
2596    #
2597
2598    def _parse_assignment(self, s):
2599        # Parses a preprocessor variable assignment, registering the variable
2600        # if it doesn't already exist. Also takes care of bare macros on lines
2601        # (which are allowed, and can be useful for their side effects).
2602
2603        # Expand any macros in the left-hand side of the assignment (the
2604        # variable name)
2605        s = s.lstrip()
2606        i = 0
2607        while 1:
2608            i = _assignment_lhs_fragment_match(s, i).end()
2609            if s.startswith("$(", i):
2610                s, i = self._expand_macro(s, i, ())
2611            else:
2612                break
2613
2614        if s.isspace():
2615            # We also accept a bare macro on a line (e.g.
2616            # $(warning-if,$(foo),ops)), provided it expands to a blank string
2617            return
2618
2619        # Assigned variable
2620        name = s[:i]
2621
2622
2623        # Extract assignment operator (=, :=, or +=) and value
2624        rhs_match = _assignment_rhs_match(s, i)
2625        if not rhs_match:
2626            self._parse_error("syntax error")
2627
2628        op, val = rhs_match.groups()
2629
2630
2631        if name in self.variables:
2632            # Already seen variable
2633            var = self.variables[name]
2634        else:
2635            # New variable
2636            var = Variable()
2637            var.kconfig = self
2638            var.name = name
2639            var._n_expansions = 0
2640            self.variables[name] = var
2641
2642            # += acts like = on undefined variables (defines a recursive
2643            # variable)
2644            if op == "+=":
2645                op = "="
2646
2647        if op == "=":
2648            var.is_recursive = True
2649            var.value = val
2650        elif op == ":=":
2651            var.is_recursive = False
2652            var.value = self._expand_whole(val, ())
2653        else:  # op == "+="
2654            # += does immediate expansion if the variable was last set
2655            # with :=
2656            var.value += " " + (val if var.is_recursive else
2657                                self._expand_whole(val, ()))
2658
2659    def _expand_whole(self, s, args):
2660        # Expands preprocessor macros in all of 's'. Used whenever we don't
2661        # have to worry about delimiters. See _expand_macro() re. the 'args'
2662        # parameter.
2663        #
2664        # Returns the expanded string.
2665
2666        i = 0
2667        while 1:
2668            i = s.find("$(", i)
2669            if i == -1:
2670                break
2671            s, i = self._expand_macro(s, i, args)
2672        return s
2673
2674    def _expand_name(self, s, i):
2675        # Expands a symbol name starting at index 'i' in 's'.
2676        #
2677        # Returns the expanded name, the expanded 's' (including the part
2678        # before the name), and the index of the first character in the next
2679        # token after the name.
2680
2681        s, end_i = self._expand_name_iter(s, i)
2682        name = s[i:end_i]
2683        # isspace() is False for empty strings
2684        if not name.strip():
2685            # Avoid creating a Kconfig symbol with a blank name. It's almost
2686            # guaranteed to be an error.
2687            self._parse_error("macro expanded to blank string")
2688
2689        # Skip trailing whitespace
2690        while end_i < len(s) and s[end_i].isspace():
2691            end_i += 1
2692
2693        return name, s, end_i
2694
2695    def _expand_name_iter(self, s, i):
2696        # Expands a symbol name starting at index 'i' in 's'.
2697        #
2698        # Returns the expanded 's' (including the part before the name) and the
2699        # index of the first character after the expanded name in 's'.
2700
2701        while 1:
2702            match = _name_special_search(s, i)
2703
2704            if match.group() != "$(":
2705                return (s, match.start())
2706            s, i = self._expand_macro(s, match.start(), ())
2707
2708    def _expand_str(self, s, i):
2709        # Expands a quoted string starting at index 'i' in 's'. Handles both
2710        # backslash escapes and macro expansion.
2711        #
2712        # Returns the expanded 's' (including the part before the string) and
2713        # the index of the first character after the expanded string in 's'.
2714
2715        quote = s[i]
2716        i += 1  # Skip over initial "/'
2717        while 1:
2718            match = _string_special_search(s, i)
2719            if not match:
2720                self._parse_error("unterminated string")
2721
2722
2723            if match.group() == quote:
2724                # Found the end of the string
2725                return (s, match.end())
2726
2727            elif match.group() == "\\":
2728                # Replace '\x' with 'x'. 'i' ends up pointing to the character
2729                # after 'x', which allows macros to be canceled with '\$(foo)'.
2730                i = match.end()
2731                s = s[:match.start()] + s[i:]
2732
2733            elif match.group() == "$(":
2734                # A macro call within the string
2735                s, i = self._expand_macro(s, match.start(), ())
2736
2737            else:
2738                # A ' quote within " quotes or vice versa
2739                i += 1
2740
2741    def _expand_macro(self, s, i, args):
2742        # Expands a macro starting at index 'i' in 's'. If this macro resulted
2743        # from the expansion of another macro, 'args' holds the arguments
2744        # passed to that macro.
2745        #
2746        # Returns the expanded 's' (including the part before the macro) and
2747        # the index of the first character after the expanded macro in 's'.
2748
2749        res = s[:i]
2750        i += 2  # Skip over "$("
2751
2752        arg_start = i  # Start of current macro argument
2753        new_args = []  # Arguments of this macro call
2754        nesting = 0  # Current parentheses nesting level
2755
2756        while 1:
2757            match = _macro_special_search(s, i)
2758            if not match:
2759                self._parse_error("missing end parenthesis in macro expansion")
2760
2761
2762            if match.group() == "(":
2763                nesting += 1
2764                i = match.end()
2765
2766            elif match.group() == ")":
2767                if nesting:
2768                    nesting -= 1
2769                    i = match.end()
2770                    continue
2771
2772                # Found the end of the macro
2773
2774                new_args.append(s[arg_start:match.start()])
2775
2776                # $(1) is replaced by the first argument to the function, etc.,
2777                # provided at least that many arguments were passed
2778
2779                try:
2780                    # Does the macro look like an integer, with a corresponding
2781                    # argument? If so, expand it to the value of the argument.
2782                    res += args[int(new_args[0])]
2783                except (ValueError, IndexError):
2784                    # Regular variables are just functions without arguments,
2785                    # and also go through the function value path
2786                    res += self._fn_val(new_args)
2787
2788                return (res + s[match.end():], len(res))
2789
2790            elif match.group() == ",":
2791                i = match.end()
2792                if nesting:
2793                    continue
2794
2795                # Found the end of a macro argument
2796                new_args.append(s[arg_start:match.start()])
2797                arg_start = i
2798
2799            else:  # match.group() == "$("
2800                # A nested macro call within the macro
2801                s, i = self._expand_macro(s, match.start(), args)
2802
2803    def _fn_val(self, args):
2804        # Returns the result of calling the function args[0] with the arguments
2805        # args[1..len(args)-1]. Plain variables are treated as functions
2806        # without arguments.
2807
2808        fn = args[0]
2809
2810        if fn in self.variables:
2811            var = self.variables[fn]
2812
2813            if len(args) == 1:
2814                # Plain variable
2815                if var._n_expansions:
2816                    self._parse_error("Preprocessor variable {} recursively "
2817                                      "references itself".format(var.name))
2818            elif var._n_expansions > 100:
2819                # Allow functions to call themselves, but guess that functions
2820                # that are overly recursive are stuck
2821                self._parse_error("Preprocessor function {} seems stuck "
2822                                  "in infinite recursion".format(var.name))
2823
2824            var._n_expansions += 1
2825            res = self._expand_whole(self.variables[fn].value, args)
2826            var._n_expansions -= 1
2827            return res
2828
2829        if fn in self._functions:
2830            # Built-in or user-defined function
2831
2832            py_fn, min_arg, max_arg = self._functions[fn]
2833
2834            if len(args) - 1 < min_arg or \
2835               (max_arg is not None and len(args) - 1 > max_arg):
2836
2837                if min_arg == max_arg:
2838                    expected_args = min_arg
2839                elif max_arg is None:
2840                    expected_args = "{} or more".format(min_arg)
2841                else:
2842                    expected_args = "{}-{}".format(min_arg, max_arg)
2843
2844                raise KconfigError("{}:{}: bad number of arguments in call "
2845                                   "to {}, expected {}, got {}"
2846                                   .format(self.filename, self.linenr, fn,
2847                                           expected_args, len(args) - 1))
2848
2849            return py_fn(self, *args)
2850
2851        # Environment variables are tried last
2852        if fn in os.environ:
2853            self.env_vars.add(fn)
2854            return os.environ[fn]
2855
2856        return ""
2857
2858    #
2859    # Parsing
2860    #
2861
2862    def _make_and(self, e1, e2):
2863        # Constructs an AND (&&) expression. Performs trivial simplification.
2864
2865        if e1 is self.y:
2866            return e2
2867
2868        if e2 is self.y:
2869            return e1
2870
2871        if e1 is self.n or e2 is self.n:
2872            return self.n
2873
2874        return (AND, e1, e2)
2875
2876    def _make_or(self, e1, e2):
2877        # Constructs an OR (||) expression. Performs trivial simplification.
2878
2879        if e1 is self.n:
2880            return e2
2881
2882        if e2 is self.n:
2883            return e1
2884
2885        if e1 is self.y or e2 is self.y:
2886            return self.y
2887
2888        return (OR, e1, e2)
2889
2890    def _parse_block(self, end_token, parent, prev):
2891        # Parses a block, which is the contents of either a file or an if,
2892        # menu, or choice statement.
2893        #
2894        # end_token:
2895        #   The token that ends the block, e.g. _T_ENDIF ("endif") for ifs.
2896        #   None for files.
2897        #
2898        # parent:
2899        #   The parent menu node, corresponding to a menu, Choice, or 'if'.
2900        #   'if's are flattened after parsing.
2901        #
2902        # prev:
2903        #   The previous menu node. New nodes will be added after this one (by
2904        #   modifying 'next' pointers).
2905        #
2906        #   'prev' is reused to parse a list of child menu nodes (for a menu or
2907        #   Choice): After parsing the children, the 'next' pointer is assigned
2908        #   to the 'list' pointer to "tilt up" the children above the node.
2909        #
2910        # Returns the final menu node in the block (or 'prev' if the block is
2911        # empty). This allows chaining.
2912
2913        while self._next_line():
2914            t0 = self._tokens[0]
2915
2916            if t0 is _T_CONFIG or t0 is _T_MENUCONFIG:
2917                # The tokenizer allocates Symbol objects for us
2918                sym = self._tokens[1]
2919
2920                if sym.__class__ is not Symbol or sym.is_constant:
2921                    self._parse_error("missing or bad symbol name")
2922
2923                if self._tokens[2] is not None:
2924                    self._trailing_tokens_error()
2925
2926                self.defined_syms.append(sym)
2927
2928                node = MenuNode()
2929                node.kconfig = self
2930                node.item = sym
2931                node.is_menuconfig = (t0 is _T_MENUCONFIG)
2932                node.prompt = node.help = node.list = None
2933                node.parent = parent
2934                node.filename = self.filename
2935                node.linenr = self.linenr
2936                node.include_path = self._include_path
2937
2938                sym.nodes.append(node)
2939
2940                self._parse_props(node)
2941
2942                if node.is_menuconfig and not node.prompt:
2943                    self._warn("the menuconfig symbol {} has no prompt"
2944                               .format(sym.name_and_loc))
2945
2946                # Equivalent to
2947                #
2948                #   prev.next = node
2949                #   prev = node
2950                #
2951                # due to tricky Python semantics. The order matters.
2952                prev.next = prev = node
2953
2954            elif t0 is None:
2955                # Blank line
2956                continue
2957
2958            elif t0 in _SOURCE_TOKENS:
2959                pattern = self._expect_str_and_eol()
2960
2961                if t0 in _REL_SOURCE_TOKENS:
2962                    # Relative source
2963                    pattern = join(dirname(self.filename), pattern)
2964
2965                # - glob() doesn't support globbing relative to a directory, so
2966                #   we need to prepend $srctree to 'pattern'. Use join()
2967                #   instead of '+' so that an absolute path in 'pattern' is
2968                #   preserved.
2969                #
2970                # - Sort the glob results to ensure a consistent ordering of
2971                #   Kconfig symbols, which indirectly ensures a consistent
2972                #   ordering in e.g. .config files
2973                filenames = sorted(iglob(join(self._srctree_prefix, pattern)))
2974
2975                if not filenames and t0 in _OBL_SOURCE_TOKENS:
2976                    raise KconfigError(
2977                        "{}:{}: '{}' not found (in '{}'). Check that "
2978                        "environment variables are set correctly (e.g. "
2979                        "$srctree, which is {}). Also note that unset "
2980                        "environment variables expand to the empty string."
2981                        .format(self.filename, self.linenr, pattern,
2982                                self._line.strip(),
2983                                "set to '{}'".format(self.srctree)
2984                                    if self.srctree else "unset or blank"))
2985
2986                for filename in filenames:
2987                    self._enter_file(filename)
2988                    prev = self._parse_block(None, parent, prev)
2989                    self._leave_file()
2990
2991            elif t0 is end_token:
2992                # Reached the end of the block. Terminate the final node and
2993                # return it.
2994
2995                if self._tokens[1] is not None:
2996                    self._trailing_tokens_error()
2997
2998                prev.next = None
2999                return prev
3000
3001            elif t0 is _T_IF:
3002                node = MenuNode()
3003                node.item = node.prompt = None
3004                node.parent = parent
3005                node.dep = self._expect_expr_and_eol()
3006
3007                self._parse_block(_T_ENDIF, node, node)
3008                node.list = node.next
3009
3010                prev.next = prev = node
3011
3012            elif t0 is _T_MENU:
3013                node = MenuNode()
3014                node.kconfig = self
3015                node.item = t0  # _T_MENU == MENU
3016                node.is_menuconfig = True
3017                node.prompt = (self._expect_str_and_eol(), self.y)
3018                node.visibility = self.y
3019                node.parent = parent
3020                node.filename = self.filename
3021                node.linenr = self.linenr
3022                node.include_path = self._include_path
3023
3024                self.menus.append(node)
3025
3026                self._parse_props(node)
3027                self._parse_block(_T_ENDMENU, node, node)
3028                node.list = node.next
3029
3030                prev.next = prev = node
3031
3032            elif t0 is _T_COMMENT:
3033                node = MenuNode()
3034                node.kconfig = self
3035                node.item = t0  # _T_COMMENT == COMMENT
3036                node.is_menuconfig = False
3037                node.prompt = (self._expect_str_and_eol(), self.y)
3038                node.list = None
3039                node.parent = parent
3040                node.filename = self.filename
3041                node.linenr = self.linenr
3042                node.include_path = self._include_path
3043
3044                self.comments.append(node)
3045
3046                self._parse_props(node)
3047
3048                prev.next = prev = node
3049
3050            elif t0 is _T_CHOICE:
3051                if self._tokens[1] is None:
3052                    choice = Choice()
3053                    choice.direct_dep = self.n
3054                else:
3055                    # Named choice
3056                    name = self._expect_str_and_eol()
3057                    choice = self.named_choices.get(name)
3058                    if not choice:
3059                        choice = Choice()
3060                        choice.name = name
3061                        choice.direct_dep = self.n
3062                        self.named_choices[name] = choice
3063
3064                self.choices.append(choice)
3065
3066                node = MenuNode()
3067                node.kconfig = choice.kconfig = self
3068                node.item = choice
3069                node.is_menuconfig = True
3070                node.prompt = node.help = None
3071                node.parent = parent
3072                node.filename = self.filename
3073                node.linenr = self.linenr
3074                node.include_path = self._include_path
3075
3076                choice.nodes.append(node)
3077
3078                self._parse_props(node)
3079                self._parse_block(_T_ENDCHOICE, node, node)
3080                node.list = node.next
3081
3082                prev.next = prev = node
3083
3084            elif t0 is _T_MAINMENU:
3085                self.top_node.prompt = (self._expect_str_and_eol(), self.y)
3086
3087            else:
3088                # A valid endchoice/endif/endmenu is caught by the 'end_token'
3089                # check above
3090                self._parse_error(
3091                    "no corresponding 'choice'" if t0 is _T_ENDCHOICE else
3092                    "no corresponding 'if'"     if t0 is _T_ENDIF else
3093                    "no corresponding 'menu'"   if t0 is _T_ENDMENU else
3094                    "unrecognized construct")
3095
3096        # End of file reached. Return the last node.
3097
3098        if end_token:
3099            raise KconfigError(
3100                "error: expected '{}' at end of '{}'"
3101                .format("endchoice" if end_token is _T_ENDCHOICE else
3102                        "endif"     if end_token is _T_ENDIF else
3103                        "endmenu",
3104                        self.filename))
3105
3106        return prev
3107
3108    def _parse_cond(self):
3109        # Parses an optional 'if <expr>' construct and returns the parsed
3110        # <expr>, or self.y if the next token is not _T_IF
3111
3112        expr = self._parse_expr(True) if self._check_token(_T_IF) else self.y
3113
3114        if self._tokens[self._tokens_i] is not None:
3115            self._trailing_tokens_error()
3116
3117        return expr
3118
3119    def _parse_props(self, node):
3120        # Parses and adds properties to the MenuNode 'node' (type, 'prompt',
3121        # 'default's, etc.) Properties are later copied up to symbols and
3122        # choices in a separate pass after parsing, in e.g.
3123        # _add_props_to_sym().
3124        #
3125        # An older version of this code added properties directly to symbols
3126        # and choices instead of to their menu nodes (and handled dependency
3127        # propagation simultaneously), but that loses information on where a
3128        # property is added when a symbol or choice is defined in multiple
3129        # locations. Some Kconfig configuration systems rely heavily on such
3130        # symbols, and better docs can be generated by keeping track of where
3131        # properties are added.
3132        #
3133        # node:
3134        #   The menu node we're parsing properties on
3135
3136        # Dependencies from 'depends on'. Will get propagated to the properties
3137        # below.
3138        node.dep = self.y
3139
3140        while self._next_line():
3141            t0 = self._tokens[0]
3142
3143            if t0 in _TYPE_TOKENS:
3144                # Relies on '_T_BOOL is BOOL', etc., to save a conversion
3145                self._set_type(node.item, t0)
3146                if self._tokens[1] is not None:
3147                    self._parse_prompt(node)
3148
3149            elif t0 is _T_DEPENDS:
3150                if not self._check_token(_T_ON):
3151                    self._parse_error("expected 'on' after 'depends'")
3152
3153                node.dep = self._make_and(node.dep,
3154                                          self._expect_expr_and_eol())
3155
3156            elif t0 is _T_HELP:
3157                self._parse_help(node)
3158
3159            elif t0 is _T_SELECT:
3160                if node.item.__class__ is not Symbol:
3161                    self._parse_error("only symbols can select")
3162
3163                node.selects.append((self._expect_nonconst_sym(),
3164                                     self._parse_cond()))
3165
3166            elif t0 is None:
3167                # Blank line
3168                continue
3169
3170            elif t0 is _T_DEFAULT:
3171                node.defaults.append((self._parse_expr(False),
3172                                      self._parse_cond()))
3173
3174            elif t0 in _DEF_TOKEN_TO_TYPE:
3175                self._set_type(node.item, _DEF_TOKEN_TO_TYPE[t0])
3176                node.defaults.append((self._parse_expr(False),
3177                                      self._parse_cond()))
3178
3179            elif t0 is _T_PROMPT:
3180                self._parse_prompt(node)
3181
3182            elif t0 is _T_RANGE:
3183                node.ranges.append((self._expect_sym(), self._expect_sym(),
3184                                    self._parse_cond()))
3185
3186            elif t0 is _T_IMPLY:
3187                if node.item.__class__ is not Symbol:
3188                    self._parse_error("only symbols can imply")
3189
3190                node.implies.append((self._expect_nonconst_sym(),
3191                                     self._parse_cond()))
3192
3193            elif t0 is _T_VISIBLE:
3194                if not self._check_token(_T_IF):
3195                    self._parse_error("expected 'if' after 'visible'")
3196
3197                node.visibility = self._make_and(node.visibility,
3198                                                 self._expect_expr_and_eol())
3199
3200            elif t0 is _T_OPTION:
3201                if self._check_token(_T_ENV):
3202                    if not self._check_token(_T_EQUAL):
3203                        self._parse_error("expected '=' after 'env'")
3204
3205                    env_var = self._expect_str_and_eol()
3206                    node.item.env_var = env_var
3207
3208                    if env_var in os.environ:
3209                        node.defaults.append(
3210                            (self._lookup_const_sym(os.environ[env_var]),
3211                             self.y))
3212                    else:
3213                        self._warn("{1} has 'option env=\"{0}\"', "
3214                                   "but the environment variable {0} is not "
3215                                   "set".format(node.item.name, env_var),
3216                                   self.filename, self.linenr)
3217
3218                    if env_var != node.item.name:
3219                        self._warn("Kconfiglib expands environment variables "
3220                                   "in strings directly, meaning you do not "
3221                                   "need 'option env=...' \"bounce\" symbols. "
3222                                   "For compatibility with the C tools, "
3223                                   "rename {} to {} (so that the symbol name "
3224                                   "matches the environment variable name)."
3225                                   .format(node.item.name, env_var),
3226                                   self.filename, self.linenr)
3227
3228                elif self._check_token(_T_DEFCONFIG_LIST):
3229                    if not self.defconfig_list:
3230                        self.defconfig_list = node.item
3231                    else:
3232                        self._warn("'option defconfig_list' set on multiple "
3233                                   "symbols ({0} and {1}). Only {0} will be "
3234                                   "used.".format(self.defconfig_list.name,
3235                                                  node.item.name),
3236                                   self.filename, self.linenr)
3237
3238                elif self._check_token(_T_MODULES):
3239                    # To reduce warning spam, only warn if 'option modules' is
3240                    # set on some symbol that isn't MODULES, which should be
3241                    # safe. I haven't run into any projects that make use
3242                    # modules besides the kernel yet, and there it's likely to
3243                    # keep being called "MODULES".
3244                    if node.item is not self.modules:
3245                        self._warn("the 'modules' option is not supported. "
3246                                   "Let me know if this is a problem for you, "
3247                                   "as it wouldn't be that hard to implement. "
3248                                   "Note that modules are supported -- "
3249                                   "Kconfiglib just assumes the symbol name "
3250                                   "MODULES, like older versions of the C "
3251                                   "implementation did when 'option modules' "
3252                                   "wasn't used.",
3253                                   self.filename, self.linenr)
3254
3255                elif self._check_token(_T_ALLNOCONFIG_Y):
3256                    if node.item.__class__ is not Symbol:
3257                        self._parse_error("the 'allnoconfig_y' option is only "
3258                                          "valid for symbols")
3259
3260                    node.item.is_allnoconfig_y = True
3261
3262                else:
3263                    self._parse_error("unrecognized option")
3264
3265            elif t0 is _T_OPTIONAL:
3266                if node.item.__class__ is not Choice:
3267                    self._parse_error('"optional" is only valid for choices')
3268
3269                node.item.is_optional = True
3270
3271            else:
3272                # Reuse the tokens for the non-property line later
3273                self._reuse_tokens = True
3274                return
3275
3276    def _set_type(self, sc, new_type):
3277        # Sets the type of 'sc' (symbol or choice) to 'new_type'
3278
3279        # UNKNOWN is falsy
3280        if sc.orig_type and sc.orig_type is not new_type:
3281            self._warn("{} defined with multiple types, {} will be used"
3282                       .format(sc.name_and_loc, TYPE_TO_STR[new_type]))
3283
3284        sc.orig_type = new_type
3285
3286    def _parse_prompt(self, node):
3287        # 'prompt' properties override each other within a single definition of
3288        # a symbol, but additional prompts can be added by defining the symbol
3289        # multiple times
3290
3291        if node.prompt:
3292            self._warn(node.item.name_and_loc +
3293                       " defined with multiple prompts in single location")
3294
3295        prompt = self._tokens[1]
3296        self._tokens_i = 2
3297
3298        if prompt.__class__ is not str:
3299            self._parse_error("expected prompt string")
3300
3301        if prompt != prompt.strip():
3302            self._warn(node.item.name_and_loc +
3303                       " has leading or trailing whitespace in its prompt")
3304
3305            # This avoid issues for e.g. reStructuredText documentation, where
3306            # '*prompt *' is invalid
3307            prompt = prompt.strip()
3308
3309        node.prompt = (prompt, self._parse_cond())
3310
3311    def _parse_help(self, node):
3312        if node.help is not None:
3313            self._warn(node.item.name_and_loc + " defined with more than "
3314                       "one help text -- only the last one will be used")
3315
3316        # Micro-optimization. This code is pretty hot.
3317        readline = self._readline
3318
3319        # Find first non-blank (not all-space) line and get its
3320        # indentation
3321
3322        while 1:
3323            line = readline()
3324            self.linenr += 1
3325            if not line:
3326                self._empty_help(node, line)
3327                return
3328            if not line.isspace():
3329                break
3330
3331        len_ = len  # Micro-optimization
3332
3333        # Use a separate 'expline' variable here and below to avoid stomping on
3334        # any tabs people might've put deliberately into the first line after
3335        # the help text
3336        expline = line.expandtabs()
3337        indent = len_(expline) - len_(expline.lstrip())
3338        if not indent:
3339            self._empty_help(node, line)
3340            return
3341
3342        # The help text goes on till the first non-blank line with less indent
3343        # than the first line
3344
3345        # Add the first line
3346        lines = [expline[indent:]]
3347        add_line = lines.append  # Micro-optimization
3348
3349        while 1:
3350            line = readline()
3351            if line.isspace():
3352                # No need to preserve the exact whitespace in these
3353                add_line("\n")
3354            elif not line:
3355                # End of file
3356                break
3357            else:
3358                expline = line.expandtabs()
3359                if len_(expline) - len_(expline.lstrip()) < indent:
3360                    break
3361                add_line(expline[indent:])
3362
3363        self.linenr += len_(lines)
3364        node.help = "".join(lines).rstrip()
3365        if line:
3366            self._line_after_help(line)
3367
3368    def _empty_help(self, node, line):
3369        self._warn(node.item.name_and_loc +
3370                   " has 'help' but empty help text")
3371        node.help = ""
3372        if line:
3373            self._line_after_help(line)
3374
3375    def _parse_expr(self, transform_m):
3376        # Parses an expression from the tokens in Kconfig._tokens using a
3377        # simple top-down approach. See the module docstring for the expression
3378        # format.
3379        #
3380        # transform_m:
3381        #   True if m should be rewritten to m && MODULES. See the
3382        #   Kconfig.eval_string() documentation.
3383
3384        # Grammar:
3385        #
3386        #   expr:     and_expr ['||' expr]
3387        #   and_expr: factor ['&&' and_expr]
3388        #   factor:   <symbol> ['='/'!='/'<'/... <symbol>]
3389        #             '!' factor
3390        #             '(' expr ')'
3391        #
3392        # It helps to think of the 'expr: and_expr' case as a single-operand OR
3393        # (no ||), and of the 'and_expr: factor' case as a single-operand AND
3394        # (no &&). Parsing code is always a bit tricky.
3395
3396        # Mind dump: parse_factor() and two nested loops for OR and AND would
3397        # work as well. The straightforward implementation there gives a
3398        # (op, (op, (op, A, B), C), D) parse for A op B op C op D. Representing
3399        # expressions as (op, [list of operands]) instead goes nicely with that
3400        # version, but is wasteful for short expressions and complicates
3401        # expression evaluation and other code that works on expressions (more
3402        # complicated code likely offsets any performance gain from less
3403        # recursion too). If we also try to optimize the list representation by
3404        # merging lists when possible (e.g. when ANDing two AND expressions),
3405        # we end up allocating a ton of lists instead of reusing expressions,
3406        # which is bad.
3407
3408        and_expr = self._parse_and_expr(transform_m)
3409
3410        # Return 'and_expr' directly if we have a "single-operand" OR.
3411        # Otherwise, parse the expression on the right and make an OR node.
3412        # This turns A || B || C || D into (OR, A, (OR, B, (OR, C, D))).
3413        return and_expr if not self._check_token(_T_OR) else \
3414            (OR, and_expr, self._parse_expr(transform_m))
3415
3416    def _parse_and_expr(self, transform_m):
3417        factor = self._parse_factor(transform_m)
3418
3419        # Return 'factor' directly if we have a "single-operand" AND.
3420        # Otherwise, parse the right operand and make an AND node. This turns
3421        # A && B && C && D into (AND, A, (AND, B, (AND, C, D))).
3422        return factor if not self._check_token(_T_AND) else \
3423            (AND, factor, self._parse_and_expr(transform_m))
3424
3425    def _parse_factor(self, transform_m):
3426        token = self._tokens[self._tokens_i]
3427        self._tokens_i += 1
3428
3429        if token.__class__ is Symbol:
3430            # Plain symbol or relation
3431
3432            if self._tokens[self._tokens_i] not in _RELATIONS:
3433                # Plain symbol
3434
3435                # For conditional expressions ('depends on <expr>',
3436                # '... if <expr>', etc.), m is rewritten to m && MODULES.
3437                if transform_m and token is self.m:
3438                    return (AND, self.m, self.modules)
3439
3440                return token
3441
3442            # Relation
3443            #
3444            # _T_EQUAL, _T_UNEQUAL, etc., deliberately have the same values as
3445            # EQUAL, UNEQUAL, etc., so we can just use the token directly
3446            self._tokens_i += 1
3447            return (self._tokens[self._tokens_i - 1], token,
3448                    self._expect_sym())
3449
3450        if token is _T_NOT:
3451            # token == _T_NOT == NOT
3452            return (token, self._parse_factor(transform_m))
3453
3454        if token is _T_OPEN_PAREN:
3455            expr_parse = self._parse_expr(transform_m)
3456            if self._check_token(_T_CLOSE_PAREN):
3457                return expr_parse
3458
3459        self._parse_error("malformed expression")
3460
3461    #
3462    # Caching and invalidation
3463    #
3464
3465    def _build_dep(self):
3466        # Populates the Symbol/Choice._dependents sets, which contain all other
3467        # items (symbols and choices) that immediately depend on the item in
3468        # the sense that changing the value of the item might affect the value
3469        # of the dependent items. This is used for caching/invalidation.
3470        #
3471        # The calculated sets might be larger than necessary as we don't do any
3472        # complex analysis of the expressions.
3473
3474        depend_on = _depend_on  # Micro-optimization
3475
3476        # Only calculate _dependents for defined symbols. Constant and
3477        # undefined symbols could theoretically be selected/implied, but it
3478        # wouldn't change their value, so it's not a true dependency.
3479        for sym in self.unique_defined_syms:
3480            # Symbols depend on the following:
3481
3482            # The prompt conditions
3483            for node in sym.nodes:
3484                if node.prompt:
3485                    depend_on(sym, node.prompt[1])
3486
3487            # The default values and their conditions
3488            for value, cond in sym.defaults:
3489                depend_on(sym, value)
3490                depend_on(sym, cond)
3491
3492            # The reverse and weak reverse dependencies
3493            depend_on(sym, sym.rev_dep)
3494            depend_on(sym, sym.weak_rev_dep)
3495
3496            # The ranges along with their conditions
3497            for low, high, cond in sym.ranges:
3498                depend_on(sym, low)
3499                depend_on(sym, high)
3500                depend_on(sym, cond)
3501
3502            # The direct dependencies. This is usually redundant, as the direct
3503            # dependencies get propagated to properties, but it's needed to get
3504            # invalidation solid for 'imply', which only checks the direct
3505            # dependencies (even if there are no properties to propagate it
3506            # to).
3507            depend_on(sym, sym.direct_dep)
3508
3509            # In addition to the above, choice symbols depend on the choice
3510            # they're in, but that's handled automatically since the Choice is
3511            # propagated to the conditions of the properties before
3512            # _build_dep() runs.
3513
3514        for choice in self.unique_choices:
3515            # Choices depend on the following:
3516
3517            # The prompt conditions
3518            for node in choice.nodes:
3519                if node.prompt:
3520                    depend_on(choice, node.prompt[1])
3521
3522            # The default symbol conditions
3523            for _, cond in choice.defaults:
3524                depend_on(choice, cond)
3525
3526    def _add_choice_deps(self):
3527        # Choices also depend on the choice symbols themselves, because the
3528        # y-mode selection of the choice might change if a choice symbol's
3529        # visibility changes.
3530        #
3531        # We add these dependencies separately after dependency loop detection.
3532        # The invalidation algorithm can handle the resulting
3533        # <choice symbol> <-> <choice> dependency loops, but they make loop
3534        # detection awkward.
3535
3536        for choice in self.unique_choices:
3537            for sym in choice.syms:
3538                sym._dependents.add(choice)
3539
3540    def _invalidate_all(self):
3541        # Undefined symbols never change value and don't need to be
3542        # invalidated, so we can just iterate over defined symbols.
3543        # Invalidating constant symbols would break things horribly.
3544        for sym in self.unique_defined_syms:
3545            sym._invalidate()
3546
3547        for choice in self.unique_choices:
3548            choice._invalidate()
3549
3550    #
3551    # Post-parsing menu tree processing, including dependency propagation and
3552    # implicit submenu creation
3553    #
3554
3555    def _finalize_node(self, node, visible_if):
3556        # Finalizes a menu node and its children:
3557        #
3558        #  - Copies properties from menu nodes up to their contained
3559        #    symbols/choices
3560        #
3561        #  - Propagates dependencies from parent to child nodes
3562        #
3563        #  - Creates implicit menus (see kconfig-language.txt)
3564        #
3565        #  - Removes 'if' nodes
3566        #
3567        #  - Sets 'choice' types and registers choice symbols
3568        #
3569        # menu_finalize() in the C implementation is similar.
3570        #
3571        # node:
3572        #   The menu node to finalize. This node and its children will have
3573        #   been finalized when the function returns, and any implicit menus
3574        #   will have been created.
3575        #
3576        # visible_if:
3577        #   Dependencies from 'visible if' on parent menus. These are added to
3578        #   the prompts of symbols and choices.
3579
3580        if node.item.__class__ is Symbol:
3581            # Copy defaults, ranges, selects, and implies to the Symbol
3582            self._add_props_to_sym(node)
3583
3584            # Find any items that should go in an implicit menu rooted at the
3585            # symbol
3586            cur = node
3587            while cur.next and _auto_menu_dep(node, cur.next):
3588                # This makes implicit submenu creation work recursively, with
3589                # implicit menus inside implicit menus
3590                self._finalize_node(cur.next, visible_if)
3591                cur = cur.next
3592                cur.parent = node
3593
3594            if cur is not node:
3595                # Found symbols that should go in an implicit submenu. Tilt
3596                # them up above us.
3597                node.list = node.next
3598                node.next = cur.next
3599                cur.next = None
3600
3601        elif node.list:
3602            # The menu node is a choice, menu, or if. Finalize each child node.
3603
3604            if node.item is MENU:
3605                visible_if = self._make_and(visible_if, node.visibility)
3606
3607            # Propagate the menu node's dependencies to each child menu node.
3608            #
3609            # This needs to go before the recursive _finalize_node() call so
3610            # that implicit submenu creation can look ahead at dependencies.
3611            self._propagate_deps(node, visible_if)
3612
3613            # Finalize the children
3614            cur = node.list
3615            while cur:
3616                self._finalize_node(cur, visible_if)
3617                cur = cur.next
3618
3619        if node.list:
3620            # node's children have been individually finalized. Do final steps
3621            # to finalize this "level" in the menu tree.
3622            _flatten(node.list)
3623            _remove_ifs(node)
3624
3625        # Empty choices (node.list None) are possible, so this needs to go
3626        # outside
3627        if node.item.__class__ is Choice:
3628            # Add the node's non-node-specific properties to the choice, like
3629            # _add_props_to_sym() does
3630            choice = node.item
3631            choice.direct_dep = self._make_or(choice.direct_dep, node.dep)
3632            choice.defaults += node.defaults
3633
3634            _finalize_choice(node)
3635
3636    def _propagate_deps(self, node, visible_if):
3637        # Propagates 'node's dependencies to its child menu nodes
3638
3639        # If the parent node holds a Choice, we use the Choice itself as the
3640        # parent dependency. This makes sense as the value (mode) of the choice
3641        # limits the visibility of the contained choice symbols. The C
3642        # implementation works the same way.
3643        #
3644        # Due to the similar interface, Choice works as a drop-in replacement
3645        # for Symbol here.
3646        basedep = node.item if node.item.__class__ is Choice else node.dep
3647
3648        cur = node.list
3649        while cur:
3650            dep = cur.dep = self._make_and(cur.dep, basedep)
3651
3652            if cur.item.__class__ in _SYMBOL_CHOICE:
3653                # Propagate 'visible if' and dependencies to the prompt
3654                if cur.prompt:
3655                    cur.prompt = (cur.prompt[0],
3656                                  self._make_and(
3657                                      cur.prompt[1],
3658                                      self._make_and(visible_if, dep)))
3659
3660                # Propagate dependencies to defaults
3661                if cur.defaults:
3662                    cur.defaults = [(default, self._make_and(cond, dep))
3663                                    for default, cond in cur.defaults]
3664
3665                # Propagate dependencies to ranges
3666                if cur.ranges:
3667                    cur.ranges = [(low, high, self._make_and(cond, dep))
3668                                  for low, high, cond in cur.ranges]
3669
3670                # Propagate dependencies to selects
3671                if cur.selects:
3672                    cur.selects = [(target, self._make_and(cond, dep))
3673                                   for target, cond in cur.selects]
3674
3675                # Propagate dependencies to implies
3676                if cur.implies:
3677                    cur.implies = [(target, self._make_and(cond, dep))
3678                                   for target, cond in cur.implies]
3679
3680            elif cur.prompt:  # Not a symbol/choice
3681                # Propagate dependencies to the prompt. 'visible if' is only
3682                # propagated to symbols/choices.
3683                cur.prompt = (cur.prompt[0],
3684                              self._make_and(cur.prompt[1], dep))
3685
3686            cur = cur.next
3687
3688    def _add_props_to_sym(self, node):
3689        # Copies properties from the menu node 'node' up to its contained
3690        # symbol, and adds (weak) reverse dependencies to selected/implied
3691        # symbols.
3692        #
3693        # This can't be rolled into _propagate_deps(), because that function
3694        # traverses the menu tree roughly breadth-first, meaning properties on
3695        # symbols defined in multiple locations could end up in the wrong
3696        # order.
3697
3698        sym = node.item
3699
3700        # See the Symbol class docstring
3701        sym.direct_dep = self._make_or(sym.direct_dep, node.dep)
3702
3703        sym.defaults += node.defaults
3704        sym.ranges += node.ranges
3705        sym.selects += node.selects
3706        sym.implies += node.implies
3707
3708        # Modify the reverse dependencies of the selected symbol
3709        for target, cond in node.selects:
3710            target.rev_dep = self._make_or(
3711                target.rev_dep,
3712                self._make_and(sym, cond))
3713
3714        # Modify the weak reverse dependencies of the implied
3715        # symbol
3716        for target, cond in node.implies:
3717            target.weak_rev_dep = self._make_or(
3718                target.weak_rev_dep,
3719                self._make_and(sym, cond))
3720
3721    #
3722    # Misc.
3723    #
3724
3725    def _check_sym_sanity(self):
3726        # Checks various symbol properties that are handiest to check after
3727        # parsing. Only generates errors and warnings.
3728
3729        def num_ok(sym, type_):
3730            # Returns True if the (possibly constant) symbol 'sym' is valid as a value
3731            # for a symbol of type type_ (INT or HEX)
3732
3733            # 'not sym.nodes' implies a constant or undefined symbol, e.g. a plain
3734            # "123"
3735            if not sym.nodes:
3736                return _is_base_n(sym.name, _TYPE_TO_BASE[type_])
3737
3738            return sym.orig_type is type_
3739
3740        for sym in self.unique_defined_syms:
3741            if sym.orig_type in _BOOL_TRISTATE:
3742                # A helper function could be factored out here, but keep it
3743                # speedy/straightforward
3744
3745                for target_sym, _ in sym.selects:
3746                    if target_sym.orig_type not in _BOOL_TRISTATE_UNKNOWN:
3747                        self._warn("{} selects the {} symbol {}, which is not "
3748                                   "bool or tristate"
3749                                   .format(sym.name_and_loc,
3750                                           TYPE_TO_STR[target_sym.orig_type],
3751                                           target_sym.name_and_loc))
3752
3753                for target_sym, _ in sym.implies:
3754                    if target_sym.orig_type not in _BOOL_TRISTATE_UNKNOWN:
3755                        self._warn("{} implies the {} symbol {}, which is not "
3756                                   "bool or tristate"
3757                                   .format(sym.name_and_loc,
3758                                           TYPE_TO_STR[target_sym.orig_type],
3759                                           target_sym.name_and_loc))
3760
3761            elif sym.orig_type:  # STRING/INT/HEX
3762                for default, _ in sym.defaults:
3763                    if default.__class__ is not Symbol:
3764                        raise KconfigError(
3765                            "the {} symbol {} has a malformed default {} -- "
3766                            "expected a single symbol"
3767                            .format(TYPE_TO_STR[sym.orig_type],
3768                                    sym.name_and_loc, expr_str(default)))
3769
3770                    if sym.orig_type is STRING:
3771                        if not default.is_constant and not default.nodes and \
3772                           not default.name.isupper():
3773                            # 'default foo' on a string symbol could be either a symbol
3774                            # reference or someone leaving out the quotes. Guess that
3775                            # the quotes were left out if 'foo' isn't all-uppercase
3776                            # (and no symbol named 'foo' exists).
3777                            self._warn("style: quotes recommended around "
3778                                       "default value for string symbol "
3779                                       + sym.name_and_loc)
3780
3781                    elif not num_ok(default, sym.orig_type):  # INT/HEX
3782                        self._warn("the {0} symbol {1} has a non-{0} default {2}"
3783                                   .format(TYPE_TO_STR[sym.orig_type],
3784                                           sym.name_and_loc,
3785                                           default.name_and_loc))
3786
3787                if sym.selects or sym.implies:
3788                    self._warn("the {} symbol {} has selects or implies"
3789                               .format(TYPE_TO_STR[sym.orig_type],
3790                                       sym.name_and_loc))
3791
3792            else:  # UNKNOWN
3793                self._warn("{} defined without a type"
3794                           .format(sym.name_and_loc))
3795
3796
3797            if sym.ranges:
3798                if sym.orig_type not in _INT_HEX:
3799                    self._warn(
3800                        "the {} symbol {} has ranges, but is not int or hex"
3801                        .format(TYPE_TO_STR[sym.orig_type],
3802                                sym.name_and_loc))
3803                else:
3804                    for low, high, _ in sym.ranges:
3805                        if not num_ok(low, sym.orig_type) or \
3806                           not num_ok(high, sym.orig_type):
3807
3808                            self._warn("the {0} symbol {1} has a non-{0} "
3809                                       "range [{2}, {3}]"
3810                                       .format(TYPE_TO_STR[sym.orig_type],
3811                                               sym.name_and_loc,
3812                                               low.name_and_loc,
3813                                               high.name_and_loc))
3814
3815    def _check_choice_sanity(self):
3816        # Checks various choice properties that are handiest to check after
3817        # parsing. Only generates errors and warnings.
3818
3819        def warn_select_imply(sym, expr, expr_type):
3820            msg = "the choice symbol {} is {} by the following symbols, but " \
3821                  "select/imply has no effect on choice symbols" \
3822                  .format(sym.name_and_loc, expr_type)
3823
3824            # si = select/imply
3825            for si in split_expr(expr, OR):
3826                msg += "\n - " + split_expr(si, AND)[0].name_and_loc
3827
3828            self._warn(msg)
3829
3830        for choice in self.unique_choices:
3831            if choice.orig_type not in _BOOL_TRISTATE:
3832                self._warn("{} defined with type {}"
3833                           .format(choice.name_and_loc,
3834                                   TYPE_TO_STR[choice.orig_type]))
3835
3836            for node in choice.nodes:
3837                if node.prompt:
3838                    break
3839            else:
3840                self._warn(choice.name_and_loc + " defined without a prompt")
3841
3842            for default, _ in choice.defaults:
3843                if default.__class__ is not Symbol:
3844                    raise KconfigError(
3845                        "{} has a malformed default {}"
3846                        .format(choice.name_and_loc, expr_str(default)))
3847
3848                if default.choice is not choice:
3849                    self._warn("the default selection {} of {} is not "
3850                               "contained in the choice"
3851                               .format(default.name_and_loc,
3852                                       choice.name_and_loc))
3853
3854            for sym in choice.syms:
3855                if sym.defaults:
3856                    self._warn("default on the choice symbol {} will have "
3857                               "no effect, as defaults do not affect choice "
3858                               "symbols".format(sym.name_and_loc))
3859
3860                if sym.rev_dep is not sym.kconfig.n:
3861                    warn_select_imply(sym, sym.rev_dep, "selected")
3862
3863                if sym.weak_rev_dep is not sym.kconfig.n:
3864                    warn_select_imply(sym, sym.weak_rev_dep, "implied")
3865
3866                for node in sym.nodes:
3867                    if node.parent.item is choice:
3868                        if not node.prompt:
3869                            self._warn("the choice symbol {} has no prompt"
3870                                       .format(sym.name_and_loc))
3871
3872                    elif node.prompt:
3873                        self._warn("the choice symbol {} is defined with a "
3874                                   "prompt outside the choice"
3875                                   .format(sym.name_and_loc))
3876
3877    def _parse_error(self, msg):
3878        raise KconfigError("{}error: couldn't parse '{}': {}".format(
3879            "" if self.filename is None else
3880                "{}:{}: ".format(self.filename, self.linenr),
3881            self._line.strip(), msg))
3882
3883    def _trailing_tokens_error(self):
3884        self._parse_error("extra tokens at end of line")
3885
3886    def _open(self, filename, mode):
3887        # open() wrapper:
3888        #
3889        # - Enable universal newlines mode on Python 2 to ease
3890        #   interoperability between Linux and Windows. It's already the
3891        #   default on Python 3.
3892        #
3893        #   The "U" flag would currently work for both Python 2 and 3, but it's
3894        #   deprecated on Python 3, so play it future-safe.
3895        #
3896        #   io.open() defaults to universal newlines on Python 2 (and is an
3897        #   alias for open() on Python 3), but it returns 'unicode' strings and
3898        #   slows things down:
3899        #
3900        #     Parsing x86 Kconfigs on Python 2
3901        #
3902        #     with open(..., "rU"):
3903        #
3904        #       real  0m0.930s
3905        #       user  0m0.905s
3906        #       sys   0m0.025s
3907        #
3908        #     with io.open():
3909        #
3910        #       real  0m1.069s
3911        #       user  0m1.040s
3912        #       sys   0m0.029s
3913        #
3914        #   There's no appreciable performance difference between "r" and
3915        #   "rU" for parsing performance on Python 2.
3916        #
3917        # - For Python 3, force the encoding. Forcing the encoding on Python 2
3918        #   turns strings into Unicode strings, which gets messy. Python 2
3919        #   doesn't decode regular strings anyway.
3920        return open(filename, "rU" if mode == "r" else mode) if _IS_PY2 else \
3921               open(filename, mode, encoding=self._encoding)
3922
3923    def _check_undef_syms(self):
3924        # Prints warnings for all references to undefined symbols within the
3925        # Kconfig files
3926
3927        def is_num(s):
3928            # Returns True if the string 's' looks like a number.
3929            #
3930            # Internally, all operands in Kconfig are symbols, only undefined symbols
3931            # (which numbers usually are) get their name as their value.
3932            #
3933            # Only hex numbers that start with 0x/0X are classified as numbers.
3934            # Otherwise, symbols whose names happen to contain only the letters A-F
3935            # would trigger false positives.
3936
3937            try:
3938                int(s)
3939            except ValueError:
3940                if not s.startswith(("0x", "0X")):
3941                    return False
3942
3943                try:
3944                    int(s, 16)
3945                except ValueError:
3946                    return False
3947
3948            return True
3949
3950        for sym in (self.syms.viewvalues if _IS_PY2 else self.syms.values)():
3951            # - sym.nodes empty means the symbol is undefined (has no
3952            #   definition locations)
3953            #
3954            # - Due to Kconfig internals, numbers show up as undefined Kconfig
3955            #   symbols, but shouldn't be flagged
3956            #
3957            # - The MODULES symbol always exists
3958            if not sym.nodes and not is_num(sym.name) and \
3959               sym.name != "MODULES":
3960
3961                msg = "undefined symbol {}:".format(sym.name)
3962                for node in self.node_iter():
3963                    if sym in node.referenced:
3964                        msg += "\n\n- Referenced at {}:{}:\n\n{}" \
3965                               .format(node.filename, node.linenr, node)
3966                self._warn(msg)
3967
3968    def _warn(self, msg, filename=None, linenr=None):
3969        # For printing general warnings
3970
3971        if not self.warn:
3972            return
3973
3974        msg = "warning: " + msg
3975        if filename is not None:
3976            msg = "{}:{}: {}".format(filename, linenr, msg)
3977
3978        self.warnings.append(msg)
3979        if self.warn_to_stderr:
3980            sys.stderr.write(msg + "\n")
3981
3982
3983class Symbol(object):
3984    """
3985    Represents a configuration symbol:
3986
3987      (menu)config FOO
3988          ...
3989
3990    The following attributes are available. They should be viewed as read-only,
3991    and some are implemented through @property magic (but are still efficient
3992    to access due to internal caching).
3993
3994    Note: Prompts, help texts, and locations are stored in the Symbol's
3995    MenuNode(s) rather than in the Symbol itself. Check the MenuNode class and
3996    the Symbol.nodes attribute. This organization matches the C tools.
3997
3998    name:
3999      The name of the symbol, e.g. "FOO" for 'config FOO'.
4000
4001    type:
4002      The type of the symbol. One of BOOL, TRISTATE, STRING, INT, HEX, UNKNOWN.
4003      UNKNOWN is for undefined symbols, (non-special) constant symbols, and
4004      symbols defined without a type.
4005
4006      When running without modules (MODULES having the value n), TRISTATE
4007      symbols magically change type to BOOL. This also happens for symbols
4008      within choices in "y" mode. This matches the C tools, and makes sense for
4009      menuconfig-like functionality.
4010
4011    orig_type:
4012      The type as given in the Kconfig file, without any magic applied. Used
4013      when printing the symbol.
4014
4015    tri_value:
4016      The tristate value of the symbol as an integer. One of 0, 1, 2,
4017      representing n, m, y. Always 0 (n) for non-bool/tristate symbols.
4018
4019      This is the symbol value that's used outside of relation expressions
4020      (A, !A, A && B, A || B).
4021
4022    str_value:
4023      The value of the symbol as a string. Gives the value for string/int/hex
4024      symbols. For bool/tristate symbols, gives "n", "m", or "y".
4025
4026      This is the symbol value that's used in relational expressions
4027      (A = B, A != B, etc.)
4028
4029      Gotcha: For int/hex symbols, the exact format of the value is often
4030      preserved (e.g. when writing a .config file), hence why you can't get it
4031      directly as an int. Do int(int_sym.str_value) or
4032      int(hex_sym.str_value, 16) to get the integer value.
4033
4034    user_value:
4035      The user value of the symbol. None if no user value has been assigned
4036      (via Kconfig.load_config() or Symbol.set_value()).
4037
4038      Holds 0, 1, or 2 for bool/tristate symbols, and a string for the other
4039      symbol types.
4040
4041      WARNING: Do not assign directly to this. It will break things. Use
4042      Symbol.set_value().
4043
4044    assignable:
4045      A tuple containing the tristate user values that can currently be
4046      assigned to the symbol (that would be respected), ordered from lowest (0,
4047      representing n) to highest (2, representing y). This corresponds to the
4048      selections available in the menuconfig interface. The set of assignable
4049      values is calculated from the symbol's visibility and selects/implies.
4050
4051      Returns the empty set for non-bool/tristate symbols and for symbols with
4052      visibility n. The other possible values are (0, 2), (0, 1, 2), (1, 2),
4053      (1,), and (2,). A (1,) or (2,) result means the symbol is visible but
4054      "locked" to m or y through a select, perhaps in combination with the
4055      visibility. menuconfig represents this as -M- and -*-, respectively.
4056
4057      For string/hex/int symbols, check if Symbol.visibility is non-0 (non-n)
4058      instead to determine if the value can be changed.
4059
4060      Some handy 'assignable' idioms:
4061
4062        # Is 'sym' an assignable (visible) bool/tristate symbol?
4063        if sym.assignable:
4064            # What's the highest value it can be assigned? [-1] in Python
4065            # gives the last element.
4066            sym_high = sym.assignable[-1]
4067
4068            # The lowest?
4069            sym_low = sym.assignable[0]
4070
4071            # Can the symbol be set to at least m?
4072            if sym.assignable[-1] >= 1:
4073                ...
4074
4075        # Can the symbol be set to m?
4076        if 1 in sym.assignable:
4077            ...
4078
4079    visibility:
4080      The visibility of the symbol. One of 0, 1, 2, representing n, m, y. See
4081      the module documentation for an overview of symbol values and visibility.
4082
4083    config_string:
4084      The .config assignment string that would get written out for the symbol
4085      by Kconfig.write_config(). Returns the empty string if no .config
4086      assignment would get written out.
4087
4088      In general, visible symbols, symbols with (active) defaults, and selected
4089      symbols get written out. This includes all non-n-valued bool/tristate
4090      symbols, and all visible string/int/hex symbols.
4091
4092      Symbols with the (no longer needed) 'option env=...' option generate no
4093      configuration output, and neither does the special
4094      'option defconfig_list' symbol.
4095
4096      Tip: This field is useful when generating custom configuration output,
4097      even for non-.config-like formats. To write just the symbols that would
4098      get written out to .config files, do this:
4099
4100        if sym.config_string:
4101            *Write symbol, e.g. by looking sym.str_value*
4102
4103      This is a superset of the symbols written out by write_autoconf().
4104      That function skips all n-valued symbols.
4105
4106      There usually won't be any great harm in just writing all symbols either,
4107      though you might get some special symbols and possibly some "redundant"
4108      n-valued symbol entries in there.
4109
4110    name_and_loc:
4111      Holds a string like
4112
4113        "MY_SYMBOL (defined at foo/Kconfig:12, bar/Kconfig:14)"
4114
4115      , giving the name of the symbol and its definition location(s).
4116
4117      If the symbol is undefined, the location is given as "(undefined)".
4118
4119    nodes:
4120      A list of MenuNodes for this symbol. Will contain a single MenuNode for
4121      most symbols. Undefined and constant symbols have an empty nodes list.
4122      Symbols defined in multiple locations get one node for each location.
4123
4124    choice:
4125      Holds the parent Choice for choice symbols, and None for non-choice
4126      symbols. Doubles as a flag for whether a symbol is a choice symbol.
4127
4128    defaults:
4129      List of (default, cond) tuples for the symbol's 'default' properties. For
4130      example, 'default A && B if C || D' is represented as
4131      ((AND, A, B), (OR, C, D)). If no condition was given, 'cond' is
4132      self.kconfig.y.
4133
4134      Note that 'depends on' and parent dependencies are propagated to
4135      'default' conditions.
4136
4137    selects:
4138      List of (symbol, cond) tuples for the symbol's 'select' properties. For
4139      example, 'select A if B && C' is represented as (A, (AND, B, C)). If no
4140      condition was given, 'cond' is self.kconfig.y.
4141
4142      Note that 'depends on' and parent dependencies are propagated to 'select'
4143      conditions.
4144
4145    implies:
4146      Like 'selects', for imply.
4147
4148    ranges:
4149      List of (low, high, cond) tuples for the symbol's 'range' properties. For
4150      example, 'range 1 2 if A' is represented as (1, 2, A). If there is no
4151      condition, 'cond' is self.kconfig.y.
4152
4153      Note that 'depends on' and parent dependencies are propagated to 'range'
4154      conditions.
4155
4156      Gotcha: 1 and 2 above will be represented as (undefined) Symbols rather
4157      than plain integers. Undefined symbols get their name as their string
4158      value, so this works out. The C tools work the same way.
4159
4160    orig_defaults:
4161    orig_selects:
4162    orig_implies:
4163    orig_ranges:
4164      See the corresponding attributes on the MenuNode class.
4165
4166    rev_dep:
4167      Reverse dependency expression from other symbols selecting this symbol.
4168      Multiple selections get ORed together. A condition on a select is ANDed
4169      with the selecting symbol.
4170
4171      For example, if A has 'select FOO' and B has 'select FOO if C', then
4172      FOO's rev_dep will be (OR, A, (AND, B, C)).
4173
4174    weak_rev_dep:
4175      Like rev_dep, for imply.
4176
4177    direct_dep:
4178      The direct ('depends on') dependencies for the symbol, or self.kconfig.y
4179      if there are no direct dependencies.
4180
4181      This attribute includes any dependencies from surrounding menus and ifs.
4182      Those get propagated to the direct dependencies, and the resulting direct
4183      dependencies in turn get propagated to the conditions of all properties.
4184
4185      If the symbol is defined in multiple locations, the dependencies from the
4186      different locations get ORed together.
4187
4188    referenced:
4189      A set() with all symbols and choices referenced in the properties and
4190      property conditions of the symbol.
4191
4192      Also includes dependencies from surrounding menus and ifs, because those
4193      get propagated to the symbol (see the 'Intro to symbol values' section in
4194      the module docstring).
4195
4196      Choices appear in the dependencies of choice symbols.
4197
4198      For the following definitions, only B and not C appears in A's
4199      'referenced'. To get transitive references, you'll have to recursively
4200      expand 'references' until no new items appear.
4201
4202        config A
4203                bool
4204                depends on B
4205
4206        config B
4207                bool
4208                depends on C
4209
4210        config C
4211                bool
4212
4213      See the Symbol.direct_dep attribute if you're only interested in the
4214      direct dependencies of the symbol (its 'depends on'). You can extract the
4215      symbols in it with the global expr_items() function.
4216
4217    env_var:
4218      If the Symbol has an 'option env="FOO"' option, this contains the name
4219      ("FOO") of the environment variable. None for symbols without no
4220      'option env'.
4221
4222      'option env="FOO"' acts like a 'default' property whose value is the
4223      value of $FOO.
4224
4225      Symbols with 'option env' are never written out to .config files, even if
4226      they are visible. env_var corresponds to a flag called SYMBOL_AUTO in the
4227      C implementation.
4228
4229    is_allnoconfig_y:
4230      True if the symbol has 'option allnoconfig_y' set on it. This has no
4231      effect internally (except when printing symbols), but can be checked by
4232      scripts.
4233
4234    is_constant:
4235      True if the symbol is a constant (quoted) symbol.
4236
4237    kconfig:
4238      The Kconfig instance this symbol is from.
4239    """
4240    __slots__ = (
4241        "_cached_assignable",
4242        "_cached_str_val",
4243        "_cached_tri_val",
4244        "_cached_vis",
4245        "_dependents",
4246        "_old_val",
4247        "_visited",
4248        "_was_set",
4249        "_write_to_conf",
4250        "choice",
4251        "defaults",
4252        "direct_dep",
4253        "env_var",
4254        "implies",
4255        "is_allnoconfig_y",
4256        "is_constant",
4257        "kconfig",
4258        "name",
4259        "nodes",
4260        "orig_type",
4261        "ranges",
4262        "rev_dep",
4263        "selects",
4264        "user_value",
4265        "weak_rev_dep",
4266    )
4267
4268    #
4269    # Public interface
4270    #
4271
4272    @property
4273    def type(self):
4274        """
4275        See the class documentation.
4276        """
4277        if self.orig_type is TRISTATE and \
4278           (self.choice and self.choice.tri_value == 2 or
4279            not self.kconfig.modules.tri_value):
4280
4281            return BOOL
4282
4283        return self.orig_type
4284
4285    @property
4286    def str_value(self):
4287        """
4288        See the class documentation.
4289        """
4290        if self._cached_str_val is not None:
4291            return self._cached_str_val
4292
4293        if self.orig_type in _BOOL_TRISTATE:
4294            # Also calculates the visibility, so invalidation safe
4295            self._cached_str_val = TRI_TO_STR[self.tri_value]
4296            return self._cached_str_val
4297
4298        # As a quirk of Kconfig, undefined symbols get their name as their
4299        # string value. This is why things like "FOO = bar" work for seeing if
4300        # FOO has the value "bar".
4301        if not self.orig_type:  # UNKNOWN
4302            self._cached_str_val = self.name
4303            return self.name
4304
4305        val = ""
4306        # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
4307        # function call (property magic)
4308        vis = self.visibility
4309
4310        self._write_to_conf = (vis != 0)
4311
4312        if self.orig_type in _INT_HEX:
4313            # The C implementation checks the user value against the range in a
4314            # separate code path (post-processing after loading a .config).
4315            # Checking all values here instead makes more sense for us. It
4316            # requires that we check for a range first.
4317
4318            base = _TYPE_TO_BASE[self.orig_type]
4319
4320            # Check if a range is in effect
4321            for low_expr, high_expr, cond in self.ranges:
4322                if expr_value(cond):
4323                    has_active_range = True
4324
4325                    # The zeros are from the C implementation running strtoll()
4326                    # on empty strings
4327                    low = int(low_expr.str_value, base) if \
4328                      _is_base_n(low_expr.str_value, base) else 0
4329                    high = int(high_expr.str_value, base) if \
4330                      _is_base_n(high_expr.str_value, base) else 0
4331
4332                    break
4333            else:
4334                has_active_range = False
4335
4336            # Defaults are used if the symbol is invisible, lacks a user value,
4337            # or has an out-of-range user value
4338            use_defaults = True
4339
4340            if vis and self.user_value:
4341                user_val = int(self.user_value, base)
4342                if has_active_range and not low <= user_val <= high:
4343                    num2str = str if base == 10 else hex
4344                    self.kconfig._warn(
4345                        "user value {} on the {} symbol {} ignored due to "
4346                        "being outside the active range ([{}, {}]) -- falling "
4347                        "back on defaults"
4348                        .format(num2str(user_val), TYPE_TO_STR[self.orig_type],
4349                                self.name_and_loc,
4350                                num2str(low), num2str(high)))
4351                else:
4352                    # If the user value is well-formed and satisfies range
4353                    # contraints, it is stored in exactly the same form as
4354                    # specified in the assignment (with or without "0x", etc.)
4355                    val = self.user_value
4356                    use_defaults = False
4357
4358            if use_defaults:
4359                # No user value or invalid user value. Look at defaults.
4360
4361                # Used to implement the warning below
4362                has_default = False
4363
4364                for sym, cond in self.defaults:
4365                    if expr_value(cond):
4366                        has_default = self._write_to_conf = True
4367
4368                        val = sym.str_value
4369
4370                        if _is_base_n(val, base):
4371                            val_num = int(val, base)
4372                        else:
4373                            val_num = 0  # strtoll() on empty string
4374
4375                        break
4376                else:
4377                    val_num = 0  # strtoll() on empty string
4378
4379                # This clamping procedure runs even if there's no default
4380                if has_active_range:
4381                    clamp = None
4382                    if val_num < low:
4383                        clamp = low
4384                    elif val_num > high:
4385                        clamp = high
4386
4387                    if clamp is not None:
4388                        # The value is rewritten to a standard form if it is
4389                        # clamped
4390                        val = str(clamp) \
4391                              if self.orig_type is INT else \
4392                              hex(clamp)
4393
4394                        if has_default:
4395                            num2str = str if base == 10 else hex
4396                            self.kconfig._warn(
4397                                "default value {} on {} clamped to {} due to "
4398                                "being outside the active range ([{}, {}])"
4399                                .format(val_num, self.name_and_loc,
4400                                        num2str(clamp), num2str(low),
4401                                        num2str(high)))
4402
4403        elif self.orig_type is STRING:
4404            if vis and self.user_value is not None:
4405                # If the symbol is visible and has a user value, use that
4406                val = self.user_value
4407            else:
4408                # Otherwise, look at defaults
4409                for sym, cond in self.defaults:
4410                    if expr_value(cond):
4411                        val = sym.str_value
4412                        self._write_to_conf = True
4413                        break
4414
4415        # env_var corresponds to SYMBOL_AUTO in the C implementation, and is
4416        # also set on the defconfig_list symbol there. Test for the
4417        # defconfig_list symbol explicitly instead here, to avoid a nonsensical
4418        # env_var setting and the defconfig_list symbol being printed
4419        # incorrectly. This code is pretty cold anyway.
4420        if self.env_var is not None or self is self.kconfig.defconfig_list:
4421            self._write_to_conf = False
4422
4423        self._cached_str_val = val
4424        return val
4425
4426    @property
4427    def tri_value(self):
4428        """
4429        See the class documentation.
4430        """
4431        if self._cached_tri_val is not None:
4432            return self._cached_tri_val
4433
4434        if self.orig_type not in _BOOL_TRISTATE:
4435            if self.orig_type:  # != UNKNOWN
4436                # Would take some work to give the location here
4437                self.kconfig._warn(
4438                    "The {} symbol {} is being evaluated in a logical context "
4439                    "somewhere. It will always evaluate to n."
4440                    .format(TYPE_TO_STR[self.orig_type], self.name_and_loc))
4441
4442            self._cached_tri_val = 0
4443            return 0
4444
4445        # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
4446        # function call (property magic)
4447        vis = self.visibility
4448        self._write_to_conf = (vis != 0)
4449
4450        val = 0
4451
4452        if not self.choice:
4453            # Non-choice symbol
4454
4455            if vis and self.user_value is not None:
4456                # If the symbol is visible and has a user value, use that
4457                val = min(self.user_value, vis)
4458
4459            else:
4460                # Otherwise, look at defaults and weak reverse dependencies
4461                # (implies)
4462
4463                for default, cond in self.defaults:
4464                    dep_val = expr_value(cond)
4465                    if dep_val:
4466                        val = min(expr_value(default), dep_val)
4467                        if val:
4468                            self._write_to_conf = True
4469                        break
4470
4471                # Weak reverse dependencies are only considered if our
4472                # direct dependencies are met
4473                dep_val = expr_value(self.weak_rev_dep)
4474                if dep_val and expr_value(self.direct_dep):
4475                    val = max(dep_val, val)
4476                    self._write_to_conf = True
4477
4478            # Reverse (select-related) dependencies take precedence
4479            dep_val = expr_value(self.rev_dep)
4480            if dep_val:
4481                if expr_value(self.direct_dep) < dep_val:
4482                    self._warn_select_unsatisfied_deps()
4483
4484                val = max(dep_val, val)
4485                self._write_to_conf = True
4486
4487            # m is promoted to y for (1) bool symbols and (2) symbols with a
4488            # weak_rev_dep (from imply) of y
4489            if val == 1 and \
4490               (self.type is BOOL or expr_value(self.weak_rev_dep) == 2):
4491                val = 2
4492
4493        elif vis == 2:
4494            # Visible choice symbol in y-mode choice. The choice mode limits
4495            # the visibility of choice symbols, so it's sufficient to just
4496            # check the visibility of the choice symbols themselves.
4497            val = 2 if self.choice.selection is self else 0
4498
4499        elif vis and self.user_value:
4500            # Visible choice symbol in m-mode choice, with set non-0 user value
4501            val = 1
4502
4503        self._cached_tri_val = val
4504        return val
4505
4506    @property
4507    def assignable(self):
4508        """
4509        See the class documentation.
4510        """
4511        if self._cached_assignable is None:
4512            self._cached_assignable = self._assignable()
4513        return self._cached_assignable
4514
4515    @property
4516    def visibility(self):
4517        """
4518        See the class documentation.
4519        """
4520        if self._cached_vis is None:
4521            self._cached_vis = _visibility(self)
4522        return self._cached_vis
4523
4524    @property
4525    def config_string(self):
4526        """
4527        See the class documentation.
4528        """
4529        # _write_to_conf is determined when the value is calculated. This is a
4530        # hidden function call due to property magic.
4531        val = self.str_value
4532        if not self._write_to_conf:
4533            return ""
4534
4535        if self.orig_type in _BOOL_TRISTATE:
4536            return "{}{}={}\n" \
4537                   .format(self.kconfig.config_prefix, self.name, val) \
4538                   if val != "n" else \
4539                   "# {}{} is not set\n" \
4540                   .format(self.kconfig.config_prefix, self.name)
4541
4542        if self.orig_type in _INT_HEX:
4543            return "{}{}={}\n" \
4544                   .format(self.kconfig.config_prefix, self.name, val)
4545
4546        # sym.orig_type is STRING
4547        return '{}{}="{}"\n' \
4548               .format(self.kconfig.config_prefix, self.name, escape(val))
4549
4550    @property
4551    def name_and_loc(self):
4552        """
4553        See the class documentation.
4554        """
4555        return self.name + " " + _locs(self)
4556
4557    def set_value(self, value):
4558        """
4559        Sets the user value of the symbol.
4560
4561        Equal in effect to assigning the value to the symbol within a .config
4562        file. For bool and tristate symbols, use the 'assignable' attribute to
4563        check which values can currently be assigned. Setting values outside
4564        'assignable' will cause Symbol.user_value to differ from
4565        Symbol.str/tri_value (be truncated down or up).
4566
4567        Setting a choice symbol to 2 (y) sets Choice.user_selection to the
4568        choice symbol in addition to setting Symbol.user_value.
4569        Choice.user_selection is considered when the choice is in y mode (the
4570        "normal" mode).
4571
4572        Other symbols that depend (possibly indirectly) on this symbol are
4573        automatically recalculated to reflect the assigned value.
4574
4575        value:
4576          The user value to give to the symbol. For bool and tristate symbols,
4577          n/m/y can be specified either as 0/1/2 (the usual format for tristate
4578          values in Kconfiglib) or as one of the strings "n", "m", or "y". For
4579          other symbol types, pass a string.
4580
4581          Note that the value for an int/hex symbol is passed as a string, e.g.
4582          "123" or "0x0123". The format of this string is preserved in the
4583          output.
4584
4585          Values that are invalid for the type (such as "foo" or 1 (m) for a
4586          BOOL or "0x123" for an INT) are ignored and won't be stored in
4587          Symbol.user_value. Kconfiglib will print a warning by default for
4588          invalid assignments, and set_value() will return False.
4589
4590        Returns True if the value is valid for the type of the symbol, and
4591        False otherwise. This only looks at the form of the value. For BOOL and
4592        TRISTATE symbols, check the Symbol.assignable attribute to see what
4593        values are currently in range and would actually be reflected in the
4594        value of the symbol. For other symbol types, check whether the
4595        visibility is non-n.
4596        """
4597        if self.orig_type in _BOOL_TRISTATE and value in STR_TO_TRI:
4598            value = STR_TO_TRI[value]
4599
4600        # If the new user value matches the old, nothing changes, and we can
4601        # avoid invalidating cached values.
4602        #
4603        # This optimization is skipped for choice symbols: Setting a choice
4604        # symbol's user value to y might change the state of the choice, so it
4605        # wouldn't be safe (symbol user values always match the values set in a
4606        # .config file or via set_value(), and are never implicitly updated).
4607        if value == self.user_value and not self.choice:
4608            self._was_set = True
4609            return True
4610
4611        # Check if the value is valid for our type
4612        if not (self.orig_type is BOOL     and value in (2, 0)     or
4613                self.orig_type is TRISTATE and value in TRI_TO_STR or
4614                value.__class__ is str and
4615                (self.orig_type is STRING                        or
4616                 self.orig_type is INT and _is_base_n(value, 10) or
4617                 self.orig_type is HEX and _is_base_n(value, 16)
4618                                       and int(value, 16) >= 0)):
4619
4620            # Display tristate values as n, m, y in the warning
4621            self.kconfig._warn(
4622                "the value {} is invalid for {}, which has type {} -- "
4623                "assignment ignored"
4624                .format(TRI_TO_STR[value] if value in TRI_TO_STR else
4625                            "'{}'".format(value),
4626                        self.name_and_loc, TYPE_TO_STR[self.orig_type]))
4627
4628            return False
4629
4630        self.user_value = value
4631        self._was_set = True
4632
4633        if self.choice and value == 2:
4634            # Setting a choice symbol to y makes it the user selection of the
4635            # choice. Like for symbol user values, the user selection is not
4636            # guaranteed to match the actual selection of the choice, as
4637            # dependencies come into play.
4638            self.choice.user_selection = self
4639            self.choice._was_set = True
4640            self.choice._rec_invalidate()
4641        else:
4642            self._rec_invalidate_if_has_prompt()
4643
4644        return True
4645
4646    def unset_value(self):
4647        """
4648        Removes any user value from the symbol, as if the symbol had never
4649        gotten a user value via Kconfig.load_config() or Symbol.set_value().
4650        """
4651        if self.user_value is not None:
4652            self.user_value = None
4653            self._rec_invalidate_if_has_prompt()
4654
4655    @property
4656    def referenced(self):
4657        """
4658        See the class documentation.
4659        """
4660        return {item for node in self.nodes for item in node.referenced}
4661
4662    @property
4663    def orig_defaults(self):
4664        """
4665        See the class documentation.
4666        """
4667        return [d for node in self.nodes for d in node.orig_defaults]
4668
4669    @property
4670    def orig_selects(self):
4671        """
4672        See the class documentation.
4673        """
4674        return [s for node in self.nodes for s in node.orig_selects]
4675
4676    @property
4677    def orig_implies(self):
4678        """
4679        See the class documentation.
4680        """
4681        return [i for node in self.nodes for i in node.orig_implies]
4682
4683    @property
4684    def orig_ranges(self):
4685        """
4686        See the class documentation.
4687        """
4688        return [r for node in self.nodes for r in node.orig_ranges]
4689
4690    def __repr__(self):
4691        """
4692        Returns a string with information about the symbol (including its name,
4693        value, visibility, and location(s)) when it is evaluated on e.g. the
4694        interactive Python prompt.
4695        """
4696        fields = ["symbol " + self.name, TYPE_TO_STR[self.type]]
4697        add = fields.append
4698
4699        for node in self.nodes:
4700            if node.prompt:
4701                add('"{}"'.format(node.prompt[0]))
4702
4703        # Only add quotes for non-bool/tristate symbols
4704        add("value " + (self.str_value if self.orig_type in _BOOL_TRISTATE
4705                        else '"{}"'.format(self.str_value)))
4706
4707        if not self.is_constant:
4708            # These aren't helpful to show for constant symbols
4709
4710            if self.user_value is not None:
4711                # Only add quotes for non-bool/tristate symbols
4712                add("user value " + (TRI_TO_STR[self.user_value]
4713                                     if self.orig_type in _BOOL_TRISTATE
4714                                     else '"{}"'.format(self.user_value)))
4715
4716            add("visibility " + TRI_TO_STR[self.visibility])
4717
4718            if self.choice:
4719                add("choice symbol")
4720
4721            if self.is_allnoconfig_y:
4722                add("allnoconfig_y")
4723
4724            if self is self.kconfig.defconfig_list:
4725                add("is the defconfig_list symbol")
4726
4727            if self.env_var is not None:
4728                add("from environment variable " + self.env_var)
4729
4730            if self is self.kconfig.modules:
4731                add("is the modules symbol")
4732
4733            add("direct deps " + TRI_TO_STR[expr_value(self.direct_dep)])
4734
4735        if self.nodes:
4736            for node in self.nodes:
4737                add("{}:{}".format(node.filename, node.linenr))
4738        else:
4739            add("constant" if self.is_constant else "undefined")
4740
4741        return "<{}>".format(", ".join(fields))
4742
4743    def __str__(self):
4744        """
4745        Returns a string representation of the symbol when it is printed.
4746        Matches the Kconfig format, with any parent dependencies propagated to
4747        the 'depends on' condition.
4748
4749        The string is constructed by joining the strings returned by
4750        MenuNode.__str__() for each of the symbol's menu nodes, so symbols
4751        defined in multiple locations will return a string with all
4752        definitions.
4753
4754        The returned string does not end in a newline. An empty string is
4755        returned for undefined and constant symbols.
4756        """
4757        return self.custom_str(standard_sc_expr_str)
4758
4759    def custom_str(self, sc_expr_str_fn):
4760        """
4761        Works like Symbol.__str__(), but allows a custom format to be used for
4762        all symbol/choice references. See expr_str().
4763        """
4764        return "\n\n".join(node.custom_str(sc_expr_str_fn)
4765                           for node in self.nodes)
4766
4767    #
4768    # Private methods
4769    #
4770
4771    def __init__(self):
4772        """
4773        Symbol constructor -- not intended to be called directly by Kconfiglib
4774        clients.
4775        """
4776        # These attributes are always set on the instance from outside and
4777        # don't need defaults:
4778        #   kconfig
4779        #   direct_dep
4780        #   is_constant
4781        #   name
4782        #   rev_dep
4783        #   weak_rev_dep
4784
4785        # - UNKNOWN == 0
4786        # - _visited is used during tree iteration and dep. loop detection
4787        self.orig_type = self._visited = 0
4788
4789        self.nodes = []
4790
4791        self.defaults = []
4792        self.selects = []
4793        self.implies = []
4794        self.ranges = []
4795
4796        self.user_value = \
4797        self.choice = \
4798        self.env_var = \
4799        self._cached_str_val = self._cached_tri_val = self._cached_vis = \
4800        self._cached_assignable = None
4801
4802        # _write_to_conf is calculated along with the value. If True, the
4803        # Symbol gets a .config entry.
4804
4805        self.is_allnoconfig_y = \
4806        self._was_set = \
4807        self._write_to_conf = False
4808
4809        # See Kconfig._build_dep()
4810        self._dependents = set()
4811
4812    def _assignable(self):
4813        # Worker function for the 'assignable' attribute
4814
4815        if self.orig_type not in _BOOL_TRISTATE:
4816            return ()
4817
4818        # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
4819        # function call (property magic)
4820        vis = self.visibility
4821        if not vis:
4822            return ()
4823
4824        rev_dep_val = expr_value(self.rev_dep)
4825
4826        if vis == 2:
4827            if self.choice:
4828                return (2,)
4829
4830            if not rev_dep_val:
4831                if self.type is BOOL or expr_value(self.weak_rev_dep) == 2:
4832                    return (0, 2)
4833                return (0, 1, 2)
4834
4835            if rev_dep_val == 2:
4836                return (2,)
4837
4838            # rev_dep_val == 1
4839
4840            if self.type is BOOL or expr_value(self.weak_rev_dep) == 2:
4841                return (2,)
4842            return (1, 2)
4843
4844        # vis == 1
4845
4846        # Must be a tristate here, because bool m visibility gets promoted to y
4847
4848        if not rev_dep_val:
4849            return (0, 1) if expr_value(self.weak_rev_dep) != 2 else (0, 2)
4850
4851        if rev_dep_val == 2:
4852            return (2,)
4853
4854        # vis == rev_dep_val == 1
4855
4856        return (1,)
4857
4858    def _invalidate(self):
4859        # Marks the symbol as needing to be recalculated
4860
4861        self._cached_str_val = self._cached_tri_val = self._cached_vis = \
4862        self._cached_assignable = None
4863
4864    def _rec_invalidate(self):
4865        # Invalidates the symbol and all items that (possibly) depend on it
4866
4867        if self is self.kconfig.modules:
4868            # Invalidating MODULES has wide-ranging effects
4869            self.kconfig._invalidate_all()
4870        else:
4871            self._invalidate()
4872
4873            for item in self._dependents:
4874                # _cached_vis doubles as a flag that tells us whether 'item'
4875                # has cached values, because it's calculated as a side effect
4876                # of calculating all other (non-constant) cached values.
4877                #
4878                # If item._cached_vis is None, it means there can't be cached
4879                # values on other items that depend on 'item', because if there
4880                # were, some value on 'item' would have been calculated and
4881                # item._cached_vis set as a side effect. It's therefore safe to
4882                # stop the invalidation at symbols with _cached_vis None.
4883                #
4884                # This approach massively speeds up scripts that set a lot of
4885                # values, vs simply invalidating all possibly dependent symbols
4886                # (even when you already have a list of all the dependent
4887                # symbols, because some symbols get huge dependency trees).
4888                #
4889                # This gracefully handles dependency loops too, which is nice
4890                # for choices, where the choice depends on the choice symbols
4891                # and vice versa.
4892                if item._cached_vis is not None:
4893                    item._rec_invalidate()
4894
4895    def _rec_invalidate_if_has_prompt(self):
4896        # Invalidates the symbol and its dependent symbols, but only if the
4897        # symbol has a prompt. User values never have an effect on promptless
4898        # symbols, so we skip invalidation for them as an optimization.
4899        #
4900        # This also prevents constant (quoted) symbols from being invalidated
4901        # if set_value() is called on them, which would make them lose their
4902        # value and break things.
4903        #
4904        # Prints a warning if the symbol has no prompt. In some contexts (e.g.
4905        # when loading a .config files) assignments to promptless symbols are
4906        # normal and expected, so the warning can be disabled.
4907
4908        for node in self.nodes:
4909            if node.prompt:
4910                self._rec_invalidate()
4911                return
4912
4913        if self.kconfig._warn_assign_no_prompt:
4914            self.kconfig._warn(self.name_and_loc + " has no prompt, meaning "
4915                               "user values have no effect on it")
4916
4917    def _str_default(self):
4918        # write_min_config() helper function. Returns the value the symbol
4919        # would get from defaults if it didn't have a user value. Uses exactly
4920        # the same algorithm as the C implementation (though a bit cleaned up),
4921        # for compatibility.
4922
4923        if self.orig_type in _BOOL_TRISTATE:
4924            val = 0
4925
4926            # Defaults, selects, and implies do not affect choice symbols
4927            if not self.choice:
4928                for default, cond in self.defaults:
4929                    cond_val = expr_value(cond)
4930                    if cond_val:
4931                        val = min(expr_value(default), cond_val)
4932                        break
4933
4934                val = max(expr_value(self.rev_dep),
4935                          expr_value(self.weak_rev_dep),
4936                          val)
4937
4938                # Transpose mod to yes if type is bool (possibly due to modules
4939                # being disabled)
4940                if val == 1 and self.type is BOOL:
4941                    val = 2
4942
4943            return TRI_TO_STR[val]
4944
4945        if self.orig_type:  # STRING/INT/HEX
4946            for default, cond in self.defaults:
4947                if expr_value(cond):
4948                    return default.str_value
4949
4950        return ""
4951
4952    def _warn_select_unsatisfied_deps(self):
4953        # Helper for printing an informative warning when a symbol with
4954        # unsatisfied direct dependencies (dependencies from 'depends on', ifs,
4955        # and menus) is selected by some other symbol. Also warn if a symbol
4956        # whose direct dependencies evaluate to m is selected to y.
4957
4958        msg = "{} has direct dependencies {} with value {}, but is " \
4959              "currently being {}-selected by the following symbols:" \
4960              .format(self.name_and_loc, expr_str(self.direct_dep),
4961                      TRI_TO_STR[expr_value(self.direct_dep)],
4962                      TRI_TO_STR[expr_value(self.rev_dep)])
4963
4964        # The reverse dependencies from each select are ORed together
4965        for select in split_expr(self.rev_dep, OR):
4966            if expr_value(select) <= expr_value(self.direct_dep):
4967                # Only include selects that exceed the direct dependencies
4968                continue
4969
4970            # - 'select A if B' turns into A && B
4971            # - 'select A' just turns into A
4972            #
4973            # In both cases, we can split on AND and pick the first operand
4974            selecting_sym = split_expr(select, AND)[0]
4975
4976            msg += "\n - {}, with value {}, direct dependencies {} " \
4977                   "(value: {})" \
4978                   .format(selecting_sym.name_and_loc,
4979                           selecting_sym.str_value,
4980                           expr_str(selecting_sym.direct_dep),
4981                           TRI_TO_STR[expr_value(selecting_sym.direct_dep)])
4982
4983            if select.__class__ is tuple:
4984                msg += ", and select condition {} (value: {})" \
4985                       .format(expr_str(select[2]),
4986                               TRI_TO_STR[expr_value(select[2])])
4987
4988        self.kconfig._warn(msg)
4989
4990
4991class Choice(object):
4992    """
4993    Represents a choice statement:
4994
4995      choice
4996          ...
4997      endchoice
4998
4999    The following attributes are available on Choice instances. They should be
5000    treated as read-only, and some are implemented through @property magic (but
5001    are still efficient to access due to internal caching).
5002
5003    Note: Prompts, help texts, and locations are stored in the Choice's
5004    MenuNode(s) rather than in the Choice itself. Check the MenuNode class and
5005    the Choice.nodes attribute. This organization matches the C tools.
5006
5007    name:
5008      The name of the choice, e.g. "FOO" for 'choice FOO', or None if the
5009      Choice has no name.
5010
5011    type:
5012      The type of the choice. One of BOOL, TRISTATE, UNKNOWN. UNKNOWN is for
5013      choices defined without a type where none of the contained symbols have a
5014      type either (otherwise the choice inherits the type of the first symbol
5015      defined with a type).
5016
5017      When running without modules (CONFIG_MODULES=n), TRISTATE choices
5018      magically change type to BOOL. This matches the C tools, and makes sense
5019      for menuconfig-like functionality.
5020
5021    orig_type:
5022      The type as given in the Kconfig file, without any magic applied. Used
5023      when printing the choice.
5024
5025    tri_value:
5026      The tristate value (mode) of the choice. A choice can be in one of three
5027      modes:
5028
5029        0 (n) - The choice is disabled and no symbols can be selected. For
5030                visible choices, this mode is only possible for choices with
5031                the 'optional' flag set (see kconfig-language.txt).
5032
5033        1 (m) - Any number of choice symbols can be set to m, the rest will
5034                be n.
5035
5036        2 (y) - One symbol will be y, the rest n.
5037
5038      Only tristate choices can be in m mode. The visibility of the choice is
5039      an upper bound on the mode, and the mode in turn is an upper bound on the
5040      visibility of the choice symbols.
5041
5042      To change the mode, use Choice.set_value().
5043
5044      Implementation note:
5045        The C tools internally represent choices as a type of symbol, with
5046        special-casing in many code paths. This is why there is a lot of
5047        similarity to Symbol. The value (mode) of a choice is really just a
5048        normal symbol value, and an implicit reverse dependency forces its
5049        lower bound to m for visible non-optional choices (the reverse
5050        dependency is 'm && <visibility>').
5051
5052        Symbols within choices get the choice propagated as a dependency to
5053        their properties. This turns the mode of the choice into an upper bound
5054        on e.g. the visibility of choice symbols, and explains the gotcha
5055        related to printing choice symbols mentioned in the module docstring.
5056
5057        Kconfiglib uses a separate Choice class only because it makes the code
5058        and interface less confusing (especially in a user-facing interface).
5059        Corresponding attributes have the same name in the Symbol and Choice
5060        classes, for consistency and compatibility.
5061
5062    str_value:
5063      Like choice.tri_value, but gives the value as one of the strings
5064      "n", "m", or "y"
5065
5066    user_value:
5067      The value (mode) selected by the user through Choice.set_value(). Either
5068      0, 1, or 2, or None if the user hasn't selected a mode. See
5069      Symbol.user_value.
5070
5071      WARNING: Do not assign directly to this. It will break things. Use
5072      Choice.set_value() instead.
5073
5074    assignable:
5075      See the symbol class documentation. Gives the assignable values (modes).
5076
5077    selection:
5078      The Symbol instance of the currently selected symbol. None if the Choice
5079      is not in y mode or has no selected symbol (due to unsatisfied
5080      dependencies on choice symbols).
5081
5082      WARNING: Do not assign directly to this. It will break things. Call
5083      sym.set_value(2) on the choice symbol you want to select instead.
5084
5085    user_selection:
5086      The symbol selected by the user (by setting it to y). Ignored if the
5087      choice is not in y mode, but still remembered so that the choice "snaps
5088      back" to the user selection if the mode is changed back to y. This might
5089      differ from 'selection' due to unsatisfied dependencies.
5090
5091      WARNING: Do not assign directly to this. It will break things. Call
5092      sym.set_value(2) on the choice symbol to be selected instead.
5093
5094    visibility:
5095      See the Symbol class documentation. Acts on the value (mode).
5096
5097    name_and_loc:
5098      Holds a string like
5099
5100        "<choice MY_CHOICE> (defined at foo/Kconfig:12)"
5101
5102      , giving the name of the choice and its definition location(s). If the
5103      choice has no name (isn't defined with 'choice MY_CHOICE'), then it will
5104      be shown as "<choice>" before the list of locations (always a single one
5105      in that case).
5106
5107    syms:
5108      List of symbols contained in the choice.
5109
5110      Obscure gotcha: If a symbol depends on the previous symbol within a
5111      choice so that an implicit menu is created, it won't be a choice symbol,
5112      and won't be included in 'syms'.
5113
5114    nodes:
5115      A list of MenuNodes for this choice. In practice, the list will probably
5116      always contain a single MenuNode, but it is possible to give a choice a
5117      name and define it in multiple locations.
5118
5119    defaults:
5120      List of (symbol, cond) tuples for the choice's 'defaults' properties. For
5121      example, 'default A if B && C' is represented as (A, (AND, B, C)). If
5122      there is no condition, 'cond' is self.kconfig.y.
5123
5124      Note that 'depends on' and parent dependencies are propagated to
5125      'default' conditions.
5126
5127    orig_defaults:
5128      See the corresponding attribute on the MenuNode class.
5129
5130    direct_dep:
5131      See Symbol.direct_dep.
5132
5133    referenced:
5134      A set() with all symbols referenced in the properties and property
5135      conditions of the choice.
5136
5137      Also includes dependencies from surrounding menus and ifs, because those
5138      get propagated to the choice (see the 'Intro to symbol values' section in
5139      the module docstring).
5140
5141    is_optional:
5142      True if the choice has the 'optional' flag set on it and can be in
5143      n mode.
5144
5145    kconfig:
5146      The Kconfig instance this choice is from.
5147    """
5148    __slots__ = (
5149        "_cached_assignable",
5150        "_cached_selection",
5151        "_cached_vis",
5152        "_dependents",
5153        "_visited",
5154        "_was_set",
5155        "defaults",
5156        "direct_dep",
5157        "is_constant",
5158        "is_optional",
5159        "kconfig",
5160        "name",
5161        "nodes",
5162        "orig_type",
5163        "syms",
5164        "user_selection",
5165        "user_value",
5166    )
5167
5168    #
5169    # Public interface
5170    #
5171
5172    @property
5173    def type(self):
5174        """
5175        Returns the type of the choice. See Symbol.type.
5176        """
5177        if self.orig_type is TRISTATE and not self.kconfig.modules.tri_value:
5178            return BOOL
5179        return self.orig_type
5180
5181    @property
5182    def str_value(self):
5183        """
5184        See the class documentation.
5185        """
5186        return TRI_TO_STR[self.tri_value]
5187
5188    @property
5189    def tri_value(self):
5190        """
5191        See the class documentation.
5192        """
5193        # This emulates a reverse dependency of 'm && visibility' for
5194        # non-optional choices, which is how the C implementation does it
5195
5196        val = 0 if self.is_optional else 1
5197
5198        if self.user_value is not None:
5199            val = max(val, self.user_value)
5200
5201        # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
5202        # function call (property magic)
5203        val = min(val, self.visibility)
5204
5205        # Promote m to y for boolean choices
5206        return 2 if val == 1 and self.type is BOOL else val
5207
5208    @property
5209    def assignable(self):
5210        """
5211        See the class documentation.
5212        """
5213        if self._cached_assignable is None:
5214            self._cached_assignable = self._assignable()
5215        return self._cached_assignable
5216
5217    @property
5218    def visibility(self):
5219        """
5220        See the class documentation.
5221        """
5222        if self._cached_vis is None:
5223            self._cached_vis = _visibility(self)
5224        return self._cached_vis
5225
5226    @property
5227    def name_and_loc(self):
5228        """
5229        See the class documentation.
5230        """
5231        # Reuse the expression format, which is '<choice (name, if any)>'.
5232        return standard_sc_expr_str(self) + " " + _locs(self)
5233
5234    @property
5235    def selection(self):
5236        """
5237        See the class documentation.
5238        """
5239        if self._cached_selection is _NO_CACHED_SELECTION:
5240            self._cached_selection = self._selection()
5241        return self._cached_selection
5242
5243    def set_value(self, value):
5244        """
5245        Sets the user value (mode) of the choice. Like for Symbol.set_value(),
5246        the visibility might truncate the value. Choices without the 'optional'
5247        attribute (is_optional) can never be in n mode, but 0/"n" is still
5248        accepted since it's not a malformed value (though it will have no
5249        effect).
5250
5251        Returns True if the value is valid for the type of the choice, and
5252        False otherwise. This only looks at the form of the value. Check the
5253        Choice.assignable attribute to see what values are currently in range
5254        and would actually be reflected in the mode of the choice.
5255        """
5256        if value in STR_TO_TRI:
5257            value = STR_TO_TRI[value]
5258
5259        if value == self.user_value:
5260            # We know the value must be valid if it was successfully set
5261            # previously
5262            self._was_set = True
5263            return True
5264
5265        if not (self.orig_type is BOOL     and value in (2, 0) or
5266                self.orig_type is TRISTATE and value in TRI_TO_STR):
5267
5268            # Display tristate values as n, m, y in the warning
5269            self.kconfig._warn(
5270                "the value {} is invalid for {}, which has type {} -- "
5271                "assignment ignored"
5272                .format(TRI_TO_STR[value] if value in TRI_TO_STR else
5273                            "'{}'".format(value),
5274                        self.name_and_loc, TYPE_TO_STR[self.orig_type]))
5275
5276            return False
5277
5278        self.user_value = value
5279        self._was_set = True
5280        self._rec_invalidate()
5281
5282        return True
5283
5284    def unset_value(self):
5285        """
5286        Resets the user value (mode) and user selection of the Choice, as if
5287        the user had never touched the mode or any of the choice symbols.
5288        """
5289        if self.user_value is not None or self.user_selection:
5290            self.user_value = self.user_selection = None
5291            self._rec_invalidate()
5292
5293    @property
5294    def referenced(self):
5295        """
5296        See the class documentation.
5297        """
5298        return {item for node in self.nodes for item in node.referenced}
5299
5300    @property
5301    def orig_defaults(self):
5302        """
5303        See the class documentation.
5304        """
5305        return [d for node in self.nodes for d in node.orig_defaults]
5306
5307    def __repr__(self):
5308        """
5309        Returns a string with information about the choice when it is evaluated
5310        on e.g. the interactive Python prompt.
5311        """
5312        fields = ["choice " + self.name if self.name else "choice",
5313                  TYPE_TO_STR[self.type]]
5314        add = fields.append
5315
5316        for node in self.nodes:
5317            if node.prompt:
5318                add('"{}"'.format(node.prompt[0]))
5319
5320        add("mode " + self.str_value)
5321
5322        if self.user_value is not None:
5323            add('user mode {}'.format(TRI_TO_STR[self.user_value]))
5324
5325        if self.selection:
5326            add("{} selected".format(self.selection.name))
5327
5328        if self.user_selection:
5329            user_sel_str = "{} selected by user" \
5330                           .format(self.user_selection.name)
5331
5332            if self.selection is not self.user_selection:
5333                user_sel_str += " (overridden)"
5334
5335            add(user_sel_str)
5336
5337        add("visibility " + TRI_TO_STR[self.visibility])
5338
5339        if self.is_optional:
5340            add("optional")
5341
5342        for node in self.nodes:
5343            add("{}:{}".format(node.filename, node.linenr))
5344
5345        return "<{}>".format(", ".join(fields))
5346
5347    def __str__(self):
5348        """
5349        Returns a string representation of the choice when it is printed.
5350        Matches the Kconfig format (though without the contained choice
5351        symbols), with any parent dependencies propagated to the 'depends on'
5352        condition.
5353
5354        The returned string does not end in a newline.
5355
5356        See Symbol.__str__() as well.
5357        """
5358        return self.custom_str(standard_sc_expr_str)
5359
5360    def custom_str(self, sc_expr_str_fn):
5361        """
5362        Works like Choice.__str__(), but allows a custom format to be used for
5363        all symbol/choice references. See expr_str().
5364        """
5365        return "\n\n".join(node.custom_str(sc_expr_str_fn)
5366                           for node in self.nodes)
5367
5368    #
5369    # Private methods
5370    #
5371
5372    def __init__(self):
5373        """
5374        Choice constructor -- not intended to be called directly by Kconfiglib
5375        clients.
5376        """
5377        # These attributes are always set on the instance from outside and
5378        # don't need defaults:
5379        #   direct_dep
5380        #   kconfig
5381
5382        # - UNKNOWN == 0
5383        # - _visited is used during dep. loop detection
5384        self.orig_type = self._visited = 0
5385
5386        self.nodes = []
5387
5388        self.syms = []
5389        self.defaults = []
5390
5391        self.name = \
5392        self.user_value = self.user_selection = \
5393        self._cached_vis = self._cached_assignable = None
5394
5395        self._cached_selection = _NO_CACHED_SELECTION
5396
5397        # is_constant is checked by _depend_on(). Just set it to avoid having
5398        # to special-case choices.
5399        self.is_constant = self.is_optional = False
5400
5401        # See Kconfig._build_dep()
5402        self._dependents = set()
5403
5404    def _assignable(self):
5405        # Worker function for the 'assignable' attribute
5406
5407        # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
5408        # function call (property magic)
5409        vis = self.visibility
5410
5411        if not vis:
5412            return ()
5413
5414        if vis == 2:
5415            if not self.is_optional:
5416                return (2,) if self.type is BOOL else (1, 2)
5417            return (0, 2) if self.type is BOOL else (0, 1, 2)
5418
5419        # vis == 1
5420
5421        return (0, 1) if self.is_optional else (1,)
5422
5423    def _selection(self):
5424        # Worker function for the 'selection' attribute
5425
5426        # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
5427        # function call (property magic)
5428        if self.tri_value != 2:
5429            # Not in y mode, so no selection
5430            return None
5431
5432        # Use the user selection if it's visible
5433        if self.user_selection and self.user_selection.visibility:
5434            return self.user_selection
5435
5436        # Otherwise, check if we have a default
5437        return self._selection_from_defaults()
5438
5439    def _selection_from_defaults(self):
5440        # Check if we have a default
5441        for sym, cond in self.defaults:
5442            # The default symbol must be visible too
5443            if expr_value(cond) and sym.visibility:
5444                return sym
5445
5446        # Otherwise, pick the first visible symbol, if any
5447        for sym in self.syms:
5448            if sym.visibility:
5449                return sym
5450
5451        # Couldn't find a selection
5452        return None
5453
5454    def _invalidate(self):
5455        self._cached_vis = self._cached_assignable = None
5456        self._cached_selection = _NO_CACHED_SELECTION
5457
5458    def _rec_invalidate(self):
5459        # See Symbol._rec_invalidate()
5460
5461        self._invalidate()
5462
5463        for item in self._dependents:
5464            if item._cached_vis is not None:
5465                item._rec_invalidate()
5466
5467
5468class MenuNode(object):
5469    """
5470    Represents a menu node in the configuration. This corresponds to an entry
5471    in e.g. the 'make menuconfig' interface, though non-visible choices, menus,
5472    and comments also get menu nodes. If a symbol or choice is defined in
5473    multiple locations, it gets one menu node for each location.
5474
5475    The top-level menu node, corresponding to the implicit top-level menu, is
5476    available in Kconfig.top_node.
5477
5478    The menu nodes for a Symbol or Choice can be found in the
5479    Symbol/Choice.nodes attribute. Menus and comments are represented as plain
5480    menu nodes, with their text stored in the prompt attribute (prompt[0]).
5481    This mirrors the C implementation.
5482
5483    The following attributes are available on MenuNode instances. They should
5484    be viewed as read-only.
5485
5486    item:
5487      Either a Symbol, a Choice, or one of the constants MENU and COMMENT.
5488      Menus and comments are represented as plain menu nodes. Ifs are collapsed
5489      (matching the C implementation) and do not appear in the final menu tree.
5490
5491    next:
5492      The following menu node. None if there is no following node.
5493
5494    list:
5495      The first child menu node. None if there are no children.
5496
5497      Choices and menus naturally have children, but Symbols can also have
5498      children because of menus created automatically from dependencies (see
5499      kconfig-language.txt).
5500
5501    parent:
5502      The parent menu node. None if there is no parent.
5503
5504    prompt:
5505      A (string, cond) tuple with the prompt for the menu node and its
5506      conditional expression (which is self.kconfig.y if there is no
5507      condition). None if there is no prompt.
5508
5509      For symbols and choices, the prompt is stored in the MenuNode rather than
5510      the Symbol or Choice instance. For menus and comments, the prompt holds
5511      the text.
5512
5513    defaults:
5514      The 'default' properties for this particular menu node. See
5515      symbol.defaults.
5516
5517      When evaluating defaults, you should use Symbol/Choice.defaults instead,
5518      as it include properties from all menu nodes (a symbol/choice can have
5519      multiple definition locations/menu nodes). MenuNode.defaults is meant for
5520      documentation generation.
5521
5522    selects:
5523      Like MenuNode.defaults, for selects.
5524
5525    implies:
5526      Like MenuNode.defaults, for implies.
5527
5528    ranges:
5529      Like MenuNode.defaults, for ranges.
5530
5531    orig_prompt:
5532    orig_defaults:
5533    orig_selects:
5534    orig_implies:
5535    orig_ranges:
5536      These work the like the corresponding attributes without orig_*, but omit
5537      any dependencies propagated from 'depends on' and surrounding 'if's (the
5538      direct dependencies, stored in MenuNode.dep).
5539
5540      One use for this is generating less cluttered documentation, by only
5541      showing the direct dependencies in one place.
5542
5543    help:
5544      The help text for the menu node for Symbols and Choices. None if there is
5545      no help text. Always stored in the node rather than the Symbol or Choice.
5546      It is possible to have a separate help text at each location if a symbol
5547      is defined in multiple locations.
5548
5549      Trailing whitespace (including a final newline) is stripped from the help
5550      text. This was not the case before Kconfiglib 10.21.0, where the format
5551      was undocumented.
5552
5553    dep:
5554      The direct ('depends on') dependencies for the menu node, or
5555      self.kconfig.y if there are no direct dependencies.
5556
5557      This attribute includes any dependencies from surrounding menus and ifs.
5558      Those get propagated to the direct dependencies, and the resulting direct
5559      dependencies in turn get propagated to the conditions of all properties.
5560
5561      If a symbol or choice is defined in multiple locations, only the
5562      properties defined at a particular location get the corresponding
5563      MenuNode.dep dependencies propagated to them.
5564
5565    visibility:
5566      The 'visible if' dependencies for the menu node (which must represent a
5567      menu), or self.kconfig.y if there are no 'visible if' dependencies.
5568      'visible if' dependencies are recursively propagated to the prompts of
5569      symbols and choices within the menu.
5570
5571    referenced:
5572      A set() with all symbols and choices referenced in the properties and
5573      property conditions of the menu node.
5574
5575      Also includes dependencies inherited from surrounding menus and ifs.
5576      Choices appear in the dependencies of choice symbols.
5577
5578    is_menuconfig:
5579      Set to True if the children of the menu node should be displayed in a
5580      separate menu. This is the case for the following items:
5581
5582        - Menus (node.item == MENU)
5583
5584        - Choices
5585
5586        - Symbols defined with the 'menuconfig' keyword. The children come from
5587          implicitly created submenus, and should be displayed in a separate
5588          menu rather than being indented.
5589
5590      'is_menuconfig' is just a hint on how to display the menu node. It's
5591      ignored internally by Kconfiglib, except when printing symbols.
5592
5593    filename/linenr:
5594      The location where the menu node appears. The filename is relative to
5595      $srctree (or to the current directory if $srctree isn't set), except
5596      absolute paths are used for paths outside $srctree.
5597
5598    include_path:
5599      A tuple of (filename, linenr) tuples, giving the locations of the
5600      'source' statements via which the Kconfig file containing this menu node
5601      was included. The first element is the location of the 'source' statement
5602      in the top-level Kconfig file passed to Kconfig.__init__(), etc.
5603
5604      Note that the Kconfig file of the menu node itself isn't included. Check
5605      'filename' and 'linenr' for that.
5606
5607    kconfig:
5608      The Kconfig instance the menu node is from.
5609    """
5610    __slots__ = (
5611        "dep",
5612        "filename",
5613        "help",
5614        "include_path",
5615        "is_menuconfig",
5616        "item",
5617        "kconfig",
5618        "linenr",
5619        "list",
5620        "next",
5621        "parent",
5622        "prompt",
5623        "visibility",
5624
5625        # Properties
5626        "defaults",
5627        "selects",
5628        "implies",
5629        "ranges",
5630    )
5631
5632    def __init__(self):
5633        # Properties defined on this particular menu node. A local 'depends on'
5634        # only applies to these, in case a symbol is defined in multiple
5635        # locations.
5636        self.defaults = []
5637        self.selects = []
5638        self.implies = []
5639        self.ranges = []
5640
5641    @property
5642    def orig_prompt(self):
5643        """
5644        See the class documentation.
5645        """
5646        if not self.prompt:
5647            return None
5648        return (self.prompt[0], self._strip_dep(self.prompt[1]))
5649
5650    @property
5651    def orig_defaults(self):
5652        """
5653        See the class documentation.
5654        """
5655        return [(default, self._strip_dep(cond))
5656                for default, cond in self.defaults]
5657
5658    @property
5659    def orig_selects(self):
5660        """
5661        See the class documentation.
5662        """
5663        return [(select, self._strip_dep(cond))
5664                for select, cond in self.selects]
5665
5666    @property
5667    def orig_implies(self):
5668        """
5669        See the class documentation.
5670        """
5671        return [(imply, self._strip_dep(cond))
5672                for imply, cond in self.implies]
5673
5674    @property
5675    def orig_ranges(self):
5676        """
5677        See the class documentation.
5678        """
5679        return [(low, high, self._strip_dep(cond))
5680                for low, high, cond in self.ranges]
5681
5682    @property
5683    def referenced(self):
5684        """
5685        See the class documentation.
5686        """
5687        # self.dep is included to catch dependencies from a lone 'depends on'
5688        # when there are no properties to propagate it to
5689        res = expr_items(self.dep)
5690
5691        if self.prompt:
5692            res |= expr_items(self.prompt[1])
5693
5694        if self.item is MENU:
5695            res |= expr_items(self.visibility)
5696
5697        for value, cond in self.defaults:
5698            res |= expr_items(value)
5699            res |= expr_items(cond)
5700
5701        for value, cond in self.selects:
5702            res.add(value)
5703            res |= expr_items(cond)
5704
5705        for value, cond in self.implies:
5706            res.add(value)
5707            res |= expr_items(cond)
5708
5709        for low, high, cond in self.ranges:
5710            res.add(low)
5711            res.add(high)
5712            res |= expr_items(cond)
5713
5714        return res
5715
5716    def __repr__(self):
5717        """
5718        Returns a string with information about the menu node when it is
5719        evaluated on e.g. the interactive Python prompt.
5720        """
5721        fields = []
5722        add = fields.append
5723
5724        if self.item.__class__ is Symbol:
5725            add("menu node for symbol " + self.item.name)
5726
5727        elif self.item.__class__ is Choice:
5728            s = "menu node for choice"
5729            if self.item.name is not None:
5730                s += " " + self.item.name
5731            add(s)
5732
5733        elif self.item is MENU:
5734            add("menu node for menu")
5735
5736        else:  # self.item is COMMENT
5737            add("menu node for comment")
5738
5739        if self.prompt:
5740            add('prompt "{}" (visibility {})'.format(
5741                self.prompt[0], TRI_TO_STR[expr_value(self.prompt[1])]))
5742
5743        if self.item.__class__ is Symbol and self.is_menuconfig:
5744            add("is menuconfig")
5745
5746        add("deps " + TRI_TO_STR[expr_value(self.dep)])
5747
5748        if self.item is MENU:
5749            add("'visible if' deps " + TRI_TO_STR[expr_value(self.visibility)])
5750
5751        if self.item.__class__ in _SYMBOL_CHOICE and self.help is not None:
5752            add("has help")
5753
5754        if self.list:
5755            add("has child")
5756
5757        if self.next:
5758            add("has next")
5759
5760        add("{}:{}".format(self.filename, self.linenr))
5761
5762        return "<{}>".format(", ".join(fields))
5763
5764    def __str__(self):
5765        """
5766        Returns a string representation of the menu node. Matches the Kconfig
5767        format, with any parent dependencies propagated to the 'depends on'
5768        condition.
5769
5770        The output could (almost) be fed back into a Kconfig parser to redefine
5771        the object associated with the menu node. See the module documentation
5772        for a gotcha related to choice symbols.
5773
5774        For symbols and choices with multiple menu nodes (multiple definition
5775        locations), properties that aren't associated with a particular menu
5776        node are shown on all menu nodes ('option env=...', 'optional' for
5777        choices, etc.).
5778
5779        The returned string does not end in a newline.
5780        """
5781        return self.custom_str(standard_sc_expr_str)
5782
5783    def custom_str(self, sc_expr_str_fn):
5784        """
5785        Works like MenuNode.__str__(), but allows a custom format to be used
5786        for all symbol/choice references. See expr_str().
5787        """
5788        return self._menu_comment_node_str(sc_expr_str_fn) \
5789               if self.item in _MENU_COMMENT else \
5790               self._sym_choice_node_str(sc_expr_str_fn)
5791
5792    def _menu_comment_node_str(self, sc_expr_str_fn):
5793        s = '{} "{}"'.format("menu" if self.item is MENU else "comment",
5794                             self.prompt[0])
5795
5796        if self.dep is not self.kconfig.y:
5797            s += "\n\tdepends on {}".format(expr_str(self.dep, sc_expr_str_fn))
5798
5799        if self.item is MENU and self.visibility is not self.kconfig.y:
5800            s += "\n\tvisible if {}".format(expr_str(self.visibility,
5801                                                     sc_expr_str_fn))
5802
5803        return s
5804
5805    def _sym_choice_node_str(self, sc_expr_str_fn):
5806        def indent_add(s):
5807            lines.append("\t" + s)
5808
5809        def indent_add_cond(s, cond):
5810            if cond is not self.kconfig.y:
5811                s += " if " + expr_str(cond, sc_expr_str_fn)
5812            indent_add(s)
5813
5814        sc = self.item
5815
5816        if sc.__class__ is Symbol:
5817            lines = [("menuconfig " if self.is_menuconfig else "config ")
5818                     + sc.name]
5819        else:
5820            lines = ["choice " + sc.name if sc.name else "choice"]
5821
5822        if sc.orig_type and not self.prompt:  # sc.orig_type != UNKNOWN
5823            # If there's a prompt, we'll use the '<type> "prompt"' shorthand
5824            # instead
5825            indent_add(TYPE_TO_STR[sc.orig_type])
5826
5827        if self.prompt:
5828            if sc.orig_type:
5829                prefix = TYPE_TO_STR[sc.orig_type]
5830            else:
5831                # Symbol defined without a type (which generates a warning)
5832                prefix = "prompt"
5833
5834            indent_add_cond(prefix + ' "{}"'.format(escape(self.prompt[0])),
5835                            self.orig_prompt[1])
5836
5837        if sc.__class__ is Symbol:
5838            if sc.is_allnoconfig_y:
5839                indent_add("option allnoconfig_y")
5840
5841            if sc is sc.kconfig.defconfig_list:
5842                indent_add("option defconfig_list")
5843
5844            if sc.env_var is not None:
5845                indent_add('option env="{}"'.format(sc.env_var))
5846
5847            if sc is sc.kconfig.modules:
5848                indent_add("option modules")
5849
5850            for low, high, cond in self.orig_ranges:
5851                indent_add_cond(
5852                    "range {} {}".format(sc_expr_str_fn(low),
5853                                         sc_expr_str_fn(high)),
5854                    cond)
5855
5856        for default, cond in self.orig_defaults:
5857            indent_add_cond("default " + expr_str(default, sc_expr_str_fn),
5858                            cond)
5859
5860        if sc.__class__ is Choice and sc.is_optional:
5861            indent_add("optional")
5862
5863        if sc.__class__ is Symbol:
5864            for select, cond in self.orig_selects:
5865                indent_add_cond("select " + sc_expr_str_fn(select), cond)
5866
5867            for imply, cond in self.orig_implies:
5868                indent_add_cond("imply " + sc_expr_str_fn(imply), cond)
5869
5870        if self.dep is not sc.kconfig.y:
5871            indent_add("depends on " + expr_str(self.dep, sc_expr_str_fn))
5872
5873        if self.help is not None:
5874            indent_add("help")
5875            for line in self.help.splitlines():
5876                indent_add("  " + line)
5877
5878        return "\n".join(lines)
5879
5880    def _strip_dep(self, expr):
5881        # Helper function for removing MenuNode.dep from 'expr'. Uses two
5882        # pieces of internal knowledge: (1) Expressions are reused rather than
5883        # copied, and (2) the direct dependencies always appear at the end.
5884
5885        # ... if dep -> ... if y
5886        if self.dep is expr:
5887            return self.kconfig.y
5888
5889        # (AND, X, dep) -> X
5890        if expr.__class__ is tuple and expr[0] is AND and expr[2] is self.dep:
5891            return expr[1]
5892
5893        return expr
5894
5895
5896class Variable(object):
5897    """
5898    Represents a preprocessor variable/function.
5899
5900    The following attributes are available:
5901
5902    name:
5903      The name of the variable.
5904
5905    value:
5906      The unexpanded value of the variable.
5907
5908    expanded_value:
5909      The expanded value of the variable. For simple variables (those defined
5910      with :=), this will equal 'value'. Accessing this property will raise a
5911      KconfigError if the expansion seems to be stuck in a loop.
5912
5913      Accessing this field is the same as calling expanded_value_w_args() with
5914      no arguments. I hadn't considered function arguments when adding it. It
5915      is retained for backwards compatibility though.
5916
5917    is_recursive:
5918      True if the variable is recursive (defined with =).
5919    """
5920    __slots__ = (
5921        "_n_expansions",
5922        "is_recursive",
5923        "kconfig",
5924        "name",
5925        "value",
5926    )
5927
5928    @property
5929    def expanded_value(self):
5930        """
5931        See the class documentation.
5932        """
5933        return self.expanded_value_w_args()
5934
5935    def expanded_value_w_args(self, *args):
5936        """
5937        Returns the expanded value of the variable/function. Any arguments
5938        passed will be substituted for $(1), $(2), etc.
5939
5940        Raises a KconfigError if the expansion seems to be stuck in a loop.
5941        """
5942        return self.kconfig._fn_val((self.name,) + args)
5943
5944    def __repr__(self):
5945        return "<variable {}, {}, value '{}'>" \
5946               .format(self.name,
5947                       "recursive" if self.is_recursive else "immediate",
5948                       self.value)
5949
5950
5951class KconfigError(Exception):
5952    """
5953    Exception raised for Kconfig-related errors.
5954
5955    KconfigError and KconfigSyntaxError are the same class. The
5956    KconfigSyntaxError alias is only maintained for backwards compatibility.
5957    """
5958
5959KconfigSyntaxError = KconfigError  # Backwards compatibility
5960
5961
5962class InternalError(Exception):
5963    "Never raised. Kept around for backwards compatibility."
5964
5965
5966# Workaround:
5967#
5968# If 'errno' and 'strerror' are set on IOError, then __str__() always returns
5969# "[Errno <errno>] <strerror>", ignoring any custom message passed to the
5970# constructor. By defining our own subclass, we can use a custom message while
5971# also providing 'errno', 'strerror', and 'filename' to scripts.
5972class _KconfigIOError(IOError):
5973    def __init__(self, ioerror, msg):
5974        self.msg = msg
5975        super(_KconfigIOError, self).__init__(
5976            ioerror.errno, ioerror.strerror, ioerror.filename)
5977
5978    def __str__(self):
5979        return self.msg
5980
5981
5982#
5983# Public functions
5984#
5985
5986
5987def expr_value(expr):
5988    """
5989    Evaluates the expression 'expr' to a tristate value. Returns 0 (n), 1 (m),
5990    or 2 (y).
5991
5992    'expr' must be an already-parsed expression from a Symbol, Choice, or
5993    MenuNode property. To evaluate an expression represented as a string, use
5994    Kconfig.eval_string().
5995
5996    Passing subexpressions of expressions to this function works as expected.
5997    """
5998    if expr.__class__ is not tuple:
5999        return expr.tri_value
6000
6001    if expr[0] is AND:
6002        v1 = expr_value(expr[1])
6003        # Short-circuit the n case as an optimization (~5% faster
6004        # allnoconfig.py and allyesconfig.py, as of writing)
6005        return 0 if not v1 else min(v1, expr_value(expr[2]))
6006
6007    if expr[0] is OR:
6008        v1 = expr_value(expr[1])
6009        # Short-circuit the y case as an optimization
6010        return 2 if v1 == 2 else max(v1, expr_value(expr[2]))
6011
6012    if expr[0] is NOT:
6013        return 2 - expr_value(expr[1])
6014
6015    # Relation
6016    #
6017    # Implements <, <=, >, >= comparisons as well. These were added to
6018    # kconfig in 31847b67 (kconfig: allow use of relations other than
6019    # (in)equality).
6020
6021    rel, v1, v2 = expr
6022
6023    # If both operands are strings...
6024    if v1.orig_type is STRING and v2.orig_type is STRING:
6025        # ...then compare them lexicographically
6026        comp = _strcmp(v1.str_value, v2.str_value)
6027    else:
6028        # Otherwise, try to compare them as numbers
6029        try:
6030            comp = _sym_to_num(v1) - _sym_to_num(v2)
6031        except ValueError:
6032            # Fall back on a lexicographic comparison if the operands don't
6033            # parse as numbers
6034            comp = _strcmp(v1.str_value, v2.str_value)
6035
6036    return 2*(comp == 0 if rel is EQUAL else
6037              comp != 0 if rel is UNEQUAL else
6038              comp <  0 if rel is LESS else
6039              comp <= 0 if rel is LESS_EQUAL else
6040              comp >  0 if rel is GREATER else
6041              comp >= 0)
6042
6043
6044def standard_sc_expr_str(sc):
6045    """
6046    Standard symbol/choice printing function. Uses plain Kconfig syntax, and
6047    displays choices as <choice> (or <choice NAME>, for named choices).
6048
6049    See expr_str().
6050    """
6051    if sc.__class__ is Symbol:
6052        if sc.is_constant and sc.name not in STR_TO_TRI:
6053            return '"{}"'.format(escape(sc.name))
6054        return sc.name
6055
6056    return "<choice {}>".format(sc.name) if sc.name else "<choice>"
6057
6058
6059def expr_str(expr, sc_expr_str_fn=standard_sc_expr_str):
6060    """
6061    Returns the string representation of the expression 'expr', as in a Kconfig
6062    file.
6063
6064    Passing subexpressions of expressions to this function works as expected.
6065
6066    sc_expr_str_fn (default: standard_sc_expr_str):
6067      This function is called for every symbol/choice (hence "sc") appearing in
6068      the expression, with the symbol/choice as the argument. It is expected to
6069      return a string to be used for the symbol/choice.
6070
6071      This can be used e.g. to turn symbols/choices into links when generating
6072      documentation, or for printing the value of each symbol/choice after it.
6073
6074      Note that quoted values are represented as constants symbols
6075      (Symbol.is_constant == True).
6076    """
6077    if expr.__class__ is not tuple:
6078        return sc_expr_str_fn(expr)
6079
6080    if expr[0] is AND:
6081        return "{} && {}".format(_parenthesize(expr[1], OR, sc_expr_str_fn),
6082                                 _parenthesize(expr[2], OR, sc_expr_str_fn))
6083
6084    if expr[0] is OR:
6085        # This turns A && B || C && D into "(A && B) || (C && D)", which is
6086        # redundant, but more readable
6087        return "{} || {}".format(_parenthesize(expr[1], AND, sc_expr_str_fn),
6088                                 _parenthesize(expr[2], AND, sc_expr_str_fn))
6089
6090    if expr[0] is NOT:
6091        if expr[1].__class__ is tuple:
6092            return "!({})".format(expr_str(expr[1], sc_expr_str_fn))
6093        return "!" + sc_expr_str_fn(expr[1])  # Symbol
6094
6095    # Relation
6096    #
6097    # Relation operands are always symbols (quoted strings are constant
6098    # symbols)
6099    return "{} {} {}".format(sc_expr_str_fn(expr[1]), REL_TO_STR[expr[0]],
6100                             sc_expr_str_fn(expr[2]))
6101
6102
6103def expr_items(expr):
6104    """
6105    Returns a set() of all items (symbols and choices) that appear in the
6106    expression 'expr'.
6107
6108    Passing subexpressions of expressions to this function works as expected.
6109    """
6110    res = set()
6111
6112    def rec(subexpr):
6113        if subexpr.__class__ is tuple:
6114            # AND, OR, NOT, or relation
6115
6116            rec(subexpr[1])
6117
6118            # NOTs only have a single operand
6119            if subexpr[0] is not NOT:
6120                rec(subexpr[2])
6121
6122        else:
6123            # Symbol or choice
6124            res.add(subexpr)
6125
6126    rec(expr)
6127    return res
6128
6129
6130def split_expr(expr, op):
6131    """
6132    Returns a list containing the top-level AND or OR operands in the
6133    expression 'expr', in the same (left-to-right) order as they appear in
6134    the expression.
6135
6136    This can be handy e.g. for splitting (weak) reverse dependencies
6137    from 'select' and 'imply' into individual selects/implies.
6138
6139    op:
6140      Either AND to get AND operands, or OR to get OR operands.
6141
6142      (Having this as an operand might be more future-safe than having two
6143      hardcoded functions.)
6144
6145
6146    Pseudo-code examples:
6147
6148      split_expr( A                    , OR  )  ->  [A]
6149      split_expr( A && B               , OR  )  ->  [A && B]
6150      split_expr( A || B               , OR  )  ->  [A, B]
6151      split_expr( A || B               , AND )  ->  [A || B]
6152      split_expr( A || B || (C && D)   , OR  )  ->  [A, B, C && D]
6153
6154      # Second || is not at the top level
6155      split_expr( A || (B && (C || D)) , OR )  ->  [A, B && (C || D)]
6156
6157      # Parentheses don't matter as long as we stay at the top level (don't
6158      # encounter any non-'op' nodes)
6159      split_expr( (A || B) || C        , OR )  ->  [A, B, C]
6160      split_expr( A || (B || C)        , OR )  ->  [A, B, C]
6161    """
6162    res = []
6163
6164    def rec(subexpr):
6165        if subexpr.__class__ is tuple and subexpr[0] is op:
6166            rec(subexpr[1])
6167            rec(subexpr[2])
6168        else:
6169            res.append(subexpr)
6170
6171    rec(expr)
6172    return res
6173
6174
6175def escape(s):
6176    r"""
6177    Escapes the string 's' in the same fashion as is done for display in
6178    Kconfig format and when writing strings to a .config file. " and \ are
6179    replaced by \" and \\, respectively.
6180    """
6181    # \ must be escaped before " to avoid double escaping
6182    return s.replace("\\", r"\\").replace('"', r'\"')
6183
6184
6185def unescape(s):
6186    r"""
6187    Unescapes the string 's'. \ followed by any character is replaced with just
6188    that character. Used internally when reading .config files.
6189    """
6190    return _unescape_sub(r"\1", s)
6191
6192# unescape() helper
6193_unescape_sub = re.compile(r"\\(.)").sub
6194
6195
6196def standard_kconfig(description=None):
6197    """
6198    Argument parsing helper for tools that take a single optional Kconfig file
6199    argument (default: Kconfig). Returns the Kconfig instance for the parsed
6200    configuration. Uses argparse internally.
6201
6202    Exits with sys.exit() (which raises SystemExit) on errors.
6203
6204    description (default: None):
6205      The 'description' passed to argparse.ArgumentParser(allow_abbrev=False).
6206      argparse.RawDescriptionHelpFormatter is used, so formatting is preserved.
6207    """
6208    import argparse
6209
6210    parser = argparse.ArgumentParser(
6211        formatter_class=argparse.RawDescriptionHelpFormatter,
6212        description=description, allow_abbrev=False)
6213
6214    parser.add_argument(
6215        "kconfig",
6216        metavar="KCONFIG",
6217        default="Kconfig",
6218        nargs="?",
6219        help="Top-level Kconfig file (default: Kconfig)")
6220
6221    return Kconfig(parser.parse_args().kconfig, suppress_traceback=True)
6222
6223
6224def standard_config_filename():
6225    """
6226    Helper for tools. Returns the value of KCONFIG_CONFIG (which specifies the
6227    .config file to load/save) if it is set, and ".config" otherwise.
6228
6229    Calling load_config() with filename=None might give the behavior you want,
6230    without having to use this function.
6231    """
6232    return os.getenv("KCONFIG_CONFIG", ".config")
6233
6234
6235def load_allconfig(kconf, filename):
6236    """
6237    Use Kconfig.load_allconfig() instead, which was added in Kconfiglib 13.4.0.
6238    Supported for backwards compatibility. Might be removed at some point after
6239    a long period of deprecation warnings.
6240    """
6241    allconfig = os.getenv("KCONFIG_ALLCONFIG")
6242    if allconfig is None:
6243        return
6244
6245    def std_msg(e):
6246        # "Upcasts" a _KconfigIOError to an IOError, removing the custom
6247        # __str__() message. The standard message is better here.
6248        #
6249        # This might also convert an OSError to an IOError in obscure cases,
6250        # but it's probably not a big deal. The distinction is shaky (see
6251        # PEP-3151).
6252        return IOError(e.errno, e.strerror, e.filename)
6253
6254    old_warn_assign_override = kconf.warn_assign_override
6255    old_warn_assign_redun = kconf.warn_assign_redun
6256    kconf.warn_assign_override = kconf.warn_assign_redun = False
6257
6258    if allconfig in ("", "1"):
6259        try:
6260            print(kconf.load_config(filename, False))
6261        except EnvironmentError as e1:
6262            try:
6263                print(kconf.load_config("all.config", False))
6264            except EnvironmentError as e2:
6265                sys.exit("error: KCONFIG_ALLCONFIG is set, but neither {} "
6266                         "nor all.config could be opened: {}, {}"
6267                         .format(filename, std_msg(e1), std_msg(e2)))
6268    else:
6269        try:
6270            print(kconf.load_config(allconfig, False))
6271        except EnvironmentError as e:
6272            sys.exit("error: KCONFIG_ALLCONFIG is set to '{}', which "
6273                     "could not be opened: {}"
6274                     .format(allconfig, std_msg(e)))
6275
6276    kconf.warn_assign_override = old_warn_assign_override
6277    kconf.warn_assign_redun = old_warn_assign_redun
6278
6279
6280#
6281# Internal functions
6282#
6283
6284
6285def _visibility(sc):
6286    # Symbols and Choices have a "visibility" that acts as an upper bound on
6287    # the values a user can set for them, corresponding to the visibility in
6288    # e.g. 'make menuconfig'. This function calculates the visibility for the
6289    # Symbol or Choice 'sc' -- the logic is nearly identical.
6290
6291    vis = 0
6292
6293    for node in sc.nodes:
6294        if node.prompt:
6295            vis = max(vis, expr_value(node.prompt[1]))
6296
6297    if sc.__class__ is Symbol and sc.choice:
6298        if sc.choice.orig_type is TRISTATE and \
6299           sc.orig_type is not TRISTATE and sc.choice.tri_value != 2:
6300            # Non-tristate choice symbols are only visible in y mode
6301            return 0
6302
6303        if sc.orig_type is TRISTATE and vis == 1 and sc.choice.tri_value == 2:
6304            # Choice symbols with m visibility are not visible in y mode
6305            return 0
6306
6307    # Promote m to y if we're dealing with a non-tristate (possibly due to
6308    # modules being disabled)
6309    if vis == 1 and sc.type is not TRISTATE:
6310        return 2
6311
6312    return vis
6313
6314
6315def _depend_on(sc, expr):
6316    # Adds 'sc' (symbol or choice) as a "dependee" to all symbols in 'expr'.
6317    # Constant symbols in 'expr' are skipped as they can never change value
6318    # anyway.
6319
6320    if expr.__class__ is tuple:
6321        # AND, OR, NOT, or relation
6322
6323        _depend_on(sc, expr[1])
6324
6325        # NOTs only have a single operand
6326        if expr[0] is not NOT:
6327            _depend_on(sc, expr[2])
6328
6329    elif not expr.is_constant:
6330        # Non-constant symbol, or choice
6331        expr._dependents.add(sc)
6332
6333
6334def _parenthesize(expr, type_, sc_expr_str_fn):
6335    # expr_str() helper. Adds parentheses around expressions of type 'type_'.
6336
6337    if expr.__class__ is tuple and expr[0] is type_:
6338        return "({})".format(expr_str(expr, sc_expr_str_fn))
6339    return expr_str(expr, sc_expr_str_fn)
6340
6341
6342def _ordered_unique(lst):
6343    # Returns 'lst' with any duplicates removed, preserving order. This hacky
6344    # version seems to be a common idiom. It relies on short-circuit evaluation
6345    # and set.add() returning None, which is falsy.
6346
6347    seen = set()
6348    seen_add = seen.add
6349    return [x for x in lst if x not in seen and not seen_add(x)]
6350
6351
6352def _is_base_n(s, n):
6353    try:
6354        int(s, n)
6355        return True
6356    except ValueError:
6357        return False
6358
6359
6360def _strcmp(s1, s2):
6361    # strcmp()-alike that returns -1, 0, or 1
6362
6363    return (s1 > s2) - (s1 < s2)
6364
6365
6366def _sym_to_num(sym):
6367    # expr_value() helper for converting a symbol to a number. Raises
6368    # ValueError for symbols that can't be converted.
6369
6370    # For BOOL and TRISTATE, n/m/y count as 0/1/2. This mirrors 9059a3493ef
6371    # ("kconfig: fix relational operators for bool and tristate symbols") in
6372    # the C implementation.
6373    return sym.tri_value if sym.orig_type in _BOOL_TRISTATE else \
6374           int(sym.str_value, _TYPE_TO_BASE[sym.orig_type])
6375
6376
6377def _touch_dep_file(path, sym_name):
6378    # If sym_name is MY_SYM_NAME, touches my/sym/name.h. See the sync_deps()
6379    # docstring.
6380
6381    sym_path = path + os.sep + sym_name.lower().replace("_", os.sep) + ".h"
6382    sym_path_dir = dirname(sym_path)
6383    if not exists(sym_path_dir):
6384        os.makedirs(sym_path_dir, 0o755)
6385
6386    # A kind of truncating touch, mirroring the C tools
6387    os.close(os.open(
6388        sym_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o644))
6389
6390
6391def _save_old(path):
6392    # See write_config()
6393
6394    if not os.path.isfile(path):
6395        # Backup only files (and symlinks to files). Simplest alternative
6396        # to avoid e.g. (potentially successful attempt to) rename /dev/null
6397        # (and to keep fifos).
6398        return
6399
6400    def copy(src, dst):
6401        # Import as needed, to save some startup time
6402        import shutil
6403        shutil.copyfile(src, dst)
6404
6405    if islink(path):
6406        # Preserve symlinks
6407        copy_fn = copy
6408    elif hasattr(os, "replace"):
6409        # Python 3 (3.3+) only. Best choice when available, because it
6410        # removes <filename>.old on both *nix and Windows.
6411        copy_fn = os.replace
6412    elif os.name == "posix":
6413        # Removes <filename>.old on POSIX systems
6414        copy_fn = os.rename
6415    else:
6416        # Fall back on copying
6417        copy_fn = copy
6418
6419    try:
6420        copy_fn(path, path + ".old")
6421    except Exception:
6422        # Ignore errors from 'path' missing as well as other errors.
6423        # <filename>.old file is usually more of a nice-to-have, and not worth
6424        # erroring out over e.g. if <filename>.old happens to be a directory.
6425        pass
6426
6427
6428def _locs(sc):
6429    # Symbol/Choice.name_and_loc helper. Returns the "(defined at ...)" part of
6430    # the string. 'sc' is a Symbol or Choice.
6431
6432    if sc.nodes:
6433        return "(defined at {})".format(
6434            ", ".join("{0.filename}:{0.linenr}".format(node)
6435                      for node in sc.nodes))
6436
6437    return "(undefined)"
6438
6439
6440# Menu manipulation
6441
6442
6443def _expr_depends_on(expr, sym):
6444    # Reimplementation of expr_depends_symbol() from mconf.c. Used to determine
6445    # if a submenu should be implicitly created. This also influences which
6446    # items inside choice statements are considered choice items.
6447
6448    if expr.__class__ is not tuple:
6449        return expr is sym
6450
6451    if expr[0] in _EQUAL_UNEQUAL:
6452        # Check for one of the following:
6453        # sym = m/y, m/y = sym, sym != n, n != sym
6454
6455        left, right = expr[1:]
6456
6457        if right is sym:
6458            left, right = right, left
6459        elif left is not sym:
6460            return False
6461
6462        return (expr[0] is EQUAL and right is sym.kconfig.m or
6463                                     right is sym.kconfig.y) or \
6464               (expr[0] is UNEQUAL and right is sym.kconfig.n)
6465
6466    return expr[0] is AND and \
6467           (_expr_depends_on(expr[1], sym) or
6468            _expr_depends_on(expr[2], sym))
6469
6470
6471def _auto_menu_dep(node1, node2):
6472    # Returns True if node2 has an "automatic menu dependency" on node1. If
6473    # node2 has a prompt, we check its condition. Otherwise, we look directly
6474    # at node2.dep.
6475
6476    return _expr_depends_on(node2.prompt[1] if node2.prompt else node2.dep,
6477                            node1.item)
6478
6479
6480def _flatten(node):
6481    # "Flattens" menu nodes without prompts (e.g. 'if' nodes and non-visible
6482    # symbols with children from automatic menu creation) so that their
6483    # children appear after them instead. This gives a clean menu structure
6484    # with no unexpected "jumps" in the indentation.
6485    #
6486    # Do not flatten promptless choices (which can appear "legitimately" if a
6487    # named choice is defined in multiple locations to add on symbols). It
6488    # looks confusing, and the menuconfig already shows all choice symbols if
6489    # you enter the choice at some location with a prompt.
6490
6491    while node:
6492        if node.list and not node.prompt and \
6493           node.item.__class__ is not Choice:
6494
6495            last_node = node.list
6496            while 1:
6497                last_node.parent = node.parent
6498                if not last_node.next:
6499                    break
6500                last_node = last_node.next
6501
6502            last_node.next = node.next
6503            node.next = node.list
6504            node.list = None
6505
6506        node = node.next
6507
6508
6509def _remove_ifs(node):
6510    # Removes 'if' nodes (which can be recognized by MenuNode.item being None),
6511    # which are assumed to already have been flattened. The C implementation
6512    # doesn't bother to do this, but we expose the menu tree directly, and it
6513    # makes it nicer to work with.
6514
6515    cur = node.list
6516    while cur and not cur.item:
6517        cur = cur.next
6518
6519    node.list = cur
6520
6521    while cur:
6522        next = cur.next
6523        while next and not next.item:
6524            next = next.next
6525
6526        # Equivalent to
6527        #
6528        #   cur.next = next
6529        #   cur = next
6530        #
6531        # due to tricky Python semantics. The order matters.
6532        cur.next = cur = next
6533
6534
6535def _finalize_choice(node):
6536    # Finalizes a choice, marking each symbol whose menu node has the choice as
6537    # the parent as a choice symbol, and automatically determining types if not
6538    # specified.
6539
6540    choice = node.item
6541
6542    cur = node.list
6543    while cur:
6544        if cur.item.__class__ is Symbol:
6545            cur.item.choice = choice
6546            choice.syms.append(cur.item)
6547        cur = cur.next
6548
6549    # If no type is specified for the choice, its type is that of
6550    # the first choice item with a specified type
6551    if not choice.orig_type:
6552        for item in choice.syms:
6553            if item.orig_type:
6554                choice.orig_type = item.orig_type
6555                break
6556
6557    # Each choice item of UNKNOWN type gets the type of the choice
6558    for sym in choice.syms:
6559        if not sym.orig_type:
6560            sym.orig_type = choice.orig_type
6561
6562
6563def _check_dep_loop_sym(sym, ignore_choice):
6564    # Detects dependency loops using depth-first search on the dependency graph
6565    # (which is calculated earlier in Kconfig._build_dep()).
6566    #
6567    # Algorithm:
6568    #
6569    #  1. Symbols/choices start out with _visited = 0, meaning unvisited.
6570    #
6571    #  2. When a symbol/choice is first visited, _visited is set to 1, meaning
6572    #     "visited, potentially part of a dependency loop". The recursive
6573    #     search then continues from the symbol/choice.
6574    #
6575    #  3. If we run into a symbol/choice X with _visited already set to 1,
6576    #     there's a dependency loop. The loop is found on the call stack by
6577    #     recording symbols while returning ("on the way back") until X is seen
6578    #     again.
6579    #
6580    #  4. Once a symbol/choice and all its dependencies (or dependents in this
6581    #     case) have been checked recursively without detecting any loops, its
6582    #     _visited is set to 2, meaning "visited, not part of a dependency
6583    #     loop".
6584    #
6585    #     This saves work if we run into the symbol/choice again in later calls
6586    #     to _check_dep_loop_sym(). We just return immediately.
6587    #
6588    # Choices complicate things, as every choice symbol depends on every other
6589    # choice symbol in a sense. When a choice is "entered" via a choice symbol
6590    # X, we visit all choice symbols from the choice except X, and prevent
6591    # immediately revisiting the choice with a flag (ignore_choice).
6592    #
6593    # Maybe there's a better way to handle this (different flags or the
6594    # like...)
6595
6596    if not sym._visited:
6597        # sym._visited == 0, unvisited
6598
6599        sym._visited = 1
6600
6601        for dep in sym._dependents:
6602            # Choices show up in Symbol._dependents when the choice has the
6603            # symbol in a 'prompt' or 'default' condition (e.g.
6604            # 'default ... if SYM').
6605            #
6606            # Since we aren't entering the choice via a choice symbol, all
6607            # choice symbols need to be checked, hence the None.
6608            loop = _check_dep_loop_choice(dep, None) \
6609                   if dep.__class__ is Choice \
6610                   else _check_dep_loop_sym(dep, False)
6611
6612            if loop:
6613                # Dependency loop found
6614                return _found_dep_loop(loop, sym)
6615
6616        if sym.choice and not ignore_choice:
6617            loop = _check_dep_loop_choice(sym.choice, sym)
6618            if loop:
6619                # Dependency loop found
6620                return _found_dep_loop(loop, sym)
6621
6622        # The symbol is not part of a dependency loop
6623        sym._visited = 2
6624
6625        # No dependency loop found
6626        return None
6627
6628    if sym._visited == 2:
6629        # The symbol was checked earlier and is already known to not be part of
6630        # a dependency loop
6631        return None
6632
6633    # sym._visited == 1, found a dependency loop. Return the symbol as the
6634    # first element in it.
6635    return (sym,)
6636
6637
6638def _check_dep_loop_choice(choice, skip):
6639    if not choice._visited:
6640        # choice._visited == 0, unvisited
6641
6642        choice._visited = 1
6643
6644        # Check for loops involving choice symbols. If we came here via a
6645        # choice symbol, skip that one, as we'd get a false positive
6646        # '<sym FOO> -> <choice> -> <sym FOO>' loop otherwise.
6647        for sym in choice.syms:
6648            if sym is not skip:
6649                # Prevent the choice from being immediately re-entered via the
6650                # "is a choice symbol" path by passing True
6651                loop = _check_dep_loop_sym(sym, True)
6652                if loop:
6653                    # Dependency loop found
6654                    return _found_dep_loop(loop, choice)
6655
6656        # The choice is not part of a dependency loop
6657        choice._visited = 2
6658
6659        # No dependency loop found
6660        return None
6661
6662    if choice._visited == 2:
6663        # The choice was checked earlier and is already known to not be part of
6664        # a dependency loop
6665        return None
6666
6667    # choice._visited == 1, found a dependency loop. Return the choice as the
6668    # first element in it.
6669    return (choice,)
6670
6671
6672def _found_dep_loop(loop, cur):
6673    # Called "on the way back" when we know we have a loop
6674
6675    # Is the symbol/choice 'cur' where the loop started?
6676    if cur is not loop[0]:
6677        # Nope, it's just a part of the loop
6678        return loop + (cur,)
6679
6680    # Yep, we have the entire loop. Throw an exception that shows it.
6681
6682    msg = "\nDependency loop\n" \
6683            "===============\n\n"
6684
6685    for item in loop:
6686        if item is not loop[0]:
6687            msg += "...depends on "
6688            if item.__class__ is Symbol and item.choice:
6689                msg += "the choice symbol "
6690
6691        msg += "{}, with definition...\n\n{}\n\n" \
6692               .format(item.name_and_loc, item)
6693
6694        # Small wart: Since we reuse the already calculated
6695        # Symbol/Choice._dependents sets for recursive dependency detection, we
6696        # lose information on whether a dependency came from a 'select'/'imply'
6697        # condition or e.g. a 'depends on'.
6698        #
6699        # This might cause selecting symbols to "disappear". For example,
6700        # a symbol B having 'select A if C' gives a direct dependency from A to
6701        # C, since it corresponds to a reverse dependency of B && C.
6702        #
6703        # Always print reverse dependencies for symbols that have them to make
6704        # sure information isn't lost. I wonder if there's some neat way to
6705        # improve this.
6706
6707        if item.__class__ is Symbol:
6708            if item.rev_dep is not item.kconfig.n:
6709                msg += "(select-related dependencies: {})\n\n" \
6710                       .format(expr_str(item.rev_dep))
6711
6712            if item.weak_rev_dep is not item.kconfig.n:
6713                msg += "(imply-related dependencies: {})\n\n" \
6714                       .format(expr_str(item.rev_dep))
6715
6716    msg += "...depends again on " + loop[0].name_and_loc
6717
6718    raise KconfigError(msg)
6719
6720
6721def _decoding_error(e, filename, macro_linenr=None):
6722    # Gives the filename and context for UnicodeDecodeError's, which are a pain
6723    # to debug otherwise. 'e' is the UnicodeDecodeError object.
6724    #
6725    # If the decoding error is for the output of a $(shell,...) command,
6726    # macro_linenr holds the line number where it was run (the exact line
6727    # number isn't available for decoding errors in files).
6728
6729    raise KconfigError(
6730        "\n"
6731        "Malformed {} in {}\n"
6732        "Context: {}\n"
6733        "Problematic data: {}\n"
6734        "Reason: {}".format(
6735            e.encoding,
6736            "'{}'".format(filename) if macro_linenr is None else
6737                "output from macro at {}:{}".format(filename, macro_linenr),
6738            e.object[max(e.start - 40, 0):e.end + 40],
6739            e.object[e.start:e.end],
6740            e.reason))
6741
6742
6743def _warn_verbose_deprecated(fn_name):
6744    sys.stderr.write(
6745        "Deprecation warning: {0}()'s 'verbose' argument has no effect. Since "
6746        "Kconfiglib 12.0.0, the message is returned from {0}() instead, "
6747        "and is always generated. Do e.g. print(kconf.{0}()) if you want to "
6748        "want to show a message like \"Loaded configuration '.config'\" on "
6749        "stdout. The old API required ugly hacks to reuse messages in "
6750        "configuration interfaces.\n".format(fn_name))
6751
6752
6753# Predefined preprocessor functions
6754
6755
6756def _filename_fn(kconf, _):
6757    return kconf.filename
6758
6759
6760def _lineno_fn(kconf, _):
6761    return str(kconf.linenr)
6762
6763
6764def _info_fn(kconf, _, msg):
6765    print("{}:{}: {}".format(kconf.filename, kconf.linenr, msg))
6766
6767    return ""
6768
6769
6770def _warning_if_fn(kconf, _, cond, msg):
6771    if cond == "y":
6772        kconf._warn(msg, kconf.filename, kconf.linenr)
6773
6774    return ""
6775
6776
6777def _error_if_fn(kconf, _, cond, msg):
6778    if cond == "y":
6779        raise KconfigError("{}:{}: {}".format(
6780            kconf.filename, kconf.linenr, msg))
6781
6782    return ""
6783
6784
6785def _shell_fn(kconf, _, command):
6786    import subprocess  # Only import as needed, to save some startup time
6787
6788    stdout, stderr = subprocess.Popen(
6789        command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
6790    ).communicate()
6791
6792    if not _IS_PY2:
6793        try:
6794            stdout = stdout.decode(kconf._encoding)
6795            stderr = stderr.decode(kconf._encoding)
6796        except UnicodeDecodeError as e:
6797            _decoding_error(e, kconf.filename, kconf.linenr)
6798
6799    if stderr:
6800        kconf._warn("'{}' wrote to stderr: {}".format(
6801                        command, "\n".join(stderr.splitlines())),
6802                    kconf.filename, kconf.linenr)
6803
6804    # Universal newlines with splitlines() (to prevent e.g. stray \r's in
6805    # command output on Windows), trailing newline removal, and
6806    # newline-to-space conversion.
6807    #
6808    # On Python 3 versions before 3.6, it's not possible to specify the
6809    # encoding when passing universal_newlines=True to Popen() (the 'encoding'
6810    # parameter was added in 3.6), so we do this manual version instead.
6811    return "\n".join(stdout.splitlines()).rstrip("\n").replace("\n", " ")
6812
6813#
6814# Global constants
6815#
6816
6817TRI_TO_STR = {
6818    0: "n",
6819    1: "m",
6820    2: "y",
6821}
6822
6823STR_TO_TRI = {
6824    "n": 0,
6825    "m": 1,
6826    "y": 2,
6827}
6828
6829# Constant representing that there's no cached choice selection. This is
6830# distinct from a cached None (no selection). Any object that's not None or a
6831# Symbol will do. We test this with 'is'.
6832_NO_CACHED_SELECTION = 0
6833
6834# Are we running on Python 2?
6835_IS_PY2 = sys.version_info[0] < 3
6836
6837try:
6838    _UNAME_RELEASE = os.uname()[2]
6839except AttributeError:
6840    # Only import as needed, to save some startup time
6841    import platform
6842    _UNAME_RELEASE = platform.uname()[2]
6843
6844# The token and type constants below are safe to test with 'is', which is a bit
6845# faster (~30% faster on my machine, and a few % faster for total parsing
6846# time), even without assuming Python's small integer optimization (which
6847# caches small integer objects). The constants end up pointing to unique
6848# integer objects, and since we consistently refer to them via the names below,
6849# we always get the same object.
6850#
6851# Client code should use == though.
6852
6853# Tokens, with values 1, 2, ... . Avoiding 0 simplifies some checks by making
6854# all tokens except empty strings truthy.
6855(
6856    _T_ALLNOCONFIG_Y,
6857    _T_AND,
6858    _T_BOOL,
6859    _T_CHOICE,
6860    _T_CLOSE_PAREN,
6861    _T_COMMENT,
6862    _T_CONFIG,
6863    _T_DEFAULT,
6864    _T_DEFCONFIG_LIST,
6865    _T_DEF_BOOL,
6866    _T_DEF_HEX,
6867    _T_DEF_INT,
6868    _T_DEF_STRING,
6869    _T_DEF_TRISTATE,
6870    _T_DEPENDS,
6871    _T_ENDCHOICE,
6872    _T_ENDIF,
6873    _T_ENDMENU,
6874    _T_ENV,
6875    _T_EQUAL,
6876    _T_GREATER,
6877    _T_GREATER_EQUAL,
6878    _T_HELP,
6879    _T_HEX,
6880    _T_IF,
6881    _T_IMPLY,
6882    _T_INT,
6883    _T_LESS,
6884    _T_LESS_EQUAL,
6885    _T_MAINMENU,
6886    _T_MENU,
6887    _T_MENUCONFIG,
6888    _T_MODULES,
6889    _T_NOT,
6890    _T_ON,
6891    _T_OPEN_PAREN,
6892    _T_OPTION,
6893    _T_OPTIONAL,
6894    _T_OR,
6895    _T_ORSOURCE,
6896    _T_OSOURCE,
6897    _T_PROMPT,
6898    _T_RANGE,
6899    _T_RSOURCE,
6900    _T_SELECT,
6901    _T_SOURCE,
6902    _T_STRING,
6903    _T_TRISTATE,
6904    _T_UNEQUAL,
6905    _T_VISIBLE,
6906) = range(1, 51)
6907
6908# Keyword to token map, with the get() method assigned directly as a small
6909# optimization
6910_get_keyword = {
6911    "---help---":     _T_HELP,
6912    "allnoconfig_y":  _T_ALLNOCONFIG_Y,
6913    "bool":           _T_BOOL,
6914    "boolean":        _T_BOOL,
6915    "choice":         _T_CHOICE,
6916    "comment":        _T_COMMENT,
6917    "config":         _T_CONFIG,
6918    "def_bool":       _T_DEF_BOOL,
6919    "def_hex":        _T_DEF_HEX,
6920    "def_int":        _T_DEF_INT,
6921    "def_string":     _T_DEF_STRING,
6922    "def_tristate":   _T_DEF_TRISTATE,
6923    "default":        _T_DEFAULT,
6924    "defconfig_list": _T_DEFCONFIG_LIST,
6925    "depends":        _T_DEPENDS,
6926    "endchoice":      _T_ENDCHOICE,
6927    "endif":          _T_ENDIF,
6928    "endmenu":        _T_ENDMENU,
6929    "env":            _T_ENV,
6930    "grsource":       _T_ORSOURCE,  # Backwards compatibility
6931    "gsource":        _T_OSOURCE,   # Backwards compatibility
6932    "help":           _T_HELP,
6933    "hex":            _T_HEX,
6934    "if":             _T_IF,
6935    "imply":          _T_IMPLY,
6936    "int":            _T_INT,
6937    "mainmenu":       _T_MAINMENU,
6938    "menu":           _T_MENU,
6939    "menuconfig":     _T_MENUCONFIG,
6940    "modules":        _T_MODULES,
6941    "on":             _T_ON,
6942    "option":         _T_OPTION,
6943    "optional":       _T_OPTIONAL,
6944    "orsource":       _T_ORSOURCE,
6945    "osource":        _T_OSOURCE,
6946    "prompt":         _T_PROMPT,
6947    "range":          _T_RANGE,
6948    "rsource":        _T_RSOURCE,
6949    "select":         _T_SELECT,
6950    "source":         _T_SOURCE,
6951    "string":         _T_STRING,
6952    "tristate":       _T_TRISTATE,
6953    "visible":        _T_VISIBLE,
6954}.get
6955
6956# The constants below match the value of the corresponding tokens to remove the
6957# need for conversion
6958
6959# Node types
6960MENU    = _T_MENU
6961COMMENT = _T_COMMENT
6962
6963# Expression types
6964AND           = _T_AND
6965OR            = _T_OR
6966NOT           = _T_NOT
6967EQUAL         = _T_EQUAL
6968UNEQUAL       = _T_UNEQUAL
6969LESS          = _T_LESS
6970LESS_EQUAL    = _T_LESS_EQUAL
6971GREATER       = _T_GREATER
6972GREATER_EQUAL = _T_GREATER_EQUAL
6973
6974REL_TO_STR = {
6975    EQUAL:         "=",
6976    UNEQUAL:       "!=",
6977    LESS:          "<",
6978    LESS_EQUAL:    "<=",
6979    GREATER:       ">",
6980    GREATER_EQUAL: ">=",
6981}
6982
6983# Symbol/choice types. UNKNOWN is 0 (falsy) to simplify some checks.
6984# Client code shouldn't rely on it though, as it was non-zero in
6985# older versions.
6986UNKNOWN  = 0
6987BOOL     = _T_BOOL
6988TRISTATE = _T_TRISTATE
6989STRING   = _T_STRING
6990INT      = _T_INT
6991HEX      = _T_HEX
6992
6993TYPE_TO_STR = {
6994    UNKNOWN:  "unknown",
6995    BOOL:     "bool",
6996    TRISTATE: "tristate",
6997    STRING:   "string",
6998    INT:      "int",
6999    HEX:      "hex",
7000}
7001
7002# Used in comparisons. 0 means the base is inferred from the format of the
7003# string.
7004_TYPE_TO_BASE = {
7005    HEX:      16,
7006    INT:      10,
7007    STRING:   0,
7008    UNKNOWN:  0,
7009}
7010
7011# def_bool -> BOOL, etc.
7012_DEF_TOKEN_TO_TYPE = {
7013    _T_DEF_BOOL:     BOOL,
7014    _T_DEF_HEX:      HEX,
7015    _T_DEF_INT:      INT,
7016    _T_DEF_STRING:   STRING,
7017    _T_DEF_TRISTATE: TRISTATE,
7018}
7019
7020# Tokens after which strings are expected. This is used to tell strings from
7021# constant symbol references during tokenization, both of which are enclosed in
7022# quotes.
7023#
7024# Identifier-like lexemes ("missing quotes") are also treated as strings after
7025# these tokens. _T_CHOICE is included to avoid symbols being registered for
7026# named choices.
7027_STRING_LEX = frozenset({
7028    _T_BOOL,
7029    _T_CHOICE,
7030    _T_COMMENT,
7031    _T_HEX,
7032    _T_INT,
7033    _T_MAINMENU,
7034    _T_MENU,
7035    _T_ORSOURCE,
7036    _T_OSOURCE,
7037    _T_PROMPT,
7038    _T_RSOURCE,
7039    _T_SOURCE,
7040    _T_STRING,
7041    _T_TRISTATE,
7042})
7043
7044# Various sets for quick membership tests. Gives a single global lookup and
7045# avoids creating temporary dicts/tuples.
7046
7047_TYPE_TOKENS = frozenset({
7048    _T_BOOL,
7049    _T_TRISTATE,
7050    _T_INT,
7051    _T_HEX,
7052    _T_STRING,
7053})
7054
7055_SOURCE_TOKENS = frozenset({
7056    _T_SOURCE,
7057    _T_RSOURCE,
7058    _T_OSOURCE,
7059    _T_ORSOURCE,
7060})
7061
7062_REL_SOURCE_TOKENS = frozenset({
7063    _T_RSOURCE,
7064    _T_ORSOURCE,
7065})
7066
7067# Obligatory (non-optional) sources
7068_OBL_SOURCE_TOKENS = frozenset({
7069    _T_SOURCE,
7070    _T_RSOURCE,
7071})
7072
7073_BOOL_TRISTATE = frozenset({
7074    BOOL,
7075    TRISTATE,
7076})
7077
7078_BOOL_TRISTATE_UNKNOWN = frozenset({
7079    BOOL,
7080    TRISTATE,
7081    UNKNOWN,
7082})
7083
7084_INT_HEX = frozenset({
7085    INT,
7086    HEX,
7087})
7088
7089_SYMBOL_CHOICE = frozenset({
7090    Symbol,
7091    Choice,
7092})
7093
7094_MENU_COMMENT = frozenset({
7095    MENU,
7096    COMMENT,
7097})
7098
7099_EQUAL_UNEQUAL = frozenset({
7100    EQUAL,
7101    UNEQUAL,
7102})
7103
7104_RELATIONS = frozenset({
7105    EQUAL,
7106    UNEQUAL,
7107    LESS,
7108    LESS_EQUAL,
7109    GREATER,
7110    GREATER_EQUAL,
7111})
7112
7113# Helper functions for getting compiled regular expressions, with the needed
7114# matching function returned directly as a small optimization.
7115#
7116# Use ASCII regex matching on Python 3. It's already the default on Python 2.
7117
7118
7119def _re_match(regex):
7120    return re.compile(regex, 0 if _IS_PY2 else re.ASCII).match
7121
7122
7123def _re_search(regex):
7124    return re.compile(regex, 0 if _IS_PY2 else re.ASCII).search
7125
7126
7127# Various regular expressions used during parsing
7128
7129# The initial token on a line. Also eats leading and trailing whitespace, so
7130# that we can jump straight to the next token (or to the end of the line if
7131# there is only one token).
7132#
7133# This regex will also fail to match for empty lines and comment lines.
7134#
7135# '$' is included to detect preprocessor variable assignments with macro
7136# expansions in the left-hand side.
7137_command_match = _re_match(r"\s*([A-Za-z0-9_$-]+)\s*")
7138
7139# An identifier/keyword after the first token. Also eats trailing whitespace.
7140# '$' is included to detect identifiers containing macro expansions.
7141_id_keyword_match = _re_match(r"([A-Za-z0-9_$/.-]+)\s*")
7142
7143# A fragment in the left-hand side of a preprocessor variable assignment. These
7144# are the portions between macro expansions ($(foo)). Macros are supported in
7145# the LHS (variable name).
7146_assignment_lhs_fragment_match = _re_match("[A-Za-z0-9_-]*")
7147
7148# The assignment operator and value (right-hand side) in a preprocessor
7149# variable assignment
7150_assignment_rhs_match = _re_match(r"\s*(=|:=|\+=)\s*(.*)")
7151
7152# Special characters/strings while expanding a macro ('(', ')', ',', and '$(')
7153_macro_special_search = _re_search(r"\(|\)|,|\$\(")
7154
7155# Special characters/strings while expanding a string (quotes, '\', and '$(')
7156_string_special_search = _re_search(r'"|\'|\\|\$\(')
7157
7158# Special characters/strings while expanding a symbol name. Also includes
7159# end-of-line, in case the macro is the last thing on the line.
7160_name_special_search = _re_search(r'[^A-Za-z0-9_$/.-]|\$\(|$')
7161
7162# A valid right-hand side for an assignment to a string symbol in a .config
7163# file, including escaped characters. Extracts the contents.
7164_conf_string_match = _re_match(r'"((?:[^\\"]|\\.)*)"')
7165