1 # SPDX-License-Identifier: ISC
3 # Author: Ulf Magnusson
4 # https://github.com/ulfalizer/Kconfiglib
6 # This is Kconfiglib, a Python library for scripting, debugging, and extracting
7 # information from Kconfig-based configuration systems. To view the
12 # or, if you prefer HTML,
14 # $ pydoc -w kconfiglib
16 # The examples/ subdirectory contains examples, to be run with e.g.
18 # $ make scriptconfig SCRIPT=Kconfiglib/examples/print_tree.py
20 # Look in testsuite.py for the test suite.
23 Kconfiglib is a Python library for scripting and extracting information from
24 Kconfig-based configuration systems. Features include the following:
26 - Symbol values and properties can be looked up and values assigned
28 - .config files can be read and written.
29 - Expressions can be evaluated in the context of a Kconfig configuration.
30 - Relations between symbols can be quickly determined, such as finding all
31 symbols that reference a particular symbol.
32 - Highly compatible with the scripts/kconfig/*conf utilities. The test suite
33 automatically compares outputs between Kconfiglib and the C implementation
34 for a large number of cases.
36 For the Linux kernel, scripts are run using
38 $ make scriptconfig [ARCH=<arch>] SCRIPT=<path to script> [SCRIPT_ARG=<arg>]
40 Using the 'scriptconfig' target ensures that required environment variables
41 (SRCARCH, ARCH, srctree, KERNELVERSION, etc.) are set up correctly.
43 Scripts receive the name of the Kconfig file to load in sys.argv[1]. As of
44 Linux 4.1.0-rc5, this is always "Kconfig" from the kernel top-level directory.
45 If an argument is provided with SCRIPT_ARG, it appears as sys.argv[2].
47 To get an interactive Python prompt with Kconfiglib preloaded and a Config
48 object 'c' created, run
50 $ make iscriptconfig [ARCH=<arch>]
52 Kconfiglib supports both Python 2 and Python 3. For (i)scriptconfig, the Python
53 interpreter to use can be passed in PYTHONCMD, which defaults to 'python'. PyPy
54 works well too, and might give a nice speedup for long-running jobs.
56 The examples/ directory contains short example scripts, which can be run with
59 $ make scriptconfig SCRIPT=Kconfiglib/examples/print_tree.py
63 $ make scriptconfig SCRIPT=Kconfiglib/examples/help_grep.py SCRIPT_ARG=kernel
65 testsuite.py contains the test suite. See the top of the script for how to run
68 Credits: Written by Ulf "Ulfalizer" Magnusson
70 Send bug reports, suggestions and other feedback to ulfalizer a.t Google's
71 email service. Don't wrestle with internal APIs. Tell me what you need and I
72 might add it in a safe way as a client API instead."""
85 # Internal global constants
87 # Line length: 79 columns
95 """Represents a Kconfig configuration, e.g. for i386 or ARM. This is the
96 set of symbols and other items appearing in the configuration together with
97 their values. Creating any number of Config objects -- including for
98 different architectures -- is safe; Kconfiglib has no global state."""
104 def __init__(self, filename="Kconfig", base_dir=None, print_warnings=True,
105 print_undef_assign=False):
106 """Creates a new Config object, representing a Kconfig configuration.
107 Raises Kconfig_Syntax_Error on syntax errors.
109 filename (default: "Kconfig"): The base Kconfig file of the
110 configuration. For the Linux kernel, you'll probably want "Kconfig"
111 from the top-level directory, as environment variables will make
112 sure the right Kconfig is included from there
113 (arch/<architecture>/Kconfig). If you are using Kconfiglib via 'make
114 scriptconfig', the filename of the base base Kconfig file will be in
117 base_dir (default: None): The base directory relative to which 'source'
118 statements within Kconfig files will work. For the Linux kernel this
119 should be the top-level directory of the kernel tree. $-references
120 to existing environment variables will be expanded.
122 If None (the default), the environment variable 'srctree' will be
123 used if set, and the current directory otherwise. 'srctree' is set
124 by the Linux makefiles to the top-level kernel directory. A default
125 of "." would not work with an alternative build directory.
127 print_warnings (default: True): Set to True if warnings related to this
128 configuration should be printed to stderr. This can be changed later
129 with Config.set_print_warnings(). It is provided as a constructor
130 argument since warnings might be generated during parsing.
132 print_undef_assign (default: False): Set to True if informational
133 messages related to assignments to undefined symbols should be
134 printed to stderr for this configuration. Can be changed later with
135 Config.set_print_undef_assign()."""
137 # The set of all symbols, indexed by name (a string)
139 # Python 2/3 compatibility hack. This is the only one needed.
140 self.syms_iter = self.syms.values if sys.version_info[0] >= 3 else \
143 # The set of all defined symbols in the configuration in the order they
144 # appear in the Kconfig files. This excludes the special symbols n, m,
145 # and y as well as symbols that are referenced but never defined.
146 self.kconfig_syms = []
148 # The set of all named choices (yes, choices can have names), indexed
150 self.named_choices = {}
152 # Lists containing all choices, menus and comments in the configuration
157 def register_special_symbol(type_, name, val):
159 sym.is_special_ = True
160 sym.is_defined_ = True
165 self.syms[name] = sym
168 # The special symbols n, m and y, used as shorthand for "n", "m" and
170 self.n = register_special_symbol(TRISTATE, "n", "n")
171 self.m = register_special_symbol(TRISTATE, "m", "m")
172 self.y = register_special_symbol(TRISTATE, "y", "y")
173 # DEFCONFIG_LIST uses this
174 register_special_symbol(STRING, "UNAME_RELEASE", platform.uname()[2])
176 # The symbol with "option defconfig_list" set, containing a list of
177 # default .config files
178 self.defconfig_sym = None
180 # See Symbol.get_(src)arch()
181 self.arch = os.environ.get("ARCH")
182 self.srcarch = os.environ.get("SRCARCH")
184 # If you set CONFIG_ in the environment, Kconfig will prefix all symbols
185 # with its value when saving the configuration, instead of using the default, "CONFIG_".
186 self.config_prefix = os.environ.get("CONFIG_")
187 if self.config_prefix is None:
188 self.config_prefix = "CONFIG_"
190 # See Config.__init__(). We need this for get_defconfig_filename().
191 self.srctree = os.environ.get("srctree")
192 if self.srctree is None:
195 self.filename = filename
196 self.base_dir = self.srctree if base_dir is None else \
197 os.path.expandvars(base_dir)
199 # The 'mainmenu' text
200 self.mainmenu_text = None
202 # The filename of the most recently loaded .config file
203 self.config_filename = None
204 # The textual header of the most recently loaded .config, uncommented
205 self.config_header = None
207 self.print_warnings = print_warnings
208 self.print_undef_assign = print_undef_assign
211 # For parsing routines that stop when finding a line belonging to a
212 # different construct, these holds that line and the tokenized version
213 # of that line. The purpose is to avoid having to re-tokenize the line,
214 # which is inefficient and causes problems when recording references to
217 self.end_line_tokens = None
219 # See the comment in _parse_expr().
220 self._cur_item = None
222 self._filename = None
224 self._transform_m = None
226 # Parse the Kconfig files
228 self._parse_file(filename, None, None, None, self.top_block)
230 # Build Symbol.dep for all symbols
234 """Returns the value the environment variable ARCH had at the time the
235 Config instance was created, or None if ARCH was not set. For the
236 kernel, this corresponds to the architecture being built for, with
237 values such as "i386" or "mips"."""
240 def get_srcarch(self):
241 """Returns the value the environment variable SRCARCH had at the time
242 the Config instance was created, or None if SRCARCH was not set. For
243 the kernel, this corresponds to the particular arch/ subdirectory
244 containing architecture-specific code."""
247 def get_srctree(self):
248 """Returns the value the environment variable srctree had at the time
249 the Config instance was created, or None if srctree was not defined.
250 This variable points to the source directory and is used when building
251 in a separate directory."""
254 def get_base_dir(self):
255 """Returns the base directory relative to which 'source' statements
256 will work, passed as an argument to Config.__init__()."""
259 def get_kconfig_filename(self):
260 """Returns the name of the (base) kconfig file this configuration was
264 def get_config_filename(self):
265 """Returns the filename of the most recently loaded configuration file,
266 or None if no configuration has been loaded."""
267 return self.config_filename
269 def get_config_header(self):
270 """Returns the (uncommented) textual header of the .config file most
271 recently loaded with load_config(). Returns None if no .config file has
272 been loaded or if the most recently loaded .config file has no header.
273 The header consists of all lines up to but not including the first line
276 1. Does not start with "#"
277 2. Has the form "# CONFIG_FOO is not set."
279 return self.config_header
281 def get_mainmenu_text(self):
282 """Returns the text of the 'mainmenu' statement (with $-references to
283 symbols replaced by symbol values), or None if the configuration has no
284 'mainmenu' statement."""
285 return None if self.mainmenu_text is None else \
286 self._expand_sym_refs(self.mainmenu_text)
288 def get_defconfig_filename(self):
289 """Returns the name of the defconfig file, which is the first existing
290 file in the list given in a symbol having 'option defconfig_list' set.
291 $-references to symbols will be expanded ("$FOO bar" -> "foo bar" if
292 FOO has the value "foo"). Returns None in case of no defconfig file.
293 Setting 'option defconfig_list' on multiple symbols currently results
294 in undefined behavior.
296 If the environment variable 'srctree' was set when the Config was
297 created, get_defconfig_filename() will first look relative to that
298 directory before looking in the current directory; see
301 WARNING: A wart here is that scripts/kconfig/Makefile sometimes uses
302 the --defconfig=<defconfig> option when calling the C implementation of
303 e.g. 'make defconfig'. This option overrides the 'option
304 defconfig_list' symbol, meaning the result from
305 get_defconfig_filename() might not match what 'make defconfig' would
306 use. That probably ought to be worked around somehow, so that this
307 function always gives the "expected" result."""
308 if self.defconfig_sym is None:
310 for filename, cond_expr in self.defconfig_sym.def_exprs:
311 if self._eval_expr(cond_expr) == "y":
312 filename = self._expand_sym_refs(filename)
313 # We first look in $srctree. os.path.join() won't work here as
314 # an absolute path in filename would override $srctree.
315 srctree_filename = os.path.normpath(self.srctree + "/" +
317 if os.path.exists(srctree_filename):
318 return srctree_filename
319 if os.path.exists(filename):
323 def get_symbol(self, name):
324 """Returns the symbol with name 'name', or None if no such symbol
325 appears in the configuration. An alternative shorthand is conf[name],
326 where conf is a Config instance, though that will instead raise
327 KeyError if the symbol does not exist."""
328 return self.syms.get(name)
330 def __getitem__(self, name):
331 """Returns the symbol with name 'name'. Raises KeyError if the symbol
332 does not appear in the configuration."""
333 return self.syms[name]
335 def get_symbols(self, all_symbols=True):
336 """Returns a list of symbols from the configuration. An alternative for
337 iterating over all defined symbols (in the order of definition) is
342 which relies on Config implementing __iter__() and is equivalent to
344 for sym in config.get_symbols(False):
347 all_symbols (default: True): If True, all symbols -- including special
348 and undefined symbols -- will be included in the result, in an
349 undefined order. If False, only symbols actually defined and not
350 merely referred to in the configuration will be included in the
351 result, and will appear in the order that they are defined within
352 the Kconfig configuration files."""
353 return list(self.syms.values()) if all_symbols else self.kconfig_syms
356 """Convenience function for iterating over the set of all defined
357 symbols in the configuration, used like
362 The iteration happens in the order of definition within the Kconfig
363 configuration files. Symbols only referred to but not defined will not
364 be included, nor will the special symbols n, m, and y. If you want to
365 include such symbols as well, see config.get_symbols()."""
366 return iter(self.kconfig_syms)
368 def get_choices(self):
369 """Returns a list containing all choice statements in the
370 configuration, in the order they appear in the Kconfig files."""
374 """Returns a list containing all menus in the configuration, in the
375 order they appear in the Kconfig files."""
378 def get_comments(self):
379 """Returns a list containing all comments in the configuration, in the
380 order they appear in the Kconfig files."""
383 def get_top_level_items(self):
384 """Returns a list containing the items (symbols, menus, choices, and
385 comments) at the top level of the configuration -- that is, all items
386 that do not appear within a menu or choice. The items appear in the
387 same order as within the configuration."""
388 return self.top_block
390 def load_config(self, filename, replace=True):
391 """Loads symbol values from a file in the familiar .config format.
392 Equivalent to calling Symbol.set_user_value() to set each of the
395 "# CONFIG_FOO is not set" within a .config file is treated specially
396 and sets the user value of FOO to 'n'. The C implementation works the
399 filename: The .config file to load. $-references to existing
400 environment variables will be expanded. For scripts to work even when
401 an alternative build directory is used with the Linux kernel, you
402 need to refer to the top-level kernel directory with "$srctree".
404 replace (default: True): True if the configuration should replace the
405 old configuration; False if it should add to it.
407 Returns a list or warnings (hopefully empty)
411 # Regular expressions for parsing .config files
412 _set_re_match = re.compile(r"{}(\w+)=(.*)".format(self.config_prefix)).match
413 _unset_re_match = re.compile(r"# {}(\w+) is not set".format(self.config_prefix)).match
415 # Put this first so that a missing file doesn't screw up our state
416 filename = os.path.expandvars(filename)
417 line_feeder = _FileFeed(filename)
419 self.config_filename = filename
425 def is_header_line(line):
426 return line is not None and line.startswith("#") and \
427 not _unset_re_match(line)
429 self.config_header = None
431 line = line_feeder.peek_next()
432 if is_header_line(line):
433 self.config_header = ""
434 while is_header_line(line_feeder.peek_next()):
435 self.config_header += line_feeder.get_next()[1:]
436 # Remove trailing newline
437 if self.config_header.endswith("\n"):
438 self.config_header = self.config_header[:-1]
441 # Read assignments. Hotspot for some workloads.
444 def warn_override(filename, linenr, name, old_user_val, new_user_val):
445 self._warn('overriding the value of {0}. '
446 'Old value: "{1}", new value: "{2}".'
447 .format(name, old_user_val, new_user_val),
450 # Invalidate everything to keep things simple. It might be possible to
451 # improve performance for the case where multiple configurations are
452 # loaded by only invalidating a symbol (and its dependent symbols) if
453 # the new user value differs from the old. One complication would be
454 # that symbols not mentioned in the .config must lose their user value
455 # when replace = True, which is the usual case.
457 self.unset_user_values()
459 self._invalidate_all()
462 line = line_feeder.get_next()
464 return self._warnings
468 set_match = _set_re_match(line)
470 name, val = set_match.groups()
472 if val.startswith('"'):
473 if len(val) < 2 or val[-1] != '"':
474 _parse_error(line, "malformed string literal",
475 line_feeder.filename, line_feeder.linenr)
476 # Strip quotes and remove escapings. The unescaping
477 # procedure should be safe since " can only appear as \"
479 val = val[1:-1].replace('\\"', '"').replace("\\\\", "\\")
481 if name in self.syms:
482 sym = self.syms[name]
483 if sym.user_val is not None:
484 warn_override(line_feeder.filename, line_feeder.linenr,
485 name, sym.user_val, val)
487 if sym.is_choice_sym:
488 user_mode = sym.parent.user_mode
489 if user_mode is not None and user_mode != val:
490 self._warn("assignment to {0} changes mode of "
491 'containing choice from "{1}" to "{2}".'
492 .format(name, val, user_mode),
493 line_feeder.filename,
496 sym._set_user_value_no_invalidate(val, True)
498 if self.print_undef_assign:
499 _stderr_msg('note: attempt to assign the value "{0}" '
500 "to the undefined symbol {1}."
502 line_feeder.filename, line_feeder.linenr)
504 unset_match = _unset_re_match(line)
506 name = unset_match.group(1)
507 if name in self.syms:
508 sym = self.syms[name]
509 if sym.user_val is not None:
510 warn_override(line_feeder.filename,
512 name, sym.user_val, "n")
514 sym._set_user_value_no_invalidate("n", True)
516 def write_config(self, filename, header=None):
517 """Writes out symbol values in the familiar .config format.
519 Kconfiglib makes sure the format matches what the C implementation
520 would generate, down to whitespace. This eases testing.
522 filename: The filename under which to save the configuration.
524 header (default: None): A textual header that will appear at the
525 beginning of the file, with each line commented out automatically.
526 None means no header."""
528 for sym in self.syms_iter():
529 sym.already_written = False
531 with open(filename, "w") as f:
533 if header is not None:
534 f.write(_comment(header) + "\n")
536 # Build and write configuration
538 _make_block_conf(self.top_block, conf_strings.append)
539 f.write("\n".join(conf_strings) + "\n")
542 """Returns the value of the expression 's' -- where 's' is represented
543 as a string -- in the context of the configuration. Raises
544 Kconfig_Syntax_Error if syntax errors are detected in 's'.
546 For example, if FOO and BAR are tristate symbols at least one of which
547 has the value "y", then config.eval("y && (FOO || BAR)") => "y"
549 This function always yields a tristate value. To get the value of
550 non-bool, non-tristate symbols, use Symbol.get_value().
552 The result of this function is consistent with how evaluation works for
553 conditional expressions in the configuration as well as in the C
554 implementation. "m" and m are rewritten as '"m" && MODULES' and 'm &&
555 MODULES', respectively, and a result of "m" will get promoted to "y" if
556 we're running without modules.
558 Syntax checking is somewhat lax, partly to be compatible with lax
559 parsing in the C implementation."""
560 return self._eval_expr(self._parse_expr(self._tokenize(s, True), # Feed
561 None, # Current symbol/choice
564 def unset_user_values(self):
565 """Resets the values of all symbols, as if Config.load_config() or
566 Symbol.set_user_value() had never been called."""
567 for sym in self.syms_iter():
568 sym._unset_user_value_no_recursive_invalidate()
570 def set_print_warnings(self, print_warnings):
571 """Determines whether warnings related to this configuration (for
572 things like attempting to assign illegal values to symbols with
573 Symbol.set_user_value()) should be printed to stderr.
575 print_warnings: True if warnings should be printed."""
576 self.print_warnings = print_warnings
578 def set_print_undef_assign(self, print_undef_assign):
579 """Determines whether informational messages related to assignments to
580 undefined symbols should be printed to stderr for this configuration.
582 print_undef_assign: If True, such messages will be printed."""
583 self.print_undef_assign = print_undef_assign
586 """Returns a string containing various information about the Config."""
587 return _lines("Configuration",
590 "Base directory : " +
592 "Value of $ARCH at creation time : " +
593 ("(not set)" if self.arch is None else self.arch),
594 "Value of $SRCARCH at creation time : " +
595 ("(not set)" if self.srcarch is None else
597 "Source tree (derived from $srctree;",
598 "defaults to '.' if $srctree isn't set) : " +
600 "Most recently loaded .config : " +
601 ("(no .config loaded)"
602 if self.config_filename is None else
603 self.config_filename),
604 "Print warnings : " +
605 BOOL_STR[self.print_warnings],
606 "Print assignments to undefined symbols : " +
607 BOOL_STR[self.print_undef_assign])
617 def _parse_file(self, filename, parent, deps, visible_if_deps, block):
618 """Parses the Kconfig file 'filename'. Appends the Items in the file
619 (and any file it sources) to the list passed in the 'block' parameter.
620 See _parse_block() for the meaning of the parameters."""
621 self._parse_block(_FileFeed(filename), None, parent, deps,
622 visible_if_deps, block)
624 def _parse_block(self, line_feeder, end_marker, parent, deps,
625 visible_if_deps, block):
626 """Parses a block, which is the contents of either a file or an if,
627 menu, or choice statement. Appends the Items to the list passed in the
630 line_feeder: A _FileFeed instance feeding lines from a file. The
631 Kconfig language is line-based in practice.
633 end_marker: The token that ends the block, e.g. T_ENDIF ("endif") for
636 parent: The enclosing menu or choice, or None if we're at the top
639 deps: Dependencies from enclosing menus, choices and ifs.
641 visible_if_deps (default: None): 'visible if' dependencies from
644 block: The list to add items to."""
647 # Do we already have a tokenized line that we determined wasn't
648 # part of whatever we were parsing earlier? See comment in
650 if self.end_line is not None:
652 tokens = self.end_line_tokens
656 self.end_line_tokens = None
658 line = line_feeder.get_next()
660 if end_marker is not None:
661 raise Kconfig_Syntax_Error("Unexpected end of file {0}"
662 .format(line_feeder.filename))
665 tokens = self._tokenize(line, False, line_feeder.filename,
668 t0 = tokens.get_next()
672 # Cases are ordered roughly by frequency, which speeds things up a
675 if t0 == T_CONFIG or t0 == T_MENUCONFIG:
676 # The tokenizer will automatically allocate a new Symbol object
677 # for any new names it encounters, so we don't need to worry
679 sym = tokens.get_next()
681 # Symbols defined in multiple places get the parent of their
682 # first definition. However, for symbols whose parents are
683 # choice statements, the choice statement takes precedence.
684 if not sym.is_defined_ or isinstance(parent, Choice):
686 sym.is_defined_ = True
688 self._parse_properties(line_feeder, sym, deps, visible_if_deps)
690 self.kconfig_syms.append(sym)
694 kconfig_file = tokens.get_next()
695 exp_kconfig_file = self._expand_sym_refs(kconfig_file)
696 f = os.path.join(self.base_dir, exp_kconfig_file)
697 if not os.path.exists(f):
698 raise IOError('{0}:{1}: sourced file "{2}" (expands to '
699 '"{3}") not found. Perhaps base_dir '
700 '(argument to Config.__init__(), currently '
701 '"{4}") is set to the wrong value.'
702 .format(line_feeder.filename,
704 kconfig_file, exp_kconfig_file,
706 # Add items to the same block
707 self._parse_file(f, parent, deps, visible_if_deps, block)
709 elif t0 == end_marker:
710 # We have reached the end of the block
714 # If statements are treated as syntactic sugar for adding
715 # dependencies to enclosed items and do not have an explicit
716 # object representation.
718 dep_expr = self._parse_expr(tokens, None, line,
719 line_feeder.filename,
721 # Add items to the same block
722 self._parse_block(line_feeder, T_ENDIF, parent,
723 _make_and(dep_expr, deps),
724 visible_if_deps, block)
726 elif t0 == T_COMMENT:
728 comment.config = self
729 comment.parent = parent
730 comment.filename = line_feeder.filename
731 comment.linenr = line_feeder.linenr
732 comment.text = tokens.get_next()
734 self._parse_properties(line_feeder, comment, deps,
737 self.comments.append(comment)
738 block.append(comment)
744 menu.filename = line_feeder.filename
745 menu.linenr = line_feeder.linenr
746 menu.title = tokens.get_next()
748 self._parse_properties(line_feeder, menu, deps,
751 # This needs to go before _parse_block() so that we get the
752 # proper menu ordering in the case of nested functions
753 self.menus.append(menu)
754 # Parse contents and put Items in menu.block
755 self._parse_block(line_feeder, T_ENDMENU, menu, menu.dep_expr,
756 _make_and(visible_if_deps,
757 menu.visible_if_expr),
763 name = tokens.get_next()
766 self.choices.append(choice)
769 choice = self.named_choices.get(name)
773 self.named_choices[name] = choice
774 self.choices.append(choice)
777 choice.parent = parent
779 choice.def_locations.append((line_feeder.filename,
782 self._parse_properties(line_feeder, choice, deps,
785 # Parse contents and put Items in choice.block
786 self._parse_block(line_feeder, T_ENDCHOICE, choice, deps,
787 visible_if_deps, choice.block)
789 choice._determine_actual_symbols()
791 # If no type is specified for the choice, its type is that of
792 # the first choice item with a specified type
793 if choice.type == UNKNOWN:
794 for item in choice.actual_symbols:
795 if item.type != UNKNOWN:
796 choice.type = item.type
799 # Each choice item of UNKNOWN type gets the type of the choice
800 for item in choice.actual_symbols:
801 if item.type == UNKNOWN:
802 item.type = choice.type
806 elif t0 == T_MAINMENU:
807 text = tokens.get_next()
808 if self.mainmenu_text is not None:
809 self._warn("overriding 'mainmenu' text. "
810 'Old value: "{0}", new value: "{1}".'
811 .format(self.mainmenu_text, text),
812 line_feeder.filename, line_feeder.linenr)
813 self.mainmenu_text = text
816 _parse_error(line, "unrecognized construct",
817 line_feeder.filename, line_feeder.linenr)
819 def _parse_properties(self, line_feeder, stmt, deps, visible_if_deps):
820 """Parsing of properties for symbols, menus, choices, and comments.
821 Takes care of propagating dependencies from enclosing menus and ifs."""
823 def parse_val_and_cond(tokens, line, filename, linenr):
824 """Parses '<expr1> if <expr2>' constructs, where the 'if' part is
825 optional. Returns a tuple containing the parsed expressions, with
826 None as the second element if the 'if' part is missing."""
827 return (self._parse_expr(tokens, stmt, line, filename, linenr,
829 self._parse_expr(tokens, stmt, line, filename, linenr)
830 if tokens.check(T_IF) else None)
832 # In case the symbol is defined in multiple locations, we need to
833 # remember what prompts, defaults, selects, and implies are new for
834 # this definition, as "depends on" should only apply to the local
841 # Dependencies from 'depends on' statements
842 depends_on_expr = None
845 line = line_feeder.get_next()
849 filename = line_feeder.filename
850 linenr = line_feeder.linenr
852 tokens = self._tokenize(line, False, filename, linenr)
854 t0 = tokens.get_next()
858 # Cases are ordered roughly by frequency, which speeds things up a
862 if not tokens.check(T_ON):
863 _parse_error(line, 'expected "on" after "depends"',
866 parsed_deps = self._parse_expr(tokens, stmt, line, filename,
869 if isinstance(stmt, (Menu, Comment)):
870 stmt.orig_deps = _make_and(stmt.orig_deps, parsed_deps)
872 depends_on_expr = _make_and(depends_on_expr, parsed_deps)
875 # Find first non-blank (not all-space) line and get its
877 line = line_feeder.next_nonblank()
881 indent = _indentation(line)
883 # If the first non-empty lines has zero indent, there is no
889 # The help text goes on till the first non-empty line with less
891 help_lines = [_deindent(line, indent)]
893 line = line_feeder.get_next()
895 (not line.isspace() and _indentation(line) < indent):
896 stmt.help = "".join(help_lines)
898 help_lines.append(_deindent(line, indent))
906 target = tokens.get_next()
908 stmt.referenced_syms.add(target)
909 stmt.selected_syms.add(target)
913 self._parse_expr(tokens, stmt, line, filename, linenr)
914 if tokens.check(T_IF) else None))
917 target = tokens.get_next()
919 stmt.referenced_syms.add(target)
920 stmt.implied_syms.add(target)
924 self._parse_expr(tokens, stmt, line, filename, linenr)
925 if tokens.check(T_IF) else None))
927 elif t0 in (T_BOOL, T_TRISTATE, T_INT, T_HEX, T_STRING):
928 stmt.type = TOKEN_TO_TYPE[t0]
929 if tokens.peek_next() is not None:
930 new_prompt = parse_val_and_cond(tokens, line, filename,
933 elif t0 == T_DEFAULT:
934 new_def_exprs.append(parse_val_and_cond(tokens, line, filename,
937 elif t0 == T_DEF_BOOL:
939 if tokens.peek_next() is not None:
940 new_def_exprs.append(parse_val_and_cond(tokens, line,
944 # 'prompt' properties override each other within a single
945 # definition of a symbol, but additional prompts can be added
946 # by defining the symbol multiple times; hence 'new_prompt'
947 # instead of 'prompt'.
948 new_prompt = parse_val_and_cond(tokens, line, filename, linenr)
951 low = tokens.get_next()
952 high = tokens.get_next()
953 stmt.referenced_syms.add(low)
954 stmt.referenced_syms.add(high)
958 self._parse_expr(tokens, stmt, line, filename, linenr)
959 if tokens.check(T_IF) else None))
961 elif t0 == T_DEF_TRISTATE:
963 if tokens.peek_next() is not None:
964 new_def_exprs.append(parse_val_and_cond(tokens, line,
968 if tokens.check(T_ENV) and tokens.check(T_EQUAL):
969 env_var = tokens.get_next()
971 stmt.is_special_ = True
972 stmt.is_from_env = True
974 if env_var not in os.environ:
975 self._warn("The symbol {0} references the "
976 "non-existent environment variable {1} and "
977 "will get the empty string as its value. "
978 "If you're using Kconfiglib via "
979 "'make (i)scriptconfig', it should have "
980 "set up the environment correctly for you. "
981 "If you still got this message, that "
982 "might be an error, and you should email "
983 "ulfalizer a.t Google's email service."""
984 .format(stmt.name, env_var),
989 stmt.cached_val = os.environ[env_var]
991 elif tokens.check(T_DEFCONFIG_LIST):
992 self.defconfig_sym = stmt
994 elif tokens.check(T_MODULES):
995 # To reduce warning spam, only warn if 'option modules' is
996 # set on some symbol that isn't MODULES, which should be
997 # safe. I haven't run into any projects that make use
998 # modules besides the kernel yet, and there it's likely to
999 # keep being called "MODULES".
1000 if stmt.name != "MODULES":
1001 self._warn("the 'modules' option is not supported. "
1002 "Let me know if this is a problem for you; "
1003 "it shouldn't be that hard to implement. "
1004 "(Note that modules are still supported -- "
1005 "Kconfiglib just assumes the symbol name "
1006 "MODULES, like older versions of the C "
1007 "implementation did when 'option modules' "
1011 elif tokens.check(T_ALLNOCONFIG_Y):
1012 if not isinstance(stmt, Symbol):
1014 "the 'allnoconfig_y' option is only "
1015 "valid for symbols",
1017 stmt.allnoconfig_y = True
1020 _parse_error(line, "unrecognized option", filename, linenr)
1022 elif t0 == T_VISIBLE:
1023 if not tokens.check(T_IF):
1024 _parse_error(line, 'expected "if" after "visible"',
1026 if not isinstance(stmt, Menu):
1028 "'visible if' is only valid for menus",
1031 parsed_deps = self._parse_expr(tokens, stmt, line, filename,
1033 stmt.visible_if_expr = _make_and(stmt.visible_if_expr,
1036 elif t0 == T_OPTIONAL:
1037 if not isinstance(stmt, Choice):
1039 '"optional" is only valid for choices',
1042 stmt.optional = True
1045 # See comment in Config.__init__()
1046 self.end_line = line
1047 self.end_line_tokens = tokens
1050 # Done parsing properties. Now propagate 'depends on' and enclosing
1051 # menu/if dependencies to expressions.
1053 # The set of symbols referenced directly by the statement plus all
1054 # symbols referenced by enclosing menus and ifs
1055 stmt.all_referenced_syms = stmt.referenced_syms | _get_expr_syms(deps)
1057 # Save original dependencies from enclosing menus and ifs
1058 stmt.deps_from_containing = deps
1060 if isinstance(stmt, (Menu, Comment)):
1061 stmt.dep_expr = _make_and(stmt.orig_deps, deps)
1065 # See comment for 'menu_dep'
1066 stmt.menu_dep = _make_and(deps, depends_on_expr)
1068 # Propagate dependencies to prompts
1070 if new_prompt is not None:
1071 prompt, cond_expr = new_prompt
1072 # Propagate 'visible if' dependencies from menus and local
1073 # 'depends on' dependencies
1074 cond_expr = _make_and(_make_and(cond_expr, visible_if_deps),
1077 stmt.orig_prompts.append((prompt, cond_expr))
1078 # Finalize with dependencies from enclosing menus and ifs
1079 stmt.prompts.append((prompt, _make_and(cond_expr, deps)))
1081 # Propagate dependencies to defaults
1083 # Propagate 'depends on' dependencies
1084 new_def_exprs = [(val_expr, _make_and(cond_expr, depends_on_expr))
1085 for val_expr, cond_expr in new_def_exprs]
1087 stmt.orig_def_exprs.extend(new_def_exprs)
1088 # Finalize with dependencies from enclosing menus and ifs
1089 stmt.def_exprs.extend([(val_expr, _make_and(cond_expr, deps))
1090 for val_expr, cond_expr in new_def_exprs])
1092 # Propagate dependencies to selects and implies
1094 # Only symbols can select and imply
1095 if isinstance(stmt, Symbol):
1096 # Propagate 'depends on' dependencies
1097 new_selects = [(target, _make_and(cond_expr, depends_on_expr))
1098 for target, cond_expr in new_selects]
1099 new_implies = [(target, _make_and(cond_expr, depends_on_expr))
1100 for target, cond_expr in new_implies]
1102 stmt.orig_selects.extend(new_selects)
1103 stmt.orig_implies.extend(new_implies)
1104 # Finalize with dependencies from enclosing menus and ifs
1105 for target, cond in new_selects:
1107 _make_or(target.rev_dep,
1108 _make_and(stmt, _make_and(cond, deps)))
1109 for target, cond in new_implies:
1110 target.weak_rev_dep = \
1111 _make_or(target.weak_rev_dep,
1112 _make_and(stmt, _make_and(cond, deps)))
1114 def _parse_expr(self, feed, cur_item, line, filename=None, linenr=None,
1116 """Parses an expression from the tokens in 'feed' using a simple
1117 top-down approach. The result has the form
1118 '(<operator>, [<parsed operands>])', where <operator> is e.g.
1119 kconfiglib.AND. If there is only one operand (i.e., no && or ||), then
1120 the operand is returned directly. This also goes for subexpressions.
1122 feed: _Feed instance containing the tokens for the expression.
1124 cur_item: The item (Symbol, Choice, Menu, or Comment) currently being
1125 parsed, or None if we're not parsing an item. Used for recording
1126 references to symbols.
1128 line: The line containing the expression being parsed.
1130 filename (default: None): The file containing the expression.
1132 linenr (default: None): The line number containing the expression.
1134 transform_m (default: False): Determines if 'm' should be rewritten to
1135 'm && MODULES' -- see parse_val_and_cond().
1137 Expression grammar, in decreasing order of precedence:
1140 <symbol> '=' <symbol>
1141 <symbol> '!=' <symbol>
1145 <expr> '||' <expr>"""
1147 # Use instance variables to avoid having to pass these as arguments
1148 # through the top-down parser in _parse_expr_rec(), which is tedious
1149 # and obfuscates the code. A profiler run shows no noticeable
1150 # performance difference.
1151 self._cur_item = cur_item
1152 self._transform_m = transform_m
1154 self._filename = filename
1155 self._linenr = linenr
1157 return self._parse_expr_rec(feed)
1159 def _parse_expr_rec(self, feed):
1160 or_term = self._parse_or_term(feed)
1161 if not feed.check(T_OR):
1162 # Common case -- no need for an OR node since it's just a single
1165 or_terms = [or_term, self._parse_or_term(feed)]
1166 while feed.check(T_OR):
1167 or_terms.append(self._parse_or_term(feed))
1168 return (OR, or_terms)
1170 def _parse_or_term(self, feed):
1171 and_term = self._parse_factor(feed)
1172 if not feed.check(T_AND):
1173 # Common case -- no need for an AND node since it's just a single
1176 and_terms = [and_term, self._parse_factor(feed)]
1177 while feed.check(T_AND):
1178 and_terms.append(self._parse_factor(feed))
1179 return (AND, and_terms)
1181 def _parse_factor(self, feed):
1182 token = feed.get_next()
1184 if isinstance(token, (Symbol, str)):
1185 if self._cur_item is not None and isinstance(token, Symbol):
1186 self._cur_item.referenced_syms.add(token)
1188 next_token = feed.peek_next()
1189 # For conditional expressions ('depends on <expr>',
1190 # '... if <expr>', # etc.), "m" and m are rewritten to
1192 if next_token != T_EQUAL and next_token != T_UNEQUAL:
1193 if self._transform_m and (token is self.m or token == "m"):
1194 return (AND, ["m", self._sym_lookup("MODULES")])
1197 relation = EQUAL if (feed.get_next() == T_EQUAL) else UNEQUAL
1198 token_2 = feed.get_next()
1199 if self._cur_item is not None and isinstance(token_2, Symbol):
1200 self._cur_item.referenced_syms.add(token_2)
1201 return (relation, token, token_2)
1204 return (NOT, self._parse_factor(feed))
1206 if token == T_OPEN_PAREN:
1207 expr_parse = self._parse_expr_rec(feed)
1208 if not feed.check(T_CLOSE_PAREN):
1209 _parse_error(self._line, "missing end parenthesis",
1210 self._filename, self._linenr)
1213 _parse_error(self._line, "malformed expression", self._filename,
1216 def _tokenize(self, s, for_eval, filename=None, linenr=None):
1217 """Returns a _Feed instance containing tokens derived from the string
1218 's'. Registers any new symbols encountered (via _sym_lookup()).
1220 (I experimented with a pure regular expression implementation, but it
1221 came out slower, less readable, and wouldn't have been as flexible.)
1223 for_eval: True when parsing an expression for a call to Config.eval(),
1224 in which case we should not treat the first token specially nor
1225 register new symbols."""
1228 if s == "" or s[0] == "#":
1232 previous = None # The previous token seen
1234 i = 0 # The current index in the string being tokenized
1237 # The initial word on a line is parsed specially. Let
1238 # command_chars = [A-Za-z0-9_]. Then
1239 # - leading non-command_chars characters are ignored, and
1240 # - the first token consists the following one or more
1241 # command_chars characters.
1242 # This is why things like "----help--" are accepted.
1243 initial_token_match = _initial_token_re_match(s)
1244 if initial_token_match is None:
1246 keyword = _get_keyword(initial_token_match.group(1))
1247 if keyword == T_HELP:
1248 # Avoid junk after "help", e.g. "---", being registered as a
1250 return _Feed([T_HELP])
1252 # We expect a keyword as the first token
1253 _tokenization_error(s, filename, linenr)
1257 # The current index in the string being tokenized
1258 i = initial_token_match.end()
1260 # _tokenize() is a hotspot during parsing, and this speeds things up a
1263 append = tokens.append
1265 # Main tokenization loop. (Handles tokens past the first one.)
1267 # Test for an identifier/keyword preceded by whitespace first; this
1268 # is the most common case.
1269 id_keyword_match = _id_keyword_re_match(s, i)
1270 if id_keyword_match:
1271 # We have an identifier or keyword. The above also stripped any
1272 # whitespace for us.
1273 name = id_keyword_match.group(1)
1275 i = id_keyword_match.end()
1277 keyword = _get_keyword(name)
1278 if keyword is not None:
1281 elif previous in STRING_LEX:
1282 # What would ordinarily be considered an identifier is
1283 # treated as a string after certain tokens
1286 # It's a symbol name. _sym_lookup() will take care of
1287 # allocating a new Symbol instance if it's the first time
1289 sym = self._sym_lookup(name, for_eval)
1291 if previous == T_CONFIG or previous == T_MENUCONFIG:
1292 # If the previous token is T_(MENU)CONFIG
1293 # ("(menu)config"), we're tokenizing the first line of
1294 # a symbol definition, and should remember this as a
1295 # location where the symbol is defined
1296 sym.def_locations.append((filename, linenr))
1298 # Otherwise, it's a reference to the symbol
1299 sym.ref_locations.append((filename, linenr))
1304 # Not an identifier/keyword
1306 while i < strlen and s[i].isspace():
1313 # String literal (constant symbol)
1314 if c == '"' or c == "'":
1316 # Slow path: This could probably be sped up, but it's a
1317 # very unusual case anyway.
1322 _tokenization_error(s, filename, linenr)
1328 _tokenization_error(s, filename, linenr)
1337 # Fast path: If the string contains no backslashes
1338 # (almost always) we can simply look for the matching
1342 _tokenization_error(s, filename, linenr)
1347 # Invalid characters are ignored
1348 if i >= len(s) or s[i] != "&": continue
1353 # Invalid characters are ignored
1354 if i >= len(s) or s[i] != "|": continue
1359 if i < len(s) and s[i] == "=":
1365 elif c == "=": append(T_EQUAL)
1366 elif c == "(": append(T_OPEN_PAREN)
1367 elif c == ")": append(T_CLOSE_PAREN)
1368 elif c == "#": break # Comment
1370 else: continue # Invalid characters are ignored
1372 previous = tokens[-1]
1374 return _Feed(tokens)
1376 def _sym_lookup(self, name, for_eval=False):
1377 """Fetches the symbol 'name' from the symbol table, creating and
1378 registering it if it does not exist. If 'for_eval' is True, the symbol
1379 won't be added to the symbol table if it does not exist -- this is for
1381 if name in self.syms:
1382 return self.syms[name]
1385 new_sym.config = self
1388 self._warn("no symbol {0} in configuration".format(name))
1390 self.syms[name] = new_sym
1394 # Expression evaluation
1397 def _eval_expr(self, expr):
1398 """Evaluates an expression to "n", "m", or "y"."""
1400 # Handles e.g. an "x if y" condition where the "if y" part is missing.
1404 res = self._eval_expr_rec(expr)
1406 # Promote "m" to "y" if we're running without modules.
1408 # Internally, "m" is often rewritten to "m" && MODULES by both the
1409 # C implementation and Kconfiglib, which takes care of cases where
1410 # "m" should be demoted to "n" instead.
1411 modules_sym = self.syms.get("MODULES")
1412 if modules_sym is None or modules_sym.get_value() != "y":
1416 def _eval_expr_rec(self, expr):
1417 if isinstance(expr, Symbol):
1418 # Non-bool/tristate symbols are always "n" in a tristate sense,
1419 # regardless of their value
1420 if expr.type != BOOL and expr.type != TRISTATE:
1422 return expr.get_value()
1424 if isinstance(expr, str):
1425 return expr if (expr == "y" or expr == "m") else "n"
1427 # Ordered by frequency
1431 for subexpr in expr[1]:
1432 ev = self._eval_expr_rec(subexpr)
1433 # Return immediately upon discovering an "n" term
1438 # 'res' is either "m" or "y" here; we already handled the
1439 # short-circuiting "n" case in the loop.
1443 ev = self._eval_expr_rec(expr[1])
1446 return "y" if (ev == "n") else "m"
1450 for subexpr in expr[1]:
1451 ev = self._eval_expr_rec(subexpr)
1452 # Return immediately upon discovering a "y" term
1457 # 'res' is either "n" or "m" here; we already handled the
1458 # short-circuiting "y" case in the loop.
1461 if expr[0] == EQUAL:
1462 return "y" if (_str_val(expr[1]) == _str_val(expr[2])) else "n"
1464 if expr[0] == UNEQUAL:
1465 return "y" if (_str_val(expr[1]) != _str_val(expr[2])) else "n"
1467 _internal_error("Internal error while evaluating expression: "
1468 "unknown operation {0}.".format(expr[0]))
1470 def _eval_min(self, e1, e2):
1471 """Returns the minimum value of the two expressions. Equates None with
1473 e1_eval = self._eval_expr(e1)
1474 e2_eval = self._eval_expr(e2)
1475 return e1_eval if tri_less(e1_eval, e2_eval) else e2_eval
1477 def _eval_max(self, e1, e2):
1478 """Returns the maximum value of the two expressions. Equates None with
1480 e1_eval = self._eval_expr(e1)
1481 e2_eval = self._eval_expr(e2)
1482 return e1_eval if tri_greater(e1_eval, e2_eval) else e2_eval
1485 # Dependency tracking (for caching and invalidation)
1488 def _build_dep(self):
1489 """Populates the Symbol.dep sets, linking the symbol to the symbols
1490 that immediately depend on it in the sense that changing the value of
1491 the symbol might affect the values of those other symbols. This is used
1492 for caching/invalidation purposes. The calculated sets might be larger
1493 than necessary as we don't do any complicated analysis of the
1496 # Adds 'sym' as a directly dependent symbol to all symbols that appear
1497 # in the expression 'e'
1498 def add_expr_deps(e, sym):
1499 for s in _get_expr_syms(e):
1502 # The directly dependent symbols of a symbol are:
1503 # - Any symbols whose prompts, default values, rev_dep (select
1504 # condition), weak_rev_dep (imply condition) or ranges depend on the
1506 # - Any symbols that belong to the same choice statement as the symbol
1507 # (these won't be included in 'dep' as that makes the dependency
1508 # graph unwieldy, but Symbol._get_dependent() will include them)
1509 # - Any symbols in a choice statement that depends on the symbol
1510 for sym in self.syms_iter():
1511 for _, e in sym.prompts:
1512 add_expr_deps(e, sym)
1514 for v, e in sym.def_exprs:
1515 add_expr_deps(v, sym)
1516 add_expr_deps(e, sym)
1518 add_expr_deps(sym.rev_dep, sym)
1519 add_expr_deps(sym.weak_rev_dep, sym)
1521 for l, u, e in sym.ranges:
1522 add_expr_deps(l, sym)
1523 add_expr_deps(u, sym)
1524 add_expr_deps(e, sym)
1526 if sym.is_choice_sym:
1528 for _, e in choice.prompts:
1529 add_expr_deps(e, sym)
1530 for _, e in choice.def_exprs:
1531 add_expr_deps(e, sym)
1533 def _eq_to_sym(self, eq):
1534 """_expr_depends_on() helper. For (in)equalities of the form sym = y/m
1535 or sym != n, returns sym. For other (in)equalities, returns None."""
1536 relation, left, right = eq
1538 def transform_y_m_n(item):
1539 if item is self.y: return "y"
1540 if item is self.m: return "m"
1541 if item is self.n: return "n"
1544 left = transform_y_m_n(left)
1545 right = transform_y_m_n(right)
1547 # Make sure the symbol (if any) appears to the left
1548 if not isinstance(left, Symbol):
1549 left, right = right, left
1550 if not isinstance(left, Symbol):
1552 if (relation == EQUAL and (right == "y" or right == "m")) or \
1553 (relation == UNEQUAL and right == "n"):
1557 def _expr_depends_on(self, expr, sym):
1558 """Reimplementation of expr_depends_symbol() from mconf.c. Used to
1559 determine if a submenu should be implicitly created, which influences
1560 what items inside choice statements are considered choice items."""
1565 if isinstance(expr, str):
1567 if isinstance(expr, Symbol):
1570 if expr[0] in (EQUAL, UNEQUAL):
1571 return self._eq_to_sym(expr) is sym
1573 for and_expr in expr[1]:
1580 def _invalidate_all(self):
1581 for sym in self.syms_iter():
1585 # Printing and misc.
1588 def _expand_sym_refs(self, s):
1589 """Expands $-references to symbols in 's' to symbol values, or to the
1590 empty string for undefined symbols."""
1593 sym_ref_match = _sym_ref_re_search(s)
1594 if sym_ref_match is None:
1597 sym_name = sym_ref_match.group(0)[1:]
1598 sym = self.syms.get(sym_name)
1599 expansion = "" if sym is None else sym.get_value()
1601 s = s[:sym_ref_match.start()] + \
1603 s[sym_ref_match.end():]
1605 def _expr_val_str(self, expr, no_value_str="(none)",
1606 get_val_instead_of_eval=False):
1607 """Printing helper. Returns a string with 'expr' and its value.
1609 no_value_str: String to return when 'expr' is missing (None).
1611 get_val_instead_of_eval: Assume 'expr' is a symbol or string (constant
1612 symbol) and get its value directly instead of evaluating it to a
1618 if get_val_instead_of_eval:
1619 if isinstance(expr, str):
1620 return _expr_to_str(expr)
1621 val = expr.get_value()
1623 val = self._eval_expr(expr)
1625 return "{0} (value: {1})".format(_expr_to_str(expr), _expr_to_str(val))
1627 def _get_sym_or_choice_str(self, sc):
1628 """Symbols and choices have many properties in common, so we factor out
1629 common __str__() stuff here. "sc" is short for "symbol or choice"."""
1631 # As we deal a lot with string representations here, use some
1632 # convenient shorthand:
1636 # Common symbol/choice properties
1639 user_val_str = "(no user value)" if sc.user_val is None else \
1642 # Build prompts string
1644 prompts_str = " (no prompts)"
1646 prompts_str_rows = []
1647 for prompt, cond_expr in sc.orig_prompts:
1648 prompts_str_rows.append(
1649 ' "{0}"'.format(prompt) if cond_expr is None else
1650 ' "{0}" if {1}'.format(prompt,
1651 self._expr_val_str(cond_expr)))
1652 prompts_str = "\n".join(prompts_str_rows)
1654 # Build locations string
1655 locations_str = "(no locations)" if not sc.def_locations else \
1656 " ".join(["{0}:{1}".format(filename, linenr) for
1657 filename, linenr in sc.def_locations])
1659 # Build additional-dependencies-from-menus-and-ifs string
1660 additional_deps_str = " " + \
1661 self._expr_val_str(sc.deps_from_containing,
1662 "(no additional dependencies)")
1665 # Symbol-specific stuff
1668 if isinstance(sc, Symbol):
1669 # Build ranges string
1670 if isinstance(sc, Symbol):
1672 ranges_str = " (no ranges)"
1674 ranges_str_rows = []
1675 for l, u, cond_expr in sc.ranges:
1676 ranges_str_rows.append(
1677 " [{0}, {1}]".format(s(l), s(u))
1678 if cond_expr is None else
1679 " [{0}, {1}] if {2}"
1680 .format(s(l), s(u), self._expr_val_str(cond_expr)))
1681 ranges_str = "\n".join(ranges_str_rows)
1683 # Build default values string
1684 if not sc.def_exprs:
1685 defaults_str = " (no default values)"
1687 defaults_str_rows = []
1688 for val_expr, cond_expr in sc.orig_def_exprs:
1689 row_str = " " + self._expr_val_str(val_expr, "(none)",
1691 defaults_str_rows.append(row_str)
1692 defaults_str_rows.append(" Condition: " +
1693 self._expr_val_str(cond_expr))
1694 defaults_str = "\n".join(defaults_str_rows)
1696 # Build selects string
1697 if not sc.orig_selects:
1698 selects_str = " (no selects)"
1700 selects_str_rows = []
1701 for target, cond_expr in sc.orig_selects:
1702 selects_str_rows.append(
1703 " {0}".format(target.name) if cond_expr is None else
1704 " {0} if {1}".format(target.name,
1705 self._expr_val_str(cond_expr)))
1706 selects_str = "\n".join(selects_str_rows)
1708 # Build implies string
1709 if not sc.orig_implies:
1710 implies_str = " (no implies)"
1712 implies_str_rows = []
1713 for target, cond_expr in sc.orig_implies:
1714 implies_str_rows.append(
1715 " {0}".format(target.name) if cond_expr is None else
1716 " {0} if {1}".format(target.name,
1717 self._expr_val_str(cond_expr)))
1718 implies_str = "\n".join(implies_str_rows)
1720 res = _lines("Symbol " +
1721 ("(no name)" if sc.name is None else sc.name),
1722 "Type : " + TYPENAME[sc.type],
1723 "Value : " + s(sc.get_value()),
1724 "User value : " + user_val_str,
1725 "Visibility : " + s(_get_visibility(sc)),
1726 "Is choice item : " + BOOL_STR[sc.is_choice_sym],
1727 "Is defined : " + BOOL_STR[sc.is_defined_],
1728 "Is from env. : " + BOOL_STR[sc.is_from_env],
1729 "Is special : " + BOOL_STR[sc.is_special_] + "\n")
1731 res += _lines("Ranges:", ranges_str + "\n")
1732 res += _lines("Prompts:",
1740 "Reverse (select-related) dependencies:",
1741 " (no reverse dependencies)"
1742 if sc.rev_dep == "n"
1743 else " " + self._expr_val_str(sc.rev_dep),
1744 "Weak reverse (imply-related) dependencies:",
1745 " (no weak reverse dependencies)"
1746 if sc.weak_rev_dep == "n"
1747 else " " + self._expr_val_str(sc.weak_rev_dep),
1748 "Additional dependencies from enclosing menus "
1750 additional_deps_str,
1751 "Locations: " + locations_str)
1756 # Choice-specific stuff
1759 # Build selected symbol string
1760 sel = sc.get_selection()
1761 sel_str = "(no selection)" if sel is None else sel.name
1763 # Build default values string
1764 if not sc.def_exprs:
1765 defaults_str = " (no default values)"
1767 defaults_str_rows = []
1768 for sym, cond_expr in sc.orig_def_exprs:
1769 defaults_str_rows.append(
1770 " {0}".format(sym.name) if cond_expr is None else
1771 " {0} if {1}".format(sym.name,
1772 self._expr_val_str(cond_expr)))
1773 defaults_str = "\n".join(defaults_str_rows)
1775 # Build contained symbols string
1776 names = [sym.name for sym in sc.actual_symbols]
1777 syms_string = " ".join(names) if names else "(empty)"
1779 return _lines("Choice",
1780 "Name (for named choices): " +
1781 ("(no name)" if sc.name is None else sc.name),
1782 "Type : " + TYPENAME[sc.type],
1783 "Selected symbol : " + sel_str,
1784 "User value : " + user_val_str,
1785 "Mode : " + s(sc.get_mode()),
1786 "Visibility : " + s(_get_visibility(sc)),
1787 "Optional : " + BOOL_STR[sc.optional],
1794 "Additional dependencies from enclosing menus and "
1796 additional_deps_str,
1797 "Locations: " + locations_str)
1799 def _warn(self, msg, filename=None, linenr=None):
1800 """For printing warnings to stderr."""
1801 msg = _build_msg("warning: " + msg, filename, linenr)
1802 if self.print_warnings:
1803 sys.stderr.write(msg + "\n")
1804 self._warnings.append(msg)
1808 """Base class for symbols and other Kconfig constructs. Subclasses are
1809 Symbol, Choice, Menu, and Comment."""
1811 def is_symbol(self):
1812 """Returns True if the item is a symbol. Short for
1813 isinstance(item, kconfiglib.Symbol)."""
1814 return isinstance(self, Symbol)
1816 def is_choice(self):
1817 """Returns True if the item is a choice. Short for
1818 isinstance(item, kconfiglib.Choice)."""
1819 return isinstance(self, Choice)
1822 """Returns True if the item is a menu. Short for
1823 isinstance(item, kconfiglib.Menu)."""
1824 return isinstance(self, Menu)
1826 def is_comment(self):
1827 """Returns True if the item is a comment. Short for
1828 isinstance(item, kconfiglib.Comment)."""
1829 return isinstance(self, Comment)
1833 """Represents a configuration symbol - e.g. FOO for
1842 def get_config(self):
1843 """Returns the Config instance this symbol is from."""
1847 """Returns the name of the symbol."""
1851 """Returns the type of the symbol: one of UNKNOWN, BOOL, TRISTATE,
1852 STRING, HEX, or INT. These are defined at the top level of the module,
1853 so you'd do something like
1855 if sym.get_type() == kconfiglib.STRING:
1859 def get_prompts(self):
1860 """Returns a list of prompts defined for the symbol, in the order they
1861 appear in the configuration files. Returns the empty list for symbols
1864 This list will have a single entry for the vast majority of symbols
1865 having prompts, but having multiple prompts for a single symbol is
1866 possible through having multiple 'config' entries for it."""
1867 return [prompt for prompt, _ in self.orig_prompts]
1870 """Returns the help text of the symbol, or None if the symbol has no
1874 def get_parent(self):
1875 """Returns the menu or choice statement that contains the symbol, or
1876 None if the symbol is at the top level. Note that if statements are
1877 treated as syntactic and do not have an explicit class
1881 def get_def_locations(self):
1882 """Returns a list of (filename, linenr) tuples, where filename (string)
1883 and linenr (int) represent a location where the symbol is defined. For
1884 the vast majority of symbols this list will only contain one element.
1885 For the following Kconfig, FOO would get two entries: the lines marked
1894 return self.def_locations
1896 def get_ref_locations(self):
1897 """Returns a list of (filename, linenr) tuples, where filename (string)
1898 and linenr (int) represent a location where the symbol is referenced in
1899 the configuration. For example, the lines marked by * would be included
1904 default BAR || FOO *
1916 config FOO (definition not included)
1919 return self.ref_locations
1921 def get_value(self):
1922 """Calculate and return the value of the symbol. See also
1923 Symbol.set_user_value()."""
1925 if self.cached_val is not None:
1926 return self.cached_val
1928 # As a quirk of Kconfig, undefined symbols get their name as their
1929 # value. This is why things like "FOO = bar" work for seeing if FOO has
1931 if self.type == UNKNOWN:
1932 self.cached_val = self.name
1935 new_val = DEFAULT_VALUE[self.type]
1936 vis = _get_visibility(self)
1938 # This is easiest to calculate together with the value
1939 self.write_to_conf = False
1941 if self.type == BOOL or self.type == TRISTATE:
1942 # The visibility and mode (modules-only or single-selection) of
1943 # choice items will be taken into account in _get_visibility()
1944 if self.is_choice_sym:
1946 choice = self.parent
1947 mode = choice.get_mode()
1949 self.write_to_conf = (mode != "n")
1952 new_val = "y" if choice.get_selection() is self \
1955 if self.user_val == "m" or self.user_val == "y":
1959 # If the symbol is visible and has a user value, use that.
1960 # Otherwise, look at defaults and weak reverse dependencies
1962 use_defaults_and_weak_rev_deps = True
1965 self.write_to_conf = True
1966 if self.user_val is not None:
1967 new_val = self.config._eval_min(self.user_val, vis)
1968 use_defaults_and_weak_rev_deps = False
1970 if use_defaults_and_weak_rev_deps:
1971 for val_expr, cond_expr in self.def_exprs:
1972 cond_eval = self.config._eval_expr(cond_expr)
1973 if cond_eval != "n":
1974 self.write_to_conf = True
1975 new_val = self.config._eval_min(val_expr,
1979 weak_rev_dep_val = \
1980 self.config._eval_expr(self.weak_rev_dep)
1981 if weak_rev_dep_val != "n":
1982 self.write_to_conf = True
1983 new_val = self.config._eval_max(new_val,
1986 # Reverse (select-related) dependencies take precedence
1987 rev_dep_val = self.config._eval_expr(self.rev_dep)
1988 if rev_dep_val != "n":
1989 self.write_to_conf = True
1990 new_val = self.config._eval_max(new_val, rev_dep_val)
1992 # We need to promote "m" to "y" in two circumstances:
1993 # 1) If our type is boolean
1994 # 2) If our weak_rev_dep (from IMPLY) is "y"
1995 if new_val == "m" and \
1996 (self.type == BOOL or
1997 self.config._eval_expr(self.weak_rev_dep) == "y"):
2000 elif self.type == INT or self.type == HEX:
2001 has_active_range = False
2006 base = 16 if self.type == HEX else 10
2008 for l, h, cond_expr in self.ranges:
2009 if self.config._eval_expr(cond_expr) != "n":
2010 has_active_range = True
2012 low_str = _str_val(l)
2013 high_str = _str_val(h)
2014 low = int(low_str, base) if \
2015 _is_base_n(low_str, base) else 0
2016 high = int(high_str, base) if \
2017 _is_base_n(high_str, base) else 0
2022 self.write_to_conf = True
2024 if self.user_val is not None and \
2025 _is_base_n(self.user_val, base) and \
2026 (not has_active_range or
2027 low <= int(self.user_val, base) <= high):
2029 # If the user value is OK, it is stored in exactly the same
2030 # form as specified in the assignment (with or without
2033 use_defaults = False
2034 new_val = self.user_val
2037 for val_expr, cond_expr in self.def_exprs:
2038 if self.config._eval_expr(cond_expr) != "n":
2039 self.write_to_conf = True
2041 # If the default value is OK, it is stored in exactly
2042 # the same form as specified. Otherwise, it is clamped
2043 # to the range, and the output has "0x" as appropriate
2046 new_val = _str_val(val_expr)
2048 if _is_base_n(new_val, base):
2049 new_val_num = int(new_val, base)
2050 if has_active_range:
2053 if new_val_num < low:
2055 elif new_val_num > high:
2058 if clamped_val is not None:
2059 new_val = (hex(clamped_val) if \
2060 self.type == HEX else str(clamped_val))
2063 else: # For the for loop
2064 # If no user value or default kicks in but the hex/int has
2065 # an active range, then the low end of the range is used,
2066 # provided it's > 0, with "0x" prepended as appropriate.
2067 if has_active_range and low > 0:
2068 new_val = (hex(low) if self.type == HEX else str(low))
2070 elif self.type == STRING:
2074 self.write_to_conf = True
2075 if self.user_val is not None:
2076 new_val = self.user_val
2077 use_defaults = False
2080 for val_expr, cond_expr in self.def_exprs:
2081 if self.config._eval_expr(cond_expr) != "n":
2082 self.write_to_conf = True
2083 new_val = _str_val(val_expr)
2086 self.cached_val = new_val
2089 def get_user_value(self):
2090 """Returns the value assigned to the symbol in a .config or via
2091 Symbol.set_user_value() (provided the value was valid for the type of
2092 the symbol). Returns None in case of no user value."""
2093 return self.user_val
2095 def get_upper_bound(self):
2096 """For string/hex/int symbols and for bool and tristate symbols that
2097 cannot be modified (see is_modifiable()), returns None.
2099 Otherwise, returns the highest value the symbol can be set to with
2100 Symbol.set_user_value() (that will not be truncated): one of "m" or
2101 "y", arranged from lowest to highest. This corresponds to the highest
2102 value the symbol could be given in e.g. the 'make menuconfig'
2105 See also the tri_less*() and tri_greater*() functions, which could come
2107 if self.type != BOOL and self.type != TRISTATE:
2109 rev_dep = self.config._eval_expr(self.rev_dep)
2110 # A bool selected to "m" gets promoted to "y", pinning it
2111 if rev_dep == "m" and self.type == BOOL:
2113 vis = _get_visibility(self)
2114 if TRI_TO_INT[vis] > TRI_TO_INT[rev_dep]:
2118 def get_lower_bound(self):
2119 """For string/hex/int symbols and for bool and tristate symbols that
2120 cannot be modified (see is_modifiable()), returns None.
2122 Otherwise, returns the lowest value the symbol can be set to with
2123 Symbol.set_user_value() (that will not be truncated): one of "n" or
2124 "m", arranged from lowest to highest. This corresponds to the lowest
2125 value the symbol could be given in e.g. the 'make menuconfig'
2128 See also the tri_less*() and tri_greater*() functions, which could come
2130 if self.type != BOOL and self.type != TRISTATE:
2132 rev_dep = self.config._eval_expr(self.rev_dep)
2133 # A bool selected to "m" gets promoted to "y", pinning it
2134 if rev_dep == "m" and self.type == BOOL:
2136 if TRI_TO_INT[_get_visibility(self)] > TRI_TO_INT[rev_dep]:
2140 def get_assignable_values(self):
2141 """For string/hex/int symbols and for bool and tristate symbols that
2142 cannot be modified (see is_modifiable()), returns the empty list.
2144 Otherwise, returns a list containing the user values that can be
2145 assigned to the symbol (that won't be truncated). Usage example:
2147 if "m" in sym.get_assignable_values():
2148 sym.set_user_value("m")
2150 This is basically a more convenient interface to
2151 get_lower/upper_bound() when wanting to test if a particular tristate
2152 value can be assigned."""
2153 if self.type != BOOL and self.type != TRISTATE:
2155 rev_dep = self.config._eval_expr(self.rev_dep)
2156 # A bool selected to "m" gets promoted to "y", pinning it
2157 if rev_dep == "m" and self.type == BOOL:
2159 res = ["n", "m", "y"][TRI_TO_INT[rev_dep] :
2160 TRI_TO_INT[_get_visibility(self)] + 1]
2161 return res if len(res) > 1 else []
2163 def get_visibility(self):
2164 """Returns the visibility of the symbol: one of "n", "m" or "y". For
2165 bool and tristate symbols, this is an upper bound on the value users
2166 can set for the symbol. For other types of symbols, a visibility of "n"
2167 means the user value will be ignored. A visibility of "n" corresponds
2168 to not being visible in the 'make *config' interfaces.
2170 Example (assuming we're running with modules enabled -- i.e., MODULES
2173 # Assume this has been assigned 'n'
2177 # Assume this has been assigned 'm'
2181 # Has visibility 'n'
2186 # Has visibility 'm'
2191 # Has visibility 'y'
2195 # Has no prompt, and hence visibility 'n'
2199 Having visibility be tri-valued ensures that e.g. a symbol cannot be
2200 set to "y" by the user if it depends on a symbol with value "m", which
2203 You should probably look at get_lower/upper_bound(),
2204 get_assignable_values() and is_modifiable() before using this."""
2205 return _get_visibility(self)
2207 def get_referenced_symbols(self, refs_from_enclosing=False):
2208 """Returns the set() of all symbols referenced by this symbol. For
2209 example, the symbol defined by
2213 prompt "foo" if A && B
2218 references the symbols A through G.
2220 refs_from_enclosing (default: False): If True, the symbols referenced
2221 by enclosing menus and ifs will be included in the result."""
2222 return self.all_referenced_syms if refs_from_enclosing else \
2223 self.referenced_syms
2225 def get_selected_symbols(self):
2226 """Returns the set() of all symbols X for which this symbol has a
2227 'select X' or 'select X if Y' (regardless of whether Y is satisfied or
2228 not). This is a subset of the symbols returned by
2229 get_referenced_symbols()."""
2230 return self.selected_syms
2232 def get_implied_symbols(self):
2233 """Returns the set() of all symbols X for which this symbol has an
2234 'imply X' or 'imply X if Y' (regardless of whether Y is satisfied or
2235 not). This is a subset of the symbols returned by
2236 get_referenced_symbols()."""
2237 return self.implied_syms
2239 def set_user_value(self, v):
2240 """Sets the user value of the symbol.
2242 Equal in effect to assigning the value to the symbol within a .config
2243 file. Use get_lower/upper_bound() or get_assignable_values() to find
2244 the range of currently assignable values for bool and tristate symbols;
2245 setting values outside this range will cause the user value to differ
2246 from the result of Symbol.get_value() (be truncated). Values that are
2247 invalid for the type (such as a_bool.set_user_value("foo")) are
2248 ignored, and a warning is emitted if an attempt is made to assign such
2251 For any type of symbol, is_modifiable() can be used to check if a user
2252 value will currently have any effect on the symbol, as determined by
2253 its visibility and range of assignable values. Any value that is valid
2254 for the type (bool, tristate, etc.) will end up being reflected in
2255 get_user_value() though, and might have an effect later if conditions
2256 change. To get rid of the user value, use unset_user_value().
2258 Any symbols dependent on the symbol are (recursively) invalidated, so
2259 things will just work with regards to dependencies.
2261 v: The user value to give to the symbol."""
2262 self._set_user_value_no_invalidate(v, False)
2264 # There might be something more efficient you could do here, but play
2266 if self.name == "MODULES":
2267 self.config._invalidate_all()
2271 self._invalidate_dependent()
2273 def unset_user_value(self):
2274 """Resets the user value of the symbol, as if the symbol had never
2275 gotten a user value via Config.load_config() or
2276 Symbol.set_user_value()."""
2277 self._unset_user_value_no_recursive_invalidate()
2278 self._invalidate_dependent()
2280 def is_modifiable(self):
2281 """Returns True if the value of the symbol could be modified by calling
2282 Symbol.set_user_value().
2284 For bools and tristates, this corresponds to the symbol being visible
2285 in the 'make menuconfig' interface and not already being pinned to a
2286 specific value (e.g. because it is selected by another symbol).
2288 For strings and numbers, this corresponds to just being visible. (See
2289 Symbol.get_visibility().)"""
2290 if self.is_special_:
2292 if self.type == BOOL or self.type == TRISTATE:
2293 rev_dep = self.config._eval_expr(self.rev_dep)
2294 # A bool selected to "m" gets promoted to "y", pinning it
2295 if rev_dep == "m" and self.type == BOOL:
2297 return TRI_TO_INT[_get_visibility(self)] > TRI_TO_INT[rev_dep]
2298 return _get_visibility(self) != "n"
2300 def is_defined(self):
2301 """Returns False if the symbol is referred to in the Kconfig but never
2302 actually defined."""
2303 return self.is_defined_
2305 def is_special(self):
2306 """Returns True if the symbol is one of the special symbols n, m, y, or
2307 UNAME_RELEASE, or gets its value from the environment."""
2308 return self.is_special_
2310 def is_from_environment(self):
2311 """Returns True if the symbol gets its value from the environment."""
2312 return self.is_from_env
2314 def has_ranges(self):
2315 """Returns True if the symbol is of type INT or HEX and has ranges that
2316 limit what values it can take on."""
2317 return bool(self.ranges)
2319 def is_choice_symbol(self):
2320 """Returns True if the symbol is in a choice statement and is an actual
2321 choice symbol (see Choice.get_symbols())."""
2322 return self.is_choice_sym
2324 def is_choice_selection(self):
2325 """Returns True if the symbol is contained in a choice statement and is
2326 the selected item. Equivalent to
2328 sym.is_choice_symbol() and sym.get_parent().get_selection() is sym"""
2329 return self.is_choice_sym and self.parent.get_selection() is self
2331 def is_allnoconfig_y(self):
2332 """Returns True if the symbol has the 'allnoconfig_y' option set."""
2333 return self.allnoconfig_y
2336 """Returns a string containing various information about the symbol."""
2337 return self.config._get_sym_or_choice_str(self)
2344 """Symbol constructor -- not intended to be called directly by
2345 Kconfiglib clients."""
2350 self.def_exprs = [] # 'default' properties
2351 self.ranges = [] # 'range' properties (for int and hex)
2352 self.help = None # Help text
2353 self.rev_dep = "n" # Reverse (select-related) dependencies
2354 self.weak_rev_dep = "n" # Weak reverse (imply-related) dependencies
2358 self.user_val = None # Value set by user
2360 # The prompt, default value, select, and imply conditions without any
2361 # dependencies from menus and ifs propagated to them
2362 self.orig_prompts = []
2363 self.orig_def_exprs = []
2364 self.orig_selects = []
2365 self.orig_implies = []
2367 # Dependencies inherited from containing menus and ifs
2368 self.deps_from_containing = None
2369 # The set of symbols referenced by this symbol (see
2370 # get_referenced_symbols())
2371 self.referenced_syms = set()
2372 # The set of symbols selected by this symbol (see
2373 # get_selected_symbols())
2374 self.selected_syms = set()
2375 # The set of symbols implied by this symbol (see get_implied_symbols())
2376 self.implied_syms = set()
2377 # Like 'referenced_syms', but includes symbols from
2378 # dependencies inherited from enclosing menus and ifs
2379 self.all_referenced_syms = set()
2381 # This records only dependencies from enclosing ifs and menus together
2382 # with local 'depends on' dependencies. Needed when determining actual
2383 # choice items (hrrrr...). See Choice._determine_actual_symbols().
2384 self.menu_dep = None
2386 # See Symbol.get_ref/def_locations().
2387 self.def_locations = []
2388 self.ref_locations = []
2390 # Populated in Config._build_dep() after parsing. Links the symbol to
2391 # the symbols that immediately depend on it (in a caching/invalidation
2392 # sense). The total set of dependent symbols for the symbol (the
2393 # transitive closure) is calculated on an as-needed basis in
2399 # Caches the calculated value
2400 self.cached_val = None
2401 # Caches the visibility, which acts as an upper bound on the value
2402 self.cached_visibility = None
2403 # Caches the total list of dependent symbols. Calculated in
2405 self.cached_deps = None
2409 # Does the symbol have an entry in the Kconfig file? The trailing
2410 # underscore avoids a collision with is_defined().
2411 self.is_defined_ = False
2412 # Should the symbol get an entry in .config?
2413 self.write_to_conf = False
2414 # Set to true when _make_conf() is called on a symbol, so that symbols
2415 # defined in multiple locations only get one .config entry. We need to
2416 # reset it prior to writing out a new .config.
2417 self.already_written = False
2418 # This is set to True for "actual" choice symbols; see
2419 # Choice._determine_actual_symbols().
2420 self.is_choice_sym = False
2421 # Does the symbol get its value in some special way, e.g. from the
2422 # environment or by being one of the special symbols n, m, and y? If
2423 # so, the value is stored in self.cached_val, which is never
2424 # invalidated. The trailing underscore avoids a collision with
2426 self.is_special_ = False
2427 # Does the symbol get its value from the environment?
2428 self.is_from_env = False
2429 # Does the symbol have the 'allnoconfig_y' option set?
2430 self.allnoconfig_y = False
2432 def _invalidate(self):
2433 if self.is_special_:
2436 if self.is_choice_sym:
2437 self.parent._invalidate()
2439 self.cached_val = None
2440 self.cached_visibility = None
2442 def _invalidate_dependent(self):
2443 for sym in self._get_dependent():
2446 def _set_user_value_no_invalidate(self, v, suppress_load_warnings):
2447 """Like set_user_value(), but does not invalidate any symbols.
2449 suppress_load_warnings: some warnings are annoying when loading a
2450 .config that can be helpful when manually invoking set_user_value().
2451 This flag is set to True to suppress such warnings.
2453 Perhaps this could be made optional for load_config() instead."""
2455 if self.is_special_:
2456 if self.is_from_env:
2457 self.config._warn('attempt to assign the value "{0}" to the '
2458 'symbol {1}, which gets its value from the '
2459 'environment. Assignment ignored.'
2460 .format(v, self.name))
2462 self.config._warn('attempt to assign the value "{0}" to the '
2463 'special symbol {1}. Assignment ignored.'
2464 .format(v, self.name))
2467 if not self.is_defined_:
2468 filename, linenr = self.ref_locations[0]
2469 if self.config.print_undef_assign:
2470 _stderr_msg('note: attempt to assign the value "{0}" to {1}, '
2471 "which is referenced at {2}:{3} but never "
2472 "defined. Assignment ignored."
2473 .format(v, self.name, filename, linenr))
2476 # Check if the value is valid for our type
2477 if not ((self.type == BOOL and (v == "y" or v == "n") ) or
2478 (self.type == TRISTATE and (v == "y" or v == "m" or
2480 (self.type == STRING ) or
2481 (self.type == INT and _is_base_n(v, 10) ) or
2482 (self.type == HEX and _is_base_n(v, 16) )):
2483 self.config._warn('the value "{0}" is invalid for {1}, which has '
2484 "type {2}. Assignment ignored."
2485 .format(v, self.name, TYPENAME[self.type]))
2488 if not self.prompts and not suppress_load_warnings:
2489 self.config._warn('assigning "{0}" to the symbol {1} which '
2490 'lacks prompts and thus has visibility "n". '
2491 'The assignment will have no effect.'
2492 .format(v, self.name))
2496 if self.is_choice_sym and (self.type == BOOL or self.type == TRISTATE):
2497 choice = self.parent
2499 choice.user_val = self
2500 choice.user_mode = "y"
2502 choice.user_val = None
2503 choice.user_mode = "m"
2505 def _unset_user_value_no_recursive_invalidate(self):
2507 self.user_val = None
2509 if self.is_choice_sym:
2510 self.parent._unset_user_value()
2512 def _make_conf(self, append_fn):
2513 if self.already_written:
2516 self.already_written = True
2518 # Note: write_to_conf is determined in get_value()
2519 val = self.get_value()
2520 if not self.write_to_conf:
2523 if self.type == BOOL or self.type == TRISTATE:
2524 append_fn("{0}{1}={2}".format(self.config.config_prefix, self.name, val)
2525 if val == "y" or val == "m" else
2526 "# {0}{1} is not set".format(self.config.config_prefix, self.name))
2528 elif self.type == INT or self.type == HEX:
2529 append_fn("{0}{1}={2}".format(self.config.config_prefix, self.name, val))
2531 elif self.type == STRING:
2533 append_fn('{0}{1}="{2}"'
2534 .format(self.config.config_prefix, self.name,
2535 val.replace("\\", "\\\\").replace('"', '\\"')))
2538 _internal_error("Internal error while creating .config: unknown "
2539 'type "{0}".'.format(self.type))
2541 def _get_dependent(self):
2542 """Returns the set of symbols that should be invalidated if the value
2543 of the symbol changes, because they might be affected by the change.
2544 Note that this is an internal API -- it's probably of limited
2545 usefulness to clients."""
2546 if self.cached_deps is not None:
2547 return self.cached_deps
2551 res |= s._get_dependent()
2553 if self.is_choice_sym:
2554 # Choice symbols also depend (recursively) on their siblings. The
2555 # siblings are not included in 'dep' to avoid dependency loops.
2556 for sibling in self.parent.actual_symbols:
2557 if sibling is not self:
2560 for s in sibling.dep:
2561 res |= s._get_dependent()
2563 self.cached_deps = res
2566 def _has_auto_menu_dep_on(self, on):
2567 """See Choice._determine_actual_symbols()."""
2568 if not isinstance(self.parent, Choice):
2569 _internal_error("Attempt to determine auto menu dependency for "
2570 "symbol ouside of choice.")
2572 if not self.prompts:
2573 # If we have no prompt, use the menu dependencies instead (what was
2574 # specified with 'depends on')
2575 return self.menu_dep is not None and \
2576 self.config._expr_depends_on(self.menu_dep, on)
2578 for _, cond_expr in self.prompts:
2579 if self.config._expr_depends_on(cond_expr, on):
2586 """Represents a menu statement."""
2592 def get_config(self):
2593 """Return the Config instance this menu is from."""
2596 def get_title(self):
2597 """Returns the title text of the menu."""
2600 def get_parent(self):
2601 """Returns the menu or choice statement that contains the menu, or
2602 None if the menu is at the top level. Note that if statements are
2603 treated as syntactic sugar and do not have an explicit class
2607 def get_location(self):
2608 """Returns the location of the menu as a (filename, linenr) tuple,
2609 where filename is a string and linenr an int."""
2610 return (self.filename, self.linenr)
2612 def get_items(self, recursive=False):
2613 """Returns a list containing the items (symbols, menus, choice
2614 statements and comments) in in the menu, in the same order that the
2615 items appear within the menu.
2617 recursive (default: False): True if items contained in items within the
2618 menu should be included recursively (preorder)."""
2624 for item in self.block:
2626 if isinstance(item, Menu):
2627 res.extend(item.get_items(True))
2628 elif isinstance(item, Choice):
2629 res.extend(item.get_items())
2632 def get_symbols(self, recursive=False):
2633 """Returns a list containing the symbols in the menu, in the same order
2634 that they appear within the menu.
2636 recursive (default: False): True if symbols contained in items within
2637 the menu should be included recursively."""
2639 return [item for item in self.get_items(recursive) if
2640 isinstance(item, Symbol)]
2642 def get_visibility(self):
2643 """Returns the visibility of the menu. This also affects the visibility
2644 of subitems. See also Symbol.get_visibility()."""
2645 return self.config._eval_expr(self.dep_expr)
2647 def get_visible_if_visibility(self):
2648 """Returns the visibility the menu gets from its 'visible if'
2649 condition. "y" if the menu has no 'visible if' condition."""
2650 return self.config._eval_expr(self.visible_if_expr)
2652 def get_referenced_symbols(self, refs_from_enclosing=False):
2653 """See Symbol.get_referenced_symbols()."""
2654 return self.all_referenced_syms if refs_from_enclosing else \
2655 self.referenced_syms
2658 """Returns a string containing various information about the menu."""
2659 depends_on_str = self.config._expr_val_str(self.orig_deps,
2660 "(no dependencies)")
2661 visible_if_str = self.config._expr_val_str(self.visible_if_expr,
2662 "(no dependencies)")
2664 additional_deps_str = " " + \
2665 self.config._expr_val_str(self.deps_from_containing,
2666 "(no additional dependencies)")
2668 return _lines("Menu",
2669 "Title : " + self.title,
2670 "'depends on' dependencies : " + depends_on_str,
2671 "'visible if' dependencies : " + visible_if_str,
2672 "Additional dependencies from enclosing menus and "
2674 additional_deps_str,
2675 "Location: {0}:{1}".format(self.filename, self.linenr))
2682 """Menu constructor -- not intended to be called directly by
2683 Kconfiglib clients."""
2686 self.dep_expr = None
2687 self.visible_if_expr = None
2688 self.block = [] # List of contained items
2692 # Dependency expression without dependencies from enclosing menus and
2694 self.orig_deps = None
2696 # Dependencies inherited from containing menus and ifs
2697 self.deps_from_containing = None
2698 # The set of symbols referenced by this menu (see
2699 # get_referenced_symbols())
2700 self.referenced_syms = set()
2701 # Like 'referenced_syms', but includes symbols from
2702 # dependencies inherited from enclosing menus and ifs
2703 self.all_referenced_syms = None
2705 self.filename = None
2708 def _make_conf(self, append_fn):
2709 if self.config._eval_expr(self.dep_expr) != "n" and \
2710 self.config._eval_expr(self.visible_if_expr) != "n":
2711 append_fn("\n#\n# {0}\n#".format(self.title))
2712 _make_block_conf(self.block, append_fn)
2716 """Represents a choice statement. A choice can be in one of three modes:
2718 "n" - The choice is not visible and no symbols can be selected.
2720 "m" - Any number of symbols can be set to "m". The rest will be "n". This
2721 is safe since potentially conflicting options don't actually get
2722 compiled into the kernel simultaneously with "m".
2724 "y" - One symbol will be "y" while the rest are "n".
2726 Only tristate choices can be in "m" mode, and the visibility of the choice
2727 is an upper bound on the mode, so that e.g. a choice that depends on a
2728 symbol with value "m" will be in "m" mode.
2730 The mode changes automatically when a value is assigned to a symbol within
2733 See Symbol.get_visibility() too."""
2739 def get_config(self):
2740 """Returns the Config instance this choice is from."""
2744 """For named choices, returns the name. Returns None for unnamed
2745 choices. No named choices appear anywhere in the kernel Kconfig files
2746 as of Linux 3.7.0-rc8."""
2750 """Returns the type of the choice. See Symbol.get_type()."""
2753 def get_prompts(self):
2754 """Returns a list of prompts defined for the choice, in the order they
2755 appear in the configuration files. Returns the empty list for choices
2758 This list will have a single entry for the vast majority of choices
2759 having prompts, but having multiple prompts for a single choice is
2760 possible through having multiple 'choice' entries for it (though I'm
2761 not sure if that ever happens in practice)."""
2762 return [prompt for prompt, _ in self.orig_prompts]
2765 """Returns the help text of the choice, or None if the choice has no
2769 def get_parent(self):
2770 """Returns the menu or choice statement that contains the choice, or
2771 None if the choice is at the top level. Note that if statements are
2772 treated as syntactic sugar and do not have an explicit class
2776 def get_def_locations(self):
2777 """Returns a list of (filename, linenr) tuples, where filename (string)
2778 and linenr (int) represent a location where the choice is defined. For
2779 the vast majority of choices (all of them as of Linux 3.7.0-rc8) this
2780 list will only contain one element, but its possible for named choices
2781 to be defined in multiple locations."""
2782 return self.def_locations
2784 def get_selection(self):
2785 """Returns the symbol selected (either by the user or through
2786 defaults), or None if either no symbol is selected or the mode is not
2788 if self.cached_selection is not None:
2789 if self.cached_selection == NO_SELECTION:
2791 return self.cached_selection
2793 if self.get_mode() != "y":
2794 return self._cache_ret(None)
2796 # User choice available?
2797 if self.user_val is not None and _get_visibility(self.user_val) == "y":
2798 return self._cache_ret(self.user_val)
2801 return self._cache_ret(None)
2803 return self._cache_ret(self.get_selection_from_defaults())
2805 def get_selection_from_defaults(self):
2806 """Like Choice.get_selection(), but acts as if no symbol has been
2807 selected by the user and no 'optional' flag is in effect."""
2809 if not self.actual_symbols:
2812 for symbol, cond_expr in self.def_exprs:
2813 if self.config._eval_expr(cond_expr) != "n":
2814 chosen_symbol = symbol
2817 chosen_symbol = self.actual_symbols[0]
2819 # Is the chosen symbol visible?
2820 if _get_visibility(chosen_symbol) != "n":
2821 return chosen_symbol
2822 # Otherwise, pick the first visible symbol
2823 for sym in self.actual_symbols:
2824 if _get_visibility(sym) != "n":
2828 def get_user_selection(self):
2829 """If the choice is in "y" mode and has a user-selected symbol, returns
2830 that symbol. Otherwise, returns None."""
2831 return self.user_val
2833 def get_items(self):
2834 """Gets all items contained in the choice in the same order as within
2835 the configuration ("items" instead of "symbols" since choices and
2836 comments might appear within choices. This only happens in one place as
2837 of Linux 3.7.0-rc8, in drivers/usb/gadget/Kconfig)."""
2840 def get_symbols(self):
2841 """Returns a list containing the choice's symbols.
2843 A quirk (perhaps a bug) of Kconfig is that you can put items within a
2844 choice that will not be considered members of the choice insofar as
2845 selection is concerned. This happens for example if one symbol within a
2846 choice 'depends on' the symbol preceding it, or if you put non-symbol
2847 items within choices.
2849 As of Linux 3.7.0-rc8, this seems to be used intentionally in one
2850 place: drivers/usb/gadget/Kconfig.
2852 This function returns the "proper" symbols of the choice in the order
2853 they appear in the choice, excluding such items. If you want all items
2854 in the choice, use get_items()."""
2855 return self.actual_symbols
2857 def get_referenced_symbols(self, refs_from_enclosing=False):
2858 """See Symbol.get_referenced_symbols()."""
2859 return self.all_referenced_syms if refs_from_enclosing else \
2860 self.referenced_syms
2862 def get_visibility(self):
2863 """Returns the visibility of the choice statement: one of "n", "m" or
2864 "y". This acts as an upper limit on the mode of the choice (though bool
2865 choices can only have the mode "y"). See the class documentation for an
2866 explanation of modes."""
2867 return _get_visibility(self)
2870 """Returns the mode of the choice. See the class documentation for
2871 an explanation of modes."""
2872 minimum_mode = "n" if self.optional else "m"
2873 mode = self.user_mode if self.user_mode is not None else minimum_mode
2874 mode = self.config._eval_min(mode, _get_visibility(self))
2876 # Promote "m" to "y" for boolean choices
2877 if mode == "m" and self.type == BOOL:
2882 def is_optional(self):
2883 """Returns True if the choice has the 'optional' flag set (and so will
2884 default to "n" mode)."""
2885 return self.optional
2888 """Returns a string containing various information about the choice
2890 return self.config._get_sym_or_choice_str(self)
2897 """Choice constructor -- not intended to be called directly by
2898 Kconfiglib clients."""
2900 self.name = None # Yes, choices can be named
2903 self.def_exprs = [] # 'default' properties
2904 self.help = None # Help text
2905 self.block = [] # List of contained items
2909 self.user_val = None
2910 self.user_mode = None
2912 # We need to filter out symbols that appear within the choice block but
2913 # are not considered choice items (see
2914 # Choice._determine_actual_symbols()) This list holds the "actual"
2916 self.actual_symbols = []
2918 # The prompts and default values without any dependencies from
2919 # enclosing menus and ifs propagated
2920 self.orig_prompts = []
2921 self.orig_def_exprs = []
2923 # Dependencies inherited from containing menus and ifs
2924 self.deps_from_containing = None
2925 # The set of symbols referenced by this choice (see
2926 # get_referenced_symbols())
2927 self.referenced_syms = set()
2928 # Like 'referenced_syms', but includes symbols from
2929 # dependencies inherited from enclosing menus and ifs
2930 self.all_referenced_syms = set()
2932 # See Choice.get_def_locations()
2933 self.def_locations = []
2936 self.cached_selection = None
2937 self.cached_visibility = None
2939 self.optional = False
2941 def _determine_actual_symbols(self):
2942 """If a symbol's visibility depends on the preceding symbol within a
2943 choice, it is no longer viewed as a choice item. (This is quite
2944 possibly a bug, but some things consciously use it... ugh. It stems
2945 from automatic submenu creation.) In addition, it's possible to have
2946 choices and comments within choices, and those shouldn't be considered
2947 choice items either. Only drivers/usb/gadget/Kconfig seems to depend on
2948 any of this. This method computes the "actual" items in the choice and
2949 sets the is_choice_sym flag on them (retrieved via is_choice_symbol()).
2951 Don't let this scare you: an earlier version simply checked for a
2952 sequence of symbols where all symbols after the first appeared in the
2953 'depends on' expression of the first, and that worked fine. The added
2954 complexity is to be future-proof in the event that
2955 drivers/usb/gadget/Kconfig turns even more sinister. It might very well
2956 be overkilling things (especially if that file is refactored ;)."""
2958 # Items might depend on each other in a tree structure, so we need a
2959 # stack to keep track of the current tentative parent
2962 for item in self.block:
2963 if not isinstance(item, Symbol):
2968 if item._has_auto_menu_dep_on(stack[-1]):
2969 # The item should not be viewed as a choice item, so don't
2970 # set item.is_choice_sym
2976 item.is_choice_sym = True
2977 self.actual_symbols.append(item)
2980 def _cache_ret(self, selection):
2981 # As None is used to indicate the lack of a cached value we can't use
2982 # that to cache the fact that the choice has no selection. Instead, we
2983 # use the symbolic constant NO_SELECTION.
2984 if selection is None:
2985 self.cached_selection = NO_SELECTION
2987 self.cached_selection = selection
2991 def _invalidate(self):
2992 self.cached_selection = None
2993 self.cached_visibility = None
2995 def _unset_user_value(self):
2997 self.user_val = None
2998 self.user_mode = None
3000 def _make_conf(self, append_fn):
3001 _make_block_conf(self.block, append_fn)
3003 class Comment(Item):
3005 """Represents a comment statement."""
3011 def get_config(self):
3012 """Returns the Config instance this comment is from."""
3016 """Returns the text of the comment."""
3019 def get_parent(self):
3020 """Returns the menu or choice statement that contains the comment, or
3021 None if the comment is at the top level. Note that if statements are
3022 treated as syntactic sugar and do not have an explicit class
3026 def get_location(self):
3027 """Returns the location of the comment as a (filename, linenr) tuple,
3028 where filename is a string and linenr an int."""
3029 return (self.filename, self.linenr)
3031 def get_visibility(self):
3032 """Returns the visibility of the comment. See also
3033 Symbol.get_visibility()."""
3034 return self.config._eval_expr(self.dep_expr)
3036 def get_referenced_symbols(self, refs_from_enclosing=False):
3037 """See Symbol.get_referenced_symbols()."""
3038 return self.all_referenced_syms if refs_from_enclosing else \
3039 self.referenced_syms
3042 """Returns a string containing various information about the
3044 dep_str = self.config._expr_val_str(self.orig_deps,
3045 "(no dependencies)")
3047 additional_deps_str = " " + \
3048 self.config._expr_val_str(self.deps_from_containing,
3049 "(no additional dependencies)")
3051 return _lines("Comment",
3052 "Text: " + str(self.text),
3053 "Dependencies: " + dep_str,
3054 "Additional dependencies from enclosing menus and "
3056 additional_deps_str,
3057 "Location: {0}:{1}".format(self.filename, self.linenr))
3064 """Comment constructor -- not intended to be called directly by
3065 Kconfiglib clients."""
3068 self.dep_expr = None
3072 # Dependency expression without dependencies from enclosing menus and
3074 self.orig_deps = None
3076 # Dependencies inherited from containing menus and ifs
3077 self.deps_from_containing = None
3078 # The set of symbols referenced by this comment (see
3079 # get_referenced_symbols())
3080 self.referenced_syms = set()
3081 # Like 'referenced_syms', but includes symbols from
3082 # dependencies inherited from enclosing menus and ifs
3083 self.all_referenced_syms = None
3085 self.filename = None
3088 def _make_conf(self, append_fn):
3089 if self.config._eval_expr(self.dep_expr) != "n":
3090 append_fn("\n#\n# {0}\n#".format(self.text))
3092 class Kconfig_Syntax_Error(Exception):
3093 """Exception raised for syntax errors."""
3096 class Internal_Error(Exception):
3097 """Exception raised for internal errors."""
3104 def tri_less(v1, v2):
3105 """Returns True if the tristate v1 is less than the tristate v2, where "n",
3106 "m" and "y" are ordered from lowest to highest."""
3107 return TRI_TO_INT[v1] < TRI_TO_INT[v2]
3109 def tri_less_eq(v1, v2):
3110 """Returns True if the tristate v1 is less than or equal to the tristate
3111 v2, where "n", "m" and "y" are ordered from lowest to highest."""
3112 return TRI_TO_INT[v1] <= TRI_TO_INT[v2]
3114 def tri_greater(v1, v2):
3115 """Returns True if the tristate v1 is greater than the tristate v2, where
3116 "n", "m" and "y" are ordered from lowest to highest."""
3117 return TRI_TO_INT[v1] > TRI_TO_INT[v2]
3119 def tri_greater_eq(v1, v2):
3120 """Returns True if the tristate v1 is greater than or equal to the tristate
3121 v2, where "n", "m" and "y" are ordered from lowest to highest."""
3122 return TRI_TO_INT[v1] >= TRI_TO_INT[v2]
3128 class _Feed(object):
3130 """Class for working with sequences in a stream-like fashion; handy for
3133 # This would be more helpful on the item classes, but would remove some
3135 __slots__ = ['items', 'length', 'i']
3137 def __init__(self, items):
3139 self.length = len(self.items)
3143 if self.i >= self.length:
3145 item = self.items[self.i]
3149 def peek_next(self):
3150 return None if self.i >= self.length else self.items[self.i]
3152 def check(self, token):
3153 """Check if the next token is 'token'. If so, remove it from the token
3154 feed and return True. Otherwise, leave it in and return False."""
3155 if self.i < self.length and self.items[self.i] == token:
3160 def unget_all(self):
3163 class _FileFeed(object):
3165 """Feeds lines from a file. Keeps track of the filename and current line
3166 number. Joins any line ending in \\ with the following line. We need to be
3167 careful to get the line number right in the presence of continuation
3170 __slots__ = ['filename', 'lines', 'length', 'linenr']
3172 def __init__(self, filename):
3173 self.filename = _clean_up_path(filename)
3174 with open(filename, "r") as f:
3175 # No interleaving of I/O and processing yet. Don't know if it would
3177 self.lines = f.readlines()
3178 self.length = len(self.lines)
3182 if self.linenr >= self.length:
3184 line = self.lines[self.linenr]
3186 while line.endswith("\\\n"):
3187 line = line[:-2] + self.lines[self.linenr]
3191 def peek_next(self):
3192 linenr = self.linenr
3193 if linenr >= self.length:
3195 line = self.lines[linenr]
3196 while line.endswith("\\\n"):
3198 line = line[:-2] + self.lines[linenr]
3203 while self.lines[self.linenr].endswith("\\\n"):
3206 def next_nonblank(self):
3207 """Removes lines up to and including the next non-blank (not all-space)
3208 line and returns it. Returns None if there are no more non-blank
3211 line = self.get_next()
3212 if line is None or not line.isspace():
3216 # Internal functions
3219 def _get_visibility(sc):
3220 """Symbols and Choices have a "visibility" that acts as an upper bound on
3221 the values a user can set for them, corresponding to the visibility in e.g.
3222 'make menuconfig'. This function calculates the visibility for the Symbol
3223 or Choice 'sc' -- the logic is nearly identical."""
3224 if sc.cached_visibility is None:
3226 for _, cond_expr in sc.prompts:
3227 vis = sc.config._eval_max(vis, cond_expr)
3229 if isinstance(sc, Symbol) and sc.is_choice_sym:
3230 if sc.type == TRISTATE and vis == "m" and \
3231 sc.parent.get_mode() == "y":
3232 # Choice symbols with visibility "m" are not visible if the
3233 # choice has mode "y"
3236 vis = sc.config._eval_min(vis, _get_visibility(sc.parent))
3238 # Promote "m" to "y" if we're dealing with a non-tristate
3239 if vis == "m" and sc.type != TRISTATE:
3242 sc.cached_visibility = vis
3244 return sc.cached_visibility
3246 def _make_and(e1, e2):
3247 """Constructs an AND (&&) expression. Performs trivial simplification.
3248 Nones equate to 'y'.
3250 Note: returns None if e1 == e2 == None."""
3251 if e1 is None or e1 == "y":
3253 if e2 is None or e2 == "y":
3256 # Prefer to merge argument lists if possible to reduce the number of nodes
3258 if isinstance(e1, tuple) and e1[0] == AND:
3259 if isinstance(e2, tuple) and e2[0] == AND:
3260 return (AND, e1[1] + e2[1])
3261 return (AND, e1[1] + [e2])
3263 if isinstance(e2, tuple) and e2[0] == AND:
3264 return (AND, e2[1] + [e1])
3266 return (AND, [e1, e2])
3268 def _make_or(e1, e2):
3269 """Constructs an OR (||) expression. Performs trivial simplification and
3270 avoids Nones. Nones equate to 'y', which is usually what we want, but needs
3271 to be kept in mind."""
3273 # Perform trivial simplification and avoid None's (which
3274 # correspond to y's)
3275 if e1 is None or e2 is None or e1 == "y" or e2 == "y":
3280 # Prefer to merge argument lists if possible to reduce the number of nodes
3282 if isinstance(e1, tuple) and e1[0] == OR:
3283 if isinstance(e2, tuple) and e2[0] == OR:
3284 return (OR, e1[1] + e2[1])
3285 return (OR, e1[1] + [e2])
3287 if isinstance(e2, tuple) and e2[0] == OR:
3288 return (OR, e2[1] + [e1])
3290 return (OR, [e1, e2])
3292 def _get_expr_syms_rec(expr, res):
3293 """_get_expr_syms() helper. Recurses through expressions."""
3294 if isinstance(expr, Symbol):
3296 elif isinstance(expr, str):
3298 elif expr[0] == AND or expr[0] == OR:
3299 for term in expr[1]:
3300 _get_expr_syms_rec(term, res)
3301 elif expr[0] == NOT:
3302 _get_expr_syms_rec(expr[1], res)
3303 elif expr[0] == EQUAL or expr[0] == UNEQUAL:
3304 if isinstance(expr[1], Symbol):
3306 if isinstance(expr[2], Symbol):
3309 _internal_error("Internal error while fetching symbols from an "
3310 "expression with token stream {0}.".format(expr))
3312 def _get_expr_syms(expr):
3313 """Returns the set() of symbols appearing in expr."""
3315 if expr is not None:
3316 _get_expr_syms_rec(expr, res)
3320 """Returns the value of obj as a string. If obj is not a string (constant
3321 symbol), it must be a Symbol."""
3322 return obj if isinstance(obj, str) else obj.get_value()
3324 def _make_block_conf(block, append_fn):
3325 """Returns a list of .config strings for a block (list) of items."""
3327 # Collect the substrings in a list and later use join() instead of += to
3328 # build the final .config contents. With older Python versions, this yields
3329 # linear instead of quadratic complexity.
3331 item._make_conf(append_fn)
3333 def _sym_str_string(sym_or_str):
3334 if isinstance(sym_or_str, str):
3335 return '"' + sym_or_str + '"'
3336 return sym_or_str.name
3338 def _intersperse(lst, op):
3339 """_expr_to_str() helper. Gets the string representation of each expression
3340 in lst and produces a list where op has been inserted between the
3347 def handle_sub_expr(expr):
3348 no_parens = isinstance(expr, (str, Symbol)) or \
3349 expr[0] in (EQUAL, UNEQUAL) or \
3350 PRECEDENCE[op] <= PRECEDENCE[expr[0]]
3353 res.extend(_expr_to_str_rec(expr))
3357 op_str = OP_TO_STR[op]
3359 handle_sub_expr(lst[0])
3360 for expr in lst[1:]:
3362 handle_sub_expr(expr)
3366 def _expr_to_str_rec(expr):
3370 if isinstance(expr, (Symbol, str)):
3371 return [_sym_str_string(expr)]
3373 if expr[0] in (AND, OR):
3374 return _intersperse(expr[1], expr[0])
3377 need_parens = not isinstance(expr[1], (str, Symbol))
3382 res.extend(_expr_to_str_rec(expr[1]))
3387 if expr[0] in (EQUAL, UNEQUAL):
3388 return [_sym_str_string(expr[1]),
3390 _sym_str_string(expr[2])]
3392 def _expr_to_str(expr):
3393 return "".join(_expr_to_str_rec(expr))
3395 def _indentation(line):
3396 """Returns the length of the line's leading whitespace, treating tab stops
3397 as being spaced 8 characters apart."""
3398 line = line.expandtabs()
3399 return len(line) - len(line.lstrip())
3401 def _deindent(line, indent):
3402 """Deindent 'line' by 'indent' spaces."""
3403 line = line.expandtabs()
3404 if len(line) <= indent:
3406 return line[indent:]
3408 def _is_base_n(s, n):
3416 """Returns a string consisting of all arguments, with newlines inserted
3418 return "\n".join(args)
3421 """Returns a new string with "#" inserted before each line in 's'."""
3424 res = "".join(["#" + line for line in s.splitlines(True)])
3425 if s.endswith("\n"):
3429 def _clean_up_path(path):
3430 """Strips an initial "./" and any trailing slashes from 'path'."""
3431 if path.startswith("./"):
3433 return path.rstrip("/")
3435 def _build_msg(msg, filename, linenr):
3436 if filename is not None:
3437 msg = "{0}:{1}: ".format(_clean_up_path(filename), linenr) + msg
3440 def _stderr_msg(msg, filename, linenr):
3441 sys.stderr.write(_build_msg(msg, filename, linenr) + "\n")
3443 def _tokenization_error(s, filename, linenr):
3444 loc = "" if filename is None else "{0}:{1}: ".format(filename, linenr)
3445 raise Kconfig_Syntax_Error("{0}Couldn't tokenize '{1}'"
3446 .format(loc, s.strip()))
3448 def _parse_error(s, msg, filename, linenr):
3449 loc = "" if filename is None else "{0}:{1}: ".format(filename, linenr)
3450 raise Kconfig_Syntax_Error("{0}Couldn't parse '{1}'{2}"
3451 .format(loc, s.strip(),
3452 "." if msg is None else ": " + msg))
3454 def _internal_error(msg):
3455 raise Internal_Error(msg +
3456 "\nSorry! You may want to send an email to ulfalizer a.t Google's "
3457 "email service to tell me about this. Include the message above and the "
3458 "stack trace and describe what you were doing.")
3461 # Internal global constants
3465 (T_AND, T_OR, T_NOT,
3466 T_OPEN_PAREN, T_CLOSE_PAREN,
3468 T_MAINMENU, T_MENU, T_ENDMENU,
3469 T_SOURCE, T_CHOICE, T_ENDCHOICE,
3470 T_COMMENT, T_CONFIG, T_MENUCONFIG,
3471 T_HELP, T_IF, T_ENDIF, T_DEPENDS, T_ON,
3472 T_OPTIONAL, T_PROMPT, T_DEFAULT,
3473 T_BOOL, T_TRISTATE, T_HEX, T_INT, T_STRING,
3474 T_DEF_BOOL, T_DEF_TRISTATE,
3475 T_SELECT, T_IMPLY, T_RANGE, T_OPTION, T_ALLNOCONFIG_Y, T_ENV,
3476 T_DEFCONFIG_LIST, T_MODULES, T_VISIBLE) = range(40)
3478 # The leading underscore before the function assignments below prevent pydoc
3479 # from listing them. The constants could be hidden too, but they're fairly
3480 # obviously internal anyway, so don't bother spamming the code.
3482 # Keyword to token map. Note that the get() method is assigned directly as a
3483 # small optimization.
3485 {"mainmenu": T_MAINMENU, "menu": T_MENU, "endmenu": T_ENDMENU,
3486 "endif": T_ENDIF, "endchoice": T_ENDCHOICE, "source": T_SOURCE,
3487 "choice": T_CHOICE, "config": T_CONFIG, "comment": T_COMMENT,
3488 "menuconfig": T_MENUCONFIG, "help": T_HELP, "if": T_IF,
3489 "depends": T_DEPENDS, "on": T_ON, "optional": T_OPTIONAL,
3490 "prompt": T_PROMPT, "default": T_DEFAULT, "bool": T_BOOL, "boolean": T_BOOL,
3491 "tristate": T_TRISTATE, "int": T_INT, "hex": T_HEX, "def_bool": T_DEF_BOOL,
3492 "def_tristate": T_DEF_TRISTATE, "string": T_STRING, "select": T_SELECT,
3493 "imply" : T_IMPLY, "range": T_RANGE, "option": T_OPTION,
3494 "allnoconfig_y": T_ALLNOCONFIG_Y, "env": T_ENV,
3495 "defconfig_list": T_DEFCONFIG_LIST, "modules": T_MODULES,
3496 "visible": T_VISIBLE}.get
3498 # Strings to use for True and False
3499 BOOL_STR = {False: "false", True: "true"}
3501 # Tokens after which identifier-like lexemes are treated as strings. T_CHOICE
3502 # is included to avoid symbols being registered for named choices.
3503 STRING_LEX = frozenset((T_BOOL, T_TRISTATE, T_INT, T_HEX, T_STRING, T_CHOICE,
3504 T_PROMPT, T_MENU, T_COMMENT, T_SOURCE, T_MAINMENU))
3506 # Matches the initial token on a line; see _tokenize(). Also eats trailing
3507 # whitespace as an optimization.
3508 _initial_token_re_match = re.compile(r"[^\w]*(\w+)\s*").match
3510 # Matches an identifier/keyword optionally preceded by whitespace. Also eats
3511 # trailing whitespace as an optimization.
3512 _id_keyword_re_match = re.compile(r"\s*([\w./-]+)\s*").match
3514 # Regular expression for finding $-references to symbols in strings
3515 _sym_ref_re_search = re.compile(r"\$[A-Za-z0-9_]+").search
3517 # Integers representing symbol types
3518 UNKNOWN, BOOL, TRISTATE, STRING, HEX, INT = range(6)
3520 # Strings to use for types
3521 TYPENAME = {UNKNOWN: "unknown", BOOL: "bool", TRISTATE: "tristate",
3522 STRING: "string", HEX: "hex", INT: "int"}
3524 # Token to type mapping
3525 TOKEN_TO_TYPE = {T_BOOL: BOOL, T_TRISTATE: TRISTATE, T_STRING: STRING,
3526 T_INT: INT, T_HEX: HEX}
3528 # Default values for symbols of different types (the value the symbol gets if
3529 # it is not assigned a user value and none of its 'default' clauses kick in)
3530 DEFAULT_VALUE = {BOOL: "n", TRISTATE: "n", STRING: "", INT: "", HEX: ""}
3532 # Indicates that no item is selected in a choice statement
3535 # Integers representing expression types
3536 AND, OR, NOT, EQUAL, UNEQUAL = range(5)
3538 # Map from tristate values to integers
3539 TRI_TO_INT = {"n": 0, "m": 1, "y": 2}
3541 # Printing-related stuff
3543 OP_TO_STR = {AND: " && ", OR: " || ", EQUAL: " = ", UNEQUAL: " != "}
3544 PRECEDENCE = {OR: 0, AND: 1, NOT: 2}