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