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