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