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 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 557 | VERSION = (14, 1, 0) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 558 | |
Simon Glass | ac05335 | 2022-02-11 13:23:19 -0700 | [diff] [blame] | 559 | # pylint: disable=E1101 |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 560 | |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 561 | # File layout: |
| 562 | # |
| 563 | # Public classes |
| 564 | # Public functions |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 565 | # Internal functions |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 566 | # Global constants |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 567 | |
| 568 | # Line length: 79 columns |
| 569 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 570 | |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 571 | # |
| 572 | # Public classes |
| 573 | # |
| 574 | |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 575 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 576 | class Kconfig(object): |
| 577 | """ |
| 578 | Represents a Kconfig configuration, e.g. for x86 or ARM. This is the set of |
| 579 | symbols, choices, and menu nodes appearing in the configuration. Creating |
| 580 | any number of Kconfig objects (including for different architectures) is |
| 581 | safe. Kconfiglib doesn't keep any global state. |
| 582 | |
| 583 | The following attributes are available. They should be treated as |
| 584 | read-only, and some are implemented through @property magic. |
| 585 | |
| 586 | syms: |
| 587 | A dictionary with all symbols in the configuration, indexed by name. Also |
| 588 | includes all symbols that are referenced in expressions but never |
| 589 | defined, except for constant (quoted) symbols. |
| 590 | |
| 591 | Undefined symbols can be recognized by Symbol.nodes being empty -- see |
| 592 | the 'Intro to the menu tree' section in the module docstring. |
| 593 | |
| 594 | const_syms: |
| 595 | A dictionary like 'syms' for constant (quoted) symbols |
| 596 | |
| 597 | named_choices: |
| 598 | A dictionary like 'syms' for named choices (choice FOO) |
| 599 | |
| 600 | defined_syms: |
| 601 | A list with all defined symbols, in the same order as they appear in the |
| 602 | Kconfig files. Symbols defined in multiple locations appear multiple |
| 603 | times. |
| 604 | |
| 605 | Note: You probably want to use 'unique_defined_syms' instead. This |
| 606 | attribute is mostly maintained for backwards compatibility. |
| 607 | |
| 608 | unique_defined_syms: |
| 609 | A list like 'defined_syms', but with duplicates removed. Just the first |
| 610 | instance is kept for symbols defined in multiple locations. Kconfig order |
| 611 | is preserved otherwise. |
| 612 | |
| 613 | Using this attribute instead of 'defined_syms' can save work, and |
| 614 | automatically gives reasonable behavior when writing configuration output |
| 615 | (symbols defined in multiple locations only generate output once, while |
| 616 | still preserving Kconfig order for readability). |
| 617 | |
| 618 | choices: |
| 619 | A list with all choices, in the same order as they appear in the Kconfig |
| 620 | files. |
| 621 | |
| 622 | Note: You probably want to use 'unique_choices' instead. This attribute |
| 623 | is mostly maintained for backwards compatibility. |
| 624 | |
| 625 | unique_choices: |
| 626 | Analogous to 'unique_defined_syms', for choices. Named choices can have |
| 627 | multiple definition locations. |
| 628 | |
| 629 | menus: |
| 630 | A list with all menus, in the same order as they appear in the Kconfig |
| 631 | files |
| 632 | |
| 633 | comments: |
| 634 | A list with all comments, in the same order as they appear in the Kconfig |
| 635 | files |
| 636 | |
| 637 | kconfig_filenames: |
| 638 | A list with the filenames of all Kconfig files included in the |
| 639 | configuration, relative to $srctree (or relative to the current directory |
| 640 | if $srctree isn't set), except absolute paths (e.g. |
| 641 | 'source "/foo/Kconfig"') are kept as-is. |
| 642 | |
| 643 | The files are listed in the order they are source'd, starting with the |
| 644 | top-level Kconfig file. If a file is source'd multiple times, it will |
| 645 | appear multiple times. Use set() to get unique filenames. |
| 646 | |
| 647 | Note that Kconfig.sync_deps() already indirectly catches any file |
| 648 | modifications that change configuration output. |
| 649 | |
| 650 | env_vars: |
| 651 | A set() with the names of all environment variables referenced in the |
| 652 | Kconfig files. |
| 653 | |
| 654 | Only environment variables referenced with the preprocessor $(FOO) syntax |
| 655 | will be registered. The older $FOO syntax is only supported for backwards |
| 656 | compatibility. |
| 657 | |
| 658 | Also note that $(FOO) won't be registered unless the environment variable |
| 659 | $FOO is actually set. If it isn't, $(FOO) is an expansion of an unset |
| 660 | preprocessor variable (which gives the empty string). |
| 661 | |
| 662 | Another gotcha is that environment variables referenced in the values of |
| 663 | recursively expanded preprocessor variables (those defined with =) will |
| 664 | only be registered if the variable is actually used (expanded) somewhere. |
| 665 | |
| 666 | The note from the 'kconfig_filenames' documentation applies here too. |
| 667 | |
| 668 | n/m/y: |
| 669 | The predefined constant symbols n/m/y. Also available in const_syms. |
| 670 | |
| 671 | modules: |
| 672 | The Symbol instance for the modules symbol. Currently hardcoded to |
| 673 | MODULES, which is backwards compatible. Kconfiglib will warn if |
| 674 | 'option modules' is set on some other symbol. Tell me if you need proper |
| 675 | 'option modules' support. |
| 676 | |
| 677 | 'modules' is never None. If the MODULES symbol is not explicitly defined, |
| 678 | its tri_value will be 0 (n), as expected. |
| 679 | |
| 680 | A simple way to enable modules is to do 'kconf.modules.set_value(2)' |
| 681 | (provided the MODULES symbol is defined and visible). Modules are |
| 682 | disabled by default in the kernel Kconfig files as of writing, though |
| 683 | nearly all defconfig files enable them (with 'CONFIG_MODULES=y'). |
| 684 | |
| 685 | defconfig_list: |
| 686 | The Symbol instance for the 'option defconfig_list' symbol, or None if no |
| 687 | defconfig_list symbol exists. The defconfig filename derived from this |
| 688 | symbol can be found in Kconfig.defconfig_filename. |
| 689 | |
| 690 | defconfig_filename: |
| 691 | The filename given by the defconfig_list symbol. This is taken from the |
| 692 | first 'default' with a satisfied condition where the specified file |
| 693 | exists (can be opened for reading). If a defconfig file foo/defconfig is |
| 694 | not found and $srctree was set when the Kconfig was created, |
| 695 | $srctree/foo/defconfig is looked up as well. |
| 696 | |
| 697 | 'defconfig_filename' is None if either no defconfig_list symbol exists, |
| 698 | or if the defconfig_list symbol has no 'default' with a satisfied |
| 699 | condition that specifies a file that exists. |
| 700 | |
| 701 | Gotcha: scripts/kconfig/Makefile might pass --defconfig=<defconfig> to |
| 702 | scripts/kconfig/conf when running e.g. 'make defconfig'. This option |
| 703 | overrides the defconfig_list symbol, meaning defconfig_filename might not |
| 704 | always match what 'make defconfig' would use. |
| 705 | |
| 706 | top_node: |
| 707 | The menu node (see the MenuNode class) of the implicit top-level menu. |
| 708 | Acts as the root of the menu tree. |
| 709 | |
| 710 | mainmenu_text: |
| 711 | The prompt (title) of the top menu (top_node). Defaults to "Main menu". |
| 712 | Can be changed with the 'mainmenu' statement (see kconfig-language.txt). |
| 713 | |
| 714 | variables: |
| 715 | A dictionary with all preprocessor variables, indexed by name. See the |
| 716 | Variable class. |
| 717 | |
| 718 | warn: |
| 719 | Set this variable to True/False to enable/disable warnings. See |
| 720 | Kconfig.__init__(). |
| 721 | |
| 722 | When 'warn' is False, the values of the other warning-related variables |
| 723 | are ignored. |
| 724 | |
| 725 | This variable as well as the other warn* variables can be read to check |
| 726 | the current warning settings. |
| 727 | |
| 728 | warn_to_stderr: |
| 729 | Set this variable to True/False to enable/disable warnings on stderr. See |
| 730 | Kconfig.__init__(). |
| 731 | |
| 732 | warn_assign_undef: |
| 733 | Set this variable to True to generate warnings for assignments to |
| 734 | undefined symbols in configuration files. |
| 735 | |
| 736 | This variable is False by default unless the KCONFIG_WARN_UNDEF_ASSIGN |
| 737 | environment variable was set to 'y' when the Kconfig instance was |
| 738 | created. |
| 739 | |
| 740 | warn_assign_override: |
| 741 | Set this variable to True to generate warnings for multiple assignments |
| 742 | to the same symbol in configuration files, where the assignments set |
| 743 | different values (e.g. CONFIG_FOO=m followed by CONFIG_FOO=y, where the |
| 744 | last value would get used). |
| 745 | |
| 746 | This variable is True by default. Disabling it might be useful when |
| 747 | merging configurations. |
| 748 | |
| 749 | warn_assign_redun: |
| 750 | Like warn_assign_override, but for multiple assignments setting a symbol |
| 751 | to the same value. |
| 752 | |
| 753 | This variable is True by default. Disabling it might be useful when |
| 754 | merging configurations. |
| 755 | |
| 756 | warnings: |
| 757 | A list of strings containing all warnings that have been generated, for |
| 758 | cases where more flexibility is needed. |
| 759 | |
| 760 | See the 'warn_to_stderr' parameter to Kconfig.__init__() and the |
| 761 | Kconfig.warn_to_stderr variable as well. Note that warnings still get |
| 762 | added to Kconfig.warnings when 'warn_to_stderr' is True. |
| 763 | |
| 764 | Just as for warnings printed to stderr, only warnings that are enabled |
| 765 | will get added to Kconfig.warnings. See the various Kconfig.warn* |
| 766 | variables. |
| 767 | |
| 768 | missing_syms: |
| 769 | A list with (name, value) tuples for all assignments to undefined symbols |
| 770 | within the most recently loaded .config file(s). 'name' is the symbol |
| 771 | name without the 'CONFIG_' prefix. 'value' is a string that gives the |
| 772 | right-hand side of the assignment verbatim. |
| 773 | |
| 774 | See Kconfig.load_config() as well. |
| 775 | |
| 776 | srctree: |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 777 | The value the $srctree environment variable had when the Kconfig instance |
| 778 | was created, or the empty string if $srctree wasn't set. This gives nice |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 779 | behavior with os.path.join(), which treats "" as the current directory, |
| 780 | without adding "./". |
| 781 | |
| 782 | Kconfig files are looked up relative to $srctree (unless absolute paths |
| 783 | are used), and .config files are looked up relative to $srctree if they |
| 784 | are not found in the current directory. This is used to support |
| 785 | out-of-tree builds. The C tools use this environment variable in the same |
| 786 | way. |
| 787 | |
| 788 | Changing $srctree after creating the Kconfig instance has no effect. Only |
| 789 | the value when the configuration is loaded matters. This avoids surprises |
| 790 | if multiple configurations are loaded with different values for $srctree. |
| 791 | |
| 792 | config_prefix: |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 793 | The value the CONFIG_ environment variable had when the Kconfig instance |
| 794 | was created, or "CONFIG_" if CONFIG_ wasn't set. This is the prefix used |
| 795 | (and expected) on symbol names in .config files and C headers. Used in |
| 796 | the same way in the C tools. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 797 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 798 | config_header: |
| 799 | The value the KCONFIG_CONFIG_HEADER environment variable had when the |
| 800 | Kconfig instance was created, or the empty string if |
| 801 | KCONFIG_CONFIG_HEADER wasn't set. This string is inserted verbatim at the |
| 802 | beginning of configuration files. See write_config(). |
| 803 | |
| 804 | header_header: |
| 805 | The value the KCONFIG_AUTOHEADER_HEADER environment variable had when the |
| 806 | Kconfig instance was created, or the empty string if |
| 807 | KCONFIG_AUTOHEADER_HEADER wasn't set. This string is inserted verbatim at |
| 808 | the beginning of header files. See write_autoconf(). |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 809 | |
| 810 | filename/linenr: |
| 811 | The current parsing location, for use in Python preprocessor functions. |
| 812 | See the module docstring. |
| 813 | """ |
| 814 | __slots__ = ( |
| 815 | "_encoding", |
| 816 | "_functions", |
| 817 | "_set_match", |
| 818 | "_srctree_prefix", |
| 819 | "_unset_match", |
| 820 | "_warn_assign_no_prompt", |
| 821 | "choices", |
| 822 | "comments", |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 823 | "config_header", |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 824 | "config_prefix", |
| 825 | "const_syms", |
| 826 | "defconfig_list", |
| 827 | "defined_syms", |
| 828 | "env_vars", |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 829 | "header_header", |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 830 | "kconfig_filenames", |
| 831 | "m", |
| 832 | "menus", |
| 833 | "missing_syms", |
| 834 | "modules", |
| 835 | "n", |
| 836 | "named_choices", |
| 837 | "srctree", |
| 838 | "syms", |
| 839 | "top_node", |
| 840 | "unique_choices", |
| 841 | "unique_defined_syms", |
| 842 | "variables", |
| 843 | "warn", |
| 844 | "warn_assign_override", |
| 845 | "warn_assign_redun", |
| 846 | "warn_assign_undef", |
| 847 | "warn_to_stderr", |
| 848 | "warnings", |
| 849 | "y", |
| 850 | |
| 851 | # Parsing-related |
| 852 | "_parsing_kconfigs", |
| 853 | "_readline", |
| 854 | "filename", |
| 855 | "linenr", |
| 856 | "_include_path", |
| 857 | "_filestack", |
| 858 | "_line", |
| 859 | "_tokens", |
| 860 | "_tokens_i", |
| 861 | "_reuse_tokens", |
| 862 | ) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 863 | |
| 864 | # |
| 865 | # Public interface |
| 866 | # |
| 867 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 868 | def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True, |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 869 | encoding="utf-8", suppress_traceback=False): |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 870 | """ |
| 871 | Creates a new Kconfig object by parsing Kconfig files. |
| 872 | Note that Kconfig files are not the same as .config files (which store |
| 873 | configuration symbol values). |
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 | See the module docstring for some environment variables that influence |
| 876 | default warning settings (KCONFIG_WARN_UNDEF and |
| 877 | KCONFIG_WARN_UNDEF_ASSIGN). |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 878 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 879 | Raises KconfigError on syntax/semantic errors, and OSError or (possibly |
| 880 | a subclass of) IOError on IO errors ('errno', 'strerror', and |
| 881 | 'filename' are available). Note that IOError is an alias for OSError on |
| 882 | Python 3, so it's enough to catch OSError there. If you need Python 2/3 |
| 883 | compatibility, it's easiest to catch EnvironmentError, which is a |
| 884 | common base class of OSError/IOError on Python 2 and an alias for |
| 885 | OSError on Python 3. |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 886 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 887 | filename (default: "Kconfig"): |
| 888 | The Kconfig file to load. For the Linux kernel, you'll want "Kconfig" |
| 889 | from the top-level directory, as environment variables will make sure |
| 890 | the right Kconfig is included from there (arch/$SRCARCH/Kconfig as of |
| 891 | writing). |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 892 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 893 | If $srctree is set, 'filename' will be looked up relative to it. |
| 894 | $srctree is also used to look up source'd files within Kconfig files. |
| 895 | See the class documentation. |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 896 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 897 | If you are using Kconfiglib via 'make scriptconfig', the filename of |
| 898 | the base base Kconfig file will be in sys.argv[1]. It's currently |
| 899 | always "Kconfig" in practice. |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 900 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 901 | warn (default: True): |
| 902 | True if warnings related to this configuration should be generated. |
| 903 | This can be changed later by setting Kconfig.warn to True/False. It |
| 904 | is provided as a constructor argument since warnings might be |
| 905 | generated during parsing. |
| 906 | |
| 907 | See the other Kconfig.warn_* variables as well, which enable or |
| 908 | suppress certain warnings when warnings are enabled. |
| 909 | |
| 910 | All generated warnings are added to the Kconfig.warnings list. See |
| 911 | the class documentation. |
| 912 | |
| 913 | warn_to_stderr (default: True): |
| 914 | True if warnings should be printed to stderr in addition to being |
| 915 | added to Kconfig.warnings. |
| 916 | |
| 917 | This can be changed later by setting Kconfig.warn_to_stderr to |
| 918 | True/False. |
| 919 | |
| 920 | encoding (default: "utf-8"): |
| 921 | The encoding to use when reading and writing files, and when decoding |
| 922 | output from commands run via $(shell). If None, the encoding |
| 923 | specified in the current locale will be used. |
| 924 | |
| 925 | The "utf-8" default avoids exceptions on systems that are configured |
| 926 | to use the C locale, which implies an ASCII encoding. |
| 927 | |
| 928 | This parameter has no effect on Python 2, due to implementation |
| 929 | issues (regular strings turning into Unicode strings, which are |
| 930 | distinct in Python 2). Python 2 doesn't decode regular strings |
| 931 | anyway. |
| 932 | |
| 933 | Related PEP: https://www.python.org/dev/peps/pep-0538/ |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 934 | |
| 935 | suppress_traceback (default: False): |
| 936 | Helper for tools. When True, any EnvironmentError or KconfigError |
| 937 | generated during parsing is caught, the exception message is printed |
| 938 | to stderr together with the command name, and sys.exit(1) is called |
| 939 | (which generates SystemExit). |
| 940 | |
| 941 | This hides the Python traceback for "expected" errors like syntax |
| 942 | errors in Kconfig files. |
| 943 | |
| 944 | Other exceptions besides EnvironmentError and KconfigError are still |
| 945 | propagated when suppress_traceback is True. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 946 | """ |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 947 | try: |
| 948 | self._init(filename, warn, warn_to_stderr, encoding) |
| 949 | except (EnvironmentError, KconfigError) as e: |
| 950 | if suppress_traceback: |
| 951 | cmd = sys.argv[0] # Empty string if missing |
| 952 | if cmd: |
| 953 | cmd += ": " |
| 954 | # Some long exception messages have extra newlines for better |
| 955 | # formatting when reported as an unhandled exception. Strip |
| 956 | # them here. |
| 957 | sys.exit(cmd + str(e).strip()) |
| 958 | raise |
| 959 | |
| 960 | def _init(self, filename, warn, warn_to_stderr, encoding): |
| 961 | # See __init__() |
| 962 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 963 | self._encoding = encoding |
| 964 | |
| 965 | self.srctree = os.getenv("srctree", "") |
| 966 | # A prefix we can reliably strip from glob() results to get a filename |
| 967 | # relative to $srctree. relpath() can cause issues for symlinks, |
| 968 | # because it assumes symlink/../foo is the same as foo/. |
| 969 | self._srctree_prefix = realpath(self.srctree) + os.sep |
| 970 | |
| 971 | self.warn = warn |
| 972 | self.warn_to_stderr = warn_to_stderr |
| 973 | self.warn_assign_undef = os.getenv("KCONFIG_WARN_UNDEF_ASSIGN") == "y" |
| 974 | self.warn_assign_override = True |
| 975 | self.warn_assign_redun = True |
| 976 | self._warn_assign_no_prompt = True |
| 977 | |
| 978 | self.warnings = [] |
| 979 | |
| 980 | self.config_prefix = os.getenv("CONFIG_", "CONFIG_") |
| 981 | # Regular expressions for parsing .config files |
| 982 | self._set_match = _re_match(self.config_prefix + r"([^=]+)=(.*)") |
| 983 | self._unset_match = _re_match(r"# {}([^ ]+) is not set".format( |
| 984 | self.config_prefix)) |
| 985 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 986 | self.config_header = os.getenv("KCONFIG_CONFIG_HEADER", "") |
| 987 | self.header_header = os.getenv("KCONFIG_AUTOHEADER_HEADER", "") |
| 988 | |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 989 | self.syms = {} |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 990 | self.const_syms = {} |
| 991 | self.defined_syms = [] |
| 992 | self.missing_syms = [] |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 993 | self.named_choices = {} |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 994 | self.choices = [] |
| 995 | self.menus = [] |
| 996 | self.comments = [] |
| 997 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 998 | for nmy in "n", "m", "y": |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 999 | sym = Symbol() |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1000 | sym.kconfig = self |
| 1001 | sym.name = nmy |
| 1002 | sym.is_constant = True |
| 1003 | sym.orig_type = TRISTATE |
| 1004 | sym._cached_tri_val = STR_TO_TRI[nmy] |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1005 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1006 | self.const_syms[nmy] = sym |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1007 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1008 | self.n = self.const_syms["n"] |
| 1009 | self.m = self.const_syms["m"] |
| 1010 | self.y = self.const_syms["y"] |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1011 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1012 | # Make n/m/y well-formed symbols |
| 1013 | for nmy in "n", "m", "y": |
| 1014 | sym = self.const_syms[nmy] |
| 1015 | sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1016 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1017 | # Maps preprocessor variables names to Variable instances |
| 1018 | self.variables = {} |
Ulf Magnusson | 4e1102f | 2017-09-19 12:52:55 +0200 | [diff] [blame] | 1019 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1020 | # Predefined preprocessor functions, with min/max number of arguments |
| 1021 | self._functions = { |
| 1022 | "info": (_info_fn, 1, 1), |
| 1023 | "error-if": (_error_if_fn, 2, 2), |
| 1024 | "filename": (_filename_fn, 0, 0), |
| 1025 | "lineno": (_lineno_fn, 0, 0), |
| 1026 | "shell": (_shell_fn, 1, 1), |
| 1027 | "warning-if": (_warning_if_fn, 2, 2), |
| 1028 | } |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1029 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1030 | # Add any user-defined preprocessor functions |
| 1031 | try: |
| 1032 | self._functions.update( |
| 1033 | importlib.import_module( |
| 1034 | os.getenv("KCONFIG_FUNCTIONS", "kconfigfunctions") |
| 1035 | ).functions) |
| 1036 | except ImportError: |
| 1037 | pass |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1038 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1039 | # This determines whether previously unseen symbols are registered. |
| 1040 | # They shouldn't be if we parse expressions after parsing, as part of |
| 1041 | # Kconfig.eval_string(). |
| 1042 | self._parsing_kconfigs = True |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1043 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1044 | self.modules = self._lookup_sym("MODULES") |
| 1045 | self.defconfig_list = None |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1046 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1047 | self.top_node = MenuNode() |
| 1048 | self.top_node.kconfig = self |
| 1049 | self.top_node.item = MENU |
| 1050 | self.top_node.is_menuconfig = True |
| 1051 | self.top_node.visibility = self.y |
| 1052 | self.top_node.prompt = ("Main menu", self.y) |
| 1053 | self.top_node.parent = None |
| 1054 | self.top_node.dep = self.y |
| 1055 | self.top_node.filename = filename |
| 1056 | self.top_node.linenr = 1 |
| 1057 | self.top_node.include_path = () |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1058 | |
| 1059 | # Parse the Kconfig files |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1060 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1061 | # Not used internally. Provided as a convenience. |
| 1062 | self.kconfig_filenames = [filename] |
| 1063 | self.env_vars = set() |
| 1064 | |
| 1065 | # Keeps track of the location in the parent Kconfig files. Kconfig |
| 1066 | # files usually source other Kconfig files. See _enter_file(). |
| 1067 | self._filestack = [] |
| 1068 | self._include_path = () |
| 1069 | |
| 1070 | # The current parsing location |
| 1071 | self.filename = filename |
| 1072 | self.linenr = 0 |
| 1073 | |
| 1074 | # Used to avoid retokenizing lines when we discover that they're not |
| 1075 | # part of the construct currently being parsed. This is kinda like an |
| 1076 | # unget operation. |
| 1077 | self._reuse_tokens = False |
| 1078 | |
| 1079 | # Open the top-level Kconfig file. Store the readline() method directly |
| 1080 | # as a small optimization. |
| 1081 | self._readline = self._open(join(self.srctree, filename), "r").readline |
| 1082 | |
| 1083 | try: |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1084 | # Parse the Kconfig files. Returns the last node, which we |
| 1085 | # terminate with '.next = None'. |
| 1086 | self._parse_block(None, self.top_node, self.top_node).next = None |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1087 | self.top_node.list = self.top_node.next |
| 1088 | self.top_node.next = None |
| 1089 | except UnicodeDecodeError as e: |
| 1090 | _decoding_error(e, self.filename) |
| 1091 | |
| 1092 | # Close the top-level Kconfig file. __self__ fetches the 'file' object |
| 1093 | # for the method. |
| 1094 | self._readline.__self__.close() |
| 1095 | |
| 1096 | self._parsing_kconfigs = False |
| 1097 | |
| 1098 | # Do various menu tree post-processing |
| 1099 | self._finalize_node(self.top_node, self.y) |
| 1100 | |
| 1101 | self.unique_defined_syms = _ordered_unique(self.defined_syms) |
| 1102 | self.unique_choices = _ordered_unique(self.choices) |
| 1103 | |
| 1104 | # Do sanity checks. Some of these depend on everything being finalized. |
| 1105 | self._check_sym_sanity() |
| 1106 | self._check_choice_sanity() |
| 1107 | |
| 1108 | # KCONFIG_STRICT is an older alias for KCONFIG_WARN_UNDEF, supported |
| 1109 | # for backwards compatibility |
| 1110 | if os.getenv("KCONFIG_WARN_UNDEF") == "y" or \ |
| 1111 | os.getenv("KCONFIG_STRICT") == "y": |
| 1112 | |
| 1113 | self._check_undef_syms() |
| 1114 | |
| 1115 | # Build Symbol._dependents for all symbols and choices |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1116 | self._build_dep() |
| 1117 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1118 | # Check for dependency loops |
| 1119 | check_dep_loop_sym = _check_dep_loop_sym # Micro-optimization |
| 1120 | for sym in self.unique_defined_syms: |
| 1121 | check_dep_loop_sym(sym, False) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1122 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1123 | # Add extra dependencies from choices to choice symbols that get |
| 1124 | # awkward during dependency loop detection |
| 1125 | self._add_choice_deps() |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1126 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1127 | @property |
| 1128 | def mainmenu_text(self): |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1129 | """ |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1130 | See the class documentation. |
| 1131 | """ |
| 1132 | return self.top_node.prompt[0] |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1133 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1134 | @property |
| 1135 | def defconfig_filename(self): |
| 1136 | """ |
| 1137 | See the class documentation. |
| 1138 | """ |
| 1139 | if self.defconfig_list: |
| 1140 | for filename, cond in self.defconfig_list.defaults: |
| 1141 | if expr_value(cond): |
| 1142 | try: |
| 1143 | with self._open_config(filename.str_value) as f: |
| 1144 | return f.name |
| 1145 | except EnvironmentError: |
| 1146 | continue |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1147 | |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1148 | return None |
| 1149 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1150 | def load_config(self, filename=None, replace=True, verbose=None): |
Simon Glass | 8639f69 | 2017-08-04 03:30:30 -0600 | [diff] [blame] | 1151 | """ |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1152 | Loads symbol values from a file in the .config format. Equivalent to |
| 1153 | calling Symbol.set_value() to set each of the values. |
Simon Glass | 8639f69 | 2017-08-04 03:30:30 -0600 | [diff] [blame] | 1154 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1155 | "# CONFIG_FOO is not set" within a .config file sets the user value of |
| 1156 | FOO to n. The C tools work the same way. |
Ulf Magnusson | 4e1102f | 2017-09-19 12:52:55 +0200 | [diff] [blame] | 1157 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1158 | For each symbol, the Symbol.user_value attribute holds the value the |
| 1159 | symbol was assigned in the .config file (if any). The user value might |
| 1160 | differ from Symbol.str/tri_value if there are unsatisfied dependencies. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1161 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1162 | Calling this function also updates the Kconfig.missing_syms attribute |
| 1163 | with a list of all assignments to undefined symbols within the |
| 1164 | configuration file. Kconfig.missing_syms is cleared if 'replace' is |
| 1165 | True, and appended to otherwise. See the documentation for |
| 1166 | Kconfig.missing_syms as well. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1167 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1168 | See the Kconfig.__init__() docstring for raised exceptions |
| 1169 | (OSError/IOError). KconfigError is never raised here. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1170 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1171 | filename (default: None): |
| 1172 | Path to load configuration from (a string). Respects $srctree if set |
| 1173 | (see the class documentation). |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1174 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1175 | If 'filename' is None (the default), the configuration file to load |
| 1176 | (if any) is calculated automatically, giving the behavior you'd |
| 1177 | usually want: |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1178 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1179 | 1. If the KCONFIG_CONFIG environment variable is set, it gives the |
| 1180 | path to the configuration file to load. Otherwise, ".config" is |
| 1181 | used. See standard_config_filename(). |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1182 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1183 | 2. If the path from (1.) doesn't exist, the configuration file |
| 1184 | given by kconf.defconfig_filename is loaded instead, which is |
| 1185 | derived from the 'option defconfig_list' symbol. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1186 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1187 | 3. If (1.) and (2.) fail to find a configuration file to load, no |
| 1188 | configuration file is loaded, and symbols retain their current |
| 1189 | values (e.g., their default values). This is not an error. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1190 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1191 | See the return value as well. |
| 1192 | |
| 1193 | replace (default: True): |
| 1194 | If True, all existing user values will be cleared before loading the |
| 1195 | .config. Pass False to merge configurations. |
| 1196 | |
| 1197 | verbose (default: None): |
| 1198 | Limited backwards compatibility to prevent crashes. A warning is |
| 1199 | printed if anything but None is passed. |
| 1200 | |
| 1201 | Prior to Kconfiglib 12.0.0, this option enabled printing of messages |
| 1202 | to stdout when 'filename' was None. A message is (always) returned |
| 1203 | now instead, which is more flexible. |
| 1204 | |
| 1205 | Will probably be removed in some future version. |
| 1206 | |
| 1207 | Returns a string with a message saying which file got loaded (or |
| 1208 | possibly that no file got loaded, when 'filename' is None). This is |
| 1209 | meant to reduce boilerplate in tools, which can do e.g. |
| 1210 | print(kconf.load_config()). The returned message distinguishes between |
| 1211 | loading (replace == True) and merging (replace == False). |
| 1212 | """ |
| 1213 | if verbose is not None: |
| 1214 | _warn_verbose_deprecated("load_config") |
| 1215 | |
| 1216 | msg = None |
| 1217 | if filename is None: |
| 1218 | filename = standard_config_filename() |
| 1219 | if not exists(filename) and \ |
| 1220 | not exists(join(self.srctree, filename)): |
| 1221 | defconfig = self.defconfig_filename |
| 1222 | if defconfig is None: |
| 1223 | return "Using default symbol values (no '{}')" \ |
| 1224 | .format(filename) |
| 1225 | |
| 1226 | msg = " default configuration '{}' (no '{}')" \ |
| 1227 | .format(defconfig, filename) |
| 1228 | filename = defconfig |
| 1229 | |
| 1230 | if not msg: |
| 1231 | msg = " configuration '{}'".format(filename) |
| 1232 | |
| 1233 | # Disable the warning about assigning to symbols without prompts. This |
| 1234 | # is normal and expected within a .config file. |
| 1235 | self._warn_assign_no_prompt = False |
| 1236 | |
| 1237 | # This stub only exists to make sure _warn_assign_no_prompt gets |
| 1238 | # reenabled |
| 1239 | try: |
| 1240 | self._load_config(filename, replace) |
| 1241 | except UnicodeDecodeError as e: |
| 1242 | _decoding_error(e, filename) |
| 1243 | finally: |
| 1244 | self._warn_assign_no_prompt = True |
| 1245 | |
| 1246 | return ("Loaded" if replace else "Merged") + msg |
| 1247 | |
| 1248 | def _load_config(self, filename, replace): |
| 1249 | with self._open_config(filename) as f: |
| 1250 | if replace: |
| 1251 | self.missing_syms = [] |
| 1252 | |
| 1253 | # If we're replacing the configuration, keep track of which |
| 1254 | # symbols and choices got set so that we can unset the rest |
| 1255 | # later. This avoids invalidating everything and is faster. |
| 1256 | # Another benefit is that invalidation must be rock solid for |
| 1257 | # it to work, making it a good test. |
| 1258 | |
| 1259 | for sym in self.unique_defined_syms: |
| 1260 | sym._was_set = False |
| 1261 | |
| 1262 | for choice in self.unique_choices: |
| 1263 | choice._was_set = False |
| 1264 | |
| 1265 | # Small optimizations |
| 1266 | set_match = self._set_match |
| 1267 | unset_match = self._unset_match |
| 1268 | get_sym = self.syms.get |
| 1269 | |
| 1270 | for linenr, line in enumerate(f, 1): |
| 1271 | # The C tools ignore trailing whitespace |
| 1272 | line = line.rstrip() |
| 1273 | |
| 1274 | match = set_match(line) |
| 1275 | if match: |
| 1276 | name, val = match.groups() |
| 1277 | sym = get_sym(name) |
| 1278 | if not sym or not sym.nodes: |
| 1279 | self._undef_assign(name, val, filename, linenr) |
| 1280 | continue |
| 1281 | |
| 1282 | if sym.orig_type in _BOOL_TRISTATE: |
| 1283 | # The C implementation only checks the first character |
| 1284 | # to the right of '=', for whatever reason |
| 1285 | if not (sym.orig_type is BOOL |
| 1286 | and val.startswith(("y", "n")) or |
| 1287 | sym.orig_type is TRISTATE |
| 1288 | and val.startswith(("y", "m", "n"))): |
| 1289 | self._warn("'{}' is not a valid value for the {} " |
| 1290 | "symbol {}. Assignment ignored." |
| 1291 | .format(val, TYPE_TO_STR[sym.orig_type], |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1292 | sym.name_and_loc), |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1293 | filename, linenr) |
| 1294 | continue |
| 1295 | |
| 1296 | val = val[0] |
| 1297 | |
| 1298 | if sym.choice and val != "n": |
| 1299 | # During .config loading, we infer the mode of the |
| 1300 | # choice from the kind of values that are assigned |
| 1301 | # to the choice symbols |
| 1302 | |
| 1303 | prev_mode = sym.choice.user_value |
| 1304 | if prev_mode is not None and \ |
| 1305 | TRI_TO_STR[prev_mode] != val: |
| 1306 | |
| 1307 | self._warn("both m and y assigned to symbols " |
| 1308 | "within the same choice", |
| 1309 | filename, linenr) |
| 1310 | |
| 1311 | # Set the choice's mode |
| 1312 | sym.choice.set_value(val) |
| 1313 | |
| 1314 | elif sym.orig_type is STRING: |
| 1315 | match = _conf_string_match(val) |
| 1316 | if not match: |
| 1317 | self._warn("malformed string literal in " |
| 1318 | "assignment to {}. Assignment ignored." |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1319 | .format(sym.name_and_loc), |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1320 | filename, linenr) |
| 1321 | continue |
| 1322 | |
| 1323 | val = unescape(match.group(1)) |
| 1324 | |
| 1325 | else: |
| 1326 | match = unset_match(line) |
| 1327 | if not match: |
| 1328 | # Print a warning for lines that match neither |
| 1329 | # set_match() nor unset_match() and that are not blank |
| 1330 | # lines or comments. 'line' has already been |
| 1331 | # rstrip()'d, so blank lines show up as "" here. |
| 1332 | if line and not line.lstrip().startswith("#"): |
| 1333 | self._warn("ignoring malformed line '{}'" |
| 1334 | .format(line), |
| 1335 | filename, linenr) |
| 1336 | |
| 1337 | continue |
| 1338 | |
| 1339 | name = match.group(1) |
| 1340 | sym = get_sym(name) |
| 1341 | if not sym or not sym.nodes: |
| 1342 | self._undef_assign(name, "n", filename, linenr) |
| 1343 | continue |
| 1344 | |
| 1345 | if sym.orig_type not in _BOOL_TRISTATE: |
| 1346 | continue |
| 1347 | |
| 1348 | val = "n" |
| 1349 | |
| 1350 | # Done parsing the assignment. Set the value. |
| 1351 | |
| 1352 | if sym._was_set: |
| 1353 | self._assigned_twice(sym, val, filename, linenr) |
| 1354 | |
| 1355 | sym.set_value(val) |
| 1356 | |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1357 | if replace: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1358 | # If we're replacing the configuration, unset the symbols that |
| 1359 | # didn't get set |
| 1360 | |
| 1361 | for sym in self.unique_defined_syms: |
| 1362 | if not sym._was_set: |
| 1363 | sym.unset_value() |
| 1364 | |
| 1365 | for choice in self.unique_choices: |
| 1366 | if not choice._was_set: |
| 1367 | choice.unset_value() |
| 1368 | |
| 1369 | def _undef_assign(self, name, val, filename, linenr): |
| 1370 | # Called for assignments to undefined symbols during .config loading |
| 1371 | |
| 1372 | self.missing_syms.append((name, val)) |
| 1373 | if self.warn_assign_undef: |
| 1374 | self._warn( |
| 1375 | "attempt to assign the value '{}' to the undefined symbol {}" |
| 1376 | .format(val, name), filename, linenr) |
| 1377 | |
| 1378 | def _assigned_twice(self, sym, new_val, filename, linenr): |
| 1379 | # Called when a symbol is assigned more than once in a .config file |
| 1380 | |
| 1381 | # Use strings for bool/tristate user values in the warning |
| 1382 | if sym.orig_type in _BOOL_TRISTATE: |
| 1383 | user_val = TRI_TO_STR[sym.user_value] |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1384 | else: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1385 | user_val = sym.user_value |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1386 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1387 | msg = '{} set more than once. Old value "{}", new value "{}".'.format( |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1388 | sym.name_and_loc, user_val, new_val) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1389 | |
| 1390 | if user_val == new_val: |
| 1391 | if self.warn_assign_redun: |
| 1392 | self._warn(msg, filename, linenr) |
| 1393 | elif self.warn_assign_override: |
| 1394 | self._warn(msg, filename, linenr) |
| 1395 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1396 | def load_allconfig(self, filename): |
| 1397 | """ |
| 1398 | Helper for all*config. Loads (merges) the configuration file specified |
| 1399 | by KCONFIG_ALLCONFIG, if any. See Documentation/kbuild/kconfig.txt in |
| 1400 | the Linux kernel. |
| 1401 | |
| 1402 | Disables warnings for duplicated assignments within configuration files |
| 1403 | for the duration of the call |
| 1404 | (kconf.warn_assign_override/warn_assign_redun = False), and restores |
| 1405 | the previous warning settings at the end. The KCONFIG_ALLCONFIG |
| 1406 | configuration file is expected to override symbols. |
| 1407 | |
| 1408 | Exits with sys.exit() (which raises a SystemExit exception) and prints |
| 1409 | an error to stderr if KCONFIG_ALLCONFIG is set but the configuration |
| 1410 | file can't be opened. |
| 1411 | |
| 1412 | filename: |
| 1413 | Command-specific configuration filename - "allyes.config", |
| 1414 | "allno.config", etc. |
| 1415 | """ |
| 1416 | load_allconfig(self, filename) |
| 1417 | |
| 1418 | def write_autoconf(self, filename=None, header=None): |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1419 | r""" |
| 1420 | Writes out symbol values as a C header file, matching the format used |
| 1421 | by include/generated/autoconf.h in the kernel. |
| 1422 | |
| 1423 | The ordering of the #defines matches the one generated by |
| 1424 | write_config(). The order in the C implementation depends on the hash |
| 1425 | table implementation as of writing, and so won't match. |
| 1426 | |
| 1427 | If 'filename' exists and its contents is identical to what would get |
| 1428 | written out, it is left untouched. This avoids updating file metadata |
| 1429 | like the modification time and possibly triggering redundant work in |
| 1430 | build tools. |
| 1431 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1432 | filename (default: None): |
| 1433 | Path to write header to. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1434 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1435 | If None (the default), the path in the environment variable |
| 1436 | KCONFIG_AUTOHEADER is used if set, and "include/generated/autoconf.h" |
| 1437 | otherwise. This is compatible with the C tools. |
| 1438 | |
| 1439 | header (default: None): |
| 1440 | Text inserted verbatim at the beginning of the file. You would |
| 1441 | usually want it enclosed in '/* */' to make it a C comment, and |
| 1442 | include a trailing newline. |
| 1443 | |
| 1444 | If None (the default), the value of the environment variable |
| 1445 | KCONFIG_AUTOHEADER_HEADER had when the Kconfig instance was created |
| 1446 | will be used if it was set, and no header otherwise. See the |
| 1447 | Kconfig.header_header attribute. |
| 1448 | |
| 1449 | Returns a string with a message saying that the header got saved, or |
| 1450 | that there were no changes to it. This is meant to reduce boilerplate |
| 1451 | in tools, which can do e.g. print(kconf.write_autoconf()). |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1452 | """ |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1453 | if filename is None: |
| 1454 | filename = os.getenv("KCONFIG_AUTOHEADER", |
| 1455 | "include/generated/autoconf.h") |
| 1456 | |
| 1457 | if self._write_if_changed(filename, self._autoconf_contents(header)): |
| 1458 | return "Kconfig header saved to '{}'".format(filename) |
| 1459 | return "No change to Kconfig header in '{}'".format(filename) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1460 | |
| 1461 | def _autoconf_contents(self, header): |
| 1462 | # write_autoconf() helper. Returns the contents to write as a string, |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1463 | # with 'header' or KCONFIG_AUTOHEADER_HEADER at the beginning. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1464 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1465 | if header is None: |
| 1466 | header = self.header_header |
| 1467 | |
| 1468 | chunks = [header] # "".join()ed later |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1469 | add = chunks.append |
| 1470 | |
| 1471 | for sym in self.unique_defined_syms: |
| 1472 | # _write_to_conf is determined when the value is calculated. This |
| 1473 | # is a hidden function call due to property magic. |
| 1474 | # |
| 1475 | # Note: In client code, you can check if sym.config_string is empty |
| 1476 | # instead, to avoid accessing the internal _write_to_conf variable |
| 1477 | # (though it's likely to keep working). |
| 1478 | val = sym.str_value |
| 1479 | if not sym._write_to_conf: |
| 1480 | continue |
| 1481 | |
| 1482 | if sym.orig_type in _BOOL_TRISTATE: |
| 1483 | if val == "y": |
| 1484 | add("#define {}{} 1\n" |
| 1485 | .format(self.config_prefix, sym.name)) |
| 1486 | elif val == "m": |
| 1487 | add("#define {}{}_MODULE 1\n" |
| 1488 | .format(self.config_prefix, sym.name)) |
| 1489 | |
| 1490 | elif sym.orig_type is STRING: |
| 1491 | add('#define {}{} "{}"\n' |
| 1492 | .format(self.config_prefix, sym.name, escape(val))) |
| 1493 | |
| 1494 | else: # sym.orig_type in _INT_HEX: |
| 1495 | if sym.orig_type is HEX and \ |
| 1496 | not val.startswith(("0x", "0X")): |
| 1497 | val = "0x" + val |
| 1498 | |
| 1499 | add("#define {}{} {}\n" |
| 1500 | .format(self.config_prefix, sym.name, val)) |
| 1501 | |
| 1502 | return "".join(chunks) |
| 1503 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1504 | def write_config(self, filename=None, header=None, save_old=True, |
| 1505 | verbose=None): |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1506 | r""" |
| 1507 | Writes out symbol values in the .config format. The format matches the |
| 1508 | C implementation, including ordering. |
| 1509 | |
| 1510 | Symbols appear in the same order in generated .config files as they do |
| 1511 | in the Kconfig files. For symbols defined in multiple locations, a |
| 1512 | single assignment is written out corresponding to the first location |
| 1513 | where the symbol is defined. |
| 1514 | |
| 1515 | See the 'Intro to symbol values' section in the module docstring to |
| 1516 | understand which symbols get written out. |
| 1517 | |
| 1518 | If 'filename' exists and its contents is identical to what would get |
| 1519 | written out, it is left untouched. This avoids updating file metadata |
| 1520 | like the modification time and possibly triggering redundant work in |
| 1521 | build tools. |
| 1522 | |
| 1523 | See the Kconfig.__init__() docstring for raised exceptions |
| 1524 | (OSError/IOError). KconfigError is never raised here. |
| 1525 | |
| 1526 | filename (default: None): |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1527 | Path to write configuration to (a string). |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1528 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1529 | If None (the default), the path in the environment variable |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1530 | KCONFIG_CONFIG is used if set, and ".config" otherwise. See |
| 1531 | standard_config_filename(). |
| 1532 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1533 | header (default: None): |
| 1534 | Text inserted verbatim at the beginning of the file. You would |
| 1535 | usually want each line to start with '#' to make it a comment, and |
| 1536 | include a trailing newline. |
| 1537 | |
| 1538 | if None (the default), the value of the environment variable |
| 1539 | KCONFIG_CONFIG_HEADER had when the Kconfig instance was created will |
| 1540 | be used if it was set, and no header otherwise. See the |
| 1541 | Kconfig.config_header attribute. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1542 | |
| 1543 | save_old (default: True): |
| 1544 | If True and <filename> already exists, a copy of it will be saved to |
| 1545 | <filename>.old in the same directory before the new configuration is |
| 1546 | written. |
| 1547 | |
| 1548 | Errors are silently ignored if <filename>.old cannot be written (e.g. |
| 1549 | due to being a directory, or <filename> being something like |
| 1550 | /dev/null). |
| 1551 | |
| 1552 | verbose (default: None): |
| 1553 | Limited backwards compatibility to prevent crashes. A warning is |
| 1554 | printed if anything but None is passed. |
| 1555 | |
| 1556 | Prior to Kconfiglib 12.0.0, this option enabled printing of messages |
| 1557 | to stdout when 'filename' was None. A message is (always) returned |
| 1558 | now instead, which is more flexible. |
| 1559 | |
| 1560 | Will probably be removed in some future version. |
| 1561 | |
| 1562 | Returns a string with a message saying which file got saved. This is |
| 1563 | meant to reduce boilerplate in tools, which can do e.g. |
| 1564 | print(kconf.write_config()). |
| 1565 | """ |
| 1566 | if verbose is not None: |
| 1567 | _warn_verbose_deprecated("write_config") |
| 1568 | |
| 1569 | if filename is None: |
| 1570 | filename = standard_config_filename() |
| 1571 | |
| 1572 | contents = self._config_contents(header) |
| 1573 | if self._contents_eq(filename, contents): |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1574 | return "No change to configuration in '{}'".format(filename) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1575 | |
| 1576 | if save_old: |
| 1577 | _save_old(filename) |
| 1578 | |
| 1579 | with self._open(filename, "w") as f: |
| 1580 | f.write(contents) |
| 1581 | |
| 1582 | return "Configuration saved to '{}'".format(filename) |
| 1583 | |
| 1584 | def _config_contents(self, header): |
| 1585 | # write_config() helper. Returns the contents to write as a string, |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1586 | # with 'header' or KCONFIG_CONFIG_HEADER at the beginning. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1587 | # |
| 1588 | # More memory friendly would be to 'yield' the strings and |
| 1589 | # "".join(_config_contents()), but it was a bit slower on my system. |
| 1590 | |
| 1591 | # node_iter() was used here before commit 3aea9f7 ("Add '# end of |
| 1592 | # <menu>' after menus in .config"). Those comments get tricky to |
| 1593 | # implement with it. |
| 1594 | |
| 1595 | for sym in self.unique_defined_syms: |
| 1596 | sym._visited = False |
| 1597 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1598 | if header is None: |
| 1599 | header = self.config_header |
| 1600 | |
| 1601 | chunks = [header] # "".join()ed later |
| 1602 | add = chunks.append |
| 1603 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1604 | # Did we just print an '# end of ...' comment? |
| 1605 | after_end_comment = False |
| 1606 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1607 | node = self.top_node |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1608 | while 1: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1609 | # Jump to the next node with an iterative tree walk |
| 1610 | if node.list: |
| 1611 | node = node.list |
| 1612 | elif node.next: |
| 1613 | node = node.next |
| 1614 | else: |
| 1615 | while node.parent: |
| 1616 | node = node.parent |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1617 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1618 | # Add a comment when leaving visible menus |
| 1619 | if node.item is MENU and expr_value(node.dep) and \ |
| 1620 | expr_value(node.visibility) and \ |
| 1621 | node is not self.top_node: |
| 1622 | add("# end of {}\n".format(node.prompt[0])) |
| 1623 | after_end_comment = True |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1624 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1625 | if node.next: |
| 1626 | node = node.next |
| 1627 | break |
| 1628 | else: |
| 1629 | # No more nodes |
| 1630 | return "".join(chunks) |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1631 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1632 | # Generate configuration output for the node |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1633 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1634 | item = node.item |
| 1635 | |
| 1636 | if item.__class__ is Symbol: |
| 1637 | if item._visited: |
| 1638 | continue |
| 1639 | item._visited = True |
| 1640 | |
| 1641 | conf_string = item.config_string |
| 1642 | if not conf_string: |
| 1643 | continue |
| 1644 | |
| 1645 | if after_end_comment: |
| 1646 | # Add a blank line before the first symbol printed after an |
| 1647 | # '# end of ...' comment |
| 1648 | after_end_comment = False |
| 1649 | add("\n") |
| 1650 | add(conf_string) |
| 1651 | |
| 1652 | elif expr_value(node.dep) and \ |
| 1653 | ((item is MENU and expr_value(node.visibility)) or |
| 1654 | item is COMMENT): |
| 1655 | |
| 1656 | add("\n#\n# {}\n#\n".format(node.prompt[0])) |
| 1657 | after_end_comment = False |
| 1658 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1659 | def write_min_config(self, filename, header=None): |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1660 | """ |
| 1661 | Writes out a "minimal" configuration file, omitting symbols whose value |
| 1662 | matches their default value. The format matches the one produced by |
| 1663 | 'make savedefconfig'. |
| 1664 | |
| 1665 | The resulting configuration file is incomplete, but a complete |
| 1666 | configuration can be derived from it by loading it. Minimal |
| 1667 | configuration files can serve as a more manageable configuration format |
| 1668 | compared to a "full" .config file, especially when configurations files |
| 1669 | are merged or edited by hand. |
| 1670 | |
| 1671 | See the Kconfig.__init__() docstring for raised exceptions |
| 1672 | (OSError/IOError). KconfigError is never raised here. |
| 1673 | |
| 1674 | filename: |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1675 | Path to write minimal configuration to. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1676 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1677 | header (default: None): |
| 1678 | Text inserted verbatim at the beginning of the file. You would |
| 1679 | usually want each line to start with '#' to make it a comment, and |
| 1680 | include a final terminating newline. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1681 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1682 | if None (the default), the value of the environment variable |
| 1683 | KCONFIG_CONFIG_HEADER had when the Kconfig instance was created will |
| 1684 | be used if it was set, and no header otherwise. See the |
| 1685 | Kconfig.config_header attribute. |
| 1686 | |
| 1687 | Returns a string with a message saying the minimal configuration got |
| 1688 | saved, or that there were no changes to it. This is meant to reduce |
| 1689 | boilerplate in tools, which can do e.g. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1690 | print(kconf.write_min_config()). |
| 1691 | """ |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1692 | if self._write_if_changed(filename, self._min_config_contents(header)): |
| 1693 | return "Minimal configuration saved to '{}'".format(filename) |
| 1694 | return "No change to minimal configuration in '{}'".format(filename) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1695 | |
| 1696 | def _min_config_contents(self, header): |
| 1697 | # write_min_config() helper. Returns the contents to write as a string, |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1698 | # with 'header' or KCONFIG_CONFIG_HEADER at the beginning. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1699 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 1700 | if header is None: |
| 1701 | header = self.config_header |
| 1702 | |
| 1703 | chunks = [header] # "".join()ed later |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1704 | add = chunks.append |
| 1705 | |
| 1706 | for sym in self.unique_defined_syms: |
| 1707 | # Skip symbols that cannot be changed. Only check |
| 1708 | # non-choice symbols, as selects don't affect choice |
| 1709 | # symbols. |
| 1710 | if not sym.choice and \ |
| 1711 | sym.visibility <= expr_value(sym.rev_dep): |
| 1712 | continue |
| 1713 | |
| 1714 | # Skip symbols whose value matches their default |
| 1715 | if sym.str_value == sym._str_default(): |
| 1716 | continue |
| 1717 | |
| 1718 | # Skip symbols that would be selected by default in a |
| 1719 | # choice, unless the choice is optional or the symbol type |
| 1720 | # isn't bool (it might be possible to set the choice mode |
| 1721 | # to n or the symbol to m in those cases). |
| 1722 | if sym.choice and \ |
| 1723 | not sym.choice.is_optional and \ |
| 1724 | sym.choice._selection_from_defaults() is sym and \ |
| 1725 | sym.orig_type is BOOL and \ |
| 1726 | sym.tri_value == 2: |
| 1727 | continue |
| 1728 | |
| 1729 | add(sym.config_string) |
| 1730 | |
| 1731 | return "".join(chunks) |
| 1732 | |
| 1733 | def sync_deps(self, path): |
| 1734 | """ |
| 1735 | Creates or updates a directory structure that can be used to avoid |
| 1736 | doing a full rebuild whenever the configuration is changed, mirroring |
| 1737 | include/config/ in the kernel. |
| 1738 | |
| 1739 | This function is intended to be called during each build, before |
| 1740 | compiling source files that depend on configuration symbols. |
| 1741 | |
| 1742 | See the Kconfig.__init__() docstring for raised exceptions |
| 1743 | (OSError/IOError). KconfigError is never raised here. |
| 1744 | |
| 1745 | path: |
| 1746 | Path to directory |
| 1747 | |
| 1748 | sync_deps(path) does the following: |
| 1749 | |
| 1750 | 1. If the directory <path> does not exist, it is created. |
| 1751 | |
| 1752 | 2. If <path>/auto.conf exists, old symbol values are loaded from it, |
| 1753 | which are then compared against the current symbol values. If a |
| 1754 | symbol has changed value (would generate different output in |
| 1755 | autoconf.h compared to before), the change is signaled by |
| 1756 | touch'ing a file corresponding to the symbol. |
| 1757 | |
| 1758 | The first time sync_deps() is run on a directory, <path>/auto.conf |
| 1759 | won't exist, and no old symbol values will be available. This |
| 1760 | logically has the same effect as updating the entire |
| 1761 | configuration. |
| 1762 | |
| 1763 | The path to a symbol's file is calculated from the symbol's name |
| 1764 | by replacing all '_' with '/' and appending '.h'. For example, the |
| 1765 | symbol FOO_BAR_BAZ gets the file <path>/foo/bar/baz.h, and FOO |
| 1766 | gets the file <path>/foo.h. |
| 1767 | |
| 1768 | This scheme matches the C tools. The point is to avoid having a |
| 1769 | single directory with a huge number of files, which the underlying |
| 1770 | filesystem might not handle well. |
| 1771 | |
| 1772 | 3. A new auto.conf with the current symbol values is written, to keep |
| 1773 | track of them for the next build. |
| 1774 | |
| 1775 | If auto.conf exists and its contents is identical to what would |
| 1776 | get written out, it is left untouched. This avoids updating file |
| 1777 | metadata like the modification time and possibly triggering |
| 1778 | redundant work in build tools. |
| 1779 | |
| 1780 | |
| 1781 | The last piece of the puzzle is knowing what symbols each source file |
| 1782 | depends on. Knowing that, dependencies can be added from source files |
| 1783 | to the files corresponding to the symbols they depends on. The source |
| 1784 | file will then get recompiled (only) when the symbol value changes |
| 1785 | (provided sync_deps() is run first during each build). |
| 1786 | |
| 1787 | The tool in the kernel that extracts symbol dependencies from source |
| 1788 | files is scripts/basic/fixdep.c. Missing symbol files also correspond |
| 1789 | to "not changed", which fixdep deals with by using the $(wildcard) Make |
| 1790 | function when adding symbol prerequisites to source files. |
| 1791 | |
| 1792 | In case you need a different scheme for your project, the sync_deps() |
| 1793 | implementation can be used as a template. |
| 1794 | """ |
| 1795 | if not exists(path): |
| 1796 | os.mkdir(path, 0o755) |
| 1797 | |
| 1798 | # Load old values from auto.conf, if any |
| 1799 | self._load_old_vals(path) |
| 1800 | |
| 1801 | for sym in self.unique_defined_syms: |
| 1802 | # _write_to_conf is determined when the value is calculated. This |
| 1803 | # is a hidden function call due to property magic. |
| 1804 | # |
| 1805 | # Note: In client code, you can check if sym.config_string is empty |
| 1806 | # instead, to avoid accessing the internal _write_to_conf variable |
| 1807 | # (though it's likely to keep working). |
| 1808 | val = sym.str_value |
| 1809 | |
| 1810 | # n tristate values do not get written to auto.conf and autoconf.h, |
| 1811 | # making a missing symbol logically equivalent to n |
| 1812 | |
| 1813 | if sym._write_to_conf: |
| 1814 | if sym._old_val is None and \ |
| 1815 | sym.orig_type in _BOOL_TRISTATE and \ |
| 1816 | val == "n": |
| 1817 | # No old value (the symbol was missing or n), new value n. |
| 1818 | # No change. |
| 1819 | continue |
| 1820 | |
| 1821 | if val == sym._old_val: |
| 1822 | # New value matches old. No change. |
| 1823 | continue |
| 1824 | |
| 1825 | elif sym._old_val is None: |
| 1826 | # The symbol wouldn't appear in autoconf.h (because |
| 1827 | # _write_to_conf is false), and it wouldn't have appeared in |
| 1828 | # autoconf.h previously either (because it didn't appear in |
| 1829 | # auto.conf). No change. |
| 1830 | continue |
| 1831 | |
| 1832 | # 'sym' has a new value. Flag it. |
| 1833 | _touch_dep_file(path, sym.name) |
| 1834 | |
| 1835 | # Remember the current values as the "new old" values. |
| 1836 | # |
| 1837 | # This call could go anywhere after the call to _load_old_vals(), but |
| 1838 | # putting it last means _sync_deps() can be safely rerun if it fails |
| 1839 | # before this point. |
| 1840 | self._write_old_vals(path) |
| 1841 | |
| 1842 | def _load_old_vals(self, path): |
| 1843 | # Loads old symbol values from auto.conf into a dedicated |
| 1844 | # Symbol._old_val field. Mirrors load_config(). |
| 1845 | # |
| 1846 | # The extra field could be avoided with some trickery involving dumping |
| 1847 | # symbol values and restoring them later, but this is simpler and |
| 1848 | # faster. The C tools also use a dedicated field for this purpose. |
| 1849 | |
| 1850 | for sym in self.unique_defined_syms: |
| 1851 | sym._old_val = None |
| 1852 | |
| 1853 | try: |
| 1854 | auto_conf = self._open(join(path, "auto.conf"), "r") |
| 1855 | except EnvironmentError as e: |
| 1856 | if e.errno == errno.ENOENT: |
| 1857 | # No old values |
| 1858 | return |
| 1859 | raise |
| 1860 | |
| 1861 | with auto_conf as f: |
| 1862 | for line in f: |
| 1863 | match = self._set_match(line) |
| 1864 | if not match: |
| 1865 | # We only expect CONFIG_FOO=... (and possibly a header |
| 1866 | # comment) in auto.conf |
| 1867 | continue |
| 1868 | |
| 1869 | name, val = match.groups() |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1870 | if name in self.syms: |
| 1871 | sym = self.syms[name] |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1872 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1873 | if sym.orig_type is STRING: |
| 1874 | match = _conf_string_match(val) |
| 1875 | if not match: |
| 1876 | continue |
| 1877 | val = unescape(match.group(1)) |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1878 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1879 | self.syms[name]._old_val = val |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1880 | else: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1881 | # Flag that the symbol no longer exists, in |
| 1882 | # case something still depends on it |
| 1883 | _touch_dep_file(path, name) |
| 1884 | |
| 1885 | def _write_old_vals(self, path): |
| 1886 | # Helper for writing auto.conf. Basically just a simplified |
| 1887 | # write_config() that doesn't write any comments (including |
| 1888 | # '# CONFIG_FOO is not set' comments). The format matches the C |
| 1889 | # implementation, though the ordering is arbitrary there (depends on |
| 1890 | # the hash table implementation). |
| 1891 | # |
| 1892 | # A separate helper function is neater than complicating write_config() |
| 1893 | # by passing a flag to it, plus we only need to look at symbols here. |
| 1894 | |
| 1895 | self._write_if_changed( |
| 1896 | os.path.join(path, "auto.conf"), |
| 1897 | self._old_vals_contents()) |
| 1898 | |
| 1899 | def _old_vals_contents(self): |
| 1900 | # _write_old_vals() helper. Returns the contents to write as a string. |
| 1901 | |
| 1902 | # Temporary list instead of generator makes this a bit faster |
| 1903 | return "".join([ |
| 1904 | sym.config_string for sym in self.unique_defined_syms |
| 1905 | if not (sym.orig_type in _BOOL_TRISTATE and not sym.tri_value) |
| 1906 | ]) |
| 1907 | |
| 1908 | def node_iter(self, unique_syms=False): |
| 1909 | """ |
| 1910 | Returns a generator for iterating through all MenuNode's in the Kconfig |
| 1911 | tree. The iteration is done in Kconfig definition order (each node is |
| 1912 | visited before its children, and the children of a node are visited |
| 1913 | before the next node). |
| 1914 | |
| 1915 | The Kconfig.top_node menu node is skipped. It contains an implicit menu |
| 1916 | that holds the top-level items. |
| 1917 | |
| 1918 | As an example, the following code will produce a list equal to |
| 1919 | Kconfig.defined_syms: |
| 1920 | |
| 1921 | defined_syms = [node.item for node in kconf.node_iter() |
| 1922 | if isinstance(node.item, Symbol)] |
| 1923 | |
| 1924 | unique_syms (default: False): |
| 1925 | If True, only the first MenuNode will be included for symbols defined |
| 1926 | in multiple locations. |
| 1927 | |
| 1928 | Using kconf.node_iter(True) in the example above would give a list |
| 1929 | equal to unique_defined_syms. |
| 1930 | """ |
| 1931 | if unique_syms: |
| 1932 | for sym in self.unique_defined_syms: |
| 1933 | sym._visited = False |
| 1934 | |
| 1935 | node = self.top_node |
| 1936 | while 1: |
| 1937 | # Jump to the next node with an iterative tree walk |
| 1938 | if node.list: |
| 1939 | node = node.list |
| 1940 | elif node.next: |
| 1941 | node = node.next |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1942 | else: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1943 | while node.parent: |
| 1944 | node = node.parent |
| 1945 | if node.next: |
| 1946 | node = node.next |
| 1947 | break |
| 1948 | else: |
| 1949 | # No more nodes |
| 1950 | return |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1951 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1952 | if unique_syms and node.item.__class__ is Symbol: |
| 1953 | if node.item._visited: |
| 1954 | continue |
| 1955 | node.item._visited = True |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1956 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1957 | yield node |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1958 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1959 | def eval_string(self, s): |
| 1960 | """ |
| 1961 | Returns the tristate value of the expression 's', represented as 0, 1, |
| 1962 | and 2 for n, m, and y, respectively. Raises KconfigError on syntax |
| 1963 | errors. Warns if undefined symbols are referenced. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1964 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1965 | As an example, if FOO and BAR are tristate symbols at least one of |
| 1966 | which has the value y, then eval_string("y && (FOO || BAR)") returns |
| 1967 | 2 (y). |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1968 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1969 | To get the string value of non-bool/tristate symbols, use |
| 1970 | Symbol.str_value. eval_string() always returns a tristate value, and |
| 1971 | all non-bool/tristate symbols have the tristate value 0 (n). |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1972 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1973 | The expression parsing is consistent with how parsing works for |
| 1974 | conditional ('if ...') expressions in the configuration, and matches |
| 1975 | the C implementation. m is rewritten to 'm && MODULES', so |
| 1976 | eval_string("m") will return 0 (n) unless modules are enabled. |
| 1977 | """ |
| 1978 | # The parser is optimized to be fast when parsing Kconfig files (where |
| 1979 | # an expression can never appear at the beginning of a line). We have |
| 1980 | # to monkey-patch things a bit here to reuse it. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1981 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1982 | self.filename = None |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1983 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1984 | self._tokens = self._tokenize("if " + s) |
| 1985 | # Strip "if " to avoid giving confusing error messages |
| 1986 | self._line = s |
| 1987 | self._tokens_i = 1 # Skip the 'if' token |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 1988 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1989 | return expr_value(self._expect_expr_and_eol()) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 1990 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1991 | def unset_values(self): |
| 1992 | """ |
| 1993 | Removes any user values from all symbols, as if Kconfig.load_config() |
| 1994 | or Symbol.set_value() had never been called. |
| 1995 | """ |
| 1996 | self._warn_assign_no_prompt = False |
| 1997 | try: |
| 1998 | # set_value() already rejects undefined symbols, and they don't |
| 1999 | # need to be invalidated (because their value never changes), so we |
| 2000 | # can just iterate over defined symbols |
| 2001 | for sym in self.unique_defined_syms: |
| 2002 | sym.unset_value() |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2003 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2004 | for choice in self.unique_choices: |
| 2005 | choice.unset_value() |
| 2006 | finally: |
| 2007 | self._warn_assign_no_prompt = True |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2008 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2009 | def enable_warnings(self): |
| 2010 | """ |
| 2011 | Do 'Kconfig.warn = True' instead. Maintained for backwards |
| 2012 | compatibility. |
| 2013 | """ |
| 2014 | self.warn = True |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 2015 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2016 | def disable_warnings(self): |
| 2017 | """ |
| 2018 | Do 'Kconfig.warn = False' instead. Maintained for backwards |
| 2019 | compatibility. |
| 2020 | """ |
| 2021 | self.warn = False |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2022 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2023 | def enable_stderr_warnings(self): |
| 2024 | """ |
| 2025 | Do 'Kconfig.warn_to_stderr = True' instead. Maintained for backwards |
| 2026 | compatibility. |
| 2027 | """ |
| 2028 | self.warn_to_stderr = True |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2029 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2030 | def disable_stderr_warnings(self): |
| 2031 | """ |
| 2032 | Do 'Kconfig.warn_to_stderr = False' instead. Maintained for backwards |
| 2033 | compatibility. |
| 2034 | """ |
| 2035 | self.warn_to_stderr = False |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2036 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2037 | def enable_undef_warnings(self): |
| 2038 | """ |
| 2039 | Do 'Kconfig.warn_assign_undef = True' instead. Maintained for backwards |
| 2040 | compatibility. |
| 2041 | """ |
| 2042 | self.warn_assign_undef = True |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2043 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2044 | def disable_undef_warnings(self): |
| 2045 | """ |
| 2046 | Do 'Kconfig.warn_assign_undef = False' instead. Maintained for |
| 2047 | backwards compatibility. |
| 2048 | """ |
| 2049 | self.warn_assign_undef = False |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2050 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2051 | def enable_override_warnings(self): |
| 2052 | """ |
| 2053 | Do 'Kconfig.warn_assign_override = True' instead. Maintained for |
| 2054 | backwards compatibility. |
| 2055 | """ |
| 2056 | self.warn_assign_override = True |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2057 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2058 | def disable_override_warnings(self): |
| 2059 | """ |
| 2060 | Do 'Kconfig.warn_assign_override = False' instead. Maintained for |
| 2061 | backwards compatibility. |
| 2062 | """ |
| 2063 | self.warn_assign_override = False |
| 2064 | |
| 2065 | def enable_redun_warnings(self): |
| 2066 | """ |
| 2067 | Do 'Kconfig.warn_assign_redun = True' instead. Maintained for backwards |
| 2068 | compatibility. |
| 2069 | """ |
| 2070 | self.warn_assign_redun = True |
| 2071 | |
| 2072 | def disable_redun_warnings(self): |
| 2073 | """ |
| 2074 | Do 'Kconfig.warn_assign_redun = False' instead. Maintained for |
| 2075 | backwards compatibility. |
| 2076 | """ |
| 2077 | self.warn_assign_redun = False |
| 2078 | |
| 2079 | def __repr__(self): |
| 2080 | """ |
| 2081 | Returns a string with information about the Kconfig object when it is |
| 2082 | evaluated on e.g. the interactive Python prompt. |
| 2083 | """ |
| 2084 | def status(flag): |
| 2085 | return "enabled" if flag else "disabled" |
| 2086 | |
| 2087 | return "<{}>".format(", ".join(( |
| 2088 | "configuration with {} symbols".format(len(self.syms)), |
| 2089 | 'main menu prompt "{}"'.format(self.mainmenu_text), |
| 2090 | "srctree is current directory" if not self.srctree else |
| 2091 | 'srctree "{}"'.format(self.srctree), |
| 2092 | 'config symbol prefix "{}"'.format(self.config_prefix), |
| 2093 | "warnings " + status(self.warn), |
| 2094 | "printing of warnings to stderr " + status(self.warn_to_stderr), |
| 2095 | "undef. symbol assignment warnings " + |
| 2096 | status(self.warn_assign_undef), |
| 2097 | "overriding symbol assignment warnings " + |
| 2098 | status(self.warn_assign_override), |
| 2099 | "redundant symbol assignment warnings " + |
| 2100 | status(self.warn_assign_redun) |
| 2101 | ))) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2102 | |
| 2103 | # |
| 2104 | # Private methods |
| 2105 | # |
| 2106 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2107 | |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2108 | # |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2109 | # File reading |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2110 | # |
| 2111 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2112 | def _open_config(self, filename): |
| 2113 | # Opens a .config file. First tries to open 'filename', then |
| 2114 | # '$srctree/filename' if $srctree was set when the configuration was |
| 2115 | # loaded. |
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 | try: |
| 2118 | return self._open(filename, "r") |
| 2119 | except EnvironmentError as e: |
| 2120 | # This will try opening the same file twice if $srctree is unset, |
| 2121 | # but it's not a big deal |
| 2122 | try: |
| 2123 | return self._open(join(self.srctree, filename), "r") |
| 2124 | except EnvironmentError as e2: |
| 2125 | # This is needed for Python 3, because e2 is deleted after |
| 2126 | # the try block: |
| 2127 | # |
| 2128 | # https://docs.python.org/3/reference/compound_stmts.html#the-try-statement |
| 2129 | e = e2 |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2130 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2131 | raise _KconfigIOError( |
| 2132 | e, "Could not open '{}' ({}: {}). Check that the $srctree " |
| 2133 | "environment variable ({}) is set correctly." |
| 2134 | .format(filename, errno.errorcode[e.errno], e.strerror, |
| 2135 | "set to '{}'".format(self.srctree) if self.srctree |
| 2136 | else "unset or blank")) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2137 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2138 | def _enter_file(self, filename): |
| 2139 | # Jumps to the beginning of a sourced Kconfig file, saving the previous |
| 2140 | # position and file object. |
| 2141 | # |
| 2142 | # filename: |
| 2143 | # Absolute path to file |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2144 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2145 | # Path relative to $srctree, stored in e.g. self.filename (which makes |
| 2146 | # it indirectly show up in MenuNode.filename). Equals 'filename' for |
| 2147 | # absolute paths passed to 'source'. |
| 2148 | if filename.startswith(self._srctree_prefix): |
| 2149 | # Relative path (or a redundant absolute path to within $srctree, |
| 2150 | # but it's probably fine to reduce those too) |
| 2151 | rel_filename = filename[len(self._srctree_prefix):] |
| 2152 | else: |
| 2153 | # Absolute path |
| 2154 | rel_filename = filename |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2155 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2156 | self.kconfig_filenames.append(rel_filename) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2157 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2158 | # The parent Kconfig files are represented as a list of |
| 2159 | # (<include path>, <Python 'file' object for Kconfig file>) tuples. |
| 2160 | # |
| 2161 | # <include path> is immutable and holds a *tuple* of |
| 2162 | # (<filename>, <linenr>) tuples, giving the locations of the 'source' |
| 2163 | # statements in the parent Kconfig files. The current include path is |
| 2164 | # also available in Kconfig._include_path. |
| 2165 | # |
| 2166 | # The point of this redundant setup is to allow Kconfig._include_path |
| 2167 | # to be assigned directly to MenuNode.include_path without having to |
| 2168 | # copy it, sharing it wherever possible. |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2169 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2170 | # Save include path and 'file' object (via its 'readline' function) |
| 2171 | # before entering the file |
| 2172 | self._filestack.append((self._include_path, self._readline)) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2173 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2174 | # _include_path is a tuple, so this rebinds the variable instead of |
| 2175 | # doing in-place modification |
| 2176 | self._include_path += ((self.filename, self.linenr),) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2177 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2178 | # Check for recursive 'source' |
| 2179 | for name, _ in self._include_path: |
| 2180 | if name == rel_filename: |
| 2181 | raise KconfigError( |
| 2182 | "\n{}:{}: recursive 'source' of '{}' detected. Check that " |
| 2183 | "environment variables are set correctly.\n" |
| 2184 | "Include path:\n{}" |
| 2185 | .format(self.filename, self.linenr, rel_filename, |
| 2186 | "\n".join("{}:{}".format(name, linenr) |
| 2187 | for name, linenr in self._include_path))) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2188 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2189 | try: |
| 2190 | self._readline = self._open(filename, "r").readline |
| 2191 | except EnvironmentError as e: |
| 2192 | # We already know that the file exists |
| 2193 | raise _KconfigIOError( |
| 2194 | e, "{}:{}: Could not open '{}' (in '{}') ({}: {})" |
| 2195 | .format(self.filename, self.linenr, filename, |
| 2196 | self._line.strip(), |
| 2197 | errno.errorcode[e.errno], e.strerror)) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2198 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2199 | self.filename = rel_filename |
| 2200 | self.linenr = 0 |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2201 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2202 | def _leave_file(self): |
| 2203 | # Returns from a Kconfig file to the file that sourced it. See |
| 2204 | # _enter_file(). |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2205 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2206 | # Restore location from parent Kconfig file |
| 2207 | self.filename, self.linenr = self._include_path[-1] |
| 2208 | # Restore include path and 'file' object |
| 2209 | self._readline.__self__.close() # __self__ fetches the 'file' object |
| 2210 | self._include_path, self._readline = self._filestack.pop() |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2211 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2212 | def _next_line(self): |
| 2213 | # Fetches and tokenizes the next line from the current Kconfig file. |
| 2214 | # Returns False at EOF and True otherwise. |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2215 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2216 | # We might already have tokens from parsing a line and discovering that |
| 2217 | # it's part of a different construct |
| 2218 | if self._reuse_tokens: |
| 2219 | self._reuse_tokens = False |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2220 | # self._tokens_i is known to be 1 here, because _parse_props() |
| 2221 | # leaves it like that when it can't recognize a line (or parses a |
| 2222 | # help text) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2223 | return True |
Ulf Magnusson | 4e1102f | 2017-09-19 12:52:55 +0200 | [diff] [blame] | 2224 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2225 | # readline() returns '' over and over at EOF, which we rely on for help |
| 2226 | # texts at the end of files (see _line_after_help()) |
| 2227 | line = self._readline() |
| 2228 | if not line: |
| 2229 | return False |
| 2230 | self.linenr += 1 |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2231 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2232 | # Handle line joining |
| 2233 | while line.endswith("\\\n"): |
| 2234 | line = line[:-2] + self._readline() |
| 2235 | self.linenr += 1 |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2236 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2237 | self._tokens = self._tokenize(line) |
| 2238 | # Initialize to 1 instead of 0 to factor out code from _parse_block() |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2239 | # and _parse_props(). They immediately fetch self._tokens[0]. |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2240 | self._tokens_i = 1 |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2241 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2242 | return True |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2243 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2244 | def _line_after_help(self, line): |
| 2245 | # Tokenizes a line after a help text. This case is special in that the |
| 2246 | # line has already been fetched (to discover that it isn't part of the |
| 2247 | # help text). |
| 2248 | # |
| 2249 | # An earlier version used a _saved_line variable instead that was |
| 2250 | # checked in _next_line(). This special-casing gets rid of it and makes |
| 2251 | # _reuse_tokens alone sufficient to handle unget. |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 2252 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2253 | # Handle line joining |
| 2254 | while line.endswith("\\\n"): |
| 2255 | line = line[:-2] + self._readline() |
| 2256 | self.linenr += 1 |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 2257 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2258 | self._tokens = self._tokenize(line) |
| 2259 | self._reuse_tokens = True |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 2260 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2261 | def _write_if_changed(self, filename, contents): |
| 2262 | # Writes 'contents' into 'filename', but only if it differs from the |
| 2263 | # current contents of the file. |
| 2264 | # |
| 2265 | # Another variant would be write a temporary file on the same |
| 2266 | # filesystem, compare the files, and rename() the temporary file if it |
| 2267 | # differs, but it breaks stuff like write_config("/dev/null"), which is |
| 2268 | # used out there to force evaluation-related warnings to be generated. |
| 2269 | # This simple version is pretty failsafe and portable. |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2270 | # |
| 2271 | # Returns True if the file has changed and is updated, and False |
| 2272 | # otherwise. |
Ulf Magnusson | 4e1102f | 2017-09-19 12:52:55 +0200 | [diff] [blame] | 2273 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2274 | if self._contents_eq(filename, contents): |
| 2275 | return False |
| 2276 | with self._open(filename, "w") as f: |
| 2277 | f.write(contents) |
| 2278 | return True |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 2279 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2280 | def _contents_eq(self, filename, contents): |
| 2281 | # Returns True if the contents of 'filename' is 'contents' (a string), |
| 2282 | # and False otherwise (including if 'filename' can't be opened/read) |
Ulf Magnusson | 4e1102f | 2017-09-19 12:52:55 +0200 | [diff] [blame] | 2283 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2284 | try: |
| 2285 | with self._open(filename, "r") as f: |
| 2286 | # Robust re. things like encoding and line endings (mmap() |
| 2287 | # trickery isn't) |
| 2288 | return f.read(len(contents) + 1) == contents |
| 2289 | except EnvironmentError: |
| 2290 | # If the error here would prevent writing the file as well, we'll |
| 2291 | # notice it later |
| 2292 | return False |
Ulf Magnusson | 4e1102f | 2017-09-19 12:52:55 +0200 | [diff] [blame] | 2293 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2294 | # |
| 2295 | # Tokenization |
| 2296 | # |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2297 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2298 | def _lookup_sym(self, name): |
| 2299 | # Fetches the symbol 'name' from the symbol table, creating and |
| 2300 | # registering it if it does not exist. If '_parsing_kconfigs' is False, |
| 2301 | # it means we're in eval_string(), and new symbols won't be registered. |
| 2302 | |
| 2303 | if name in self.syms: |
| 2304 | return self.syms[name] |
| 2305 | |
| 2306 | sym = Symbol() |
| 2307 | sym.kconfig = self |
| 2308 | sym.name = name |
| 2309 | sym.is_constant = False |
| 2310 | sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n |
| 2311 | |
| 2312 | if self._parsing_kconfigs: |
| 2313 | self.syms[name] = sym |
| 2314 | else: |
| 2315 | self._warn("no symbol {} in configuration".format(name)) |
| 2316 | |
| 2317 | return sym |
| 2318 | |
| 2319 | def _lookup_const_sym(self, name): |
| 2320 | # Like _lookup_sym(), for constant (quoted) symbols |
| 2321 | |
| 2322 | if name in self.const_syms: |
| 2323 | return self.const_syms[name] |
| 2324 | |
| 2325 | sym = Symbol() |
| 2326 | sym.kconfig = self |
| 2327 | sym.name = name |
| 2328 | sym.is_constant = True |
| 2329 | sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n |
| 2330 | |
| 2331 | if self._parsing_kconfigs: |
| 2332 | self.const_syms[name] = sym |
| 2333 | |
| 2334 | return sym |
| 2335 | |
| 2336 | def _tokenize(self, s): |
| 2337 | # Parses 's', returning a None-terminated list of tokens. Registers any |
| 2338 | # new symbols encountered with _lookup(_const)_sym(). |
| 2339 | # |
| 2340 | # Tries to be reasonably speedy by processing chunks of text via |
| 2341 | # regexes and string operations where possible. This is the biggest |
| 2342 | # hotspot during parsing. |
| 2343 | # |
| 2344 | # It might be possible to rewrite this to 'yield' tokens instead, |
| 2345 | # working across multiple lines. Lookback and compatibility with old |
| 2346 | # janky versions of the C tools complicate things though. |
| 2347 | |
| 2348 | self._line = s # Used for error reporting |
| 2349 | |
| 2350 | # Initial token on the line |
| 2351 | match = _command_match(s) |
| 2352 | if not match: |
| 2353 | if s.isspace() or s.lstrip().startswith("#"): |
| 2354 | return (None,) |
| 2355 | self._parse_error("unknown token at start of line") |
| 2356 | |
| 2357 | # Tricky implementation detail: While parsing a token, 'token' refers |
| 2358 | # to the previous token. See _STRING_LEX for why this is needed. |
| 2359 | token = _get_keyword(match.group(1)) |
| 2360 | if not token: |
| 2361 | # Backwards compatibility with old versions of the C tools, which |
| 2362 | # (accidentally) accepted stuff like "--help--" and "-help---". |
| 2363 | # This was fixed in the C tools by commit c2264564 ("kconfig: warn |
| 2364 | # of unhandled characters in Kconfig commands"), committed in July |
| 2365 | # 2015, but it seems people still run Kconfiglib on older kernels. |
| 2366 | if s.strip(" \t\n-") == "help": |
| 2367 | return (_T_HELP, None) |
| 2368 | |
| 2369 | # If the first token is not a keyword (and not a weird help token), |
| 2370 | # we have a preprocessor variable assignment (or a bare macro on a |
| 2371 | # line) |
| 2372 | self._parse_assignment(s) |
| 2373 | return (None,) |
| 2374 | |
| 2375 | tokens = [token] |
| 2376 | # The current index in the string being tokenized |
| 2377 | i = match.end() |
| 2378 | |
| 2379 | # Main tokenization loop (for tokens past the first one) |
| 2380 | while i < len(s): |
| 2381 | # Test for an identifier/keyword first. This is the most common |
| 2382 | # case. |
| 2383 | match = _id_keyword_match(s, i) |
| 2384 | if match: |
| 2385 | # We have an identifier or keyword |
| 2386 | |
| 2387 | # Check what it is. lookup_sym() will take care of allocating |
| 2388 | # new symbols for us the first time we see them. Note that |
| 2389 | # 'token' still refers to the previous token. |
| 2390 | |
| 2391 | name = match.group(1) |
| 2392 | keyword = _get_keyword(name) |
| 2393 | if keyword: |
| 2394 | # It's a keyword |
| 2395 | token = keyword |
| 2396 | # Jump past it |
| 2397 | i = match.end() |
| 2398 | |
| 2399 | elif token not in _STRING_LEX: |
| 2400 | # It's a non-const symbol, except we translate n, m, and y |
| 2401 | # into the corresponding constant symbols, like the C |
| 2402 | # implementation |
| 2403 | |
| 2404 | if "$" in name: |
| 2405 | # Macro expansion within symbol name |
| 2406 | name, s, i = self._expand_name(s, i) |
| 2407 | else: |
| 2408 | i = match.end() |
| 2409 | |
| 2410 | token = self.const_syms[name] if name in STR_TO_TRI else \ |
| 2411 | self._lookup_sym(name) |
| 2412 | |
Ulf Magnusson | 90c36d8 | 2015-08-13 19:55:40 +0200 | [diff] [blame] | 2413 | else: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2414 | # It's a case of missing quotes. For example, the |
| 2415 | # following is accepted: |
| 2416 | # |
| 2417 | # menu unquoted_title |
| 2418 | # |
| 2419 | # config A |
| 2420 | # tristate unquoted_prompt |
| 2421 | # |
| 2422 | # endmenu |
| 2423 | # |
| 2424 | # Named choices ('choice FOO') also end up here. |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2425 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2426 | if token is not _T_CHOICE: |
| 2427 | self._warn("style: quotes recommended around '{}' in '{}'" |
| 2428 | .format(name, self._line.strip()), |
| 2429 | self.filename, self.linenr) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2430 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2431 | token = name |
| 2432 | i = match.end() |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2433 | |
| 2434 | else: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2435 | # Neither a keyword nor a non-const symbol |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2436 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2437 | # We always strip whitespace after tokens, so it is safe to |
| 2438 | # assume that s[i] is the start of a token here. |
| 2439 | c = s[i] |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2440 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2441 | if c in "\"'": |
| 2442 | if "$" not in s and "\\" not in s: |
| 2443 | # Fast path for lines without $ and \. Find the |
| 2444 | # matching quote. |
| 2445 | end_i = s.find(c, i + 1) + 1 |
| 2446 | if not end_i: |
| 2447 | self._parse_error("unterminated string") |
| 2448 | val = s[i + 1:end_i - 1] |
| 2449 | i = end_i |
| 2450 | else: |
| 2451 | # Slow path |
| 2452 | s, end_i = self._expand_str(s, i) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2453 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2454 | # os.path.expandvars() and the $UNAME_RELEASE replace() |
| 2455 | # is a backwards compatibility hack, which should be |
| 2456 | # reasonably safe as expandvars() leaves references to |
| 2457 | # undefined env. vars. as is. |
| 2458 | # |
| 2459 | # The preprocessor functionality changed how |
| 2460 | # environment variables are referenced, to $(FOO). |
| 2461 | val = expandvars(s[i + 1:end_i - 1] |
| 2462 | .replace("$UNAME_RELEASE", |
| 2463 | _UNAME_RELEASE)) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2464 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2465 | i = end_i |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2466 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2467 | # This is the only place where we don't survive with a |
| 2468 | # single token of lookback: 'option env="FOO"' does not |
| 2469 | # refer to a constant symbol named "FOO". |
| 2470 | token = \ |
| 2471 | val if token in _STRING_LEX or tokens[0] is _T_OPTION \ |
| 2472 | else self._lookup_const_sym(val) |
| 2473 | |
| 2474 | elif s.startswith("&&", i): |
| 2475 | token = _T_AND |
| 2476 | i += 2 |
| 2477 | |
| 2478 | elif s.startswith("||", i): |
| 2479 | token = _T_OR |
| 2480 | i += 2 |
| 2481 | |
| 2482 | elif c == "=": |
| 2483 | token = _T_EQUAL |
| 2484 | i += 1 |
| 2485 | |
| 2486 | elif s.startswith("!=", i): |
| 2487 | token = _T_UNEQUAL |
| 2488 | i += 2 |
| 2489 | |
| 2490 | elif c == "!": |
| 2491 | token = _T_NOT |
| 2492 | i += 1 |
| 2493 | |
| 2494 | elif c == "(": |
| 2495 | token = _T_OPEN_PAREN |
| 2496 | i += 1 |
| 2497 | |
| 2498 | elif c == ")": |
| 2499 | token = _T_CLOSE_PAREN |
| 2500 | i += 1 |
| 2501 | |
| 2502 | elif c == "#": |
| 2503 | break |
| 2504 | |
| 2505 | |
| 2506 | # Very rare |
| 2507 | |
| 2508 | elif s.startswith("<=", i): |
| 2509 | token = _T_LESS_EQUAL |
| 2510 | i += 2 |
| 2511 | |
| 2512 | elif c == "<": |
| 2513 | token = _T_LESS |
| 2514 | i += 1 |
| 2515 | |
| 2516 | elif s.startswith(">=", i): |
| 2517 | token = _T_GREATER_EQUAL |
| 2518 | i += 2 |
| 2519 | |
| 2520 | elif c == ">": |
| 2521 | token = _T_GREATER |
| 2522 | i += 1 |
| 2523 | |
| 2524 | |
| 2525 | else: |
| 2526 | self._parse_error("unknown tokens in line") |
| 2527 | |
| 2528 | |
| 2529 | # Skip trailing whitespace |
| 2530 | while i < len(s) and s[i].isspace(): |
| 2531 | i += 1 |
| 2532 | |
| 2533 | |
| 2534 | # Add the token |
| 2535 | tokens.append(token) |
| 2536 | |
| 2537 | # None-terminating the token list makes token fetching simpler/faster |
| 2538 | tokens.append(None) |
| 2539 | |
| 2540 | return tokens |
| 2541 | |
| 2542 | # Helpers for syntax checking and token fetching. See the |
| 2543 | # 'Intro to expressions' section for what a constant symbol is. |
| 2544 | # |
| 2545 | # More of these could be added, but the single-use cases are inlined as an |
| 2546 | # optimization. |
| 2547 | |
| 2548 | def _expect_sym(self): |
| 2549 | token = self._tokens[self._tokens_i] |
| 2550 | self._tokens_i += 1 |
| 2551 | |
| 2552 | if token.__class__ is not Symbol: |
| 2553 | self._parse_error("expected symbol") |
| 2554 | |
| 2555 | return token |
| 2556 | |
| 2557 | def _expect_nonconst_sym(self): |
| 2558 | # Used for 'select' and 'imply' only. We know the token indices. |
| 2559 | |
| 2560 | token = self._tokens[1] |
| 2561 | self._tokens_i = 2 |
| 2562 | |
| 2563 | if token.__class__ is not Symbol or token.is_constant: |
| 2564 | self._parse_error("expected nonconstant symbol") |
| 2565 | |
| 2566 | return token |
| 2567 | |
| 2568 | def _expect_str_and_eol(self): |
| 2569 | token = self._tokens[self._tokens_i] |
| 2570 | self._tokens_i += 1 |
| 2571 | |
| 2572 | if token.__class__ is not str: |
| 2573 | self._parse_error("expected string") |
| 2574 | |
| 2575 | if self._tokens[self._tokens_i] is not None: |
| 2576 | self._trailing_tokens_error() |
| 2577 | |
| 2578 | return token |
| 2579 | |
| 2580 | def _expect_expr_and_eol(self): |
| 2581 | expr = self._parse_expr(True) |
| 2582 | |
| 2583 | if self._tokens[self._tokens_i] is not None: |
| 2584 | self._trailing_tokens_error() |
| 2585 | |
| 2586 | return expr |
| 2587 | |
| 2588 | def _check_token(self, token): |
| 2589 | # If the next token is 'token', removes it and returns True |
| 2590 | |
| 2591 | if self._tokens[self._tokens_i] is token: |
| 2592 | self._tokens_i += 1 |
| 2593 | return True |
| 2594 | return False |
| 2595 | |
| 2596 | # |
| 2597 | # Preprocessor logic |
| 2598 | # |
| 2599 | |
| 2600 | def _parse_assignment(self, s): |
| 2601 | # Parses a preprocessor variable assignment, registering the variable |
| 2602 | # if it doesn't already exist. Also takes care of bare macros on lines |
| 2603 | # (which are allowed, and can be useful for their side effects). |
| 2604 | |
| 2605 | # Expand any macros in the left-hand side of the assignment (the |
| 2606 | # variable name) |
| 2607 | s = s.lstrip() |
| 2608 | i = 0 |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2609 | while 1: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2610 | i = _assignment_lhs_fragment_match(s, i).end() |
| 2611 | if s.startswith("$(", i): |
| 2612 | s, i = self._expand_macro(s, i, ()) |
| 2613 | else: |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2614 | break |
| 2615 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2616 | if s.isspace(): |
| 2617 | # We also accept a bare macro on a line (e.g. |
| 2618 | # $(warning-if,$(foo),ops)), provided it expands to a blank string |
| 2619 | return |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2620 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2621 | # Assigned variable |
| 2622 | name = s[:i] |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2623 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2624 | |
| 2625 | # Extract assignment operator (=, :=, or +=) and value |
| 2626 | rhs_match = _assignment_rhs_match(s, i) |
| 2627 | if not rhs_match: |
| 2628 | self._parse_error("syntax error") |
| 2629 | |
| 2630 | op, val = rhs_match.groups() |
| 2631 | |
| 2632 | |
| 2633 | if name in self.variables: |
| 2634 | # Already seen variable |
| 2635 | var = self.variables[name] |
| 2636 | else: |
| 2637 | # New variable |
| 2638 | var = Variable() |
| 2639 | var.kconfig = self |
| 2640 | var.name = name |
| 2641 | var._n_expansions = 0 |
| 2642 | self.variables[name] = var |
| 2643 | |
| 2644 | # += acts like = on undefined variables (defines a recursive |
| 2645 | # variable) |
| 2646 | if op == "+=": |
| 2647 | op = "=" |
| 2648 | |
| 2649 | if op == "=": |
| 2650 | var.is_recursive = True |
| 2651 | var.value = val |
| 2652 | elif op == ":=": |
| 2653 | var.is_recursive = False |
| 2654 | var.value = self._expand_whole(val, ()) |
| 2655 | else: # op == "+=" |
| 2656 | # += does immediate expansion if the variable was last set |
| 2657 | # with := |
| 2658 | var.value += " " + (val if var.is_recursive else |
| 2659 | self._expand_whole(val, ())) |
| 2660 | |
| 2661 | def _expand_whole(self, s, args): |
| 2662 | # Expands preprocessor macros in all of 's'. Used whenever we don't |
| 2663 | # have to worry about delimiters. See _expand_macro() re. the 'args' |
| 2664 | # parameter. |
| 2665 | # |
| 2666 | # Returns the expanded string. |
| 2667 | |
| 2668 | i = 0 |
| 2669 | while 1: |
| 2670 | i = s.find("$(", i) |
| 2671 | if i == -1: |
| 2672 | break |
| 2673 | s, i = self._expand_macro(s, i, args) |
| 2674 | return s |
| 2675 | |
| 2676 | def _expand_name(self, s, i): |
| 2677 | # Expands a symbol name starting at index 'i' in 's'. |
| 2678 | # |
| 2679 | # Returns the expanded name, the expanded 's' (including the part |
| 2680 | # before the name), and the index of the first character in the next |
| 2681 | # token after the name. |
| 2682 | |
| 2683 | s, end_i = self._expand_name_iter(s, i) |
| 2684 | name = s[i:end_i] |
| 2685 | # isspace() is False for empty strings |
| 2686 | if not name.strip(): |
| 2687 | # Avoid creating a Kconfig symbol with a blank name. It's almost |
| 2688 | # guaranteed to be an error. |
| 2689 | self._parse_error("macro expanded to blank string") |
| 2690 | |
| 2691 | # Skip trailing whitespace |
| 2692 | while end_i < len(s) and s[end_i].isspace(): |
| 2693 | end_i += 1 |
| 2694 | |
| 2695 | return name, s, end_i |
| 2696 | |
| 2697 | def _expand_name_iter(self, s, i): |
| 2698 | # Expands a symbol name starting at index 'i' in 's'. |
| 2699 | # |
| 2700 | # Returns the expanded 's' (including the part before the name) and the |
| 2701 | # index of the first character after the expanded name in 's'. |
| 2702 | |
| 2703 | while 1: |
| 2704 | match = _name_special_search(s, i) |
| 2705 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2706 | if match.group() != "$(": |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2707 | return (s, match.start()) |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2708 | s, i = self._expand_macro(s, match.start(), ()) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2709 | |
| 2710 | def _expand_str(self, s, i): |
| 2711 | # Expands a quoted string starting at index 'i' in 's'. Handles both |
| 2712 | # backslash escapes and macro expansion. |
| 2713 | # |
| 2714 | # Returns the expanded 's' (including the part before the string) and |
| 2715 | # the index of the first character after the expanded string in 's'. |
| 2716 | |
| 2717 | quote = s[i] |
| 2718 | i += 1 # Skip over initial "/' |
| 2719 | while 1: |
| 2720 | match = _string_special_search(s, i) |
| 2721 | if not match: |
| 2722 | self._parse_error("unterminated string") |
| 2723 | |
| 2724 | |
| 2725 | if match.group() == quote: |
| 2726 | # Found the end of the string |
| 2727 | return (s, match.end()) |
| 2728 | |
| 2729 | elif match.group() == "\\": |
| 2730 | # Replace '\x' with 'x'. 'i' ends up pointing to the character |
| 2731 | # after 'x', which allows macros to be canceled with '\$(foo)'. |
| 2732 | i = match.end() |
| 2733 | s = s[:match.start()] + s[i:] |
| 2734 | |
| 2735 | elif match.group() == "$(": |
| 2736 | # A macro call within the string |
| 2737 | s, i = self._expand_macro(s, match.start(), ()) |
| 2738 | |
| 2739 | else: |
| 2740 | # A ' quote within " quotes or vice versa |
| 2741 | i += 1 |
| 2742 | |
| 2743 | def _expand_macro(self, s, i, args): |
| 2744 | # Expands a macro starting at index 'i' in 's'. If this macro resulted |
| 2745 | # from the expansion of another macro, 'args' holds the arguments |
| 2746 | # passed to that macro. |
| 2747 | # |
| 2748 | # Returns the expanded 's' (including the part before the macro) and |
| 2749 | # the index of the first character after the expanded macro in 's'. |
| 2750 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2751 | res = s[:i] |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2752 | i += 2 # Skip over "$(" |
| 2753 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2754 | arg_start = i # Start of current macro argument |
| 2755 | new_args = [] # Arguments of this macro call |
| 2756 | nesting = 0 # Current parentheses nesting level |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2757 | |
| 2758 | while 1: |
| 2759 | match = _macro_special_search(s, i) |
| 2760 | if not match: |
| 2761 | self._parse_error("missing end parenthesis in macro expansion") |
| 2762 | |
| 2763 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2764 | if match.group() == "(": |
| 2765 | nesting += 1 |
| 2766 | i = match.end() |
| 2767 | |
| 2768 | elif match.group() == ")": |
| 2769 | if nesting: |
| 2770 | nesting -= 1 |
| 2771 | i = match.end() |
| 2772 | continue |
| 2773 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2774 | # Found the end of the macro |
| 2775 | |
| 2776 | new_args.append(s[arg_start:match.start()]) |
| 2777 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2778 | # $(1) is replaced by the first argument to the function, etc., |
| 2779 | # provided at least that many arguments were passed |
| 2780 | |
| 2781 | try: |
| 2782 | # Does the macro look like an integer, with a corresponding |
| 2783 | # argument? If so, expand it to the value of the argument. |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2784 | res += args[int(new_args[0])] |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2785 | except (ValueError, IndexError): |
| 2786 | # Regular variables are just functions without arguments, |
| 2787 | # and also go through the function value path |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2788 | res += self._fn_val(new_args) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2789 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2790 | return (res + s[match.end():], len(res)) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2791 | |
| 2792 | elif match.group() == ",": |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2793 | i = match.end() |
| 2794 | if nesting: |
| 2795 | continue |
| 2796 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2797 | # Found the end of a macro argument |
| 2798 | new_args.append(s[arg_start:match.start()]) |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2799 | arg_start = i |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2800 | |
| 2801 | else: # match.group() == "$(" |
| 2802 | # A nested macro call within the macro |
| 2803 | s, i = self._expand_macro(s, match.start(), args) |
| 2804 | |
| 2805 | def _fn_val(self, args): |
| 2806 | # Returns the result of calling the function args[0] with the arguments |
| 2807 | # args[1..len(args)-1]. Plain variables are treated as functions |
| 2808 | # without arguments. |
| 2809 | |
| 2810 | fn = args[0] |
| 2811 | |
| 2812 | if fn in self.variables: |
| 2813 | var = self.variables[fn] |
| 2814 | |
| 2815 | if len(args) == 1: |
| 2816 | # Plain variable |
| 2817 | if var._n_expansions: |
| 2818 | self._parse_error("Preprocessor variable {} recursively " |
| 2819 | "references itself".format(var.name)) |
| 2820 | elif var._n_expansions > 100: |
| 2821 | # Allow functions to call themselves, but guess that functions |
| 2822 | # that are overly recursive are stuck |
| 2823 | self._parse_error("Preprocessor function {} seems stuck " |
| 2824 | "in infinite recursion".format(var.name)) |
| 2825 | |
| 2826 | var._n_expansions += 1 |
| 2827 | res = self._expand_whole(self.variables[fn].value, args) |
| 2828 | var._n_expansions -= 1 |
| 2829 | return res |
| 2830 | |
| 2831 | if fn in self._functions: |
| 2832 | # Built-in or user-defined function |
| 2833 | |
| 2834 | py_fn, min_arg, max_arg = self._functions[fn] |
| 2835 | |
| 2836 | if len(args) - 1 < min_arg or \ |
| 2837 | (max_arg is not None and len(args) - 1 > max_arg): |
| 2838 | |
| 2839 | if min_arg == max_arg: |
| 2840 | expected_args = min_arg |
| 2841 | elif max_arg is None: |
| 2842 | expected_args = "{} or more".format(min_arg) |
| 2843 | else: |
| 2844 | expected_args = "{}-{}".format(min_arg, max_arg) |
| 2845 | |
| 2846 | raise KconfigError("{}:{}: bad number of arguments in call " |
| 2847 | "to {}, expected {}, got {}" |
| 2848 | .format(self.filename, self.linenr, fn, |
| 2849 | expected_args, len(args) - 1)) |
| 2850 | |
| 2851 | return py_fn(self, *args) |
| 2852 | |
| 2853 | # Environment variables are tried last |
| 2854 | if fn in os.environ: |
| 2855 | self.env_vars.add(fn) |
| 2856 | return os.environ[fn] |
| 2857 | |
| 2858 | return "" |
| 2859 | |
| 2860 | # |
| 2861 | # Parsing |
| 2862 | # |
| 2863 | |
| 2864 | def _make_and(self, e1, e2): |
| 2865 | # Constructs an AND (&&) expression. Performs trivial simplification. |
| 2866 | |
| 2867 | if e1 is self.y: |
| 2868 | return e2 |
| 2869 | |
| 2870 | if e2 is self.y: |
| 2871 | return e1 |
| 2872 | |
| 2873 | if e1 is self.n or e2 is self.n: |
| 2874 | return self.n |
| 2875 | |
| 2876 | return (AND, e1, e2) |
| 2877 | |
| 2878 | def _make_or(self, e1, e2): |
| 2879 | # Constructs an OR (||) expression. Performs trivial simplification. |
| 2880 | |
| 2881 | if e1 is self.n: |
| 2882 | return e2 |
| 2883 | |
| 2884 | if e2 is self.n: |
| 2885 | return e1 |
| 2886 | |
| 2887 | if e1 is self.y or e2 is self.y: |
| 2888 | return self.y |
| 2889 | |
| 2890 | return (OR, e1, e2) |
| 2891 | |
| 2892 | def _parse_block(self, end_token, parent, prev): |
| 2893 | # Parses a block, which is the contents of either a file or an if, |
| 2894 | # menu, or choice statement. |
| 2895 | # |
| 2896 | # end_token: |
| 2897 | # The token that ends the block, e.g. _T_ENDIF ("endif") for ifs. |
| 2898 | # None for files. |
| 2899 | # |
| 2900 | # parent: |
| 2901 | # The parent menu node, corresponding to a menu, Choice, or 'if'. |
| 2902 | # 'if's are flattened after parsing. |
| 2903 | # |
| 2904 | # prev: |
| 2905 | # The previous menu node. New nodes will be added after this one (by |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2906 | # modifying 'next' pointers). |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2907 | # |
| 2908 | # 'prev' is reused to parse a list of child menu nodes (for a menu or |
| 2909 | # Choice): After parsing the children, the 'next' pointer is assigned |
| 2910 | # to the 'list' pointer to "tilt up" the children above the node. |
| 2911 | # |
| 2912 | # Returns the final menu node in the block (or 'prev' if the block is |
| 2913 | # empty). This allows chaining. |
| 2914 | |
| 2915 | while self._next_line(): |
| 2916 | t0 = self._tokens[0] |
| 2917 | |
| 2918 | if t0 is _T_CONFIG or t0 is _T_MENUCONFIG: |
| 2919 | # The tokenizer allocates Symbol objects for us |
| 2920 | sym = self._tokens[1] |
| 2921 | |
| 2922 | if sym.__class__ is not Symbol or sym.is_constant: |
| 2923 | self._parse_error("missing or bad symbol name") |
| 2924 | |
| 2925 | if self._tokens[2] is not None: |
| 2926 | self._trailing_tokens_error() |
| 2927 | |
| 2928 | self.defined_syms.append(sym) |
| 2929 | |
| 2930 | node = MenuNode() |
| 2931 | node.kconfig = self |
| 2932 | node.item = sym |
| 2933 | node.is_menuconfig = (t0 is _T_MENUCONFIG) |
| 2934 | node.prompt = node.help = node.list = None |
| 2935 | node.parent = parent |
| 2936 | node.filename = self.filename |
| 2937 | node.linenr = self.linenr |
| 2938 | node.include_path = self._include_path |
| 2939 | |
| 2940 | sym.nodes.append(node) |
| 2941 | |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2942 | self._parse_props(node) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2943 | |
| 2944 | if node.is_menuconfig and not node.prompt: |
| 2945 | self._warn("the menuconfig symbol {} has no prompt" |
Tom Rini | ddf34e7 | 2020-05-14 08:30:00 -0400 | [diff] [blame] | 2946 | .format(sym.name_and_loc)) |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2947 | |
| 2948 | # Equivalent to |
| 2949 | # |
| 2950 | # prev.next = node |
| 2951 | # prev = node |
| 2952 | # |
| 2953 | # due to tricky Python semantics. The order matters. |
| 2954 | prev.next = prev = node |
| 2955 | |
| 2956 | elif t0 is None: |
| 2957 | # Blank line |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2958 | continue |
| 2959 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2960 | elif t0 in _SOURCE_TOKENS: |
| 2961 | pattern = self._expect_str_and_eol() |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2962 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2963 | if t0 in _REL_SOURCE_TOKENS: |
| 2964 | # Relative source |
| 2965 | pattern = join(dirname(self.filename), pattern) |
Masahiro Yamada | f219e01 | 2014-09-01 19:57:37 +0900 | [diff] [blame] | 2966 | |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 2967 | # - glob() doesn't support globbing relative to a directory, so |
| 2968 | # we need to prepend $srctree to 'pattern'. Use join() |
| 2969 | # instead of '+' so that an absolute path in 'pattern' is |
| 2970 | # preserved. |
| 2971 | # |
| 2972 | # - Sort the glob results to ensure a consistent ordering of |
| 2973 | # Kconfig symbols, which indirectly ensures a consistent |
| 2974 | # ordering in e.g. .config files |
| 2975 | filenames = sorted(iglob(join(self._srctree_prefix, pattern))) |
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 | if not filenames and t0 in _OBL_SOURCE_TOKENS: |
| 2978 | raise KconfigError( |
| 2979 | "{}:{}: '{}' not found (in '{}'). Check that " |
| 2980 | "environment variables are set correctly (e.g. " |
| 2981 | "$srctree, which is {}). Also note that unset " |
| 2982 | "environment variables expand to the empty string." |
| 2983 | .format(self.filename, self.linenr, pattern, |
| 2984 | self._line.strip(), |
| 2985 | "set to '{}'".format(self.srctree) |
| 2986 | if self.srctree else "unset or blank")) |
| 2987 | |
| 2988 | for filename in filenames: |
| 2989 | self._enter_file(filename) |
| 2990 | prev = self._parse_block(None, parent, prev) |
| 2991 | self._leave_file() |
| 2992 | |
| 2993 | elif t0 is end_token: |
| 2994 | # Reached the end of the block. Terminate the final node and |
| 2995 | # return it. |
| 2996 | |
| 2997 | if self._tokens[1] is not None: |
| 2998 | self._trailing_tokens_error() |
| 2999 | |
| 3000 | prev.next = None |
| 3001 | return prev |
| 3002 | |
| 3003 | elif t0 is _T_IF: |
| 3004 | node = MenuNode() |
| 3005 | node.item = node.prompt = None |
| 3006 | node.parent = parent |
| 3007 | node.dep = self._expect_expr_and_eol() |
| 3008 | |
| 3009 | self._parse_block(_T_ENDIF, node, node) |
| 3010 | node.list = node.next<
|