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