summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2017-11-09 11:43:13 +0100
committerUlf Magnusson <ulfalizer@gmail.com>2017-11-09 11:43:13 +0100
commit395c2db0e9761def8eb992e3e8068ba2d3ab179c (patch)
tree7b14ac791dbf9d4b9354f1c6149444e090068309
parent8c978ee0b9c0f7f8406f58d24478a73330512056 (diff)
parent4bffd653148d6fa1c8e626872ae4f445e2b0a24c (diff)
Make Kconfiglib 2 official
Merge in the 'kconfiglib-2-backup' branch.
-rw-r--r--README.rst400
-rw-r--r--examples/Kmenuconfig102
-rw-r--r--examples/allnoconfig.py96
-rw-r--r--examples/allnoconfig_simpler.py34
-rw-r--r--examples/allyesconfig.py117
-rw-r--r--examples/defconfig.py15
-rw-r--r--examples/defconfig_oldconfig.py28
-rw-r--r--examples/eval_expr.py24
-rw-r--r--examples/find_symbol.py205
-rw-r--r--examples/help_grep.py109
l---------examples/kconfiglib.py1
-rw-r--r--examples/menuconfig.py348
-rw-r--r--examples/print_refs.py13
-rw-r--r--examples/print_sym_info.py61
-rw-r--r--examples/print_tree.py80
-rw-r--r--examples/print_undefined.py15
-rw-r--r--kconfiglib-2-changes.txt112
-rw-r--r--kconfiglib.py6464
-rw-r--r--makefile.patch20
-rw-r--r--tests/Kassignable229
-rw-r--r--tests/Kchoice26
-rw-r--r--tests/Kdefconfig_existent1
-rw-r--r--tests/Keval1
-rw-r--r--tests/Khelp46
-rw-r--r--tests/Klocation79
-rw-r--r--tests/Klocation_included43
-rw-r--r--tests/Kmisc5
-rw-r--r--tests/Kmodifiable50
-rw-r--r--tests/Kprompt77
-rw-r--r--tests/Krelation6
-rw-r--r--tests/Krepr61
-rw-r--r--tests/Kstr78
-rw-r--r--tests/Ktext145
-rw-r--r--tests/Kvisibility161
-rw-r--r--tests/Kwtf18
-rwxr-xr-xtests/reltest52
-rw-r--r--testsuite.py3210
37 files changed, 6845 insertions, 5687 deletions
diff --git a/README.rst b/README.rst
index 181c5c3..410c949 100644
--- a/README.rst
+++ b/README.rst
@@ -1,22 +1,126 @@
-Kconfiglib
-==========
+.. contents:: Table of contents
+ :backlinks: none
-A Python library for doing stuff with `Kconfig <https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt>`_-based
-configuration systems. Can extract information, query and set symbol values,
-and read and write ``.config`` files. Highly compatible with the
-``scripts/kconfig/*conf`` utilities in the kernel, usually invoked via make
-targets such as ``menuconfig`` and ``defconfig``.
+Overview
+--------
-Supports both Python 2 and Python 3 without modification, and should also run
-on non-\*nix platforms.
+*This is version 2 of Kconfiglib, which is not backwards-compatible with
+Kconfiglib 1. For a summary of changes between Kconfiglib 1 and Kconfiglib 2,
+see* |changes|_.
-*NOTE: A non-backwards-compatible 2.0 version will be coming soon, with a much cleaner and more general API (property-based,
-less redundant, more logical). That one will also expose the menu structure (including implicit menus) and all the
-expressions directly. The major version will be bumped to 2 in accordance with
-semantic versioning. See the kconfiglib-2-backup branch for a sneak peek (or if you feel like beta testing).*
+.. _changes: kconfiglib-2-changes.txt
+.. |changes| replace:: *kconfiglib-2-changes.txt*
-.. contents:: Table of contents
- :backlinks: none
+Kconfiglib is a Python 2/3 library for scripting and extracting information
+from `Kconfig
+<https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt>`_
+configuration systems. It can do the following, among other things:
+
+- **Programmatically get and set symbol values**
+
+ `allnoconfig.py <examples/allnoconfig.py>`_ and `allyesconfig.py
+ <examples/allyesconfig.py>`_ examples are provided, automatically verified to
+ produce identical output to the standard ``make allnoconfig`` and ``make
+ allyesconfig``.
+
+- **Read and write .config files**
+
+ The generated ``.config`` files are character-for-character identical to what
+ the C implementation would generate (except for the header comment). The test
+ suite relies on this, as it compares the generated files.
+
+- **Inspect symbols**
+
+ Printing a symbol gives output which could be fed back into a Kconfig parser
+ to redefine it***. The printing function (``__str__()``) is implemented with
+ public APIs, meaning you can fetch just whatever information you need as
+ well.
+
+ A helpful ``__repr__()`` is implemented on all objects too, also implemented
+ with public APIs.
+
+ \***Choice symbols get their parent choice as a dependency, which shows up as
+ e.g. ``prompt "choice symbol" if <choice>`` when printing the symbol. This
+ could easily be worked around if 100% reparsable output is needed.
+
+- **Inspect expressions**
+
+ Expressions use a simple tuple-based format that can be processed manually
+ if needed. Expression printing and evaluation functions are provided,
+ implemented with public APIs.
+
+- **Inspect the menu tree**
+
+ The underlying menu tree is exposed, including submenus created implicitly
+ from symbols depending on preceding symbols. This can be used e.g. to
+ implement menuconfig-like functionality.
+
+ See the `menuconfig.py <examples/menuconfig.py>`_ example.
+
+
+Here are some other features:
+
+- **Single-file implementation**
+
+ The entire library is contained in `kconfiglib.py <kconfiglib.py>`_.
+
+- **Runs unmodified under both Python 2 and Python 3**
+
+ The code mostly uses basic Python features and has no third-party
+ dependencies. The most advanced things used are probably ``@property`` and
+ ``__slots__``.
+
+- **Robust and highly compatible with the standard Kconfig C tools**
+
+ The test suite automatically compares output from Kconfiglib and the C tools
+ by diffing the generated ``.config`` files for the real kernel Kconfig and
+ defconfig files, for all ARCHes.
+
+ This currently involves comparing the output for 36 ARCHes and 498 defconfig
+ files (or over 18000 ARCH/defconfig combinations in "obsessive" test suite
+ mode). All tests are expected to pass.
+
+ A comprehensive suite of selftests is included as well.
+
+- **Not horribly slow despite being a pure Python implementation**
+
+ The `allyesconfig.py <examples/allyesconfig.py>`_ example currently runs in
+ about 1.6 seconds on a Core i7 2600K (with a warm file cache), where half a
+ second is overhead from ``make scriptconfig`` (see below).
+
+ For long-running jobs, `PyPy <https://pypy.org/>`_ gives a big performance
+ boost. CPython is faster for short-running jobs as PyPy needs some time to
+ warm up.
+
+- **Internals that (mostly) mirror the C implementation**
+
+ While being simpler to understand.
+
+Documentation
+-------------
+
+Kconfiglib comes with extensive documentation in the form of docstrings. To view it, run e.g.
+the following command:
+
+.. code:: sh
+
+ $ pydoc kconfiglib
+
+For HTML output, add ``-w``:
+
+.. code:: sh
+
+ $ pydoc -w kconfiglib
+
+A good starting point is to read the module docstring (which you could also just read directly
+at the beginning of `kconfiglib.py <kconfiglib.py>`_). It gives an introduction to symbol
+values, the menu tree, and expressions.
+
+After reading the module docstring, a good next step is to read the ``Kconfig`` class
+documentation, and then the documentation for the ``Symbol``, ``Choice``, and ``MenuNode``
+classes.
+
+Please tell me if something is unclear to you or can be explained better.
Installation
------------
@@ -37,77 +141,174 @@ All releases have a corresponding tag in the git repository, e.g. ``v1.0.6``.
Installation for the Linux kernel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-After installing with ``pip(3)``, apply ``makefile.patch``
-by running e.g. the following commands in the kernel root:
+See the module docstring at the top of `kconfiglib.py <kconfiglib.py>`_.
-.. code:: sh
+Manual installation
+~~~~~~~~~~~~~~~~~~~
-   $ wget https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch
- $ git am makefile.patch
+The entire library is contained in
+`kconfiglib.py <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_.
+Just drop it somewhere.
-If you do not wish to install anything, the following manual approach will work as well:
+Examples
+--------
-.. code:: sh
+Example scripts
+~~~~~~~~~~~~~~~
- $ git clone git://github.com/ulfalizer/Kconfiglib.git
- $ git am Kconfiglib/makefile.patch
+The ``examples/`` subdirectory contains some simple example scripts. Among these are the following ones:
-(Warning: The directory name ``Kconfiglib/`` is significant in this case, because it's added to ``PYTHONPATH`` by the new targets in ``makefile.patch``.)
+- `allnoconfig.py <examples/allnoconfig.py>`_, `allnoconfig_simpler.py <examples/allnoconfig_simpler.py>`_, and `allyesconfig.py <examples/allyesconfig.py>`_ implement ``make allnoconfig`` and ``make allyesconfig`` in various ways. Demonstrates menu tree walking and value setting.
-In addition to creating a handy interface, the make targets created by the
-patch (``scriptconfig`` and ``iscriptconfig``) are needed to pick up environment
-variables set in the kernel makefiles and later referenced in the Kconfig files
-(``ARCH``, ``SRCARCH``, and ``KERNELVERSION`` as of Linux v4.14.0-rc1).
-The documentation explains how the make targets are used. The compatibility
-tests in the test suite also needs them.
+- `defconfig.py <examples/defconfig.py>`_ has the same effect as going into ``make menuconfig`` and immediately saving and exiting.
-Please tell me if the patch does not apply. It should be trivial to apply
-manually, as it's just a block of text that needs to be inserted near the other
-``*conf:`` targets.
+- `eval_expr.py <examples/eval_expr.py>`_ evaluates an expression in the context of a configuration.
-Manual installation
-~~~~~~~~~~~~~~~~~~~
+- `find_symbol.py <examples/find_symbol.py>`_ searches through expressions to find references to a symbol, also printing a "backtrace" with parents for each reference found.
-The entire library is contained in
-`kconfiglib.py <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_.
-Just drop it somewhere.
+- `help_grep.py <examples/help_grep.py>`_ searches for a string in all help texts.
-Documentation
--------------
+- `print_tree.py <examples/print_tree.py>`_ prints a tree of all configuration items.
-The (extensive) documentation is generated by running the following command in the same
-directory as `kconfiglib.py <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_:
+- `menuconfig.py <examples/menuconfig.py>`_ implements a configuration interface that uses notation similar to ``make menuconfig``. It's deliberately kept as simple as possible to demonstrate just the core concepts, and isn't something you'd actually want to use. Here's a screenshot:
-.. code:: sh
+.. code-block::
- $ pydoc kconfiglib
+ ======== Example Kconfig configuration ========
-For HTML output, use
-
-.. code:: sh
+ [*] Enable loadable module support (MODULES)
+ Bool and tristate symbols
+ [*] Bool symbol (BOOL)
+ [ ] Dependent bool symbol (BOOL_DEP)
+ < > Dependent tristate symbol (TRI_DEP)
+ [ ] First prompt (TWO_MENU_NODES)
+ < > Tristate symbol (TRI)
+ [ ] Second prompt (TWO_MENU_NODES)
+ *** These are selected by TRI_DEP ***
+ < > Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
+ < > Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
+ String, int, and hex symbols
+ (foo) String symbol (STRING)
+ (747) Int symbol (INT)
+ (0xABC) Hex symbol (HEX)
+ Various choices
+ -*- Bool choice (BOOL_CHOICE)
+ --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
+ Bool choice sym 2 (BOOL_CHOICE_SYM_2)
+ {M} Tristate choice (TRI_CHOICE)
+ < > Tristate choice sym 1 (TRI_CHOICE_SYM_1)
+ < > Tristate choice sym 2 (TRI_CHOICE_SYM_2)
+ [ ] Optional bool choice (OPT_BOOL_CHOICE)
- $ pydoc -w kconfiglib
+ Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): BOOL
+ Value for BOOL (available: n, y): n
+ ...
-You could also browse the docstrings directly in
-`kconfiglib.py <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_.
+I'm not currently interested in implementing a (more usable) menuconfig myself, but all the infrastructure
+for a great one should be there if you want to give it a go. I'll help you out with any questions you might
+have.
-Please tell me if something is unclear to you or can be explained better. The Kconfig
-language has some dark corners.
+Real-world examples
+~~~~~~~~~~~~~~~~~~~
-Examples
---------
+These use the older Kconfiglib 1 API, which was clunkier and not as general (functions instead of properties, no direct access to the menu structure or properties, uglier ``__str__()`` output):
+
+- `genboardscfg.py <http://git.denx.de/?p=u-boot.git;a=blob;f=tools/genboardscfg.py;hb=HEAD>`_ from `Das U-Boot <http://www.denx.de/wiki/U-Boot>`_ generates some sort of legacy board database by pulling information from a newly added Kconfig-based configuration system (as far as I understand it :).
+
+- `gen-manual-lists.py <https://git.busybox.net/buildroot/tree/support/scripts/gen-manual-lists.py?id=5676a2deea896f38123b99781da0a612865adeb0>`_ generated listings for an appendix in the `Buildroot <https://buildroot.org>`_ manual. (The listing has since been removed.)
+
+- `gen_kconfig_doc.py <https://github.com/espressif/esp-idf/blob/master/docs/gen-kconfig-doc.py>`_ from the `esp-idf <https://github.com/espressif/esp-idf>`_ project generates documentation from Kconfig files.
+
+- `SConf <https://github.com/CoryXie/SConf>`_ builds an interactive configuration interface (like ``menuconfig``) on top of Kconfiglib, for use e.g. with `SCons <scons.org>`_.
-* The `examples/ <https://github.com/ulfalizer/Kconfiglib/tree/master/examples>`_ directory contains simple example scripts. See the documentation for how to run them.
+- `kconfig-diff.py <https://gist.github.com/dubiousjim/5638961>`_ -- a script by `dubiousjim <https://github.com/dubiousjim>`_ that compares kernel configurations.
-* `genboardscfg.py <http://git.denx.de/?p=u-boot.git;a=blob;f=tools/genboardscfg.py;hb=HEAD>`_ from `Das U-Boot <http://www.denx.de/wiki/U-Boot>`_ generates some sort of legacy board database by pulling information from a newly added Kconfig-based configuration system (as far as I understand it :).
+- Originally, Kconfiglib was used in chapter 4 of my `master's thesis <http://liu.diva-portal.org/smash/get/diva2:473038/FULLTEXT01.pdf>`_ to automatically generate a "minimal" kernel for a given system. Parts of it bother me a bit now, but that's how it goes with old work.
-* `gen-manual-lists.py <https://git.busybox.net/buildroot/tree/support/scripts/gen-manual-lists.py?id=5676a2deea896f38123b99781da0a612865adeb0>`_ generated listings for an appendix in the `Buildroot <https://buildroot.org>`_ manual. (The listing has since been removed.)
+Sample ``make iscriptconfig`` session
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-* `SConf <https://github.com/CoryXie/SConf>`_ builds an interactive configuration interface (like ``menuconfig``) on top of Kconfiglib, for use e.g. with `SCons <scons.org>`_.
+The following log should give some idea of the functionality available in the API:
-* `kconfig-diff.py <https://gist.github.com/dubiousjim/5638961>`_ -- a script by `dubiousjim <https://github.com/dubiousjim>`_ that compares kernel configurations.
+.. code-block::
-* Originally, Kconfiglib was used in chapter 4 of my `master's thesis <http://liu.diva-portal.org/smash/get/diva2:473038/FULLTEXT01.pdf>`_ to automatically generate a "minimal" kernel for a given system. Parts of it bother me a bit now, but that's how it goes with old work.
+ $ make iscriptconfig
+ A Kconfig instance 'kconf' for the architecture x86 has been created.
+ >>> kconf # Calls Kconfig.__repr__()
+ <configuration with 13711 symbols, main menu prompt "Linux/x86 4.14.0-rc7 Kernel Configuration", srctree ".", config symbol prefix "CONFIG_", warnings enabled, undef. symbol assignment warnings disabled>
+ >>> kconf.mainmenu_text # Expanded main menu text
+ 'Linux/x86 4.14.0-rc7 Kernel Configuration'
+ >>> kconf.top_node # The implicit top-level menu
+ <menu node for menu, prompt "Linux/$ARCH $KERNELVERSION Kernel Configuration" (visibility y), deps y, 'visible if' deps y, has child, Kconfig:5>
+ >>> kconf.top_node.list # First child menu node
+ <menu node for symbol SRCARCH, deps y, has next, Kconfig:7>
+ >>> print(kconf.top_node.list) # Calls MenuNode.__str__()
+ config SRCARCH
+ string
+ option env="SRCARCH"
+ default "x86"
+
+ >>> sym = kconf.top_node.list.next.item # Item contained in next menu node
+ >>> print(sym) # Calls Symbol.__str__()
+ config 64BIT
+ bool
+ prompt "64-bit kernel" if ARCH = "x86"
+ default ARCH != "i386"
+ help
+ Say yes to build a 64-bit kernel - formerly known as x86_64
+ Say no to build a 32-bit kernel - formerly known as i386
+
+ >>> sym # Calls Symbol.__repr__()
+ <symbol 64BIT, bool, "64-bit kernel", value y, visibility y, direct deps y, arch/x86/Kconfig:2>
+ >>> sym.assignable # Currently assignable values (0, 1, 2 = n, m, y)
+ (0, 2)
+ >>> sym.set_value(0) # Set it to n
+ True
+ >>> sym.tri_value # Check the new value
+ 0
+ >>> sym = kconf.syms["X86_MPPARSE"] # Look up symbol by name
+ >>> print(sym)
+ config X86_MPPARSE
+ bool
+ prompt "Enable MPS table" if (ACPI || SFI) && X86_LOCAL_APIC
+ default "y" if X86_LOCAL_APIC
+ help
+ For old smp systems that do not have proper acpi support. Newer systems
+ (esp with 64bit cpus) with acpi support, MADT and DSDT will override it
+
+ >>> default = sym.defaults[0] # Fetch its first default
+ >>> sym = default[1] # Fetch the default's condition (just a Symbol here)
+ >>> print(sym) # Print it. Dependencies are propagated to properties, like in the C implementation.
+ config X86_LOCAL_APIC
+ bool
+ default "y" if X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI
+ select IRQ_DOMAIN_HIERARCHY if X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI
+ select PCI_MSI_IRQ_DOMAIN if PCI_MSI && (X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI)
+
+ >>> sym.nodes # Show the MenuNode(s) associated with it
+ [<menu node for symbol X86_LOCAL_APIC, deps n, has next, arch/x86/Kconfig:1015>]
+ >>> kconfiglib.expr_str(sym.defaults[0][1]) # Print the default's condition
+ 'X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI'
+ >>> kconfiglib.expr_value(sym.defaults[0][1]) # Evaluate it (0 = n)
+ 0
+ >>> kconf.syms["64BIT"].set_value(2)
+ True
+ >>> kconfiglib.expr_value(sym.defaults[0][1]) # Evaluate it again (2 = y)
+ 2
+ >>> kconf.write_config("myconfig") # Save a .config
+ >>> ^D
+ $ cat myconfig
+ # Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)
+ CONFIG_64BIT=y
+ CONFIG_X86_64=y
+ CONFIG_X86=y
+ CONFIG_INSTRUCTION_DECODER=y
+ CONFIG_OUTPUT_FORMAT="elf64-x86-64"
+ CONFIG_ARCH_DEFCONFIG="arch/x86/configs/x86_64_defconfig"
+ CONFIG_LOCKDEP_SUPPORT=y
+ CONFIG_STACKTRACE_SUPPORT=y
+ CONFIG_MMU=y
+ ...
Test suite
----------
@@ -118,18 +319,20 @@ The test suite is run with
$ python(3) Kconfiglib/testsuite.py
-(`pypy <http://pypy.org>`_ works too, and is much speedier.)
+`pypy <http://pypy.org>`_ works too, and is much speedier for everything except ``allnoconfig.py``/``allnoconfig_simpler.py``/``allyesconfig.py``, where it doesn't have time to warm up since
+the scripts are run via ``make scriptconfig``.
-The test suite must be run from the top-level kernel directory. It requires that the git
-repository has been cloned into it and that ``makefile.patch`` has been applied.
+The test suite must be run from the top-level kernel directory. It requires that the
+Kconfiglib git repository has been cloned into it and that the makefile patch has been applied.
-**NOTE: Some tests currently overwrite .config in the kernel root, so make sure to back it up.**
+**NOTE: The test suite overwrites .config in the kernel root, so make sure to back it up.**
The test suite consists of a set of selftests and a set of compatibility tests that
-compare (character for character) configurations generated by Kconfiglib with
-configurations generated by ``scripts/kconfig/conf`` for a number of cases. You
-might want to use the "speedy" option; see
-`testsuite.py <https://github.com/ulfalizer/Kconfiglib/blob/master/testsuite.py>`_.
+compare configurations generated by Kconfiglib with
+configurations generated by the C tools, for a number of cases. See
+`testsuite.py <https://github.com/ulfalizer/Kconfiglib/blob/master/testsuite.py>`_
+for the available options. You might want to use the "speedy" option to speed things
+up a bit.
The test suite might fail for a few configurations for kernels older than April 2016,
when a fix was added to Kconfig that's also mirrored in Kconfiglib
@@ -137,61 +340,18 @@ when a fix was added to Kconfig that's also mirrored in Kconfiglib
This is harmless, and only counts as a fail since the test suite compares literal
output from the kconfig version that's bundled with the kernel.
-Kconfiglib is much faster than the test suite would indicate. Most of the time
-is spent waiting around for ``make`` or the C utilities. Adding some multiprocessing
-to the test suite would make sense.
+A lot of time is spent waiting around for ``make`` and the C utilities (which need to reparse all the
+Kconfig files for each defconfig test). Adding some multiprocessing to the test suite would make sense
+too.
Notes
-----
-* **Useful information can be extracted from internal data structures.** The
- expression format is pretty simple for example: ``A && B && (!C || D == 3)`` is
- represented as the tuple structure
- ``(_AND, A, (_AND, B, (_OR, (_NOT, C), (_EQUAL, D, 3))))``; see the
- ``Config._parse_expr()`` docstring.
-
- It's hard to come up with good APIs for dealing with expressions given how
- general they are, so feel free to look at them directly if none of the
- exposed APIs will suffice (modifying them is dangerous though, because it
- breaks dependency tracking). Maybe I'll officially document the expression
- format and add a bunch of accessors later. The internal format is unlikely
- to change in either case, and would probably be returned directly.
-
- If you come up with some good generally-usable APIs involving
- expressions, please tell me. Make sure they also make sense for expressions
- involving ``||`` (or) and ``!`` (not).
-
-* Kconfiglib works well with `PyPy <http://pypy.org>`_. It gives a nice speedup
- over CPython when batch processing a large number of configurations (like
- the test suite does).
-
-* Kconfiglib assumes the modules symbol is ``MODULES`` and will warn if
- ``option modules`` is set on some other symbol. Let me know if this is a
- problem for you. Adding proper ``option modules`` support should be pretty
- easy.
-
-* At least two things make it awkward to replicate a ``menuconfig``-like
- interface in Kconfiglib at the moment (but see
- `SConf <https://github.com/CoryXie/SConf>`_, as mentioned above).
-
- * There are no good APIs for figuring out what other symbols change in value
- when the value of some symbol is changed, to allow for "live" updates
- in the configuration interface. The simplest workaround is to refetch the
- value of each currently visible symbol every time a symbol value is
- changed.
-
- * ``menuconfig`` sometimes creates cosmetic menus implicitly by looking at
- dependencies. For example, a list of symbols where all symbols depend on
- the first symbol creates a cosmetic menu rooted at the first symbol.
- Recreating such menus is awkward.
-
- There is already basic support internally though, because it's needed to
-   get obscure ``choice`` behavior right. See ``_determine_actual_symbols()`` and
- its helper ``_has_auto_menu_dep_on()``.
-
-* Using `__slots__ <https://docs.python.org/3.1/reference/datamodel.html#slots>`_
- on classes would speed things up a bit and save memory. It'd remove some
- flexibility though.
+* Kconfiglib assumes the modules symbol is ``MODULES``, which is backwards-compatible.
+ A warning is printed by default if ``option modules`` is set on some other symbol.
+
+ Let me know if you need proper ``option modules`` support. It wouldn't be that
+ hard to add.
* `fpemud <https://github.com/fpemud>`_ has put together
`Python bindings <https://github.com/fpemud/pylkc>`_ to internal functions in the C
diff --git a/examples/Kmenuconfig b/examples/Kmenuconfig
new file mode 100644
index 0000000..f1cb67b
--- /dev/null
+++ b/examples/Kmenuconfig
@@ -0,0 +1,102 @@
+mainmenu "Example Kconfig configuration"
+
+config MODULES
+ bool "Enable loadable module support"
+ option modules
+ default y
+
+menu "Bool and tristate symbols"
+
+config BOOL
+ bool "Bool symbol"
+ default y
+
+config BOOL_DEP
+ bool "Dependent bool symbol"
+ depends on BOOL
+
+# Mix it up a bit with an 'if' instead of a 'depends on'
+if BOOL
+
+config TRI_DEP
+ tristate "Dependent tristate symbol"
+ select SELECTED_BY_TRI_DEP
+ imply IMPLIED_BY_TRI_DEP
+
+endif
+
+config TWO_MENU_NODES
+ bool "First prompt"
+ depends on BOOL
+
+config TRI
+ tristate "Tristate symbol"
+
+config TWO_MENU_NODES
+ bool "Second prompt"
+
+comment "These are selected by TRI_DEP"
+
+config SELECTED_BY_TRI_DEP
+ tristate "Tristate selected by TRI_DEP"
+
+config IMPLIED_BY_TRI_DEP
+ tristate "Tristate implied by TRI_DEP"
+
+endmenu
+
+
+menu "String, int, and hex symbols"
+
+config STRING
+ string "String symbol"
+ default "foo"
+
+config INT
+ int "Int symbol"
+ default 747
+
+config HEX
+ hex "Hex symbol"
+ default 0xABC
+
+endmenu
+
+
+menu "Various choices"
+
+choice BOOL_CHOICE
+ bool "Bool choice"
+
+config BOOL_CHOICE_SYM_1
+ bool "Bool choice sym 1"
+
+config BOOL_CHOICE_SYM_2
+ bool "Bool choice sym 2"
+
+endchoice
+
+choice TRI_CHOICE
+ tristate "Tristate choice"
+
+config TRI_CHOICE_SYM_1
+ tristate "Tristate choice sym 1"
+
+config TRI_CHOICE_SYM_2
+ tristate "Tristate choice sym 2"
+
+endchoice
+
+choice OPT_BOOL_CHOICE
+ bool "Optional bool choice"
+ optional
+
+config OPT_BOOL_CHOICE_SYM_1
+ bool "Optional bool choice sym 1"
+
+config OPT_BOOL_CHOICE_SYM_2
+ bool "Optional bool choice sym 2"
+
+endchoice
+
+endmenu
diff --git a/examples/allnoconfig.py b/examples/allnoconfig.py
index 9423352..127c60d 100644
--- a/examples/allnoconfig.py
+++ b/examples/allnoconfig.py
@@ -1,40 +1,62 @@
-# Works like allnoconfig. Automatically verified by the testsuite to generate
-# identical output to 'make allnoconfig' for all ARCHes. The looping is done in
-# case setting one symbol to "n" allows other symbols to be set to "n" (due to
-# dependencies).
+# Works like 'make allnoconfig'. Verified by the test suite to generate
+# identical output to 'make allnoconfig' for all ARCHes.
+#
+# See allnoconfig_simpler.py for a much simpler version. This more roundabout
+# version demonstrates some tree walking and value processing.
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/allnoconfig.py
-import kconfiglib
+from kconfiglib import Kconfig, Symbol, STR_TO_TRI
import sys
-conf = kconfiglib.Config(sys.argv[1])
-
-# Do an initial pass to give allnoconfig_y symbols the user value 'y'. It might
-# be possible to handle this through "successive raising" similarly to the
-# "successive lowering" below too, but keep it simple.
-for sym in conf:
- if sym.get_type() in (kconfiglib.BOOL, kconfiglib.TRISTATE) and \
- sym.is_allnoconfig_y():
- sym.set_user_value('y')
-
-done = False
-while not done:
- done = True
-
- for sym in conf:
- # Choices take care of themselves for allnoconfig, so we only need to
- # worry about non-choice symbols
- if not sym.is_choice_symbol() and not sym.is_allnoconfig_y():
- # If we can assign a value to the symbol (where "n", "m" and "y"
- # are ordered from lowest to highest), then assign the lowest
- # value. lower_bound() returns None for symbols whose values cannot
- # (currently) be changed, as well as for non-bool/tristate symbols.
- lower_bound = sym.get_lower_bound()
- if lower_bound is not None and \
- kconfiglib.tri_less(lower_bound, sym.get_value()):
-
- sym.set_user_value(lower_bound)
- # We just changed the value of some symbol. As this may affect
- # other symbols, keep going.
- done = False
-
-conf.write_config(".config")
+def do_allnoconfig(node):
+ global changed
+
+ # Walk the tree of menu nodes. You can imagine this as going down/into menu
+ # entries in the menuconfig interface, setting each to n (or the lowest
+ # assignable value).
+
+ while node:
+ if isinstance(node.item, Symbol):
+ sym = node.item
+
+ # Is the symbol a non-allnoconfig_y symbol that can be set to a
+ # lower value than its current value?
+ if (not sym.is_allnoconfig_y and
+ sym.assignable and
+ sym.assignable[0] < sym.tri_value):
+
+ # Yup, lower it
+ sym.set_value(sym.assignable[0])
+ changed = True
+
+ # Recursively lower children
+ if node.list:
+ do_allnoconfig(node.list)
+
+ node = node.next
+
+# Parse the Kconfig files
+kconf = Kconfig(sys.argv[1])
+
+# Do an initial pass to set 'option allnoconfig_y' symbols to y
+for sym in kconf.defined_syms:
+ if sym.is_allnoconfig_y:
+ sym.set_value(2)
+
+while 1:
+ # Changing later symbols in the configuration can sometimes allow earlier
+ # symbols to be lowered, e.g. if a later symbol 'select's an earlier
+ # symbol. To handle such situations, we do additional passes over the tree
+ # until we're no longer able to change the value of any symbol in a pass.
+ changed = False
+
+ do_allnoconfig(kconf.top_node)
+
+ # Did the pass change any symbols?
+ if not changed:
+ break
+
+kconf.write_config(".config")
diff --git a/examples/allnoconfig_simpler.py b/examples/allnoconfig_simpler.py
index f3cfe9a..81e701c 100644
--- a/examples/allnoconfig_simpler.py
+++ b/examples/allnoconfig_simpler.py
@@ -1,28 +1,22 @@
# This is a simpler version of allnoconfig.py, corresponding to how the C
-# implementation does it. Setting a user value that's not in the assignable
-# range of the symbol (between get_lower_bound() and get_upper_bound(), or,
-# equivalently, not in get_assignable_values()) is OK; the value will simply
-# get truncated downwards or upwards as determined by the visibility and
-# selects.
+# implementation does it. Verified by the test suite to produce identical
+# output to 'make allnoconfig' for all ARCHes.
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/allnoconfig_simpler.py
-# This version is a bit slower compared allnoconfig.py since Kconfiglib
-# invalidates all dependent symbols for each set_user_value() call. This does not
-# happen for load_config(), which instead invalidates all symbols once after
-# the configuration has been loaded. This is OK for load_config() since nearly
-# all symbols will tend to be affected anyway.
-
-import kconfiglib
+from kconfiglib import Kconfig, BOOL, TRISTATE
import sys
-conf = kconfiglib.Config(sys.argv[1])
+conf = Kconfig(sys.argv[1])
-# Avoid warnings printed by Kconfiglib when assigning a user value with
-# set_user_value() to a symbol that has no prompt (such assignments never have
-# an effect)
-conf.set_print_warnings(False)
+# Avoid warnings printed by Kconfiglib when assigning a value to a symbol that
+# has no prompt. Such assignments never have an effect.
+conf.disable_warnings()
-for sym in conf:
- if sym.get_type() in (kconfiglib.BOOL, kconfiglib.TRISTATE):
- sym.set_user_value("y" if sym.is_allnoconfig_y() else "n")
+for sym in conf.defined_syms:
+ if sym.type in (BOOL, TRISTATE):
+ sym.set_value(2 if sym.is_allnoconfig_y else 0)
conf.write_config(".config")
diff --git a/examples/allyesconfig.py b/examples/allyesconfig.py
index 906343c..1bac713 100644
--- a/examples/allyesconfig.py
+++ b/examples/allyesconfig.py
@@ -1,65 +1,90 @@
-# Works like allyesconfig. This is a bit more involved than allnoconfig as we
-# need to handle choices in two different modes:
+# Works like 'make allyesconfig'. Verified by the test suite to generate output
+# identical to 'make allyesconfig', for all ARCHES.
#
-# "y": One symbol is "y", the rest are "n".
-# "m": Any number of symbols are "m", the rest are "n".
+# This example is implemented a bit differently from allnoconfig.py to
+# demonstrate some other possibilities. A variant similar to
+# allnoconfig_simpler.py could be constructed too.
#
-# Only tristate choices can be in "m" mode. It is safe since the code for two
-# conflicting options will appear as separate modules instead of simultaneously
-# in the kernel.
+# In theory, we need to handle choices in two different modes:
#
-# If a choice can be in "y" mode, it will be. If it can only be in "m" mode
-# (due to dependencies), then all the options will be set to "m".
+# y: One symbol is y, the rest are n
+# m: Any number of symbols are m, the rest are n
#
-# The looping is in case setting one symbol to "y" (or "m") allows the value of
-# other symbols to be raised.
+# Only tristate choices can be in m mode.
+#
+# In practice, no m mode choices appear for allyesconfig as of 4.14, as
+# expected, but we still handle them here for completeness. Here's a convoluted
+# example of how you might get an m-mode choice even during allyesconfig:
+#
+# choice
+# tristate "weird choice"
+# depends on m
+#
+# ...
+#
+# endchoice
+#
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/allyesconfig.py
-import kconfiglib
+from kconfiglib import Kconfig, Choice, STR_TO_TRI
import sys
-conf = kconfiglib.Config(sys.argv[1])
+def all_choices(node):
+ """
+ Returns all choices in the menu tree rooted at 'node'. See the
+ Kconfig.write_config() implementation in kconfiglib.py for an example of
+ how the tree can be walked iteratively instead.
-# Get a list of all symbols that are not in choices
-non_choice_syms = [sym for sym in conf.get_symbols() if
- not sym.is_choice_symbol()]
+ (I was thinking of making a list of choices available directly in the API,
+ but I'm not sure it will always be needed internally, and I'm trying to
+ spam the API with less seldomly-used stuff compared to Kconfiglib 1.)
+ """
+ res = []
-done = False
-while not done:
- done = True
+ while node:
+ if isinstance(node.item, Choice):
+ res.append(node.item)
- # Handle symbols outside of choices
+ if node.list:
+ res.extend(all_choices(node.list))
- for sym in non_choice_syms:
- upper_bound = sym.get_upper_bound()
+ node = node.next
- # See corresponding comment for allnoconfig implementation
- if upper_bound is not None and \
- kconfiglib.tri_less(sym.get_value(), upper_bound):
- sym.set_user_value(upper_bound)
- done = False
+ return res
- # Handle symbols within choices
+kconf = Kconfig(sys.argv[1])
- for choice in conf.get_choices():
+non_choice_syms = [sym for sym in kconf.defined_syms if not sym.choice]
+choices = all_choices(kconf.top_node) # All choices in the configuration
- # Handle choices whose visibility allow them to be in "y" mode
+while True:
+ changed = False
+
+ for sym in non_choice_syms:
+ # Set the symbol to the highest assignable value, unless it already has
+ # that value. sym.assignable[-1] gives the last element in assignable.
+ if sym.assignable and sym.tri_value < sym.assignable[-1]:
+ sym.set_value(sym.assignable[-1])
+ changed = True
- if choice.get_visibility() == "y":
- selection = choice.get_selection_from_defaults()
- if selection is not None and \
- selection is not choice.get_user_selection():
- selection.set_user_value("y")
- done = False
+ for choice in choices:
+ # Same logic as above for choices
+ if choice.assignable and choice.tri_value < choice.assignable[-1]:
+ choice.set_value(choice.assignable[-1])
+ changed = True
- # Handle choices whose visibility only allow them to be in "m" mode.
- # This might happen if a choice depends on a symbol that can only be
- # "m" for example.
+ # For y-mode choices, we just let the choice get its default
+ # selection. For m-mode choices, we set all choice symbols to m.
+ if choice.tri_value == 1:
+ for sym in choice.syms:
+ sym.set_value(1)
- elif choice.get_visibility() == "m":
- for sym in choice.get_symbols():
- if sym.get_value() != "m" and \
- sym.get_upper_bound() != "n":
- sym.set_user_value("m")
- done = False
+ # Do multiple passes until we longer manage to raise any symbols or
+ # choices, like in allnoconfig.py
+ if not changed:
+ break
-conf.write_config(".config")
+kconf.write_config(".config")
diff --git a/examples/defconfig.py b/examples/defconfig.py
index 3e958e2..236db8d 100644
--- a/examples/defconfig.py
+++ b/examples/defconfig.py
@@ -1,17 +1,22 @@
# Works like entering "make menuconfig" and immediately saving and exiting
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/allyesconfig.py
import kconfiglib
import os
import sys
-conf = kconfiglib.Config(sys.argv[1])
+conf = kconfiglib.Kconfig(sys.argv[1])
if os.path.exists(".config"):
+ print("using existing .config")
conf.load_config(".config")
else:
- defconfig = conf.get_defconfig_filename()
- if defconfig is not None:
- print("Using" + defconfig)
- conf.load_config(defconfig)
+ if conf.defconfig_filename is not None:
+ print("using " + conf.defconfig_filename)
+ conf.load_config(conf.defconfig_filename)
conf.write_config(".config")
+print("configuration written to .config")
diff --git a/examples/defconfig_oldconfig.py b/examples/defconfig_oldconfig.py
index 9a85440..84aa134 100644
--- a/examples/defconfig_oldconfig.py
+++ b/examples/defconfig_oldconfig.py
@@ -7,27 +7,31 @@
# yes n | make oldconfig
#
# This came up in https://github.com/ulfalizer/Kconfiglib/issues/15.
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/defconfig_oldconfig.py
import kconfiglib
import sys
-conf = kconfiglib.Config(sys.argv[1])
+kconf = kconfiglib.Kconfig(sys.argv[1])
# Mirrors defconfig
-conf.load_config("arch/x86/configs/x86_64_defconfig")
-conf.write_config(".config")
+kconf.load_config("arch/x86/configs/x86_64_defconfig")
+kconf.write_config(".config")
# Mirrors the first oldconfig
-conf.load_config(".config")
-conf["ETHERNET"].set_user_value('n')
-conf.write_config(".config")
+kconf.load_config(".config")
+kconf.syms["ETHERNET"].set_value(0)
+kconf.write_config(".config")
# Mirrors the second oldconfig
-conf.load_config(".config")
-conf["ETHERNET"].set_user_value('y')
-for s in conf:
- if s.get_user_value() is None and 'n' in s.get_assignable_values():
- s.set_user_value('n')
+kconf.load_config(".config")
+kconf.syms["ETHERNET"].set_value(2)
+for s in kconf.defined_syms:
+ if s.user_value is None and 0 in s.assignable:
+ s.set_value(0)
# Write the final configuration
-conf.write_config(".config")
+kconf.write_config(".config")
diff --git a/examples/eval_expr.py b/examples/eval_expr.py
index edb33e6..36a7e6a 100644
--- a/examples/eval_expr.py
+++ b/examples/eval_expr.py
@@ -1,8 +1,24 @@
-# Evaluates an expression in the context of a configuration. (Here we could
-# load a .config as well.)
+# Evaluates an expression (e.g. "X86_64 || (X86_32 && X86_LOCAL_APIC)") in the
+# context of a configuration. Note that this always yields a tristate value (n,
+# m, or y).
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/eval_expr.py SCRIPT_ARG=<expr>
import kconfiglib
import sys
-conf = kconfiglib.Config(sys.argv[1])
-print(conf.eval("(TRACE_IRQFLAGS_SUPPORT || PPC32) && STACKTRACE_SUPPORT"))
+if len(sys.argv) < 3:
+ print('Pass symbol name (without "CONFIG_" prefix) with SCRIPT_ARG=NAME')
+ sys.exit(1)
+
+expr = sys.argv[2]
+
+kconf = kconfiglib.Kconfig(sys.argv[1])
+
+# Enable modules so that m doesn't get demoted to n
+kconf.syms["MODULES"].set_value(2)
+
+print("the expression '{}' evaluates to {}"
+ .format(expr, kconf.eval_string(expr)))
diff --git a/examples/find_symbol.py b/examples/find_symbol.py
new file mode 100644
index 0000000..42677ec
--- /dev/null
+++ b/examples/find_symbol.py
@@ -0,0 +1,205 @@
+# Prints all symbols, choices, menus, and comments that reference a symbol with
+# a particular name in any of their properties or property conditions.
+# Demonstrates expression fetching and walking.
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/find_symbol.py SCRIPT_ARG=<name>
+#
+# Example output for SCRIPT_ARG=X86:
+#
+#
+# Found 452 locations that reference 'X86':
+#
+# ========== Location 1 (init/Kconfig:1122) ==========
+#
+# config SGETMASK_SYSCALL
+# bool
+# prompt "sgetmask/ssetmask syscalls support" if EXPERT
+# default PARISC || MN10300 || BLACKFIN || M68K || PPC || MIPS || X86 || SPARC || CRIS || MICROBLAZE || SUPERH
+# help
+# sys_sgetmask and sys_ssetmask are obsolete system calls
+# no longer supported in libc but still enabled by default in some
+# architectures.
+#
+# If unsure, leave the default option here.
+#
+# ---------- Parent 1 (init/Kconfig:1091) ----------
+#
+# menuconfig EXPERT
+# bool
+# prompt "Configure standard kernel features (expert users)"
+# select DEBUG_KERNEL
+# help
+# This option allows certain base kernel options and settings
+# to be disabled or tweaked. This is for specialized
+# environments which can tolerate a "non-standard" kernel.
+# Only use this if you really know what you are doing.
+#
+# ---------- Parent 2 (init/Kconfig:39) ----------
+#
+# menu "General setup"
+#
+# ========== Location 2 (arch/Kconfig:28) ==========
+#
+# config OPROFILE_EVENT_MULTIPLEX
+# bool
+# prompt "OProfile multiplexing support (EXPERIMENTAL)" if OPROFILE && X86
+# default "n" if OPROFILE && X86
+# help
+# The number of hardware counters is limited. The multiplexing
+# feature enables OProfile to gather more events than counters
+# are provided by the hardware. This is realized by switching
+# between events at a user specified time interval.
+#
+# If unsure, say N.
+#
+# ---------- Parent 1 (arch/Kconfig:15) ----------
+#
+# config OPROFILE
+# ... (tons more lines)
+
+from kconfiglib import Kconfig, Symbol, Choice, MENU, COMMENT, NOT
+import sys
+
+def expr_contains_sym(expr, sym_name):
+ """
+ Returns True if a symbol (or choice, though that's unlikely) with name
+ 'sym_name' appears in the expression 'expr', and False otherwise.
+
+ Note that "foo" is represented as a constant symbol, like in the C
+ implementation.
+ """
+ # Choice symbols have a Choice instance propagated to the conditions of
+ # their properties, so we need this test rather than
+ # isinstance(expr, Symbol)
+ if not isinstance(expr, tuple):
+ return expr.name == sym_name
+
+ if expr[0] == NOT:
+ return expr_contains_sym(expr[1], sym_name)
+
+ # AND, OR, or relation
+ return expr_contains_sym(expr[1], sym_name) or \
+ expr_contains_sym(expr[2], sym_name)
+
+def sc_references_sym(sc, sym_name):
+ """
+ Returns True if a symbol with name 'sym_name' appears in any of the
+ properties or property conditions of the Symbol or Choice 'sc', and False
+ otherwise.
+ """
+ # Search defaults
+ for default, cond in sc.defaults:
+ if expr_contains_sym(default, sym_name) or \
+ expr_contains_sym(cond, sym_name):
+ return True
+
+ if isinstance(sc, Symbol):
+ # Search selects
+ for select, cond in sc.selects:
+ if select.name == sym_name or \
+ expr_contains_sym(cond, sym_name):
+ return True
+
+ # Search implies
+ for imply, cond in sc.implies:
+ if imply.name == sym_name or \
+ expr_contains_sym(cond, sym_name):
+ return True
+
+ # Search ranges
+ for low, high, cond in sc.ranges:
+ if low.name == sym_name or \
+ high.name == sym_name or \
+ expr_contains_sym(cond, sym_name):
+ return True
+
+ return False
+
+def node_references_sym(node, sym_name):
+ """
+ Returns True if a symbol with name 'sym_name' appears in the prompt
+ condition of the MenuNode 'node' or in any of the properties of a
+ symbol/choice stored in the menu node, and False otherwise.
+
+ For MENU menu nodes, also searches the 'visible if' condition.
+
+ Note that prompts are always stored in menu nodes. This is why a symbol can
+ be defined in multiple locations and have a different prompt in each
+ location. For MENU and COMMENT menu nodes, the prompt holds the menu title
+ or comment text. This organization matches the C implementation.
+ """
+ if node.prompt:
+ # Search the prompt condition
+ if expr_contains_sym(node.prompt[1], sym_name):
+ return True
+
+ if isinstance(node.item, (Symbol, Choice)):
+ # Search symbol or choice
+ return sc_references_sym(node.item, sym_name)
+
+ if node.item == MENU:
+ # Search the 'visible if' condition
+ return expr_contains_sym(node.visibility, sym_name)
+
+ # Comments are already handled by searching the prompt condition, because
+ # 'depends on' gets propagated to its condition. This is why we don't need
+ # to look at the direct dependencies for MENU either.
+
+def nodes_referencing_sym(node, sym_name):
+ """
+ Returns a list of all menu nodes in the menu tree rooted at 'node' that
+ reference a symbol with name 'sym_name' in any of their properties. Also
+ checks the properties of any symbols or choices contained in the menu
+ nodes.
+ """
+ res = []
+
+ while node:
+ if node_references_sym(node, sym_name):
+ res.append(node)
+
+ if node.list:
+ res.extend(nodes_referencing_sym(node.list, sym_name))
+
+ node = node.next
+
+ return res
+
+if len(sys.argv) < 3:
+ print('Pass symbol name (without "CONFIG_" prefix) with SCRIPT_ARG=<name>')
+ sys.exit(1)
+
+sym_name = sys.argv[2]
+
+kconf = Kconfig(sys.argv[1])
+nodes = nodes_referencing_sym(kconf.top_node, sym_name)
+
+if not nodes:
+ print("No reference to '{}' found".format(sym_name))
+ sys.exit()
+
+print("Found {} locations that reference '{}':\n".format(len(nodes), sym_name))
+
+for i, node in enumerate(nodes, 1):
+ print("========== Location {} ({}:{}) ==========\n".format(i, node.filename, node.linenr))
+ print(node)
+
+ parent_i = 0
+
+ # Print the parents of the menu node too
+ while True:
+ node = node.parent
+ if node is kconf.top_node:
+ # Don't print the top node. Would say something like the following,
+ # which isn't that interesting:
+ #
+ # menu "Linux/$ARCH $KERNELVERSION Kernel Configuration"
+ break
+
+ parent_i += 1
+
+ print("---------- Parent {} ({}:{}) ----------\n"
+ .format(parent_i, node.filename, node.linenr))
+ print(node)
diff --git a/examples/help_grep.py b/examples/help_grep.py
index 61ac936..01ea5e7 100644
--- a/examples/help_grep.py
+++ b/examples/help_grep.py
@@ -1,49 +1,72 @@
-# Does a case-insensitive search for a string in the help texts for symbols and
-# choices and the titles of menus and comments. Prints the matching items
-# together with their locations and the matching text. Used like
+# Does a case-insensitive search for a regular expression in the help texts of
+# symbols and choices and the prompts of menus and comments. Prints the
+# matching items together with their locations and the matching text.
#
-# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/help_grep.py SCRIPT_ARG=<search text>
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/help_grep.py SCRIPT_ARG=<regex>
+#
+# Shortened example output for SCRIPT_ARG=general:
+#
+# menu "General setup"
+# location: init/Kconfig:39
+#
+# config SYSVIPC
+# bool
+# prompt "System V IPC"
+# help
+# ...
+# exchange information. It is generally considered to be a good thing,
+# ...
+#
+# location: init/Kconfig:233
+#
+# config BSD_PROCESS_ACCT
+# bool
+# prompt "BSD Process Accounting" if MULTIUSER
+# help
+# ...
+# information. This is generally a good idea, so say Y.
+#
+# location: init/Kconfig:403
+#
+# ...
+
-import kconfiglib
+from kconfiglib import Kconfig, Symbol, Choice, MENU, COMMENT
+import re
import sys
if len(sys.argv) < 3:
- print('Pass search string with SCRIPT_ARG="search string"')
+ print('Pass the regex with SCRIPT_ARG=regex')
sys.exit(1)
-search_string = sys.argv[2].lower()
-
-conf = kconfiglib.Config(sys.argv[1])
-
-for item in conf.get_symbols() + \
- conf.get_choices() + conf.get_menus() + conf.get_comments():
- if item.is_symbol() or item.is_choice():
- text = item.get_help()
- elif item.is_menu():
- text = item.get_title()
- else:
- # Comment
- text = item.get_text()
-
- # Case-insensitive search
- if text is not None and search_string in text.lower():
- if item.is_symbol() or item.is_choice():
- # Indent lines in help text. (There might be a nicer way. :)
- text = "\n".join([" " + s for s in text.splitlines()])
-
- # Don't worry about symbols/choices defined in multiple locations to
- # keep things simple
- fname, linenr = item.get_def_locations()[0]
- if item.is_symbol():
- print("config {0} at {1}:{2}:\n{3}"
- .format(item.get_name(), fname, linenr, text))
- elif item.is_choice():
- print("choice at {0}:{1}:\n{2}".format(fname, linenr, text))
-
- else:
- # Menu or comment
- fname, linenr = item.get_location()
- if item.is_menu():
- print('menu "{0}" at {1}:{2}'.format(text, fname, linenr))
- else:
- # Comment
- print('comment "{0}" at {1}:{2}'.format(text, fname, linenr))
+
+search = re.compile(sys.argv[2], re.IGNORECASE).search
+
+def search_tree(node):
+ while node:
+ match = False
+
+ if isinstance(node.item, (Symbol, Choice)) and \
+ node.help is not None and search(node.help):
+ print(node.item)
+ match = True
+
+ elif node.item == MENU and search(node.prompt[0]):
+ print('menu "{}"'.format(node.prompt[0]))
+ match = True
+
+ elif node.item == COMMENT and search(node.prompt[0]):
+ print('comment "{}"'.format(node.prompt[0]))
+ match = True
+
+ if match:
+ print("location: {}:{}\n".format(node.filename, node.linenr))
+
+ if node.list:
+ search_tree(node.list)
+
+ node = node.next
+
+kconf = Kconfig(sys.argv[1])
+search_tree(kconf.top_node)
diff --git a/examples/kconfiglib.py b/examples/kconfiglib.py
new file mode 120000
index 0000000..b9dfb64
--- /dev/null
+++ b/examples/kconfiglib.py
@@ -0,0 +1 @@
+../kconfiglib.py \ No newline at end of file
diff --git a/examples/menuconfig.py b/examples/menuconfig.py
new file mode 100644
index 0000000..375b8c8
--- /dev/null
+++ b/examples/menuconfig.py
@@ -0,0 +1,348 @@
+# Implements a simple configuration interface on top of Kconfiglib to
+# demonstrate concepts for building a menuconfig-like. Emulates how the
+# standard menuconfig prints menu entries.
+#
+# Always displays the entire Kconfig tree to keep things as simple as possible
+# (all symbols, choices, menus, and comments).
+#
+# Usage:
+#
+# $ python(3) Kconfiglib/examples/menuconfig.py <Kconfig file>
+#
+# A sample Kconfig is available in Kconfiglib/examples/Kmenuconfig.
+#
+# Here's a notation guide. The notation matches the one used by menuconfig
+# (scripts/kconfig/mconf):
+#
+# [ ] prompt - Bool
+# < > prompt - Tristate
+# {M} prompt - Tristate selected to m. Can only be set to m or y.
+# -*- prompt - Bool/tristate selected to y, pinning it
+# -M- prompt - Tristate selected to m that also has m visibility,
+# pinning it to m
+# (foo) prompt - String/int/hex symbol with value "foo"
+# --> prompt - The selected symbol in a choice in y mode. This
+# syntax is unique to this example.
+#
+# When modules are disabled, the .type attribute of TRISTATE symbols and
+# choices automatically changes to BOOL. This trick is used by the C
+# implementation as well, and gives the expected behavior without having to do
+# anything extra here. The original type is available in .orig_type if needed.
+#
+# The Kconfiglib/examples/Kmenuconfig example uses named choices to be able to
+# refer to choices by name. Named choices are supported in the C tools too, but
+# I don't think I've ever seen them used in the wild.
+#
+# Sample session:
+#
+# $ python Kconfiglib/examples/menuconfig.py Kconfiglib/examples/Kmenuconfig
+#
+# ======== Example Kconfig configuration ========
+#
+# [*] Enable loadable module support (MODULES)
+# Bool and tristate symbols
+# [*] Bool symbol (BOOL)
+# [ ] Dependent bool symbol (BOOL_DEP)
+# < > Dependent tristate symbol (TRI_DEP)
+# [ ] First prompt (TWO_MENU_NODES)
+# < > Tristate symbol (TRI)
+# [ ] Second prompt (TWO_MENU_NODES)
+# *** These are selected by TRI_DEP ***
+# < > Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
+# < > Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
+# String, int, and hex symbols
+# (foo) String symbol (STRING)
+# (747) Int symbol (INT)
+# (0xABC) Hex symbol (HEX)
+# Various choices
+# -*- Bool choice (BOOL_CHOICE)
+# --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
+# Bool choice sym 2 (BOOL_CHOICE_SYM_2)
+# {M} Tristate choice (TRI_CHOICE)
+# < > Tristate choice sym 1 (TRI_CHOICE_SYM_1)
+# < > Tristate choice sym 2 (TRI_CHOICE_SYM_2)
+# [ ] Optional bool choice (OPT_BOOL_CHOICE)
+#
+# Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): BOOL
+# Value for BOOL (available: n, y): n
+#
+# ======== Example Kconfig configuration ========
+#
+# [*] Enable loadable module support (MODULES)
+# Bool and tristate symbols
+# [ ] Bool symbol (BOOL)
+# < > Tristate symbol (TRI)
+# [ ] Second prompt (TWO_MENU_NODES)
+# *** These are selected by TRI_DEP ***
+# < > Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
+# < > Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
+# String, int, and hex symbols
+# (foo) String symbol (STRING)
+# (747) Int symbol (INT)
+# (0xABC) Hex symbol (HEX)
+# Various choices
+# -*- Bool choice (BOOL_CHOICE)
+# --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
+# Bool choice sym 2 (BOOL_CHOICE_SYM_2)
+# {M} Tristate choice (TRI_CHOICE)
+# < > Tristate choice sym 1 (TRI_CHOICE_SYM_1)
+# < > Tristate choice sym 2 (TRI_CHOICE_SYM_2)
+# [ ] Optional bool choice (OPT_BOOL_CHOICE)
+#
+# Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): MODULES
+# Value for MODULES (available: n, y): n
+#
+# ======== Example Kconfig configuration ========
+#
+# [ ] Enable loadable module support (MODULES)
+# Bool and tristate symbols
+# [ ] Bool symbol (BOOL)
+# [ ] Tristate symbol (TRI)
+# [ ] Second prompt (TWO_MENU_NODES)
+# *** These are selected by TRI_DEP ***
+# [ ] Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
+# [ ] Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
+# String, int, and hex symbols
+# (foo) String symbol (STRING)
+# (747) Int symbol (INT)
+# (0xABC) Hex symbol (HEX)
+# Various choices
+# -*- Bool choice (BOOL_CHOICE)
+# --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
+# Bool choice sym 2 (BOOL_CHOICE_SYM_2)
+# -*- Tristate choice (TRI_CHOICE)
+# --> Tristate choice sym 1 (TRI_CHOICE_SYM_1)
+# Tristate choice sym 2 (TRI_CHOICE_SYM_2)
+# [ ] Optional bool choice (OPT_BOOL_CHOICE)
+#
+# Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ^D
+
+from kconfiglib import Kconfig, \
+ Symbol, Choice, MENU, COMMENT, \
+ BOOL, TRISTATE, STRING, INT, HEX, UNKNOWN, \
+ expr_value, \
+ TRI_TO_STR, STR_TO_TRI
+import readline
+import sys
+
+# Python 2/3 compatibility hack
+if sys.version_info[0] < 3:
+ input = raw_input
+
+def indent_print(s, indent):
+ print((" " * indent) + s)
+
+def value_str(sc):
+ """
+ Returns the value part ("[*]", "<M>", "(foo)" etc.) of a menu entry.
+
+ sc: Symbol or Choice.
+ """
+ if sc.type in (STRING, INT, HEX):
+ return "({})".format(sc.str_value)
+
+ # BOOL or TRISTATE
+
+ # The choice mode acts as an upper bound on the visibility of choice
+ # symbols, so we can check the choice symbols' own visibility to see if the
+ # choice is in y mode
+ if isinstance(sc, Symbol) and sc.choice and sc.visibility == 2:
+ # For choices in y mode, print '-->' next to the selected symbol
+ if sc.choice.selection is sc:
+ return "-->"
+ return " "
+
+ tri_val_str = {0: " ", 1: "M", 2: "*"}[sc.tri_value]
+
+ if len(sc.assignable) == 1:
+ # Pinned to a single value
+ return "-{}-".format(tri_val_str)
+
+ if sc.type == BOOL:
+ return "[{}]".format(tri_val_str)
+
+ if sc.type == TRISTATE:
+ if sc.assignable == (1, 2):
+ # m and y available
+ return "{" + tri_val_str + "}" # Gets a bit confusing with .format()
+ return "<{}>".format(tri_val_str)
+
+def node_str(node):
+ """
+ Returns the complete menu entry text for a menu node, or "" for invisible
+ menu nodes. Invisible menu nodes are those that lack a prompt or don't have
+ a satisfied prompt condition.
+
+ Example return value: "[*] Bool symbol (BOOL)"
+
+ The symbol name is printed in parentheses to the right of the prompt. This
+ is so that symbols can easily be referred to in the configuration
+ interface.
+ """
+ if not node.prompt:
+ return ""
+
+ # Even for menu nodes for symbols and choices, it's wrong to check
+ # Symbol.visibility / Choice.visibility here. The reason is that a symbol
+ # (and a choice, in theory) can be defined in multiple locations, giving it
+ # multiple menu nodes, which do not necessarily all have the same prompt
+ # visibility. Symbol.visibility / Choice.visibility is calculated as the OR
+ # of the visibility of all the prompts.
+ prompt, prompt_cond = node.prompt
+ if not expr_value(prompt_cond):
+ return ""
+
+ if node.item == MENU:
+ return " " + prompt
+
+ if node.item == COMMENT:
+ return " *** {} ***".format(prompt)
+
+ # Symbol or Choice
+
+ sc = node.item
+
+ if sc.type == UNKNOWN:
+ # Skip symbols defined without a type
+ return ""
+
+ # {:3} sets the field width to three. Gives nice alignment for empty string
+ # values.
+ res = "{:3} {}".format(value_str(sc), prompt)
+
+ # Don't print the name for unnamed choices (the normal kind)
+ if sc.name is not None:
+ res += " ({})".format(sc.name)
+
+ return res
+
+def print_menuconfig_nodes(node, indent):
+ """
+ Prints a tree with all the menu entries rooted at node. Child menu entries
+ are indented.
+ """
+ while node:
+ string = node_str(node)
+ if string:
+ indent_print(string, indent)
+
+ if node.list:
+ print_menuconfig_nodes(node.list, indent + 8)
+
+ node = node.next
+
+def print_menuconfig(kconf):
+ """
+ Prints all menu entries for the configuration.
+ """
+ # Print the expanded mainmenu text at the top. This is the same as
+ # kconf.top_node.prompt[0], but with variable references expanded.
+ print("\n======== {} ========\n".format(kconf.mainmenu_text))
+
+ print_menuconfig_nodes(kconf.top_node.list, 0)
+ print("")
+
+def get_value_from_user(sc):
+ """
+ Prompts the user for a value for the symbol or choice 'sc'. For
+ bool/tristate symbols and choices, provides a list of all the assignable
+ values.
+ """
+ if not sc.visibility:
+ print(sc.name + " is not currently visible")
+ return False
+
+ prompt = "Value for {}".format(sc.name)
+ if sc.type in (BOOL, TRISTATE):
+ prompt += " (available: {})" \
+ .format(", ".join([TRI_TO_STR[val] for val in sc.assignable]))
+ prompt += ": "
+
+ val_str = input(prompt).strip()
+ if sc.type in (BOOL, TRISTATE):
+ if val_str not in STR_TO_TRI:
+ print("'{}' is not a valid tristate value".format(val_str))
+ return False
+
+ # I was thinking of having set_value() accept "n", "m", "y" as well as
+ # a convenience for BOOL / TRISTATE symbols. Consistently using 0, 1, 2
+ # makes the format clearer though. That's the best format in all ways
+ # except for readability (where it isn't horrible either).
+ val = STR_TO_TRI[val_str]
+ else:
+ val = val_str
+
+ # Automatically add a "0x" prefix for hex symbols, like the menuconfig
+ # interface does. This isn't done when loading .config files, hence why
+ # set_value() doesn't do it automatically.
+ if sc.type == HEX and not val.startswith(("0x", "0X")):
+ val = "0x" + val
+
+ # Let Kconfiglib itself print a warning here if the value is invalid. We
+ # could also disable warnings temporarily with
+ # kconf.disable_warnings() / kconf.enable_warnings() and print our own
+ # warning.
+ return sc.set_value(val)
+
+if __name__ == "__main__":
+ if len(sys.argv) != 2:
+ sys.exit("usage: menuconfig.py <Kconfig file>")
+
+ # Load Kconfig configuration files
+ kconf = Kconfig(sys.argv[1])
+
+ # Print the initial configuration tree
+ print_menuconfig(kconf)
+
+ while True:
+ try:
+ cmd = input('Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ') \
+ .strip()
+ except EOFError:
+ print("")
+ break
+
+ if cmd == "load_config":
+ config_filename = input(".config file to load: ")
+
+ try:
+ kconf.load_config(config_filename)
+ except IOError as e:
+ # Print the (spammy) error from Kconfiglib itself
+ print(e.message + "\n")
+ else:
+ print("Configuration loaded from " + config_filename)
+
+ print_menuconfig(kconf)
+ continue
+
+ if cmd == "write_config":
+ config_filename = input("To this file: ")
+
+ try:
+ kconf.write_config(config_filename)
+ except IOError as e:
+ print(e.message)
+ else:
+ print("Configuration written to " + config_filename)
+
+ continue
+
+ # Assume 'cmd' is the name of a symbol or choice if it isn't one of the
+ # commands above, prompt the user for a value for it, and print the new
+ # configuration tree
+
+ if cmd in kconf.syms:
+ if get_value_from_user(kconf.syms[cmd]):
+ print_menuconfig(kconf)
+
+ continue
+
+ if cmd in kconf.named_choices:
+ if get_value_from_user(kconf.named_choices[cmd]):
+ print_menuconfig(kconf)
+
+ continue
+
+ print("No symbol/choice named '{}' in the configuration"
+ .format(cmd))
diff --git a/examples/print_refs.py b/examples/print_refs.py
deleted file mode 100644
index ea62223..0000000
--- a/examples/print_refs.py
+++ /dev/null
@@ -1,13 +0,0 @@
-# Prints the names of all symbols that reference a particular symbol. (There's
-# also a method get_selected_symbols() for determining just selection
-# relations.)
-
-import kconfiglib
-import sys
-
-conf = kconfiglib.Config(sys.argv[1])
-
-x86 = conf["X86"]
-for sym in conf:
- if x86 in sym.get_referenced_symbols():
- print(sym.get_name())
diff --git a/examples/print_sym_info.py b/examples/print_sym_info.py
index c913358..3ee3c97 100644
--- a/examples/print_sym_info.py
+++ b/examples/print_sym_info.py
@@ -1,18 +1,53 @@
-# Loads a Kconfig and a .config and prints information about a symbol.
+# Loads a Kconfig and a .config and prints a symbol.
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/print_sym_info.py SCRIPT_ARG=<name>
+#
+# Example output for SCRIPT_ARG=MODULES:
+#
+# menuconfig MODULES
+# bool
+# prompt "Enable loadable module support"
+# option modules
+# help
+# Kernel modules are small pieces of compiled code which can
+# be inserted in the running kernel, rather than being
+# permanently built into the kernel. You use the "modprobe"
+# tool to add (and sometimes remove) them. If you say Y here,
+# many parts of the kernel can be built as modules (by
+# answering M instead of Y where indicated): this is most
+# useful for infrequently used options which are not required
+# for booting. For more information, see the man pages for
+# modprobe, lsmod, modinfo, insmod and rmmod.
+#
+# If you say Y here, you will need to run "make
+# modules_install" to put the modules under /lib/modules/
+# where modprobe can find them (you may need to be root to do
+# this).
+#
+# If unsure, say Y.
+#
+# value = n
+# visibility = y
+# currently assignable values: n, y
+# defined at init/Kconfig:1674
-import kconfiglib
+from kconfiglib import Kconfig, TRI_TO_STR
import sys
-# Create a Config object representing a Kconfig configuration. (Any number of
-# these can be created -- the library has no global state.)
-conf = kconfiglib.Config(sys.argv[1])
+if len(sys.argv) < 3:
+ print('Pass symbol name (without "CONFIG_" prefix) with SCRIPT_ARG=<name>')
+ sys.exit(1)
-# Load values from a .config file. 'srctree' is an environment variable set by
-# the Linux makefiles to the top-level directory of the kernel tree. It needs
-# to be used here for the script to work with alternative build directories
-# (specified e.g. with O=).
-conf.load_config("$srctree/arch/x86/configs/i386_defconfig")
+kconf = Kconfig(sys.argv[1])
+sym = kconf.syms[sys.argv[2]]
-# Print some information about a symbol. (The Config class implements
-# __getitem__() to provide a handy syntax for getting symbols.)
-print(conf["SERIAL_UARTLITE_CONSOLE"])
+print(sym)
+print("value = " + sym.str_value)
+print("visibility = " + TRI_TO_STR[sym.visibility])
+print("currently assignable values: " +
+ ", ".join([TRI_TO_STR[v] for v in sym.assignable]))
+
+for node in sym.nodes:
+ print("defined at {}:{}".format(node.filename, node.linenr))
diff --git a/examples/print_tree.py b/examples/print_tree.py
index 1405ed5..1a53a3a 100644
--- a/examples/print_tree.py
+++ b/examples/print_tree.py
@@ -1,23 +1,67 @@
-# Prints a tree of all items in the configuration
+# Prints the menu tree of the configuration. Dependencies between symbols can
+# sometimes implicitly alter the menu structure (see kconfig-language.txt), and
+# that's implemented too.
+#
+# Usage:
+#
+# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/print_tree.py
+#
+# Example output:
+#
+# ...
+# config HAVE_KERNEL_LZO
+# config HAVE_KERNEL_LZ4
+# choice
+# config KERNEL_GZIP
+# config KERNEL_BZIP2
+# config KERNEL_LZMA
+# config KERNEL_XZ
+# config KERNEL_LZO
+# config KERNEL_LZ4
+# config DEFAULT_HOSTNAME
+# config SWAP
+# config SYSVIPC
+# config SYSVIPC_SYSCTL
+# config POSIX_MQUEUE
+# config POSIX_MQUEUE_SYSCTL
+# config CROSS_MEMORY_ATTACH
+# config FHANDLE
+# config USELIB
+# config AUDIT
+# config HAVE_ARCH_AUDITSYSCALL
+# config AUDITSYSCALL
+# config AUDIT_WATCH
+# config AUDIT_TREE
+# menu "IRQ subsystem"
+# config MAY_HAVE_SPARSE_IRQ
+# config GENERIC_IRQ_LEGACY
+# config GENERIC_IRQ_PROBE
+# ...
-import kconfiglib
+from kconfiglib import Kconfig, Symbol, Choice, MENU, COMMENT
import sys
-def print_with_indent(s, indent):
+def indent_print(s, indent):
print((" " * indent) + s)
-def print_items(items, indent):
- for item in items:
- if item.is_symbol():
- print_with_indent("config {0}".format(item.get_name()), indent)
- elif item.is_menu():
- print_with_indent('menu "{0}"'.format(item.get_title()), indent)
- print_items(item.get_items(), indent + 2)
- elif item.is_choice():
- print_with_indent('choice', indent)
- print_items(item.get_items(), indent + 2)
- elif item.is_comment():
- print_with_indent('comment "{0}"'.format(item.get_text()), indent)
-
-conf = kconfiglib.Config(sys.argv[1])
-print_items(conf.get_top_level_items(), 0)
+def print_items(node, indent):
+ while node:
+ if isinstance(node.item, Symbol):
+ indent_print("config " + node.item.name, indent)
+
+ elif isinstance(node.item, Choice):
+ indent_print("choice", indent)
+
+ elif node.item == MENU:
+ indent_print('menu "{0}"'.format(node.prompt[0]), indent)
+
+ elif node.item == COMMENT:
+ indent_print('comment "{0}"'.format(node.prompt[0]), indent)
+
+ if node.list:
+ print_items(node.list, indent + 2)
+
+ node = node.next
+
+kconf = Kconfig(sys.argv[1])
+print_items(kconf.top_node, 0)
diff --git a/examples/print_undefined.py b/examples/print_undefined.py
deleted file mode 100644
index fb8120b..0000000
--- a/examples/print_undefined.py
+++ /dev/null
@@ -1,15 +0,0 @@
-# Prints the names of all symbols that are referenced but never defined in the
-# current configuration together with the locations where they are referenced.
-# Integers being included in the list is not a bug, as these need to be treated
-# as symbols per the design of Kconfig.
-
-import kconfiglib
-import sys
-
-conf = kconfiglib.Config(sys.argv[1])
-
-for sym in conf.get_symbols():
- if not sym.is_defined():
- print(sym.get_name())
- for (filename, linenr) in sym.get_ref_locations():
- print(" {0}:{1}".format(filename, linenr))
diff --git a/kconfiglib-2-changes.txt b/kconfiglib-2-changes.txt
new file mode 100644
index 0000000..b7a78c2
--- /dev/null
+++ b/kconfiglib-2-changes.txt
@@ -0,0 +1,112 @@
+This is a summary of the changes between Kconfiglib 1 and Kconfiglib 2.
+
+The Kconfiglib 1 API was designed to solve a particular problem, before I even
+knew that I'd release it as a library. I was also hazy on many details of
+Kconfig internals when I first designed it.
+
+Bolting things like implicit menus and symbol introspection onto the Kconfiglib
+1 API would have been awkward, so I decided to redo things the right way.
+
+API changes:
+
+ - Read-only fields now use attributes instead of get_*() methods, which is
+ less clunky to work with.
+
+ A few attributes use @property magic internally, but are still efficient to
+ access due to caching.
+
+ - Tristate values are now represented as 0, 1, 2 instead of "n", "m", "y".
+ This is a small loss in readability in a few spots, but the advantages far
+ outweigh the disadvantages. Tristate values can now be compared directly,
+ and tristate n is naturally false. ("n" < "m" < "y" is false even
+ lexicographically, which is awkward.)
+
+ The original design was an attempt to keep the value format consistent for
+ all types of symbols (as far as I can remember).
+
+ - Much improved value API. Instead of get_upper/lower_bound(), is_assignable(),
+ etc., there's now just a single .assignable attribute, which returns a
+ sorted tuple with the currently assignable values (that would be reflected
+ in the value of the symbol). This corresponds to the selections available
+ in the menuconfig interface.
+
+ For example, sym.assignable might be (0, 2) for a visible, non-selected
+ bool symbol.
+
+ - Many seldom-used APIs, like get_ref_locations(), were removed. They can be
+ implemented manually in a much more general and powerful way with newly
+ added functionality (see e.g. examples/find_symbol.py).
+
+ - Printing a Symbol or other Kconfig object now returns its representation in
+ Kconfig syntax instead of the ugly Kconfiglib 1 format.
+
+ - Symbols and other Kconfig objects now have a __repr__() that returns useful
+ information (name, type, value, user value, prompt, location, etc.)
+
+ - __str__() and __repr__() are deliberately implemented with just public
+ APIs, which indirectly ensures that all Kconfig object properties can be
+ fetched separately.
+
+ - The Item base class had gotten kinda pointless and has been removed.
+ Instead of is_symbol(), etc., just use isinstance() to check what kind of
+ object you're dealing with. There's fewer of them now too.
+
+ - Probably lots of other stuff I forgot to mention.
+
+
+New features:
+
+ - Direct access to the menu tree
+
+ Kconfiglib 1 represented the menu tree with nested lists, which works
+ poorly for e.g. implicit menus. Kconfiglib 2 uses a separate user-visible
+ MenuNode class that corresponds directly to 'struct menu' in the C
+ implementation, storing the same fields.
+
+ The Menu and Comment classes were removed. They're represented as plain
+ menu nodes with node.item set to MENU and COMMENT, respectively.
+
+ - Direct access to symbol properties and their conditions
+
+ Expressions use a simple tuple-based format (think Lisp).
+
+ Kconfiglib 1 stored constant (quoted) symbols as plain Python strings. This
+ makes expressions awkward to work with due to inconsistent value types.
+ Kconfiglib 2 uses constant Symbol instances for quoted symbols, like the C
+ implementation.
+
+ - Better 'choice' handling
+
+ In Kconfiglib 1, choices magically changed mode when values were assigned
+ to choice symbols, which was meant to make .config loading just work. In
+ Kconfiglib 2, the mode must be assigned manually (though most choices can
+ only be in n or y mode anyway and take care of themselves). load_config()
+ was updated to infer the mode from the types of values assigned to choice
+ symbols and set it explicitly.
+
+ Choices now have an .assignable attribute too, which makes the API
+ perfectly consistent between symbols and choices. The choice mode acts as
+ an upper bound on the visibility of the choice symbols (the choice is
+ simply propagated to property conditions, which makes this automatic).
+
+ - Faster parsing code
+
+ The helper classes were merged into the Kconfig (new name for Config)
+ class. This removes a lot of temporary object creation and passing around
+ of values. The 4.14 x86 Kconfigs now parse in about a second on a Core
+ i7-2600k (or much faster than that with PyPy if multiple configurations are
+ loaded).
+
+ - Better invalidation algorithm
+
+ Internal reorganization allowed for a better invalidation algorithm to be
+ used, which stops redundant invalidation much earlier and gives a huge
+ speedup for scripts that set a lot of values. The non-Kconfig-parsing
+ overhead is often irrelevant now. allyesconfig.py runs in about 1.5 seconds
+ when run via 'make scriptconfig', including the 'make' overhead.
+
+ - Probably lots of other stuff I forgot to mention.
+
+
+In general, Kconfiglib 2 is just a better and more general Kconfiglib 1, with
+some old mistakes cleaned up.
diff --git a/kconfiglib.py b/kconfiglib.py
index 186c931..487d57d 100644
--- a/kconfiglib.py
+++ b/kconfiglib.py
@@ -1,1320 +1,1114 @@
-# This is Kconfiglib, a Python library for scripting, debugging, and extracting
-# information from Kconfig-based configuration systems. To view the
-# documentation, run
-#
-# $ pydoc kconfiglib
-#
-# or, if you prefer HTML,
-#
-# $ pydoc -w kconfiglib
-#
-# The examples/ subdirectory contains examples, to be run with e.g.
-#
-# $ make scriptconfig SCRIPT=Kconfiglib/examples/print_tree.py
-#
-# Look in testsuite.py for the test suite.
-
"""
-Kconfiglib is a Python library for scripting and extracting information from
-Kconfig-based configuration systems. Features include the following:
+Overview
+========
- - Symbol values and properties can be looked up and values assigned
- programmatically.
- - .config files can be read and written.
- - Expressions can be evaluated in the context of a Kconfig configuration.
- - Relations between symbols can be quickly determined, such as finding all
- symbols that reference a particular symbol.
- - Highly compatible with the scripts/kconfig/*conf utilities. The test suite
- automatically compares outputs between Kconfiglib and the C implementation
- for a large number of cases.
+Kconfiglib is a Python 2/3 library for scripting and extracting information
+from Kconfig configuration systems. It can be used for the following, among
+other things:
-For the Linux kernel, scripts are run using
+ - Programmatically get and set symbol values
- $ make scriptconfig [ARCH=<arch>] SCRIPT=<path to script> [SCRIPT_ARG=<arg>]
+ allnoconfig.py and allyesconfig.py examples are provided, automatically
+ verified to produce identical output to the standard 'make allnoconfig' and
+ 'make allyesconfig'.
-Using the 'scriptconfig' target ensures that required environment variables
-(SRCARCH, ARCH, srctree, KERNELVERSION, etc.) are set up correctly.
+ - Read and write .config files
-Scripts receive the name of the Kconfig file to load in sys.argv[1]. As of
-Linux 4.1.0-rc5, this is always "Kconfig" from the kernel top-level directory.
-If an argument is provided with SCRIPT_ARG, it appears as sys.argv[2].
+ The generated .config files are character-for-character identical to what
+ the C implementation would generate (except for the header comment). The
+ test suite relies on this, as it compares the generated files.
-To get an interactive Python prompt with Kconfiglib preloaded and a Config
-object 'c' created, run
+ - Inspect symbols
- $ make iscriptconfig [ARCH=<arch>]
+ Printing a symbol gives output which could be fed back into a Kconfig parser
+ to redefine it***. The printing function (__str__()) is implemented with
+ public APIs, meaning you can fetch just whatever information you need as
+ well.
-Kconfiglib supports both Python 2 and Python 3. For (i)scriptconfig, the Python
-interpreter to use can be passed in PYTHONCMD, which defaults to 'python'. PyPy
-works well too, and might give a nice speedup for long-running jobs.
+ A helpful __repr__() is implemented on all objects too, also implemented
+ with public APIs.
-The examples/ directory contains short example scripts, which can be run with
-e.g.
+ ***Choice symbols get their parent choice as a dependency, which shows up as
+ e.g. 'prompt "choice symbol" if <choice>' when printing the symbol. This
+ could easily be worked around if 100% reparsable output is needed.
- $ make scriptconfig SCRIPT=Kconfiglib/examples/print_tree.py
+ - Inspect expressions
-or
+ Expressions use a simple tuple-based format that can be processed manually
+ if needed. Expression printing and evaluation functions are provided,
+ implemented with public APIs.
- $ make scriptconfig SCRIPT=Kconfiglib/examples/help_grep.py SCRIPT_ARG=kernel
+ - Inspect the menu tree
-testsuite.py contains the test suite. See the top of the script for how to run
-it.
+ The underlying menu tree is exposed, including submenus created implicitly
+ from symbols depending on preceding symbols. This can be used e.g. to
+ implement menuconfig-like functionality. See the menuconfig.py example.
-Credits: Written by Ulf "Ulfalizer" Magnusson
-Send bug reports, suggestions and other feedback to ulfalizer a.t Google's
-email service. Don't wrestle with internal APIs. Tell me what you need and I
-might add it in a safe way as a client API instead."""
+Here are some other features:
-import os
-import platform
-import re
-import sys
+ - Single-file implementation
-# File layout:
-#
-# Public classes
-# Public functions
-# Internal classes
-# Internal functions
-# Public global constants
-# Internal global constants
+ The entire library is contained in this file.
-# Line length: 79 columns
+ - Runs unmodified under both Python 2 and Python 3
-#
-# Public classes
-#
+ The code mostly uses basic Python features and has no third-party
+ dependencies. The most advanced things used are probably @property and
+ __slots__.
-class Config(object):
+ - Robust and highly compatible with the standard Kconfig C tools
- """Represents a Kconfig configuration, e.g. for i386 or ARM. This is the
- set of symbols and other items appearing in the configuration together with
- their values. Creating any number of Config objects -- including for
- different architectures -- is safe; Kconfiglib has no global state."""
+ The test suite automatically compares output from Kconfiglib and the C tools
+ by diffing the generated .config files for the real kernel Kconfig and
+ defconfig files, for all ARCHes.
- #
- # Public interface
- #
+ This currently involves comparing the output for 36 ARCHes and 498 defconfig
+ files (or over 18000 ARCH/defconfig combinations in "obsessive" test suite
+ mode). All tests are expected to pass.
- def __init__(self, filename="Kconfig", base_dir=None, print_warnings=True,
- print_undef_assign=False):
- """Creates a new Config object, representing a Kconfig configuration.
- Raises Kconfig_Syntax_Error on syntax errors.
-
- filename (default: "Kconfig"): The base Kconfig file of the
- configuration. For the Linux kernel, you'll probably want "Kconfig"
- from the top-level directory, as environment variables will make
- sure the right Kconfig is included from there
- (arch/<architecture>/Kconfig). If you are using Kconfiglib via 'make
- scriptconfig', the filename of the base base Kconfig file will be in
- sys.argv[1].
-
- base_dir (default: None): The base directory relative to which 'source'
- statements within Kconfig files will work. For the Linux kernel this
- should be the top-level directory of the kernel tree. $-references
- to existing environment variables will be expanded.
-
- If None (the default), the environment variable 'srctree' will be
- used if set, and the current directory otherwise. 'srctree' is set
- by the Linux makefiles to the top-level kernel directory. A default
- of "." would not work with an alternative build directory.
-
- print_warnings (default: True): Set to True if warnings related to this
- configuration should be printed to stderr. This can be changed later
- with Config.set_print_warnings(). It is provided as a constructor
- argument since warnings might be generated during parsing.
-
- print_undef_assign (default: False): Set to True if informational
- messages related to assignments to undefined symbols should be
- printed to stderr for this configuration. Can be changed later with
- Config.set_print_undef_assign()."""
-
- # The set of all symbols, indexed by name (a string)
- self._syms = {}
-
- # The set of all defined symbols in the configuration in the order they
- # appear in the Kconfig files. This excludes the special symbols n, m,
- # and y as well as symbols that are referenced but never defined.
- self._defined_syms = []
-
- # The set of all named choices (yes, choices can have names), indexed
- # by name (a string)
- self._named_choices = {}
-
- # Lists containing all choices, menus, and comments in the
- # configuration
- self._choices = []
- self._menus = []
- self._comments = []
+ - **Not horribly slow despite being a pure Python implementation**
- def register_special_symbol(type_, name, val):
- sym = Symbol()
- sym._is_special = True
- sym._is_defined = True
- sym._config = self
- sym._name = name
- sym._type = type_
- sym._cached_val = val
- self._syms[name] = sym
- return sym
-
- # The special symbols n, m and y, used as shorthand for "n", "m" and
- # "y"
- self._n = register_special_symbol(TRISTATE, "n", "n")
- self._m = register_special_symbol(TRISTATE, "m", "m")
- self._y = register_special_symbol(TRISTATE, "y", "y")
- # DEFCONFIG_LIST uses this
- register_special_symbol(STRING, "UNAME_RELEASE", platform.uname()[2])
-
- # The symbol with "option defconfig_list" set, containing a list of
- # default .config files
- self._defconfig_sym = None
-
- # See Symbol.get_(src)arch()
- self._arch = os.environ.get("ARCH")
- self._srcarch = os.environ.get("SRCARCH")
-
- # If you set CONFIG_ in the environment, Kconfig will prefix all
- # symbols with its value when saving the configuration, instead of
- # using the default, "CONFIG_".
- self._config_prefix = os.environ.get("CONFIG_")
- if self._config_prefix is None:
- self._config_prefix = "CONFIG_"
-
- # Regular expressions for parsing .config files
- self._set_re = re.compile(r"{}(\w+)=(.*)"
- .format(self._config_prefix))
- self._unset_re = re.compile(r"# {}(\w+) is not set"
- .format(self._config_prefix))
-
- self._kconfig_filename = filename
-
- # See Config.__init__(). We need this for get_defconfig_filename().
- self._srctree = os.environ.get("srctree")
-
- if base_dir is None:
- self._base_dir = "." if self._srctree is None else self._srctree
- else:
- self._base_dir = os.path.expandvars(base_dir)
+ The allyesconfig.py example currently runs in about 1.6 seconds on a Core i7
+ 2600K (with a warm file cache), where half a second is overhead from 'make
+ scriptconfig' (see below).
- # The 'mainmenu' text
- self._mainmenu_text = None
+ For long-running jobs, PyPy gives a big performance boost. CPython is faster
+ for short-running jobs as PyPy needs some time to warm up.
- # The filename of the most recently loaded .config file
- self._config_filename = None
- # The textual header of the most recently loaded .config, uncommented
- self._config_header = None
+ - Internals that (mostly) mirror the C implementation
- self._print_warnings = print_warnings
- self._print_undef_assign = print_undef_assign
+ While being simpler to understand.
- # When parsing properties, we stop on the first (non-empty)
- # non-property line. _end_line and _end_line_tokens hold that line and
- # its tokens so that we don't have to re-tokenize the line later. This
- # isn't just an optimization: We record references to symbols during
- # tokenization, so tokenizing twice would cause double registration.
- #
- # self._end_line doubles as a flag where None means we don't have a
- # cached tokenized line.
- self._end_line = None
- # self.end_line_tokens is set later during parsing
- # Parse the Kconfig files
- self._top_block = []
- self._parse_file(filename, None, None, None, self._top_block)
+Using Kconfiglib on the Linux kernel with the Makefile targets
+==============================================================
- # Build Symbol._direct_dependents for all symbols
- self._build_dep()
+For the Linux kernel, a handy interface is provided by the
+scripts/kconfig/Makefile patch. It can be applied by running the following
+command in the kernel root:
- def get_arch(self):
- """Returns the value the environment variable ARCH had at the time the
- Config instance was created, or None if ARCH was not set. For the
- kernel, this corresponds to the architecture being built for, with
- values such as "i386" or "mips"."""
- return self._arch
-
- def get_srcarch(self):
- """Returns the value the environment variable SRCARCH had at the time
- the Config instance was created, or None if SRCARCH was not set. For
- the kernel, this corresponds to the particular arch/ subdirectory
- containing architecture-specific code."""
- return self._srcarch
-
- def get_srctree(self):
- """Returns the value the environment variable 'srctree' had at the time
- the Config instance was created, or None if 'srctree' was not defined.
- This variable points to the source directory and is used when building
- in a separate directory."""
- return self._srctree
-
- def get_base_dir(self):
- """Returns the base directory relative to which 'source' statements
- will work, passed as an argument to Config.__init__()."""
- return self._base_dir
-
- def get_kconfig_filename(self):
- """Returns the name of the (base) kconfig file this configuration was
- loaded from."""
- return self._kconfig_filename
-
- def get_config_filename(self):
- """Returns the filename of the most recently loaded configuration file,
- or None if no configuration has been loaded."""
- return self._config_filename
-
- def get_config_header(self):
- """Returns the (uncommented) textual header of the .config file most
- recently loaded with load_config(). Returns None if no .config file has
- been loaded or if the most recently loaded .config file has no header.
-
- The header consists of all lines up to but not including the first line
- that either (1) does not begin with "#", or (2) matches
- "# CONFIG_FOO is not set"."""
- return self._config_header
-
- def get_mainmenu_text(self):
- """Returns the text of the 'mainmenu' statement (with $-references to
- symbols replaced by symbol values), or None if the configuration has no
- 'mainmenu' statement."""
- return None if self._mainmenu_text is None else \
- self._expand_sym_refs(self._mainmenu_text)
-
- def get_defconfig_filename(self):
- """Returns the name of the defconfig file, which is the first existing
- file in the list given in a symbol having 'option defconfig_list' set.
- $-references to symbols will be expanded ("$FOO bar" -> "foo bar" if
- FOO has the value "foo"). Returns None in case of no defconfig file.
- Setting 'option defconfig_list' on multiple symbols ignores the symbols
- past the first one (and prints a warning).
-
- If the environment variable 'srctree' was set when the Config was
- created, each defconfig specified with a relative path will be
- searched for in $srcdir if it is not found at the specified path (i.e.,
- if foo/defconfig is not found, $srctree/foo/defconfig will be looked
- up).
-
- WARNING: A wart here is that scripts/kconfig/Makefile sometimes uses
- the --defconfig=<defconfig> option when calling the C implementation of
- e.g. 'make defconfig'. This option overrides the 'option
- defconfig_list' symbol, meaning the result from
- get_defconfig_filename() might not match what 'make defconfig' would
- use. That probably ought to be worked around somehow, so that this
- function always gives the "expected" result."""
- if self._defconfig_sym is None:
- return None
- for filename, cond_expr in self._defconfig_sym._def_exprs:
- if self._eval_expr(cond_expr) != "n":
- filename = self._expand_sym_refs(filename)
- if os.access(filename, os.R_OK):
- return filename
- # defconfig not found. If the path is a relative path and
- # $srctree is set, we also look in $srctree.
- if not os.path.isabs(filename) and self._srctree is not None:
- filename = os.path.join(self._srctree, filename)
- if os.access(filename, os.R_OK):
- return filename
+ $ wget -qO- https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch | git am
- return None
+Please tell me if the patch does not apply. It should be trivial to apply
+manually, as it's just a block of text that needs to be inserted near the other
+``*conf:`` targets in scripts/kconfig/Makefile.
- def get_symbol(self, name):
- """Returns the symbol with name 'name', or None if no such symbol
- appears in the configuration. An alternative shorthand is conf[name],
- where conf is a Config instance, though that will instead raise
- KeyError if the symbol does not exist."""
- return self._syms.get(name)
+If you do not wish to install Kconfiglib via pip, the Makefile patch is set up
+so that you can also just clone Kconfiglib into the kernel root:
- def __getitem__(self, name):
- """Returns the symbol with name 'name'. Raises KeyError if the symbol
- does not appear in the configuration."""
- return self._syms[name]
+ $ git clone git://github.com/ulfalizer/Kconfiglib.git
+ $ git am Kconfiglib/makefile.patch
- def get_symbols(self, all_symbols=True):
- """Returns a list of symbols from the configuration. An alternative for
- iterating over all defined symbols (in the order of definition) is
+(Warning: The directory name Kconfiglib/ is significant in this case, because
+it's added to PYTHONPATH by the new targets in makefile.patch.)
- for sym in config:
- ...
+Look further down for a motivation for the Makefile patch and for instructions
+on how you can use Kconfiglib without it.
- which relies on Config implementing __iter__() and is equivalent to
+The Makefile patch adds the following targets:
- for sym in config.get_symbols(False):
- ...
- all_symbols (default: True): If True, all symbols -- including special
- and undefined symbols -- will be included in the result, in an
- undefined order. If False, only symbols actually defined and not
- merely referred to in the configuration will be included in the
- result, and will appear in the order that they are defined within
- the Kconfig configuration files."""
- return list(self._syms.values()) if all_symbols else \
- self._defined_syms
+make [ARCH=<arch>] iscriptconfig
+--------------------------------
- def __iter__(self):
- """Convenience function for iterating over the set of all defined
- symbols in the configuration, used like
+This target gives an interactive Python prompt where a Kconfig instance has
+been preloaded and is available in 'kconf'. To change the Python interpreter
+used, pass PYTHONCMD=<executable> to make. The default is "python".
- for sym in conf:
- ...
+To get a feel for the API, try evaluating and printing the symbols in
+kconf.defined_syms, and explore the MenuNode menu tree starting at
+kconf.top_node by following 'next' and 'list' pointers.
- The iteration happens in the order of definition within the Kconfig
- configuration files. Symbols only referred to but not defined will not
- be included, nor will the special symbols n, m, and y. If you want to
- include such symbols as well, see config.get_symbols()."""
- return iter(self._defined_syms)
-
- def get_choices(self):
- """Returns a list containing all choice statements in the
- configuration, in the order they appear in the Kconfig files."""
- return self._choices
-
- def get_menus(self):
- """Returns a list containing all menus in the configuration, in the
- order they appear in the Kconfig files."""
- return self._menus
-
- def get_comments(self):
- """Returns a list containing all comments in the configuration, in the
- order they appear in the Kconfig files."""
- return self._comments
-
- def get_top_level_items(self):
- """Returns a list containing the items (symbols, menus, choices, and
- comments) at the top level of the configuration -- that is, all items
- that do not appear within a menu or choice. The items appear in the
- same order as within the configuration."""
- return self._top_block
+The item contained in a menu node is found in MenuNode.item (note that this can
+be one of the constants MENU and COMMENT), and all symbols and choices have a
+'nodes' attribute containing their menu nodes (usually only one). Printing a
+menu node will print its item, in Kconfig format.
- def load_config(self, filename, replace=True):
- """Loads symbol values from a file in the familiar .config format.
- Equivalent to calling Symbol.set_user_value() to set each of the
- values.
+If you want to look up a symbol by name, use the kconf.syms dictionary.
- "# CONFIG_FOO is not set" within a .config file is treated specially
- and sets the user value of FOO to 'n'. The C implementation works the
- same way.
- filename: The .config file to load. $-references to existing
- environment variables will be expanded. For scripts to work even when
- an alternative build directory is used with the Linux kernel, you
- need to refer to the top-level kernel directory with "$srctree".
+make scriptconfig SCRIPT=<script> [SCRIPT_ARG=<arg>]
+----------------------------------------------------
- replace (default: True): True if the configuration should replace the
- old configuration; False if it should add to it."""
+This target runs the Python script given by the SCRIPT parameter on the
+configuration. sys.argv[1] holds the name of the top-level Kconfig file
+(currently always "Kconfig" in practice), and sys.argv[2] holds the SCRIPT_ARG
+argument, if given.
- # Put this first so that a missing file doesn't screw up our state
- filename = os.path.expandvars(filename)
- line_feeder = _FileFeed(filename)
+See the examples/ subdirectory for example scripts.
- self._config_filename = filename
- #
- # Read header
- #
+Using Kconfiglib without the Makefile targets
+=============================================
- if not self._is_header_line(line_feeder.peek_next()):
- self._config_header = None
- else:
- # Kinda inefficient, but this is an unlikely hotspot
- self._config_header = ""
- while self._is_header_line(line_feeder.peek_next()):
- self._config_header += line_feeder.get_next()[1:]
- # Makes c.write_config(".config", c.get_config_header()) preserve
- # the header exactly. We also handle weird cases like a .config
- # file with just "# foo" and no trailing newline in it (though we
- # would never generate that ourselves), hence the slight
- # awkwardness.
- if self._config_header.endswith("\n"):
- self._config_header = self._config_header[:-1]
+The make targets are only needed for a trivial reason: The Kbuild makefiles
+export environment variables which are referenced inside the Kconfig files (via
+'option env="ENV_VARIABLE"').
- #
- # Read assignments. Hotspot for some workloads.
- #
+In practice, the only variables referenced (as of writing, and for many years)
+are ARCH, SRCARCH, and KERNELVERSION. To run Kconfiglib without the Makefile
+patch, do this:
- if replace:
- # This invalidates all symbols as a side effect
- self.unset_user_values()
- else:
- self._invalidate_all()
+ $ ARCH=x86 SRCARCH=x86 KERNELVERSION=`make kernelversion` python
+ >>> import kconfiglib
+ >>> kconf = kconfiglib.Kconfig() # filename defaults to "Kconfig"
- # Small optimization
- set_re_match = self._set_re.match
- unset_re_match = self._unset_re.match
+Search the top-level Makefile for "Additional ARCH settings" to see other
+possibilities for ARCH and SRCARCH. Kconfiglib will print a warning if an unset
+environment variable is referenced inside the Kconfig files.
- while 1:
- line = line_feeder.get_next()
- if line is None:
- return
-
- line = line.rstrip()
-
- set_match = set_re_match(line)
- if set_match:
- name, val = set_match.groups()
- if name not in self._syms:
- self._warn_undef_assign_load(
- name, val, line_feeder.filename, line_feeder.linenr)
- continue
- sym = self._syms[name]
+Gotcha
+******
- if sym._type == STRING and val.startswith('"'):
- if len(val) < 2 or val[-1] != '"':
- self._warn("malformed string literal",
- line_feeder.filename,
- line_feeder.linenr)
- continue
- # Strip quotes and remove escapings. The unescaping
- # procedure should be safe since " can only appear as \"
- # inside the string.
- val = val[1:-1].replace('\\"', '"') \
- .replace("\\\\", "\\")
-
- if sym._is_choice_sym:
- user_mode = sym._parent._user_mode
- if user_mode is not None and user_mode != val:
- self._warn("assignment to {} changes mode of "
- 'containing choice from "{}" to "{}".'
- .format(name, val, user_mode),
- line_feeder.filename,
- line_feeder.linenr)
+It's important to set $SRCARCH even if you don't care about values and only
+want to extract information from Kconfig files, because the top-level Makefile
+does this (as of writing):
- else:
- unset_match = unset_re_match(line)
- if not unset_match:
- continue
+ source "arch/$SRCARCH/Kconfig"
- name = unset_match.group(1)
- if name not in self._syms:
- self._warn_undef_assign_load(
- name, val, line_feeder.filename, line_feeder.linenr)
- continue
+If $SRCARCH is not set, this expands to "arch//Kconfig", and arch/Kconfig
+happens to be an existing file, giving something that appears to work but is
+actually a truncated configuration. The available symbols will differ depending
+on the arch as well.
- sym = self._syms[name]
- val = "n"
- # Done parsing the assignment. Set the value.
+Intro to symbol values
+======================
- if sym._user_val is not None:
- self._warn('{} set more than once. Old value: "{}", new '
- 'value: "{}".'
- .format(name, sym._user_val, val),
- line_feeder.filename, line_feeder.linenr)
+Kconfiglib has the same assignment semantics as the C implementation.
- sym._set_user_value_no_invalidate(val, True)
+Any symbol can be assigned a value by the user (via Kconfig.load_config() or
+Symbol.set_value()), but this user value is only respected if the symbol is
+visible, which corresponds to it (currently) being visible in the menuconfig
+interface.
- def write_config(self, filename, header=None):
- """Writes out symbol values in the familiar .config format.
+Symbols without prompts are never visible (setting a user value on them is
+pointless). For symbols with prompts, the visibility of the symbol is
+determined by the condition on the prompt.
- Kconfiglib makes sure the format matches what the C implementation
- would generate, down to whitespace. This eases testing.
+Dependencies from parents and 'if'/'depends on' are propagated to properties,
+including prompts, so these two configurations are logically equivalent:
- filename: The filename under which to save the configuration.
+(1)
- header (default: None): A textual header that will appear at the
- beginning of the file, with each line commented out automatically.
- Does not need to include a trailing newline. None means no
- header."""
+ menu "menu"
+ depends on A
- # Symbol._already_written is set to True when _add_config_strings() is
- # called on a symbol, so that symbols defined in multiple locations
- # only get one .config entry. We reset it prior to writing out a new
- # .config. It only needs to be reset for defined symbols, because
- # undefined symbols will never have _add_config_strings() called on
- # them (because they do not appear in the block structure rooted at
- # _top_block).
- #
- # The C implementation reuses _write_to_conf for this, but we cache
- # _write_to_conf together with the value and don't invalidate cached
- # values when writing .config files, so that won't work.
- for sym in self._defined_syms:
- sym._already_written = False
+ if B
- # Build configuration. Avoiding string concatenation is worthwhile at
- # least for PyPy.
- config_strings = []
- add_fn = config_strings.append
- for item in self._top_block:
- item._add_config_strings(add_fn)
+ config FOO
+ tristate "foo" if D
+ default y
+ depends on C
- with open(filename, "w") as f:
- # Write header
- if header is not None:
- f.writelines(["#" + line
- for line in (header + "\n").splitlines(True)])
- # Write configuration
- f.writelines(config_strings)
-
- def eval(self, s):
- """Returns the value of the expression 's' -- where 's' is represented
- as a string -- in the context of the configuration. Raises
- Kconfig_Syntax_Error if syntax errors are detected in 's'.
-
- For example, if FOO and BAR are tristate symbols at least one of which
- has the value "y", then config.eval("y && (FOO || BAR)") => "y"
-
- This function always yields a tristate value. To get the value of
- non-bool, non-tristate symbols, use Symbol.get_value().
-
- The result of this function is consistent with how evaluation works for
- conditional expressions in the configuration as well as in the C
- implementation. "m" and m are rewritten as '"m" && MODULES' and 'm &&
- MODULES', respectively, and a result of "m" will get promoted to "y" if
- we're running without modules.
-
- Syntax checking is somewhat lax, partly to be compatible with lax
- parsing in the C implementation."""
- return self._eval_expr(self._parse_expr(self._tokenize(s, True),
- None, # Current symbol/choice
- s,
- None, # filename
- None, # linenr
- True)) # transform_m
-
- def unset_user_values(self):
- """Resets the values of all symbols, as if Config.load_config() or
- Symbol.set_user_value() had never been called."""
-
- # set_user_value() already rejects undefined symbols, and they don't
- # need to be invalidated (because their value never changes), so we can
- # just iterate over defined symbols.
-
- for sym in self._defined_syms:
- # We're iterating over all symbols already, so no need for symbols
- # to invalidate their dependent symbols
- sym._unset_user_value_no_recursive_invalidate()
-
- def set_print_warnings(self, print_warnings):
- """Determines whether warnings related to this configuration (for
- things like attempting to assign illegal values to symbols with
- Symbol.set_user_value()) should be printed to stderr.
-
- print_warnings: True if warnings should be printed."""
- self._print_warnings = print_warnings
-
- def set_print_undef_assign(self, print_undef_assign):
- """Determines whether informational messages related to assignments to
- undefined symbols should be printed to stderr for this configuration.
-
- print_undef_assign: If True, such messages will be printed."""
- self._print_undef_assign = print_undef_assign
+ endif
- def __str__(self):
- """Returns a string containing various information about the Config."""
- return _lines("Configuration",
- "File : " +
- self._kconfig_filename,
- "Base directory : " +
- self._base_dir,
- "Value of $ARCH at creation time : " +
- ("(not set)"
- if self._arch is None
- else self._arch),
- "Value of $SRCARCH at creation time : " +
- ("(not set)"
- if self._srcarch is None
- else self._srcarch),
- "Value of $srctree at creation time : " +
- ("(not set)"
- if self._srctree is None
- else self._srctree),
- "Most recently loaded .config : " +
- ("(no .config loaded)"
- if self._config_filename is None
- else self._config_filename),
- "Print warnings : " +
- str(self._print_warnings),
- "Print assignments to undefined symbols : " +
- str(self._print_undef_assign))
+ endmenu
- #
- # Private methods
- #
+(2)
- #
- # Kconfig parsing
- #
+ menu "menu"
+ depends on A
- def _parse_file(self, filename, parent, deps, visible_if_deps, block):
- """Parses the Kconfig file 'filename'. Appends the Items in the file
- (and any file it sources) to the list passed in the 'block' parameter.
- See _parse_block() for the meaning of the parameters."""
- self._parse_block(_FileFeed(filename), None, parent, deps,
- visible_if_deps, block)
+ config FOO
+ tristate "foo" if A && B && C && D
+ default y if A && B && C
- def _parse_block(self, line_feeder, end_marker, parent, deps,
- visible_if_deps, block):
- """Parses a block, which is the contents of either a file or an if,
- menu, or choice statement. Appends the Items to the list passed in the
- 'block' parameter.
+ endmenu
- line_feeder: A _FileFeed instance feeding lines from a file. The
- Kconfig language is line-based in practice.
+In this example, A && B && C && D (the prompt condition) needs to be non-n for
+FOO to be visible (assignable). If the value is m, the symbol can only be
+assigned the value m. The visibility sets an upper bound on the value that can
+be assigned by the user, and any higher user value will be truncated down.
- end_marker: The token that ends the block, e.g. _T_ENDIF ("endif") for
- ifs. None for files.
+'default' properties are independent of the visibility, though a 'default' will
+often get the same condition as the prompt due to dependency propagation.
+'default' properties are used if the symbol is not visible or has no user
+value.
- parent: The enclosing menu or choice, or None if we're at the top
- level.
+Symbols with no (active) user value and no (active) 'default' default to n for
+bool/tristate symbols, and to the empty string for other symbols.
- deps: Dependencies from enclosing menus, choices and ifs.
+'select' works similarly to symbol visibility, but sets a lower bound on the
+value of the symbol. The lower bound is determined by the value of the
+select*ing* symbol. 'select' does not respect visibility, so non-visible
+symbols can be forced to a particular (minimum) value by a select as well.
- visible_if_deps (default: None): 'visible if' dependencies from
- enclosing menus.
+For non-bool/tristate symbols, it only matters whether the visibility is n or
+non-n: m visibility acts the same as y visibility.
- block: The list to add items to."""
+Conditions on 'default' and 'select' work in mostly intuitive ways. If the
+condition is n, the 'default' or 'select' is disabled. If it is m, the
+'default' or 'select' value (the value of the selecting symbol) is truncated
+down to m.
- while 1:
- # See the _end_line description in Config.__init__()
- if self._end_line is not None:
- line = self._end_line
- tokens = self._end_line_tokens
- self._end_line = None
- else:
- line = line_feeder.get_next()
- if line is None:
- if end_marker is not None:
- raise Kconfig_Syntax_Error("Unexpected end of file " +
- line_feeder.filename)
- return
+When writing a configuration with Kconfig.write_config(), only symbols that are
+visible, have an (active) default, or are selected will get written out (note
+that this includes all symbols that would accept user values). Kconfiglib
+matches the .config format produced by the C implementations down to the
+character. This eases testing.
- tokens = self._tokenize(line, False, line_feeder.filename,
- line_feeder.linenr)
+In Kconfiglib, the set of (currently) assignable values for a bool/tristate
+symbol appear in Symbol.assignable. For other symbol types, just check if
+sym.visibility is non-0 (non-n).
- t0 = tokens.get_next()
- if t0 is None:
- continue
- # Cases are ordered roughly by frequency, which speeds things up a
- # bit
+Intro to the menu tree
+======================
- # This also handles 'menuconfig'. See the comment in the token
- # definitions.
- if t0 == _T_CONFIG:
- # The tokenizer will automatically allocate a new Symbol object
- # for any new names it encounters, so we don't need to worry
- # about that here.
- sym = tokens.get_next()
+The menu structure, as seen in e.g. menuconfig, is represented by a tree of
+MenuNode objects. The top node of the configuration corresponds to an implicit
+top-level menu, the title of which is shown at the top in the standard
+menuconfig interface. (The title with variables expanded is available in
+Kconfig.mainmenu_text in Kconfiglib.)
- # Symbols defined in multiple places get the parent of their
- # first definition. However, for symbols whose parents are
- # choice statements, the choice statement takes precedence.
- if not sym._is_defined or isinstance(parent, Choice):
- sym._parent = parent
- sym._is_defined = True
+The top node is found in Kconfig.top_node. From there, you can visit child menu
+nodes by following the 'list' pointer, and any following menu nodes by
+following the 'next' pointer. Usually, a non-None 'list' pointer indicates a
+menu or Choice, but menu nodes for symbols can sometimes have a non-None 'list'
+pointer too due to submenus created implicitly from dependencies.
- self._parse_properties(line_feeder, sym, deps, visible_if_deps)
+MenuNode.item is either a Symbol or a Choice object, or one of the constants
+MENU and COMMENT. The prompt of the menu node (which also holds the text for
+menus and comments) can be found in MenuNode.prompt. For Symbol and Choice,
+MenuNode.help holds the help text (if any, otherwise None).
- self._defined_syms.append(sym)
- block.append(sym)
+Note that prompts and help texts for symbols and choices are stored in the menu
+node. This makes it possible to define a symbol in multiple locations with a
+different prompt or help text in each location.
- elif t0 == _T_SOURCE:
- kconfig_file = tokens.get_next()
- exp_kconfig_file = self._expand_sym_refs(kconfig_file)
-
- # Hack: Avoid passing on a "./" prefix in the common case of
- # 'base_dir' defaulting to ".", just to give less awkward
- # results from e.g. get_def/ref_locations(). Maybe this could
- # be handled in a nicer way.
- if self._base_dir == ".":
- filename = exp_kconfig_file
- else:
- filename = os.path.join(self._base_dir, exp_kconfig_file)
-
- if not os.path.exists(filename):
- raise IOError(
- '{}:{}: sourced file "{}" (expands to "{}") not '
- "found. Perhaps base_dir (argument to "
- 'Config.__init__(), currently "{}") is set to the '
- "the wrong value. Also note that e.g. $FOO in a "
- "'source' statement does not refer to the "
- "environment variable FOO, but rather to the Kconfig "
- "symbol FOO (which would commonly have "
- "'option env=\"FOO\"' in its definition)."
- .format(line_feeder.filename, line_feeder.linenr,
- kconfig_file, exp_kconfig_file,
- self._base_dir))
-
- # Add items to the same block
- self._parse_file(filename, parent, deps, visible_if_deps,
- block)
-
- elif t0 == end_marker:
- # We have reached the end of the block
- return
+This organization mirrors the C implementation. MenuNode is called
+'struct menu' there, but I thought "menu" was a confusing name.
- elif t0 == _T_IF:
- # If statements are treated as syntactic sugar for adding
- # dependencies to enclosed items and do not have an explicit
- # object representation.
-
- dep_expr = self._parse_expr(tokens, None, line,
- line_feeder.filename,
- line_feeder.linenr, True)
- # Add items to the same block
- self._parse_block(line_feeder, _T_ENDIF, parent,
- _make_and(dep_expr, deps),
- visible_if_deps, block)
+The list of menu nodes for a Symbol or Choice can be found in the
+Symbol/Choice.nodes attribute.
- elif t0 == _T_COMMENT:
- comment = Comment()
- comment._config = self
- comment._parent = parent
- comment._filename = line_feeder.filename
- comment._linenr = line_feeder.linenr
- comment._text = tokens.get_next()
+It is possible to give a Choice a name and define it in multiple locations,
+hence why Choice.nodes is a list. In practice, you're unlikely to ever see a
+choice defined in more than one location. I don't think I've even seen a named
+choice outside of the test suite.
- self._parse_properties(line_feeder, comment, deps,
- visible_if_deps)
- self._comments.append(comment)
- block.append(comment)
+Intro to expressions
+====================
- elif t0 == _T_MENU:
- menu = Menu()
- menu._config = self
- menu._parent = parent
- menu._filename = line_feeder.filename
- menu._linenr = line_feeder.linenr
- menu._title = tokens.get_next()
-
- self._parse_properties(line_feeder, menu, deps,
- visible_if_deps)
-
- # This needs to go before _parse_block() so that we get the
- # proper menu ordering in the case of nested menus
- self._menus.append(menu)
- # Parse contents and put Items in menu._block
- self._parse_block(line_feeder, _T_ENDMENU, menu,
- menu._menu_dep,
- _make_and(visible_if_deps,
- menu._visible_if_expr),
- menu._block)
-
- block.append(menu)
+Expressions can be evaluated with the expr_value() function and printed with
+the expr_str() function (these are used internally as well). Evaluating an
+expression always yields a tristate value, where n, m, and y are represented as
+0, 1, and 2, respectively.
- elif t0 == _T_CHOICE:
- name = tokens.get_next()
- if name is None:
- choice = Choice()
- self._choices.append(choice)
- else:
- # Named choice
- choice = self._named_choices.get(name)
- if choice is None:
- choice = Choice()
- choice._name = name
- self._named_choices[name] = choice
- self._choices.append(choice)
+The following table should help you figure out how expressions are represented.
+A, B, C, ... are symbols (Symbol instances), NOT is the kconfiglib.NOT
+constant, etc.
- choice._config = self
- choice._parent = parent
+Expression Representation
+---------- --------------
+A A
+"A" A (constant symbol)
+!A (NOT, A)
+A && B (AND, A, B)
+A && B && C (AND, A, (AND, B, C))
+A || B (OR, A, B)
+A || (B && C && D) (OR, A, (AND, B, (AND, C, D)))
+A = B (EQUAL, A, B)
+A != "foo" (UNEQUAL, A, foo (constant symbol))
+A && B = C && D (AND, A, (AND, (EQUAL, B, C), D))
+n Kconfig.n (constant symbol)
+m Kconfig.m (constant symbol)
+y Kconfig.y (constant symbol)
+"y" Kconfig.y (constant symbol)
- choice._def_locations.append((line_feeder.filename,
- line_feeder.linenr))
+Strings like "foo" in 'default "foo"' or 'depends on SYM = "foo"' are
+represented as constant symbols, so the only values that appear in expressions
+are symbols***. This mirrors the C implementation.
- self._parse_properties(line_feeder, choice, deps,
- visible_if_deps)
+***For choice symbols, the parent Choice will appear in expressions as well,
+but it's usually invisible as the value interfaces of Symbol and Choice are
+identical. This mirrors the C implementation and makes different choice modes
+"just work".
- # Parse contents and put Items in choice._block
- self._parse_block(line_feeder, _T_ENDCHOICE, choice, deps,
- visible_if_deps, choice._block)
+Manual evaluation examples:
- choice._determine_actual_symbols()
+ - The value of A && B is min(A.tri_value, B.tri_value)
- # If no type is specified for the choice, its type is that of
- # the first choice item with a specified type
- if choice._type == UNKNOWN:
- for item in choice._actual_symbols:
- if item._type != UNKNOWN:
- choice._type = item._type
- break
+ - The value of A || B is max(A.tri_value, B.tri_value)
- # Each choice item of UNKNOWN type gets the type of the choice
- for item in choice._actual_symbols:
- if item._type == UNKNOWN:
- item._type = choice._type
+ - The value of !A is 2 - A.tri_value
- block.append(choice)
+ - The value of A = B is 2 (y) if A.str_value == B.str_value, and 0 (n)
+ otherwise. Note that str_value is used here instead of tri_value.
- elif t0 == _T_MAINMENU:
- text = tokens.get_next()
- if self._mainmenu_text is not None:
- self._warn("overriding 'mainmenu' text. "
- 'Old value: "{}", new value: "{}".'
- .format(self._mainmenu_text, text),
- line_feeder.filename, line_feeder.linenr)
- self._mainmenu_text = text
+ For constant (as well as undefined) symbols, str_value matches the name of
+ the symbol. This mirrors the C implementation and explains why
+ 'depends on SYM = "foo"' above works as expected.
- else:
- _parse_error(line, "unrecognized construct",
- line_feeder.filename, line_feeder.linenr)
-
- def _parse_cond(self, tokens, stmt, line, filename, linenr):
- """Parses an optional 'if <expr>' construct and returns the parsed
- <expr>, or None if the next token is not _T_IF."""
- return self._parse_expr(tokens, stmt, line, filename, linenr, True) \
- if tokens.check(_T_IF) else None
-
- def _parse_val_and_cond(self, tokens, stmt, line, filename, linenr):
- """Parses '<expr1> if <expr2>' constructs, where the 'if' part is
- optional. Returns a tuple containing the parsed expressions, with
- None as the second element if the 'if' part is missing."""
- return (self._parse_expr(tokens, stmt, line, filename, linenr, False),
- self._parse_cond(tokens, stmt, line, filename, linenr))
-
- def _parse_properties(self, line_feeder, stmt, deps, visible_if_deps):
- """Parsing of properties for symbols, menus, choices, and comments.
- Takes care of propagating dependencies from enclosing menus and ifs."""
-
- # In case the symbol is defined in multiple locations, we need to
- # remember what prompts, defaults, selects, implies, and ranges are new
- # for this definition, as "depends on" should only apply to the local
- # definition.
- new_prompt = None
- new_def_exprs = []
- new_selects = []
- new_implies = []
- new_ranges = []
-
- # Dependencies from 'depends on' statements
- depends_on_expr = None
+n/m/y are automatically converted to the corresponding constant symbols
+"n"/"m"/"y" (Kconfig.n/m/y) during parsing.
- while 1:
- line = line_feeder.get_next()
- if line is None:
- break
+Kconfig.const_syms is a dictionary like Kconfig.syms but for constant symbols.
- filename = line_feeder.filename
- linenr = line_feeder.linenr
+If a condition is missing (e.g., <cond> when the 'if <cond>' is removed from
+'default A if <cond>'), it is actually Kconfig.y. The standard __str__()
+functions just avoid printing 'if y' conditions to give cleaner output.
- tokens = self._tokenize(line, False, filename, linenr)
- t0 = tokens.get_next()
- if t0 is None:
- continue
+Feedback
+========
- # Cases are ordered roughly by frequency, which speeds things up a
- # bit
+Send bug reports, suggestions, and questions to ulfalizer a.t Google's email
+service, or open a ticket on the GitHub page.
+"""
+import errno
+import os
+import platform
+import re
+import sys
- if t0 == _T_DEPENDS:
- if not tokens.check(_T_ON):
- _parse_error(line, 'expected "on" after "depends"',
- filename, linenr)
+# File layout:
+#
+# Public classes
+# Public functions
+# Internal functions
+# Public global constants
+# Internal global constants
- depends_on_expr = \
- _make_and(depends_on_expr,
- self._parse_expr(tokens, stmt, line, filename,
- linenr, True))
+# Line length: 79 columns
- elif t0 == _T_HELP:
- # Find first non-blank (not all-space) line and get its
- # indentation
- line = line_feeder.next_nonblank()
- if line is None:
- stmt._help = ""
- break
- indent = _indentation(line)
- if indent == 0:
- # If the first non-empty lines has zero indent, there is no
- # help text
- stmt._help = ""
- line_feeder.unget()
- break
+#
+# Public classes
+#
- # The help text goes on till the first non-empty line with less
- # indent
- help_lines = [_deindent(line, indent)]
- while 1:
- line = line_feeder.get_next()
- if line is None or \
- (not line.isspace() and _indentation(line) < indent):
- stmt._help = "".join(help_lines)
- break
- help_lines.append(_deindent(line, indent))
+class Kconfig(object):
+ """
+ Represents a Kconfig configuration, e.g. for x86 or ARM. This is the set of
+ symbols, choices, and menu nodes appearing in the configuration. Creating
+ any number of Kconfig objects (including for different architectures) is
+ safe. Kconfiglib doesn't keep any global state.
+
+ The following attributes are available. They should be treated as
+ read-only, and some are implemented through @property magic.
+
+ syms:
+ A dictionary with all symbols in the configuration, indexed by name. Also
+ includes all symbols that are referenced in expressions but never
+ defined, except for constant (quoted) symbols.
+
+ const_syms:
+ A dictionary like 'syms' for constant (quoted) symbols.
+
+ named_choices:
+ A dictionary like 'syms' for named choices (choice FOO). This is for
+ completeness. I've never seen a named choice outside of the test suite.
+
+ defined_syms:
+ A list with all defined symbols, in the same order as they appear in the
+ Kconfig files. Provided as a convenience.
+
+ n/m/y:
+ The predefined constant symbols n/m/y. Also available in const_syms.
+
+ modules:
+ The Symbol instance for the modules symbol. Currently hardcoded to
+ MODULES, which is backwards compatible. Kconfiglib will warn if
+ 'option modules' is set on some other symbol. Tell me if you need proper
+ 'option modules' support.
+
+ 'modules' is never None. If the MODULES symbol is not explicitly defined,
+ its tri_value will be 0 (n), as expected.
+
+ A simple way to enable modules is to do 'kconf.modules.set_value(2)'
+ (provided the MODULES symbol is defined and visible). Modules are
+ disabled by default in the kernel Kconfig files as of writing, though
+ nearly all defconfig files enable them (with 'CONFIG_MODULES=y').
+
+ defconfig_list:
+ The Symbol instance for the 'option defconfig_list' symbol, or None if no
+ defconfig_list symbol exists. The defconfig filename derived from this
+ symbol can be found in Kconfig.defconfig_filename.
+
+ defconfig_filename:
+ The filename given by the defconfig_list symbol. This is taken from the
+ first 'default' with a satisfied condition where the specified file
+ exists (can be opened for reading). If a defconfig file foo/defconfig is
+ not found and $srctree was set when the Kconfig was created,
+ $srctree/foo/defconfig is looked up as well.
+
+ References to Kconfig symbols ("$FOO") in the 'default' properties of the
+ defconfig_filename symbol are are expanded before the file is looked up.
+
+ 'defconfig_filename' is None if either no defconfig_list symbol exists,
+ or if the defconfig_list symbol has no 'default' with a satisfied
+ condition that specifies a file that exists.
+
+ Gotcha: scripts/kconfig/Makefile might pass --defconfig=<defconfig> to
+ scripts/kconfig/conf when running e.g. 'make defconfig'. This option
+ overrides the defconfig_list symbol, meaning defconfig_filename might not
+ always match what 'make defconfig' would use.
+
+ top_node:
+ The menu node (see the MenuNode class) of the implicit top-level menu.
+ Acts as the root of the menu tree.
+
+ mainmenu_text:
+ The prompt (title) of the top_node menu, with Kconfig variable references
+ ("$FOO") expanded. Defaults to "Linux Kernel Configuration" (like in the
+ C tools). Can be changed with the 'mainmenu' statement (see
+ kconfig-language.txt).
+
+ srctree:
+ The value of the $srctree environment variable when the configuration was
+ loaded, or None if $srctree wasn't set. Kconfig and .config files are
+ looked up relative to $srctree if they are not found in the base path
+ (unless absolute paths are used). This is used to support out-of-tree
+ builds. The C tools use this environment variable in the same way.
+
+ Changing $srctree after creating the Kconfig instance has no effect. Only
+ the value when the configuration is loaded matters. This avoids surprises
+ if multiple configurations are loaded with different values for $srctree.
+
+ config_prefix:
+ The value of the $CONFIG_ environment variable when the configuration was
+ loaded. This is the prefix used (and expected) in .config files. Defaults
+ to "CONFIG_". Used in the same way in the C tools.
+
+ Like for srctree, only the value of $CONFIG_ when the configuration is
+ loaded matters.
+ """
+ __slots__ = (
+ "_choices",
+ "_print_undef_assign",
+ "_print_warnings",
+ "_set_re_match",
+ "_unset_re_match",
+ "_warn_no_prompt",
+ "config_prefix",
+ "const_syms",
+ "defconfig_list",
+ "defined_syms",
+ "m",
+ "modules",
+ "n",
+ "named_choices",
+ "srctree",
+ "syms",
+ "top_node",
+ "y",
+
+ # Parsing-related
+ "_parsing_kconfigs",
+ "_reuse_line",
+ "_file",
+ "_filename",
+ "_linenr",
+ "_filestack",
+ "_line",
+ "_tokens",
+ "_tokens_i",
+ "_has_tokens",
+ )
- if line is None:
- break
+ #
+ # Public interface
+ #
- line_feeder.unget()
+ def __init__(self, filename="Kconfig", warn=True):
+ """
+ Creates a new Kconfig object by parsing Kconfig files. Raises
+ KconfigSyntaxError on syntax errors. Note that Kconfig files are not
+ the same as .config files (which store configuration symbol values).
+
+ filename (default: "Kconfig"):
+ The base Kconfig file. For the Linux kernel, you'll want "Kconfig"
+ from the top-level directory, as environment variables will make sure
+ the right Kconfig is included from there (arch/$SRCARCH/Kconfig as of
+ writing).
+
+ If you are using Kconfiglib via 'make scriptconfig', the filename of
+ the base base Kconfig file will be in sys.argv[1]. It's currently
+ always "Kconfig" in practice.
+
+ The $srctree environment variable is used to look up Kconfig files if
+ set. See the class documentation.
+
+ warn (default: True):
+ True if warnings related to this configuration should be printed to
+ stderr. This can be changed later with
+ Kconfig.enable/disable_warnings(). It is provided as a constructor
+ argument since warnings might be generated during parsing.
+ """
+ self.srctree = os.environ.get("srctree")
+
+ self.config_prefix = os.environ.get("CONFIG_")
+ if self.config_prefix is None:
+ self.config_prefix = "CONFIG_"
+
+ # Regular expressions for parsing .config files, with the get() method
+ # assigned directly as a small optimization (microscopic in this case,
+ # but it's consistent with the other regexes)
+ self._set_re_match = re.compile(r"{}(\w+)=(.*)"
+ .format(self.config_prefix)).match
+ self._unset_re_match = re.compile(r"# {}(\w+) is not set"
+ .format(self.config_prefix)).match
+
+ self._print_warnings = warn
+ self._print_undef_assign = False
+
+ self.syms = {}
+ self.const_syms = {}
+ self.defined_syms = []
+ self.named_choices = {}
+ # Used for quickly invalidating all choices
+ self._choices = []
- elif t0 == _T_SELECT:
- if not isinstance(stmt, Symbol):
- _parse_error(line, "only symbols can select", filename,
- linenr)
+ for nmy in "n", "m", "y":
+ sym = Symbol()
+ sym.kconfig = self
+ sym.name = nmy
+ sym.is_constant = True
+ sym.orig_type = TRISTATE
+ sym._cached_tri_val = STR_TO_TRI[nmy]
+
+ self.const_syms[nmy] = sym
+
+ self.n = self.const_syms["n"]
+ self.m = self.const_syms["m"]
+ self.y = self.const_syms["y"]
+
+ # Make n/m/y well-formed symbols
+ for nmy in "n", "m", "y":
+ sym = self.const_syms[nmy]
+ sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
+
+ # This is used to determine whether previously unseen symbols should be
+ # registered. They shouldn't be if we parse expressions after parsing,
+ # as part of Kconfig.eval_string().
+ self._parsing_kconfigs = True
+
+ self.modules = self._lookup_sym("MODULES")
+ self.defconfig_list = None
+
+ # The only predefined symbol besides n/m/y. DEFCONFIG_LIST uses this as
+ # of writing.
+ uname_sym = self._lookup_const_sym("UNAME_RELEASE")
+ uname_sym.orig_type = STRING
+ # env_var doubles as the SYMBOL_AUTO flag from the C implementation, so
+ # just set it to something. The naming breaks a bit here.
+ uname_sym.env_var = "<uname release>"
+ uname_sym.defaults.append(
+ (self._lookup_const_sym(platform.uname()[2]), self.y))
+ self.syms["UNAME_RELEASE"] = uname_sym
+
+ self.top_node = MenuNode()
+ self.top_node.kconfig = self
+ self.top_node.item = MENU
+ self.top_node.visibility = self.y
+ self.top_node.prompt = ("Linux Kernel Configuration", self.y)
+ self.top_node.parent = None
+ self.top_node.dep = self.y
+ self.top_node.filename = filename
+ self.top_node.linenr = 1
- new_selects.append(
- (tokens.get_next(),
- self._parse_cond(tokens, stmt, line, filename, linenr)))
+ # Parse the Kconfig files
- elif t0 == _T_IMPLY:
- if not isinstance(stmt, Symbol):
- _parse_error(line, "only symbols can imply", filename,
- linenr)
+ # These implement a single line of "unget" for the parser
+ self._reuse_line = False
+ self._has_tokens = False
- new_implies.append(
- (tokens.get_next(),
- self._parse_cond(tokens, stmt, line, filename, linenr)))
+ # Keeps track of the location in the parent Kconfig files. Kconfig
+ # files usually source other Kconfig files.
+ self._filestack = []
- elif t0 in (_T_BOOL, _T_TRISTATE, _T_INT, _T_HEX, _T_STRING):
- stmt._type = _TOKEN_TO_TYPE[t0]
- if tokens.peek_next() is not None:
- new_prompt = self._parse_val_and_cond(tokens, stmt, line,
- filename, linenr)
+ # The current parsing location
+ self._filename = filename
+ self._linenr = 0
- elif t0 == _T_DEFAULT:
- new_def_exprs.append(
- self._parse_val_and_cond(
- tokens, stmt, line, filename, linenr))
+ self._file = self._open(filename)
- elif t0 in (_T_DEF_BOOL, _T_DEF_TRISTATE):
- stmt._type = _TOKEN_TO_TYPE[t0]
- if tokens.peek_next() is not None:
- new_def_exprs.append(
- self._parse_val_and_cond(tokens, stmt, line, filename,
- linenr))
+ self._parse_block(None, # end_token
+ self.top_node, # parent
+ self.y, # visible_if_deps
+ self.top_node) # prev_node
+ self.top_node.list = self.top_node.next
+ self.top_node.next = None
- elif t0 == _T_PROMPT:
- # 'prompt' properties override each other within a single
- # definition of a symbol, but additional prompts can be added
- # by defining the symbol multiple times; hence 'new_prompt'
- # instead of 'prompt'.
- new_prompt = self._parse_val_and_cond(tokens, stmt, line,
- filename, linenr)
+ self._parsing_kconfigs = False
- elif t0 == _T_RANGE:
- new_ranges.append(
- (tokens.get_next(),
- tokens.get_next(),
- self._parse_cond(tokens, stmt, line, filename, linenr)))
+ # Do various post-processing of the menu tree
+ _finalize_tree(self.top_node)
- elif t0 == _T_OPTION:
- if tokens.check(_T_ENV) and tokens.check(_T_EQUAL):
- env_var = tokens.get_next()
+ # Build Symbol._dependents for all symbols
+ self._build_dep()
- stmt._is_special = True
- stmt._is_from_env = True
+ self._warn_no_prompt = False
- if env_var not in os.environ:
- self._warn("the symbol {} references the non-existent "
- "environment variable {} and will get the "
- "empty string as its value. If you're "
- "using Kconfiglib via "
- "'make (i)scriptconfig', it should have "
- "set up the environment correctly for you. "
- "If you still got this message, that "
- "might be an error, and you should email "
- "ulfalizer a.t Google's email service."""
- .format(stmt._name, env_var),
- filename, linenr)
-
- stmt._cached_val = ""
- else:
- stmt._cached_val = os.environ[env_var]
+ @property
+ def mainmenu_text(self):
+ """
+ See the class documentation.
+ """
+ return self._expand_syms(self.top_node.prompt[0])
- elif tokens.check(_T_DEFCONFIG_LIST):
- if self._defconfig_sym is None:
- self._defconfig_sym = stmt
- else:
- self._warn("'option defconfig_list' set on multiple "
- "symbols ({0} and {1}). Only {0} will be "
- "used."
- .format(self._defconfig_sym._name,
- stmt._name))
+ @property
+ def defconfig_filename(self):
+ """
+ See the class documentation.
+ """
+ if not self.defconfig_list:
+ return None
- elif tokens.check(_T_MODULES):
- # To reduce warning spam, only warn if 'option modules' is
- # set on some symbol that isn't MODULES, which should be
- # safe. I haven't run into any projects that make use
- # modules besides the kernel yet, and there it's likely to
- # keep being called "MODULES".
- if stmt._name != "MODULES":
- self._warn("the 'modules' option is not supported. "
- "Let me know if this is a problem for you; "
- "it shouldn't be that hard to implement. "
- "(Note that modules are still supported -- "
- "Kconfiglib just assumes the symbol name "
- "MODULES, like older versions of the C "
- "implementation did when 'option modules' "
- "wasn't used.)",
- filename, linenr)
+ for filename, cond in self.defconfig_list.defaults:
+ if expr_value(cond):
+ try:
+ with self._open(self._expand_syms(filename.str_value)) as f:
+ return f.name
+ except IOError:
+ continue
- elif tokens.check(_T_ALLNOCONFIG_Y):
- if not isinstance(stmt, Symbol):
- _parse_error(line,
- "the 'allnoconfig_y' option is only "
- "valid for symbols",
- filename, linenr)
- stmt._allnoconfig_y = True
+ return None
- else:
- _parse_error(line, "unrecognized option", filename, linenr)
+ def load_config(self, filename, replace=True):
+ """
+ Loads symbol values from a file in the .config format. Equivalent to
+ calling Symbol.set_value() to set each of the values.
- elif t0 == _T_VISIBLE:
- if not tokens.check(_T_IF):
- _parse_error(line, 'expected "if" after "visible"',
- filename, linenr)
- if not isinstance(stmt, Menu):
- _parse_error(line,
- "'visible if' is only valid for menus",
- filename, linenr)
-
- stmt._visible_if_expr = \
- _make_and(stmt._visible_if_expr,
- self._parse_expr(tokens, stmt, line, filename,
- linenr, True))
+ "# CONFIG_FOO is not set" within a .config file sets the user value of
+ FOO to n. The C tools work the same way.
- elif t0 == _T_OPTIONAL:
- if not isinstance(stmt, Choice):
- _parse_error(line,
- '"optional" is only valid for choices',
- filename,
- linenr)
- stmt._optional = True
+ filename:
+ The file to load. Respects $srctree if set (see the class
+ documentation).
- else:
- # See the _end_line description in Config.__init__()
- self._end_line = line
- tokens.unget_all()
- self._end_line_tokens = tokens
- break
+ replace (default: True):
+ True if all existing user values should be cleared before loading the
+ .config.
+ """
+ # Disable the warning about assigning to symbols without prompts. This
+ # is normal and expected within a .config file.
+ self._warn_no_prompt = False
+
+ # This stub only exists to make sure _warn_no_prompt gets reenabled
+ try:
+ self._load_config(filename, replace)
+ finally:
+ self._warn_no_prompt = True
+
+ def _load_config(self, filename, replace):
+ with self._open(filename) as f:
+ if replace:
+ # If we're replacing the configuration, keep track of which
+ # symbols and choices got set so that we can unset the rest
+ # later. This avoids invalidating everything and is a tiny bit
+ # faster in the test suite. The main benefit though is that
+ # invalidation must be rock solid for it to work, making it a
+ # good test.
+
+ for sym in self.defined_syms:
+ sym._was_set = False
+
+ for choice in self._choices:
+ choice._was_set = False
+
+ # Small optimizations
+ set_re_match = self._set_re_match
+ unset_re_match = self._unset_re_match
+ syms = self.syms
+
+ for linenr, line in enumerate(f, 1):
+ # The C tools ignore trailing whitespace
+ line = line.rstrip()
+
+ set_match = set_re_match(line)
+ if set_match:
+ name, val = set_match.groups()
+ if name not in syms:
+ self._warn_undef_assign_load(name, val, filename,
+ linenr)
+ continue
- # Done parsing properties. Now add the new
- # prompts/defaults/selects/implies/ranges, with dependencies
- # propagated.
+ sym = syms[name]
+ if not sym.nodes:
+ self._warn_undef_assign_load(name, val, filename,
+ linenr)
+ continue
- # Save original dependencies from enclosing menus and ifs
- stmt._deps_from_containing = deps
+ if sym.orig_type in (BOOL, TRISTATE):
+ # The C implementation only checks the first character
+ # to the right of '=', for whatever reason
+ if not ((sym.orig_type == BOOL and
+ val.startswith(("n", "y"))) or \
+ (sym.orig_type == TRISTATE and
+ val.startswith(("n", "m", "y")))):
+ self._warn("'{}' is not a valid value for the {} "
+ "symbol {}. Assignment ignored."
+ .format(val, TYPE_TO_STR[sym.orig_type],
+ sym.name))
+ continue
+
+ # We represent tristate values as 0, 1, 2
+ val = STR_TO_TRI[val[0]]
+
+ if sym.choice and val:
+ # During .config loading, we infer the mode of the
+ # choice from the kind of values that are assigned
+ # to the choice symbols
+
+ prev_mode = sym.choice.user_value
+ if prev_mode is not None and prev_mode != val:
+ self._warn("both m and y assigned to symbols "
+ "within the same choice",
+ filename, linenr)
+
+ # Set the choice's mode
+ # TODO: this causes redundant invalidation
+ sym.choice.set_value(val)
+
+ elif sym.orig_type == STRING:
+ string_match = _conf_string_re_match(val)
+ if not string_match:
+ self._warn("Malformed string literal in "
+ "assignment to {}. Assignment ignored."
+ .format(sym.name),
+ filename, linenr)
+ continue
+
+ val = unescape(string_match.group(1))
- # The parent deps + the 'depends on' deps. This is also used to
- # implicitly create menus when a symbol depends on the previous symbol,
- # hence the name. In the C implementation, it's the dependency of a
- # menu "node".
- stmt._menu_dep = _make_and(deps, depends_on_expr)
+ else:
+ unset_match = unset_re_match(line)
+ if not unset_match:
+ continue
- if isinstance(stmt, (Menu, Comment)):
- # For display purposes
- stmt._orig_deps = depends_on_expr
- else:
- # Symbol or Choice
+ name = unset_match.group(1)
+ if name not in syms:
+ self._warn_undef_assign_load(name, "n", filename,
+ linenr)
+ continue
- if isinstance(stmt, Symbol):
- stmt._direct_deps = _make_or(stmt._direct_deps, stmt._menu_dep)
+ sym = syms[name]
+ if sym.orig_type not in (BOOL, TRISTATE):
+ continue
- # Propagate dependencies to prompts
- if new_prompt is not None:
- prompt, cond_expr = new_prompt
+ val = 0
- # Propagate 'visible if' and 'depends on'
- cond_expr = _make_and(_make_and(cond_expr, visible_if_deps),
- depends_on_expr)
+ # Done parsing the assignment. Set the value.
- # Version without parent dependencies, for display
- stmt._orig_prompts.append((prompt, cond_expr))
+ if sym._was_set:
+ # Use strings for tristate values in the warning
+ if sym.orig_type in (BOOL, TRISTATE):
+ display_val = TRI_TO_STR[val]
+ display_user_val = TRI_TO_STR[sym.user_value]
+ else:
+ display_val = val
+ display_user_val = sym.user_value
- # This is what we actually use for evaluation
- stmt._prompts.append((prompt, _make_and(cond_expr, deps)))
+ self._warn('{} set more than once. Old value: "{}", new '
+ 'value: "{}".'
+ .format(name, display_user_val, display_val),
+ filename, linenr)
- # Propagate dependencies to defaults
- for val_expr, cond_expr in new_def_exprs:
- # Version without parent dependencies, for display
- stmt._orig_def_exprs.append(
- (val_expr, _make_and(cond_expr, depends_on_expr)))
+ sym.set_value(val)
- # This is what we actually use for evaluation
- stmt._def_exprs.append(
- (val_expr, _make_and(cond_expr, stmt._menu_dep)))
+ if replace:
+ # If we're replacing the configuration, unset the symbols that
+ # didn't get set
- # Propagate dependencies to ranges
- for low, high, cond_expr in new_ranges:
- # Version without parent dependencies, for display
- stmt._orig_ranges.append(
- (low, high, _make_and(cond_expr, depends_on_expr)))
+ for sym in self.defined_syms:
+ if not sym._was_set:
+ sym.unset_value()
- # This is what we actually use for evaluation
- stmt._ranges.append(
- (low, high, _make_and(cond_expr, stmt._menu_dep)))
+ for choice in self._choices:
+ if not choice._was_set:
+ choice.unset_value()
- # Handle selects
- for target, cond_expr in new_selects:
- # Used for display
- stmt._orig_selects.append(
- (target, _make_and(cond_expr, depends_on_expr)))
+ def write_config(self, filename,
+ header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"):
+ r"""
+ Writes out symbol values in the .config format.
- # Modify the dependencies of the selected symbol
- target._rev_dep = \
- _make_or(target._rev_dep,
- _make_and(stmt, _make_and(cond_expr,
- stmt._menu_dep)))
+ filename:
+ Self-explanatory.
- # Handle implies
- for target, cond_expr in new_implies:
- # Used for display
- stmt._orig_implies.append(
- (target, _make_and(cond_expr, depends_on_expr)))
+ header (default: "# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"):
+ Text that will be inserted verbatim at the beginning of the file. You
+ would usually want each line to start with '#' to make it a comment,
+ and include a final terminating newline.
+ """
+ with open(filename, "w") as f:
+ f.write(header)
+ f.writelines(self._get_config_strings())
- # Modify the dependencies of the implied symbol
- target._weak_rev_dep = \
- _make_or(target._weak_rev_dep,
- _make_and(stmt, _make_and(cond_expr,
- stmt._menu_dep)))
+ def eval_string(self, s):
+ """
+ Returns the tristate value of the expression 's', represented as 0, 1,
+ and 2 for n, m, and y, respectively. Raises KconfigSyntaxError if
+ syntax errors are detected in 's'. Warns if undefined symbols are
+ referenced.
+
+ As an example, if FOO and BAR are tristate symbols at least one of
+ which has the value y, then config.eval_string("y && (FOO || BAR)")
+ returns 2 (y).
+
+ To get the string value of non-bool/tristate symbols, use
+ Symbol.str_value. eval_string() always returns a tristate value, and
+ all non-bool/tristate symbols have the tristate value 0 (n).
+
+ The expression parsing is consistent with how parsing works for
+ conditional ('if ...') expressions in the configuration, and matches
+ the C implementation. m is rewritten to 'm && MODULES', so
+ eval_string("m") will return 0 (n) unless modules are enabled.
+ """
+ # The parser is optimized to be fast when parsing Kconfig files (where
+ # an expression can never appear at the beginning of a line). We have
+ # to monkey-patch things a bit here to reuse it.
- def _parse_expr(self, feed, cur_item, line, filename, linenr, transform_m):
- """Parses an expression from the tokens in 'feed' using a simple
- top-down approach. The result has the form
- '(<operator> <operand 1> <operand 2>)' where <operator> is e.g.
- kconfiglib._AND. If there is only one operand (i.e., no && or ||), then
- the operand is returned directly. This also goes for subexpressions.
+ self._filename = None
- As an example, A && B && (!C || D == 3) is represented as the tuple
- structure (_AND, A, (_AND, B, (_OR, (_NOT, C), (_EQUAL, D, 3)))), with
- the Symbol objects stored directly in the expression.
+ self._line = "if " + s
+ self._tokenize()
+ # Remove the "if " to avoid giving confusing error messages
+ self._line = s
+ # Remove the T_IF token
+ del self._tokens[0]
- feed: _Feed instance containing the tokens for the expression.
+ return expr_value(self._parse_expr(True)) # transform_m
- cur_item: The item (Symbol, Choice, Menu, or Comment) currently being
- parsed, or None if we're not parsing an item. Used for recording
- references to symbols.
+ def unset_values(self):
+ """
+ Resets the user values of all symbols, as if Kconfig.load_config() or
+ Symbol.set_value() had never been called.
+ """
+ self._warn_no_prompt = False
+ try:
+ # set_value() already rejects undefined symbols, and they don't
+ # need to be invalidated (because their value never changes), so we
+ # can just iterate over defined symbols
+ for sym in self.defined_syms:
+ sym.unset_value()
+
+ for choice in self._choices:
+ choice.unset_value()
+ finally:
+ self._warn_no_prompt = True
+
+ def enable_warnings(self):
+ """
+ See Kconfig.__init__().
+ """
+ self._print_warnings = True
- line: The line containing the expression being parsed.
+ def disable_warnings(self):
+ """
+ See Kconfig.__init__().
+ """
+ self._print_warnings = False
- filename: The file containing the expression. None when using
- Config.eval().
+ def enable_undef_warnings(self):
+ """
+ Enables warnings for assignments to undefined symbols. Printed to
+ stderr. Disabled by default since they tend to be spammy for Kernel
+ configurations (and mostly suggests cleanups).
+ """
+ self._print_undef_assign = True
- linenr: The line number containing the expression. None when using
- Config.eval().
+ def disable_undef_warnings(self):
+ """
+ See enable_undef_assign().
+ """
+ self._print_undef_assign = False
- transform_m (default: False): Determines if 'm' should be rewritten to
- 'm && MODULES'. See the Config.eval() docstring."""
+ def __repr__(self):
+ """
+ Returns a string with information about the Kconfig object when it is
+ evaluated on e.g. the interactive Python prompt.
+ """
+ return "<{}>".format(", ".join((
+ "configuration with {} symbols".format(len(self.syms)),
+ 'main menu prompt "{}"'.format(self.mainmenu_text),
+ "srctree not set" if self.srctree is None else
+ 'srctree "{}"'.format(self.srctree),
+ 'config symbol prefix "{}"'.format(self.config_prefix),
+ "warnings " + ("enabled" if self._print_warnings else "disabled"),
+ "undef. symbol assignment warnings " +
+ ("enabled" if self._print_undef_assign else "disabled"),
+ )))
- # Grammar:
- #
- # expr: and_expr ['||' expr]
- # and_expr: factor ['&&' and_expr]
- # factor: <symbol> ['='/'!='/'<'/... <symbol>]
- # '!' factor
- # '(' expr ')'
- #
- # It helps to think of the 'expr: and_expr' case as a single-operand OR
- # (no ||), and of the 'and_expr: factor' case as a single-operand AND
- # (no &&). Parsing code is always a bit tricky.
+ #
+ # Private methods
+ #
- # Mind dump: parse_factor() and two nested loops for OR and AND would
- # work as well. The straightforward implementation there gives a
- # (op, (op, (op, A, B), C), D) parse for A op B op C op D. Representing
- # expressions as (op, [list of operands]) instead goes nicely with that
- # version, but is wasteful for short expressions and complicates
- # expression evaluation and other code that works on expressions (more
- # complicated code likely offsets any performance gain from less
- # recursion too). If we also try to optimize the list representation by
- # merging lists when possible (e.g. when ANDing two AND expressions),
- # we end up allocating a ton of lists instead of reusing expressions,
- # which is bad.
- and_expr = self._parse_and_expr(feed, cur_item, line, filename,
- linenr, transform_m)
+ #
+ # File reading
+ #
- # Return 'and_expr' directly if we have a "single-operand" OR.
- # Otherwise, parse the expression on the right and make an _OR node.
- # This turns A || B || C || D into
- # (_OR, A, (_OR, B, (_OR, C, D))).
- return and_expr \
- if not feed.check(_T_OR) else \
- (_OR, and_expr, self._parse_expr(feed, cur_item, line, filename,
- linenr, transform_m))
+ def _open(self, filename):
+ """
+ First tries to open 'filename', then '$srctree/filename' if $srctree
+ was set when the configuration was loaded.
+ """
+ try:
+ return open(filename)
+ except IOError as e:
+ if not os.path.isabs(filename) and self.srctree is not None:
+ filename = os.path.join(self.srctree, filename)
+ try:
+ return open(filename)
+ except IOError as e2:
+ # This is needed for Python 3, because e2 is deleted after
+ # the try block:
+ #
+ # https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
+ e = e2
+
+ raise IOError(
+ "Could not open '{}' ({}: {}). Perhaps the $srctree "
+ "environment variable (which was {}) is set incorrectly. Note "
+ "that the current value of $srctree is saved when the Kconfig "
+ "instance is created (for consistency and to cleanly "
+ "separate instances)."
+ .format(filename, errno.errorcode[e.errno], e.strerror,
+ "unset" if self.srctree is None else
+ '"{}"'.format(self.srctree)))
+
+ def _enter_file(self, filename):
+ """
+ Jumps to the beginning of a sourced Kconfig file, saving the previous
+ position and file object.
+ """
+ self._filestack.append((self._file, self._filename, self._linenr))
+ try:
+ self._file = self._open(filename)
+ except IOError as e:
+ # Extend the error message a bit in this case
+ raise IOError(
+ "{}:{}: {} Also note that e.g. $FOO in a 'source' "
+ "statement does not refer to the environment "
+ "variable FOO, but rather to the Kconfig Symbol FOO "
+ "(which would commonly have 'option env=\"FOO\"' in "
+ "its definition)."
+ .format(self._filename, self._linenr, e.message))
+
+ self._filename = filename
+ self._linenr = 0
+
+ def _leave_file(self):
+ """
+ Returns from a Kconfig file to the file that sourced it.
+ """
+ self._file.close()
+ self._file, self._filename, self._linenr = self._filestack.pop()
- def _parse_and_expr(self, feed, cur_item, line, filename, linenr,
- transform_m):
+ def _next_line(self):
+ """
+ Returns the next line in the current file, or the empty string at EOF
+ (like the standard readline() function).
+ """
+ # This provides a single line of "unget" if _reuse_line is set to True
+ if not self._reuse_line:
+ self._line = self._file.readline()
+ self._linenr += 1
- factor = self._parse_factor(feed, cur_item, line, filename, linenr,
- transform_m)
+ self._reuse_line = False
- # Return 'factor' directly if we have a "single-operand" AND.
- # Otherwise, parse the right operand and make an _AND node. This turns
- # A && B && C && D into (_AND, A, (_AND, B, (_AND, C, D))).
- return factor \
- if not feed.check(_T_AND) else \
- (_AND, factor, self._parse_and_expr(feed, cur_item, line,
- filename, linenr,
- transform_m))
+ # Handle line joining
+ while self._line.endswith("\\\n"):
+ self._line = self._line[:-2] + self._file.readline()
+ self._linenr += 1
- def _parse_factor(self, feed, cur_item, line, filename, linenr,
- transform_m):
- token = feed.get_next()
+ return self._line
- if isinstance(token, (Symbol, str)):
- # Plain symbol or relation
+ def _next_line_no_join(self):
+ """
+ Used for help texts, which don't do line joining.
+ """
+ self._line = self._file.readline()
+ self._linenr += 1
+ return self._line
- next_token = feed.peek_next()
- if next_token not in _TOKEN_TO_RELATION:
- # Plain symbol
- # For conditional expressions ('depends on <expr>',
- # '... if <expr>', etc.), "m" and m are rewritten to
- # "m" && MODULES.
- if transform_m and (token is self._m or token == "m"):
- return (_AND, "m", self._lookup_sym("MODULES"))
+ #
+ # Tokenization
+ #
- return token
+ def _lookup_sym(self, name):
+ """
+ Fetches the symbol 'name' from the symbol table, creating and
+ registering it if it does not exist. If '_parsing_configs' is False, it
+ means we're in eval_string(), and new symbols won't be registered.
+ """
+ if name in self.syms:
+ return self.syms[name]
- # Relation
- return (_TOKEN_TO_RELATION[feed.get_next()],
- token,
- feed.get_next())
+ sym = Symbol()
+ sym.kconfig = self
+ sym.name = name
+ sym.is_constant = False
+ sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
- if token == _T_NOT:
- return (_NOT, self._parse_factor(feed, cur_item, line, filename,
- linenr, transform_m))
+ if self._parsing_kconfigs:
+ self.syms[name] = sym
+ else:
+ self._warn("no symbol {} in configuration".format(name))
- if token == _T_OPEN_PAREN:
- expr_parse = self._parse_expr(feed, cur_item, line, filename,
- linenr, transform_m)
- if not feed.check(_T_CLOSE_PAREN):
- _parse_error(line, "missing end parenthesis", filename, linenr)
- return expr_parse
+ return sym
- _parse_error(line, "malformed expression", filename, linenr)
+ def _lookup_const_sym(self, name):
+ """
+ Like _lookup_sym(), for constant (quoted) symbols
+ """
+ if name in self.const_syms:
+ return self.const_syms[name]
- def _tokenize(self, s, for_eval, filename=None, linenr=None):
- """Returns a _Feed instance containing tokens derived from the string
- 's'. Registers any new symbols encountered (via _lookup_sym()).
+ sym = Symbol()
+ sym.kconfig = self
+ sym.name = name
+ sym.is_constant = True
+ sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
- Tries to be reasonably speedy by processing chunks of text via regexes
- and string operations where possible. This is a hotspot during parsing.
+ if self._parsing_kconfigs:
+ self.const_syms[name] = sym
- for_eval: True when parsing an expression for a call to Config.eval(),
- in which case we should not treat the first token specially nor
- register new symbols."""
+ return sym
+
+ def _tokenize(self):
+ """
+ Parses Kconfig._line, putting the tokens in Kconfig._tokens. Registers
+ any new symbols encountered with _lookup(_const)_sym().
+
+ Tries to be reasonably speedy by processing chunks of text via regexes
+ and string operations where possible. This is the biggest hotspot
+ during parsing.
+ """
+ s = self._line
# Tricky implementation detail: While parsing a token, 'token' refers
- # to the previous token. See _NOT_REF for why this is needed.
+ # to the previous token. See _STRING_LEX for why this is needed.
- if for_eval:
- token = None
- tokens = []
+ # See comment at _initial_token_re_match definition
+ initial_token_match = _initial_token_re_match(s)
+ if not initial_token_match:
+ self._tokens = (None,)
+ self._tokens_i = -1
+ return
- # The current index in the string being tokenized
- i = 0
+ keyword = _get_keyword(initial_token_match.group(1))
- else:
- # See comment at _initial_token_re_match definition
- initial_token_match = _initial_token_re_match(s)
- if not initial_token_match:
- return _Feed(())
-
- keyword = _get_keyword(initial_token_match.group(1))
- if keyword == _T_HELP:
- # Avoid junk after "help", e.g. "---", being registered as a
- # symbol
- return _Feed((_T_HELP,))
- if keyword is None:
- # We expect a keyword as the first token
- _tokenization_error(s, filename, linenr)
-
- token = keyword
- tokens = [keyword]
- # The current index in the string being tokenized
- i = initial_token_match.end()
+ if keyword == _T_HELP:
+ # Avoid junk after "help", e.g. "---", being registered as a
+ # symbol
+ self._tokens = (_T_HELP, None)
+ self._tokens_i = -1
+ return
+
+ if keyword is None:
+ self._parse_error("expected keyword as first token")
+
+ token = keyword
+ self._tokens = [keyword]
+ # The current index in the string being tokenized
+ i = initial_token_match.end()
# Main tokenization loop (for tokens past the first one)
while i < len(s):
@@ -1337,15 +1131,15 @@ class Config(object):
# It's a keyword
token = keyword
- elif token not in _NOT_REF:
- # It's a symbol reference
- token = self._lookup_sym(name, for_eval)
- token._ref_locations.append((filename, linenr))
-
- elif token == _T_CONFIG:
- # It's a symbol definition
- token = self._lookup_sym(name, for_eval)
- token._def_locations.append((filename, linenr))
+ elif token not in _STRING_LEX:
+ # It's a non-const symbol...
+ if name in ("n", "m", "y"):
+ # ...except we translate n, m, and y into the
+ # corresponding constant symbols, like the C
+ # implementation
+ token = self.const_syms[name]
+ else:
+ token = self._lookup_sym(name)
else:
# It's a case of missing quotes. For example, the
@@ -1360,7 +1154,7 @@ class Config(object):
token = name
else:
- # Not an identifier/keyword
+ # Not keyword/non-const symbol
# Note: _id_keyword_match and _initial_token_match strip
# trailing whitespace, making it safe to assume s[i] is the
@@ -1378,44 +1172,63 @@ class Config(object):
if c in "\"'":
# String literal/constant symbol
if "\\" not in s:
- # Fast path: If the string contains no backslashes, we
+ # Fast path: If the line contains no backslashes, we
# can just find the matching quote.
+
end = s.find(c, i)
if end == -1:
- _tokenization_error(s, filename, linenr)
- token = s[i:end]
+ self._parse_error("unterminated string")
+
+ val = s[i:end]
i = end + 1
else:
- # Slow path: This could probably be sped up, but it's a
- # very unusual case anyway.
+ # Slow path for lines with backslashes (very rare,
+ # performance irrelevant)
+
quote = c
val = ""
+
while 1:
if i >= len(s):
- _tokenization_error(s, filename, linenr)
+ self._parse_error("unterminated string")
+
c = s[i]
if c == quote:
break
+
if c == "\\":
if i + 1 >= len(s):
- _tokenization_error(s, filename, linenr)
+ self._parse_error("unterminated string")
+
val += s[i + 1]
i += 2
else:
val += c
i += 1
+
i += 1
- token = val
+
+ # This is the only place where we don't survive with a
+ # single token of lookback: 'option env="FOO"' does not
+ # refer to a constant symbol named "FOO".
+ token = val \
+ if token in _STRING_LEX or \
+ self._tokens[0] == _T_OPTION else \
+ self._lookup_const_sym(val)
elif c == "&":
- # Invalid characters are ignored
- if i >= len(s) or s[i] != "&": continue
+ # Invalid characters are ignored (backwards-compatible)
+ if i >= len(s) or s[i] != "&":
+ continue
+
token = _T_AND
i += 1
elif c == "|":
- # Invalid characters are ignored
- if i >= len(s) or s[i] != "|": continue
+ # Invalid characters are ignored (backwards-compatible)
+ if i >= len(s) or s[i] != "|":
+ continue
+
token = _T_OR
i += 1
@@ -1435,7 +1248,8 @@ class Config(object):
elif c == ")":
token = _T_CLOSE_PAREN
- elif c == "#": break # Comment
+ elif c == "#":
+ break
# Very rare
elif c == "<":
@@ -1454,2085 +1268,2423 @@ class Config(object):
token = _T_GREATER
else:
- # Invalid characters are ignored
+ # Invalid characters are ignored (backwards-compatible)
continue
# Skip trailing whitespace
while i < len(s) and s[i].isspace():
i += 1
- tokens.append(token)
+ self._tokens.append(token)
- return _Feed(tokens)
+ # None-terminating token streams makes the token fetching functions
+ # simpler/faster
+ self._tokens.append(None)
+ self._tokens_i = -1
- def _lookup_sym(self, name, for_eval=False):
- """Fetches the symbol 'name' from the symbol table, creating and
- registering it if it does not exist. If 'for_eval' is True, the symbol
- won't be added to the symbol table if it does not exist -- this is for
- Config.eval()."""
- if name in self._syms:
- return self._syms[name]
+ def _next_token(self):
+ self._tokens_i += 1
+ return self._tokens[self._tokens_i]
- new_sym = Symbol()
- new_sym._config = self
- new_sym._name = name
- if for_eval:
- self._warn("no symbol {} in configuration".format(name))
- else:
- self._syms[name] = new_sym
- return new_sym
+ def _peek_token(self):
+ return self._tokens[self._tokens_i + 1]
- #
- # Expression evaluation
- #
+ def _check_token(self, token):
+ """
+ If the next token is 'token', removes it and returns True.
+ """
+ if self._tokens[self._tokens_i + 1] == token:
+ self._tokens_i += 1
+ return True
+ return False
- def _eval_expr(self, expr):
- """Evaluates an expression to "n", "m", or "y"."""
-
- # Handles e.g. an "x if y" condition where the "if y" part is missing.
- if expr is None:
- return "y"
-
- res = self._eval_expr_rec(expr)
- if res == "m":
- # Promote "m" to "y" if we're running without modules.
- #
- # Internally, "m" is often rewritten to "m" && MODULES by both the
- # C implementation and Kconfiglib, which takes care of cases where
- # "m" should be demoted to "n" instead.
- modules_sym = self._syms.get("MODULES")
- if modules_sym is None or modules_sym.get_value() != "y":
- return "y"
- return res
-
- def _eval_expr_rec(self, expr):
- if isinstance(expr, Symbol):
- # Non-bool/tristate symbols are always "n" in a tristate sense,
- # regardless of their value
- return expr.get_value() if expr._type in (BOOL, TRISTATE) else "n"
-
- if isinstance(expr, str):
- return expr if expr in ("m", "y") else "n"
-
- if expr[0] == _AND:
- ev1 = self._eval_expr_rec(expr[1])
- if ev1 == "n":
- return "n"
- ev2 = self._eval_expr_rec(expr[2])
- return ev2 if ev1 == "y" else \
- "m" if ev2 != "n" else \
- "n"
-
- if expr[0] == _OR:
- ev1 = self._eval_expr_rec(expr[1])
- if ev1 == "y":
- return "y"
- ev2 = self._eval_expr_rec(expr[2])
- return ev2 if ev1 == "n" else \
- "y" if ev2 == "y" else \
- "m"
-
- if expr[0] == _NOT:
- ev = self._eval_expr_rec(expr[1])
- return "n" if ev == "y" else \
- "y" if ev == "n" else \
- "m"
-
- if expr[0] in _RELATIONS:
- # Implements <, <=, >, >= comparisons as well. These were added to
- # kconfig in 31847b67 (kconfig: allow use of relations other than
- # (in)equality).
-
- # This mirrors the C implementation pretty closely. Perhaps there's
- # a more pythonic way to structure this.
-
- oper, op1, op2 = expr
- op1_type, op1_str = _type_and_val(op1)
- op2_type, op2_str = _type_and_val(op2)
-
- # If both operands are strings...
- if op1_type == STRING and op2_type == STRING:
- # ...then compare them lexicographically
- comp = _strcmp(op1_str, op2_str)
- else:
- # Otherwise, try to compare them as numbers
- try:
- comp = int(op1_str, _TYPE_TO_BASE[op1_type]) - \
- int(op2_str, _TYPE_TO_BASE[op2_type])
- except ValueError:
- # They're not both valid numbers. If the comparison is
- # anything but = or !=, return 'n'. Otherwise, reuse
- # _strcmp() to check for (in)equality.
- if oper not in (_EQUAL, _UNEQUAL):
- return "n"
- comp = _strcmp(op1_str, op2_str)
-
- if oper == _EQUAL: res = comp == 0
- elif oper == _UNEQUAL: res = comp != 0
- elif oper == _LESS: res = comp < 0
- elif oper == _LESS_EQUAL: res = comp <= 0
- elif oper == _GREATER: res = comp > 0
- elif oper == _GREATER_EQUAL: res = comp >= 0
-
- return "y" if res else "n"
-
- _internal_error("Internal error while evaluating expression: "
- "unknown operation {}.".format(expr[0]))
-
- def _eval_min(self, e1, e2):
- """Returns the minimum value of the two expressions. Equates None with
- 'y'."""
- e1_eval = self._eval_expr(e1)
- e2_eval = self._eval_expr(e2)
- return e1_eval if tri_less(e1_eval, e2_eval) else e2_eval
-
- def _eval_max(self, e1, e2):
- """Returns the maximum value of the two expressions. Equates None with
- 'y'."""
- e1_eval = self._eval_expr(e1)
- e2_eval = self._eval_expr(e2)
- return e1_eval if tri_greater(e1_eval, e2_eval) else e2_eval
#
- # Dependency tracking (for caching and invalidation)
+ # Parsing
#
- def _build_dep(self):
- """Populates the Symbol._direct_dependents sets, linking the symbol to
- the symbols that immediately depend on it in the sense that changing
- the value of the symbol might affect the values of those other symbols.
- This is used for caching/invalidation purposes. The calculated sets
- might be larger than necessary as we don't do any complicated analysis
- of the expressions."""
-
- # Adds 'sym' as a directly dependent symbol to all symbols that appear
- # in the expression 'e'
- def add_expr_deps(expr, sym):
- res = []
- _expr_syms(expr, res)
- for expr_sym in res:
- expr_sym._direct_dependents.add(sym)
-
- # The directly dependent symbols of a symbol S are:
- #
- # - Any symbols whose prompts, default values, _rev_dep (select
- # condition), _weak_rev_dep (imply condition) or ranges depend on S
- #
- # - Any symbol that has S as a direct dependency (has S in
- # _direct_deps). This is needed to get invalidation right for
- # 'imply'.
- #
- # - Any symbols that belong to the same choice statement as S
- # (these won't be included in S._direct_dependents as that makes the
- # dependency graph unwieldy, but S._get_dependent() will include
- # them)
- #
- # - Any symbols in a choice statement that depends on S
-
- # Only calculate _direct_dependents for defined symbols. Undefined
- # symbols could theoretically be selected/implied, but it wouldn't
- # change their value (they always evaluate to their name), so it's not
- # a true dependency.
-
- for sym in self._defined_syms:
- for _, e in sym._prompts:
- add_expr_deps(e, sym)
-
- for v, e in sym._def_exprs:
- add_expr_deps(v, sym)
- add_expr_deps(e, sym)
-
- add_expr_deps(sym._rev_dep, sym)
- add_expr_deps(sym._weak_rev_dep, sym)
-
- for l, u, e in sym._ranges:
- add_expr_deps(l, sym)
- add_expr_deps(u, sym)
- add_expr_deps(e, sym)
-
- add_expr_deps(sym._direct_deps, sym)
-
- if sym._is_choice_sym:
- choice = sym._parent
- for _, e in choice._prompts:
- add_expr_deps(e, sym)
- for _, e in choice._def_exprs:
- add_expr_deps(e, sym)
-
- def _eq_to_sym(self, eq):
- """_expr_depends_on() helper. For (in)equalities of the form sym = y/m
- or sym != n, returns sym. For other (in)equalities, returns None."""
- relation, left, right = eq
-
- def transform_y_m_n(item):
- if item is self._y: return "y"
- if item is self._m: return "m"
- if item is self._n: return "n"
- return item
-
- left = transform_y_m_n(left)
- right = transform_y_m_n(right)
-
- # Make sure the symbol (if any) appears to the left
- if not isinstance(left, Symbol):
- left, right = right, left
- if not isinstance(left, Symbol):
- return None
- if (relation == _EQUAL and right in ("m", "y")) or \
- (relation == _UNEQUAL and right == "n"):
- return left
- return None
+ def _make_and(self, e1, e2):
+ """
+ Constructs an AND (&&) expression. Performs trivial simplification.
+ """
+ if e1 is self.y:
+ return e2
- def _expr_depends_on(self, expr, sym):
- """Reimplementation of expr_depends_symbol() from mconf.c. Used to
- determine if a submenu should be implicitly created, which influences
- what items inside choice statements are considered choice items."""
- if expr is None:
- return False
+ if e2 is self.y:
+ return e1
- def rec(expr):
- if isinstance(expr, str):
- return False
- if isinstance(expr, Symbol):
- return expr is sym
+ if e1 is self.n or e2 is self.n:
+ return self.n
- if expr[0] in (_EQUAL, _UNEQUAL):
- return self._eq_to_sym(expr) is sym
- if expr[0] == _AND:
- return rec(expr[1]) or rec(expr[2])
- return False
+ return (AND, e1, e2)
- return rec(expr)
+ def _make_or(self, e1, e2):
+ """
+ Constructs an OR (||) expression. Performs trivial simplification.
+ """
+ if e1 is self.n:
+ return e2
- def _invalidate_all(self):
- # Undefined symbols never change value and don't need to be
- # invalidated, so we can just iterate over defined symbols
- for sym in self._defined_syms:
- sym._invalidate()
+ if e2 is self.n:
+ return e1
- #
- # Printing and misc.
- #
+ if e1 is self.y or e2 is self.y:
+ return self.y
+
+ return (OR, e1, e2)
+
+ def _parse_block(self, end_token, parent, visible_if_deps, prev_node):
+ """
+ Parses a block, which is the contents of either a file or an if, menu,
+ or choice statement.
+
+ end_token:
+ The token that ends the block, e.g. _T_ENDIF ("endif") for ifs. None
+ for files.
+
+ parent:
+ The parent menu node, corresponding to e.g. a menu or Choice. Can
+ also be a Symbol, due to automatic submenu creation from
+ dependencies.
+
+ visible_if_deps:
+ 'visible if' dependencies from enclosing menus. Propagated to Symbol
+ and Choice prompts.
+
+ prev_node:
+ The previous menu node. New nodes will be added after this one (by
+ modifying their 'next' pointer).
+
+ prev_node is reused to parse a list of child menu nodes (for a menu
+ or Choice): After parsing the children, the 'next' pointer is
+ assigned to the 'list' pointer to "tilt up" the children above the
+ node.
- def _expand_sym_refs(self, s):
- """Expands $-references to symbols in 's' to symbol values, or to the
- empty string for undefined symbols."""
+ Returns the final menu node in the block (or prev_node if the block is
+ empty). This allows chaining.
+ """
while 1:
- sym_ref_match = _sym_ref_re_search(s)
- if sym_ref_match is None:
- return s
+ # We might already have tokens from parsing a line to check if it's
+ # a property and discovering it isn't. This is a kind of "unget".
+ if not self._has_tokens:
+ # Advance to the next line
+ if not self._next_line():
+ if end_token is not None:
+ raise KconfigSyntaxError("Unexpected end of file " +
+ self._filename)
- sym = self._syms.get(sym_ref_match.group(1))
+ # We have reached the end of the file. Terminate the final
+ # node and return it.
+ prev_node.next = None
+ return prev_node
- s = s[:sym_ref_match.start()] + \
- (sym.get_value() if sym is not None else "") + \
- s[sym_ref_match.end():]
+ self._tokenize()
- def _expr_val_str(self, expr, no_value_str="(none)",
- get_val_instead_of_eval=False):
- """Printing helper. Returns a string with 'expr' and its value.
+ self._has_tokens = False
- no_value_str: String to return when 'expr' is missing (None).
+ t0 = self._next_token()
+ if t0 is None:
+ continue
- get_val_instead_of_eval: Assume 'expr' is a symbol or string (constant
- symbol) and get its value directly instead of evaluating it to a
- tristate value."""
+ if t0 in (_T_CONFIG, _T_MENUCONFIG):
+ # The tokenizer allocates a Symbol objects the first time a
+ # symbol is seen
+ sym = self._next_token()
- if expr is None:
- return no_value_str
+ node = MenuNode()
+ node.kconfig = self
+ node.item = sym
+ node.help = None
+ node.list = None
+ node.parent = parent
+ node.filename = self._filename
+ node.linenr = self._linenr
+ node.is_menuconfig = (t0 == _T_MENUCONFIG)
- if get_val_instead_of_eval:
- if isinstance(expr, str):
- return _expr_to_str(expr)
- val = expr.get_value()
- else:
- val = self._eval_expr(expr)
+ self._parse_properties(node, visible_if_deps)
- return "{} (value: {})".format(_expr_to_str(expr), _expr_to_str(val))
+ sym.nodes.append(node)
+ self.defined_syms.append(sym)
- def _get_sym_or_choice_str(self, sc):
- """Symbols and choices have many properties in common, so we factor out
- common __str__() stuff here. "sc" is short for "symbol or choice"."""
+ # Tricky Python semantics: This assign prev_node.next before
+ # prev_node
+ prev_node.next = prev_node = node
- # As we deal a lot with string representations here, use some
- # convenient shorthand:
- s = _expr_to_str
+ elif t0 == _T_SOURCE:
+ self._enter_file(self._expand_syms(self._next_token()))
+ prev_node = self._parse_block(None, # end_token
+ parent,
+ visible_if_deps,
+ prev_node)
+ self._leave_file()
+
+ elif t0 == end_token:
+ # We have reached the end of the block. Terminate the final
+ # node and return it.
+ prev_node.next = None
+ return prev_node
- #
- # Common symbol/choice properties
- #
+ elif t0 == _T_IF:
+ node = MenuNode()
+ node.item = None
+ node.prompt = None
+ node.parent = parent
+ node.filename = self._filename
+ node.linenr = self._linenr
+
+ # See similar code in _parse_properties()
+ if isinstance(node.parent.item, Choice):
+ parent_dep = parent.item
+ else:
+ parent_dep = parent.dep
- user_val_str = "(no user value)" if sc._user_val is None else \
- s(sc._user_val)
+ node.dep = self._make_and(parent_dep, self._parse_expr(True))
- # Build prompts string
- if not sc._prompts:
- prompts_str = " (no prompts)"
- else:
- prompts_str_rows = []
- for prompt, cond_expr in sc._orig_prompts:
- prompts_str_rows.append(
- ' "{}"'.format(prompt)
- if cond_expr is None else
- ' "{}" if {}'.format(prompt,
- self._expr_val_str(cond_expr)))
- prompts_str = "\n".join(prompts_str_rows)
-
- # Build locations string
- locations_str = "(no locations)" \
- if not sc._def_locations else \
- " ".join(["{}:{}".format(filename, linenr)
- for filename, linenr in sc._def_locations])
-
- # Build additional-dependencies-from-menus-and-ifs string
- additional_deps_str = " " + \
- self._expr_val_str(sc._deps_from_containing,
- "(no additional dependencies)")
+ self._parse_block(_T_ENDIF,
+ node, # parent
+ visible_if_deps,
+ node) # prev_node
+ node.list = node.next
- #
- # Symbol-specific stuff
- #
+ prev_node.next = prev_node = node
- if isinstance(sc, Symbol):
- # Build ranges string
- if isinstance(sc, Symbol):
- if not sc._orig_ranges:
- ranges_str = " (no ranges)"
+ elif t0 == _T_MENU:
+ node = MenuNode()
+ node.kconfig = self
+ node.item = MENU
+ node.visibility = self.y
+ node.parent = parent
+ node.filename = self._filename
+ node.linenr = self._linenr
+
+ prompt = self._next_token()
+ self._parse_properties(node, visible_if_deps)
+ node.prompt = (prompt, node.dep)
+
+ self._parse_block(_T_ENDMENU,
+ node, # parent
+ self._make_and(visible_if_deps,
+ node.visibility),
+ node) # prev_node
+ node.list = node.next
+
+ prev_node.next = prev_node = node
+
+ elif t0 == _T_COMMENT:
+ node = MenuNode()
+ node.kconfig = self
+ node.item = COMMENT
+ node.list = None
+ node.parent = parent
+ node.filename = self._filename
+ node.linenr = self._linenr
+
+ prompt = self._next_token()
+ self._parse_properties(node, visible_if_deps)
+ node.prompt = (prompt, node.dep)
+
+ prev_node.next = prev_node = node
+
+ elif t0 == _T_CHOICE:
+ name = self._next_token()
+ if name is None:
+ choice = Choice()
+ self._choices.append(choice)
else:
- ranges_str_rows = []
- for l, u, cond_expr in sc._orig_ranges:
- ranges_str_rows.append(
- " [{}, {}]".format(s(l), s(u))
- if cond_expr is None else
- " [{}, {}] if {}"
- .format(s(l), s(u), self._expr_val_str(cond_expr)))
- ranges_str = "\n".join(ranges_str_rows)
-
- # Build default values string
- if not sc._orig_def_exprs:
- defaults_str = " (no default values)"
+ # Named choice
+ choice = self.named_choices.get(name)
+ if not choice:
+ choice = Choice()
+ self._choices.append(choice)
+ choice.name = name
+ self.named_choices[name] = choice
+
+ choice.kconfig = self
+
+ node = MenuNode()
+ node.kconfig = self
+ node.item = choice
+ node.help = None
+ node.parent = parent
+ node.filename = self._filename
+ node.linenr = self._linenr
+
+ self._parse_properties(node, visible_if_deps)
+ self._parse_block(_T_ENDCHOICE,
+ node, # parent
+ visible_if_deps,
+ node) # prev_node
+ node.list = node.next
+
+ choice.nodes.append(node)
+
+ prev_node.next = prev_node = node
+
+ elif t0 == _T_MAINMENU:
+ self.top_node.prompt = (self._next_token(), self.y)
+ self.top_node.filename = self._filename
+ self.top_node.linenr = self._linenr
+
else:
- defaults_str_rows = []
- for val_expr, cond_expr in sc._orig_def_exprs:
- row_str = " " + self._expr_val_str(val_expr, "(none)",
- sc._type == STRING)
- defaults_str_rows.append(row_str)
- defaults_str_rows.append(" Condition: " +
- self._expr_val_str(cond_expr))
- defaults_str = "\n".join(defaults_str_rows)
-
- # Build selects string
- if not sc._orig_selects:
- selects_str = " (no selects)"
+ self._parse_error("unrecognized construct")
+
+ def _parse_cond(self):
+ """
+ Parses an optional 'if <expr>' construct and returns the parsed <expr>,
+ or self.y if the next token is not _T_IF
+ """
+ return self._parse_expr(True) if self._check_token(_T_IF) else self.y
+
+ def _parse_properties(self, node, visible_if_deps):
+ """
+ Parses properties for symbols, menus, choices, and comments. Also takes
+ care of propagating dependencies from the menu node to the properties
+ of the item (this mirrors the C tools, though they do it after
+ parsing).
+
+ node:
+ The menu node we're parsing properties on. Prompt, help text,
+ 'depends on', and 'visible if' properties apply to the Menu node,
+ while the others apply to the contained item.
+
+ visible_if_deps:
+ 'visible if' dependencies from enclosing menus. Propagated to Symbol
+ and Choice prompts.
+ """
+ # New properties encountered at this location. A local 'depends on'
+ # only applies to these, in case a symbol is defined in multiple
+ # locations.
+ prompt = None
+ defaults = []
+ selects = []
+ implies = []
+ ranges = []
+
+ # Menu node dependencies from 'depends on'. Will get propagated to the
+ # properties above.
+ node.dep = self.y
+
+ while 1:
+ # Advance to the next line
+ if not self._next_line():
+ break
+
+ self._tokenize()
+
+ t0 = self._next_token()
+ if t0 is None:
+ continue
+
+ if t0 == _T_DEPENDS:
+ if not self._check_token(_T_ON):
+ self._parse_error('expected "on" after "depends"')
+
+ node.dep = self._make_and(node.dep, self._parse_expr(True))
+
+ elif t0 == _T_HELP:
+ # Find first non-blank (not all-space) line and get its
+ # indentation
+
+ while 1:
+ line = self._next_line_no_join()
+ if not line or not line.isspace():
+ break
+
+ if not line:
+ node.help = ""
+ break
+
+ indent = _indentation(line)
+ if indent == 0:
+ # If the first non-empty lines has zero indent, there is no
+ # help text
+ node.help = ""
+ self._reuse_line = True # "Unget" the line
+ break
+
+ # The help text goes on till the first non-empty line with less
+ # indent
+
+ help_lines = [_deindent(line, indent).rstrip()]
+ while 1:
+ line = self._next_line_no_join()
+
+ if not line or \
+ (not line.isspace() and _indentation(line) < indent):
+ node.help = "\n".join(help_lines).rstrip() + "\n"
+ break
+
+ help_lines.append(_deindent(line, indent).rstrip())
+
+ if not line:
+ break
+
+ self._reuse_line = True # "Unget" the line
+
+ elif t0 == _T_SELECT:
+ if not isinstance(node.item, Symbol):
+ self._parse_error("only symbols can select")
+
+ selects.append((self._next_token(), self._parse_cond()))
+
+ elif t0 == _T_IMPLY:
+ if not isinstance(node.item, Symbol):
+ self._parse_error("only symbols can imply")
+
+ implies.append((self._next_token(), self._parse_cond()))
+
+ elif t0 in (_T_BOOL, _T_TRISTATE, _T_INT, _T_HEX, _T_STRING):
+ node.item.orig_type = _TOKEN_TO_TYPE[t0]
+
+ if self._peek_token() is not None:
+ prompt = (self._next_token(), self._parse_cond())
+
+ elif t0 == _T_DEFAULT:
+ defaults.append((self._parse_expr(False), self._parse_cond()))
+
+ elif t0 in (_T_DEF_BOOL, _T_DEF_TRISTATE):
+ node.item.orig_type = _TOKEN_TO_TYPE[t0]
+
+ defaults.append((self._parse_expr(False), self._parse_cond()))
+
+ elif t0 == _T_PROMPT:
+ # 'prompt' properties override each other within a single
+ # definition of a symbol, but additional prompts can be added
+ # by defining the symbol multiple times
+ prompt = (self._next_token(), self._parse_cond())
+
+ elif t0 == _T_RANGE:
+ ranges.append((self._next_token(),
+ self._next_token(),
+ self._parse_cond()))
+
+ elif t0 == _T_OPTION:
+ if self._check_token(_T_ENV) and self._check_token(_T_EQUAL):
+ env_var = self._next_token()
+
+ node.item.env_var = env_var
+
+ if env_var not in os.environ:
+ self._warn("'option env=\"{0}\"' on symbol {1} has "
+ "no effect, because the environment "
+ "variable {0} is not set"
+ .format(env_var, node.item.name),
+ self._filename, self._linenr)
+ else:
+ defaults.append(
+ (self._lookup_const_sym(os.environ[env_var]),
+ self.y))
+
+ elif self._check_token(_T_DEFCONFIG_LIST):
+ if not self.defconfig_list:
+ self.defconfig_list = node.item
+ else:
+ self._warn("'option defconfig_list' set on multiple "
+ "symbols ({0} and {1}). Only {0} will be "
+ "used.".format(self.defconfig_list.name,
+ node.item.name),
+ self._filename, self._linenr)
+
+ elif self._check_token(_T_MODULES):
+ # To reduce warning spam, only warn if 'option modules' is
+ # set on some symbol that isn't MODULES, which should be
+ # safe. I haven't run into any projects that make use
+ # modules besides the kernel yet, and there it's likely to
+ # keep being called "MODULES".
+ if node.item is not self.modules:
+ self._warn("the 'modules' option is not supported. "
+ "Let me know if this is a problem for you, "
+ "as it wouldn't be that hard to implement. "
+ "Note that modules are supported -- "
+ "Kconfiglib just assumes the symbol name "
+ "MODULES, like older versions of the C "
+ "implementation did when 'option modules' "
+ "wasn't used.",
+ self._filename, self._linenr)
+
+ elif self._check_token(_T_ALLNOCONFIG_Y):
+ if not isinstance(node.item, Symbol):
+ self._parse_error("the 'allnoconfig_y' option is only "
+ "valid for symbols")
+
+ node.item.is_allnoconfig_y = True
+
+ else:
+ self._parse_error("unrecognized option")
+
+ elif t0 == _T_VISIBLE:
+ if not self._check_token(_T_IF):
+ self._parse_error('expected "if" after "visible"')
+
+ node.visibility = \
+ self._make_and(node.visibility, self._parse_expr(True))
+
+ elif t0 == _T_OPTIONAL:
+ if not isinstance(node.item, Choice):
+ self._parse_error('"optional" is only valid for choices')
+
+ node.item.is_optional = True
+
else:
- selects_str_rows = []
- for target, cond_expr in sc._orig_selects:
- selects_str_rows.append(
- " " + target._name
- if cond_expr is None else
- " {} if {}".format(target._name,
- self._expr_val_str(cond_expr)))
- selects_str = "\n".join(selects_str_rows)
-
- # Build implies string
- if not sc._orig_implies:
- implies_str = " (no implies)"
+ self._tokens_i = -1
+ # Reuse the tokens for the non-property line later
+ self._has_tokens = True
+ break
+
+ # Done parsing properties. Now add the new
+ # prompts/defaults/selects/implies/ranges properties, with dependencies
+ # from node.dep propagated.
+
+ # First propagate parent dependencies to node.dep
+
+ # If the parent node holds a Choice, we use the Choice itself as the
+ # parent dependency. This matches the C implementation, and makes sense
+ # as the value (mode) of the choice limits the visibility of the
+ # contained choice symbols. Due to the similar interface, Choice works
+ # as a drop-in replacement for Symbol here.
+ if isinstance(node.parent.item, Choice):
+ node.dep = self._make_and(node.dep, node.parent.item)
+ else:
+ node.dep = self._make_and(node.dep, node.parent.dep)
+
+ if isinstance(node.item, (Symbol, Choice)):
+ if isinstance(node.item, Symbol):
+ # See the class documentation
+ node.item.direct_dep = \
+ self._make_or(node.item.direct_dep, node.dep)
+
+ # Set the prompt, with dependencies propagated
+ if prompt:
+ node.prompt = (prompt[0],
+ self._make_and(self._make_and(prompt[1],
+ node.dep),
+ visible_if_deps))
else:
- implies_str_rows = []
- for target, cond_expr in sc._orig_implies:
- implies_str_rows.append(
- " " + target._name
- if cond_expr is None else
- " {} if {}".format(target._name,
- self._expr_val_str(cond_expr)))
- implies_str = "\n".join(implies_str_rows)
-
- res = _lines("Symbol " +
- ("(no name)" if sc._name is None else sc._name),
- "Type : " + _TYPENAME[sc._type],
- "Value : " + s(sc.get_value()),
- "User value : " + user_val_str,
- "Visibility : " + s(_get_visibility(sc)),
- "Is choice item : " + str(sc._is_choice_sym),
- "Is defined : " + str(sc._is_defined),
- "Is from env. : " + str(sc._is_from_env),
- "Is special : " + str(sc._is_special),
- "")
- if sc._ranges:
- res += _lines("Ranges:", ranges_str + "\n")
- res += _lines("Prompts:",
- prompts_str,
- "Default values:",
- defaults_str,
- "Selects:",
- selects_str,
- "Implies:",
- implies_str,
- "Reverse (select-related) dependencies:",
- " (no reverse dependencies)"
- if sc._rev_dep == "n"
- else " " + self._expr_val_str(sc._rev_dep),
- "Weak reverse (imply-related) dependencies:",
- " (no weak reverse dependencies)"
- if sc._weak_rev_dep == "n"
- else " " + self._expr_val_str(sc._weak_rev_dep),
- "Additional dependencies from enclosing menus "
- "and ifs:",
- additional_deps_str,
- "Locations: " + locations_str)
-
- return res
+ node.prompt = None
+ # Add the new defaults, with dependencies propagated
+ for val_expr, cond in defaults:
+ node.item.defaults.append(
+ (val_expr, self._make_and(cond, node.dep)))
+
+ # Add the new ranges, with dependencies propagated
+ for low, high, cond in ranges:
+ node.item.ranges.append(
+ (low, high, self._make_and(cond, node.dep)))
+
+ # Handle selects
+ for target, cond in selects:
+ # Only stored for inspection. Not used during evaluation.
+ node.item.selects.append(
+ (target, self._make_and(cond, node.dep)))
+
+ # Modify the dependencies of the selected symbol
+ target.rev_dep = \
+ self._make_or(target.rev_dep,
+ self._make_and(node.item,
+ self._make_and(cond,
+ node.dep)))
+
+ # Handle implies
+ for target, cond in implies:
+ # Only stored for inspection. Not used during evaluation.
+ node.item.implies.append(
+ (target, self._make_and(cond, node.dep)))
+
+ # Modify the dependencies of the implied symbol
+ target.weak_rev_dep = \
+ self._make_or(target.weak_rev_dep,
+ self._make_and(node.item,
+ self._make_and(cond,
+ node.dep)))
+
+ def _parse_expr(self, transform_m):
+ """
+ Parses an expression from the tokens in Kconfig._tokens using a simple
+ top-down approach. See the module docs for the expression format.
+
+ transform_m:
+ True if m should be rewritten to m && MODULES. See the
+ Kconfig.eval_string() documentation.
+ """
+ # Grammar:
#
- # Choice-specific stuff
+ # expr: and_expr ['||' expr]
+ # and_expr: factor ['&&' and_expr]
+ # factor: <symbol> ['='/'!='/'<'/... <symbol>]
+ # '!' factor
+ # '(' expr ')'
#
+ # It helps to think of the 'expr: and_expr' case as a single-operand OR
+ # (no ||), and of the 'and_expr: factor' case as a single-operand AND
+ # (no &&). Parsing code is always a bit tricky.
+
+ # Mind dump: parse_factor() and two nested loops for OR and AND would
+ # work as well. The straightforward implementation there gives a
+ # (op, (op, (op, A, B), C), D) parse for A op B op C op D. Representing
+ # expressions as (op, [list of operands]) instead goes nicely with that
+ # version, but is wasteful for short expressions and complicates
+ # expression evaluation and other code that works on expressions (more
+ # complicated code likely offsets any performance gain from less
+ # recursion too). If we also try to optimize the list representation by
+ # merging lists when possible (e.g. when ANDing two AND expressions),
+ # we end up allocating a ton of lists instead of reusing expressions,
+ # which is bad.
- # Build selected symbol string
- sel = sc.get_selection()
- sel_str = "(no selection)" if sel is None else sel._name
+ and_expr = self._parse_and_expr(transform_m)
- # Build default values string
- if not sc._def_exprs:
- defaults_str = " (no default values)"
- else:
- defaults_str_rows = []
- for sym, cond_expr in sc._orig_def_exprs:
- defaults_str_rows.append(
- " " + sym._name
- if cond_expr is None else
- " {} if {}".format(sym._name,
- self._expr_val_str(cond_expr)))
- defaults_str = "\n".join(defaults_str_rows)
-
- # Build contained symbols string
- names = [sym._name for sym in sc._actual_symbols]
- syms_string = " ".join(names) if names else "(empty)"
-
- return _lines("Choice",
- "Name (for named choices): " +
- ("(no name)" if sc._name is None else sc._name),
- "Type : " + _TYPENAME[sc._type],
- "Selected symbol : " + sel_str,
- "User value : " + user_val_str,
- "Mode : " + s(sc.get_mode()),
- "Visibility : " + s(_get_visibility(sc)),
- "Optional : " + str(sc._optional),
- "Prompts:",
- prompts_str,
- "Defaults:",
- defaults_str,
- "Choice symbols:",
- " " + syms_string,
- "Additional dependencies from enclosing menus and ifs:",
- additional_deps_str,
- "Locations: " + locations_str)
-
- def _is_header_line(self, line):
- """Returns True is the line could be part of the initial header in a
- .config file (which is really just another comment, but can be handy
- for storing metadata)."""
- return line is not None and line.startswith("#") and \
- not self._unset_re.match(line)
+ # Return 'and_expr' directly if we have a "single-operand" OR.
+ # Otherwise, parse the expression on the right and make an OR node.
+ # This turns A || B || C || D into (OR, A, (OR, B, (OR, C, D))).
+ return and_expr \
+ if not self._check_token(_T_OR) else \
+ (OR, and_expr, self._parse_expr(transform_m))
+
+ def _parse_and_expr(self, transform_m):
+ factor = self._parse_factor(transform_m)
+
+ # Return 'factor' directly if we have a "single-operand" AND.
+ # Otherwise, parse the right operand and make an AND node. This turns
+ # A && B && C && D into (AND, A, (AND, B, (AND, C, D))).
+ return factor \
+ if not self._check_token(_T_AND) else \
+ (AND, factor, self._parse_and_expr(transform_m))
+
+ def _parse_factor(self, transform_m):
+ token = self._next_token()
+
+ if isinstance(token, Symbol):
+ # Plain symbol or relation
+
+ next_token = self._peek_token()
+ if next_token not in _TOKEN_TO_REL:
+ # Plain symbol
+
+ # For conditional expressions ('depends on <expr>',
+ # '... if <expr>', etc.), m is rewritten to m && MODULES.
+ if transform_m and token is self.m:
+ return (AND, self.m, self.modules)
+
+ return token
+
+ # Relation
+ return (_TOKEN_TO_REL[self._next_token()], token,
+ self._next_token())
+
+ if token == _T_NOT:
+ return (NOT, self._parse_factor(transform_m))
+
+ if token == _T_OPEN_PAREN:
+ expr_parse = self._parse_expr(transform_m)
+ if not self._check_token(_T_CLOSE_PAREN):
+ self._parse_error("missing end parenthesis")
+
+ return expr_parse
+
+ self._parse_error("malformed expression")
#
- # Warnings
+ # Caching and invalidation
#
+ def _build_dep(self):
+ """
+ Populates the Symbol/Choice._dependents sets, which contain all other
+ items (symbols and choices) that immediately depend on the item in the
+ sense that changing the value of the item might affect the value of the
+ dependent items. This is used for caching/invalidation.
+
+ The calculated sets might be larger than necessary as we don't do any
+ complex analysis of the expressions.
+ """
+ # Only calculate _dependents for defined symbols. Constant and
+ # undefined symbols could theoretically be selected/implied, but it
+ # wouldn't change their value, so it's not a true dependency.
+ for sym in self.defined_syms:
+ # Symbols depend on the following:
+
+ # The prompt conditions
+ for node in sym.nodes:
+ if node.prompt:
+ _make_depend_on(sym, node.prompt[1])
+
+ # The default values and their conditions
+ for value, cond in sym.defaults:
+ _make_depend_on(sym, value)
+ _make_depend_on(sym, cond)
+
+ # The reverse and weak reverse dependencies
+ _make_depend_on(sym, sym.rev_dep)
+ _make_depend_on(sym, sym.weak_rev_dep)
+
+ # The ranges along with their conditions
+ for low, high, cond in sym.ranges:
+ _make_depend_on(sym, low)
+ _make_depend_on(sym, high)
+ _make_depend_on(sym, cond)
+
+ # The direct dependencies. This is usually redundant, as the direct
+ # dependencies get propagated to properties, but it's needed to get
+ # invalidation solid for 'imply', which only checks the direct
+ # dependencies (even if there are no properties to propagate it
+ # to).
+ _make_depend_on(sym, sym.direct_dep)
+
+ # In addition to the above, choice symbols depend on the choice
+ # they're in, but that's handled automatically since the Choice is
+ # propagated to the conditions of the properties before
+ # _build_dep() runs.
+
+ for choice in self._choices:
+ # Choices depend on the following:
+
+ # The prompt conditions
+ for node in choice.nodes:
+ if node.prompt:
+ _make_depend_on(choice, node.prompt[1])
+
+ # The default symbol conditions
+ for _, cond in choice.defaults:
+ _make_depend_on(choice, cond)
+
+ # The choice symbols themselves, because the y mode selection might
+ # change if a choice symbol's visibility changes
+ for sym in choice.syms:
+ # the default selection depends on the symbols
+ sym._dependents.add(choice)
+
+ def _invalidate_all(self):
+ # Undefined symbols never change value and don't need to be
+ # invalidated, so we can just iterate over defined symbols.
+ # Invalidating constant symbols would break things horribly.
+ for sym in self.defined_syms:
+ sym._invalidate()
+
+ for choice in self._choices:
+ choice._invalidate()
+
+
+ #
+ # .config writing
+ #
+
+ def _get_config_strings(self):
+ """
+ Returns a list containing all .config strings for the configuration.
+ """
+ # Symbol._already_written is set to True when a symbol config string is
+ # fetched, so that symbols defined in multiple locations only get one
+ # .config entry. We reset it prior to writing out a new .config. It
+ # only needs to be reset for defined symbols, because undefined symbols
+ # will never be written out (because they do not appear structure
+ # rooted at Kconfig.top_node).
+ #
+ # The C tools reuse _write_to_conf for this, but we cache
+ # _write_to_conf together with the value and don't invalidate cached
+ # values when writing .config files, so that won't work.
+ for sym in self.defined_syms:
+ sym._already_written = False
+
+ node = self.top_node.list
+ if not node:
+ # Empty configuration
+ return []
+
+ config_strings = []
+ # Small optimization
+ append = config_strings.append
+
+ while 1:
+ if isinstance(node.item, Symbol):
+ sym = node.item
+ if not sym._already_written:
+ config_string = sym.config_string
+ if config_string:
+ append(config_string)
+ sym._already_written = True
+
+ elif expr_value(node.dep) and \
+ ((node.item == MENU and expr_value(node.visibility)) or
+ node.item == COMMENT):
+
+ append("\n#\n# {}\n#\n".format(node.prompt[0]))
+
+ # Iterative tree walk using parent pointers
+
+ if node.list:
+ node = node.list
+ elif node.next:
+ node = node.next
+ else:
+ while node.parent:
+ node = node.parent
+ if node.next:
+ node = node.next
+ break
+ else:
+ return config_strings
+
+
+ #
+ # Misc.
+ #
+
+ def _expand_syms(self, s):
+ """
+ Expands $-references to symbols in 's' to symbol values, or to the
+ empty string for undefined symbols.
+ """
+ while 1:
+ sym_ref_match = _sym_ref_re_search(s)
+ if not sym_ref_match:
+ return s
+
+ sym = self.syms.get(sym_ref_match.group(1))
+
+ s = s[:sym_ref_match.start()] + \
+ (sym.str_value if sym else "") + \
+ s[sym_ref_match.end():]
+
+ def _parse_error(self, msg):
+ if self._filename is None:
+ loc = ""
+ else:
+ loc = "{}:{}: ".format(self._filename, self._linenr)
+
+ raise KconfigSyntaxError(
+ "{}Couldn't parse '{}': {}".format(loc, self._line.rstrip(), msg))
+
def _warn(self, msg, filename=None, linenr=None):
- """For printing general warnings."""
+ """
+ For printing general warnings.
+ """
if self._print_warnings:
_stderr_msg("warning: " + msg, filename, linenr)
def _warn_undef_assign(self, msg, filename=None, linenr=None):
- """For printing warnings for assignments to undefined variables. We
- treat this is a separate category of warnings to avoid spamming lots of
- warnings."""
+ """
+ See the class documentation.
+ """
if self._print_undef_assign:
_stderr_msg("warning: " + msg, filename, linenr)
def _warn_undef_assign_load(self, name, val, filename, linenr):
- """Special version for load_config()."""
+ """
+ Special version for load_config().
+ """
self._warn_undef_assign(
'attempt to assign the value "{}" to the undefined symbol {}' \
.format(val, name), filename, linenr)
-class Item(object):
-
- """Base class for symbols and other Kconfig constructs. Subclasses are
- Symbol, Choice, Menu, and Comment."""
-
- def is_symbol(self):
- """Returns True if the item is a symbol. Short for
- isinstance(item, kconfiglib.Symbol)."""
- return isinstance(self, Symbol)
-
- def is_choice(self):
- """Returns True if the item is a choice. Short for
- isinstance(item, kconfiglib.Choice)."""
- return isinstance(self, Choice)
-
- def is_menu(self):
- """Returns True if the item is a menu. Short for
- isinstance(item, kconfiglib.Menu)."""
- return isinstance(self, Menu)
-
- def is_comment(self):
- """Returns True if the item is a comment. Short for
- isinstance(item, kconfiglib.Comment)."""
- return isinstance(self, Comment)
-class Symbol(Item):
+class Symbol(object):
+ """
+ Represents a configuration symbol:
- """Represents a configuration symbol - e.g. FOO for
+ (menu)config FOO
+ ...
- config FOO
- ..."""
+ The following attributes are available. They should be viewed as read-only,
+ and some are implemented through @property magic (but are still efficient
+ to access due to internal caching).
- #
- # Public interface
- #
+ Note: Prompts, help texts, and locations are stored in the Symbol's
+ MenuNode(s) rather than in the Symbol itself. Check the MenuNode class and
+ the Symbol.nodes attribute. This organization matches the C tools.
- def get_config(self):
- """Returns the Config instance this symbol is from."""
- return self._config
+ name:
+ The name of the symbol, e.g. "FOO" for 'config FOO'.
- def get_name(self):
- """Returns the name of the symbol."""
- return self._name
+ type:
+ The type of the symbol. One of BOOL, TRISTATE, STRING, INT, HEX, UNKNOWN.
+ UNKNOWN is for undefined symbols, (non-special) constant symbols, and
+ symbols defined without a type.
- def get_type(self):
- """Returns the type of the symbol: one of UNKNOWN, BOOL, TRISTATE,
- STRING, HEX, or INT. These are defined at the top level of the module,
- so you'd do something like
+ When running without modules (MODULES having the value n), TRISTATE
+ symbols magically change type to BOOL. This also happens for symbols
+ within choices in "y" mode. This matches the C tools, and makes sense for
+ menuconfig-like functionality.
- if sym.get_type() == kconfiglib.STRING:
- ..."""
- return self._type
+ orig_type:
+ The type as given in the Kconfig file, without any magic applied. Used
+ when printing the symbol.
- def get_prompts(self):
- """Returns a list of prompts defined for the symbol, in the order they
- appear in the configuration files. Returns the empty list for symbols
- with no prompt.
+ str_value:
+ The value of the symbol as a string. Gives the value for string/int/hex
+ symbols. For bool/tristate symbols, gives "n", "m", or "y".
- This list will have a single entry for the vast majority of symbols
- having prompts, but having multiple prompts for a single symbol is
- possible through having multiple 'config' entries for it."""
- return [prompt for prompt, _ in self._orig_prompts]
+ This is the symbol value that's used in relational expressions
+ (A = B, A != B, etc.)
- def get_help(self):
- """Returns the help text of the symbol, or None if the symbol has no
- help text."""
- return self._help
+ Gotcha: For int/hex symbols, the exact format of the value must often be
+ preserved (e.g., when writing a .config file), hence why you can't get it
+ directly as an int. Do int(int_sym.str_value) or
+ int(hex_sym.str_value, 16) to get the integer value.
- def get_parent(self):
- """Returns the menu or choice statement that contains the symbol, or
- None if the symbol is at the top level. Note that if statements are
- treated as syntactic and do not have an explicit class
- representation."""
- return self._parent
+ tri_value:
+ The tristate value of the symbol as an integer. One of 0, 1, 2,
+ representing n, m, y. Always 0 (n) for non-bool/tristate symbols.
- def get_def_locations(self):
- """Returns a list of (filename, linenr) tuples, where filename (string)
- and linenr (int) represent a location where the symbol is defined. For
- the vast majority of symbols this list will only contain one element.
- For the following Kconfig, FOO would get two entries: the lines marked
- with *.
+ This is the symbol value that's used outside of relation expressions
+ (A, !A, A && B, A || B).
- config FOO *
- bool "foo prompt 1"
+ assignable:
+ A tuple containing the tristate user values that can currently be
+ assigned to the symbol (that would be respected), ordered from lowest (0,
+ representing n) to highest (2, representing y). This corresponds to the
+ selections available in the menuconfig interface. The set of assignable
+ values is calculated from the symbol's visibility and selects/implies.
- config FOO *
- bool "foo prompt 2"
- """
- return self._def_locations
+ Returns the empty set for non-bool/tristate symbols and for symbols with
+ visibility n. The other possible values are (0, 2), (0, 1, 2), (1, 2),
+ (1,), and (2,). A (1,) or (2,) result means the symbol is visible but
+ "locked" to m or y through a select, perhaps in combination with the
+ visibility. menuconfig represents this as -M- and -*-, respectively.
- def get_ref_locations(self):
- """Returns a list of (filename, linenr) tuples, where filename (string)
- and linenr (int) represent a location where the symbol is referenced in
- the configuration. For example, the lines marked by * would be included
- for FOO below:
+ For string/hex/int symbols, check if Symbol.visibility is non-0 (non-n)
+ instead to determine if the value can be changed.
- config A
- bool
- default BAR || FOO *
+ Some handy 'assignable' idioms:
- config B
- tristate
- depends on FOO *
- default m if FOO *
+ # Is 'sym' an assignable (visible) bool/tristate symbol?
+ if sym.assignable:
+ # What's the highest value it can be assigned? [-1] in Python
+ # gives the last element.
+ sym_high = sym.assignable[-1]
- if FOO *
- config A
- bool "A"
- endif
+ # The lowest?
+ sym_low = sym.assignable[0]
- config FOO (definition not included)
- bool
- """
- return self._ref_locations
-
- def get_value(self):
- """Calculate and return the value of the symbol. See also
- Symbol.set_user_value()."""
+ # Can the symbol be set to at least m?
+ if sym.assignable[-1] >= 1:
+ ...
- if self._cached_val is not None:
- return self._cached_val
+ # Can the symbol be set to m?
+ if 1 in sym.assignable:
+ ...
- # As a quirk of Kconfig, undefined symbols get their name as their
- # value. This is why things like "FOO = bar" work for seeing if FOO has
- # the value "bar".
- if self._type == UNKNOWN:
- self._cached_val = self._name
- return self._name
+ visibility:
+ The visibility of the symbol. One of 0, 1, 2, representing n, m, y. See
+ the module documentation for an overview of symbol values and visibility.
- # This will hold the value at the end of the function
- val = _DEFAULT_VALUE[self._type]
+ user_value:
+ The user value of the symbol. None if no user value has been assigned
+ (via Kconfig.load_config() or Symbol.set_value()).
- vis = _get_visibility(self)
+ Holds 0, 1, or 2 for bool/tristate symbols, and a string for the other
+ symbol types.
+
+ WARNING: Do not assign directly to this. It will break things. Use
+ Symbol.set_value().
+
+ config_string:
+ The .config assignment string that would get written out for the symbol
+ by Kconfig.write_config(). None if no .config assignment would get
+ written out. In general, visible symbols, symbols with (active) defaults,
+ and selected symbols get written out.
+
+ nodes:
+ A list of MenuNodes for this symbol. Will contain a single MenuNode for
+ most symbols. Undefined and constant symbols have an empty nodes list.
+ Symbols defined in multiple locations get one node for each location.
+
+ choice:
+ Holds the parent Choice for choice symbols, and None for non-choice
+ symbols. Doubles as a flag for whether a symbol is a choice symbol.
+
+ defaults:
+ List of (default, cond) tuples for the symbol's 'default' properties. For
+ example, 'default A && B if C || D' is represented as
+ ((AND, A, B), (OR, C, D)). If no condition was given, 'cond' is
+ self.kconfig.y.
+
+ Note that 'depends on' and parent dependencies are propagated to
+ 'default' conditions.
+
+ selects:
+ List of (symbol, cond) tuples for the symbol's 'select' properties. For
+ example, 'select A if B && C' is represented as (A, (AND, B, C)). If no
+ condition was given, 'cond' is self.kconfig.y.
+
+ Note that 'depends on' and parent dependencies are propagated to 'select'
+ conditions.
+
+ implies:
+ Like 'selects', for imply.
+
+ ranges:
+ List of (low, high, cond) tuples for the symbol's 'range' properties. For
+ example, 'range 1 2 if A' is represented as (1, 2, A). If there is no
+ condition, 'cond' is self.config.y.
+
+ Note that 'depends on' and parent dependencies are propagated to 'range'
+ conditions.
+
+ Gotcha: 1 and 2 above will be represented as (undefined) Symbols rather
+ than plain integers. Undefined symbols get their name as their string
+ value, so this works out. The C tools work the same way.
+
+ rev_dep:
+ Reverse dependency expression from other symbols selecting this symbol.
+ Multiple selections get ORed together. A condition on a select is ANDed
+ with the selecting symbol.
+
+ For example, if A has 'select FOO' and B has 'select FOO if C', then
+ FOO's rev_dep will be (OR, A, (AND, B, C)).
+
+ weak_rev_dep:
+ Like rev_dep, for imply.
+
+ direct_dep:
+ The 'depends on' dependencies. If a symbol is defined in multiple
+ locations, the dependencies at each location are ORed together.
+
+ Internally, this is only used to implement 'imply', which only applies if
+ the implied symbol has expr_value(self.direct_dep) != 0. 'depends on' and
+ parent dependencies are automatically propagated to the conditions of
+ properties, so normally it's redundant to check the direct dependencies.
+
+ env_var:
+ If the Symbol has an 'option env="FOO"' option, this contains the name
+ ("FOO") of the environment variable. None for symbols that aren't set
+ from the environment.
+
+ 'option env="FOO"' acts as a 'default' property whose value is the value
+ of $FOO.
+
+ env_var is set to "<uname release>" for the predefined symbol
+ UNAME_RELEASE, which holds the 'release' field from uname.
+
+ Symbols with an 'option env' option are never written out to .config
+ files, even if they are visible. env_var corresponds to a flag called
+ SYMBOL_AUTO in the C implementation.
+
+ is_allnoconfig_y:
+ True if the symbol has 'option allnoconfig_y' set on it. This has no
+ effect internally (except when printing symbols), but can be checked by
+ scripts.
+
+ is_constant:
+ True if the symbol is a constant (quoted) symbol.
+
+ kconfig:
+ The Kconfig instance this symbol is from.
+ """
+ __slots__ = (
+ "_already_written",
+ "_cached_assignable",
+ "_cached_str_val",
+ "_cached_tri_val",
+ "_cached_vis",
+ "_dependents",
+ "_was_set",
+ "_write_to_conf",
+ "choice",
+ "defaults",
+ "direct_dep",
+ "env_var",
+ "implies",
+ "is_allnoconfig_y",
+ "is_constant",
+ "kconfig",
+ "name",
+ "nodes",
+ "orig_type",
+ "ranges",
+ "rev_dep",
+ "selects",
+ "user_value",
+ "weak_rev_dep",
+ )
- if self._type in (BOOL, TRISTATE):
- if not self._is_choice_sym:
- self._write_to_conf = (vis != "n")
+ #
+ # Public interface
+ #
- if vis != "n" and self._user_val is not None:
- # If the symbol is visible and has a user value, we use
- # that
- val = self._config._eval_min(self._user_val, vis)
+ @property
+ def type(self):
+ """
+ See the class documentation.
+ """
+ if self.orig_type == TRISTATE and \
+ ((self.choice and self.choice.tri_value == 2) or
+ not self.kconfig.modules.tri_value):
+ return BOOL
- else:
- # Otherwise, we look at defaults and weak reverse
- # dependencies (implies)
-
- for def_expr, cond_expr in self._def_exprs:
- cond_val = self._config._eval_expr(cond_expr)
- if cond_val != "n":
- self._write_to_conf = True
- val = self._config._eval_min(def_expr, cond_val)
- break
-
- # Weak reverse dependencies are only considered if our
- # direct dependencies are met
- if self._config._eval_expr(self._direct_deps) != "n":
- weak_rev_dep_val = \
- self._config._eval_expr(self._weak_rev_dep)
- if weak_rev_dep_val != "n":
- self._write_to_conf = True
- val = self._config._eval_max(val, weak_rev_dep_val)
-
- # Reverse (select-related) dependencies take precedence
- rev_dep_val = self._config._eval_expr(self._rev_dep)
- if rev_dep_val != "n":
- self._write_to_conf = True
- val = self._config._eval_max(val, rev_dep_val)
+ return self.orig_type
- else:
- # (bool/tristate) symbol in choice. See _get_visibility() for
- # more choice-related logic.
+ @property
+ def str_value(self):
+ """
+ See the class documentation.
+ """
+ if self._cached_str_val is not None:
+ return self._cached_str_val
- # Initially
- self._write_to_conf = False
+ if self.orig_type in (BOOL, TRISTATE):
+ # Also calculates the visibility, so invalidation safe
+ self._cached_str_val = TRI_TO_STR[self.tri_value]
+ return self._cached_str_val
- if vis != "n":
- choice = self._parent
- mode = choice.get_mode()
+ # As a quirk of Kconfig, undefined symbols get their name as their
+ # string value. This is why things like "FOO = bar" work for seeing if
+ # FOO has the value "bar".
+ if self.orig_type == UNKNOWN:
+ self._cached_str_val = self.name
+ return self.name
- if mode != "n":
- self._write_to_conf = True
+ val = ""
+ # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
+ # function call (property magic)
+ vis = self.visibility
- if mode == "y":
- val = "y" if choice.get_selection() is self \
- else "n"
- elif self._user_val in ("m", "y"):
- # mode == "m" here
- val = "m"
+ self._write_to_conf = (vis != 0)
- # We need to promote "m" to "y" in two circumstances:
- # 1) If our type is boolean
- # 2) If our _weak_rev_dep (from IMPLY) is "y"
- if val == "m" and \
- (self._type == BOOL or
- self._config._eval_expr(self._weak_rev_dep) == "y"):
- val = "y"
+ if self.orig_type in (INT, HEX):
+ # The C implementation checks the user value against the range in a
+ # separate code path (post-processing after loading a .config).
+ # Checking all values here instead makes more sense for us. It
+ # requires that we check for a range first.
- elif self._type in (INT, HEX):
- base = _TYPE_TO_BASE[self._type]
+ base = _TYPE_TO_BASE[self.orig_type]
# Check if a range is in effect
- for low_expr, high_expr, cond_expr in self._ranges:
- if self._config._eval_expr(cond_expr) != "n":
+ for low_expr, high_expr, cond in self.ranges:
+ if expr_value(cond):
has_active_range = True
- low_str = _str_val(low_expr)
- high_str = _str_val(high_expr)
-
- low = int(low_str, base) if \
- _is_base_n(low_str, base) else 0
- high = int(high_str, base) if \
- _is_base_n(high_str, base) else 0
+ # The zeros are from the C implementation running strtoll()
+ # on empty strings
+ low = int(low_expr.str_value, base) if \
+ _is_base_n(low_expr.str_value, base) else 0
+ high = int(high_expr.str_value, base) if \
+ _is_base_n(high_expr.str_value, base) else 0
break
else:
has_active_range = False
- self._write_to_conf = (vis != "n")
-
- if vis != "n" and self._user_val is not None and \
- _is_base_n(self._user_val, base) and \
+ if vis and self.user_value is not None and \
+ _is_base_n(self.user_value, base) and \
(not has_active_range or
- low <= int(self._user_val, base) <= high):
+ low <= int(self.user_value, base) <= high):
# If the user value is well-formed and satisfies range
# contraints, it is stored in exactly the same form as
# specified in the assignment (with or without "0x", etc.)
- val = self._user_val
+ val = self.user_value
else:
# No user value or invalid user value. Look at defaults.
- for val_expr, cond_expr in self._def_exprs:
- if self._config._eval_expr(cond_expr) != "n":
+ for val_expr, cond in self.defaults:
+ if expr_value(cond):
self._write_to_conf = True
- # Similarly to above, well-formed defaults are
- # preserved as is. Defaults that do not satisfy a range
- # constraints are clamped and take on a standard form.
-
- val = _str_val(val_expr)
+ val = val_expr.str_value
if _is_base_n(val, base):
val_num = int(val, base)
- if has_active_range:
- clamped_val = None
+ else:
+ val_num = 0 # strtoll() on empty string
+
+ break
+ else:
+ val_num = 0 # strtoll() on empty string
+
+ # This clamping procedure runs even if there's no default
+ if has_active_range:
+ clamp = None
+ if val_num < low:
+ clamp = low
+ elif val_num > high:
+ clamp = high
+
+ if clamp is not None:
+ # The value is rewritten to a standard form if it is
+ # clamped
+ val = str(clamp) \
+ if self.orig_type == INT else \
+ hex(clamp)
+
+ elif self.orig_type == STRING:
+ if vis and self.user_value is not None:
+ # If the symbol is visible and has a user value, use that
+ val = self.user_value
+ else:
+ # Otherwise, look at defaults
+ for val_expr, cond in self.defaults:
+ if expr_value(cond):
+ self._write_to_conf = True
+ val = val_expr.str_value
+ break
- if val_num < low:
- clamped_val = low
- elif val_num > high:
- clamped_val = high
+ self._cached_str_val = val
+ return val
- if clamped_val is not None:
- val = (hex(clamped_val)
- if self._type == HEX else
- str(clamped_val))
+ @property
+ def tri_value(self):
+ """
+ See the class documentation.
+ """
+ if self._cached_tri_val is not None:
+ return self._cached_tri_val
- break
+ if self.orig_type not in (BOOL, TRISTATE):
+ self._cached_tri_val = 0
+ return self._cached_tri_val
- else:
- # No default kicked in. If there is an active range
- # constraint, then the low end of the range is used,
- # provided it's > 0, with "0x" prepended as appropriate.
- if has_active_range and low > 0:
- val = (hex(low) if self._type == HEX else str(low))
+ val = 0
+ # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
+ # function call (property magic)
+ vis = self.visibility
+ self._write_to_conf = (vis != 0)
+
+ if not self.choice:
+ # Non-choice symbol
- elif self._type == STRING:
- self._write_to_conf = (vis != "n")
+ if vis and self.user_value is not None:
+ # If the symbol is visible and has a user value, use that
+ val = min(self.user_value, vis)
- if vis != "n" and self._user_val is not None:
- val = self._user_val
else:
- for val_expr, cond_expr in self._def_exprs:
- if self._config._eval_expr(cond_expr) != "n":
+ # Otherwise, look at defaults and weak reverse dependencies
+ # (implies)
+
+ for default, cond in self.defaults:
+ cond_val = expr_value(cond)
+ if cond_val:
+ val = min(expr_value(default), cond_val)
self._write_to_conf = True
- val = _str_val(val_expr)
break
- self._cached_val = val
+ # Weak reverse dependencies are only considered if our
+ # direct dependencies are met
+ weak_rev_dep_val = expr_value(self.weak_rev_dep)
+ if weak_rev_dep_val and expr_value(self.direct_dep):
+ val = max(weak_rev_dep_val, val)
+ self._write_to_conf = True
+
+ # Reverse (select-related) dependencies take precedence
+ rev_dep_val = expr_value(self.rev_dep)
+ if rev_dep_val:
+ val = max(rev_dep_val, val)
+ self._write_to_conf = True
+
+ # m is promoted to y for (1) bool symbols and (2) symbols with a
+ # weak_rev_dep (from imply) of y
+ if val == 1 and \
+ (self.type == BOOL or expr_value(self.weak_rev_dep) == 2):
+ val = 2
+
+ elif vis == 2:
+ # Visible choice symbol in y-mode choice. The choice mode limits
+ # the visibility of choice symbols, so it's sufficient to just
+ # check the visibility of the choice symbols themselves.
+ val = 2 if self.choice.selection is self else 0
+
+ elif vis and self.user_value:
+ # Visible choice symbol in m-mode choice, with set non-0 user value
+ val = 1
+
+ self._cached_tri_val = val
return val
- def get_user_value(self):
- """Returns the value assigned to the symbol in a .config or via
- Symbol.set_user_value() (provided the value was valid for the type of
- the symbol). Returns None in case of no user value."""
- return self._user_val
-
- def get_upper_bound(self):
- """For string/hex/int symbols and for bool and tristate symbols that
- cannot be modified (see is_modifiable()), returns None.
-
- Otherwise, returns the highest value the symbol can be set to with
- Symbol.set_user_value() (that will not be truncated): one of "m" or
- "y", arranged from lowest to highest. This corresponds to the highest
- value the symbol could be given in e.g. the 'make menuconfig'
- interface.
-
- See also the tri_less*() and tri_greater*() functions, which could come
- in handy."""
- if self._type not in (BOOL, TRISTATE):
- return None
+ @property
+ def assignable(self):
+ """
+ See the class documentation.
+ """
+ if self._cached_assignable is not None:
+ return self._cached_assignable
- # Fast path for the common case
- if self._rev_dep == "n":
- vis = _get_visibility(self)
- return vis if vis != "n" else None
+ self._cached_assignable = self._get_assignable()
+ return self._cached_assignable
- rev_dep_val = self._config._eval_expr(self._rev_dep)
- # A bool selected to "m" gets promoted to "y", pinning it
- if rev_dep_val == "m" and self._type == BOOL:
- return None
- vis = _get_visibility(self)
- return vis if tri_greater(vis, rev_dep_val) else None
-
- def get_lower_bound(self):
- """For string/hex/int symbols and for bool and tristate symbols that
- cannot be modified (see is_modifiable()), returns None.
-
- Otherwise, returns the lowest value the symbol can be set to with
- Symbol.set_user_value() (that will not be truncated): one of "n" or
- "m", arranged from lowest to highest. This corresponds to the lowest
- value the symbol could be given in e.g. the 'make menuconfig'
- interface.
-
- See also the tri_less*() and tri_greater*() functions, which could come
- in handy."""
- if self._type not in (BOOL, TRISTATE):
+ @property
+ def visibility(self):
+ """
+ See the class documentation.
+ """
+ if self._cached_vis is not None:
+ return self._cached_vis
+
+ self._cached_vis = _get_visibility(self)
+ return self._cached_vis
+
+ @property
+ def config_string(self):
+ """
+ See the class documentation.
+ """
+ if self.env_var is not None:
+ # Corresponds to SYMBOL_AUTO being set in the C implementation
return None
- rev_dep_val = self._config._eval_expr(self._rev_dep)
- # A bool selected to "m" gets promoted to "y", pinning it
- if rev_dep_val == "m" and self._type == BOOL:
+
+ # Note: _write_to_conf is determined when the value is calculated. This
+ # is a hidden function call due to property magic.
+ val = self.str_value
+ if not self._write_to_conf:
return None
- return rev_dep_val if tri_greater(_get_visibility(self), rev_dep_val) \
- else None
- def get_assignable_values(self):
- """For string/hex/int symbols and for bool and tristate symbols that
- cannot be modified (see is_modifiable()), returns the empty list.
+ if self.orig_type in (BOOL, TRISTATE):
+ return "{}{}={}\n" \
+ .format(self.kconfig.config_prefix, self.name, val) \
+ if val != "n" else \
+ "# {}{} is not set\n" \
+ .format(self.kconfig.config_prefix, self.name)
- Otherwise, returns a list containing the user values that can be
- assigned to the symbol (that won't be truncated). Usage example:
+ if self.orig_type in (INT, HEX):
+ return "{}{}={}\n" \
+ .format(self.kconfig.config_prefix, self.name, val)
- if "m" in sym.get_assignable_values():
- sym.set_user_value("m")
+ if self.orig_type == STRING:
+ # Escape \ and "
+ return '{}{}="{}"\n' \
+ .format(self.kconfig.config_prefix, self.name, escape(val))
- This is basically a more convenient interface to
- get_lower/upper_bound() when wanting to test if a particular tristate
- value can be assigned."""
- if self._type not in (BOOL, TRISTATE):
- return []
- rev_dep_val = self._config._eval_expr(self._rev_dep)
- # A bool selected to "m" gets promoted to "y", pinning it
- if rev_dep_val == "m" and self._type == BOOL:
- return []
- res = ["n", "m", "y"][_TRI_TO_INT[rev_dep_val] :
- _TRI_TO_INT[_get_visibility(self)] + 1]
- return res if len(res) > 1 else []
-
- def get_visibility(self):
- """Returns the visibility of the symbol: one of "n", "m" or "y". For
- bool and tristate symbols, this is an upper bound on the value users
- can set for the symbol. For other types of symbols, a visibility of "n"
- means the user value will be ignored. A visibility of "n" corresponds
- to not being visible in the 'make *config' interfaces.
-
- Example (assuming we're running with modules enabled -- i.e., MODULES
- set to 'y'):
-
- # Assume this has been assigned 'n'
- config N_SYM
- tristate "N_SYM"
-
- # Assume this has been assigned 'm'
- config M_SYM
- tristate "M_SYM"
-
- # Has visibility 'n'
- config A
- tristate "A"
- depends on N_SYM
-
- # Has visibility 'm'
- config B
- tristate "B"
- depends on M_SYM
-
- # Has visibility 'y'
- config C
- tristate "C"
-
- # Has no prompt, and hence visibility 'n'
- config D
- tristate
-
- Having visibility be tri-valued ensures that e.g. a symbol cannot be
- set to "y" by the user if it depends on a symbol with value "m", which
- wouldn't be safe.
-
- You should probably look at get_lower/upper_bound(),
- get_assignable_values() and is_modifiable() before using this."""
- return _get_visibility(self)
-
- def get_referenced_symbols(self, refs_from_enclosing=False):
- """Returns the set() of all symbols referenced by this item. For
- example, the symbol defined by
-
- config FOO
- bool
- prompt "foo" if A && B
- default C if D
- depends on E
- select F if G
-
- references the symbols A through G.
-
- refs_from_enclosing (default: False): If True, the symbols referenced
- by enclosing menus and ifs will be included in the result."""
- res = []
-
- for _, cond_expr in self._orig_prompts:
- _expr_syms(cond_expr, res)
- for val_expr, cond_expr in self._orig_def_exprs:
- _expr_syms(val_expr, res)
- _expr_syms(cond_expr, res)
- for sym, cond_expr in self._orig_selects:
- res.append(sym)
- _expr_syms(cond_expr, res)
- for sym, cond_expr in self._orig_implies:
- res.append(sym)
- _expr_syms(cond_expr, res)
- for low, high, cond_expr in self._orig_ranges:
- res.append(low)
- res.append(high)
- _expr_syms(cond_expr, res)
-
- if refs_from_enclosing:
- _expr_syms(self._deps_from_containing, res)
-
- # Remove duplicates and return
- return set(res)
-
- def get_selected_symbols(self):
- """Returns the set() of all symbols X for which this symbol has a
- 'select X' or 'select X if Y' (regardless of whether Y is satisfied or
- not). This is a subset of the symbols returned by
- get_referenced_symbols()."""
- return {sym for sym, _ in self._orig_selects}
-
- def get_implied_symbols(self):
- """Returns the set() of all symbols X for which this symbol has an
- 'imply X' or 'imply X if Y' (regardless of whether Y is satisfied or
- not). This is a subset of the symbols returned by
- get_referenced_symbols()."""
- return {sym for sym, _ in self._orig_implies}
-
- def set_user_value(self, v):
- """Sets the user value of the symbol.
+ _internal_error("Internal error while creating .config: unknown "
+ 'type "{}".'.format(self.orig_type))
+
+ def set_value(self, value):
+ """
+ Sets the user value of the symbol.
Equal in effect to assigning the value to the symbol within a .config
- file. Use get_lower/upper_bound() or get_assignable_values() to find
- the range of currently assignable values for bool and tristate symbols;
- setting values outside this range will cause the user value to differ
- from the result of Symbol.get_value() (be truncated). Values that are
- invalid for the type (such as a_bool.set_user_value("foo")) are
- ignored, and a warning is emitted if an attempt is made to assign such
- a value.
-
- For any type of symbol, is_modifiable() can be used to check if a user
- value will currently have any effect on the symbol, as determined by
- its visibility and range of assignable values. Any value that is valid
- for the type (bool, tristate, etc.) will end up being reflected in
- get_user_value() though, and might have an effect later if conditions
- change. To get rid of the user value, use unset_user_value().
-
- Any symbols dependent on the symbol are (recursively) invalidated, so
- things will just work with regards to dependencies.
-
- v: The user value to give to the symbol."""
- self._set_user_value_no_invalidate(v, False)
-
- if self._name == "MODULES":
- # Changing MODULES has wide-ranging effects
- self._config._invalidate_all()
- return
+ file. For bool and tristate symbols, use the 'assignable' attribute to
+ check which values can currently be assigned. Setting values outside
+ 'assignable' will cause Symbol.user_str/tri_value to differ from
+ Symbol.str/tri_value (be truncated down or up).
+
+ Setting a choice symbol to 2 (y) only updates Choice.user_selection on
+ the parent choice and not Symbol.user_value itself. This gives the
+ expected behavior when a choice is switched between different modes.
+ Choice.user_selection is considered when the choice is in y mode (the
+ "normal" mode).
+
+ Other symbols that depend (possibly indirectly) on this symbol are
+ automatically recalculated to reflect the assigned value.
+
+ value:
+ The user value to give to the symbol. For bool and tristate symbols,
+ pass 0, 1, 2 for n, m, and y, respectively. For other symbol types,
+ pass a string.
+
+ Values that are invalid for the type (such as "foo" or 1 (m) for a
+ BOOL) are ignored and won't be stored in Symbol.user_str/tri_value.
+ Kconfiglib will print a warning by default for invalid assignments,
+ and set_value() will return False.
+
+ Returns True if the value is valid for the type of the symbol, and
+ False otherwise. This only looks at the form of the value. For BOOL and
+ TRISTATE symbols, check the Symbol.assignable attribute to see what
+ values are currently in range and would actually be reflected in the
+ value of the symbol. For other symbol types, check whether the
+ visibility is non-n.
+ """
+ if value == self.user_value:
+ # We know the value must be valid if it was successfully set
+ # previously
+ self._was_set = True
+ return True
+
+ # Check if the value is valid for our type
+ if not ((self.orig_type == BOOL and value in (0, 2) ) or
+ (self.orig_type == TRISTATE and value in (0, 1, 2) ) or
+ (self.orig_type == STRING and isinstance(value, str)) or
+ (self.orig_type == INT and isinstance(value, str)
+ and _is_base_n(value, 10) ) or
+ (self.orig_type == HEX and isinstance(value, str)
+ and _is_base_n(value, 16)
+ and int(value, 16) >= 0)):
+
+ # Display tristate values as n, m, y in the warning
+ warning = "the value {} is invalid for {}, which has type {}" \
+ .format(TRI_TO_STR[value] if value in (0, 1, 2) else
+ "'{}'".format(value),
+ self.name, TYPE_TO_STR[self.orig_type])
+
+ if self.orig_type in (BOOL, TRISTATE) and value in ("n", "m", "y"):
+ warning += ' (pass 0, 1, 2 for n, m, y, respectively)'
+
+ self.kconfig._warn(warning)
- self._invalidate()
- self._invalidate_dependent()
-
- def unset_user_value(self):
- """Resets the user value of the symbol, as if the symbol had never
- gotten a user value via Config.load_config() or
- Symbol.set_user_value()."""
- self._unset_user_value_no_recursive_invalidate()
- self._invalidate_dependent()
-
- def is_modifiable(self):
- """Returns True if the value of the symbol could be modified by calling
- Symbol.set_user_value().
-
- For bools and tristates, this corresponds to the symbol being visible
- in the 'make menuconfig' interface and not already being pinned to a
- specific value (e.g. because it is selected by another symbol).
-
- For strings and numbers, this corresponds to just being visible. (See
- Symbol.get_visibility().)"""
- if self._is_special:
return False
- if self._type in (BOOL, TRISTATE):
- rev_dep_val = self._config._eval_expr(self._rev_dep)
- # A bool selected to "m" gets promoted to "y", pinning it
- if rev_dep_val == "m" and self._type == BOOL:
- return False
- return tri_greater(_get_visibility(self), rev_dep_val)
- return _get_visibility(self) != "n"
-
- def is_defined(self):
- """Returns False if the symbol is referred to in the Kconfig but never
- actually defined."""
- return self._is_defined
-
- def is_special(self):
- """Returns True if the symbol is one of the special symbols n, m, y, or
- UNAME_RELEASE, or gets its value from the environment."""
- return self._is_special
-
- def is_from_environment(self):
- """Returns True if the symbol gets its value from the environment."""
- return self._is_from_env
-
- def has_ranges(self):
- """Returns True if the symbol is of type INT or HEX and has ranges that
- limit what values it can take on."""
- return bool(self._ranges)
-
- def is_choice_symbol(self):
- """Returns True if the symbol is in a choice statement and is an actual
- choice symbol (see Choice.get_symbols())."""
- return self._is_choice_sym
-
- def is_choice_selection(self):
- """Returns True if the symbol is contained in a choice statement and is
- the selected item. Equivalent to
-
- sym.is_choice_symbol() and sym.get_parent().get_selection() is sym"""
- return self._is_choice_sym and self._parent.get_selection() is self
-
- def is_allnoconfig_y(self):
- """Returns True if the symbol has the 'allnoconfig_y' option set."""
- return self._allnoconfig_y
+
+ if self.choice and value == 2:
+ # Remember this as a choice selection only. Makes switching back
+ # and forth between choice modes work as expected, and makes the
+ # check for whether the user value is the same as before above
+ # safe.
+ self.choice.user_selection = self
+ self.choice._was_set = True
+ if self._is_user_assignable():
+ self.choice._rec_invalidate()
+ else:
+ self.user_value = value
+ self._was_set = True
+ if self._is_user_assignable():
+ self._rec_invalidate()
+
+ return True
+
+ def unset_value(self):
+ """
+ Resets the user value of the symbol, as if the symbol had never gotten
+ a user value via Kconfig.load_config() or Symbol.set_value().
+ """
+ if self.user_value is not None:
+ self.user_value = None
+ if self._is_user_assignable():
+ self._rec_invalidate()
+
+ def __repr__(self):
+ """
+ Returns a string with information about the symbol (including its name,
+ value, visibility, and location(s)) when it is evaluated on e.g. the
+ interactive Python prompt.
+ """
+ fields = []
+
+ fields.append("symbol " + self.name)
+ fields.append(TYPE_TO_STR[self.type])
+
+ for node in self.nodes:
+ if node.prompt:
+ fields.append('"{}"'.format(node.prompt[0]))
+
+ # Only add quotes for non-bool/tristate symbols
+ fields.append("value " +
+ (self.str_value
+ if self.orig_type in (BOOL, TRISTATE) else
+ '"{}"'.format(self.str_value)))
+
+ if not self.is_constant:
+ # These aren't helpful to show for constant symbols
+
+ if self.user_value is not None:
+ # Only add quotes for non-bool/tristate symbols
+ fields.append("user value " +
+ (TRI_TO_STR[self.user_value]
+ if self.orig_type in (BOOL, TRISTATE) else
+ '"{}"'.format(self.user_value)))
+
+ fields.append("visibility " + TRI_TO_STR[self.visibility])
+
+ if self.choice:
+ fields.append("choice symbol")
+
+ if self.is_allnoconfig_y:
+ fields.append("allnoconfig_y")
+
+ if self is self.kconfig.defconfig_list:
+ fields.append("is the defconfig_list symbol")
+
+ if self.env_var is not None:
+ fields.append("from environment variable " + self.env_var)
+
+ if self is self.kconfig.modules:
+ fields.append("is the modules symbol")
+
+ fields.append("direct deps " +
+ TRI_TO_STR[expr_value(self.direct_dep)])
+
+ if self.nodes:
+ for node in self.nodes:
+ fields.append("{}:{}".format(node.filename, node.linenr))
+ else:
+ if self.is_constant:
+ fields.append("constant")
+ else:
+ fields.append("undefined")
+
+ return "<{}>".format(", ".join(fields))
def __str__(self):
- """Returns a string containing various information about the symbol."""
- return self._config._get_sym_or_choice_str(self)
+ """
+ Returns a string representation of the symbol when it is printed,
+ matching the Kconfig format. Prompts and help texts are included,
+ though they really belong to the symbol's menu nodes rather than the
+ symbol itself.
+
+ The output is designed so that feeding it back to a Kconfig parser
+ redefines the symbol as is. This also works for symbols defined in
+ multiple locations, where all the definitions are output. See the
+ module documentation for a small gotcha related to choice symbols.
+
+ An empty string is returned for undefined and constant symbols.
+ """
+ return _sym_choice_str(self)
#
# Private methods
#
def __init__(self):
- """Symbol constructor -- not intended to be called directly by
- Kconfiglib clients."""
-
+ """
+ Symbol constructor -- not intended to be called directly by Kconfiglib
+ clients.
+ """
# These attributes are always set on the instance from outside and
# don't need defaults:
- # _config
- # _name
# _already_written
+ # kconfig
+ # direct_dep
+ # is_constant
+ # name
+ # rev_dep
+ # weak_rev_dep
- self._type = UNKNOWN
- self._prompts = []
- self._def_exprs = [] # 'default' properties
- self._ranges = [] # 'range' properties (for int and hex)
- self._help = None # Help text
- self._rev_dep = "n" # Reverse (select-related) dependencies
- self._weak_rev_dep = "n" # Weak reverse (imply-related) dependencies
- self._parent = None
-
- self._user_val = None # Value set by user
-
- # Prompts, default values, ranges, selects, and implies without any
- # dependencies from parents propagated to them
- self._orig_prompts = []
- self._orig_def_exprs = []
- self._orig_selects = []
- self._orig_implies = []
- self._orig_ranges = []
-
- # Dependencies inherited from containing menus and ifs
- self._deps_from_containing = None
-
- # See comment in _parse_properties()
- self._menu_dep = None
-
- # The direct dependencies (inherited + 'depends on', with OR if a
- # symbol is defined in multiple locations). This is needed for 'imply'
- # support.
- self._direct_deps = "n"
-
- # See Symbol.get_ref/def_locations().
- self._def_locations = []
- self._ref_locations = []
-
- # Populated in Config._build_dep() after parsing. Links the symbol to
- # the symbols that immediately depend on it (in a caching/invalidation
- # sense). The total set of dependent symbols for the symbol (the
- # transitive closure) is calculated on an as-needed basis in
- # _get_dependent().
- self._direct_dependents = set()
+ self.orig_type = UNKNOWN
+ self.defaults = []
+ self.selects = []
+ self.implies = []
+ self.ranges = []
- # Cached values
+ self.nodes = []
- # Caches the calculated value
- self._cached_val = None
- # Caches the visibility, which acts as an upper bound on the value
- self._cached_visibility = None
- # Caches the total list of dependent symbols. Calculated in
- # _get_dependent().
- self._cached_deps = None
+ self.user_value = None
- # Flags
+ # See Kconfig._build_dep()
+ self._dependents = set()
- # Does the symbol have an entry in the Kconfig file?
- self._is_defined = False
- # Should the symbol get an entry in .config?
- self._write_to_conf = False
- # This is set to True for "actual" choice symbols; see
- # Choice._determine_actual_symbols().
- self._is_choice_sym = False
- # Does the symbol get its value in some special way, e.g. from the
- # environment or by being one of the special symbols n, m, and y? If
- # so, the value is stored in self._cached_val, which is never
- # invalidated.
- self._is_special = False
- # Does the symbol get its value from the environment?
- self._is_from_env = False
- # Does the symbol have the 'allnoconfig_y' option set?
- self._allnoconfig_y = False
+ # Cached values
- def _invalidate(self):
- if self._is_special:
- # Special symbols never change value and keep their value in
- # _cached_val
- return
+ self._cached_str_val = self._cached_tri_val = self._cached_vis = \
+ self._cached_assignable = None
- if self._is_choice_sym:
- self._parent._invalidate()
+ # Flags
- self._cached_val = None
- self._cached_visibility = None
+ self.choice = None
+ self.env_var = None
+ self.is_allnoconfig_y = False
- def _invalidate_dependent(self):
- for sym in self._get_dependent():
- sym._invalidate()
+ self._was_set = False
- def _set_user_value_no_invalidate(self, v, suppress_load_warnings):
- """Like set_user_value(), but does not invalidate any symbols.
+ # Should the symbol get an entry in .config? Calculated along with the
+ # value.
+ self._write_to_conf = False
- suppress_load_warnings: some warnings are annoying when loading a
- .config that can be helpful when manually invoking set_user_value().
- This flag is set to True to suppress such warnings.
+ def _get_assignable(self):
+ """
+ Worker function for the 'assignable' attribute.
+ """
+ if self.orig_type not in (BOOL, TRISTATE):
+ return ()
- Perhaps this could be made optional for load_config() instead."""
+ # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
+ # function call (property magic)
+ vis = self.visibility
- if self._is_special:
- if self._is_from_env:
- self._config._warn('attempt to assign the value "{}" to the '
- 'symbol {}, which gets its value from the '
- 'environment. Assignment ignored.'
- .format(v, self._name))
- else:
- self._config._warn('attempt to assign the value "{}" to the '
- 'special symbol {}. Assignment ignored.'
- .format(v, self._name))
- return
+ if not vis:
+ return ()
- if not self._is_defined:
- filename, linenr = self._ref_locations[0]
- self._config._warn_undef_assign(
- 'attempt to assign the value "{}" to {}, which is referenced '
- "at {}:{} but never defined. Assignment ignored."
- .format(v, self._name, filename, linenr))
- return
+ rev_dep_val = expr_value(self.rev_dep)
- # Check if the value is valid for our type
- if not ((self._type == BOOL and v in ("n", "y") ) or
- (self._type == TRISTATE and v in ("n", "m", "y")) or
- (self._type == STRING ) or
- (self._type == INT and _is_base_n(v, 10) ) or
- (self._type == HEX and _is_base_n(v, 16) )):
- self._config._warn('the value "{}" is invalid for {}, which has '
- "type {}. Assignment ignored."
- .format(v, self._name, _TYPENAME[self._type]))
- return
+ if vis == 2:
+ if self.choice:
+ return (2,)
- if not self._prompts and not suppress_load_warnings:
- self._config._warn('assigning "{}" to the symbol {} which lacks '
- 'prompts and thus has visibility "n". The '
- 'assignment will have no effect.'
- .format(v, self._name))
+ if not rev_dep_val:
+ if self.type == BOOL or expr_value(self.weak_rev_dep) == 2:
+ return (0, 2)
+ return (0, 1, 2)
- self._user_val = v
+ if rev_dep_val == 2:
+ return (2,)
- if self._is_choice_sym and self._type in (BOOL, TRISTATE):
- choice = self._parent
- if v == "y":
- choice._user_val = self
- choice._user_mode = "y"
- elif v == "m":
- choice._user_val = None
- choice._user_mode = "m"
+ # rev_dep_val == 1
- def _unset_user_value_no_recursive_invalidate(self):
- self._invalidate()
- self._user_val = None
+ if self.type == BOOL or expr_value(self.weak_rev_dep) == 2:
+ return (2,)
+ return (1, 2)
- if self._is_choice_sym:
- self._parent._unset_user_value()
+ # vis == 1
- def _add_config_strings(self, add_fn):
- if self._already_written:
- return
+ # Must be a tristate here, because bool m visibility gets promoted to y
- self._already_written = True
+ if not rev_dep_val:
+ return (0, 1) if expr_value(self.weak_rev_dep) != 2 else (0, 2)
- # Note: _write_to_conf is determined in get_value()
- val = self.get_value()
- if not self._write_to_conf:
- return
+ if rev_dep_val == 2:
+ return (2,)
- if self._type in (BOOL, TRISTATE):
- add_fn("# {}{} is not set\n".format(self._config._config_prefix,
- self._name)
- if val == "n" else
- "{}{}={}\n".format(self._config._config_prefix, self._name,
- val))
+ # vis == rev_dep_val == 1
- elif self._type in (INT, HEX):
- add_fn("{}{}={}\n".format(self._config._config_prefix, self._name,
- val))
+ return (1,)
- elif self._type == STRING:
- # Escape \ and "
- add_fn('{}{}="{}"\n'
- .format(self._config._config_prefix, self._name,
- val.replace("\\", "\\\\").replace('"', '\\"')))
+ def _is_user_assignable(self):
+ """
+ Returns True if the symbol has a prompt, meaning a user value might
+ have an effect on it. Used as an optimization to skip invalidation when
+ promptless symbols are assigned to (given a user value).
- else:
- _internal_error("Internal error while creating .config: unknown "
- 'type "{}".'.format(self._type))
-
- def _get_dependent(self):
- """Returns the set of symbols that should be invalidated if the value
- of the symbol changes, because they might be affected by the change.
- Note that this is an internal API -- it's probably of limited
- usefulness to clients."""
- if self._cached_deps is not None:
- return self._cached_deps
-
- # Less readable version of the following, measured to reduce the the
- # running time of _get_dependent() on kernel Kconfigs by about 1/3 as
- # measured by line_profiler.
- #
- # res = set(self._direct_dependents)
- # for s in self._direct_dependents:
- # res |= s._get_dependent()
- res = self._direct_dependents | \
- {sym for dep in self._direct_dependents
- for sym in dep._get_dependent()}
-
- if self._is_choice_sym:
- # Choice symbols also depend (recursively) on their siblings. The
- # siblings are not included in _direct_dependents to avoid
- # dependency loops.
- for sibling in self._parent._actual_symbols:
- if sibling is not self:
- res.add(sibling)
- res |= sibling._direct_dependents
- for s in sibling._direct_dependents:
- res |= s._get_dependent()
-
- self._cached_deps = res
- return res
-
- def _has_auto_menu_dep_on(self, on):
- """See Choice._determine_actual_symbols()."""
- if not isinstance(self._parent, Choice):
- _internal_error("Attempt to determine auto menu dependency for "
- "symbol ouside of choice.")
-
- if not self._prompts:
- # If we have no prompt, use the menu dependencies instead (what was
- # specified with 'depends on')
- return self._menu_dep is not None and \
- self._config._expr_depends_on(self._menu_dep, on)
-
- for _, cond_expr in self._prompts:
- if self._config._expr_depends_on(cond_expr, on):
+ Prints a warning if the symbol has no prompt. In some contexts (e.g.
+ when loading a .config files) assignments to promptless symbols are
+ normal and expected, so the warning can be disabled.
+ """
+ for node in self.nodes:
+ if node.prompt:
return True
+ if self.kconfig._warn_no_prompt:
+ self.kconfig._warn(self.name + " has no prompt, meaning user "
+ "values have no effect on it")
return False
-class Menu(Item):
+ def _invalidate(self):
+ """
+ Marks the symbol as needing to be recalculated.
+ """
+ self._cached_str_val = self._cached_tri_val = self._cached_vis = \
+ self._cached_assignable = None
+
+ def _rec_invalidate(self):
+ """
+ Invalidates the symbol and all items that (possibly) depend on it.
+ """
+ if self is self.kconfig.modules:
+ # Invalidating MODULES has wide-ranging effects
+ self.kconfig._invalidate_all()
+ else:
+ self._invalidate()
- """Represents a menu statement."""
+ for item in self._dependents:
+ # _cached_vis doubles as a flag that tells us whether 'item'
+ # has cached values, because it's calculated as a side effect
+ # of calculating all other (non-constant) cached values.
+ #
+ # If item._cached_vis is None, it means there can't be cached
+ # values on other items that depend on 'item', because if there
+ # were, some value on 'item' would have been calculated and
+ # item._cached_vis set as a side effect. It's therefore safe to
+ # stop the invalidation at symbols with _cached_vis None.
+ #
+ # This approach massively speeds up scripts that set a lot of
+ # values, vs simply invalidating all possibly dependent symbols
+ # (even when you already have a list of all the dependent
+ # symbols, because some symbols get huge dependency trees).
+ #
+ # This gracefully handles dependency loops too, which is nice
+ # for choices, where the choice depends on the choice symbols
+ # and vice versa.
+ if item._cached_vis is not None:
+ item._rec_invalidate()
+
+class Choice(object):
+ """
+ Represents a choice statement:
+
+ choice
+ ...
+ endchoice
+
+ The following attributes are available on Choice instances. They should be
+ treated as read-only, and some are implemented through @property magic (but
+ are still efficient to access due to internal caching).
+
+ Note: Prompts, help texts, and locations are stored in the Choice's
+ MenuNode(s) rather than in the Choice itself. Check the MenuNode class and
+ the Choice.nodes attribute. This organization matches the C tools.
+
+ name:
+ The name of the choice, e.g. "FOO" for 'choice FOO', or None if the
+ Choice has no name. I can't remember ever seeing named choices in
+ practice, but the C tools support them too.
+
+ type:
+ The type of the choice. One of BOOL, TRISTATE, UNKNOWN. UNKNOWN is for
+ choices defined without a type where none of the contained symbols have a
+ type either (otherwise the choice inherits the type of the first symbol
+ defined with a type).
+
+ When running without modules (CONFIG_MODULES=n), TRISTATE choices
+ magically change type to BOOL. This matches the C tools, and makes sense
+ for menuconfig-like functionality.
+
+ orig_type:
+ The type as given in the Kconfig file, without any magic applied. Used
+ when printing the choice.
+
+ tri_value:
+ The tristate value (mode) of the choice. A choice can be in one of three
+ modes:
+
+ 0 (n) - The choice is disabled and no symbols can be selected. For
+ visible choices, this mode is only possible for choices with
+ the 'optional' flag set (see kconfig-language.txt).
+
+ 1 (m) - Any number of choice symbols can be set to m, the rest will
+ be n.
+
+ 2 (y) - One symbol will be y, the rest n.
+
+ Only tristate choices can be in m mode. The visibility of the choice is
+ an upper bound on the mode, and the mode in turn is an upper bound on the
+ visibility of the choice symbols.
+
+ To change the mode, use Choice.set_value().
+
+ Implementation note:
+ The C tools internally represent choices as a type of symbol, with
+ special-casing in many code paths. This is why there is a lot of
+ similarity to Symbol. The value (mode) of a choice is really just a
+ normal symbol value, and an implicit reverse dependency forces its
+ lower bound to m for visible non-optional choices (the reverse
+ dependency is 'm && <visibility>').
+
+ Symbols within choices get the choice propagated as a dependency to
+ their properties. This turns the mode of the choice into an upper bound
+ on e.g. the visibility of choice symbols, and explains the gotcha
+ related to printing choice symbols mentioned in the module docstring.
+
+ Kconfiglib uses a separate Choice class only because it makes the code
+ and interface less confusing (especially in a user-facing interface).
+ Corresponding attributes have the same name in the Symbol and Choice
+ classes, for consistency and compatibility.
+
+ assignable:
+ See the symbol class documentation. Gives the assignable values (modes).
+
+ visibility:
+ See the Symbol class documentation. Acts on the value (mode).
+
+ selection:
+ The Symbol instance of the currently selected symbol. None if the Choice
+ is not in y mode or has no selected symbol (due to unsatisfied
+ dependencies on choice symbols).
+
+ WARNING: Do not assign directly to this. It will break things. Call
+ sym.set_value(2) on the choice symbol you want to select instead.
+
+ user_value:
+ The value (mode) selected by the user through Choice.set_value(). Either
+ 0, 1, or 2, or None if the user hasn't selected a mode. See
+ Symbol.user_value.
+
+ WARNING: Do not assign directly to this. It will break things. Use
+ Choice.set_value() instead.
+
+ user_selection:
+ The symbol selected by the user (by setting it to y). Ignored if the
+ choice is not in y mode, but still remembered so that the choice "snaps
+ back" to the user selection if the mode is changed back to y. This might
+ differ from 'selection' due to unsatisfied dependencies.
+
+ WARNING: Do not assign directly to this. It will break things. Call
+ sym.set_value(2) on the choice symbol to be selected instead.
+
+ syms:
+ List of symbols contained in the choice.
+
+ Gotcha: If a symbol depends on the previous symbol within a choice so
+ that an implicit menu is created, it won't be a choice symbol, and won't
+ be included in 'syms'. There are real-world examples of this, and it was
+ a PITA to support in older versions of Kconfiglib that didn't implement
+ the menu structure.
+
+ nodes:
+ A list of MenuNodes for this choice. In practice, the list will probably
+ always contain a single MenuNode, but it is possible to give a choice a
+ name and define it in multiple locations (i've never even seen a named
+ choice though).
+
+ defaults:
+ List of (symbol, cond) tuples for the choice's 'defaults' properties. For
+ example, 'default A if B && C' is represented as (A, (AND, B, C)). If
+ there is no condition, 'cond' is self.config.y.
+
+ Note that 'depends on' and parent dependencies are propagated to
+ 'default' conditions.
+
+ is_optional:
+ True if the choice has the 'optional' flag set on it and can be in
+ n mode.
+
+ kconfig:
+ The Kconfig instance this choice is from.
+ """
+ __slots__ = (
+ "_cached_assignable",
+ "_cached_selection",
+ "_cached_vis",
+ "_dependents",
+ "_was_set",
+ "defaults",
+ "is_constant",
+ "is_optional",
+ "kconfig",
+ "name",
+ "nodes",
+ "orig_type",
+ "syms",
+ "user_selection",
+ "user_value",
+ )
#
# Public interface
#
- def get_config(self):
- """Return the Config instance this menu is from."""
- return self._config
-
- def get_title(self):
- """Returns the title text of the menu."""
- return self._title
-
- def get_parent(self):
- """Returns the menu or choice statement that contains the menu, or
- None if the menu is at the top level. Note that if statements are
- treated as syntactic sugar and do not have an explicit class
- representation."""
- return self._parent
-
- def get_location(self):
- """Returns the location of the menu as a (filename, linenr) tuple,
- where filename is a string and linenr an int."""
- return (self._filename, self._linenr)
-
- def get_items(self, recursive=False):
- """Returns a list containing the items (symbols, menus, choice
- statements and comments) in in the menu, in the same order that the
- items appear within the menu.
-
- recursive (default: False): True if items contained in items within the
- menu should be included recursively (preorder)."""
-
- if not recursive:
- return self._block
-
- res = []
- for item in self._block:
- res.append(item)
- if isinstance(item, Menu):
- res.extend(item.get_items(True))
- elif isinstance(item, Choice):
- res.extend(item.get_items())
- return res
-
- def get_symbols(self, recursive=False):
- """Returns a list containing the symbols in the menu, in the same order
- that they appear within the menu.
-
- recursive (default: False): True if symbols contained in items within
- the menu should be included recursively."""
-
- return [item for item in self.get_items(recursive) if
- isinstance(item, Symbol)]
-
- def get_visibility(self):
- """Returns the visibility of the menu. This also affects the visibility
- of subitems. See also Symbol.get_visibility()."""
- return self._config._eval_expr(self._menu_dep)
-
- def get_visible_if_visibility(self):
- """Returns the visibility the menu gets from its 'visible if'
- condition. "y" if the menu has no 'visible if' condition."""
- return self._config._eval_expr(self._visible_if_expr)
-
- def get_referenced_symbols(self, refs_from_enclosing=False):
- """See Symbol.get_referenced_symbols()."""
- res = []
-
- _expr_syms(self._visible_if_expr, res)
- _expr_syms(self._orig_deps
- if not refs_from_enclosing else
- self._menu_dep,
- res)
-
- # Remove duplicates and return
- return set(res)
+ @property
+ def type(self):
+ """
+ Returns the type of the choice. See Symbol.type.
+ """
+ if self.orig_type == TRISTATE and not self.kconfig.modules.tri_value:
+ return BOOL
- def __str__(self):
- """Returns a string containing various information about the menu."""
- depends_on_str = self._config._expr_val_str(self._orig_deps,
- "(no dependencies)")
- visible_if_str = self._config._expr_val_str(self._visible_if_expr,
- "(no dependencies)")
-
- additional_deps_str = " " + \
- self._config._expr_val_str(self._deps_from_containing,
- "(no additional dependencies)")
-
- return _lines("Menu",
- "Title : " + self._title,
- "'depends on' dependencies : " + depends_on_str,
- "'visible if' dependencies : " + visible_if_str,
- "Additional dependencies from enclosing menus and ifs:",
- additional_deps_str,
- "Location: {}:{}".format(self._filename, self._linenr))
+ return self.orig_type
- #
- # Private methods
- #
+ @property
+ def str_value(self):
+ """
+ See the class documentation.
+ """
+ return TRI_TO_STR[self.tri_value]
- def __init__(self):
- """Menu constructor -- not intended to be called directly by
- Kconfiglib clients."""
+ @property
+ def tri_value(self):
+ """
+ See the class documentation.
+ """
+ # This emulates a reverse dependency of 'm && visibility' for
+ # non-optional choices, which is how the C implementation does it
- # These attributes are always set on the instance from outside and
- # don't need defaults:
- # _config
- # _parent
- # _filename
- # _linenr
- # _title
- # _deps_from_containing
- # _menu_dep
+ val = 0 if self.is_optional else 1
- # Dependencies specified with 'visible_if'
- self._visible_if_expr = None
+ if self.user_value is not None:
+ val = max(val, self.user_value)
- # Dependency expression without dependencies from enclosing menus and
- # ifs propagated
- self._orig_deps = None
+ # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
+ # function call (property magic)
+ val = min(val, self.visibility)
- # Contained items
- self._block = []
+ # Promote m to y for boolean choices
+ return 2 if val == 1 and self.type == BOOL else val
- def _add_config_strings(self, add_fn):
- if self._config._eval_expr(self._menu_dep) != "n" and \
- self._config._eval_expr(self._visible_if_expr) != "n":
- add_fn("\n#\n# {}\n#\n".format(self._title))
+ @property
+ def assignable(self):
+ """
+ See the class documentation.
+ """
+ if self._cached_assignable is not None:
+ return self._cached_assignable
- for item in self._block:
- item._add_config_strings(add_fn)
+ self._cached_assignable = self._get_assignable()
+ return self._cached_assignable
-class Choice(Item):
+ @property
+ def visibility(self):
+ """
+ See the class documentation.
+ """
+ if self._cached_vis is not None:
+ return self._cached_vis
- """Represents a choice statement. A choice can be in one of three modes:
+ self._cached_vis = _get_visibility(self)
+ return self._cached_vis
- "n" - The choice is not visible and no symbols can be selected.
+ @property
+ def selection(self):
+ """
+ See the class documentation.
+ """
+ if self._cached_selection is not _NO_CACHED_SELECTION:
+ return self._cached_selection
- "m" - Any number of symbols can be set to "m". The rest will be "n". This
- is safe since potentially conflicting options don't actually get
- compiled into the kernel simultaneously with "m".
+ # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
+ # function call (property magic)
+ if self.tri_value != 2:
+ self._cached_selection = None
+ return None
- "y" - One symbol will be "y" while the rest are "n".
+ # Use the user selection if it's visible
+ if self.user_selection and self.user_selection.visibility == 2:
+ self._cached_selection = self.user_selection
+ return self.user_selection
- Only tristate choices can be in "m" mode, and the visibility of the choice
- is an upper bound on the mode, so that e.g. a choice that depends on a
- symbol with value "m" will be in "m" mode.
+ # Otherwise, check if we have a default
+ for sym, cond in self.defaults:
+ # The default symbol must be visible too
+ if expr_value(cond) and sym.visibility:
+ self._cached_selection = sym
+ return sym
- The mode changes automatically when a value is assigned to a symbol within
- the choice.
+ # Otherwise, pick the first visible symbol, if any
+ for sym in self.syms:
+ if sym.visibility:
+ self._cached_selection = sym
+ return sym
- See Symbol.get_visibility() too."""
+ # Couldn't find a selection
+ self._cached_selection = None
+ return None
- #
- # Public interface
- #
+ def set_value(self, value):
+ """
+ Sets the user value (mode) of the choice. Like for Symbol.set_value(),
+ the visibility might truncate the value. Choices without the 'optional'
+ attribute (is_optional) can never be in n mode, but 0 is still accepted
+ since it's not a malformed value (though it will have no effect).
+
+ Returns True if the value is valid for the type of the choice, and
+ False otherwise. This only looks at the form of the value. Check the
+ Choice.assignable attribute to see what values are currently in range
+ and would actually be reflected in the mode of the choice.
+ """
+ if value == self.user_value:
+ # We know the value must be valid if it was successfully set
+ # previously
+ self._was_set = True
+ return True
- def get_config(self):
- """Returns the Config instance this choice is from."""
- return self._config
-
- def get_name(self):
- """For named choices, returns the name. Returns None for unnamed
- choices. No named choices appear anywhere in the kernel Kconfig files
- as of Linux 3.7.0-rc8."""
- return self._name
-
- def get_type(self):
- """Returns the type of the choice. See Symbol.get_type()."""
- return self._type
-
- def get_prompts(self):
- """Returns a list of prompts defined for the choice, in the order they
- appear in the configuration files. Returns the empty list for choices
- with no prompt.
-
- This list will have a single entry for the vast majority of choices
- having prompts, but having multiple prompts for a single choice is
- possible through having multiple 'choice' entries for it (though I'm
- not sure if that ever happens in practice)."""
- return [prompt for prompt, _ in self._orig_prompts]
-
- def get_help(self):
- """Returns the help text of the choice, or None if the choice has no
- help text."""
- return self._help
-
- def get_parent(self):
- """Returns the menu or choice statement that contains the choice, or
- None if the choice is at the top level. Note that if statements are
- treated as syntactic sugar and do not have an explicit class
- representation."""
- return self._parent
-
- def get_def_locations(self):
- """Returns a list of (filename, linenr) tuples, where filename (string)
- and linenr (int) represent a location where the choice is defined. For
- the vast majority of choices (all of them as of Linux 3.7.0-rc8) this
- list will only contain one element, but its possible for named choices
- to be defined in multiple locations."""
- return self._def_locations
-
- def get_selection(self):
- """Returns the symbol selected (either by the user or through
- defaults), or None if either no symbol is selected or the mode is not
- "y"."""
- if self._cached_selection is not None:
- if self._cached_selection == _NO_SELECTION:
- return None
- return self._cached_selection
+ if not ((self.orig_type == BOOL and value in (0, 2) ) or
+ (self.orig_type == TRISTATE and value in (0, 1, 2))):
+ self.kconfig._warn("the value '{}' is invalid for the choice, "
+ "which has type {}. Assignment ignored"
+ .format(value, TYPE_TO_STR[self.orig_type]))
+ return False
- if self.get_mode() != "y":
- return self._cache_ret(None)
+ self.user_value = value
+ self._was_set = True
+ self._rec_invalidate()
- # User choice available?
- if self._user_val is not None and \
- _get_visibility(self._user_val) == "y":
- return self._cache_ret(self._user_val)
+ return True
- if self._optional:
- return self._cache_ret(None)
+ def unset_value(self):
+ """
+ Resets the user value (mode) and user selection of the Choice, as if
+ the user had never touched the mode or any of the choice symbols.
+ """
+ if self.user_value is not None or self.user_selection:
+ self.user_value = self.user_selection = None
+ self._rec_invalidate()
- return self._cache_ret(self.get_selection_from_defaults())
+ def __repr__(self):
+ """
+ Returns a string with information about the choice when it is evaluated
+ on e.g. the interactive Python prompt.
+ """
+ fields = []
- def get_selection_from_defaults(self):
- """Like Choice.get_selection(), but acts as if no symbol has been
- selected by the user and no 'optional' flag is in effect."""
+ fields.append("choice" if self.name is None else \
+ "choice " + self.name)
+ fields.append(TYPE_TO_STR[self.type])
- # Does any 'default SYM [if <cond>]' property apply?
- for sym, cond_expr in self._def_exprs:
- if (self._config._eval_expr(cond_expr) != "n" and
- # Must be visible too
- _get_visibility(sym) != "n"):
- return sym
+ for node in self.nodes:
+ if node.prompt:
+ fields.append('"{}"'.format(node.prompt[0]))
- # Otherwise, pick the first visible symbol
- for sym in self._actual_symbols:
- if _get_visibility(sym) != "n":
- return sym
+ fields.append("mode " + self.str_value)
- # Couldn't find a default
- return None
+ if self.user_value is not None:
+ fields.append('user mode {}'.format(TRI_TO_STR[self.user_value]))
+
+ if self.selection:
+ fields.append("{} selected".format(self.selection.name))
+
+ if self.user_selection:
+ user_sel_str = "{} selected by user" \
+ .format(self.user_selection.name)
+
+ if self.selection is not self.user_selection:
+ user_sel_str += " (overridden)"
+
+ fields.append(user_sel_str)
+
+ fields.append("visibility " + TRI_TO_STR[self.visibility])
- def get_user_selection(self):
- """If the choice is in "y" mode and has a user-selected symbol, returns
- that symbol. Otherwise, returns None."""
- return self._user_val
-
- def get_items(self):
- """Gets all items contained in the choice in the same order as within
- the configuration ("items" instead of "symbols" since choices and
- comments might appear within choices. This only happens in one place as
- of Linux 3.7.0-rc8, in drivers/usb/gadget/Kconfig)."""
- return self._block
-
- def get_symbols(self):
- """Returns a list containing the choice's symbols.
-
- A quirk (perhaps a bug) of Kconfig is that you can put items within a
- choice that will not be considered members of the choice insofar as
- selection is concerned. This happens for example if one symbol within a
- choice 'depends on' the symbol preceding it, or if you put non-symbol
- items within choices.
-
- As of Linux 3.7.0-rc8, this seems to be used intentionally in one
- place: drivers/usb/gadget/Kconfig.
-
- This function returns the "proper" symbols of the choice in the order
- they appear in the choice, excluding such items. If you want all items
- in the choice, use get_items()."""
- return self._actual_symbols
-
- def get_referenced_symbols(self, refs_from_enclosing=False):
- """See Symbol.get_referenced_symbols()."""
- res = []
-
- for _, cond_expr in self._orig_prompts:
- _expr_syms(cond_expr, res)
- for val_expr, cond_expr in self._orig_def_exprs:
- _expr_syms(val_expr, res)
- _expr_syms(cond_expr, res)
-
- if refs_from_enclosing:
- _expr_syms(self._deps_from_containing, res)
-
- # Remove duplicates and return
- return set(res)
-
- def get_visibility(self):
- """Returns the visibility of the choice statement: one of "n", "m" or
- "y". This acts as an upper limit on the mode of the choice (though bool
- choices can only have the mode "y"). See the class documentation for an
- explanation of modes."""
- return _get_visibility(self)
-
- def get_mode(self):
- """Returns the mode of the choice. See the class documentation for
- an explanation of modes."""
- minimum_mode = "n" if self._optional else "m"
- mode = self._user_mode if self._user_mode is not None else minimum_mode
- mode = self._config._eval_min(mode, _get_visibility(self))
-
- # Promote "m" to "y" for boolean choices
- if mode == "m" and self._type == BOOL:
- return "y"
-
- return mode
-
- def is_optional(self):
- """Returns True if the choice has the 'optional' flag set (and so will
- default to "n" mode)."""
- return self._optional
+ if self.is_optional:
+ fields.append("optional")
+
+ for node in self.nodes:
+ fields.append("{}:{}".format(node.filename, node.linenr))
+
+ return "<{}>".format(", ".join(fields))
def __str__(self):
- """Returns a string containing various information about the choice
- statement."""
- return self._config._get_sym_or_choice_str(self)
+ """
+ Returns a string representation of the choice when it is printed,
+ matching the Kconfig format (though without the contained choice
+ symbols). Prompts and help texts are included, though they really
+ belong to the choice's menu nodes rather than the choice itself.
+
+ See Symbol.__str__() as well.
+ """
+ return _sym_choice_str(self)
#
# Private methods
#
def __init__(self):
- """Choice constructor -- not intended to be called directly by
- Kconfiglib clients."""
-
+ """
+ Choice constructor -- not intended to be called directly by Kconfiglib
+ clients.
+ """
# These attributes are always set on the instance from outside and
# don't need defaults:
- # _config
- # _parent
- # _deps_from_containing
- # _actual_symbols (set in _determine_actual_symbols())
+ # kconfig
+
+ self.name = None
+ self.orig_type = UNKNOWN
+ self.syms = []
+ self.defaults = []
- self._name = None # Yes, choices can be named
- self._type = UNKNOWN
- self._prompts = []
- self._def_exprs = [] # 'default' properties
- self._help = None # Help text
+ self.nodes = []
- self._user_val = None
- self._user_mode = None
+ self.user_value = self.user_selection = None
+
+ # Checked by _make_depend_on(). Just set it to avoid having to
+ # special-case choices.
+ self.is_constant = False
+ # See Kconfig._build_dep()
+ self._dependents = set()
# The prompts and default values without any dependencies from
# enclosing menus and ifs propagated
- self._orig_prompts = []
- self._orig_def_exprs = []
-
- # See Choice.get_def_locations()
- self._def_locations = []
+ self.defaults = []
# Cached values
- self._cached_selection = None
- self._cached_visibility = None
-
- self._optional = False
-
- # Contained items
- self._block = []
-
- def _determine_actual_symbols(self):
- """If a symbol's visibility depends on the preceding symbol within a
- choice, it is no longer viewed as a choice item. (This is quite
- possibly a bug, but some things consciously use it... ugh. It stems
- from automatic submenu creation.) In addition, it's possible to have
- choices and comments within choices, and those shouldn't be considered
- choice items either. Only drivers/usb/gadget/Kconfig seems to depend on
- any of this. This method computes the "actual" items in the choice and
- sets the _is_choice_sym flag on them (retrieved via
- is_choice_symbol()).
-
- Don't let this scare you: an earlier version simply checked for a
- sequence of symbols where all symbols after the first appeared in the
- 'depends on' expression of the first, and that worked fine. The added
- complexity is to be future-proof in the event that
- drivers/usb/gadget/Kconfig turns even more sinister. It might very well
- be overkilling things (especially if that file is refactored ;)."""
-
- self._actual_symbols = []
-
- # Items might depend on each other in a tree structure, so we need a
- # stack to keep track of the current tentative parent
- stack = []
-
- for item in self._block:
- if not isinstance(item, Symbol):
- stack = []
- continue
+ self._cached_vis = self._cached_assignable = None
+ self._cached_selection = _NO_CACHED_SELECTION
- while stack:
- if item._has_auto_menu_dep_on(stack[-1]):
- # The item should not be viewed as a choice item, so don't
- # set item._is_choice_sym
- stack.append(item)
- break
- else:
- stack.pop()
- else:
- item._is_choice_sym = True
- self._actual_symbols.append(item)
- stack.append(item)
-
- def _cache_ret(self, selection):
- # As None is used to indicate the lack of a cached value we can't use
- # that to cache the fact that the choice has no selection. Instead, we
- # use the symbolic constant _NO_SELECTION.
- if selection is None:
- self._cached_selection = _NO_SELECTION
- else:
- self._cached_selection = selection
+ self.is_optional = False
+
+ def _get_assignable(self):
+ """
+ Worker function for the 'assignable' attribute.
+ """
+ # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
+ # function call (property magic)
+ vis = self.visibility
+
+ if not vis:
+ return ()
- return selection
+ if vis == 2:
+ if not self.is_optional:
+ return (2,) if self.type == BOOL else (1, 2)
+ return (0, 2) if self.type == BOOL else (0, 1, 2)
+
+ # vis == 1
+
+ return (0, 1) if self.is_optional else (1,)
def _invalidate(self):
- self._cached_selection = None
- self._cached_visibility = None
+ self._cached_vis = self._cached_assignable = None
+ self._cached_selection = _NO_CACHED_SELECTION
- def _unset_user_value(self):
+ def _rec_invalidate(self):
+ """
+ See Symbol._rec_invalidate()
+ """
self._invalidate()
- self._user_val = None
- self._user_mode = None
- def _add_config_strings(self, add_fn):
- for item in self._block:
- item._add_config_strings(add_fn)
+ for item in self._dependents:
+ if item._cached_vis is not None:
+ item._rec_invalidate()
+
+class MenuNode(object):
+ """
+ Represents a menu node in the configuration. This corresponds to an entry
+ in e.g. the 'make menuconfig' interface, though non-visible choices, menus,
+ and comments also get menu nodes. If a symbol or choice is defined in
+ multiple locations, it gets one menu node for each location.
+
+ The top-level menu node, corresponding to the implicit top-level menu, is
+ available in Kconfig.top_node.
+
+ The menu nodes for a Symbol or Choice can be found in the
+ Symbol/Choice.nodes attribute. Menus and comments are represented as plain
+ menu nodes, with their text stored in the prompt attribute (prompt[0]).
+ This mirrors the C implementation.
+
+ The following attributes are available on MenuNode instances. They should
+ be viewed as read-only.
+
+ item:
+ Either a Symbol, a Choice, or one of the constants MENU and COMMENT.
+ Menus and comments are represented as plain menu nodes. Ifs are collapsed
+ (matching the C implementation) and do not appear in the final menu tree.
+
+ next:
+ The following menu node. None if there is no following node.
+
+ list:
+ The first child menu node. None if there are no children.
+
+ Choices and menus naturally have children, but Symbols can also have
+ children because of menus created automatically from dependencies (see
+ kconfig-language.txt).
+
+ parent:
+ The parent menu node. None if there is no parent.
+
+ prompt:
+ A (string, cond) tuple with the prompt for the menu node and its
+ conditional expression (which is self.kconfig.y if there is no
+ condition). None if there is no prompt.
+
+ For symbols and choices, the prompt is stored in the MenuNode rather than
+ the Symbol or Choice instance. For menus and comments, the prompt holds
+ the text.
+
+ help:
+ The help text for the menu node for Symbols and Choices. None if there is
+ no help text. Always stored in the node rather than the Symbol or Choice.
+ It is possible to have a separate help text at each location if a symbol
+ is defined in multiple locations.
+
+ dep:
+ The 'depends on' dependencies for the menu node, or self.kconfig.y if
+ there are no dependencies. Parent dependencies are propagated to this
+ attribute, and this attribute is then in turn propagated to the
+ properties of symbols and choices.
+
+ If a symbol is defined in multiple locations, only the properties defined
+ at a particular location get the corresponding MenuNode.dep dependencies
+ propagated to them.
+
+ visibility:
+ The 'visible if' dependencies for the menu node (which must represent a
+ menu), or self.kconfig.y if there are no 'visible if' dependencies.
+ 'visible if' dependencies are recursively propagated to the prompts of
+ symbols and choices within the menu.
+
+ is_menuconfig:
+ True if the symbol for the menu node (it must be a symbol) was defined
+ with 'menuconfig' rather than 'config' (at this location). This is a hint
+ on how to display the menu entry (display the children in a separate menu
+ rather than indenting them). It's ignored internally by Kconfiglib,
+ except when printing symbols.
+
+ filename/linenr:
+ The location where the menu node appears.
+
+ kconfig:
+ The Kconfig instance the menu node is from.
+ """
+ __slots__ = (
+ "dep",
+ "filename",
+ "help",
+ "is_menuconfig",
+ "item",
+ "kconfig",
+ "linenr",
+ "list",
+ "next",
+ "parent",
+ "prompt",
+ "visibility",
+ )
+
+ def __repr__(self):
+ """
+ Returns a string with information about the menu node when it is
+ evaluated on e.g. the interactive Python prompt.
+ """
+ fields = []
-class Comment(Item):
+ if isinstance(self.item, Symbol):
+ fields.append("menu node for symbol " + self.item.name)
- """Represents a comment statement."""
+ elif isinstance(self.item, Choice):
+ s = "menu node for choice"
+ if self.item.name is not None:
+ s += " " + self.item.name
+ fields.append(s)
- #
- # Public interface
- #
+ elif self.item == MENU:
+ fields.append("menu node for menu")
+
+ elif self.item == COMMENT:
+ fields.append("menu node for comment")
+
+ elif self.item is None:
+ fields.append("menu node for if (should not appear in the final "
+ " tree)")
+
+ else:
+ raise InternalError("unable to determine type in "
+ "MenuNode.__repr__()")
+
+ if self.prompt:
+ fields.append('prompt "{}" (visibility {})'
+ .format(self.prompt[0],
+ TRI_TO_STR[expr_value(self.prompt[1])]))
- def get_config(self):
- """Returns the Config instance this comment is from."""
- return self._config
+ if isinstance(self.item, Symbol) and self.is_menuconfig:
+ fields.append("is menuconfig")
- def get_text(self):
- """Returns the text of the comment."""
- return self._text
+ fields.append("deps " + TRI_TO_STR[expr_value(self.dep)])
- def get_parent(self):
- """Returns the menu or choice statement that contains the comment, or
- None if the comment is at the top level. Note that if statements are
- treated as syntactic sugar and do not have an explicit class
- representation."""
- return self._parent
+ if self.item == MENU:
+ fields.append("'visible if' deps " + \
+ TRI_TO_STR[expr_value(self.visibility)])
- def get_location(self):
- """Returns the location of the comment as a (filename, linenr) tuple,
- where filename is a string and linenr an int."""
- return (self._filename, self._linenr)
+ if isinstance(self.item, (Symbol, Choice)) and self.help is not None:
+ fields.append("has help")
- def get_visibility(self):
- """Returns the visibility of the comment. See also
- Symbol.get_visibility()."""
- return self._config._eval_expr(self._menu_dep)
+ if self.list:
+ fields.append("has child")
- def get_referenced_symbols(self, refs_from_enclosing=False):
- """See Symbol.get_referenced_symbols()."""
- res = []
+ if self.next:
+ fields.append("has next")
- _expr_syms(self._orig_deps
- if not refs_from_enclosing else
- self._menu_dep,
- res)
+ fields.append("{}:{}".format(self.filename, self.linenr))
- # Remove duplicates and return
- return set(res)
+ return "<{}>".format(", ".join(fields))
def __str__(self):
- """Returns a string containing various information about the
- comment."""
- dep_str = self._config._expr_val_str(self._orig_deps,
- "(no dependencies)")
-
- additional_deps_str = " " + \
- self._config._expr_val_str(self._deps_from_containing,
- "(no additional dependencies)")
-
- return _lines("Comment",
- "Text: " + self._text,
- "Dependencies: " + dep_str,
- "Additional dependencies from enclosing menus and ifs:",
- additional_deps_str,
- "Location: {}:{}".format(self._filename, self._linenr))
+ """
+ Returns a string representation of the MenuNode, matching the Kconfig
+ format.
- #
- # Private methods
- #
+ For Symbol and Choice menu nodes, this function simply calls through to
+ MenuNode.item.__str__(). For MENU and COMMENT nodes, a Kconfig-like
+ representation of the menu or comment is returned.
+ """
+ if isinstance(self.item, (Symbol, Choice)):
+ return self.item.__str__()
- # These attributes are always set on the instance from outside and don't
- # need defaults:
- # _config
- # _parent
- # _filename
- # _linenr
- # _text
- # _deps_from_containing
- # _menu_dep
- # _orig_deps
-
- def _add_config_strings(self, add_fn):
- if self._config._eval_expr(self._menu_dep) != "n":
- add_fn("\n#\n# {}\n#\n".format(self._text))
-
-class Kconfig_Syntax_Error(Exception):
- """Exception raised for syntax errors."""
- pass
+ if self.item in (MENU, COMMENT):
+ s = ("menu" if self.item == MENU else "comment") + \
+ ' "{}"\n'.format(escape(self.prompt[0]))
-class Internal_Error(Exception):
- """Exception raised for internal errors."""
- pass
+ if self.dep is not self.kconfig.y:
+ s += "\tdepends on {}\n".format(expr_str(self.dep))
-#
-# Public functions
-#
+ if self.item == MENU and self.visibility is not self.kconfig.y:
+ s += "\tvisible if {}\n".format(expr_str(self.visibility))
-def tri_less(v1, v2):
- """Returns True if the tristate v1 is less than the tristate v2, where "n",
- "m" and "y" are ordered from lowest to highest."""
- return _TRI_TO_INT[v1] < _TRI_TO_INT[v2]
+ return s
-def tri_less_eq(v1, v2):
- """Returns True if the tristate v1 is less than or equal to the tristate
- v2, where "n", "m" and "y" are ordered from lowest to highest."""
- return _TRI_TO_INT[v1] <= _TRI_TO_INT[v2]
+ # 'if' node. Should never appear in the final tree.
+ return "if " + expr_str(self.dep)
-def tri_greater(v1, v2):
- """Returns True if the tristate v1 is greater than the tristate v2, where
- "n", "m" and "y" are ordered from lowest to highest."""
- return _TRI_TO_INT[v1] > _TRI_TO_INT[v2]
+class KconfigSyntaxError(Exception):
+ """
+ Exception raised for syntax errors.
+ """
+ pass
-def tri_greater_eq(v1, v2):
- """Returns True if the tristate v1 is greater than or equal to the tristate
- v2, where "n", "m" and "y" are ordered from lowest to highest."""
- return _TRI_TO_INT[v1] >= _TRI_TO_INT[v2]
+class InternalError(Exception):
+ """
+ Exception raised for internal errors.
+ """
+ pass
#
-# Internal classes
+# Public functions
#
-class _Feed(object):
+def expr_value(expr):
+ """
+ Evaluates the expression 'expr' to a tristate value. Returns 0 (n), 1 (m),
+ or 2 (y).
- """Class for working with sequences in a stream-like fashion; handy for
- tokens."""
+ 'expr' must be an already-parsed expression from a Symbol, Choice, or
+ MenuNode property. To evaluate an expression represented as a string, use
+ Kconfig.eval_string().
- # This would be more helpful on the item classes, but would remove some
- # flexibility
- __slots__ = ['items', 'length', 'i']
+ Passing subexpressions of expressions to this function works as expected.
+ """
+ if not isinstance(expr, tuple):
+ return expr.tri_value
- def __init__(self, items):
- self.items = items
- self.length = len(self.items)
- self.i = 0
+ if expr[0] == AND:
+ v1 = expr_value(expr[1])
+ # Short-circuit the n case as an optimization (~5% faster
+ # allnoconfig.py and allyesconfig.py, as of writing)
+ return 0 if not v1 else min(v1, expr_value(expr[2]))
- def get_next(self):
- if self.i >= self.length:
- return None
- item = self.items[self.i]
- self.i += 1
- return item
-
- def peek_next(self):
- return None if self.i >= self.length else self.items[self.i]
-
- def check(self, token):
- """Check if the next token is 'token'. If so, remove it from the token
- feed and return True. Otherwise, leave it in and return False."""
- if self.i < self.length and self.items[self.i] == token:
- self.i += 1
- return True
- return False
+ if expr[0] == OR:
+ v1 = expr_value(expr[1])
+ # Short-circuit the y case as an optimization
+ return 2 if v1 == 2 else max(v1, expr_value(expr[2]))
- def unget_all(self):
- self.i = 0
+ if expr[0] == NOT:
+ return 2 - expr_value(expr[1])
-class _FileFeed(object):
+ if expr[0] in _RELATIONS:
+ # Implements <, <=, >, >= comparisons as well. These were added to
+ # kconfig in 31847b67 (kconfig: allow use of relations other than
+ # (in)equality).
- """Feeds lines from a file. Keeps track of the filename and current line
- number. Joins any line ending in \\ with the following line. We need to be
- careful to get the line number right in the presence of continuation
- lines."""
+ # This mirrors the C tools pretty closely. Perhaps there's a more
+ # pythonic way to structure this.
- __slots__ = ['filename', 'lines', 'length', 'linenr']
+ oper, op1, op2 = expr
- def __init__(self, filename):
- self.filename = filename
- with open(filename) as f:
- # No interleaving of I/O and processing yet. Don't know if it would
- # help.
- self.lines = f.readlines()
- self.length = len(self.lines)
- self.linenr = 0
-
- def get_next(self):
- if self.linenr >= self.length:
- return None
- line = self.lines[self.linenr]
- self.linenr += 1
- while line.endswith("\\\n"):
- line = line[:-2] + self.lines[self.linenr]
- self.linenr += 1
- return line
+ # If both operands are strings...
+ if op1.orig_type == STRING and op2.orig_type == STRING:
+ # ...then compare them lexicographically
+ comp = _strcmp(op1.str_value, op2.str_value)
+ else:
+ # Otherwise, try to compare them as numbers...
+ try:
+ comp = int(op1.str_value, _TYPE_TO_BASE[op1.orig_type]) - \
+ int(op2.str_value, _TYPE_TO_BASE[op2.orig_type])
+ except ValueError:
+ # Fall back on a lexicographic comparison if the operands don't
+ # parse as numbers
+ comp = _strcmp(op1.str_value, op2.str_value)
+
+ if oper == EQUAL: res = comp == 0
+ elif oper == UNEQUAL: res = comp != 0
+ elif oper == LESS: res = comp < 0
+ elif oper == LESS_EQUAL: res = comp <= 0
+ elif oper == GREATER: res = comp > 0
+ elif oper == GREATER_EQUAL: res = comp >= 0
+
+ return 2*res
+
+ _internal_error("Internal error while evaluating expression: "
+ "unknown operation {}.".format(expr[0]))
+
+def expr_str(expr):
+ """
+ Returns the string representation of the expression 'expr', as in a Kconfig
+ file.
+
+ Passing subexpressions of expressions to this function works as expected.
+ """
+ if not isinstance(expr, tuple):
+ if isinstance(expr, Choice):
+ if expr.name is not None:
+ return "<choice {}>".format(expr.name)
+ return "<choice>"
+
+ # Symbol
+
+ if expr.is_constant:
+ return '"{}"'.format(escape(expr.name))
+
+ return expr.name
+
+ if expr[0] == NOT:
+ if isinstance(expr[1], Symbol):
+ return "!" + expr_str(expr[1])
+ return "!({})".format(expr_str(expr[1]))
- def peek_next(self):
- linenr = self.linenr
- if linenr >= self.length:
- return None
- line = self.lines[linenr]
- while line.endswith("\\\n"):
- linenr += 1
- line = line[:-2] + self.lines[linenr]
- return line
+ if expr[0] == AND:
+ return "{} && {}".format(_format_and_op(expr[1]),
+ _format_and_op(expr[2]))
- def unget(self):
- self.linenr -= 1
- while self.lines[self.linenr].endswith("\\\n"):
- self.linenr -= 1
+ if expr[0] == OR:
+ return "{} || {}".format(expr_str(expr[1]), expr_str(expr[2]))
- def next_nonblank(self):
- """Removes lines up to and including the next non-blank (not all-space)
- line and returns it. Returns None if there are no more non-blank
- lines."""
- while 1:
- line = self.get_next()
- if line is None or not line.isspace():
- return line
+ # Relation
+ return "{} {} {}".format(expr_str(expr[1]),
+ _REL_TO_STR[expr[0]],
+ expr_str(expr[2]))
+
+# escape()/unescape() helpers
+_escape_re_sub = re.compile(r'(["\\])').sub
+_unescape_re_sub = re.compile(r"\\(.)").sub
+
+def escape(s):
+ r"""
+ Escapes the string 's' in the same fashion as is done for display in
+ Kconfig format and when writing strings to a .config file. " and \ are
+ replaced by \" and \\, respectively.
+ """
+ return _escape_re_sub(r"\\\1", s)
+
+def unescape(s):
+ r"""
+ Unescapes the string 's'. \ followed by any character is replaced with just
+ that character. Used internally when reading .config files.
+ """
+ return _unescape_re_sub(r"\1", s)
#
# Internal functions
#
def _get_visibility(sc):
- """Symbols and Choices have a "visibility" that acts as an upper bound on
- the values a user can set for them, corresponding to the visibility in e.g.
+ """
+ Symbols and Choices have a "visibility" that acts as an upper bound on the
+ values a user can set for them, corresponding to the visibility in e.g.
'make menuconfig'. This function calculates the visibility for the Symbol
- or Choice 'sc' -- the logic is nearly identical."""
- if sc._cached_visibility is None:
- vis = "n"
- for _, cond_expr in sc._prompts:
- vis = sc._config._eval_max(vis, cond_expr)
-
- if isinstance(sc, Symbol) and sc._is_choice_sym:
- choice = sc._parent
- if choice._type == TRISTATE and sc._type != TRISTATE and \
- choice.get_mode() != "y":
- # Non-tristate choice symbols in tristate choices depend on the
- # choice being in mode "y"
- vis = "n"
- elif sc._type == TRISTATE and vis == "m" and \
- choice.get_mode() == "y":
- # Choice symbols with visibility "m" are not visible if the
- # choice has mode "y"
- vis = "n"
- else:
- vis = sc._config._eval_min(vis, _get_visibility(choice))
-
- # Promote "m" to "y" if we're dealing with a non-tristate
- if vis == "m" and sc._type != TRISTATE:
- vis = "y"
-
- sc._cached_visibility = vis
-
- return sc._cached_visibility
-
-def _make_and(e1, e2):
- """Constructs an _AND (&&) expression. Performs trivial simplification.
- Nones equate to 'y'.
-
- Returns None if e1 == e2 == None, so that ANDing two nonexistent
- expressions gives a nonexistent expression."""
- if e1 is None or e1 == "y":
- return e2
- if e2 is None or e2 == "y":
- return e1
- return (_AND, e1, e2)
-
-def _make_or(e1, e2):
- """Constructs an _OR (||) expression. Performs trivial simplification and
- avoids Nones. Nones equate to 'y', which is usually what we want, but needs
- to be kept in mind."""
-
- # Perform trivial simplification and avoid None's (which
- # correspond to y's)
- if e1 is None or e2 is None or e1 == "y" or e2 == "y":
- return "y"
- if e1 == "n":
- return e2
- return (_OR, e1, e2)
-
-def _expr_syms_rec(expr, res):
- """_expr_syms() helper. Recurses through expressions."""
- if isinstance(expr, Symbol):
- res.append(expr)
- elif isinstance(expr, str):
- return
- elif expr[0] in (_AND, _OR):
- _expr_syms_rec(expr[1], res)
- _expr_syms_rec(expr[2], res)
- elif expr[0] == _NOT:
- _expr_syms_rec(expr[1], res)
+ or Choice 'sc' -- the logic is nearly identical.
+ """
+ vis = 0
+
+ for node in sc.nodes:
+ if node.prompt:
+ vis = max(vis, expr_value(node.prompt[1]))
+
+ if isinstance(sc, Symbol) and sc.choice:
+ if sc.choice.orig_type == TRISTATE and sc.orig_type != TRISTATE and \
+ sc.choice.tri_value != 2:
+ # Non-tristate choice symbols are only visible in y mode
+ return 0
+
+ if sc.orig_type == TRISTATE and vis == 1 and sc.choice.tri_value == 2:
+ # Choice symbols with m visibility are not visible in y mode
+ return 0
+
+ # Promote m to y if we're dealing with a non-tristate. This might lead to
+ # infinite recursion if something really weird is done with MODULES, but
+ # it's not a problem in practice.
+ if vis == 1 and \
+ (sc.orig_type != TRISTATE or not sc.kconfig.modules.tri_value):
+ return 2
+
+ return vis
+
+def _make_depend_on(sym, expr):
+ """
+ Adds 'sym' as a dependency to all symbols in 'expr'. Constant symbols in
+ 'expr' are skipped as they can never change value anyway.
+ """
+ if not isinstance(expr, tuple):
+ if not expr.is_constant:
+ expr._dependents.add(sym)
+
+ elif expr[0] in (AND, OR):
+ _make_depend_on(sym, expr[1])
+ _make_depend_on(sym, expr[2])
+
+ elif expr[0] == NOT:
+ _make_depend_on(sym, expr[1])
+
elif expr[0] in _RELATIONS:
- if isinstance(expr[1], Symbol):
- res.append(expr[1])
- if isinstance(expr[2], Symbol):
- res.append(expr[2])
+ if not expr[1].is_constant:
+ expr[1]._dependents.add(sym)
+ if not expr[2].is_constant:
+ expr[2]._dependents.add(sym)
+
else:
_internal_error("Internal error while fetching symbols from an "
"expression with token stream {}.".format(expr))
-def _expr_syms(expr, res):
- """append()s the symbols in 'expr' to 'res'. Does not remove duplicates."""
- if expr is not None:
- _expr_syms_rec(expr, res)
-
-def _str_val(obj):
- """Returns the value of obj as a string. If obj is not a string (constant
- symbol), it must be a Symbol."""
- return obj if isinstance(obj, str) else obj.get_value()
-
def _format_and_op(expr):
- """_expr_to_str() helper. Returns the string representation of 'expr',
- which is assumed to be an operand to _AND, with parentheses added if
- needed."""
- if isinstance(expr, tuple) and expr[0] == _OR:
- return "({})".format(_expr_to_str(expr))
- return _expr_to_str(expr)
-
-def _expr_to_str(expr):
- if isinstance(expr, str):
- return '"{}"'.format(expr)
-
- if isinstance(expr, Symbol):
- return expr._name
-
- if expr[0] == _NOT:
- if isinstance(expr[1], (str, Symbol)):
- return "!" + _expr_to_str(expr[1])
- return "!({})".format(_expr_to_str(expr[1]))
-
- if expr[0] == _AND:
- return "{} && {}".format(_format_and_op(expr[1]),
- _format_and_op(expr[2]))
-
- if expr[0] == _OR:
- return "{} || {}".format(_expr_to_str(expr[1]),
- _expr_to_str(expr[2]))
-
- # Relation
- return "{} {} {}".format(_expr_to_str(expr[1]),
- _RELATION_TO_STR[expr[0]],
- _expr_to_str(expr[2]))
-
-def _type_and_val(obj):
- """Helper to hack around the fact that we don't represent plain strings as
- Symbols. Takes either a plain string or a Symbol and returns a
- (<type>, <value>) tuple."""
- return (obj._type, obj.get_value()) \
- if not isinstance(obj, str) \
- else (STRING, obj)
+ """
+ expr_str() helper. Returns the string representation of 'expr', which is
+ assumed to be an operand to AND, with parentheses added if needed.
+ """
+ if isinstance(expr, tuple) and expr[0] == OR:
+ return "({})".format(expr_str(expr))
+ return expr_str(expr)
def _indentation(line):
- """Returns the length of the line's leading whitespace, treating tab stops
- as being spaced 8 characters apart."""
+ """
+ Returns the length of the line's leading whitespace, treating tab stops as
+ being spaced 8 characters apart.
+ """
line = line.expandtabs()
return len(line) - len(line.lstrip())
def _deindent(line, indent):
- """Deindent 'line' by 'indent' spaces."""
+ """
+ Deindents 'line' by 'indent' spaces.
+ """
line = line.expandtabs()
if len(line) <= indent:
return line
@@ -3546,37 +3698,291 @@ def _is_base_n(s, n):
return False
def _strcmp(s1, s2):
- """strcmp()-alike that returns -1, 0, or 1."""
+ """
+ strcmp()-alike that returns -1, 0, or 1.
+ """
return (s1 > s2) - (s1 < s2)
-def _lines(*args):
- """Returns a string consisting of all arguments, with newlines inserted
- between them."""
- return "\n".join(args)
-
def _stderr_msg(msg, filename, linenr):
if filename is not None:
- sys.stderr.write("{}:{}: ".format(filename, linenr))
- sys.stderr.write(msg + "\n")
+ msg = "{}:{}: {}".format(filename, linenr, msg)
-def _tokenization_error(s, filename, linenr):
- loc = "" if filename is None else "{}:{}: ".format(filename, linenr)
- raise Kconfig_Syntax_Error("{}Couldn't tokenize '{}'"
- .format(loc, s.strip()))
-
-def _parse_error(s, msg, filename, linenr):
- loc = "" if filename is None else "{}:{}: ".format(filename, linenr)
- raise Kconfig_Syntax_Error("{}Couldn't parse '{}'{}"
- .format(loc, s.strip(),
- "." if msg is None else ": " + msg))
+ sys.stderr.write(msg + "\n")
def _internal_error(msg):
- raise Internal_Error(
+ raise InternalError(
msg +
"\nSorry! You may want to send an email to ulfalizer a.t Google's "
"email service to tell me about this. Include the message above and "
"the stack trace and describe what you were doing.")
+# Printing functions
+
+def _sym_choice_str(sc):
+ """
+ Symbol/choice __str__() implementation. These have many properties in
+ common, so it makes sense to handle them together.
+ """
+ lines = []
+
+ def indent_add(s):
+ lines.append("\t" + s)
+
+ # We print the prompt(s) and help text(s) too as a convenience, even though
+ # they're actually part of the MenuNode. If a symbol or choice is defined
+ # in multiple locations (has more than one MenuNode), we output one
+ # statement for each location, and print all the properties that belong to
+ # the symbol/choice itself only at the first location. This gives output
+ # that would function if fed to a Kconfig parser, even for such
+ # symbols/choices (choices defined in multiple locations gets a bit iffy
+ # since they also have child nodes, though I've never seen such a choice).
+
+ if not sc.nodes:
+ return ""
+
+ for node in sc.nodes:
+ if isinstance(sc, Symbol):
+ if node.is_menuconfig:
+ lines.append("menuconfig " + sc.name)
+ else:
+ lines.append("config " + sc.name)
+ else:
+ if sc.name is None:
+ lines.append("choice")
+ else:
+ lines.append("choice " + sc.name)
+
+ if node is sc.nodes[0] and sc.orig_type != UNKNOWN:
+ indent_add(TYPE_TO_STR[sc.orig_type])
+
+ if node.prompt:
+ prompt, cond = node.prompt
+ prompt_str = 'prompt "{}"'.format(escape(prompt))
+ if cond is not sc.kconfig.y:
+ prompt_str += " if " + expr_str(cond)
+ indent_add(prompt_str)
+
+ if node is sc.nodes[0]:
+ if isinstance(sc, Symbol):
+ if sc.is_allnoconfig_y:
+ indent_add("option allnoconfig_y")
+
+ if sc is sc.kconfig.defconfig_list:
+ indent_add("option defconfig_list")
+
+ if sc.env_var is not None:
+ indent_add('option env="{}"'.format(sc.env_var))
+
+ if sc is sc.kconfig.modules:
+ indent_add("option modules")
+
+ if isinstance(sc, Symbol):
+ for low, high, cond in sc.ranges:
+ range_string = "range {} {}" \
+ .format(expr_str(low), expr_str(high))
+ if cond is not sc.kconfig.y:
+ range_string += " if " + expr_str(cond)
+ indent_add(range_string)
+
+ for default, cond in sc.defaults:
+ default_string = "default " + expr_str(default)
+ if cond is not sc.kconfig.y:
+ default_string += " if " + expr_str(cond)
+ indent_add(default_string)
+
+ if isinstance(sc, Choice) and sc.is_optional:
+ indent_add("optional")
+
+ if isinstance(sc, Symbol):
+ for select, cond in sc.selects:
+ select_string = "select " + select.name
+ if cond is not sc.kconfig.y:
+ select_string += " if " + expr_str(cond)
+ indent_add(select_string)
+
+ for imply, cond in sc.implies:
+ imply_string = "imply " + imply.name
+ if cond is not sc.kconfig.y:
+ imply_string += " if " + expr_str(cond)
+ indent_add(imply_string)
+
+ if node.help is not None:
+ indent_add("help")
+ for line in node.help.splitlines():
+ indent_add(" " + line)
+
+ # Add a blank line if there are more nodes to print
+ if node is not sc.nodes[-1]:
+ lines.append("")
+
+ return "\n".join(lines) + "\n"
+
+# Menu manipulation
+
+def _expr_depends_on(expr, sym):
+ """
+ Reimplementation of expr_depends_symbol() from mconf.c. Used to
+ determine if a submenu should be implicitly created. This also influences
+ which items inside choice statements are considered choice items.
+ """
+ if not isinstance(expr, tuple):
+ return expr is sym
+
+ if expr[0] in (EQUAL, UNEQUAL):
+ # Check for one of the following:
+ # sym = m/y, m/y = sym, sym != n, n != sym
+
+ left, right = expr[1:]
+
+ if right is sym:
+ left, right = right, left
+
+ if left is not sym:
+ return False
+
+ return (expr[0] == EQUAL and right is sym.kconfig.m or \
+ right is sym.kconfig.y) or \
+ (expr[0] == UNEQUAL and right is sym.kconfig.n)
+
+ if expr[0] == AND:
+ return _expr_depends_on(expr[1], sym) or \
+ _expr_depends_on(expr[2], sym)
+
+ return False
+
+def _has_auto_menu_dep(node1, node2):
+ """
+ Returns True if node2 has an "automatic menu dependency" on node1. If node2
+ has a prompt, we check its condition. Otherwise, we look directly at
+ node2.dep.
+ """
+ if node2.prompt:
+ return _expr_depends_on(node2.prompt[1], node1.item)
+
+ # If we have no prompt, use the menu node dependencies instead
+ return _expr_depends_on(node2.dep, node1.item)
+
+def _check_auto_menu(node):
+ """
+ Looks for menu nodes after 'node' that depend on it. Creates an implicit
+ menu rooted at 'node' with the nodes as the children if such nodes are
+ found. The recursive call to _finalize_tree() makes this work recursively.
+ """
+ cur = node
+ while cur.next and _has_auto_menu_dep(node, cur.next):
+ _finalize_tree(cur.next)
+ cur = cur.next
+ cur.parent = node
+
+ if cur is not node:
+ node.list = node.next
+ node.next = cur.next
+ cur.next = None
+
+def _flatten(node):
+ """
+ "Flattens" menu nodes without prompts (e.g. 'if' nodes and non-visible
+ symbols with children from automatic menu creation) so that their children
+ appear after them instead. This gives a clean menu structure with no
+ unexpected "jumps" in the indentation.
+ """
+ while node:
+ if node.list and (not node.prompt or node.prompt[0] == ""):
+
+ last_node = node.list
+ while 1:
+ last_node.parent = node.parent
+ if not last_node.next:
+ break
+ last_node = last_node.next
+
+ last_node.next = node.next
+ node.next = node.list
+ node.list = None
+
+ node = node.next
+
+def _remove_ifs(node):
+ """
+ Removes 'if' nodes (which can be recognized by MenuNode.item being None),
+ which are assumed to already have been flattened. The C implementation
+ doesn't bother to do this, but we expose the menu tree directly, and it
+ makes it nicer to work with.
+ """
+ first = node.list
+ while first and first.item is None:
+ first = first.next
+
+ cur = first
+ while cur:
+ if cur.next and cur.next.item is None:
+ cur.next = cur.next.next
+ cur = cur.next
+
+ node.list = first
+
+def _finalize_choice(node):
+ """
+ Finalizes a choice, marking each symbol whose menu node has the choice as
+ the parent as a choice symbol, and automatically determining types if not
+ specified.
+ """
+ choice = node.item
+
+ cur = node.list
+ while cur:
+ if isinstance(cur.item, Symbol):
+ cur.item.choice = choice
+ choice.syms.append(cur.item)
+ cur = cur.next
+
+ # If no type is specified for the choice, its type is that of
+ # the first choice item with a specified type
+ if choice.orig_type == UNKNOWN:
+ for item in choice.syms:
+ if item.orig_type != UNKNOWN:
+ choice.orig_type = item.orig_type
+ break
+
+ # Each choice item of UNKNOWN type gets the type of the choice
+ for sym in choice.syms:
+ if sym.orig_type == UNKNOWN:
+ sym.orig_type = choice.orig_type
+
+def _finalize_tree(node):
+ """
+ Creates implicit menus from dependencies (see kconfig-language.txt),
+ removes 'if' nodes, and finalizes choices. This pretty closely mirrors
+ menu_finalize() from the C implementation, though we propagate dependencies
+ during parsing instead.
+ """
+ # The ordering here gets a bit tricky. It's important to do things in this
+ # order to have everything work out correctly.
+
+ if node.list:
+ # The menu node has children. Finalize them.
+ cur = node.list
+ while cur:
+ _finalize_tree(cur)
+ # Note: _finalize_tree() might have changed cur.next. This is
+ # expected, so that we jump over e.g. implicitly created submenus.
+ cur = cur.next
+
+ elif node.item is not None:
+ # The menu node has no children (yet). See if we can create an implicit
+ # menu rooted at it (due to menu nodes after it depending on it).
+ _check_auto_menu(node)
+
+ if node.list:
+ # We have a node with finalized children. Do final steps to finalize
+ # this node.
+ _flatten(node.list)
+ _remove_ifs(node)
+
+ # Empty choices (node.list None) are possible, so this needs to go outside
+ if isinstance(node.item, Choice):
+ _finalize_choice(node)
+
#
# Public global constants
#
@@ -3591,6 +3997,47 @@ def _internal_error(msg):
UNKNOWN
) = range(6)
+# Integers representing expression types
+(
+ AND,
+ OR,
+ NOT,
+ EQUAL,
+ UNEQUAL,
+ LESS,
+ LESS_EQUAL,
+ GREATER,
+ GREATER_EQUAL,
+) = range(9)
+
+# Integers representing menu and comment menu nodes
+(
+ MENU,
+ COMMENT,
+) = range(2)
+
+# Converts a symbol/choice type to a string
+TYPE_TO_STR = {
+ UNKNOWN: "unknown",
+ BOOL: "bool",
+ TRISTATE: "tristate",
+ STRING: "string",
+ HEX: "hex",
+ INT: "int",
+}
+
+TRI_TO_STR = {
+ 0: "n",
+ 1: "m",
+ 2: "y",
+}
+
+STR_TO_TRI = {
+ "n": 0,
+ "m": 1,
+ "y": 2,
+}
+
#
# Internal global constants
#
@@ -3625,6 +4072,7 @@ def _internal_error(msg):
_T_LESS_EQUAL,
_T_MAINMENU,
_T_MENU,
+ _T_MENUCONFIG,
_T_MODULES,
_T_NOT,
_T_ON,
@@ -3640,10 +4088,10 @@ def _internal_error(msg):
_T_TRISTATE,
_T_UNEQUAL,
_T_VISIBLE,
-) = range(43)
+) = range(44)
-# Keyword to token map. Note that the get() method is assigned directly as a
-# small optimization.
+# Keyword to token map, with the get() method assigned directly as a small
+# optimization
_get_keyword = {
"allnoconfig_y": _T_ALLNOCONFIG_Y,
"bool": _T_BOOL,
@@ -3667,13 +4115,7 @@ _get_keyword = {
"int": _T_INT,
"mainmenu": _T_MAINMENU,
"menu": _T_MENU,
-
- # 'menuconfig' only deals with presentation in the configuration interface
- # and doesn't affect evaluation semantics, so treat it the same as
- # 'config'. Perhaps some presentation-related support could be added as
- # well.
- "menuconfig": _T_CONFIG,
-
+ "menuconfig": _T_MENUCONFIG,
"modules": _T_MODULES,
"on": _T_ON,
"option": _T_OPTION,
@@ -3687,12 +4129,10 @@ _get_keyword = {
"visible": _T_VISIBLE,
}.get
-# Tokens after which identifier-like lexemes are treated as strings, plus
-# _T_CONFIG. This allows us to quickly check if we have a symbol reference (as
-# opposed to a definition or something else) when tokenizing.
-_NOT_REF = frozenset((
+# Tokens after which identifier-like lexemes are treated as strings. _T_CHOICE
+# is included to avoid symbols being registered for named choices.
+_STRING_LEX = frozenset((
_T_BOOL,
- _T_CONFIG,
_T_CHOICE,
_T_COMMENT,
_T_HEX,
@@ -3730,15 +4170,9 @@ _id_keyword_re_match = re.compile(r"([\w./-]+)\s*").match
# Regular expression for finding $-references to symbols in strings
_sym_ref_re_search = re.compile(r"\$([A-Za-z0-9_]+)").search
-# Strings to use for types
-_TYPENAME = {
- UNKNOWN: "unknown",
- BOOL: "bool",
- TRISTATE: "tristate",
- STRING: "string",
- HEX: "hex",
- INT: "int",
-}
+# Matches a valid right-hand side for an assignment to a string symbol in a
+# .config file, including escaped characters. Extracts the contents.
+_conf_string_re_match = re.compile(r'"((?:[^\\"]|\\.)*)"').match
# Token to type mapping
_TOKEN_TO_TYPE = {
@@ -3751,35 +4185,14 @@ _TOKEN_TO_TYPE = {
_T_TRISTATE: TRISTATE,
}
-# Default values for symbols of different types (the value the symbol gets if
-# it is not assigned a user value and none of its 'default' clauses kick in)
-_DEFAULT_VALUE = {
- BOOL: "n",
- TRISTATE: "n",
- HEX: "",
- INT: "",
- STRING: "",
-}
-
-# Indicates that no item is selected in a choice statement
-_NO_SELECTION = 0
-
-# Integers representing expression types
-(
- _AND,
- _OR,
- _NOT,
- _EQUAL,
- _UNEQUAL,
- _LESS,
- _LESS_EQUAL,
- _GREATER,
- _GREATER_EQUAL,
-) = range(9)
+# Constant representing that there's no cached choice selection. This is
+# distinct from a cached None (no selection). We create a unique object (any
+# will do) for it so we can test with 'is'.
+_NO_CACHED_SELECTION = object()
# Used in comparisons. 0 means the base is inferred from the format of the
-# string. The entries for BOOL and TRISTATE are a convenience - they should
-# never convert to valid numbers.
+# string. The entries for BOOL and TRISTATE are an implementation convenience:
+# They should never convert to valid numbers.
_TYPE_TO_BASE = {
BOOL: 0,
HEX: 16,
@@ -3789,37 +4202,30 @@ _TYPE_TO_BASE = {
UNKNOWN: 0,
}
-# Map from tristate values to integers
-_TRI_TO_INT = {
- "n": 0,
- "m": 1,
- "y": 2,
-}
-
_RELATIONS = frozenset((
- _EQUAL,
- _UNEQUAL,
- _LESS,
- _LESS_EQUAL,
- _GREATER,
- _GREATER_EQUAL,
+ EQUAL,
+ UNEQUAL,
+ LESS,
+ LESS_EQUAL,
+ GREATER,
+ GREATER_EQUAL,
))
# Token to relation (=, !=, <, ...) mapping
-_TOKEN_TO_RELATION = {
- _T_EQUAL: _EQUAL,
- _T_GREATER: _GREATER,
- _T_GREATER_EQUAL: _GREATER_EQUAL,
- _T_LESS: _LESS,
- _T_LESS_EQUAL: _LESS_EQUAL,
- _T_UNEQUAL: _UNEQUAL,
+_TOKEN_TO_REL = {
+ _T_EQUAL: EQUAL,
+ _T_GREATER: GREATER,
+ _T_GREATER_EQUAL: GREATER_EQUAL,
+ _T_LESS: LESS,
+ _T_LESS_EQUAL: LESS_EQUAL,
+ _T_UNEQUAL: UNEQUAL,
}
-_RELATION_TO_STR = {
- _EQUAL: "=",
- _GREATER: ">",
- _GREATER_EQUAL: ">=",
- _LESS: "<",
- _LESS_EQUAL: "<=",
- _UNEQUAL: "!=",
+_REL_TO_STR = {
+ EQUAL: "=",
+ GREATER: ">",
+ GREATER_EQUAL: ">=",
+ LESS: "<",
+ LESS_EQUAL: "<=",
+ UNEQUAL: "!=",
}
diff --git a/makefile.patch b/makefile.patch
index e8c4425..5b71d10 100644
--- a/makefile.patch
+++ b/makefile.patch
@@ -1,4 +1,4 @@
-From 6ced956acfe037b0c303780487be86ec71fa2942 Mon Sep 17 00:00:00 2001
+From 01899aa5e6d1f33e1965d7934bea2ff97a48bc89 Mon Sep 17 00:00:00 2001
From: Ulf Magnusson <ulfalizer@gmail.com>
Date: Tue, 9 Jun 2015 13:01:34 +0200
Subject: [PATCH] Kconfiglib scripts/kconfig/Makefile patch.
@@ -8,10 +8,10 @@ Subject: [PATCH] Kconfiglib scripts/kconfig/Makefile patch.
1 file changed, 35 insertions(+)
diff --git a/scripts/kconfig/Makefile b/scripts/kconfig/Makefile
-index d9b1fef..e6311ed 100644
+index 8c12c20c55a6..88c618b34915 100644
--- a/scripts/kconfig/Makefile
+++ b/scripts/kconfig/Makefile
-@@ -62,1 +62,36 @@ else
+@@ -27,1 +27,36 @@ gconfig: $(obj)/gconf
+PHONY += scriptconfig iscriptconfig kconfiglibtestconfig
+
+ifneq ($(filter scriptconfig iscriptconfig,$(MAKECMDGOALS)),)
@@ -27,27 +27,27 @@ index d9b1fef..e6311ed 100644
+scriptconfig:
+ifdef SCRIPT_ARG
+ $(Q)PYTHONPATH="$(srctree)/Kconfiglib:$$PYTHONPATH" \
-+ "$(PYTHONCMD)" "$(SCRIPT)" "$(srctree)/$(Kconfig)" "$(SCRIPT_ARG)"
++ "$(PYTHONCMD)" "$(SCRIPT)" $(Kconfig) "$(SCRIPT_ARG)"
+else
+ $(Q)PYTHONPATH="$(srctree)/Kconfiglib:$$PYTHONPATH" \
-+ "$(PYTHONCMD)" "$(SCRIPT)" "$(srctree)/$(Kconfig)"
++ "$(PYTHONCMD)" "$(SCRIPT)" $(Kconfig)
+endif
+
+iscriptconfig:
+ $(Q)PYTHONPATH="$(srctree)/Kconfiglib:$$PYTHONPATH" "$(PYTHONCMD)" -i -c \
+ "import kconfiglib; \
+ import sys; \
-+ c = kconfiglib.Config(sys.argv[4 if \"$(PYTHONCMD)\".startswith(\"ipython\") else 1]); \
-+ print(\"A Config instance 'c' for the architecture ({0}) has been created.\".format(c.get_arch()))" \
-+ $(srctree)/$(Kconfig)
++ kconf = kconfiglib.Kconfig(sys.argv[4 if \"$(PYTHONCMD)\".startswith(\"ipython\") else 1]); \
++ print(\"A Kconfig instance 'kconf' for the architecture $(ARCH) has been created.\")" \
++ $(Kconfig)
+
+# Used by testsuite.py to be able to compare output for nonsensical
+# arch/defconfig combinations, which has proven effective for finding obscure
+# bugs
+kconfiglibtestconfig: $(obj)/conf
-+ $(Q)$< --defconfig=.config $(srctree)/$(Kconfig)
++ $(Q)$< --defconfig=.config $(Kconfig)
+
menuconfig: $(obj)/mconf
--
-2.1.4
+2.14.1
diff --git a/tests/Kassignable b/tests/Kassignable
new file mode 100644
index 0000000..f134a74
--- /dev/null
+++ b/tests/Kassignable
@@ -0,0 +1,229 @@
+config MODULES
+ bool "modules"
+ option modules
+
+
+# Things that should never be .assignable
+
+if UNDEFINED && "const"
+endif
+
+config NO_PROMPT
+
+config STRING
+ string "string"
+
+config INT
+ int "int"
+
+config HEX
+ hex "hex"
+
+
+# Non-selected symbols
+
+config Y_VIS_BOOL
+ bool "y-vis bool"
+
+config M_VIS_BOOL
+ bool "m-vis bool" if m
+
+config N_VIS_BOOL
+ bool "n-vis bool" if n
+
+config Y_VIS_TRI
+ tristate "y-vis tri"
+
+config M_VIS_TRI
+ tristate "m-vis tri" if m
+
+config N_VIS_TRI
+ tristate "n-vis tri" if n
+
+
+# Symbols selected to y
+
+config Y_SELECTOR
+ def_tristate y
+
+ select Y_SEL_Y_VIS_BOOL
+ select Y_SEL_M_VIS_BOOL
+ select Y_SEL_N_VIS_BOOL
+
+ select Y_SEL_Y_VIS_TRI
+ select Y_SEL_M_VIS_TRI
+ select Y_SEL_N_VIS_TRI
+
+config Y_SEL_Y_VIS_BOOL
+ bool "y-sel y-vis bool"
+
+config Y_SEL_M_VIS_BOOL
+ bool "y-sel m-vis bool" if m
+
+config Y_SEL_N_VIS_BOOL
+ bool "y-sel n-vis bool" if n
+
+config Y_SEL_Y_VIS_TRI
+ tristate "y-sel y-vis tri"
+
+config Y_SEL_M_VIS_TRI
+ tristate "y-sel m-vis tri" if m
+
+config Y_SEL_N_VIS_TRI
+ tristate "y-sel n-vis tri" if n
+
+
+# Symbols selected to m
+
+config M_SELECTOR
+ def_tristate m
+
+ select M_SEL_Y_VIS_BOOL
+ select M_SEL_M_VIS_BOOL
+ select M_SEL_N_VIS_BOOL
+
+ select M_SEL_Y_VIS_TRI
+ select M_SEL_M_VIS_TRI
+ select M_SEL_N_VIS_TRI
+
+config M_SEL_Y_VIS_BOOL
+ bool "m-sel y-vis bool"
+
+config M_SEL_M_VIS_BOOL
+ bool "m-sel m-vis bool" if m
+
+config M_SEL_N_VIS_BOOL
+ bool "m-sel n-vis bool" if n
+
+config M_SEL_Y_VIS_TRI
+ tristate "m-sel y-vis tri"
+
+config M_SEL_M_VIS_TRI
+ tristate "m-sel m-vis tri" if m
+
+config M_SEL_N_VIS_TRI
+ tristate "m-sel n-vis tri" if n
+
+
+# Symbols implied to y
+
+config Y_IMPLIER
+ def_tristate y
+
+ imply Y_IMP_Y_VIS_BOOL
+ imply Y_IMP_M_VIS_BOOL
+ imply Y_IMP_N_VIS_BOOL
+
+ imply Y_IMP_Y_VIS_TRI
+ imply Y_IMP_M_VIS_TRI
+ imply Y_IMP_N_VIS_TRI
+
+config Y_IMP_Y_VIS_BOOL
+ bool "y-imp y-vis bool"
+
+config Y_IMP_M_VIS_BOOL
+ bool "y-imp m-vis bool" if m
+
+config Y_IMP_N_VIS_BOOL
+ bool "y-imp n-vis bool" if n
+
+config Y_IMP_Y_VIS_TRI
+ tristate "y-imp y-vis tri"
+
+config Y_IMP_M_VIS_TRI
+ tristate "y-imp m-vis tri" if m
+
+config Y_IMP_N_VIS_TRI
+ tristate "y-imp n-vis tri" if n
+
+
+# Symbols implied to m (never affects assignable values)
+
+config M_IMPLIER
+ def_tristate m
+
+ imply M_IMP_Y_VIS_BOOL
+ imply M_IMP_M_VIS_BOOL
+ imply M_IMP_N_VIS_BOOL
+
+ imply M_IMP_Y_VIS_TRI
+ imply M_IMP_M_VIS_TRI
+ imply M_IMP_N_VIS_TRI
+
+config M_IMP_Y_VIS_BOOL
+ bool "m-imp y-vis bool"
+
+config M_IMP_M_VIS_BOOL
+ bool "m-imp m-vis bool" if m
+
+config M_IMP_N_VIS_BOOL
+ bool "m-imp n-vis bool" if n
+
+config M_IMP_Y_VIS_TRI
+ tristate "m-imp y-vis tri"
+
+config M_IMP_M_VIS_TRI
+ tristate "m-imp m-vis tri" if m
+
+config M_IMP_N_VIS_TRI
+ tristate "m-imp n-vis tri" if n
+
+
+# Symbols in y-mode choice
+
+choice Y_CHOICE
+ bool "y-mode choice"
+
+config Y_CHOICE_BOOL
+ bool "y-mode choice bool"
+
+config Y_CHOICE_TRISTATE
+ tristate "y-mode choice tristate"
+
+config Y_CHOICE_N_VIS_TRISTATE
+ tristate "y-mode choice tristate invisible" if n
+
+endchoice
+
+
+# Symbols in m/y-mode choice
+
+choice MY_CHOICE
+ tristate "m/y-mode choice"
+
+config MY_CHOICE_BOOL
+ bool "m/y-mode choice bool"
+
+config MY_CHOICE_TRISTATE
+ tristate "m/y-mode choice tristate"
+
+config MY_CHOICE_N_VIS_TRISTATE
+ tristate "m/y-mode choice tristate invisible" if n
+
+endchoice
+
+
+# Choices with some other possible modes
+
+choice NMY_CHOICE
+ tristate "n/m/y-mode choice"
+ optional
+endchoice
+
+choice NY_CHOICE
+ bool "n/y-mode choice"
+ optional
+endchoice
+
+choice NM_CHOICE
+ tristate "n/m-mode choice" if m
+ optional
+endchoice
+
+choice M_CHOICE
+ tristate "m-mode choice" if m
+endchoice
+
+choice N_CHOICE
+ tristate "n-mode choice" if n
+endchoice
diff --git a/tests/Kchoice b/tests/Kchoice
index e80e222..f635ccc 100644
--- a/tests/Kchoice
+++ b/tests/Kchoice
@@ -141,17 +141,39 @@ choice WEIRD_SYMS
# Only WS1 is part of the choice
config WS1
+ bool "WS1"
+
config WS2
+ bool "WS2"
depends on WS1
+
config WS3
+ bool
depends on WS2
+
config WS4
+ bool
depends on WS1
-# 'if' has the same effect, so only WS5 is part of the choice
config WS5
-if WS5
+ bool "WS5" if WS1
+
+# 'if' has the same effect, so only WS6 is part of the choice
config WS6
+ bool "WS6"
+
+if WS6
+
+config WS7
+ bool
+
+config WS8
+ bool "WS8"
+
endif
+# Should also be part of the choice
+config WS9
+ bool "WS9"
+
endchoice
diff --git a/tests/Kdefconfig_existent b/tests/Kdefconfig_existent
index 895c218..8dde443 100644
--- a/tests/Kdefconfig_existent
+++ b/tests/Kdefconfig_existent
@@ -2,6 +2,7 @@
# Should produce "Kconfiglib/tests/defconfig_2"
config FOO
+ string
option env="BAR"
config A
diff --git a/tests/Keval b/tests/Keval
index 64bd4d8..4f63247 100644
--- a/tests/Keval
+++ b/tests/Keval
@@ -1,6 +1,7 @@
# Enabled/disabled in the test
config MODULES
bool "modules"
+ option modules
config N
def_tristate n
diff --git a/tests/Khelp b/tests/Khelp
new file mode 100644
index 0000000..fdc81ed
--- /dev/null
+++ b/tests/Khelp
@@ -0,0 +1,46 @@
+config TWO_HELP_STRINGS
+ help
+ first help string
+
+
+
+
+config TWO_HELP_STRINGS
+ help
+ second help string
+
+config NO_BLANK_AFTER_HELP
+ help
+ help for
+ NO_BLANK_AFTER_HELP
+choice CHOICE_HELP
+ help
+ help for
+ CHOICE_HELP
+endchoice
+
+config HELP_TERMINATED_BY_COMMENT
+ bool
+ help
+ a
+ b
+ c
+#
+
+config TRICKY_HELP
+ bool
+ help
+
+
+ a
+ b
+ c
+
+ d
+ e
+ f
+
+
+ g
+ h
+ i
diff --git a/tests/Klocation b/tests/Klocation
index 404e5ae..737a221 100644
--- a/tests/Klocation
+++ b/tests/Klocation
@@ -1,73 +1,32 @@
-# Include some line continuations to make sure they don't mess up line numbers
+if UNDEFINED
+endif
-# Defined and referenced in multiple locations
-config A
- bool
+config SINGLE_DEF
+
+config MULTI_DEF
# Throw in some line continuations too to make sure it doesn't mess up the line
# numbers
-menu "menu 1"
- depends on A
- visible if A && \
- NOT_DEFINED
-
-# Also defined in Klocation_included
-choice B
- bool "b" if A
-
-config C
- bool "c"
-
-config D
- bool "d"
-
-\
+if y && \
+ y
+if y && \
+ y && \
+ y
-endchoice
+config MULTI_DEF
-config A
- def_bool NOT_DEFINED
+endif
+endif
-comment "comment 1"
-
-config E
- bool "E" if A
- depends on A
-
-endmenu
-
-config \
- FOO
+config EXPANDED_FROM_ENV
string
- option \
- env\
- =\
- "FOO"
+ option env="EXPANDED_FROM_ENV"
-\
-\
-
-config BAR
+config _INCLUDED
string
- default \
- "_included"
+ default "_included"
# Expands to "tests/Klocation_included"
-source \
-"$FOO/Klocation$BAR"
-
-\
-\
-\
+source "$EXPANDED_FROM_ENV/Klocation$_INCLUDED"
-config I
- int
- range A 0
- range 0 A
- range 0 1 if A
- default J if A < 0
- default K if 0 < A
- default L if 0 <= A
- default M if 0 > A
- default N if 0 >= A
- default N if y && 0 < A
+config MULTI_DEF
diff --git a/tests/Klocation_included b/tests/Klocation_included
index 674116a..fb1afaa 100644
--- a/tests/Klocation_included
+++ b/tests/Klocation_included
@@ -1,42 +1,15 @@
-menuconfig A
- bool "A"
-menuconfig A
- bool "A"
-menu "menu 2"
-config M
- def_bool !(n || (n || A = n))
- default !A if y
- default y if !A
-config S
- bool
- select A if NOT_DEFINED = y
- select E if A
- imply A
- imply E if A
-endmenu
-choice
- bool "C"
-config N
- bool "N"
-config O
- bool "O"
-endchoice
-choice B
- bool "B"
-config B1
- tristate "B1"
-config B2
- tristate "B2"
+config MULTI_DEF
+choice CHOICE
endchoice
-if !(NOT_DEFINED != A)
-comment "comment 2"
-endif
+config MENU_HOOK
-menu "visible if menu"
- visible if A
- visible if NOT_DEFINED
+menu "menu"
endmenu
+
+config COMMENT_HOOK
+
+comment "comment"
diff --git a/tests/Kmisc b/tests/Kmisc
index d2e55b2..44fdfdc 100644
--- a/tests/Kmisc
+++ b/tests/Kmisc
@@ -1,6 +1,6 @@
# For testing various minor APIs
-# is_optional()
+# optional choices
choice NOT_OPTIONAL
bool "not optional"
@@ -51,7 +51,10 @@ config HEX
hex "hex"
depends on NOT_DEFINED_2
+config COMMENT_HOOK
comment "comment"
+
+config MENU_HOOK
menu "menu"
depends on NOT_DEFINED_3 || NOT_DEFINED_2
depends on !NOT_DEFINED_4
diff --git a/tests/Kmodifiable b/tests/Kmodifiable
deleted file mode 100644
index 1f9dcc9..0000000
--- a/tests/Kmodifiable
+++ /dev/null
@@ -1,50 +0,0 @@
-config MODULES
- def_bool y
-
-config VISIBLE
- tristate "bool visible"
-
-config NOT_VISIBLE
- tristate
-
-config Y_SELECTOR
- def_tristate y
- select SELECTED_TO_Y
-
-config SELECTED_TO_Y
- tristate "selected to y"
-
-config M_SELECTOR
- def_tristate m
- select TRISTATE_SELECTED_TO_M
- select M_VISIBLE_TRISTATE_SELECTED_TO_M
- select BOOL_SELECTED_TO_M
-
-config TRISTATE_SELECTED_TO_M
- tristate "tristate selected to m"
-
-config M_VISIBLE_TRISTATE_SELECTED_TO_M
- tristate "m-visible tristate selected to m"
- depends on m
-
-# The "m" will get promoted to a "y", so this should still not be modifiable
-config BOOL_SELECTED_TO_M
- bool "bool selected to m"
-
-config VISIBLE_STRING
- string "visible string"
-
-config VISIBLE_INT
- int "visible int"
-
-config VISIBLE_HEX
- hex "visible hex"
-
-config NOT_VISIBLE_STRING
- string
-
-config NOT_VISIBLE_INT
- int "not visible int" if n
-
-config NOT_VISIBLE_HEX
- hex
diff --git a/tests/Kprompt b/tests/Kprompt
deleted file mode 100644
index a36a0fd..0000000
--- a/tests/Kprompt
+++ /dev/null
@@ -1,77 +0,0 @@
-config NO_PROMPT
- tristate
-
-config SINGLE_PROMPT_1
- bool "single prompt 1"
-
-config SINGLE_PROMPT_2
- bool
- prompt "single prompt 2" if n
-
-config MULTI_PROMPT
- bool "ignored prompt"
- prompt "prompt 1"
-config MULTI_PROMPT
- bool "prompt 2"
-config MULTI_PROMPT
- bool
-config MULTI_PROMPT
- bool
- prompt "prompt 3"
-config MULTI_PROMPT
- bool
- prompt "ignored prompt"
- prompt "ignored prompt 2" if y
- prompt "prompt 4" if y
-
-choice NO_PROMPT_CHOICE
- tristate
-config A
- bool
-config B
- bool
-endchoice
-
-choice SINGLE_PROMPT_1_CHOICE
- bool "single prompt 1 choice"
-config C
- bool
-config D
- bool
-endchoice
-
-choice SINGLE_PROMPT_2_CHOICE
- bool "ignored prompt"
- prompt "single prompt 2 choice"
-config E
- bool
-config F
- bool
-endchoice
-
-choice MULTI_PROMPT_CHOICE
- bool "prompt 1 choice"
-config G
- bool
-config H
- bool
-endchoice
-
-choice MULTI_PROMPT_CHOICE
- bool
- prompt "prompt 2 choice"
-config I
- bool
-config J
- bool
-endchoice
-
-choice MULTI_PROMPT_CHOICE
- bool
- prompt "ignored prompt"
- prompt "prompt 3 choice"
-config K
- bool
-config L
- bool
-endchoice
diff --git a/tests/Krelation b/tests/Krelation
index 057b585..940eff8 100644
--- a/tests/Krelation
+++ b/tests/Krelation
@@ -1,7 +1,8 @@
config A
bool
+ depends on UNDEFINED
-choice
+choice CHOICE_1
bool "C"
config B
bool "B"
@@ -17,7 +18,8 @@ config E
menu "m2"
config F
bool
-choice
+choice CHOICE_2
+ tristate "foo"
config G
bool "g"
config H
diff --git a/tests/Krepr b/tests/Krepr
new file mode 100644
index 0000000..5a5b8b8
--- /dev/null
+++ b/tests/Krepr
@@ -0,0 +1,61 @@
+config MODULES
+ bool
+ option modules
+ default y
+
+if UNDEFINED
+endif
+
+config BASIC
+ bool
+ default y
+ help
+
+config VISIBLE
+ bool "visible"
+
+config DIR_DEP_N
+ depends on n
+
+config OPTIONS
+ option allnoconfig_y
+ option defconfig_list
+ option env="ENV"
+
+config MULTI_DEF
+config MULTI_DEF
+
+menuconfig MENUCONFIG
+
+choice CHOICE
+ tristate "choice"
+
+config CHOICE_1
+ tristate "choice sym"
+
+config CHOICE_2
+ tristate "choice sym"
+
+endchoice
+
+config CHOICE_HOOK
+
+choice
+ tristate "optional choice" if n
+ optional
+endchoice
+
+config NO_VISIBLE_IF_HOOK
+
+menu "no visible if"
+endmenu
+
+config VISIBLE_IF_HOOK
+
+menu "visible if"
+ visible if m
+endmenu
+
+config COMMENT_HOOK
+
+comment "comment"
diff --git a/tests/Kstr b/tests/Kstr
new file mode 100644
index 0000000..d792f08
--- /dev/null
+++ b/tests/Kstr
@@ -0,0 +1,78 @@
+if UNDEFINED
+endif
+
+config NO_TYPE
+
+config BASIC_NO_PROMPT
+ bool
+ help
+ blah blah
+
+ blah blah blah
+
+ blah
+
+config BASIC_PROMPT
+ bool "basic"
+
+config ADVANCED
+ tristate "prompt" if DEP
+ default DEFAULT_1
+ default DEFAULT_2 if DEP
+ select SELECTED_1
+ select SELECTED_2 if DEP
+ imply IMPLIED_1
+ imply IMPLIED_2 if DEP
+ help
+ first help text
+
+config ADVANCED
+ prompt "prompt 2"
+
+menuconfig ADVANCED
+ prompt "prompt 3"
+ depends on DEP2
+
+config ADVANCED
+ help
+ second help text
+
+config STRING
+ string
+ default "foo"
+ default "bar" if DEP
+ default STRING2
+ default STRING3 if DEP
+
+config INT
+ int
+ range 1 2
+ range FOO BAR
+ range BAZ QAZ if DEP
+
+config MODULES
+ bool "MODULES"
+ option modules
+
+config OPTIONS
+ option allnoconfig_y
+ option defconfig_list
+ option env="ENV"
+
+choice CHOICE
+ tristate "foo"
+ default CHOICE_1
+ default CHOICE_2 if dep
+
+config CHOICE_1
+ tristate "choice 1"
+
+config CHOICE_2
+ tristate "choice 2"
+
+endchoice
+
+choice
+ tristate "no name"
+ optional
+endchoice
diff --git a/tests/Ktext b/tests/Ktext
deleted file mode 100644
index 3440c3c..0000000
--- a/tests/Ktext
+++ /dev/null
@@ -1,145 +0,0 @@
-config BASIC
- bool
-
-if !BASIC && !BASIC
-
-config ADVANCED
- tristate "advanced prompt 1" if y || (BASIC && BASIC)
- select SELECTED_1 if BASIC && DUMMY
- select SELECTED_2 if !(DUMMY || BASIC)
- imply IMPLIED_1 if BASIC || DUMMY
- imply IMPLIED_2 if !(DUMMY && BASIC)
- default y if BASIC && !BASIC
- default n if BASIC = DUMMY && X < Y && X <= Y && X > Y && X >= Y
-
-config ADVANCED
- tristate "advanced prompt 2"
-
-config STRING
- string
- default "foo"
- default "bar" if BAR
- default STRING2 if BAZ
-
-config STRING2
- string
- default "baz"
-
-endif
-
-config SELECTED_1
-config SELECTED_2
-config SELECTING_1
- select ADVANCED if BASIC
-config SELECTING_2
- select ADVANCED if !BASIC
-config IMPLYING_1
- imply ADVANCED if DUMMY
-config IMPLYING_2
- imply ADVANCED if !DUMMY
-
-config INT
- int
- default 7
-
-config HAS_RANGES
- int "ranged"
- range 1 2 if !DUMMY
- range INT INT if DUMMY
- range 123 456
-
-choice
- bool "choice"
-
-config CHOICE_ITEM_1
- bool "A"
-config CHOICE_ITEM_2
- bool "B"
-config CHOICE_ITEM_3
- bool "C"
-
-endchoice
-
-menu "simple menu"
-endmenu
-
-if !DUMMY
-menu "advanced menu"
- depends on !BASIC
- visible if !DUMMY
-endmenu
-endif
-
-comment "simple comment"
-
-if !DUMMY
- comment "advanced comment"
- depends on !BASIC
-endif
-
-config NO_HELP
- bool
-
-choice NO_HELP_CHOICE
-config FOO
-endchoice
-
-config EMPTY_HELP
- bool
- help
-config DUMMY
-
-choice EMPTY_HELP_CHOICE
- bool
- help
-config DUMMY2
-endchoice
-
-config S
- bool
- help
- help for
- S
-choice C
- bool
- help
- help for
- C
-config A
- bool "A"
-
-config B
- bool "B"
-
-endchoice
-
-comment "a comment"
-
-menu "a menu"
-endmenu
-
-config HELP_TERMINATED_BY_COMMENT
- bool
- help
- a
- b
- c
-#
-
-config TRICKY_HELP
- bool
- help
-
-
- a
- b
- c
-
- d
- e
- f
-
-
- g
- h
- i
diff --git a/tests/Kvisibility b/tests/Kvisibility
index 715d098..91def0a 100644
--- a/tests/Kvisibility
+++ b/tests/Kvisibility
@@ -1,5 +1,6 @@
config MODULES
bool "MODULES"
+ option modules
#
# Symbol visibility
@@ -12,186 +13,204 @@ config NO_PROMPT
config MOD
def_tristate m
-config BOOL_n
+config BOOL_N
bool "bool n" if n
-# Rewritten to m && MODULES
-config BOOL_m
+config BOOL_M
+ # Rewritten to m && MODULES
bool "bool m" if m
-# Not rewritten
config BOOL_MOD
bool "bool MOD"
+ # Not rewritten
depends on MOD
-# Rewritten to m && MODULES
-config BOOL_y
+config BOOL_Y
bool "bool y"
+ # Rewritten to m && MODULES
depends on y || m
-config TRISTATE_n
+config TRISTATE_N
tristate "tristate n" if n
-# Rewritten to m && MODULES
-config TRISTATE_m
+config TRISTATE_M
+ # Rewritten to m && MODULES
tristate "tristate m" if m
-# Not rewritten
config TRISTATE_MOD
tristate "tristate MOD"
+ # Not rewritten
depends on MOD
-# Rewritten to m && MODULES
-config TRISTATE_y
+config TRISTATE_Y
bool "tristate y"
+ # Rewritten to m && MODULES
depends on y || m
# Symbols nested in 'if'
if n
-config BOOL_if_n
+
+config BOOL_IF_N
bool "bool if n"
-config TRISTATE_if_n
+
+config TRISTATE_IF_N
tristate "tristate if n"
+
endif
if m
-config BOOL_if_m
+
+config BOOL_IF_M
bool "bool if m"
-config TRISTATE_if_m
+
+config TRISTATE_IF_M
tristate "tristate if n"
+
endif
if y
-config BOOL_if_y
+
+config BOOL_IF_Y
bool "bool if y"
-config TRISTATE_if_y
+
+config TRISTATE_IF_Y
tristate "tristate if y"
+
endif
# Symbols nested in 'menu'
menu "menu 1"
depends on n
-config BOOL_menu_n
+
+config BOOL_MENU_N
bool "bool menu n"
-config TRISTATE_menu_n
+
+config TRISTATE_MENU_N
tristate "tristate menu n"
+
endmenu
menu "menu 2"
depends on m
-config BOOL_menu_m
+
+config BOOL_MENU_M
bool "bool menu m"
-config TRISTATE_menu_m
+
+config TRISTATE_MENU_M
tristate "tristate menu n"
+
endmenu
menu "menu 3"
depends on y
-config BOOL_menu_y
+
+config BOOL_MENU_Y
bool "bool menu y"
-config TRISTATE_menu_y
+
+config TRISTATE_MENU_Y
tristate "tristate menu y"
+
endmenu
# Symbols nested in choices
choice C1
tristate "choice n" if n
-config BOOL_choice_n
+
+config BOOL_CHOICE_N
bool "bool choice n"
-config TRISTATE_choice_n
+
+config TRISTATE_CHOICE_N
tristate "tristate choice n"
+
endchoice
choice C2
tristate "choice m" if m
-config BOOL_choice_m
+
+config BOOL_CHOICE_M
bool "bool choice m"
-config TRISTATE_choice_m
+
+config TRISTATE_CHOICE_M
tristate "tristate choice n"
+
endchoice
choice C3
tristate "choice y" if y
-config BOOL_choice_y
+
+config BOOL_CHOICE_Y
bool "bool choice y"
-config TRISTATE_choice_y
+
+config TRISTATE_CHOICE_Y
tristate "tristate choice y"
+
endchoice
#
# Choice visibility
#
-choice BOOL_CHOICE_n
+choice BOOL_CHOICE_N
bool "bool choice n" if n
-config A
- bool "A"
-config B
- bool "B"
endchoice
-choice BOOL_CHOICE_m
+choice BOOL_CHOICE_M
bool "bool choice m" if m
-config C
- bool "C"
-config D
- bool "D"
endchoice
-choice BOOL_CHOICE_y
+choice BOOL_CHOICE_Y
bool "bool choice y" if y
-config E
- bool "E"
-config F
- bool "F"
endchoice
-choice TRISTATE_CHOICE_n
+choice TRISTATE_CHOICE_N
tristate "tristate choice n" if n
-config G
- tristate "G"
-config H
- tristate "H"
endchoice
-choice TRISTATE_CHOICE_m
+choice TRISTATE_CHOICE_M
tristate "tristate choice m" if m
-config I
- tristate "I"
-config J
- tristate "J"
endchoice
-choice TRISTATE_CHOICE_y
+choice TRISTATE_CHOICE_Y
tristate "tristate choice y" if y
+
config K
tristate "K"
+
config L
tristate "L"
+
endchoice
if m
-choice TRISTATE_CHOICE_IF_m_and_y
+choice TRISTATE_CHOICE_IF_M_AND_Y
tristate "tristate choice if m and y" if y
+
config M
bool "M"
+
config N
bool "N"
+
endchoice
endif
menu "choice-containing menu"
depends on n && y
-choice TRISTATE_CHOICE_MENU_n_and_y
+
+choice TRISTATE_CHOICE_MENU_N_AND_Y
tristate "tristate choice if n and y"
+
config O
tristate "O"
+
config P
tristate "P"
+
endchoice
+
endmenu
#
@@ -245,19 +264,25 @@ comment "comment y"
if n
comment "comment if n"
endif
+
if m
comment "comment if m"
endif
+
if y
comment "comment if y"
endif
if "y"
-menu "comment-containing menu"
+
+menu "menu with comment"
depends on m
+
comment "double-nested m comment"
depends on y
+
endmenu
+
endif
# Used to verify that string/int/hex symbols with m visibility accept a user
@@ -280,28 +305,38 @@ endif
menu "n-visible menu"
visible if n
-config VISIBLE_IF_n
+
+config VISIBLE_IF_N
tristate "visible if n"
+
endmenu
menu "m-visible menu"
visible if m
-config VISIBLE_IF_m
+
+config VISIBLE_IF_M
tristate "visible if m"
+
endmenu
menu "y-visible menu"
visible if y
-config VISIBLE_IF_y
+
+config VISIBLE_IF_Y
tristate "visible if m"
+
endmenu
menu "m-visible menu 2"
visible if y || n
visible if m && y
visible if y
+
if y
-config VISIBLE_IF_m_2
+
+config VISIBLE_IF_M_2
tristate "visible if m 2"
+
endif
+
endmenu
diff --git a/tests/Kwtf b/tests/Kwtf
new file mode 100644
index 0000000..85ae57e
--- /dev/null
+++ b/tests/Kwtf
@@ -0,0 +1,18 @@
+config A
+ bool
+ select n
+ select m
+ select y
+ select UNAME_RELEASE
+ select "n"
+ select "m"
+ select "y"
+ select "UNAME_RELEASE"
+ imply n
+ imply m
+ imply y
+ imply UNAME_RELEASE
+ imply "n"
+ imply "m"
+ imply "y"
+ imply "UNAME_RELEASE"
diff --git a/tests/reltest b/tests/reltest
new file mode 100755
index 0000000..dea0137
--- /dev/null
+++ b/tests/reltest
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+
+# Runs the test suite and all examples scripts with Python 2 and Python 3,
+# bailing immediately if anything fails. For the examples that aren't tested in
+# the test suite, we just confirm that they at least run.
+#
+# Should be run from the kernel root with $ Kconfiglib/tests/reltest
+
+test_script() {
+ echo -e "\n================= $1 with $py =================\n"
+ if (($# == 1)); then
+ make scriptconfig PYTHONCMD=$py SCRIPT=$1
+ else
+ make scriptconfig PYTHONCMD=$py SCRIPT=$1 SCRIPT_ARG="$2"
+ fi
+
+ if (($?)); then
+ echo "$1 failed to run with $py"
+ exit 1
+ fi
+}
+
+for py in python2 python3; do
+ echo -e "\n================= Test suite with $py =================\n"
+
+ if ! $py Kconfiglib/testsuite.py speedy; then
+ echo "test suite failed for $py"
+ exit 1
+ fi
+
+ # Check that the example scripts that aren't tested by the test suite run
+ # at least
+
+ test_script Kconfiglib/examples/defconfig_oldconfig.py
+ test_script Kconfiglib/examples/defconfig.py
+ test_script Kconfiglib/examples/eval_expr.py MODULES
+ test_script Kconfiglib/examples/find_symbol.py X86
+ test_script Kconfiglib/examples/help_grep.py general
+ test_script Kconfiglib/examples/print_sym_info.py MODULES
+ test_script Kconfiglib/examples/print_tree.py
+
+ $py Kconfiglib/examples/menuconfig.py Kconfiglib/examples/Kmenuconfig <<END
+BOOL
+n
+END
+ if (($?)); then
+ echo "menuconfig.py failed with $py"
+ exit 1
+ fi
+done
+
+echo "everything okay"
diff --git a/testsuite.py b/testsuite.py
index 11822ce..8ba0edc 100644
--- a/testsuite.py
+++ b/testsuite.py
@@ -1,48 +1,45 @@
-# This is a test suite for Kconfiglib. It runs selftests on Kconfigs provided
-# by us and tests compatibility with the C Kconfig implementation by comparing
-# the output of Kconfiglib with the output of the scripts/kconfig/*conf
-# utilities for different targets and defconfigs. It should be run from the
-# top-level kernel directory with
+# This is the Kconfiglib test suite. It runs selftests on Kconfigs provided by
+# us and tests compatibility with the C Kconfig implementation by comparing the
+# output of Kconfiglib with the output of the scripts/kconfig/*conf utilities
+# for different targets and defconfigs. It should be run from the top-level
+# kernel directory with
#
-# $ python Kconfiglib/testsuite.py
+# $ python Kconfiglib/testsuite.py
#
-# Some additional options can be turned on by passing arguments. With no argument,
-# they default to off.
+# Some additional options can be turned on by passing them as arguments. They
+# default to off.
#
# - speedy:
-# Run scripts/kconfig/conf directly when comparing outputs instead of using
-# 'make' targets. Makes things a lot faster, but could break if Kconfig
-# files start depending on additional environment variables besides ARCH and
-# SRCARCH. (These would be set in the Makefiles in that case.) Safe as of
-# Linux 4.1.0-rc8.
+# Run scripts/kconfig/conf directly instead of using 'make' targets. Makes
+# things a lot faster, but could break if Kconfig files start referencing
+# additional environment variables beyond ARCH, SRCARCH, and KERNELVERSION.
+# Safe as of Linux 4.14-rc3.
#
# - obsessive:
-# By default, only valid arch/defconfig pairs will be tested. With this
-# enabled, every arch will be tested with every defconfig, which increases
-# the test time by an order of magnitude. Occasionally finds (usually very
-# obscure) bugs, and I make sure everything passes with it.
+# By default, only valid arch/defconfig pairs are tested. In obsessive mode,
+# every arch will be tested with every defconfig. Increases the testing time
+# by an order of magnitude. Occasionally finds (usually obscure) bugs, and I
+# make sure everything passes with it.
#
# - log:
-# Log timestamped failures of the defconfig test to test_defconfig_fails in
-# the root. Especially handy in obsessive mode.
+# Log timestamped defconfig test failures to the file test_defconfig_fails.
+# Handy in obsessive mode.
#
-# For example, to run in speedy mode with logging, run
+# For example, this commands runs the test suite in speedy mode with logging
+# enabled:
#
-# $ python Kconfiglib/testsuite.py speedy log
+# $ python(3) Kconfiglib/testsuite.py speedy log
#
-# (PyPy also works, and runs the defconfig tests roughly 20% faster on my
-# machine. Some of the other tests get an even greater speed-up.)
-#
-# The tests have been roughly arranged in order of time needed.
+# pypy works too, and runs most tests much faster than CPython.
#
# All tests should pass. Report regressions to ulfalizer a.t Google's email
# service.
-from __future__ import print_function
-
+from kconfiglib import Kconfig, Symbol, Choice, COMMENT, MENU, \
+ BOOL, TRISTATE, HEX, STRING, \
+ KconfigSyntaxError, expr_value
import difflib
import errno
-import kconfiglib
import os
import platform
import re
@@ -51,140 +48,147 @@ import sys
import textwrap
import time
-speedy_mode = False
-obsessive_mode = False
-log_mode = False
+def shell(cmd):
+ with open(os.devnull, "w") as devnull:
+ subprocess.call(cmd, shell=True, stdout=devnull, stderr=devnull)
+
+all_passed = True
+
+def fail(msg=None):
+ global all_passed
+ all_passed = False
+ if msg is not None:
+ print("fail: " + msg)
+
+def verify(cond, msg):
+ if not cond:
+ fail(msg)
+
+def verify_equal(x, y):
+ if x != y:
+ fail("'{}' does not equal '{}'".format(x, y))
# Assign this to avoid warnings from Kconfiglib. Nothing in the kernel's
# Kconfig files seems to actually look at the value as of 3.7.0-rc8. This is
# only relevant for the test suite, as this will get set by the kernel Makefile
# when using (i)scriptconfig.
-os.environ["KERNELVERSION"] = "3.7.0"
+os.environ["KERNELVERSION"] = "1"
# Prevent accidental loading of configuration files by removing
# KCONFIG_ALLCONFIG from the environment
os.environ.pop("KCONFIG_ALLCONFIG", None)
+speedy = False
+obsessive = False
+log = False
+
# Number of arch/defconfig pairs tested so far
nconfigs = 0
def run_tests():
- global speedy_mode, obsessive_mode, log_mode
+ global speedy, obsessive, log
for s in sys.argv[1:]:
if s == "speedy":
- speedy_mode = True
+ speedy = True
print("Speedy mode enabled")
elif s == "obsessive":
- obsessive_mode = True
+ obsessive = True
print("Obsessive mode enabled")
elif s == "log":
- log_mode = True
+ log = True
print("Log mode enabled")
else:
print("Unrecognized option '{}'".format(s))
-
return
run_selftests()
run_compatibility_tests()
def run_selftests():
- """Runs tests on specific configurations provided by us."""
-
#
- # Helper functions
+ # Common helper functions. These all expect 'c' to hold the current
+ # configuration.
#
+ TRI_TO_STR = {0: "n", 1: "m", 2: "y"}
+
def verify_value(sym_name, val):
- """Verifies that a symbol has a particular value."""
- sym = c[sym_name]
- sym_val = sym.get_value()
- verify(sym_val == val,
- "{} should have the value '{}' but has the value '{}'"
- .format(sym_name, val, sym_val))
-
- def assign_and_verify_new_value(sym_name, user_val, new_val):
- """Assigns a user value to the symbol and verifies the new value."""
- sym = c[sym_name]
- sym_old_val = sym.get_value()
- sym.set_user_value(user_val)
- sym_new_val = sym.get_value()
- verify(sym_new_val == new_val,
- "{} should have the new value '{}' after being assigned the "
- "user value '{}'. Instead, the value is '{}'. The old "
- "value was '{}'."
- .format(sym_name, new_val, user_val, sym_new_val, sym_old_val))
+ """
+ Verifies that a symbol has a particular value.
+ """
+
+ if isinstance(val, int):
+ val = TRI_TO_STR[val]
+
+ sym = c.syms[sym_name]
+ verify(sym.str_value == val,
+ 'expected {} to have the value "{}", had the value "{}"'
+ .format(sym_name, val, sym.str_value))
+
+ def assign_and_verify_value(sym_name, val, new_val):
+ """
+ Assigns 'val' to a symbol and verifies that its value becomes
+ 'new_val'. Assumes (and tests) that 'val' is valid for the
+ symbol type.
+ """
+
+ if isinstance(new_val, int):
+ new_val = TRI_TO_STR[new_val]
+
+ sym = c.syms[sym_name]
+ old_val = sym.str_value
+ verify(sym.set_value(val),
+ "assigning '{}' to {} unexpectedly failed"
+ .format(val, sym_name))
+ verify(sym.str_value == new_val,
+ "expected {} to have the value '{}' after being assigned the "
+ "value '{}'. Instead, the value is '{}'. The old value was "
+ "'{}'."
+ .format(sym_name, new_val, val, sym.str_value, old_val))
def assign_and_verify(sym_name, user_val):
- """Like assign_and_verify_new_value(), with the expected value being
- the value just set."""
- assign_and_verify_new_value(sym_name, user_val, user_val)
-
- def assign_and_verify_new_user_value(sym_name, user_val, new_user_val):
- """Assigns a user value to the symbol and verifies the new user
- value."""
- sym = c[sym_name]
- sym_old_user_val = sym.get_user_value()
- sym.set_user_value(user_val)
- sym_new_user_val = sym.get_user_value()
- verify(sym_new_user_val == new_user_val,
- "{} should have the user value '{}' after being assigned "
- "the user value '{}'. Instead, the new user value was '{}'. "
- "The old user value was '{}'."
- .format(sym_name, new_user_val, user_val, sym_new_user_val,
- sym_old_user_val))
-
- print("Running selftests...\n")
-
- print("Testing tristate comparisons...")
-
- def verify_truth_table(comp_fn, *bools):
- bools_list = list(bools)
- for (x, y) in (("n", "n"), ("n", "m"), ("n", "y"),
- ("m", "n"), ("m", "m"), ("m", "y"),
- ("y", "n"), ("y", "m"), ("y", "y")):
- expected = bools_list.pop(0)
- verify(comp_fn(x, y) == expected,
- "Expected {} on ('{}', '{}') to be {}".
- format(comp_fn, x, y, expected))
-
- verify_truth_table(kconfiglib.tri_less,
- False, True, True,
- False, False, True,
- False, False, False)
-
- verify_truth_table(kconfiglib.tri_less_eq,
- True, True, True,
- False, True, True,
- False, False, True)
-
- verify_truth_table(kconfiglib.tri_greater,
- False, False, False,
- True, False, False,
- True, True, False)
-
- verify_truth_table(kconfiglib.tri_greater_eq,
- True, False, False,
- True, True, False,
- True, True, True)
+ """
+ Like assign_and_verify_value(), with the expected value being the
+ value just set.
+ """
+ assign_and_verify_value(sym_name, user_val, user_val)
+
+ def assign_and_verify_user_value(sym_name, val, user_val, valid):
+ """
+ Assigns a user value to the symbol and verifies the new user value. If
+ valid is True, the user value is valid for the type, otherwise not.
+ This is used to test the set_value() return value.
+ """
+ sym = c.syms[sym_name]
+ sym_old_user_val = sym.user_value
+
+ verify(sym.set_value(val) == valid,
+ "expected the user value '{}' to be {} for {}, was not"
+ .format(val, "valid" if valid else "invalid", sym_name))
+ verify(sym.user_value == user_val,
+ "the assigned user value '{}' wasn't reflected in user_value "
+ "on the symbol {}. Instead, the new user_value was '{}'. The "
+ "old user value was '{}'."
+ .format(user_val, sym_name, sym.user_value, sym_old_user_val))
#
- # String literal lexing. (This tests an internal API.)
+ # Selftests
#
- print("Testing string literal (constant symbol) lexing...")
+ print("Testing string literal lexing")
- c = kconfiglib.Config("Kconfiglib/tests/empty")
+ # Dummy empty configuration just to get a Kconfig object
+ c = Kconfig("Kconfiglib/tests/empty")
def verify_string_lex(s, res):
- """Verifies that the string token 'res' is produced from lexing 's'.
- Strips the first and last characters from 's' so we can use readable
- raw strings as input."""
- s = s[1:-1]
- s_res = c._tokenize(s, for_eval = True).get_next()
- verify(s_res == res,
- "'{}' produced the string token '{}'. Expected '{}'."
- .format(s, s_res, res))
+ """
+ Verifies that a constant symbol with the name 'res' is produced from
+ lexing 's'
+ """
+ c.eval_string(s)
+ verify(c._tokens[0].name == res,
+ "expected <{}> to produced the constant symbol <{}>, "
+ 'produced <{}>'.format(s[1:-1], c._tokens[0].name, res))
verify_string_lex(r""" "" """, "")
verify_string_lex(r""" '' """, "")
@@ -215,16 +219,17 @@ def run_selftests():
verify_string_lex(r""" '\a\\"\b\c\'"d' """, "a\\\"bc'\"d")
def verify_string_bad(s):
- """Verifies that tokenizing 's' throws a Kconfig_Syntax_Error. Strips
- the first and last characters from 's' so we can use readable raw
- strings as input."""
- s = s[1:-1]
+ """
+ Verifies that tokenizing 's' throws a KconfigSyntaxError. Strips the
+ first and last characters from 's' so we can use readable raw strings
+ as input.
+ """
try:
- c._tokenize(s, for_eval = True)
- except kconfiglib.Kconfig_Syntax_Error:
+ c.eval_string(s)
+ except KconfigSyntaxError:
pass
else:
- fail("Tokenization of '{}' should have failed.".format(s))
+ fail("expected tokenization of {} to fail, didn't".format(s[1:-1]))
verify_string_bad(r""" " """)
verify_string_bad(r""" ' """)
@@ -235,279 +240,192 @@ def run_selftests():
verify_string_bad(r""" "foo """)
verify_string_bad(r""" 'foo """)
- #
- # is_modifiable()
- #
-
- print("Testing is_modifiable() and range queries...")
-
- c = kconfiglib.Config("Kconfiglib/tests/Kmodifiable")
-
- for sym_name in ("VISIBLE", "TRISTATE_SELECTED_TO_M", "VISIBLE_STRING",
- "VISIBLE_INT", "VISIBLE_HEX"):
- sym = c[sym_name]
- verify(sym.is_modifiable(),
- "{} should be modifiable".format(sym_name))
-
- for sym_name in ("n", "m", "y", "NOT_VISIBLE", "SELECTED_TO_Y",
- "BOOL_SELECTED_TO_M", "M_VISIBLE_TRISTATE_SELECTED_TO_M",
- "NOT_VISIBLE_STRING", "NOT_VISIBLE_INT", "NOT_VISIBLE_HEX"):
- sym = c[sym_name]
- verify(not sym.is_modifiable(),
- "{} should not be modifiable".format(sym_name))
- #
- # get_lower/upper_bound() and get_assignable_values()
- #
-
- c = kconfiglib.Config("Kconfiglib/tests/Kbounds")
-
- def verify_bounds(sym_name, low, high):
- sym = c[sym_name]
- sym_low = sym.get_lower_bound()
- sym_high = sym.get_upper_bound()
- verify(sym_low == low and sym_high == high,
- "Incorrectly calculated bounds for {}: {}-{}. "
- "Expected {}-{}.".format(sym_name, sym_low, sym_high,
- low, high))
- # See that we get back the corresponding range from
- # get_assignable_values()
- if sym_low is None:
- vals = sym.get_assignable_values()
- verify(vals == [],
- "get_assignable_values() thinks there should be assignable "
- "values for {} ({}) but not get_lower/upper_bound()".
- format(sym_name, vals))
- if sym.get_type() in (kconfiglib.BOOL, kconfiglib.TRISTATE):
- verify(not sym.is_modifiable(),
- "get_lower_bound() thinks there should be no "
- "assignable values for the bool/tristate {} but "
- "is_modifiable() thinks it should be modifiable".
- format(sym_name))
- else:
- tri_to_int = { "n" : 0, "m" : 1, "y" : 2 }
- bound_range = ["n", "m", "y"][tri_to_int[sym_low] :
- tri_to_int[sym_high] + 1]
- assignable_range = sym.get_assignable_values()
- verify(bound_range == assignable_range,
- "get_lower/upper_bound() thinks the range for {} should "
- "be {} while get_assignable_values() thinks it should be "
- "{}".format(sym_name, bound_range, assignable_range))
- if sym.get_type() in (kconfiglib.BOOL, kconfiglib.TRISTATE):
- verify(sym.is_modifiable(),
- "get_lower/upper_bound() thinks the range for the "
- "bool/tristate {} should be {} while is_modifiable() "
- "thinks the symbol should not be modifiable".
- format(sym_name, bound_range))
-
- verify_bounds("n", None, None)
- verify_bounds("m", None, None)
- verify_bounds("y", None, None)
- verify_bounds("Y_VISIBLE_BOOL", "n", "y")
- verify_bounds("Y_VISIBLE_TRISTATE", "n", "y")
- verify_bounds("M_VISIBLE_BOOL", "n", "y")
- verify_bounds("M_VISIBLE_TRISTATE", "n", "m")
- verify_bounds("Y_SELECTED_BOOL", None, None)
- verify_bounds("M_SELECTED_BOOL", None, None)
- verify_bounds("Y_SELECTED_TRISTATE", None, None)
- verify_bounds("M_SELECTED_TRISTATE", "m", "y")
- verify_bounds("M_SELECTED_M_VISIBLE_TRISTATE", None, None)
- verify_bounds("N_IMPLIED_BOOL", "n", "y")
- verify_bounds("N_IMPLIED_TRISTATE", "n", "y")
- verify_bounds("M_IMPLIED_BOOL", "n", "y")
- verify_bounds("M_IMPLIED_TRISTATE", "n", "y")
- verify_bounds("Y_IMPLIED_BOOL", "n", "y")
- verify_bounds("Y_IMPLIED_TRISTATE", "n", "y")
- verify_bounds("STRING", None, None)
- verify_bounds("INT", None, None)
- verify_bounds("HEX", None, None)
-
- #
- # eval()
- #
+ print("Testing expression evaluation")
- print("Testing eval()...")
-
- c = kconfiglib.Config("Kconfiglib/tests/Keval")
+ c = Kconfig("Kconfiglib/tests/Keval")
def verify_eval(expr, val):
- res = c.eval(expr)
+ res = c.eval_string(expr)
verify(res == val,
"'{}' evaluated to {}, expected {}".format(expr, res, val))
- def verify_eval_bad(expr):
- try:
- c.eval(expr)
- except kconfiglib.Kconfig_Syntax_Error:
- pass
- else:
- fail('eval("{}") should throw Kconfig_Syntax_Error'
- .format(expr))
-
# No modules
- verify_eval("n", "n")
- verify_eval("m", "n")
- verify_eval("y", "y")
- verify_eval("'n'", "n")
- verify_eval("'m'", "n")
- verify_eval("'y'", "y")
- verify_eval("M", "y")
+ verify_eval("n", 0)
+ verify_eval("m", 0)
+ verify_eval("y", 2)
+ verify_eval("'n'", 0)
+ verify_eval("'m'", 0)
+ verify_eval("'y'", 2)
+ verify_eval("M", 2)
+
# Modules
- c["MODULES"].set_user_value("y")
- verify_eval("n", "n")
- verify_eval("m", "m")
- verify_eval("y", "y")
- verify_eval("'n'", "n")
- verify_eval("'m'", "m")
- verify_eval("'y'", "y")
- verify_eval("M", "m")
- verify_eval("(Y || N) && (m && y)", "m")
-
- # Non-bool/non-tristate symbols are always "n" in a tristate sense
- verify_eval("Y_STRING", "n")
- verify_eval("Y_STRING || m", "m")
-
- # As are all constants besides "y" and "m"
- verify_eval('"foo"', "n")
- verify_eval('"foo" || "bar"', "n")
+ c.modules.set_value(2)
+ verify_eval("n", 0)
+ verify_eval("m", 1)
+ verify_eval("y", 2)
+ verify_eval("'n'", 0)
+ verify_eval("'m'", 1)
+ verify_eval("'y'", 2)
+ verify_eval("M", 1)
+ verify_eval("(Y || N) && (m && y)", 1)
+
+ # Non-bool/non-tristate symbols are always n in a tristate sense
+ verify_eval("Y_STRING", 0)
+ verify_eval("Y_STRING || m", 1)
+
+ # As are all constants besides y and m
+ verify_eval('"foo"', 0)
+ verify_eval('"foo" || "bar"', 0)
+ verify_eval('"foo" || m', 1)
# Test equality for symbols
- verify_eval("N = N", "y")
- verify_eval("N = n", "y")
- verify_eval("N = 'n'", "y")
- verify_eval("N != N", "n")
- verify_eval("N != n", "n")
- verify_eval("N != 'n'", "n")
-
- verify_eval("M = M", "y")
- verify_eval("M = m", "y")
- verify_eval("M = 'm'", "y")
- verify_eval("M != M", "n")
- verify_eval("M != m", "n")
- verify_eval("M != 'm'", "n")
-
- verify_eval("Y = Y", "y")
- verify_eval("Y = y", "y")
- verify_eval("Y = 'y'", "y")
- verify_eval("Y != Y", "n")
- verify_eval("Y != y", "n")
- verify_eval("Y != 'y'", "n")
-
- verify_eval("N != M", "y")
- verify_eval("N != Y", "y")
- verify_eval("M != Y", "y")
-
- # string/int/hex
- verify_eval("Y_STRING = y", "y")
- verify_eval("Y_STRING = 'y'", "y")
- verify_eval('FOO_BAR_STRING = "foo bar"', "y")
- verify_eval('FOO_BAR_STRING != "foo bar baz"', "y")
- verify_eval('INT_37 = 37', "y")
- verify_eval("INT_37 = '37'", "y")
- verify_eval('HEX_0X37 = 0x37', "y")
- verify_eval("HEX_0X37 = '0x37'", "y")
+ verify_eval("N = N", 2)
+ verify_eval("N = n", 2)
+ verify_eval("N = 'n'", 2)
+ verify_eval("N != N", 0)
+ verify_eval("N != n", 0)
+ verify_eval("N != 'n'", 0)
+
+ verify_eval("M = M", 2)
+ verify_eval("M = m", 2)
+ verify_eval("M = 'm'", 2)
+ verify_eval("M != M", 0)
+ verify_eval("M != m", 0)
+ verify_eval("M != 'm'", 0)
+
+ verify_eval("Y = Y", 2)
+ verify_eval("Y = y", 2)
+ verify_eval("Y = 'y'", 2)
+ verify_eval("Y != Y", 0)
+ verify_eval("Y != y", 0)
+ verify_eval("Y != 'y'", 0)
+
+ verify_eval("N != M", 2)
+ verify_eval("N != Y", 2)
+ verify_eval("M != Y", 2)
+
+ verify_eval("Y_STRING = y", 2)
+ verify_eval("Y_STRING = 'y'", 2)
+ verify_eval('FOO_BAR_STRING = "foo bar"', 2)
+ verify_eval('FOO_BAR_STRING != "foo bar baz"', 2)
+ verify_eval('INT_37 = 37', 2)
+ verify_eval("INT_37 = '37'", 2)
+ verify_eval('HEX_0X37 = 0x37', 2)
+ verify_eval("HEX_0X37 = '0x37'", 2)
# These should also hold after 31847b67 (kconfig: allow use of relations
# other than (in)equality)
- verify_eval("HEX_0X37 = '0x037'", "y")
- verify_eval("HEX_0X37 = '0x0037'", "y")
+ verify_eval("HEX_0X37 = '0x037'", 2)
+ verify_eval("HEX_0X37 = '0x0037'", 2)
+
+ # Constant symbol comparisons
+ verify_eval('"foo" != "bar"', 2)
+ verify_eval('"foo" = "bar"', 0)
+ verify_eval('"foo" = "foo"', 2)
- # Compare some constants...
- verify_eval('"foo" != "bar"', "y")
- verify_eval('"foo" = "bar"', "n")
- verify_eval('"foo" = "foo"', "y")
# Undefined symbols get their name as their value
- c.set_print_warnings(False)
- verify_eval("'not_defined' = not_defined", "y")
- verify_eval("not_defined_2 = not_defined_2", "y")
- verify_eval("not_defined_1 != not_defined_2", "y")
+ c.disable_warnings()
+ verify_eval("'not_defined' = not_defined", 2)
+ verify_eval("not_defined_2 = not_defined_2", 2)
+ verify_eval("not_defined_1 != not_defined_2", 2)
# Test less than/greater than
# Basic evaluation
- verify_eval("INT_37 < 38", "y")
- verify_eval("38 < INT_37", "n")
- verify_eval("INT_37 < '38'", "y")
- verify_eval("'38' < INT_37", "n")
- verify_eval("INT_37 < 138", "y")
- verify_eval("138 < INT_37", "n")
- verify_eval("INT_37 < '138'", "y")
- verify_eval("'138' < INT_37", "n")
- verify_eval("INT_37 < -138", "n")
- verify_eval("-138 < INT_37", "y")
- verify_eval("INT_37 < '-138'", "n")
- verify_eval("'-138' < INT_37", "y")
- verify_eval("INT_37 < 37", "n")
- verify_eval("37 < INT_37", "n")
- verify_eval("INT_37 < 36", "n")
- verify_eval("36 < INT_37", "y")
+ verify_eval("INT_37 < 38", 2)
+ verify_eval("38 < INT_37", 0)
+ verify_eval("INT_37 < '38'", 2)
+ verify_eval("'38' < INT_37", 0)
+ verify_eval("INT_37 < 138", 2)
+ verify_eval("138 < INT_37", 0)
+ verify_eval("INT_37 < '138'", 2)
+ verify_eval("'138' < INT_37", 0)
+ verify_eval("INT_37 < -138", 0)
+ verify_eval("-138 < INT_37", 2)
+ verify_eval("INT_37 < '-138'", 0)
+ verify_eval("'-138' < INT_37", 2)
+ verify_eval("INT_37 < 37", 0)
+ verify_eval("37 < INT_37", 0)
+ verify_eval("INT_37 < 36", 0)
+ verify_eval("36 < INT_37", 2)
# Different formats in comparison
- verify_eval("INT_37 < 0x26", "y") # 38
- verify_eval("INT_37 < 0x25", "n") # 37
- verify_eval("INT_37 < 0x24", "n") # 36
- verify_eval("HEX_0X37 < 56", "y") # 0x38
- verify_eval("HEX_0X37 < 55", "n") # 0x37
- verify_eval("HEX_0X37 < 54", "n") # 0x36
+ verify_eval("INT_37 < 0x26", 2) # 38
+ verify_eval("INT_37 < 0x25", 0) # 37
+ verify_eval("INT_37 < 0x24", 0) # 36
+ verify_eval("HEX_0X37 < 56", 2) # 0x38
+ verify_eval("HEX_0X37 < 55", 0) # 0x37
+ verify_eval("HEX_0X37 < 54", 0) # 0x36
# Other int comparisons
- verify_eval("INT_37 <= 38", "y")
- verify_eval("INT_37 <= 37", "y")
- verify_eval("INT_37 <= 36", "n")
- verify_eval("INT_37 > 38", "n")
- verify_eval("INT_37 > 37", "n")
- verify_eval("INT_37 > 36", "y")
- verify_eval("INT_37 >= 38", "n")
- verify_eval("INT_37 >= 37", "y")
- verify_eval("INT_37 >= 36", "y")
+ verify_eval("INT_37 <= 38", 2)
+ verify_eval("INT_37 <= 37", 2)
+ verify_eval("INT_37 <= 36", 0)
+ verify_eval("INT_37 > 38", 0)
+ verify_eval("INT_37 > 37", 0)
+ verify_eval("INT_37 > 36", 2)
+ verify_eval("INT_37 >= 38", 0)
+ verify_eval("INT_37 >= 37", 2)
+ verify_eval("INT_37 >= 36", 2)
# Other hex comparisons
- verify_eval("HEX_0X37 <= 0x38", "y")
- verify_eval("HEX_0X37 <= 0x37", "y")
- verify_eval("HEX_0X37 <= 0x36", "n")
- verify_eval("HEX_0X37 > 0x38", "n")
- verify_eval("HEX_0X37 > 0x37", "n")
- verify_eval("HEX_0X37 > 0x36", "y")
- verify_eval("HEX_0X37 >= 0x38", "n")
- verify_eval("HEX_0X37 >= 0x37", "y")
- verify_eval("HEX_0X37 >= 0x36", "y")
+ verify_eval("HEX_0X37 <= 0x38", 2)
+ verify_eval("HEX_0X37 <= 0x37", 2)
+ verify_eval("HEX_0X37 <= 0x36", 0)
+ verify_eval("HEX_0X37 > 0x38", 0)
+ verify_eval("HEX_0X37 > 0x37", 0)
+ verify_eval("HEX_0X37 > 0x36", 2)
+ verify_eval("HEX_0X37 >= 0x38", 0)
+ verify_eval("HEX_0X37 >= 0x37", 2)
+ verify_eval("HEX_0X37 >= 0x36", 2)
# A hex holding a value without a "0x" prefix should still be treated as
# hexadecimal
- verify_eval("HEX_37 < 0x38", "y")
- verify_eval("HEX_37 < 0x37", "n")
- verify_eval("HEX_37 < 0x36", "n")
+ verify_eval("HEX_37 < 0x38", 2)
+ verify_eval("HEX_37 < 0x37", 0)
+ verify_eval("HEX_37 < 0x36", 0)
# Symbol comparisons
- verify_eval("INT_37 < HEX_0X37", "y")
- verify_eval("INT_37 > HEX_0X37", "n")
- verify_eval("HEX_0X37 < INT_37 ", "n")
- verify_eval("HEX_0X37 > INT_37 ", "y")
- verify_eval("INT_37 < INT_37 ", "n")
- verify_eval("INT_37 <= INT_37 ", "y")
- verify_eval("INT_37 > INT_37 ", "n")
- verify_eval("INT_37 <= INT_37 ", "y")
+ verify_eval("INT_37 < HEX_0X37", 2)
+ verify_eval("INT_37 > HEX_0X37", 0)
+ verify_eval("HEX_0X37 < INT_37 ", 0)
+ verify_eval("HEX_0X37 > INT_37 ", 2)
+ verify_eval("INT_37 < INT_37 ", 0)
+ verify_eval("INT_37 <= INT_37 ", 2)
+ verify_eval("INT_37 > INT_37 ", 0)
+ verify_eval("INT_37 <= INT_37 ", 2)
# Strings compare lexicographically
- verify_eval("'aa' < 'ab'", "y")
- verify_eval("'aa' > 'ab'", "n")
- verify_eval("'ab' < 'aa'", "n")
- verify_eval("'ab' > 'aa'", "y")
-
- # If one operand is numeric and the other not a valid number, we get 'n'
- verify_eval("INT_37 < oops ", "n")
- verify_eval("INT_37 <= oops ", "n")
- verify_eval("INT_37 > oops ", "n")
- verify_eval("INT_37 >= oops ", "n")
- verify_eval("oops < INT_37", "n")
- verify_eval("oops <= INT_37", "n")
- verify_eval("oops > INT_37", "n")
- verify_eval("oops >= INT_37", "n")
+ verify_eval("'aa' < 'ab'", 2)
+ verify_eval("'aa' > 'ab'", 0)
+ verify_eval("'ab' < 'aa'", 0)
+ verify_eval("'ab' > 'aa'", 2)
+
+ # Comparisons where one of the operands doesn't parse as a number also give
+ # a lexicographic comparison
+ verify_eval("INT_37 < '37a' ", 2)
+ verify_eval("'37a' > INT_37", 2)
+ verify_eval("INT_37 <= '37a' ", 2)
+ verify_eval("'37a' >= INT_37", 2)
+ verify_eval("INT_37 >= '37a' ", 0)
+ verify_eval("INT_37 > '37a' ", 0)
+ verify_eval("'37a' < INT_37", 0)
+ verify_eval("'37a' <= INT_37", 0)
+
+ def verify_eval_bad(expr):
+ try:
+ c.eval_string(expr)
+ except KconfigSyntaxError:
+ pass
+ else:
+ fail('expected eval_string("{}") to throw KconfigSyntaxError, '
+ "didn't".format(expr))
# The C implementation's parser can be pretty lax about syntax. Kconfiglib
# sometimes needs to emulate that. Verify that some bad stuff throws
- # Kconfig_Syntax_Error at least.
+ # KconfigSyntaxError at least.
verify_eval_bad("")
verify_eval_bad("&")
verify_eval_bad("|")
@@ -521,722 +439,593 @@ def run_selftests():
verify_eval_bad("X ||")
verify_eval_bad("|| X")
- #
- # Text queries
- #
- print("Testing text queries...")
+ print("Testing Symbol.__str__()")
+
+ def verify_str(item, s):
+ verify_equal(str(item), s[1:])
+
+ c = Kconfig("Kconfiglib/tests/Kstr", warn=False)
+
+ c.modules.set_value(2)
+
+ verify_str(c.syms["UNDEFINED"], """
+""")
+
+ verify_str(c.syms["BASIC_NO_PROMPT"], """
+config BASIC_NO_PROMPT
+ bool
+ help
+ blah blah
+
+ blah blah blah
+
+ blah
+""")
+
+ verify_str(c.syms["BASIC_PROMPT"], """
+config BASIC_PROMPT
+ bool
+ prompt "basic"
+""")
+
+ verify_str(c.syms["ADVANCED"], """
+config ADVANCED
+ tristate
+ prompt "prompt" if DEP
+ default DEFAULT_1
+ default DEFAULT_2 if DEP
+ select SELECTED_1
+ select SELECTED_2 if DEP
+ imply IMPLIED_1
+ imply IMPLIED_2 if DEP
+ help
+ first help text
+
+config ADVANCED
+ prompt "prompt 2"
+
+menuconfig ADVANCED
+ prompt "prompt 3" if DEP2
+
+config ADVANCED
+ help
+ second help text
+""")
+
+ verify_str(c.syms["STRING"], """
+config STRING
+ string
+ default "foo"
+ default "bar" if DEP
+ default STRING2
+ default STRING3 if DEP
+""")
+
+ verify_str(c.syms["INT"], """
+config INT
+ int
+ range 1 2
+ range FOO BAR
+ range BAZ QAZ if DEP
+""")
+
+ verify_str(c.modules, """
+config MODULES
+ bool
+ prompt "MODULES"
+ option modules
+""")
+
+ verify_str(c.syms["OPTIONS"], """
+config OPTIONS
+ option allnoconfig_y
+ option defconfig_list
+ option env="ENV"
+""")
- def verify_print(o, s):
- verify_equals(str(o), textwrap.dedent(s[1:]))
+ print("Testing Choice.__str__()")
+
+ verify_str(c.named_choices["CHOICE"], """
+choice CHOICE
+ tristate
+ prompt "foo"
+ default CHOICE_1
+ default CHOICE_2 if dep
+""")
- for var in ("ARCH", "SRCARCH", "srctree"):
- os.environ.pop(var, None)
+ verify_str(c.named_choices["CHOICE"].nodes[0].next.item, """
+choice
+ tristate
+ prompt "no name"
+ optional
+""")
+
+
+ print("Testing Symbol.__repr__()")
+
+ def verify_repr(item, s):
+ verify_equal(repr(item) + "\n", s[1:])
+
+ c = Kconfig("Kconfiglib/tests/Krepr", warn=False)
+
+ verify_repr(c.n, """
+<symbol n, tristate, value n, constant>
+""")
+
+ verify_repr(c.m, """
+<symbol m, tristate, value m, constant>
+""")
+
+ verify_repr(c.y, """
+<symbol y, tristate, value y, constant>
+""")
+
+ verify_repr(c.syms["UNDEFINED"], """
+<symbol UNDEFINED, unknown, value "UNDEFINED", visibility n, direct deps n, undefined>
+""")
+
+ verify_repr(c.syms["BASIC"], """
+<symbol BASIC, bool, value y, visibility n, direct deps y, Kconfiglib/tests/Krepr:9>
+""")
- # The tests below aren't meant to imply that the format is set in stone.
- # It's just to verify that the strings do not change unexpectedly.
+ verify_repr(c.syms["VISIBLE"], """
+<symbol VISIBLE, bool, "visible", value n, visibility y, direct deps y, Kconfiglib/tests/Krepr:14>
+""")
- # Printing of Config
+ c.syms["VISIBLE"].set_value(2)
- c = kconfiglib.Config("Kconfiglib/tests/Ktext")
+ verify_repr(c.syms["VISIBLE"], """
+<symbol VISIBLE, bool, "visible", value y, user value y, visibility y, direct deps y, Kconfiglib/tests/Krepr:14>
+""")
- verify_print(c, """
- Configuration
- File : Kconfiglib/tests/Ktext
- Base directory : .
- Value of $ARCH at creation time : (not set)
- Value of $SRCARCH at creation time : (not set)
- Value of $srctree at creation time : (not set)
- Most recently loaded .config : (no .config loaded)
- Print warnings : True
- Print assignments to undefined symbols : False""")
+ verify_repr(c.syms["DIR_DEP_N"], """
+<symbol DIR_DEP_N, unknown, value "DIR_DEP_N", visibility n, direct deps n, Kconfiglib/tests/Krepr:17>
+""")
- os.environ["ARCH"] = "foo"
- os.environ["SRCARCH"] = "bar"
- os.environ["srctree"] = "baz"
+ verify_repr(c.syms["OPTIONS"], """
+<symbol OPTIONS, unknown, value "OPTIONS", visibility n, allnoconfig_y, is the defconfig_list symbol, from environment variable ENV, direct deps y, Kconfiglib/tests/Krepr:20>
+""")
- c = kconfiglib.Config("Kconfiglib/tests/Ktext", base_dir="foobar")
- c.load_config("Kconfiglib/tests/empty")
- c.set_print_warnings(False)
- c.set_print_undef_assign(True)
-
- choice_print, choice_no_help, choice_empty_help, choice_help = \
- c.get_choices()
-
- verify_print(c, """
- Configuration
- File : Kconfiglib/tests/Ktext
- Base directory : foobar
- Value of $ARCH at creation time : foo
- Value of $SRCARCH at creation time : bar
- Value of $srctree at creation time : baz
- Most recently loaded .config : Kconfiglib/tests/empty
- Print warnings : False
- Print assignments to undefined symbols : True""")
-
- # Printing of Symbol
-
- verify_print(c["BASIC"], """
- Symbol BASIC
- Type : bool
- Value : "n"
- User value : (no user value)
- Visibility : "n"
- Is choice item : False
- Is defined : True
- Is from env. : False
- Is special : False
- Prompts:
- (no prompts)
- Default values:
- (no default values)
- Selects:
- (no selects)
- Implies:
- (no implies)
- Reverse (select-related) dependencies:
- (no reverse dependencies)
- Weak reverse (imply-related) dependencies:
- (no weak reverse dependencies)
- Additional dependencies from enclosing menus and ifs:
- (no additional dependencies)
- Locations: Kconfiglib/tests/Ktext:1""")
-
- c["ADVANCED"].set_user_value("m")
-
- verify_print(c["ADVANCED"], """
- Symbol ADVANCED
- Type : tristate
- Value : "y"
- User value : "m"
- Visibility : "y"
- Is choice item : False
- Is defined : True
- Is from env. : False
- Is special : False
- Prompts:
- "advanced prompt 1" if y || BASIC && BASIC (value: "y")
- "advanced prompt 2"
- Default values:
- y (value: "y")
- Condition: BASIC && !BASIC (value: "n")
- n (value: "n")
- Condition: BASIC = DUMMY && X < Y && X <= Y && X > Y && X >= Y (value: "n")
- Selects:
- SELECTED_1 if BASIC && DUMMY (value: "n")
- SELECTED_2 if !(DUMMY || BASIC) (value: "y")
- Implies:
- IMPLIED_1 if BASIC || DUMMY (value: "n")
- IMPLIED_2 if !(DUMMY && BASIC) (value: "y")
- Reverse (select-related) dependencies:
- SELECTING_1 && BASIC || SELECTING_2 && !BASIC (value: "n")
- Weak reverse (imply-related) dependencies:
- IMPLYING_1 && DUMMY || IMPLYING_2 && !DUMMY (value: "n")
- Additional dependencies from enclosing menus and ifs:
- !BASIC && !BASIC (value: "y")
- Locations: Kconfiglib/tests/Ktext:6 Kconfiglib/tests/Ktext:15""")
-
- verify_print(c["STRING"], """
- Symbol STRING
- Type : string
- Value : "foo"
- User value : (no user value)
- Visibility : "n"
- Is choice item : False
- Is defined : True
- Is from env. : False
- Is special : False
- Prompts:
- (no prompts)
- Default values:
- "foo"
- Condition: (none)
- "bar"
- Condition: BAR (value: "n")
- STRING2 (value: "baz")
- Condition: BAZ (value: "n")
- Selects:
- (no selects)
- Implies:
- (no implies)
- Reverse (select-related) dependencies:
- (no reverse dependencies)
- Weak reverse (imply-related) dependencies:
- (no weak reverse dependencies)
- Additional dependencies from enclosing menus and ifs:
- !BASIC && !BASIC (value: "y")
- Locations: Kconfiglib/tests/Ktext:18""")
-
- verify_print(c["HAS_RANGES"], """
- Symbol HAS_RANGES
- Type : int
- Value : "1"
- User value : (no user value)
- Visibility : "y"
- Is choice item : False
- Is defined : True
- Is from env. : False
- Is special : False
- Ranges:
- [1, 2] if !DUMMY (value: "y")
- [INT, INT] if DUMMY (value: "n")
- [123, 456]
- Prompts:
- "ranged"
- Default values:
- (no default values)
- Selects:
- (no selects)
- Implies:
- (no implies)
- Reverse (select-related) dependencies:
- (no reverse dependencies)
- Weak reverse (imply-related) dependencies:
- (no weak reverse dependencies)
- Additional dependencies from enclosing menus and ifs:
- (no additional dependencies)
- Locations: Kconfiglib/tests/Ktext:45""")
-
- # Printing of Choice
-
- verify_print(choice_print, """
- Choice
- Name (for named choices): (no name)
- Type : bool
- Selected symbol : CHOICE_ITEM_1
- User value : (no user value)
- Mode : "y"
- Visibility : "y"
- Optional : False
- Prompts:
- "choice"
- Defaults:
- (no default values)
- Choice symbols:
- CHOICE_ITEM_1 CHOICE_ITEM_2 CHOICE_ITEM_3
- Additional dependencies from enclosing menus and ifs:
- (no additional dependencies)
- Locations: Kconfiglib/tests/Ktext:51""")
-
- c["CHOICE_ITEM_2"].set_user_value("y")
-
- verify_print(choice_print, """
- Choice
- Name (for named choices): (no name)
- Type : bool
- Selected symbol : CHOICE_ITEM_2
- User value : CHOICE_ITEM_2
- Mode : "y"
- Visibility : "y"
- Optional : False
- Prompts:
- "choice"
- Defaults:
- (no default values)
- Choice symbols:
- CHOICE_ITEM_1 CHOICE_ITEM_2 CHOICE_ITEM_3
- Additional dependencies from enclosing menus and ifs:
- (no additional dependencies)
- Locations: Kconfiglib/tests/Ktext:51""")
-
- # Printing of Menu
-
- verify_print(c.get_menus()[0], """
- Menu
- Title : simple menu
- 'depends on' dependencies : (no dependencies)
- 'visible if' dependencies : (no dependencies)
- Additional dependencies from enclosing menus and ifs:
- (no additional dependencies)
- Location: Kconfiglib/tests/Ktext:63""")
-
- verify_print(c.get_menus()[1], """
- Menu
- Title : advanced menu
- 'depends on' dependencies : !BASIC (value: "y")
- 'visible if' dependencies : !DUMMY (value: "y")
- Additional dependencies from enclosing menus and ifs:
- !DUMMY (value: "y")
- Location: Kconfiglib/tests/Ktext:67""")
-
- # Printing of Comment
-
- verify_print(c.get_comments()[0], """
- Comment
- Text: simple comment
- Dependencies: (no dependencies)
- Additional dependencies from enclosing menus and ifs:
- (no additional dependencies)
- Location: Kconfiglib/tests/Ktext:73""")
-
- verify_print(c.get_comments()[1], """
- Comment
- Text: advanced comment
- Dependencies: !BASIC (value: "y")
- Additional dependencies from enclosing menus and ifs:
- !DUMMY (value: "y")
- Location: Kconfiglib/tests/Ktext:76""")
-
- verify_equals(c["NO_HELP"].get_help(), None)
- verify_equals(choice_no_help.get_help(), None)
- verify_equals(c["EMPTY_HELP"].get_help(), "")
- verify_equals(choice_empty_help.get_help(), "")
- verify_equals(c["HELP_TERMINATED_BY_COMMENT"].get_help(), "a\nb\nc\n")
- verify_equals(c["TRICKY_HELP"].get_help(),
- "a\n b\n c\n\n d\n e\n f\n\n\ng\n h\n i\n")
- verify_equals(c["S"].get_help(), "help for\nS\n")
- verify_equals(choice_help.get_help(), "help for\nC\n")
-
- verify_equals(c["S"].get_name(), "S")
- verify_equals(c.get_comments()[2].get_text(), "a comment")
- verify_equals(c.get_menus()[2].get_title(), "a menu")
+ verify_repr(c.syms["MULTI_DEF"], """
+<symbol MULTI_DEF, unknown, value "MULTI_DEF", visibility n, direct deps y, Kconfiglib/tests/Krepr:25, Kconfiglib/tests/Krepr:26>
+""")
- #
- # Prompt queries
- #
+ verify_repr(c.syms["CHOICE_1"], """
+<symbol CHOICE_1, tristate, "choice sym", value n, visibility m, choice symbol, direct deps m, Kconfiglib/tests/Krepr:33>
+""")
- print("Testing prompt queries...")
+ verify_repr(c.modules, """
+<symbol MODULES, bool, value y, visibility n, is the modules symbol, direct deps y, Kconfiglib/tests/Krepr:1>
+""")
- def verify_prompts(sym_or_choice, prompts):
- sym_or_choice_prompts = sym_or_choice.get_prompts()
- verify(len(sym_or_choice_prompts) == len(prompts),
- "Wrong number of prompts for " + sym_or_choice.get_name())
- for i in range(0, len(sym_or_choice_prompts)):
- verify(sym_or_choice_prompts[i] == prompts[i],
- "Prompt {} wrong for {}: Was '{}', should be '{}'".
- format(i, sym_or_choice.get_name(), sym_or_choice_prompts[i],
- prompts[i]))
- def verify_sym_prompts(sym_name, *prompts):
- verify_prompts(c[sym_name], prompts)
+ print("Testing Choice.__repr__()")
- def verify_choice_prompts(choice, *prompts):
- verify_prompts(choice, prompts)
+ verify_repr(c.named_choices["CHOICE"], """
+<choice CHOICE, tristate, "choice", mode m, visibility y, Kconfiglib/tests/Krepr:30>
+""")
- c = kconfiglib.Config("Kconfiglib/tests/Kprompt")
+ c.named_choices["CHOICE"].set_value(2)
- # Symbols
- verify_sym_prompts("NO_PROMPT")
- verify_sym_prompts("SINGLE_PROMPT_1", "single prompt 1")
- verify_sym_prompts("SINGLE_PROMPT_2", "single prompt 2")
- verify_sym_prompts("MULTI_PROMPT", "prompt 1", "prompt 2", "prompt 3", "prompt 4")
+ verify_repr(c.named_choices["CHOICE"], """
+<choice CHOICE, tristate, "choice", mode y, user mode y, CHOICE_1 selected, visibility y, Kconfiglib/tests/Krepr:30>
+""")
- no_prompt_choice, single_prompt_1_choice, single_prompt_2_choice, multi_prompt_choice = \
- c.get_choices()
+ c.syms["CHOICE_2"].set_value(2)
- # Choices
- verify_choice_prompts(no_prompt_choice)
- verify_choice_prompts(single_prompt_1_choice, "single prompt 1 choice")
- verify_choice_prompts(single_prompt_2_choice, "single prompt 2 choice")
- verify_choice_prompts(multi_prompt_choice,
- "prompt 1 choice", "prompt 2 choice", "prompt 3 choice")
+ verify_repr(c.named_choices["CHOICE"], """
+<choice CHOICE, tristate, "choice", mode y, user mode y, CHOICE_2 selected, CHOICE_2 selected by user, visibility y, Kconfiglib/tests/Krepr:30>
+""")
- #
- # Location queries
- #
+ c.named_choices["CHOICE"].set_value(1)
+
+ verify_repr(c.named_choices["CHOICE"], """
+<choice CHOICE, tristate, "choice", mode m, user mode m, CHOICE_2 selected by user (overridden), visibility y, Kconfiglib/tests/Krepr:30>
+""")
+
+ verify_repr(c.syms["CHOICE_HOOK"].nodes[0].next.item, """
+<choice, tristate, "optional choice", mode n, visibility n, optional, Kconfiglib/tests/Krepr:43>
+""")
+
+
+ print("Testing MenuNode.__repr__()")
+
+ verify_repr(c.syms["BASIC"].nodes[0], """
+<menu node for symbol BASIC, deps y, has help, has next, Kconfiglib/tests/Krepr:9>
+""")
+
+ verify_repr(c.syms["DIR_DEP_N"].nodes[0], """
+<menu node for symbol DIR_DEP_N, deps n, has next, Kconfiglib/tests/Krepr:17>
+""")
+
+ verify_repr(c.syms["MULTI_DEF"].nodes[0], """
+<menu node for symbol MULTI_DEF, deps y, has next, Kconfiglib/tests/Krepr:25>
+""")
+
+ verify_repr(c.syms["MULTI_DEF"].nodes[1], """
+<menu node for symbol MULTI_DEF, deps y, has next, Kconfiglib/tests/Krepr:26>
+""")
+
+ verify_repr(c.syms["MENUCONFIG"].nodes[0], """
+<menu node for symbol MENUCONFIG, is menuconfig, deps y, has next, Kconfiglib/tests/Krepr:28>
+""")
- print("Testing location queries...")
+ verify_repr(c.named_choices["CHOICE"].nodes[0], """
+<menu node for choice CHOICE, prompt "choice" (visibility y), deps y, has child, has next, Kconfiglib/tests/Krepr:30>
+""")
- def verify_def_locations(sym_name, *locs):
- sym_locs = c[sym_name].get_def_locations()
- verify(len(sym_locs) == len(locs),
- "Wrong number of def. locations for " + sym_name)
- for i in range(0, len(sym_locs)):
- verify(sym_locs[i] == locs[i],
- "Wrong def. location for {}: Was {}, should be {}".
- format(sym_name, sym_locs[i], locs[i]))
+ verify_repr(c.syms["CHOICE_HOOK"].nodes[0].next, """
+<menu node for choice, prompt "optional choice" (visibility n), deps y, has next, Kconfiglib/tests/Krepr:43>
+""")
+
+ verify_repr(c.syms["NO_VISIBLE_IF_HOOK"].nodes[0].next, """
+<menu node for menu, prompt "no visible if" (visibility y), deps y, 'visible if' deps y, has next, Kconfiglib/tests/Krepr:50>
+""")
+
+ verify_repr(c.syms["VISIBLE_IF_HOOK"].nodes[0].next, """
+<menu node for menu, prompt "visible if" (visibility y), deps y, 'visible if' deps m, has next, Kconfiglib/tests/Krepr:55>
+""")
+
+ verify_repr(c.syms["COMMENT_HOOK"].nodes[0].next, """
+<menu node for comment, prompt "comment" (visibility y), deps y, Kconfiglib/tests/Krepr:61>
+""")
+
+
+ print("Testing Kconfig.__repr__()")
+
+ verify_repr(c, """
+<configuration with 15 symbols, main menu prompt "Linux Kernel Configuration", srctree not set, config symbol prefix "CONFIG_", warnings disabled, undef. symbol assignment warnings disabled>
+""")
+
+ os.environ["srctree"] = "srctree value"
+ os.environ["CONFIG_"] = "CONFIG_ value"
+
+ c = Kconfig("Kconfiglib/tests/Krepr", warn=False)
+ c.enable_warnings()
+ c.enable_undef_warnings()
+
+ verify_repr(c, """
+<configuration with 15 symbols, main menu prompt "Linux Kernel Configuration", srctree "srctree value", config symbol prefix "CONFIG_ value", warnings enabled, undef. symbol assignment warnings enabled>
+""")
+
+ os.environ.pop("srctree", None)
+ os.environ.pop("CONFIG_", None)
+
+
+ print("Testing tricky help strings")
+
+ c = Kconfig("Kconfiglib/tests/Khelp")
+
+ def verify_help(node, s):
+ verify_equal(node.help, s[1:])
+
+ verify_help(c.syms["TWO_HELP_STRINGS"].nodes[0], """
+first help string
+""")
+
+ verify_help(c.syms["TWO_HELP_STRINGS"].nodes[1], """
+second help string
+""")
+
+ verify_help(c.syms["NO_BLANK_AFTER_HELP"].nodes[0], """
+help for
+NO_BLANK_AFTER_HELP
+""")
+
+ verify_help(c.named_choices["CHOICE_HELP"].nodes[0], """
+help for
+CHOICE_HELP
+""")
+
+ verify_help(c.syms["HELP_TERMINATED_BY_COMMENT"].nodes[0], """
+a
+b
+c
+""")
+
+ verify_help(c.syms["TRICKY_HELP"].nodes[0], """
+a
+ b
+ c
+
+ d
+ e
+ f
+
+
+g
+ h
+ i
+""")
+
+
+ print("Testing locations and 'source'")
+
+ def verify_locations(nodes, *expected_locs):
+ verify(len(nodes) == len(expected_locs),
+ "Wrong number of locations for " + repr(nodes))
+
+ for node, expected_loc in zip(nodes, expected_locs):
+ node_loc = "{}:{}".format(node.filename, node.linenr)
+ verify(node_loc == expected_loc,
+ "expected {} to have the location {}, had the location {}"
+ .format(repr(node), expected_loc, node_loc))
# Expanded in the 'source' statement in Klocation
- os.environ["FOO"] = "tests"
-
- c = kconfiglib.Config("Kconfiglib/tests/Klocation", base_dir="Kconfiglib/")
-
- verify_def_locations("n")
- verify_def_locations("m")
- verify_def_locations("y")
-
- verify_def_locations("A",
- ("Kconfiglib/tests/Klocation", 4),
- ("Kconfiglib/tests/Klocation", 28),
- ("Kconfiglib/tests/Klocation_included", 1),
- ("Kconfiglib/tests/Klocation_included", 3))
- verify_def_locations("C",
- ("Kconfiglib/tests/Klocation", 18))
- verify_def_locations("M",
- ("Kconfiglib/tests/Klocation_included", 6))
- verify_def_locations("N",
- ("Kconfiglib/tests/Klocation_included", 19))
- verify_def_locations("O",
- ("Kconfiglib/tests/Klocation_included", 21))
- verify_def_locations("NOT_DEFINED") # No locations
-
- def verify_ref_locations(sym_name, *locs):
- sym_locs = c[sym_name].get_ref_locations()
- verify(len(sym_locs) == len(locs),
- "Wrong number of ref. locations for " + sym_name)
- for i in range(0, len(sym_locs)):
- verify(sym_locs[i] == locs[i],
- "Wrong ref. location for {}: Was {}, should be {}".
- format(sym_name, sym_locs[i], locs[i]))
-
- # Reload without the slash at the end of 'base_dir' to get coverage for
- # that as well
- c = kconfiglib.Config("Kconfiglib/tests/Klocation", base_dir="Kconfiglib")
-
- verify_ref_locations("A",
- ("Kconfiglib/tests/Klocation", 10),
- ("Kconfiglib/tests/Klocation", 12),
- ("Kconfiglib/tests/Klocation", 16),
- ("Kconfiglib/tests/Klocation", 34),
- ("Kconfiglib/tests/Klocation", 35),
- ("Kconfiglib/tests/Klocation_included", 7),
- ("Kconfiglib/tests/Klocation_included", 8),
- ("Kconfiglib/tests/Klocation_included", 9),
- ("Kconfiglib/tests/Klocation_included", 12),
- ("Kconfiglib/tests/Klocation_included", 13),
- ("Kconfiglib/tests/Klocation_included", 14),
- ("Kconfiglib/tests/Klocation_included", 15),
- ("Kconfiglib/tests/Klocation_included", 35),
- ("Kconfiglib/tests/Klocation_included", 40),
- ("Kconfiglib/tests/Klocation", 65),
- ("Kconfiglib/tests/Klocation", 66),
- ("Kconfiglib/tests/Klocation", 67),
- ("Kconfiglib/tests/Klocation", 68),
- ("Kconfiglib/tests/Klocation", 69),
- ("Kconfiglib/tests/Klocation", 70),
- ("Kconfiglib/tests/Klocation", 71),
- ("Kconfiglib/tests/Klocation", 72),
- ("Kconfiglib/tests/Klocation", 73))
- verify_ref_locations("C")
- verify_ref_locations("NOT_DEFINED",
- ("Kconfiglib/tests/Klocation", 12),
- ("Kconfiglib/tests/Klocation", 29),
- ("Kconfiglib/tests/Klocation_included", 12),
- ("Kconfiglib/tests/Klocation_included", 35),
- ("Kconfiglib/tests/Klocation_included", 41))
-
- # Location queries for choices
-
- def verify_choice_locations(choice, *locs):
- choice_locs = choice.get_def_locations()
- verify(len(choice_locs) == len(locs),
- "Wrong number of def. locations for choice")
- for i in range(0, len(choice_locs)):
- verify(choice_locs[i] == locs[i],
- "Wrong def. location for choice: Was {}, should be {}".
- format(choice_locs[i], locs[i]))
-
- choice_1, choice_2 = c.get_choices()
-
- # Throw in named choice test
- verify(choice_1.get_name() == "B",
- "The first choice should be called B")
- verify(choice_2.get_name() is None,
- "The second choice should have no name")
-
- verify_choice_locations(choice_1,
- ("Kconfiglib/tests/Klocation", 15),
- ("Kconfiglib/tests/Klocation_included", 24))
- verify_choice_locations(choice_2,
- ("Kconfiglib/tests/Klocation_included", 17))
-
- # Location queries for menus and comments
-
- def verify_location(menu_or_comment, loc):
- menu_or_comment_loc = menu_or_comment.get_location()
- verify(menu_or_comment_loc == loc,
- "Wrong location for {} with text '{}': Was {}, should be "
- "{}".format("menu" if menu_or_comment.is_menu() else "comment",
- menu_or_comment.get_title() if
- menu_or_comment.is_menu() else
- menu_or_comment.get_text(),
- menu_or_comment_loc,
- loc))
-
- menu_1, menu_2 = c.get_menus()[:-1]
- comment_1, comment_2 = c.get_comments()
-
- verify_location(menu_1, ("Kconfiglib/tests/Klocation", 9))
- verify_location(menu_2, ("Kconfiglib/tests/Klocation_included", 5))
- verify_location(comment_1, ("Kconfiglib/tests/Klocation", 31))
- verify_location(comment_2, ("Kconfiglib/tests/Klocation_included", 36))
+ os.environ["EXPANDED_FROM_ENV"] = "tests"
+ os.environ["srctree"] = "Kconfiglib/"
- #
- # Visibility queries
- #
+ c = Kconfig("tests/Klocation")
+
+ os.environ.pop("EXPANDED_FROM_ENV", None)
+ os.environ.pop("srctree", None)
+
+ verify_locations(c.syms["SINGLE_DEF"].nodes, "tests/Klocation:4")
+
+ verify_locations(c.syms["MULTI_DEF"].nodes,
+ "tests/Klocation:6",
+ "tests/Klocation:16",
+ "tests/Klocation_included:3",
+ "tests/Klocation:32")
- print("Testing visibility queries...")
+ verify_locations(c.named_choices["CHOICE"].nodes,
+ "tests/Klocation_included:5")
- c = kconfiglib.Config("Kconfiglib/tests/Kvisibility")
+ verify_locations([c.syms["MENU_HOOK"].nodes[0].next],
+ "tests/Klocation_included:10")
- def verify_sym_visibility(sym_name, no_module_vis, module_vis):
- sym = c[sym_name]
+ verify_locations([c.syms["COMMENT_HOOK"].nodes[0].next],
+ "tests/Klocation_included:15")
- c["MODULES"].set_user_value("n")
- sym_vis = sym.get_visibility()
- verify(sym_vis == no_module_vis,
- "{} should have visibility '{}' without modules, had "
- "visibility '{}'".
- format(sym_name, no_module_vis, sym_vis))
- c["MODULES"].set_user_value("y")
- sym_vis = sym.get_visibility()
- verify(sym_vis == module_vis,
- "{} should have visibility '{}' with modules, had "
- "visibility '{}'".
- format(sym_name, module_vis, sym_vis))
+ print("Testing visibility")
+
+ c = Kconfig("Kconfiglib/tests/Kvisibility")
+
+ def verify_visibility(item, no_module_vis, module_vis):
+ c.modules.set_value(0)
+ verify(item.visibility == no_module_vis,
+ "expected {} to have visibility {} without modules, had "
+ "visibility {}".
+ format(repr(item), no_module_vis, item.visibility))
+
+ c.modules.set_value(2)
+ verify(item.visibility == module_vis,
+ "expected {} to have visibility {} with modules, had "
+ "visibility {}".
+ format(repr(item), module_vis, item.visibility))
# Symbol visibility
- verify_sym_visibility("NO_PROMPT", "n", "n")
- verify_sym_visibility("BOOL_n", "n", "n")
- verify_sym_visibility("BOOL_m", "n", "y") # Promoted
- verify_sym_visibility("BOOL_MOD", "y", "y") # Promoted
- verify_sym_visibility("BOOL_y", "y", "y")
- verify_sym_visibility("TRISTATE_m", "n", "m")
- verify_sym_visibility("TRISTATE_MOD", "y", "m") # Promoted
- verify_sym_visibility("TRISTATE_y", "y", "y")
- verify_sym_visibility("BOOL_if_n", "n", "n")
- verify_sym_visibility("BOOL_if_m", "n", "y") # Promoted
- verify_sym_visibility("BOOL_if_y", "y", "y")
- verify_sym_visibility("BOOL_menu_n", "n", "n")
- verify_sym_visibility("BOOL_menu_m", "n", "y") # Promoted
- verify_sym_visibility("BOOL_menu_y", "y", "y")
- verify_sym_visibility("BOOL_choice_n", "n", "n")
+ verify_visibility(c.syms["NO_PROMPT"], 0, 0)
+ verify_visibility(c.syms["BOOL_N"], 0, 0)
+ verify_visibility(c.syms["BOOL_M"], 0, 2)
+ verify_visibility(c.syms["BOOL_MOD"], 2, 2)
+ verify_visibility(c.syms["BOOL_Y"], 2, 2)
+ verify_visibility(c.syms["TRISTATE_M"], 0, 1)
+ verify_visibility(c.syms["TRISTATE_MOD"], 2, 1)
+ verify_visibility(c.syms["TRISTATE_Y"], 2, 2)
+ verify_visibility(c.syms["BOOL_IF_N"], 0, 0)
+ verify_visibility(c.syms["BOOL_IF_M"], 0, 2)
+ verify_visibility(c.syms["BOOL_IF_Y"], 2, 2)
+ verify_visibility(c.syms["BOOL_MENU_N"], 0, 0)
+ verify_visibility(c.syms["BOOL_MENU_M"], 0, 2)
+ verify_visibility(c.syms["BOOL_MENU_Y"], 2, 2)
+ verify_visibility(c.syms["BOOL_CHOICE_N"], 0, 0)
# Non-tristate symbols in tristate choices are only visible if the choice
- # is in "y" mode
- verify_sym_visibility("BOOL_choice_m", "n", "n")
- verify_sym_visibility("BOOL_choice_y", "y", "n")
- c["TRISTATE_choice_m"].set_user_value("y")
- c["TRISTATE_choice_y"].set_user_value("y")
- # Still limited by the visibility of the choice
- verify_sym_visibility("BOOL_choice_m", "n", "n")
- # This one should become visible now though
- verify_sym_visibility("BOOL_choice_y", "y", "y")
-
- verify_sym_visibility("TRISTATE_if_n", "n", "n")
- verify_sym_visibility("TRISTATE_if_m", "n", "m")
- verify_sym_visibility("TRISTATE_if_y", "y", "y")
- verify_sym_visibility("TRISTATE_menu_n", "n", "n")
- verify_sym_visibility("TRISTATE_menu_m", "n", "m")
- verify_sym_visibility("TRISTATE_menu_y", "y", "y")
- verify_sym_visibility("TRISTATE_choice_n", "n", "n")
- verify_sym_visibility("TRISTATE_choice_m", "n", "m")
- verify_sym_visibility("TRISTATE_choice_y", "y", "y")
-
- # Choice visibility
-
- def verify_choice_visibility(choice, no_module_vis, module_vis):
- c["MODULES"].set_user_value("n")
- choice_vis = choice.get_visibility()
- verify(choice_vis == no_module_vis,
- "choice {} should have visibility '{}' without modules, "
- "has visibility '{}'".
- format(choice.get_name(), no_module_vis, choice_vis))
-
- c["MODULES"].set_user_value("y")
- choice_vis = choice.get_visibility()
- verify(choice_vis == module_vis,
- "choice {} should have visibility '{}' with modules, "
- "has visibility '{}'".
- format(choice.get_name(), module_vis, choice_vis))
-
- choice_bool_n, choice_bool_m, choice_bool_y, choice_tristate_n, \
- choice_tristate_m, choice_tristate_y, choice_tristate_if_m_and_y, \
- choice_tristate_menu_n_and_y \
- = c.get_choices()[3:]
-
- verify_choice_visibility(choice_bool_n, "n", "n")
- verify_choice_visibility(choice_bool_m, "n", "y") # Promoted
- verify_choice_visibility(choice_bool_y, "y", "y")
- verify_choice_visibility(choice_tristate_n, "n", "n")
- verify_choice_visibility(choice_tristate_m, "n", "m")
- verify_choice_visibility(choice_tristate_y, "y", "y")
-
- verify_choice_visibility(choice_tristate_if_m_and_y, "n", "m")
- verify_choice_visibility(choice_tristate_menu_n_and_y, "n", "n")
-
- # Menu visibility
-
- def verify_menu_visibility(menu, no_module_vis, module_vis):
- c["MODULES"].set_user_value("n")
- menu_vis = menu.get_visibility()
- verify(menu_vis == no_module_vis,
- "menu \"{}\" should have visibility '{}' without modules, "
- "has visibility '{}'".
- format(menu.get_title(), no_module_vis, menu_vis))
-
- c["MODULES"].set_user_value("y")
- menu_vis = menu.get_visibility()
- verify(menu_vis == module_vis,
- "menu \"{}\" should have visibility '{}' with modules, "
- "has visibility '{}'".
- format(menu.get_title(), module_vis, menu_vis))
-
- menu_n, menu_m, menu_y, menu_if_n, menu_if_m, menu_if_y, \
- menu_if_m_and_y = c.get_menus()[4:-5]
- verify(menu_n.get_title() == "menu n", "Ops - testing the wrong menus")
-
- verify_menu_visibility(menu_n, "n", "n")
- verify_menu_visibility(menu_m, "n", "m")
- verify_menu_visibility(menu_y, "y", "y")
- verify_menu_visibility(menu_if_n, "n", "n")
- verify_menu_visibility(menu_if_m, "n", "m")
- verify_menu_visibility(menu_if_y, "y", "y")
- verify_menu_visibility(menu_if_m_and_y, "n", "m")
-
- # Menu 'visible if' visibility
-
- menu_visible_if_n, menu_visible_if_m, menu_visible_if_y, \
- menu_visible_if_m_2 = c.get_menus()[12:]
-
- def verify_visible_if_visibility(menu, no_module_vis, module_vis):
- c["MODULES"].set_user_value("n")
- menu_vis = menu.get_visible_if_visibility()
- verify(menu_vis == no_module_vis,
- "menu \"{}\" should have 'visible if' visibility '{}' "
- "without modules, has 'visible if' visibility '{}'".
- format(menu.get_title(), no_module_vis, menu_vis))
-
- c["MODULES"].set_user_value("y")
- menu_vis = menu.get_visible_if_visibility()
- verify(menu_vis == module_vis,
- "menu \"{}\" should have 'visible if' visibility '{}' "
- "with modules, has 'visible if' visibility '{}'".
- format(menu.get_title(), module_vis, menu_vis))
-
- # Ordinary visibility should not affect 'visible if' visibility
- verify_visible_if_visibility(menu_n, "y", "y")
- verify_visible_if_visibility(menu_if_n, "y", "y")
- verify_visible_if_visibility(menu_m, "y", "y")
- verify_visible_if_visibility(menu_if_m, "y", "y")
-
- verify_visible_if_visibility(menu_visible_if_n, "n", "n")
- verify_visible_if_visibility(menu_visible_if_m, "n", "m")
- verify_visible_if_visibility(menu_visible_if_y, "y", "y")
- verify_visible_if_visibility(menu_visible_if_m_2, "n", "m")
-
- # Verify that 'visible if' visibility gets propagated to contained symbols
- verify_sym_visibility("VISIBLE_IF_n", "n", "n")
- verify_sym_visibility("VISIBLE_IF_m", "n", "m")
- verify_sym_visibility("VISIBLE_IF_y", "y", "y")
- verify_sym_visibility("VISIBLE_IF_m_2", "n", "m")
-
- # Comment visibility
-
- def verify_comment_visibility(comment, no_module_vis, module_vis):
- c["MODULES"].set_user_value("n")
- comment_vis = comment.get_visibility()
- verify(comment_vis == no_module_vis,
- "comment \"{}\" should have visibility '{}' without "
- "modules, has visibility '{}'".
- format(comment.get_text(), no_module_vis, comment_vis))
-
- c["MODULES"].set_user_value("y")
- comment_vis = comment.get_visibility()
- verify(comment_vis == module_vis,
- "comment \"{}\" should have visibility '{}' with "
- "modules, has visibility '{}'".
- format(comment.get_text(), module_vis, comment_vis))
-
- comment_n, comment_m, comment_y, comment_if_n, comment_if_m, \
- comment_if_y, comment_m_nested = c.get_comments()
-
- verify_comment_visibility(comment_n, "n", "n")
- verify_comment_visibility(comment_m, "n", "m")
- verify_comment_visibility(comment_y, "y", "y")
- verify_comment_visibility(comment_if_n, "n", "n")
- verify_comment_visibility(comment_if_m, "n", "m")
- verify_comment_visibility(comment_if_y, "y", "y")
- verify_comment_visibility(comment_m_nested, "n", "m")
+ # is in y mode
+
+ # The choice can't be brought to y mode because of the 'if m'
+ verify_visibility(c.syms["BOOL_CHOICE_M"], 0, 0)
+ c.syms["BOOL_CHOICE_M"].choice.set_value(2)
+ verify_visibility(c.syms["BOOL_CHOICE_M"], 0, 0)
+
+ # The choice gets y mode only when running without modules, because it
+ # defaults to m mode
+ verify_visibility(c.syms["BOOL_CHOICE_Y"], 2, 0)
+ c.syms["BOOL_CHOICE_Y"].choice.set_value(2)
+ # When set to y mode, the choice symbol becomes visible both with and
+ # without modules
+ verify_visibility(c.syms["BOOL_CHOICE_Y"], 2, 2)
+
+ verify_visibility(c.syms["TRISTATE_IF_N"], 0, 0)
+ verify_visibility(c.syms["TRISTATE_IF_M"], 0, 1)
+ verify_visibility(c.syms["TRISTATE_IF_Y"], 2, 2)
+ verify_visibility(c.syms["TRISTATE_MENU_N"], 0, 0)
+ verify_visibility(c.syms["TRISTATE_MENU_M"], 0, 1)
+ verify_visibility(c.syms["TRISTATE_MENU_Y"], 2, 2)
+ verify_visibility(c.syms["TRISTATE_CHOICE_N"], 0, 0)
+ verify_visibility(c.syms["TRISTATE_CHOICE_M"], 0, 1)
+ verify_visibility(c.syms["TRISTATE_CHOICE_Y"], 2, 2)
+
+ verify_visibility(c.named_choices["BOOL_CHOICE_N"], 0, 0)
+ verify_visibility(c.named_choices["BOOL_CHOICE_M"], 0, 2)
+ verify_visibility(c.named_choices["BOOL_CHOICE_Y"], 2, 2)
+ verify_visibility(c.named_choices["TRISTATE_CHOICE_N"], 0, 0)
+ verify_visibility(c.named_choices["TRISTATE_CHOICE_M"], 0, 1)
+ verify_visibility(c.named_choices["TRISTATE_CHOICE_Y"], 2, 2)
+
+ verify_visibility(c.named_choices["TRISTATE_CHOICE_IF_M_AND_Y"], 0, 1)
+ verify_visibility(c.named_choices["TRISTATE_CHOICE_MENU_N_AND_Y"], 0, 0)
+
+ # Verify that 'visible if' visibility gets propagated to prompts
+
+ verify_visibility(c.syms["VISIBLE_IF_N"], 0, 0)
+ verify_visibility(c.syms["VISIBLE_IF_M"], 0, 1)
+ verify_visibility(c.syms["VISIBLE_IF_Y"], 2, 2)
+ verify_visibility(c.syms["VISIBLE_IF_M_2"], 0, 1)
# Verify that string/int/hex symbols with m visibility accept a user value
- assign_and_verify_new_value("STRING_m", "foo bar", "foo bar")
- assign_and_verify_new_value("INT_m", "123", "123")
- assign_and_verify_new_value("HEX_m", "0x123", "0x123")
-
- #
- # Object relations
- #
-
- c = kconfiglib.Config("Kconfiglib/tests/Krelation")
-
- A, B, C, D, E, F, G, H, I = c["A"], c["B"], c["C"], c["D"], c["E"], c["F"],\
- c["G"], c["H"], c["I"]
- choice_1, choice_2 = c.get_choices()
- verify([menu.get_title() for menu in c.get_menus()] ==
- ["m1", "m2", "m3", "m4"],
- "menu ordering is broken")
- menu_1, menu_2, menu_3, menu_4 = c.get_menus()
-
- print("Testing object relations...")
-
- verify(A.get_parent() is None, "A should not have a parent")
- verify(B.get_parent() is choice_1, "B's parent should be the first choice")
- verify(C.get_parent() is choice_1, "C's parent should be the first choice")
- verify(E.get_parent() is menu_1, "E's parent should be the first menu")
- verify(E.get_parent().get_parent() is None,
- "E's grandparent should be None")
- verify(G.get_parent() is choice_2,
+ assign_and_verify("STRING_m", "foo bar")
+ assign_and_verify("INT_m", "123")
+ assign_and_verify("HEX_m", "0x123")
+
+
+ print("Testing .assignable")
+
+ c = Kconfig("Kconfiglib/tests/Kassignable")
+
+ def verify_assignable_imp(item, assignable_no_modules, assignable_modules):
+ """
+ Verifies the assignable values for 'item', with and without modules.
+ """
+ for modules_val, assignable in (0, assignable_no_modules), \
+ (2, assignable_modules):
+
+ c.modules.set_value(modules_val)
+ module_msg = "without modules" if modules_val == 0 else \
+ "with modules"
+
+ verify(item.assignable == assignable,
+ "Incorrect assignable values for {} {}. Should be {}, "
+ "was {}."
+ .format(item.name, module_msg, assignable, item.assignable))
+
+ # Verify that the values can actually be assigned too
+
+ for val in item.assignable:
+ item.set_value(val)
+ verify(item.tri_value == val,
+ "Unable to set {} to {} {}, even though it was in "
+ ".assignable".format(item.name, val, module_msg))
+
+ def verify_assignable(sym_name, assignable_no_modules, assignable_modules):
+ verify_assignable_imp(c.syms[sym_name],
+ assignable_no_modules,
+ assignable_modules)
+
+ def verify_const_unassignable(sym_name):
+ verify_assignable_imp(c.const_syms[sym_name], (), ())
+
+ # Things that shouldn't be .assignable
+ verify_const_unassignable("n")
+ verify_const_unassignable("m")
+ verify_const_unassignable("y")
+ verify_const_unassignable("const")
+ verify_const_unassignable("UNAME_RELEASE")
+ verify_assignable("UNDEFINED", (), ())
+ verify_assignable("NO_PROMPT", (), ())
+ verify_assignable("STRING", (), ())
+ verify_assignable("INT", (), ())
+ verify_assignable("HEX", (), ())
+
+ # Non-selected symbols
+ verify_assignable("Y_VIS_BOOL", (0, 2), (0, 2))
+ verify_assignable("M_VIS_BOOL", ( ), (0, 2)) # Vis. promoted
+ verify_assignable("N_VIS_BOOL", ( ), ( ))
+ verify_assignable("Y_VIS_TRI", (0, 2), (0, 1, 2))
+ verify_assignable("M_VIS_TRI", ( ), (0, 1 ))
+ verify_assignable("N_VIS_TRI", ( ), ( ))
+
+ # Symbols selected to y
+ verify_assignable("Y_SEL_Y_VIS_BOOL", (2,), (2,))
+ verify_assignable("Y_SEL_M_VIS_BOOL", ( ), (2,)) # Vis. promoted
+ verify_assignable("Y_SEL_N_VIS_BOOL", ( ), ( ))
+ verify_assignable("Y_SEL_Y_VIS_TRI", (2,), (2,))
+ verify_assignable("Y_SEL_M_VIS_TRI", ( ), (2,))
+ verify_assignable("Y_SEL_N_VIS_TRI", ( ), ( ))
+
+ # Symbols selected to m
+ verify_assignable("M_SEL_Y_VIS_BOOL", (2,), ( 2,)) # Value promoted
+ verify_assignable("M_SEL_M_VIS_BOOL", ( ), ( 2,)) # Vis./value promoted
+ verify_assignable("M_SEL_N_VIS_BOOL", ( ), ( ))
+ verify_assignable("M_SEL_Y_VIS_TRI", (2,), (1, 2 ))
+ verify_assignable("M_SEL_M_VIS_TRI", ( ), (1, ))
+ verify_assignable("M_SEL_N_VIS_TRI", ( ), ( ))
+
+ # Symbols implied to y
+ verify_assignable("Y_IMP_Y_VIS_BOOL", (0, 2), (0, 2))
+ verify_assignable("Y_IMP_M_VIS_BOOL", ( ), (0, 2)) # Vis. promoted
+ verify_assignable("Y_IMP_N_VIS_BOOL", ( ), ( ))
+ verify_assignable("Y_IMP_Y_VIS_TRI", (0, 2), (0, 2)) # m removed by imply
+ verify_assignable("Y_IMP_M_VIS_TRI", ( ), (0, 2)) # m promoted to y by imply
+ verify_assignable("Y_IMP_N_VIS_TRI", ( ), ( ))
+
+ # Symbols implied to m (never affects assignable values)
+ verify_assignable("M_IMP_Y_VIS_BOOL", (0, 2), (0, 2))
+ verify_assignable("M_IMP_M_VIS_BOOL", ( ), (0, 2)) # Vis. promoted
+ verify_assignable("M_IMP_N_VIS_BOOL", ( ), ( ))
+ verify_assignable("M_IMP_Y_VIS_TRI", (0, 2), (0, 1, 2))
+ verify_assignable("M_IMP_M_VIS_TRI", ( ), (0, 1 ))
+ verify_assignable("M_IMP_N_VIS_TRI", ( ), ( ))
+
+ # Symbols in y-mode choice
+ verify_assignable("Y_CHOICE_BOOL", (2,), (2,))
+ verify_assignable("Y_CHOICE_TRISTATE", (2,), (2,))
+ verify_assignable("Y_CHOICE_N_VIS_TRISTATE", ( ), ( ))
+
+ # Symbols in m/y-mode choice, starting out in m mode, or y mode when
+ # running without modules
+ verify_assignable("MY_CHOICE_BOOL", (2,), ( ))
+ verify_assignable("MY_CHOICE_TRISTATE", (2,), (0, 1))
+ verify_assignable("MY_CHOICE_N_VIS_TRISTATE", ( ), ( ))
+
+ c.named_choices["MY_CHOICE"].set_value(2)
+
+ # Symbols in m/y-mode choice, now in y mode
+ verify_assignable("MY_CHOICE_BOOL", (2,), (2,))
+ verify_assignable("MY_CHOICE_TRISTATE", (2,), (2,))
+ verify_assignable("MY_CHOICE_N_VIS_TRISTATE", ( ), ( ))
+
+ def verify_choice_assignable(choice_name, assignable_no_modules,
+ assignable_modules):
+ verify_assignable_imp(c.named_choices[choice_name],
+ assignable_no_modules,
+ assignable_modules)
+
+ # Choices with various possible modes
+ verify_choice_assignable("Y_CHOICE", (2, ), ( 2,))
+ verify_choice_assignable("MY_CHOICE", (2, ), ( 1, 2 ))
+ verify_choice_assignable("NMY_CHOICE", (0, 2), (0, 1, 2 ))
+ verify_choice_assignable("NY_CHOICE", (0, 2), (0, 2 ))
+ verify_choice_assignable("NM_CHOICE", ( ), (0, 1 ))
+ verify_choice_assignable("M_CHOICE", ( ), ( 1, ))
+ verify_choice_assignable("N_CHOICE", ( ), ( ))
+
+
+ print("Testing object relations")
+
+ c = Kconfig("Kconfiglib/tests/Krelation")
+
+ verify(c.syms["A"].nodes[0].parent is c.top_node,
+ "A's parent should be the top node")
+
+ verify(c.syms["B"].nodes[0].parent.item is c.named_choices["CHOICE_1"],
+ "B's parent should be the first choice")
+
+ verify(c.syms["C"].nodes[0].parent.item is c.syms["B"],
+ "C's parent should be B (due to auto menus)")
+
+ verify(c.syms["E"].nodes[0].parent.item == MENU,
+ "E's parent should be a menu")
+
+ verify(c.syms["E"].nodes[0].parent.parent is c.top_node,
+ "E's grandparent should be the top node")
+
+ verify(c.syms["G"].nodes[0].parent.item is c.named_choices["CHOICE_2"],
"G's parent should be the second choice")
- verify(G.get_parent().get_parent() is menu_2,
- "G's grandparent should be the second menu")
- #
- # Object fetching (same test file)
- #
+ verify(c.syms["G"].nodes[0].parent.parent.item == MENU,
+ "G's grandparent should be a menu")
- print("Testing object fetching...")
-
- verify_equals(c.get_symbol("NON_EXISTENT"), None)
- verify(c.get_symbol("A") is A, "get_symbol() is broken")
-
- verify(c.get_top_level_items() == [A, choice_1, menu_1, menu_3, menu_4],
- "Wrong items at top level")
- verify(c.get_symbols(False) == [A, B, C, D, E, F, G, H, I],
- "get_symbols() is broken")
-
- verify(choice_1.get_items() == [B, C, D],
- "Wrong get_items() items in 'choice'")
- # Test Kconfig quirk
- verify(choice_1.get_symbols() == [B, D],
- "Wrong get_symbols() symbols in 'choice'")
-
- verify(menu_1.get_items() == [E, menu_2, I], "Wrong items in first menu")
- verify(menu_1.get_symbols() == [E, I], "Wrong symbols in first menu")
- verify(menu_1.get_items(True) == [E, menu_2, F, choice_2, G, H, I],
- "Wrong recursive items in first menu")
- verify(menu_1.get_symbols(True) == [E, F, G, H, I],
- "Wrong recursive symbols in first menu")
- verify(menu_2.get_items() == [F, choice_2],
- "Wrong items in second menu")
- verify(menu_2.get_symbols() == [F],
- "Wrong symbols in second menu")
- verify(menu_2.get_items(True) == [F, choice_2, G, H],
- "Wrong recursive items in second menu")
- verify(menu_2.get_symbols(True) == [F, G, H],
- "Wrong recursive symbols in second menu")
- #
- # hex/int ranges
- #
+ print("Testing hex/int ranges")
- print("Testing hex/int ranges...")
+ c = Kconfig("Kconfiglib/tests/Krange")
- c = kconfiglib.Config("Kconfiglib/tests/Krange")
-
- for sym_name in ("HEX_NO_RANGE", "INT_NO_RANGE", "HEX_40", "INT_40"):
- sym = c[sym_name]
- verify(not sym.has_ranges(),
+ for sym_name in "HEX_NO_RANGE", "INT_NO_RANGE", "HEX_40", "INT_40":
+ sym = c.syms[sym_name]
+ verify(not sym.ranges,
"{} should not have ranges".format(sym_name))
- for sym_name in ("HEX_ALL_RANGES_DISABLED", "INT_ALL_RANGES_DISABLED",
- "HEX_RANGE_10_20_LOW_DEFAULT",
- "INT_RANGE_10_20_LOW_DEFAULT"):
- sym = c[sym_name]
- verify(sym.has_ranges(), "{} should have ranges".format(sym_name))
+ for sym_name in "HEX_ALL_RANGES_DISABLED", "INT_ALL_RANGES_DISABLED", \
+ "HEX_RANGE_10_20_LOW_DEFAULT", \
+ "INT_RANGE_10_20_LOW_DEFAULT":
+ sym = c.syms[sym_name]
+ verify(sym.ranges, "{} should have ranges".format(sym_name))
# hex/int symbols without defaults should get no default value
verify_value("HEX_NO_RANGE", "")
@@ -1246,9 +1035,9 @@ def run_selftests():
verify_value("INT_ALL_RANGES_DISABLED", "")
# Make sure they are assignable though, and test that the form of the user
# value is reflected in the value for hex symbols
- assign_and_verify_new_value("HEX_NO_RANGE", "0x123", "0x123")
- assign_and_verify_new_value("HEX_NO_RANGE", "123", "123")
- assign_and_verify_new_value("INT_NO_RANGE", "123", "123")
+ assign_and_verify("HEX_NO_RANGE", "0x123")
+ assign_and_verify("HEX_NO_RANGE", "123")
+ assign_and_verify("INT_NO_RANGE", "123")
# Defaults outside of the valid range should be clamped
verify_value("HEX_RANGE_10_20_LOW_DEFAULT", "0x10")
@@ -1272,16 +1061,19 @@ def run_selftests():
# User values and dependent ranges
def verify_range(sym_name, low, high, default):
- """Tests that the values in the range 'low'-'high' can be assigned, and
+ """
+ Tests that the values in the range 'low'-'high' can be assigned, and
that assigning values outside this range reverts the value back to
- 'default' (None if it should revert back to "")."""
- is_hex = (c[sym_name].get_type() == kconfiglib.HEX)
+ 'default' (None if it should revert back to "").
+ """
+ is_hex = (c.syms[sym_name].type == HEX)
+
for i in range(low, high + 1):
- assign_and_verify_new_user_value(sym_name, str(i), str(i))
+ assign_and_verify_user_value(sym_name, str(i), str(i), True)
if is_hex:
# The form of the user value should be preserved for hex
# symbols
- assign_and_verify_new_user_value(sym_name, hex(i), hex(i))
+ assign_and_verify_user_value(sym_name, hex(i), hex(i), True)
# Verify that assigning a user value just outside the range causes
# defaults to be used
@@ -1298,8 +1090,8 @@ def run_selftests():
too_low_str = str(low - 1)
too_high_str = str(high + 1)
- assign_and_verify_new_value(sym_name, too_low_str, default_str)
- assign_and_verify_new_value(sym_name, too_high_str, default_str)
+ assign_and_verify_value(sym_name, too_low_str, default_str)
+ assign_and_verify_value(sym_name, too_high_str, default_str)
verify_range("HEX_RANGE_10_20_LOW_DEFAULT", 0x10, 0x20, 0x10)
verify_range("HEX_RANGE_10_20_HIGH_DEFAULT", 0x10, 0x20, 0x20)
@@ -1310,7 +1102,6 @@ def run_selftests():
verify_range("INT_RANGE_10_20_OK_DEFAULT", 10, 20, 15)
verify_range("HEX_RANGE_10_20", 0x10, 0x20, 0x10)
- verify_range("HEX_RANGE_0_10", 0x0, 0x10, None)
verify_range("INT_RANGE_10_20", 10, 20, 10)
verify_range("INT_RANGE_0_10", 0, 10, None)
@@ -1321,15 +1112,15 @@ def run_selftests():
verify_value("HEX_40", "40")
verify_value("INT_40", "40")
- c["HEX_RANGE_10_20"].unset_user_value()
- c["INT_RANGE_10_20"].unset_user_value()
+ c.syms["HEX_RANGE_10_20"].unset_value()
+ c.syms["INT_RANGE_10_20"].unset_value()
verify_value("HEX_RANGE_10_40_DEPENDENT", "0x10")
verify_value("INT_RANGE_10_40_DEPENDENT", "10")
- c["HEX_RANGE_10_20"].set_user_value("15")
- c["INT_RANGE_10_20"].set_user_value("15")
+ c.syms["HEX_RANGE_10_20"].set_value("15")
+ c.syms["INT_RANGE_10_20"].set_value("15")
verify_value("HEX_RANGE_10_40_DEPENDENT", "0x15")
verify_value("INT_RANGE_10_40_DEPENDENT", "15")
- c.unset_user_values()
+ c.unset_values()
verify_range("HEX_RANGE_10_40_DEPENDENT", 0x10, 0x40, 0x10)
verify_range("INT_RANGE_10_40_DEPENDENT", 10, 40, 10)
@@ -1339,334 +1130,161 @@ def run_selftests():
verify_value("ACTIVE_RANGE", "1")
#
- # get_referenced_symbols()
- #
-
- c = kconfiglib.Config("Kconfiglib/tests/Kref")
-
- # General function for checking get_referenced_symbols() output.
- # Specialized for symbols below.
- def verify_refs(item, refs_no_enclosing, refs_enclosing):
- item_refs = item.get_referenced_symbols()
- item_refs_enclosing = item.get_referenced_symbols(True)
-
- # For failure messages
- if item.is_symbol():
- item_string = item.get_name()
- elif item.is_choice():
- if item.get_name() is None:
- item_string = "choice"
- else:
- item_string = "choice " + item.get_name()
- elif item.is_menu():
- item_string = 'menu "{}"'.format(item.get_title())
- else:
- # Comment
- item_string = 'comment "{}"'.format(item.get_text())
-
- verify(len(item_refs) == len(refs_no_enclosing),
- "Wrong number of refs excluding enclosing for {}".
- format(item_string))
- verify(len(item_refs_enclosing) == len(refs_enclosing),
- "Wrong number of refs including enclosing for {}".
- format(item_string))
- for r in [c[name] for name in refs_no_enclosing]:
- verify(r in item_refs,
- "{} should reference {} when excluding enclosing".
- format(item_string, r.get_name()))
- for r in [c[name] for name in refs_enclosing]:
- verify(r in item_refs_enclosing,
- "{} should reference {} when including enclosing".
- format(item_string, r.get_name()))
-
- # Symbols referenced by symbols
-
- def verify_sym_refs(sym_name, refs_no_enclosing, refs_enclosing):
- verify_refs(c[sym_name], refs_no_enclosing, refs_enclosing)
-
- verify_sym_refs("NO_REF", [], [])
- verify_sym_refs("ONE_REF", ["A"], ["A"])
- own_refs = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
- "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
- "Y", "Z", "AA"]
- verify_sym_refs("MANY_REF",
- own_refs,
- own_refs + ["IF_REF_1", "IF_REF_2", "MENU_REF_1",
- "MENU_REF_2"])
-
- # Symbols referenced by choices
-
- own_refs = ["CHOICE_REF_4", "CHOICE_REF_5", "CHOICE_REF_6"]
- verify_refs(c.get_choices()[0],
- own_refs,
- own_refs + ["CHOICE_REF_1", "CHOICE_REF_2", "CHOICE_REF_3"])
-
- # Symbols referenced by menus
-
- own_refs = ["NO_REF", "MENU_REF_3"]
- verify_refs(c.get_menus()[1],
- own_refs,
- own_refs + ["MENU_REF_1", "MENU_REF_2"])
-
- # Symbols referenced by comments
-
- own_refs = ["COMMENT_REF_3", "COMMENT_REF_4", "COMMENT_REF_5"]
- verify_refs(c.get_comments()[0],
- own_refs,
- own_refs + ["COMMENT_REF_1", "COMMENT_REF_2"])
-
- #
- # get_selected_symbols() (same test file)
+ # defconfig_filename
#
- def verify_selects(sym_name, selection_names):
- sym = c[sym_name]
- sym_selections = sym.get_selected_symbols()
- verify(len(sym_selections) == len(selection_names),
- "Wrong number of selects for {}".format(sym_name))
- for sel_name in selection_names:
- sel_sym = c[sel_name]
- verify(sel_sym in sym_selections,
- "{} should be selected by {}".format(sel_name, sym_name))
-
- verify_selects("n", [])
- verify_selects("m", [])
- verify_selects("y", [])
- verify_selects("UNAME_RELEASE", [])
+ print("Testing defconfig_filename")
- verify_selects("NO_REF", [])
- verify_selects("MANY_REF", ["I", "N"])
-
- #
- # get_implied_symbols() (same test file)
- #
+ c = Kconfig("Kconfiglib/tests/empty")
+ verify(c.defconfig_filename is None,
+ "defconfig_filename should be None with no defconfig_list symbol")
- def verify_implies(sym_name, imply_names):
- sym = c[sym_name]
- sym_implies = sym.get_implied_symbols()
- verify(len(sym_implies) == len(imply_names),
- "Wrong number of implies for {}".format(sym_name))
- for imply_name in imply_names:
- implied_sym = c[imply_name]
- verify(implied_sym in sym_implies,
- "{} should be implied by {}".format(imply_name, sym_name))
-
- verify_implies("n", [])
- verify_implies("m", [])
- verify_implies("y", [])
- verify_implies("UNAME_RELEASE", [])
-
- verify_implies("NO_REF", [])
- verify_implies("MANY_REF", ["P", "U"])
-
- #
- # get_defconfig_filename()
- #
-
- print("Testing get_defconfig_filename()...")
-
- c = kconfiglib.Config("Kconfiglib/tests/empty")
- verify(c.get_defconfig_filename() is None,
- "get_defconfig_filename() should be None with no defconfig_list "
- "symbol")
-
- c = kconfiglib.Config("Kconfiglib/tests/Kdefconfig_nonexistent")
- verify(c.get_defconfig_filename() is None,
- "get_defconfig_filename() should be None when none of the files "
- "in the defconfig_list symbol exist")
+ c = Kconfig("Kconfiglib/tests/Kdefconfig_nonexistent")
+ verify(c.defconfig_filename is None,
+ "defconfig_filename should be None when none of the files in the "
+ "defconfig_list symbol exist")
# Referenced in Kdefconfig_existent(_but_n)
os.environ["BAR"] = "defconfig_2"
- c = kconfiglib.Config("Kconfiglib/tests/Kdefconfig_existent_but_n")
- verify(c.get_defconfig_filename() is None,
- "get_defconfig_filename() should be None when the condition is "
- "n for all the defaults")
+ c = Kconfig("Kconfiglib/tests/Kdefconfig_existent_but_n")
+ verify(c.defconfig_filename is None,
+ "defconfig_filename should be None when the condition is n for all "
+ "the defaults")
- c = kconfiglib.Config("Kconfiglib/tests/Kdefconfig_existent")
- verify(c.get_defconfig_filename() == "Kconfiglib/tests/defconfig_2",
- "get_defconfig_filename() should return the existent file "
+ c = Kconfig("Kconfiglib/tests/Kdefconfig_existent")
+ verify(c.defconfig_filename == "Kconfiglib/tests/defconfig_2",
+ "defconfig_filename should return the existent file "
"Kconfiglib/tests/defconfig_2")
- # Should also look relative to $srctree if the defconfig is an absolute
- # path and not found
+ # Should also look relative to $srctree if the specified defconfig is a
+ # relative path and can't be opened
- del os.environ["srctree"]
- c = kconfiglib.Config("Kconfiglib/tests/Kdefconfig_srctree")
- verify(c.get_defconfig_filename() == "Kconfiglib/tests/defconfig_2",
- "get_defconfig_filename() returned wrong file with $srctree unset")
+ c = Kconfig("Kconfiglib/tests/Kdefconfig_srctree")
+ verify(c.defconfig_filename == "Kconfiglib/tests/defconfig_2",
+ "defconfig_filename gave wrong file with $srctree unset")
os.environ["srctree"] = "Kconfiglib/tests"
- c = kconfiglib.Config("Kconfiglib/tests/Kdefconfig_srctree")
- verify(c.get_defconfig_filename() ==
- "Kconfiglib/tests/sub/defconfig_in_sub",
- "get_defconfig_filename() returned wrong file with $srctree set")
+ c = Kconfig("Kconfiglib/tests/Kdefconfig_srctree")
+ verify(c.defconfig_filename == "Kconfiglib/tests/sub/defconfig_in_sub",
+ "defconfig_filename gave wrong file with $srctree set")
- #
- # get_mainmenu_text()
- #
- print("Testing get_mainmenu_text()...")
+ print("Testing mainmenu_text")
- c = kconfiglib.Config("Kconfiglib/tests/empty")
- verify(c.get_mainmenu_text() is None,
- "An empty Kconfig should not have a mainmenu text")
+ c = Kconfig("Kconfiglib/tests/empty")
+ verify(c.mainmenu_text == "Linux Kernel Configuration",
+ "An empty Kconfig should get a default main menu prompt")
# Expanded in the mainmenu text
os.environ["FOO"] = "bar baz"
- c = kconfiglib.Config("Kconfiglib/tests/Kmainmenu")
- verify(c.get_mainmenu_text() == "---bar baz---",
+ c = Kconfig("Kconfiglib/tests/Kmainmenu")
+ verify(c.mainmenu_text == "---bar baz---",
"Wrong mainmenu text")
- #
- # Misc. minor APIs
- #
- os.environ["ENV_VAR"] = "foo"
- # Contains reference to undefined environment variable, so disable warnings
- c = kconfiglib.Config("Kconfiglib/tests/Kmisc", print_warnings = False)
+ print("Testing user_value")
- print("Testing is_optional()...")
-
- verify(not c.get_choices()[0].is_optional(),
- "First choice should not be optional")
- verify(c.get_choices()[1].is_optional(),
- "Second choice should be optional")
-
- print("Testing get_user_value()...")
+ # References undefined env. var. Disable warnings.
+ c = Kconfig("Kconfiglib/tests/Kmisc", warn=False)
# Avoid warnings from assigning invalid user values and assigning user
# values to symbols without prompts
- c.set_print_warnings(False)
+ c.disable_warnings()
- syms = [c[name] for name in \
- ("BOOL", "TRISTATE", "STRING", "INT", "HEX")]
+ syms = [c.syms[name] for name in
+ ("BOOL", "TRISTATE", "STRING", "INT", "HEX")]
for sym in syms:
- verify(sym.get_user_value() is None,
+ verify(sym.user_value is None,
"{} should not have a user value to begin with")
# Assign valid values for the types
- assign_and_verify_new_user_value("BOOL", "n", "n")
- assign_and_verify_new_user_value("BOOL", "y", "y")
- assign_and_verify_new_user_value("TRISTATE", "n", "n")
- assign_and_verify_new_user_value("TRISTATE", "m", "m")
- assign_and_verify_new_user_value("TRISTATE", "y", "y")
- assign_and_verify_new_user_value("STRING", "foo bar", "foo bar")
- assign_and_verify_new_user_value("INT", "123", "123")
- assign_and_verify_new_user_value("HEX", "0x123", "0x123")
+ assign_and_verify_user_value("BOOL", 0, 0, True)
+ assign_and_verify_user_value("BOOL", 2, 2, True)
+ assign_and_verify_user_value("TRISTATE", 0, 0, True)
+ assign_and_verify_user_value("TRISTATE", 1, 1, True)
+ assign_and_verify_user_value("TRISTATE", 2, 2, True)
+ assign_and_verify_user_value("STRING", "foo bar", "foo bar", True)
+ assign_and_verify_user_value("INT", "123", "123", True)
+ assign_and_verify_user_value("HEX", "0x123", "0x123", True)
# Assign invalid values for the types. They should retain their old user
# value.
- assign_and_verify_new_user_value("BOOL", "m", "y")
- assign_and_verify_new_user_value("BOOL", "foo", "y")
- assign_and_verify_new_user_value("BOOL", "1", "y")
- assign_and_verify_new_user_value("TRISTATE", "foo", "y")
- assign_and_verify_new_user_value("TRISTATE", "1", "y")
- assign_and_verify_new_user_value("INT", "foo", "123")
- assign_and_verify_new_user_value("HEX", "foo", "0x123")
+ assign_and_verify_user_value("BOOL", 1, 2, False)
+ assign_and_verify_user_value("BOOL", "foo", 2, False)
+ assign_and_verify_user_value("BOOL", "1", 2, False)
+ assign_and_verify_user_value("TRISTATE", "foo", 2, False)
+ assign_and_verify_user_value("TRISTATE", "1", 2, False)
+ assign_and_verify_user_value("STRING", 0, "foo bar", False)
+ assign_and_verify_user_value("INT", "foo", "123", False)
+ assign_and_verify_user_value("INT", 0, "123", False)
+ assign_and_verify_user_value("HEX", "foo", "0x123", False)
+ assign_and_verify_user_value("HEX", 0, "0x123", False)
+ assign_and_verify_user_value("HEX", "-0x1", "0x123", False)
for s in syms:
- s.unset_user_value()
- verify(s.get_user_value() is None,
+ s.unset_value()
+ verify(s.user_value is None,
"{} should not have a user value after being reset".
- format(s.get_name()))
-
- print("Testing is_defined()...")
-
- for sym_name in ("n", "m", "y", "UNAME_RELEASE", "A", "B", "C", "D",
- "BOOL", "TRISTATE", "STRING", "INT", "HEX"):
- sym = c[sym_name]
- verify(sym.is_defined(),
- "{} should be defined".format(sym_name))
-
- for sym_name in ("NOT_DEFINED_1", "NOT_DEFINED_2", "NOT_DEFINED_3",
- "NOT_DEFINED_4"):
- sym = c[sym_name]
- verify(not sym.is_defined(),
- "{} should not be defined".format(sym_name))
-
- print("Testing is_special()...")
-
- for sym_name in ("n", "m", "y", "UNAME_RELEASE", "FROM_ENV",
- "FROM_ENV_MISSING"):
- sym = c[sym_name]
- verify(sym.is_special(),
- "{} should be special".format(sym_name))
-
- for sym_name in ("A", "B", "C", "D", "BOOL", "TRISTATE", "STRING",
- "INT", "HEX", "NOT_DEFINED_1", "NOT_DEFINED_2",
- "NOT_DEFINED_3", "NOT_DEFINED_4"):
- sym = c[sym_name]
- verify(not sym.is_special(),
- "{} should not be special".format(sym_name))
-
- print("Testing is_from_environment()...")
-
- for sym_name in ("FROM_ENV", "FROM_ENV_MISSING"):
- sym = c[sym_name]
- verify(sym.is_from_environment(),
- "{} should be from the environment".format(sym_name))
-
- for sym_name in ("n", "m", "y", "UNAME_RELEASE", "A", "B", "C", "D",
- "BOOL", "TRISTATE", "STRING", "INT", "HEX",
- "NOT_DEFINED_1", "NOT_DEFINED_2", "NOT_DEFINED_3",
- "NOT_DEFINED_4"):
- sym = c[sym_name]
- verify(not sym.is_from_environment(),
- "{} should not be from the environment".format(sym_name))
-
- print("Testing is_choice_symbol()...")
-
- for sym_name in ("A", "B", "C", "D"):
- sym = c[sym_name]
- verify(sym.is_choice_symbol(),
- "{} should be a choice symbol".format(sym_name))
-
- for sym_name in ("n", "m", "y", "UNAME_RELEASE", "Q1", "Q2", "Q3", "BOOL",
- "TRISTATE", "STRING", "INT", "HEX", "FROM_ENV",
- "FROM_ENV_MISSING", "NOT_DEFINED_1", "NOT_DEFINED_2",
- "NOT_DEFINED_3", "NOT_DEFINED_4"):
- sym = c[sym_name]
- verify(not sym.is_choice_symbol(),
- "{} should not be a choice symbol".format(sym_name))
-
- print("Testing is_allnoconfig_y()...")
-
- verify(not c["NOT_ALLNOCONFIG_Y"].is_allnoconfig_y(),
+ format(s.name))
+
+
+ print("Testing defined vs undefined symbols")
+
+ for name in "A", "B", "C", "D", "BOOL", "TRISTATE", "STRING", "INT", "HEX":
+ verify(c.syms[name].nodes,
+ "{} should be defined".format(name))
+
+ for name in "NOT_DEFINED_1", "NOT_DEFINED_2", "NOT_DEFINED_3", \
+ "NOT_DEFINED_4":
+ sym = c.syms[name]
+ verify(not c.syms[name].nodes,
+ "{} should not be defined".format(name))
+
+
+ print("Testing Symbol.choice")
+
+ for name in "A", "B", "C", "D":
+ verify(c.syms[name].choice is not None,
+ "{} should be a choice symbol".format(name))
+
+ for name in "Q1", "Q2", "Q3", "BOOL", "TRISTATE", "STRING", "INT", "HEX", \
+ "FROM_ENV", "FROM_ENV_MISSING", "NOT_DEFINED_1", \
+ "NOT_DEFINED_2", "NOT_DEFINED_3", "NOT_DEFINED_4":
+ verify(c.syms[name].choice is None,
+ "{} should not be a choice symbol".format(name))
+
+
+ print("Testing is_allnoconfig_y")
+
+ verify(not c.syms["NOT_ALLNOCONFIG_Y"].is_allnoconfig_y,
"NOT_ALLNOCONFIG_Y should not be allnoconfig_y")
- verify(c["ALLNOCONFIG_Y"].is_allnoconfig_y(),
+ verify(c.syms["ALLNOCONFIG_Y"].is_allnoconfig_y,
"ALLNOCONFIG_Y should be allnoconfig_y")
- print("Testing UNAME_RELEASE value...")
+ print("Testing UNAME_RELEASE")
verify_value("UNAME_RELEASE", platform.uname()[2])
+ ur = c.syms["UNAME_RELEASE"]
+ verify(ur.kconfig is c and
+ ur.type == STRING and
+ ur.env_var == "<uname release>",
+ "UNAME_RELEASE has wrong fields")
- # Expansion of environment variables in Config.__init__'s base_dir
- # parameter. Just make sure we don't crash when Kbase_dir 'source's a file
- # from the same directory.
-
- os.environ["EnV_VaR1"] = "Kconfigl"
- os.environ["EnV_VaR2"] = "ib/tests"
- kconfiglib.Config("Kconfiglib/tests/Kbase_dir",
- base_dir="$EnV_VaR1$EnV_VaR2/")
-
- #
- # .config reading and writing
- #
- print("Testing .config reading and writing...")
+ print("Testing .config reading and writing")
config_test_file = "Kconfiglib/tests/config_test"
def write_and_verify_header(header):
c.write_config(config_test_file, header)
c.load_config(config_test_file)
- verify(c.get_config_header() == header,
+ verify(c.config_header == header,
"The header {} morphed into {} on loading"
- .format(repr(header), repr(c.get_config_header())))
+ .format(repr(header), repr(c.config_header)))
def verify_file_contents(fname, contents):
with open(fname, "r") as f:
@@ -1677,15 +1295,15 @@ def run_selftests():
# Writing/reading strings with characters that need to be escaped
- c = kconfiglib.Config("Kconfiglib/tests/Kescape")
+ c = Kconfig("Kconfiglib/tests/Kescape")
# Test the default value
- c.write_config(config_test_file + "_from_def")
+ c.write_config(config_test_file + "_from_def", header="")
verify_file_contents(config_test_file + "_from_def",
r'''CONFIG_STRING="\"\\"''' "\n")
# Write our own value
- c["STRING"].set_user_value(r'''\"a'\\''')
- c.write_config(config_test_file + "_from_user")
+ c.syms["STRING"].set_value(r'''\"a'\\''')
+ c.write_config(config_test_file + "_from_user", header="")
verify_file_contents(config_test_file + "_from_user",
r'''CONFIG_STRING="\\\"a'\\\\"''' "\n")
@@ -1695,61 +1313,21 @@ def run_selftests():
c.load_config(config_test_file + "_from_user")
verify_value("STRING", r'''\"a'\\''')
- # Reading and writing of .config headers
-
- verify(c.get_config_header() is None,
- "Expected no header before .config loaded, got '{}'".
- format(c.get_config_header()))
-
- write_and_verify_header("")
- write_and_verify_header(" ")
- write_and_verify_header("\n")
- write_and_verify_header("\n\n")
- write_and_verify_header("#")
- write_and_verify_header("a")
- write_and_verify_header("a\n")
- write_and_verify_header("a\n\n")
- write_and_verify_header("abcdef")
- write_and_verify_header("foo\nbar baz\n\n\n qaz#")
-
- c.load_config("Kconfiglib/tests/empty")
- verify(c.get_config_header() is None,
- "Expected no header in empty .config, got '{}'".
- format(c.get_config_header()))
-
- c.load_config("Kconfiglib/tests/config_hash")
- verify(c.get_config_header() == "",
- "Expected empty header in file with just '#', got '{}'".
- format(c.get_config_header()))
-
- # TODO: Line joining (which stems from _FileFeed reuse) probably doesn't
- # make sense within .config files. (The C implementation has no notion of
- # continuation lines within .config files.) It's harmless except for fairly
- # obscure cases though.
- #
- # Add a test for now just to get test coverage for _FileFeed.peek_next(),
- # which is only used while reading .config files as of writing.
-
- c.load_config("Kconfiglib/tests/config_continuation")
- verify(c.get_config_header() ==
- " Foo # Bar\n Baz # Foo # Bar\n Baz\n Foo",
- "Continuation line handling within .config headers is broken")
-
# Appending values from a .config
- c = kconfiglib.Config("Kconfiglib/tests/Kappend")
+ c = Kconfig("Kconfiglib/tests/Kappend")
# Values before assigning
verify_value("BOOL", "n")
verify_value("STRING", "")
# Assign BOOL
- c.load_config("Kconfiglib/tests/config_set_bool", replace = False)
+ c.load_config("Kconfiglib/tests/config_set_bool", replace=False)
verify_value("BOOL", "y")
verify_value("STRING", "")
# Assign STRING
- c.load_config("Kconfiglib/tests/config_set_string", replace = False)
+ c.load_config("Kconfiglib/tests/config_set_string", replace=False)
verify_value("BOOL", "y")
verify_value("STRING", "foo bar")
@@ -1766,79 +1344,24 @@ def run_selftests():
c.load_config("Kconfiglib/tests/config_indented")
verify_value("IGNOREME", "y")
- #
- # get_config()
- #
-
- print("Testing get_config()...")
- c1 = kconfiglib.Config("Kconfiglib/tests/Kmisc", print_warnings = False)
- c2 = kconfiglib.Config("Kconfiglib/tests/Kmisc", print_warnings = False)
+ print("Testing Kconfig fetching and separation")
- c1_bool, c1_choice, c1_menu, c1_comment = c1["BOOL"], \
- c1.get_choices()[0], c1.get_menus()[0], c1.get_comments()[0]
- c2_bool, c2_choice, c2_menu, c2_comment = c2["BOOL"], \
- c2.get_choices()[0], c2.get_menus()[0], c2.get_comments()[0]
+ for c in Kconfig("Kconfiglib/tests/Kmisc", warn=False), \
+ Kconfig("Kconfiglib/tests/Kmisc", warn=False):
+ for item in c.syms["BOOL"], \
+ c.syms["BOOL"].nodes[0], \
+ c.named_choices["OPTIONAL"], \
+ c.named_choices["OPTIONAL"].nodes[0], \
+ c.syms["MENU_HOOK"].nodes[0].next, \
+ c.syms["COMMENT_HOOK"].nodes[0].next:
+ verify(item.kconfig is c,
+ ".kconfig not properly set for " + repr(item))
- verify((c1_bool is not c2_bool) and (c1_choice is not c2_choice) and
- (c1_menu is not c2_menu) and (c1_comment is not c2_comment) and
- (c1_bool.get_config() is c1) and (c2_bool.get_config() is c2) and
- (c1_choice.get_config() is c1) and (c2_choice.get_config() is c2) and
- (c1_menu.get_config() is c1) and (c2_menu.get_config() is c2) and
- (c1_comment.get_config() is c1) and (c2_comment.get_config() is c2),
- "Config instance state separation or get_config() is broken")
- #
- # get_arch/srcarch/srctree/kconfig_filename()
- #
+ print("Testing imply semantics")
- del os.environ["ARCH"]
- del os.environ["SRCARCH"]
- del os.environ["srctree"]
-
- c = kconfiglib.Config("Kconfiglib/tests/Kmisc", print_warnings = False)
- arch = c.get_arch()
- verify(arch is None, "Expected None arch, got '{}'".format(arch))
- srcarch = c.get_srcarch()
- verify(srcarch is None, "Expected None srcarch, got '{}'".format(srcarch))
- srctree = c.get_srctree()
- verify(srctree is None, "Expected None srctree, got '{}'".format(srctree))
-
- os.environ["ARCH"] = "ARCH value"
- os.environ["SRCARCH"] = "SRCARCH value"
- os.environ["srctree"] = "srctree value"
- c = kconfiglib.Config("Kconfiglib/tests/Kmisc", print_warnings = False)
- c.load_config("Kconfiglib/tests/empty")
-
- arch = c.get_arch()
- srcarch = c.get_srcarch()
- srctree = c.get_srctree()
- config_filename = c.get_config_filename()
- kconfig_filename = c.get_kconfig_filename()
-
- print("Testing get_arch()...")
- verify(arch == "ARCH value",
- "Wrong arch value - got '{}'".format(arch))
- print("Testing get_srcarch()...")
- verify(srcarch == "SRCARCH value",
- "Wrong srcarch value - got '{}'".format(srcarch))
- print("Testing get_srctree()...")
- verify(srctree == "srctree value",
- "Wrong srctree value - got '{}'".format(srctree))
- print("Testing get_config_filename()...")
- verify(config_filename == "Kconfiglib/tests/empty",
- "Wrong config filename - got '{}'".format(config_filename))
- print("Testing get_kconfig_filename()...")
- verify(kconfig_filename == "Kconfiglib/tests/Kmisc",
- "Wrong Kconfig filename - got '{}'".format(kconfig_filename))
-
- #
- # Imply semantics
- #
-
- print("Testing imply semantics...")
-
- c = kconfiglib.Config("Kconfiglib/tests/Kimply")
+ c = Kconfig("Kconfiglib/tests/Kimply")
verify_value("IMPLY_DIRECT_DEPS", "y")
verify_value("UNMET_DIRECT_1", "n")
@@ -1871,293 +1394,263 @@ def run_selftests():
# Verify that IMPLIED_TRISTATE is invalidated if the direct
# dependencies change
- assign_and_verify("IMPLY", "y")
- assign_and_verify("DIRECT_DEP", "y")
- verify_value("IMPLIED_TRISTATE", "y")
- assign_and_verify("DIRECT_DEP", "n")
- verify_value("IMPLIED_TRISTATE", "n")
+ assign_and_verify("IMPLY", 2)
+ assign_and_verify("DIRECT_DEP", 2)
+ verify_value("IMPLIED_TRISTATE", 2)
+ assign_and_verify("DIRECT_DEP", 0)
+ verify_value("IMPLIED_TRISTATE", 0)
# Set back for later tests
- assign_and_verify("DIRECT_DEP", "y")
+ assign_and_verify("DIRECT_DEP", 2)
# Verify that IMPLIED_TRISTATE can be set to anything when IMPLY has value
- # "n", and that it gets the value "n" by default (for non-imply-related
+ # n, and that it gets the value n by default (for non-imply-related
# reasons)
- assign_and_verify("IMPLY", "n")
- assign_and_verify("IMPLIED_TRISTATE", "n")
- assign_and_verify("IMPLIED_TRISTATE", "m")
- assign_and_verify("IMPLIED_TRISTATE", "y")
- c["IMPLIED_TRISTATE"].unset_user_value()
+ assign_and_verify("IMPLY", 0)
+ assign_and_verify("IMPLIED_TRISTATE", 0)
+ assign_and_verify("IMPLIED_TRISTATE", 1)
+ assign_and_verify("IMPLIED_TRISTATE", 2)
+ c.syms["IMPLIED_TRISTATE"].unset_value()
verify_value("IMPLIED_TRISTATE", "n")
- # Same as above for "m". Anything still goes, but "m" by default now.
-
- assign_and_verify("IMPLY", "m")
- assign_and_verify("IMPLIED_TRISTATE", "n")
- assign_and_verify("IMPLIED_TRISTATE", "m")
- assign_and_verify("IMPLIED_TRISTATE", "y")
- c["IMPLIED_TRISTATE"].unset_user_value()
- verify_value("IMPLIED_TRISTATE", "m")
+ # Same as above for m. Anything still goes, but m by default now.
- # Same as above for "y". Only "n" and "y" should be accepted. "m" gets
- # promoted to "y". Default should be "y".
+ assign_and_verify("IMPLY", 1)
+ assign_and_verify("IMPLIED_TRISTATE", 0)
+ assign_and_verify("IMPLIED_TRISTATE", 1)
+ assign_and_verify("IMPLIED_TRISTATE", 2)
+ c.syms["IMPLIED_TRISTATE"].unset_value()
+ verify_value("IMPLIED_TRISTATE", 1)
- assign_and_verify("IMPLY", "y")
- assign_and_verify("IMPLIED_TRISTATE", "n")
- assign_and_verify_new_value("IMPLIED_TRISTATE", "m", "y")
- assign_and_verify("IMPLIED_TRISTATE", "y")
- c["IMPLIED_TRISTATE"].unset_user_value()
- verify_value("IMPLIED_TRISTATE", "y")
+ # Same as above for y. Only n and y should be accepted. m gets promoted to
+ # y. Default should be y.
- # Being implied to either "m" or "y" should give a bool the value "y"
+ assign_and_verify("IMPLY", 2)
+ assign_and_verify("IMPLIED_TRISTATE", 0)
+ assign_and_verify_value("IMPLIED_TRISTATE", 1, 2)
+ assign_and_verify("IMPLIED_TRISTATE", 2)
+ c.syms["IMPLIED_TRISTATE"].unset_value()
+ verify_value("IMPLIED_TRISTATE", 2)
- c["IMPLY"].unset_user_value()
- verify_value("IMPLIED_BOOL", "n")
- assign_and_verify("IMPLY", "n")
- verify_value("IMPLIED_BOOL", "n")
- assign_and_verify("IMPLY", "m")
- verify_value("IMPLIED_BOOL", "y")
- assign_and_verify("IMPLY", "y")
- verify_value("IMPLIED_BOOL", "y")
+ # Being implied to either m or y should give a bool the value y
- # A bool implied to "m" or "y" can take the values "n" and "y"
+ c.syms["IMPLY"].unset_value()
+ verify_value("IMPLIED_BOOL", 0)
+ assign_and_verify("IMPLY", 0)
+ verify_value("IMPLIED_BOOL", 0)
+ assign_and_verify("IMPLY", 1)
+ verify_value("IMPLIED_BOOL", 2)
+ assign_and_verify("IMPLY", 2)
+ verify_value("IMPLIED_BOOL", 2)
- c["IMPLY"].set_user_value("m")
- assign_and_verify("IMPLIED_BOOL", "n")
- assign_and_verify("IMPLIED_BOOL", "y")
+ # A bool implied to m or y can take the values n and y
- c["IMPLY"].set_user_value("y")
- assign_and_verify("IMPLIED_BOOL", "n")
- assign_and_verify("IMPLIED_BOOL", "y")
+ c.syms["IMPLY"].set_value(1)
+ assign_and_verify("IMPLIED_BOOL", 0)
+ assign_and_verify("IMPLIED_BOOL", 2)
- #
- # Choice semantics
- #
+ c.syms["IMPLY"].set_value(2)
+ assign_and_verify("IMPLIED_BOOL", 0)
+ assign_and_verify("IMPLIED_BOOL", 2)
- print("Testing choice semantics...")
- c = kconfiglib.Config("Kconfiglib/tests/Kchoice")
+ print("Testing choice semantics")
- choice_bool, choice_bool_opt, choice_tristate, choice_tristate_opt, \
- choice_bool_m, choice_tristate_m, choice_defaults, \
- choice_defaults_not_visible, choice_no_type_bool, \
- choice_no_type_tristate, choice_missing_member_type_1, \
- choice_missing_member_type_2, choice_weird_syms = c.get_choices()
+ c = Kconfig("Kconfiglib/tests/Kchoice")
- for choice in (choice_bool, choice_bool_opt, choice_bool_m,
- choice_defaults):
- verify(choice.get_type() == kconfiglib.BOOL,
- "choice {} should have type bool".format(choice.get_name()))
+ for name in "BOOL", "BOOL_OPT", "BOOL_M", "DEFAULTS":
+ verify(c.named_choices[name].orig_type == BOOL,
+ "choice {} should have type bool".format(name))
- for choice in (choice_tristate, choice_tristate_opt, choice_tristate_m):
- verify(choice.get_type() == kconfiglib.TRISTATE,
- "choice {} should have type tristate"
- .format(choice.get_name()))
+ for name in "TRISTATE", "TRISTATE_OPT", "TRISTATE_M":
+ verify(c.named_choices[name].orig_type == TRISTATE,
+ "choice {} should have type tristate".format(name))
def select_and_verify(sym):
- choice = sym.get_parent()
- sym.set_user_value("y")
- verify(choice.get_mode() == "y",
- 'The mode of the choice should be "y" after selecting a '
- "symbol")
- verify(sym.is_choice_selection(),
- "is_choice_selection() should be true for {}"
- .format(sym.get_name()))
- verify(choice.get_selection() is sym,
- "{} should be the selected symbol".format(sym.get_name()))
- verify(choice.get_user_selection() is sym,
- "{} should be the user selection of the choice"
- .format(sym.get_name()))
-
- def select_and_verify_all(choice):
- choice_syms = choice.get_symbols()
+ choice = sym.nodes[0].parent.item
+ choice.set_value(2)
+
+ sym.set_value(2)
+
+ verify(sym.choice.selection is sym,
+ sym.name + " should be the selected symbol")
+
+ verify(choice.user_selection is sym,
+ sym.name + " should be the user selection of the choice")
+
+ verify(sym.tri_value == 2,
+ sym.name + " should be y when selected")
+
+ verify(sym.user_value != 2,
+ sym.name + " should not have user value y, because choice "
+ "y mode selections are remembered on the choice "
+ "itself")
+
+ for sibling in choice.syms:
+ if sibling is not sym:
+ verify(sibling.tri_value == 0,
+ sibling.name + " should be n when not selected")
+
+ def select_and_verify_all(choice_name):
+ choice = c.named_choices[choice_name]
+
# Select in forward order
- for sym in choice_syms:
+ for sym in choice.syms:
select_and_verify(sym)
+
# Select in reverse order
- for i in range(len(choice_syms) - 1, 0, -1):
- select_and_verify(choice_syms[i])
-
- def verify_mode(choice, no_modules_mode, modules_mode):
- c["MODULES"].set_user_value("n")
- choice_mode = choice.get_mode()
- verify(choice_mode == no_modules_mode,
- 'Wrong mode for choice {} with no modules. Expected "{}", '
- 'got "{}".'.format(choice.get_name(), no_modules_mode,
- choice_mode))
-
- c["MODULES"].set_user_value("y")
- choice_mode = choice.get_mode()
- verify(choice_mode == modules_mode,
- 'Wrong mode for choice {} with modules. Expected "{}", '
- 'got "{}".'.format(choice.get_name(), modules_mode,
- choice_mode))
-
- verify_mode(choice_bool, "y", "y")
- verify_mode(choice_bool_opt, "n", "n")
- verify_mode(choice_tristate, "y", "m")
- verify_mode(choice_tristate_opt, "n", "n")
- verify_mode(choice_bool_m, "n", "y") # Promoted
- verify_mode(choice_tristate_m, "n", "m")
+ for sym in reversed(choice.syms):
+ select_and_verify(sym)
+
+ def verify_mode(choice_name, no_modules_mode, modules_mode):
+ choice = c.named_choices[choice_name]
+
+ c.modules.set_value(0)
+ verify(choice.tri_value == no_modules_mode,
+ 'Wrong mode for choice {} with no modules. Expected {}, got {}.'
+ .format(choice.name, no_modules_mode, choice.tri_value))
+
+ c.modules.set_value(2)
+ verify(choice.tri_value == modules_mode,
+ 'Wrong mode for choice {} with modules. Expected {}, got {}.'
+ .format(choice.name, modules_mode, choice.tri_value))
+
+ verify_mode("BOOL", 2, 2)
+ verify_mode("BOOL_OPT", 0, 0)
+ verify_mode("TRISTATE", 2, 1)
+ verify_mode("TRISTATE_OPT", 0, 0)
+ verify_mode("BOOL_M", 0, 2)
+ verify_mode("TRISTATE_M", 0, 1)
# Test defaults
- c["TRISTATE_SYM"].set_user_value("n")
- verify(choice_defaults.get_selection_from_defaults() is c["OPT_4"] and
- choice_defaults.get_selection() is c["OPT_4"],
+ choice = c.named_choices["DEFAULTS"]
+
+ c.syms["TRISTATE_SYM"].set_value(0)
+ verify(choice.selection is c.syms["OPT_4"],
"Wrong choice default with TRISTATE_SYM = n")
- c["TRISTATE_SYM"].set_user_value("y")
- verify(choice_defaults.get_selection_from_defaults() is c["OPT_2"] and
- choice_defaults.get_selection() is c["OPT_2"],
+
+ c.syms["TRISTATE_SYM"].set_value(2)
+ verify(choice.selection is c.syms["OPT_2"],
"Wrong choice default with TRISTATE_SYM = y")
- c["OPT_1"].set_user_value("y")
- verify(choice_defaults.get_selection_from_defaults() is c["OPT_2"],
- "User selection changed default selection - shouldn't have")
- verify(choice_defaults.get_selection() is c["OPT_1"],
+
+ c.syms["OPT_1"].set_value(2)
+ verify(choice.selection is c.syms["OPT_1"],
"User selection should override defaults")
- verify(choice_defaults_not_visible.get_selection_from_defaults()
- is c["OPT_8"] and
- choice_defaults_not_visible.get_selection()
- is c["OPT_8"],
+ verify(c.named_choices["DEFAULTS_NOT_VISIBLE"].selection
+ is c.syms["OPT_8"],
"Non-visible choice symbols should cause the next default to be "
"considered")
- # Test "y" mode selection
+ # Test y mode selection
- c["MODULES"].set_user_value("y")
+ c.modules.set_value(2)
- select_and_verify_all(choice_bool)
- select_and_verify_all(choice_bool_opt)
- select_and_verify_all(choice_tristate)
- select_and_verify_all(choice_tristate_opt)
+ select_and_verify_all("BOOL")
+ select_and_verify_all("BOOL_OPT")
+ select_and_verify_all("TRISTATE")
+ select_and_verify_all("TRISTATE_OPT")
# For BOOL_M, the mode should have been promoted
- select_and_verify_all(choice_bool_m)
+ select_and_verify_all("BOOL_M")
+
+ # Test m mode selection
- # Test "m" mode selection...
+ c.named_choices["TRISTATE"].set_value(1)
+ assign_and_verify_value("T_1", 1, 1)
+ assign_and_verify_value("T_2", 1, 1)
- # ...for a choice that can also be in "y" mode
+ c.syms["T_1"].set_value(0) # Check that this is remembered later
- for sym_name in ("T_1", "T_2"):
- assign_and_verify_new_value(sym_name, "m", "m")
- verify(choice_tristate.get_mode() == "m",
- 'Selecting {} to "m" should have changed the mode of the '
- 'choice to "m"'.format(sym_name))
+ # Switching to y mode should cause T_1 to become selected
+ c.named_choices["TRISTATE"].set_value(2)
+ verify_value("T_1", 2)
+ verify_value("T_2", 0)
- assign_and_verify_new_value(sym_name, "y", "y")
- verify(choice_tristate.get_mode() == "y" and
- choice_tristate.get_selection() is c[sym_name],
- 'Selecting {} to "y" should have changed the mode of the '
- 'choice to "y" and made it the selection'.format(sym_name))
+ # Switching back to m mode should restore the old values
+ c.named_choices["TRISTATE"].set_value(1)
+ verify_value("T_1", 0)
+ verify_value("T_2", 1)
- # ...for a choice that can only be in "m" mode
+ assign_and_verify_value("TM_1", 1, 1)
+ assign_and_verify_value("TM_1", 2, 1) # Ignored
+ verify(c.named_choices["TRISTATE"].tri_value == 1,
+ "m-visible choice got invalid mode")
- for sym_name in ("TM_1", "TM_2"):
- assign_and_verify_new_value(sym_name, "m", "m")
- assign_and_verify_new_value(sym_name, "n", "n")
- # "y" should be truncated
- assign_and_verify_new_value(sym_name, "y", "m")
- verify(choice_tristate_m.get_mode() == "m",
- 'A choice that can only be in "m" mode was not')
+ assign_and_verify_value("TM_1", 0, 0)
+ assign_and_verify_value("TM_1", 2, 0) # Ignored
# Verify that choices with no explicitly specified type get the type of the
# first contained symbol with a type
- verify(choice_no_type_bool.get_type() == kconfiglib.BOOL,
+ verify(c.named_choices["NO_TYPE_BOOL"].orig_type == BOOL,
"Expected first choice without explicit type to have type bool")
- verify(choice_no_type_tristate.get_type() == kconfiglib.TRISTATE,
+
+ verify(c.named_choices["NO_TYPE_TRISTATE"].orig_type == TRISTATE,
"Expected second choice without explicit type to have type "
"tristate")
# Verify that symbols without a type in the choice get the type of the
# choice
- verify((c["MMT_1"].get_type(), c["MMT_2"].get_type(),
- c["MMT_3"].get_type()) ==
- (kconfiglib.BOOL, kconfiglib.BOOL, kconfiglib.TRISTATE),
- "Wrong types for first choice with missing member types")
+ for name in "MMT_1", "MMT_2", "MMT_4", "MMT_5":
+ verify(c.syms[name].orig_type == BOOL,
+ "Expected {} to get type bool".format(name))
- verify((c["MMT_4"].get_type(), c["MMT_5"].get_type()) ==
- (kconfiglib.BOOL, kconfiglib.BOOL),
- "Wrong types for second choice with missing member types")
+ verify(c.syms["MMT_3"].orig_type == TRISTATE,
+ "Expected MMT_3 to have type tristate")
# Verify that symbols in choices that depend on the preceding symbol aren't
# considered choice symbols
- def verify_is_normal_choice_symbol(sym):
- verify(sym.is_choice_symbol() and
- sym in choice_weird_syms.get_symbols() and
- sym.get_parent() is choice_weird_syms,
- "{} should be a normal choice symbol".format(sym.get_name()))
-
- def verify_is_weird_choice_symbol(sym):
- verify(not sym.is_choice_symbol() and
- sym not in choice_weird_syms.get_symbols() and
- sym in choice_weird_syms.get_items() and
- sym.get_parent() is choice_weird_syms,
- "{} should be a weird (non-)choice symbol")
-
- verify_is_normal_choice_symbol(c["WS1"])
- verify_is_weird_choice_symbol(c["WS2"])
- verify_is_weird_choice_symbol(c["WS3"])
- verify_is_weird_choice_symbol(c["WS4"])
- verify_is_normal_choice_symbol(c["WS5"])
- verify_is_weird_choice_symbol(c["WS6"])
-
- #
- # Object dependencies
- #
-
- print("Testing object dependencies...")
-
- # Note: This tests an internal API
-
- c = kconfiglib.Config("Kconfiglib/tests/Kdep")
-
- def verify_dependent(sym_name, deps_names):
- sym = c[sym_name]
- deps = [c[name] for name in deps_names]
- sym_deps = sym._get_dependent()
- verify(len(sym_deps) == len(deps),
- "Wrong number of dependent symbols for {}".format(sym_name))
- verify(len(sym_deps) == len(set(sym_deps)),
- "{}'s dependencies contains duplicates".format(sym_name))
- for dep in deps:
- verify(dep in sym_deps, "{} should depend on {}".
- format(dep.get_name(), sym_name))
-
- # Test twice to cover dependency caching
- for i in range(0, 2):
- n_deps = 39
- # Verify that D1, D2, .., D<n_deps> are dependent on D
- verify_dependent("D", ["D{}".format(i) for i in range(1, n_deps + 1)])
- # Choices
- verify_dependent("A", ["B", "C"])
- verify_dependent("B", ["A", "C"])
- verify_dependent("C", ["A", "B"])
- verify_dependent("S", ["A", "B", "C"])
-
- # Verify that the last symbol depends on the first in a long chain of
- # dependencies. Test twice to cover dependency caching.
-
- c = kconfiglib.Config("Kconfiglib/tests/Kchain")
-
- for i in range(0, 2):
- verify(c["CHAIN_26"] in c["CHAIN_1"]._get_dependent(),
- "Dependency chain broken")
-
- print("\nAll selftests passed\n" if _all_ok else
+ weird_choice = c.named_choices["WEIRD_SYMS"]
+
+ def verify_is_normal_choice_symbol(name):
+ sym = c.syms[name]
+ verify(sym.choice is not None and
+ sym in weird_choice.syms and
+ sym.nodes[0].parent.item is weird_choice,
+ "{} should be a normal choice symbol".format(sym.name))
+
+ def verify_is_weird_choice_symbol(name):
+ sym = c.syms[name]
+ verify(sym.choice is None and
+ sym not in weird_choice.syms,
+ "{} should be a weird (non-)choice symbol"
+ .format(sym.name))
+
+ verify_is_normal_choice_symbol("WS1")
+ verify_is_weird_choice_symbol("WS2")
+ verify_is_weird_choice_symbol("WS3")
+ verify_is_weird_choice_symbol("WS4")
+ verify_is_weird_choice_symbol("WS5")
+ verify_is_normal_choice_symbol("WS6")
+ verify_is_weird_choice_symbol("WS7")
+ verify_is_weird_choice_symbol("WS8")
+ verify_is_normal_choice_symbol("WS9")
+
+
+ print("Testing compatibility with weird selects/implies")
+
+ # Check that Kconfiglib doesn't crash for stuff like 'select n' (seen in
+ # U-Boot). These probably originate from misunderstandings of how Kconfig
+ # works.
+ Kconfig("Kconfiglib/tests/Kwtf")
+
+ print("\nAll selftests passed\n" if all_passed else
"\nSome selftests failed\n")
def run_compatibility_tests():
- """Runs tests on configurations from the kernel. Tests compability with the
- C implementation by comparing outputs."""
+ """
+ Runs tests on configurations from the kernel. Tests compability with the
+ C implementation by comparing outputs.
+ """
- del os.environ["ARCH"]
- del os.environ["SRCARCH"]
- del os.environ["srctree"]
+ os.environ.pop("ARCH", None)
+ os.environ.pop("SRCARCH", None)
+ os.environ.pop("srctree", None)
- if speedy_mode and not os.path.exists("scripts/kconfig/conf"):
+ if speedy and not os.path.exists("scripts/kconfig/conf"):
print("\nscripts/kconfig/conf does not exist -- running "
"'make allnoconfig' to build it...")
shell("make allnoconfig")
@@ -2168,15 +1661,15 @@ def run_compatibility_tests():
# tree -- currently, all tests. The boolean flag indicates whether .config
# (generated by the C implementation) should be compared to ._config
# (generated by us) after each invocation.
- all_arch_tests = [(test_load, False),
- (test_config_absent, True),
- (test_call_all, False),
- (test_all_no, True),
- (test_all_yes, True),
- (test_all_no_simpler, True),
+ all_arch_tests = ((test_load, False),
+ (test_alldefconfig, True),
# Needs to report success/failure for each arch/defconfig
# combo, hence False.
- (test_defconfig, False)]
+ (test_defconfig, False),
+ (test_sanity, False),
+ (test_all_no, True),
+ (test_all_no_simpler, True),
+ (test_all_yes, True))
arch_srcarch_list = get_arch_srcarch_list()
@@ -2193,29 +1686,31 @@ def run_compatibility_tests():
# Previously we used to load all the arches once and keep them
# around for the tests. That now uses a huge amount of memory (pypy
# helps a bit), so reload them for each test instead.
- test_fn(kconfiglib.Config(base_dir="."))
+ test_fn(Kconfig(), arch)
# Let kbuild infer SRCARCH from ARCH if we aren't in speedy mode.
# This could detect issues with the test suite.
- if not speedy_mode:
+ if not speedy:
del os.environ["SRCARCH"]
if compare_configs:
if equal_confs():
- print(" {:14}OK".format(arch))
+ print("{:14}OK".format(arch))
else:
- print(" {:14}FAIL".format(arch))
+ print("{:14}FAIL".format(arch))
fail()
- if all_ok():
+ if all_passed:
print("All selftests and compatibility tests passed")
- print(nconfigs, "arch/defconfig pairs tested")
+ print("{} arch/defconfig pairs tested".format(nconfigs))
else:
print("Some tests failed")
+ sys.exit(1)
def get_arch_srcarch_list():
- """Returns a list of (ARCH, SRCARCH) tuples to test."""
-
+ """
+ Returns a list of (ARCH, SRCARCH) tuples to test.
+ """
res = []
def add_arch(arch):
@@ -2240,203 +1735,204 @@ def get_arch_srcarch_list():
return res
-def test_load(conf):
- """Load all arch Kconfigs to make sure we don't throw any errors"""
- print(" {:14}OK".format(conf.get_arch()))
+def test_load(conf, arch):
+ """
+ Load all arch Kconfigs to make sure we don't throw any errors
+ """
+ print("{:14}OK".format(arch))
-# The weird docstring formatting is to get the format right when we print the
-# docstring ourselves
-def test_all_no(conf):
+def test_all_no(conf, arch):
"""
Verify that our examples/allnoconfig.py script generates the same .config
as 'make allnoconfig', for each architecture. Runs the script via
- 'make scriptconfig', so kinda slow even in speedy mode."""
+ 'make scriptconfig', so kinda slow even in speedy mode.
+ """
# TODO: Support speedy mode for running the script
shell("make scriptconfig SCRIPT=Kconfiglib/examples/allnoconfig.py "
"PYTHONCMD='{}'".format(sys.executable))
shell("mv .config ._config")
- if speedy_mode:
+ if speedy:
shell("scripts/kconfig/conf --allnoconfig Kconfig")
else:
shell("make allnoconfig")
-def test_all_no_simpler(conf):
+def test_all_no_simpler(conf, arch):
"""
Verify that our examples/allnoconfig_simpler.py script generates the same
.config as 'make allnoconfig', for each architecture. Runs the script via
- 'make scriptconfig', so kinda slow even in speedy mode."""
+ 'make scriptconfig', so kinda slow even in speedy mode.
+ """
# TODO: Support speedy mode for running the script
shell("make scriptconfig SCRIPT=Kconfiglib/examples/allnoconfig_simpler.py "
"PYTHONCMD='{}'".format(sys.executable))
shell("mv .config ._config")
- if speedy_mode:
+ if speedy:
shell("scripts/kconfig/conf --allnoconfig Kconfig")
else:
shell("make allnoconfig")
-def test_all_yes(conf):
+def test_all_yes(conf, arch):
"""
Verify that our examples/allyesconfig.py script generates the same .config
as 'make allyesconfig', for each architecture. Runs the script via
- 'make scriptconfig', so kinda slow even in speedy mode."""
+ 'make scriptconfig', so kinda slow even in speedy mode.
+ """
# TODO: Support speedy mode for running the script
shell("make scriptconfig SCRIPT=Kconfiglib/examples/allyesconfig.py "
"PYTHONCMD='{}'".format(sys.executable))
shell("mv .config ._config")
- if speedy_mode:
+ if speedy:
shell("scripts/kconfig/conf --allyesconfig Kconfig")
else:
shell("make allyesconfig")
-def test_call_all(conf):
+def test_sanity(conf, arch):
"""
- Call all public methods on all symbols, menus, choices, and comments for
- all architectures to make sure we never crash or hang. (Nearly all public
- methods: some are hard to test like this, but are exercised by other
- tests.) Also do misc. sanity checks."""
- print(" For {}...".format(conf.get_arch()))
-
- conf.__str__()
- conf.get_arch()
- conf.get_base_dir()
- conf.get_config_filename()
- conf.get_config_header()
- conf.get_defconfig_filename()
- conf.get_kconfig_filename()
- conf.get_mainmenu_text()
- conf.get_srcarch()
- conf.get_srctree()
- conf.get_symbol("y")
- conf.get_symbols(False)
- conf.get_top_level_items()
- conf.set_print_undef_assign(True)
- conf.set_print_undef_assign(False)
- conf.set_print_warnings(False)
- conf.set_print_warnings(True)
- conf.unset_user_values()
-
- conf.eval("y && ARCH")
-
- for s in conf.get_symbols():
- s.__str__()
- s.get_assignable_values()
- s.get_config()
- s.get_help()
- s.get_implied_symbols()
- s.get_lower_bound()
- s.get_name()
- s.get_parent()
- s.get_prompts()
- s.get_ref_locations()
- s.get_referenced_symbols()
- s.get_referenced_symbols(True)
- s.get_selected_symbols()
- s.get_type()
- s.get_upper_bound()
- s.get_user_value()
- s.get_value()
- s.get_visibility()
- s.has_ranges()
- s.is_choice_selection()
- s.is_choice_symbol()
- s.is_defined()
- s.is_from_environment()
- s.is_modifiable()
- s.is_allnoconfig_y()
- s.unset_user_value()
-
- # Check get_ref/def_location() sanity
-
- if s.is_special():
- if s.is_from_environment():
- # Special symbols from the environment should have define
- # locations
- verify(s.get_def_locations() != [],
- "The symbol '{}' is from the environment but lacks "
- "define locations".format(s.get_name()))
- else:
- # Special symbols that are not from the environment should be
- # defined and have no define locations
- verify(s.is_defined(),
- "The special symbol '{}' is not defined".
- format(s.get_name()))
- verify(s.get_def_locations() == [],
- "The special symbol '{}' has recorded def. locations".
- format(s.get_name()))
+ Do sanity checks on each configuration and call all public methods on all
+ symbols, choices, and menu nodes for all architectures to make sure we
+ never crash or hang.
+ """
+ print("For {}...".format(arch))
+
+ conf.modules
+ conf.defconfig_list
+ conf.defconfig_filename
+ conf.enable_undef_warnings()
+ conf.disable_undef_warnings()
+ conf.enable_warnings()
+ conf.disable_warnings()
+ conf.mainmenu_text
+ conf.unset_values()
+
+ # Python 2/3 compatible
+ for key, sym in conf.syms.items():
+ verify(isinstance(key, str), "weird key '{}' in syms dict".format(key))
+
+ if sym.name != "UNAME_RELEASE":
+ verify(not sym.is_constant, sym.name + " in 'syms' and constant")
+
+ verify(sym not in conf.const_syms,
+ sym.name + " in both 'syms' and 'const_syms'")
+
+ for dep in sym._dependents:
+ verify(not dep.is_constant,
+ "the constant symbol {} depends on {}"
+ .format(dep.name, sym.name))
+
+ sym.__repr__()
+ sym.__str__()
+ sym.assignable
+ conf.disable_warnings()
+ sym.set_value(2)
+ sym.set_value("foo")
+ sym.unset_value()
+ conf.enable_warnings()
+ sym.str_value
+ sym.tri_value
+ sym.type
+ sym.user_value
+ sym.visibility
+
+ for sym in conf.defined_syms:
+ verify(sym.nodes, sym.name + " is defined but lacks menu nodes")
+
+ verify(not (sym.orig_type not in (BOOL, TRISTATE) and sym.choice),
+ sym.name + " is a choice symbol but not bool/tristate")
+
+ for key, sym in conf.const_syms.items():
+ verify(isinstance(key, str),
+ "weird key '{}' in const_syms dict".format(key))
+
+ verify(sym.is_constant,
+ '"{}" is in const_syms but not marked constant'
+ .format(sym.name))
+
+ verify(not sym.nodes,
+ '"{}" is constant but has menu nodes'.format(sym.name))
+
+ verify(not sym._dependents,
+ '"{}" is constant but is a dependency of some symbol'
+ .format(sym.name))
+
+ verify(not sym.choice,
+ '"{}" is constant and a choice symbol'.format(sym.name))
+
+ sym.__repr__()
+ sym.__str__()
+ sym.assignable
+ conf.disable_warnings()
+ sym.set_value(2)
+ sym.set_value("foo")
+ sym.unset_value()
+ conf.enable_warnings()
+ sym.str_value
+ sym.tri_value
+ sym.type
+ sym.visibility
+
+ # Cheat with internals
+ for choice in conf._choices:
+ for sym in choice.syms:
+ verify(sym.choice is choice,
+ "{0} is in choice.syms but 'sym.choice' is not the choice"
+ .format(sym.name))
+
+ verify(sym.type in (BOOL, TRISTATE),
+ "{} is a choice symbol but is not a bool/tristate"
+ .format(sym.name))
+
+ choice.__str__()
+ choice.__repr__()
+ choice.str_value
+ choice.tri_value
+ choice.user_value
+ choice.assignable
+ choice.selection
+ choice.type
+ choice.visibility
+
+ # Menu nodes
+
+ node = conf.top_node
+
+ while 1:
+ # Everything else should be well exercised elsewhere
+ node.__repr__()
+ node.__str__()
+ verify(isinstance(node.item, (Symbol, Choice)) or \
+ node.item in (MENU, COMMENT),
+ "'{}' appeared as a menu item".format(node.item))
+
+ if node.list is not None:
+ node = node.list
+
+ elif node.next is not None:
+ node = node.next
+
else:
- # Non-special symbols should have define locations iff they are
- # defined
- if s.is_defined():
- verify(s.get_def_locations() != [],
- "'{}' defined but lacks recorded locations".
- format(s.get_name()))
+ while node.parent is not None:
+ node = node.parent
+ if node.next is not None:
+ node = node.next
+ break
else:
- verify(s.get_def_locations() == [],
- "'{}' undefined but has recorded locations".
- format(s.get_name()))
- verify(s.get_ref_locations() != [],
- "'{}' both undefined and unreferenced".
- format(s.get_name()))
-
- for c in conf.get_choices():
- c.__str__()
- c.get_config()
- c.get_def_locations()
- c.get_help()
- c.get_items()
- c.get_mode()
- c.get_name()
- c.get_parent()
- c.get_prompts()
- c.get_referenced_symbols()
- c.get_referenced_symbols(True)
- c.get_selection()
- c.get_selection_from_defaults()
- c.get_symbols()
- c.get_type()
- c.get_user_selection()
- c.get_visibility()
- c.is_optional()
-
- for m in conf.get_menus():
- m.__str__()
- m.get_config()
- m.get_items()
- m.get_items(True)
- m.get_location()
- m.get_parent()
- m.get_referenced_symbols()
- m.get_referenced_symbols(True)
- m.get_symbols()
- m.get_symbols(True)
- m.get_title()
- m.get_visibility()
- m.get_visible_if_visibility()
-
- for c in conf.get_comments():
- c.__str__()
- c.get_config()
- c.get_location()
- c.get_parent()
- c.get_referenced_symbols()
- c.get_referenced_symbols(True)
- c.get_text()
- c.get_visibility()
-
-def test_config_absent(conf):
+ break
+
+def test_alldefconfig(conf, arch):
"""
Verify that Kconfiglib generates the same .config as 'make alldefconfig',
- for each architecture"""
+ for each architecture
+ """
conf.write_config("._config")
- if speedy_mode:
+ if speedy:
shell("scripts/kconfig/conf --alldefconfig Kconfig")
else:
shell("make alldefconfig")
-def test_defconfig(conf):
+def test_defconfig(conf, arch):
"""
Verify that Kconfiglib generates the same .config as scripts/kconfig/conf,
for each architecture/defconfig pair. In obsessive mode, this test includes
@@ -2445,38 +1941,43 @@ def test_defconfig(conf):
run.
With logging enabled, this test appends any failures to a file
- test_defconfig_fails in the root."""
+ test_defconfig_fails in the root.
+ """
global nconfigs
defconfigs = []
- def add_configs_for_arch(arch):
- arch_dir = os.path.join("arch", arch)
+ def add_configs_for_arch(arch_):
+ arch_dir = os.path.join("arch", arch_)
+
# Some arches have a "defconfig" in the root of their arch/<arch>/
# directory
root_defconfig = os.path.join(arch_dir, "defconfig")
if os.path.exists(root_defconfig):
defconfigs.append(root_defconfig)
+
# Assume all files in the arch/<arch>/configs directory (if it
# exists) are configurations
defconfigs_dir = os.path.join(arch_dir, "configs")
if not os.path.exists(defconfigs_dir):
return
+
if not os.path.isdir(defconfigs_dir):
print("Warning: '{}' is not a directory - skipping"
.format(defconfigs_dir))
return
+
for dirpath, _, filenames in os.walk(defconfigs_dir):
for filename in filenames:
defconfigs.append(os.path.join(dirpath, filename))
- if obsessive_mode:
+ if obsessive:
# Collect all defconfigs. This could be done once instead, but it's
# a speedy operation comparatively.
- for arch in os.listdir("arch"):
- add_configs_for_arch(arch)
+ for arch_ in os.listdir("arch"):
+ add_configs_for_arch(arch_)
else:
- add_configs_for_arch(conf.get_arch())
+ add_configs_for_arch(arch)
# Test architecture for each defconfig
@@ -2487,7 +1988,7 @@ def test_defconfig(conf):
conf.load_config(defconfig)
conf.write_config("._config")
- if speedy_mode:
+ if speedy:
shell("scripts/kconfig/conf --defconfig='{}' Kconfig".
format(defconfig))
else:
@@ -2500,34 +2001,29 @@ def test_defconfig(conf):
# bugs.
shell("make kconfiglibtestconfig")
- arch_defconfig_str = " {:14}with {:60} " \
- .format(conf.get_arch(), defconfig)
+ arch_defconfig_str = " {:14}with {:60} ".format(arch, defconfig)
if equal_confs():
print(arch_defconfig_str + "OK")
else:
print(arch_defconfig_str + "FAIL")
fail()
- if log_mode:
+ if log:
with open("test_defconfig_fails", "a") as fail_log:
fail_log.write("{} {} with {} did not match\n"
.format(time.strftime("%d %b %Y %H:%M:%S",
time.localtime()),
- conf.get_arch(),
- defconfig))
+ arch, defconfig))
#
# Helper functions
#
-devnull = open(os.devnull, "w")
-
-def shell(cmd):
- subprocess.call(cmd, shell = True, stdout = devnull, stderr = devnull)
-
def rm_configs():
- """Delete any old ".config" (generated by the C implementation) and
- "._config" (generated by us), if present."""
+ """
+ Delete any old ".config" (generated by the C implementation) and
+ "._config" (generated by us), if present.
+ """
def rm_if_exists(f):
if os.path.exists(f):
os.remove(f)
@@ -2557,7 +2053,8 @@ def equal_confs():
return False
else:
with f:
- our = f.readlines()
+ # [1:] strips the default header
+ our = f.readlines()[1:]
if their == our:
return True
@@ -2569,26 +2066,5 @@ def equal_confs():
return False
-_all_ok = True
-
-def verify(cond, msg):
- """Fails and prints 'msg' if 'cond' is False."""
- if not cond:
- fail(msg)
-
-def verify_equals(x, y):
- """Fails if 'x' does not equal 'y'."""
- if x != y:
- fail("'{}' does not equal '{}'".format(x, y))
-
-def fail(msg = None):
- global _all_ok
- if msg is not None:
- print("Fail: " + msg)
- _all_ok = False
-
-def all_ok():
- return _all_ok
-
if __name__ == "__main__":
run_tests()