1.. contents:: Table of contents
2 :backlinks: none
3
4Overview
5--------
6
7Kconfiglib is a `Kconfig
8<https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt>`__
9implementation in Python 2/3. It started out as a helper library, but now has a
10enough functionality to also work well as a standalone Kconfig implementation
11(including `menuconfig interfaces <Menuconfig interfaces_>`_ and `Kconfig extensions`_).
12
13The entire library is contained in `kconfiglib.py
14<https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_. The
15bundled scripts are implemented on top of it. Implementing your own scripts
16should be relatively easy, if needed.
17
18Kconfiglib is used exclusively by e.g. the `Zephyr <https://www.zephyrproject.org/>`_
19and `ACRN <https://projectacrn.org/>`_ projects. It is also used for many small helper
20scripts in various projects.
21
22Since Kconfiglib is based around a library, it can be used e.g. to generate a
23`Kconfig cross-reference <https://docs.zephyrproject.org/latest/reference/kconfig/index.html>`_ (note: heavy page),
24using the same robust Kconfig parser used for other Kconfig tools, instead of brittle ad-hoc parsing. The documentation generation script can be found `here <https://github.com/zephyrproject-rtos/zephyr/blob/master/doc/scripts/genrest.py>`__.
25
26Kconfiglib implements the recently added `Kconfig preprocessor
27<https://github.com/torvalds/linux/blob/master/Documentation/kbuild/kconfig-macro-language.txt>`_.
28For backwards compatibility, environment variables can be referenced both as
29``$(FOO)`` (the new syntax) and as ``$FOO`` (the old syntax). The old syntax is
30deprecated, but will probably be supported for a very long time (the major
31version would be increased if support is ever dropped). Using the old syntax
32with an undefined environment variable keeps the string as is.
33
34Note: See `this issue <https://github.com/ulfalizer/Kconfiglib/issues/47>`_ if you run into
35a "macro expanded to blank string" error with kernel 4.18+.
36
37Installation
38------------
39
40Installation with pip
41~~~~~~~~~~~~~~~~~~~~~
42
43Kconfiglib is available on `PyPI <https://pypi.python.org/pypi/kconfiglib/>`_ and can be
44installed with e.g.
45
46.. code::
47
48 $ pip(3) install kconfiglib
49
50Microsoft Windows is supported.
51
52The ``pip`` installation will give you both the base library and the following
53executables. All but one mirror functionality available in the C tools.
54
55- `menuconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/menuconfig.py>`_
56
57- `oldconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/oldconfig.py>`_
58
59- `olddefconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/olddefconfig.py>`_
60
61- `alldefconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/alldefconfig.py>`_
62
63- `allnoconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/allnoconfig.py>`_
64
65- `allmodconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/allmodconfig.py>`_
66
67- `allyesconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/allyesconfig.py>`_
68
69- `genconfig <https://github.com/ulfalizer/Kconfiglib/blob/master/genconfig.py>`_
70
71``genconfig`` is intended to be run at build time. It generates a C header from
72the configuration and (optionally) information that can be used to rebuild only
73files that reference Kconfig symbols that have changed value.
74
75The ``menuconfig`` implementation requires Python 3. It uses ``get_wch()``,
76which is needed for Unicode input support. Unfortunately, ``get_wch()`` isn't
77available in the Python 2 version of the standard ``curses`` module.
78
79**Note:** If you install Kconfiglib with ``pip``'s ``--user`` flag, make sure
80that your ``PATH`` includes the directory where the executables end up. You can
81list the installed files with ``pip(3) show -f kconfiglib``.
82
83All releases have a corresponding tag in the git repository, e.g. ``v10.21.0``.
84(the latest version).
85
86`Semantic versioning <http://semver.org/>`_ is used. There's been
87eight small changes (`1 <https://github.com/ulfalizer/Kconfiglib/commit/e8b4ecb6ff6ccc1c7be0818314fbccda2ef2b2ee>`_,
88`2 <https://github.com/ulfalizer/Kconfiglib/commit/db633015a4d7b0ba1e882f665e191f350932b2af>`_,
89`3 <https://github.com/ulfalizer/Kconfiglib/commit/8983f7eb297dd614faf0beee3129559bc8ba338e>`_,
90`4 <https://github.com/ulfalizer/Kconfiglib/commit/cbf32e29a130d22bc734b7778e6304ac9df2a3e8>`_,
91`5 <https://github.com/ulfalizer/Kconfiglib/commit/eb6c21a9b33a2d6e2bed9882d4f930d0cab2f03b>`_,
92`6 <https://github.com/ulfalizer/Kconfiglib/commit/c19fc11355b13d75d97286402c7a933fb23d3b70>`_,
93`7 <https://github.com/ulfalizer/Kconfiglib/commit/7a428aa415606820a44291f475248b08e3952c4b>`_,
94`8 <https://github.com/ulfalizer/Kconfiglib/commit/f247ddf618ad29718e5efd3e69f8baf75d4d347b>`_)
95to the behavior of the API, which is why the major version is at 10 rather than
962. I do major version bumps for all behavior changes, even tiny ones, and most of these were
97fixes for baby issues in the early days of the Kconfiglib 2 API.
98
99Manual installation
100~~~~~~~~~~~~~~~~~~~
101
102Just drop ``kconfiglib.py`` and the scripts you want somewhere. There are no
103third-party dependencies (except for the `windows-curses
104<https://github.com/zephyrproject-rtos/windows-curses>`_ package on Windows,
105when running the terminal ``menuconfig`` implementation).
106
107Installation for the Linux kernel
108~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109
110See the module docstring at the top of `kconfiglib.py <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_.
111
112Getting started
113---------------
114
1151. `Install <Installation_>`_ the library and the utilities. Use ``pip3`` if
116 you want to use the terminal ``menuconfig``.
117
1182. Write `Kconfig
119 <https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt>`__
120 files that describe the available configuration options.
121
1223. Generate an initial configuration with e.g. ``menuconfig`` or
123 ``alldefconfig``. The configuration is saved as ``.config`` by default.
124
1254. Run ``genconfig`` to generate a header file. By default, it is saved as
126 ``config.h``.
127
128 Normally, ``genconfig`` would be run automatically as part of the build.
129
130 Adding new configuration output formats should be relatively straightforward.
131 See the implementation of ``write_config()`` in `kconfiglib.py
132 <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_.
133 The documentation for the ``Symbol.config_string`` property has some tips as
134 well.
135
1365. To update an old ``.config`` file after the Kconfig files have changed (e.g.
137 to add new options), run ``oldconfig`` (prompts for values for new options)
138 or ``olddefconfig`` (gives new options their default value).
139
140 Due to Kconfig semantics, simply loading an old ``.config`` file performs an
141 implicit ``olddefconfig``, so building will normally not be affected by
142 having an outdated configuration.
143
144Using ``.config`` files as Make input
145~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146
147``.config`` files use Make syntax and can be included directly in Makefiles to
148read configuration values from there. This is why ``n``-valued
149``bool``/``tristate`` values are written out as ``# CONFIG_FOO is not set`` (a
150Make comment) in ``.config``, allowing them to be tested with ``ifdef`` in
151Make.
152
153If you make use of this, you might want to pass ``--config-out <filename>`` to
154``genconfig`` and include the configuration file it generates instead of
155including ``.config`` directly. This has the advantage that the generated
156configuration file will always be a "full" configuration file, even if
157``.config`` is outdated. Otherwise, it might be necessary to run
158``old(def)config`` or ``menuconfig`` before rebuilding with an outdated
159``.config``.
160
161If you use ``--sync-deps`` to generate incremental build information, you can
162include ``deps/auto.conf`` instead, which is also a full configuration file.
163
164Useful helper macros
165~~~~~~~~~~~~~~~~~~~~
166
167The `include/linux/kconfig.h
168<https://github.com/torvalds/linux/blob/master/include/linux/kconfig.h>`_
169header in the Linux kernel defines some useful helper macros for testing
170Kconfig configuration values.
171
172``IS_ENABLED()`` is generally useful, allowing configuration values to be
173tested in ``if`` statements with no runtime overhead.
174
175Incremental building
176~~~~~~~~~~~~~~~~~~~~
177
178See the docstring for ``Kconfig.sync_deps()`` in `kconfiglib.py
179<https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_ for hints
180on implementing incremental builds (rebuilding just source files that reference
181changed configuration values).
182
183Running the ``scripts/basic/fixdep.c`` tool from the kernel on the output of
184``gcc -MD <source file>`` might give you an idea of how it all fits together.
185
186Library documentation
187---------------------
188
189Kconfiglib comes with extensive documentation in the form of docstrings. To view it, run e.g.
190the following command:
191
192.. code:: sh
193
194 $ pydoc(3) kconfiglib
195
196For HTML output, add ``-w``:
197
198.. code:: sh
199
200 $ pydoc(3) -w kconfiglib
201
202This will also work after installing Kconfiglib with ``pip(3)``.
203
204Documentation for the ``menuconfig`` interface can be viewed in the same way:
205
206.. code:: sh
207
208 $ pydoc3 menuconfig
209
210A good starting point for learning the library is to read the module docstring
211(which you could also just read directly at the beginning of `kconfiglib.py
212<https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_). It
213gives an introduction to symbol values, the menu tree, and expressions.
214
215After reading the module docstring, a good next step is to read the ``Kconfig``
216class documentation, and then the documentation for the ``Symbol``, ``Choice``,
217and ``MenuNode`` classes.
218
219Please tell me if something is unclear or can be explained better.
220
221Library features
222----------------
223
224Kconfiglib can do the following, among other things:
225
226- **Programmatically get and set symbol values**
227
228 See `allnoconfig.py
229 <https://github.com/ulfalizer/Kconfiglib/blob/master/allnoconfig.py>`_ and
230 `allyesconfig.py
231 <https://github.com/ulfalizer/Kconfiglib/blob/master/allyesconfig.py>`_,
232 which are automatically verified to produce identical output to the standard
233 ``make allnoconfig`` and ``make allyesconfig``.
234
235- **Read and write .config and defconfig files**
236
237 The generated ``.config`` and ``defconfig`` (minimal configuration) files are
238 character-for-character identical to what the C implementation would generate
239 (except for the header comment). The test suite relies on this, as it
240 compares the generated files.
241
242- **Write C headers**
243
244 The generated headers use the same format as ``include/generated/autoconf.h``
245 from the Linux kernel.
246
247- **Implement incremental builds**
248
249 This uses the same scheme as the ``include/config`` directory in the kernel:
250 Symbols are translated into files that are touched when the symbol's value
251 changes between builds, which can be used to avoid having to do a full
252 rebuild whenever the configuration is changed.
253
254 See the ``sync_deps()`` function for more information.
255
256- **Inspect symbols**
257
258 Printing a symbol or other item (which calls ``__str__()``) returns its
259 definition in Kconfig format. This also works for symbols defined in multiple
260 locations.
261
262 A helpful ``__repr__()`` is on all objects too.
263
264 All ``__str__()`` and ``__repr__()`` methods are deliberately implemented
265 with just public APIs, so all symbol information can be fetched separately as
266 well.
267
268- **Inspect expressions**
269
270 Expressions use a simple tuple-based format that can be processed manually
271 if needed. Expression printing and evaluation functions are provided,
272 implemented with public APIs.
273
274- **Inspect the menu tree**
275
276 The underlying menu tree is exposed, including submenus created implicitly
277 from symbols depending on preceding symbols. This can be used e.g. to
278 implement menuconfig-like functionality.
279
280 See `menuconfig.py
281 <https://github.com/ulfalizer/Kconfiglib/blob/master/menuconfig.py>`_ and the
282 minimalistic `menuconfig_example.py
283 <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/menuconfig_example.py>`_
284 example.
285
286Kconfig extensions
287~~~~~~~~~~~~~~~~~~
288
289The following Kconfig extensions are available:
290
291- ``source`` supports glob patterns and includes each matching file. A pattern
292 is required to match at least one file.
293
294 A separate ``osource`` statement is available for cases where it's okay for
295 the pattern to match no files (in which case ``osource`` turns into a no-op).
296
297- A relative ``source`` statement (``rsource``) is available, where file paths
298 are specified relative to the directory of the current Kconfig file. An
299 ``orsource`` statement is available as well, analogous to ``osource``.
300
301- Preprocessor user functions can be defined in Python, which makes it simple
302 to integrate information from existing Python tools into Kconfig (e.g. to
303 have Kconfig symbols depend on hardware information stored in some other
304 format).
305
306 See the *Kconfig extensions* section in the
307 `kconfiglib.py <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_
308 module docstring for more information.
309
310- ``def_int``, ``def_hex``, and ``def_string`` are available in addition to
311 ``def_bool`` and ``def_tristate``, allowing ``int``, ``hex``, and ``string``
312 symbols to be given a type and a default at the same time.
313
314 These can be useful in projects that make use of symbols defined in multiple
315 locations, and remove some Kconfig inconsistency.
316
317- Environment variables are expanded directly in e.g. ``source`` and
318 ``mainmenu`` statements, meaning ``option env`` symbols are redundant.
319
320 This is the standard behavior with the new `Kconfig preprocessor
321 <https://github.com/torvalds/linux/blob/master/Documentation/kbuild/kconfig-macro-language.txt>`_,
322 which Kconfiglib implements.
323
324 ``option env`` symbols are supported for backwards compatibility, with the
325 caveat that they must have the same name as the environment variables they
326 reference. A warning is printed if the names differ.
327
328- Two extra optional warnings can be enabled by setting environment variables,
329 covering cases that are easily missed when making changes to Kconfig files:
330
331 * ``KCONFIG_WARN_UNDEF``: If set to ``y``, warnings will be generated for all
332 references to undefined symbols within Kconfig files. The only gotcha is
333 that all hex literals must be prefixed with ``0x`` or ``0X``, to make it
334 possible to distinguish them from symbol references.
335
336 Some projects (e.g. the Linux kernel) use multiple Kconfig trees with many
337 shared Kconfig files, leading to some safe undefined symbol references.
338 ``KCONFIG_WARN_UNDEF`` is useful in projects that only have a single
339 Kconfig tree though.
340
341 ``KCONFIG_STRICT`` is an older alias for this environment variable,
342 supported for backwards compatibility.
343
344 * ``KCONFIG_WARN_UNDEF_ASSIGN``: If set to ``y``, warnings will be generated
345 for all assignments to undefined symbols within ``.config`` files. By
346 default, no such warnings are generated.
347
348 This warning can also be enabled/disabled via
349 ``Kconfig.enable/disable_undef_warnings()``.
350
351Other features
352--------------
353
354- **Single-file implementation**
355
356 The entire library is contained in `kconfiglib.py
357 <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`_.
358
359 The tools implemented on top of it are one file each.
360
361- **Runs unmodified under both Python 2 and Python 3**
362
363 The code mostly uses basic Python features and has no third-party
364 dependencies. The most advanced things used are probably ``@property`` and
365 ``__slots__``.
366
367- **Robust and highly compatible with the standard Kconfig C tools**
368
369 The `test suite <https://github.com/ulfalizer/Kconfiglib/blob/master/testsuite.py>`_
370 automatically compares output from Kconfiglib and the C tools
371 by diffing the generated ``.config`` files for the real kernel Kconfig and
372 defconfig files, for all ARCHes.
373
374 This currently involves comparing the output for 36 ARCHes and 498 defconfig
375 files (or over 18000 ARCH/defconfig combinations in "obsessive" test suite
376 mode). All tests are expected to pass.
377
378 A comprehensive suite of selftests is included as well.
379
380- **Not horribly slow despite being a pure Python implementation**
381
382 The `allyesconfig.py
383 <https://github.com/ulfalizer/Kconfiglib/blob/master/allyesconfig.py>`_
384 script currently runs in about 1.3 seconds on the Linux kernel on a Core i7
385 2600K (with a warm file cache), including the ``make`` overhead from ``make
386 scriptconfig``. Note that the Linux kernel Kconfigs are absolutely massive
387 (over 14k symbols for x86) compared to most projects.
388
389 Kconfiglib is especially speedy in cases where multiple ``.config`` files
390 need to be processed, because the ``Kconfig`` files will only need to be parsed
391 once.
392
393 For long-running jobs, `PyPy <https://pypy.org/>`_ gives a big performance
394 boost. CPython is faster for short-running jobs as PyPy needs some time to
395 warm up.
396
397 Kconfiglib also works well with the
398 `multiprocessing <https://docs.python.org/3/library/multiprocessing.html>`_
399 module. No global state is kept.
400
401- **Generates more warnings than the C implementation**
402
403 Generates the same warnings as the C implementation, plus additional ones.
404 Also detects dependency and ``source`` loops.
405
406 All warnings point out the location(s) in the ``Kconfig`` files where a
407 symbol is defined, where applicable.
408
409- **Unicode support**
410
411 Unicode characters in string literals in ``Kconfig`` and ``.config`` files are
412 correctly handled. This support mostly comes for free from Python.
413
414- **Windows support**
415
416 Nothing Linux-specific is used. Universal newlines mode is used for both
417 Python 2 and Python 3.
418
419 The `Zephyr <https://www.zephyrproject.org/>`_ project uses Kconfiglib to
420 generate ``.config`` files and C headers on Linux as well as Windows.
421
422- **Internals that (mostly) mirror the C implementation**
423
424 While being simpler to understand and tweak.
425
426Menuconfig interfaces
427---------------------
428
429Two configuration interfaces are currently available:
430
431- `menuconfig.py <https://github.com/ulfalizer/Kconfiglib/blob/master/menuconfig.py>`_
432 is a terminal-based configuration interface implemented using the standard
433 Python ``curses`` module. ``xconfig`` features like showing invisible symbols and
434 showing symbol names are included, and it's possible to jump directly to a symbol
435 in the menu tree (even if it's currently invisible).
436
437 .. image:: https://raw.githubusercontent.com/ulfalizer/Kconfiglib/screenshots/screenshots/menuconfig.gif
438
439 ``menuconfig.py`` currently only supports Python 3, mostly due to
440 ``curses.get_wch()`` not being available on Python 2. It is needed
441 for Unicode support.
442
443 There are no third-party dependencies on \*nix. On Windows,
444 the ``curses`` modules is not available by default, but support
445 can be added by installing the ``windows-curses`` package (which is
446 installed automatically when Kconfiglib is installed via ``pip``
447 on Windows):
448
449 .. code-block:: shell
450
451 $ pip install windows-curses
452
453 This uses wheels built from `this repository
454 <https://github.com/zephyrproject-rtos/windows-curses>`_, which is in turn
455 based on Christoph Gohlke's `Python Extension Packages for Windows
456 <https://www.lfd.uci.edu/~gohlke/pythonlibs/#curses>`_.
457
458 See the docstring at the top of `menuconfig.py
459 <https://github.com/ulfalizer/Kconfiglib/blob/master/menuconfig.py>`_ for
460 more information about the terminal menuconfig implementation.
461
462- `RomaVis <https://github.com/RomaVis>`_ has built a fully portable Python
463 2/3 `TkInter <https://wiki.python.org/moin/TkInter>`_ menuconfig
464 implementation. It is still a work-in-progress, but is already functional.
465
466 See the `pymenuconfig <https://github.com/RomaVis/pymenuconfig>`_ project
467 for more information.
468
469 Screenshot below:
470
471 .. image:: https://raw.githubusercontent.com/RomaVis/pymenuconfig/master/screenshot.PNG
472
473 While working on the terminal menuconfig implementation, I added a few APIs
474 to Kconfiglib that turned out to be handy. ``pymenuconfig`` predates the
475 terminal menuconfig, and so didn't have them available. Blame me for any
476 workarounds.
477
478Examples
479--------
480
481Example scripts
482~~~~~~~~~~~~~~~
483
484The `examples/ <https://github.com/ulfalizer/Kconfiglib/blob/master/examples>`_ directory contains some simple example scripts. Among these are the following ones. Make sure you run them with the latest version of Kconfiglib, as they might make use of newly added features.
485
486- `defconfig.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/defconfig.py>`_ has the same effect as going into ``make menuconfig`` and immediately saving and exiting.
487
488- `eval_expr.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/eval_expr.py>`_ evaluates an expression in the context of a configuration.
489
490- `find_symbol.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/find_symbol.py>`_ searches through expressions to find references to a symbol, also printing a "backtrace" with parents for each reference found.
491
492- `help_grep.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/help_grep.py>`_ searches for a string in all help texts.
493
494- `print_tree.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/print_tree.py>`_ prints a tree of all configuration items.
495
496- `print_config_tree.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/print_config_tree.py>`_ is similar to ``print_tree.py``, but dumps the tree as it would appear in ``menuconfig``, including values. This can be handy for visually diffing between ``.config`` files and different versions of ``Kconfig`` files.
497
498- `list_undefined.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/list_undefined.py>`_ finds references to symbols that are not defined by any architecture in the Linux kernel.
499
500- `merge_config.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/merge_config.py>`_ merges configuration fragments to produce a complete .config, similarly to ``scripts/kconfig/merge_config.sh`` from the kernel.
501
502- `menuconfig_example.py <https://github.com/ulfalizer/Kconfiglib/blob/master/examples/menuconfig_example.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.
503
504Real-world examples
505~~~~~~~~~~~~~~~~~~~
506
507- `kconfig.py <https://github.com/zephyrproject-rtos/zephyr/blob/master/scripts/kconfig/kconfig.py>`_ from the `Zephyr <https://www.zephyrproject.org/>`_ project handles ``.config`` and header file generation, also doing configuration fragment merging.
508
509- `genrest.py <https://github.com/zephyrproject-rtos/zephyr/blob/master/doc/scripts/genrest.py>`_ generates a Kconfig symbol cross-reference, which can be viewed `here <http://docs.zephyrproject.org/reference/kconfig/index.html>`__.
510
511- `Various utilities <https://github.com/projectacrn/acrn-hypervisor/tree/master/scripts/kconfig>`_ from the `ACRN <https://projectacrn.org/>`_ project.
512
513These 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):
514
515- `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 :).
516
517- `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.)
518
519- `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.
520
521- `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>`_.
522
523- `kconfig-diff.py <https://gist.github.com/dubiousjim/5638961>`_ -- a script by `dubiousjim <https://github.com/dubiousjim>`_ that compares kernel configurations.
524
525- 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.
526
527Sample ``make iscriptconfig`` session
528~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
529
530The following log should give some idea of the functionality available in the API:
531
532.. code-block::
533
534 $ make iscriptconfig
535 A Kconfig instance 'kconf' for the architecture x86 has been created.
536 >>> kconf # Calls Kconfig.__repr__()
537 <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>
538 >>> kconf.mainmenu_text # Expanded main menu text
539 'Linux/x86 4.14.0-rc7 Kernel Configuration'
540 >>> kconf.top_node # The implicit top-level menu
541 <menu node for menu, prompt "Linux/$ARCH $KERNELVERSION Kernel Configuration" (visibility y), deps y, 'visible if' deps y, has child, Kconfig:5>
542 >>> kconf.top_node.list # First child menu node
543 <menu node for symbol SRCARCH, deps y, has next, Kconfig:7>
544 >>> print(kconf.top_node.list) # Calls MenuNode.__str__()
545 config SRCARCH
546 string
547 option env="SRCARCH"
548 default "x86"
549
550 >>> sym = kconf.top_node.list.next.item # Item contained in next menu node
551 >>> print(sym) # Calls Symbol.__str__()
552 config 64BIT
553 bool
554 prompt "64-bit kernel" if ARCH = "x86"
555 default ARCH != "i386"
556 help
557 Say yes to build a 64-bit kernel - formerly known as x86_64
558 Say no to build a 32-bit kernel - formerly known as i386
559
560 >>> sym # Calls Symbol.__repr__()
561 <symbol 64BIT, bool, "64-bit kernel", value y, visibility y, direct deps y, arch/x86/Kconfig:2>
562 >>> sym.assignable # Currently assignable values (0, 1, 2 = n, m, y)
563 (0, 2)
564 >>> sym.set_value(0) # Set it to n
565 True
566 >>> sym.tri_value # Check the new value
567 0
568 >>> sym = kconf.syms["X86_MPPARSE"] # Look up symbol by name
569 >>> print(sym)
570 config X86_MPPARSE
571 bool
572 prompt "Enable MPS table" if (ACPI || SFI) && X86_LOCAL_APIC
573 default "y" if X86_LOCAL_APIC
574 help
575 For old smp systems that do not have proper acpi support. Newer systems
576 (esp with 64bit cpus) with acpi support, MADT and DSDT will override it
577
578 >>> default = sym.defaults[0] # Fetch its first default
579 >>> sym = default[1] # Fetch the default's condition (just a Symbol here)
580 >>> print(sym) # Print it. Dependencies are propagated to properties, like in the C implementation.
581 config X86_LOCAL_APIC
582 bool
583 default "y" if X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI
584 select IRQ_DOMAIN_HIERARCHY if X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI
585 select PCI_MSI_IRQ_DOMAIN if PCI_MSI && (X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI)
586
587 >>> sym.nodes # Show the MenuNode(s) associated with it
588 [<menu node for symbol X86_LOCAL_APIC, deps n, has next, arch/x86/Kconfig:1015>]
589 >>> kconfiglib.expr_str(sym.defaults[0][1]) # Print the default's condition
590 'X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI'
591 >>> kconfiglib.expr_value(sym.defaults[0][1]) # Evaluate it (0 = n)
592 0
593 >>> kconf.syms["64BIT"].set_value(2)
594 True
595 >>> kconfiglib.expr_value(sym.defaults[0][1]) # Evaluate it again (2 = y)
596 2
597 >>> kconf.write_config("myconfig") # Save a .config
598 >>> ^D
599 $ cat myconfig
600 # Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)
601 CONFIG_64BIT=y
602 CONFIG_X86_64=y
603 CONFIG_X86=y
604 CONFIG_INSTRUCTION_DECODER=y
605 CONFIG_OUTPUT_FORMAT="elf64-x86-64"
606 CONFIG_ARCH_DEFCONFIG="arch/x86/configs/x86_64_defconfig"
607 CONFIG_LOCKDEP_SUPPORT=y
608 CONFIG_STACKTRACE_SUPPORT=y
609 CONFIG_MMU=y
610 ...
611
612Test suite
613----------
614
615The test suite is run with
616
617.. code::
618
619 $ python(3) Kconfiglib/testsuite.py
620
621`pypy <https://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
622the scripts are run via ``make scriptconfig``.
623
624The test suite must be run from the top-level kernel directory. It requires that the
625Kconfiglib git repository has been cloned into it and that the makefile patch has been applied.
626
627To get rid of warnings generated for the kernel ``Kconfig`` files, add ``2>/dev/null`` to the command to
628discard ``stderr``.
629
630**NOTE: Forgetting to apply the Makefile patch will cause some tests that compare generated configurations to fail**
631
632**NOTE: The test suite overwrites .config in the kernel root, so make sure to back it up.**
633
634The test suite consists of a set of selftests and a set of compatibility tests that
635compare configurations generated by Kconfiglib with
636configurations generated by the C tools, for a number of cases. See
637`testsuite.py <https://github.com/ulfalizer/Kconfiglib/blob/master/testsuite.py>`_
638for the available options.
639
640The `tests/reltest <https://github.com/ulfalizer/Kconfiglib/blob/master/tests/reltest>`_ script runs the test suite
641and all the example scripts for both Python 2 and Python 3, verifying that everything works.
642
643Rarely, the output from the C tools is changed slightly (most recently due to a
644`change <https://www.spinics.net/lists/linux-kbuild/msg17074.html>`_ I added).
645If you get test suite failures, try running the test suite again against the
646`linux-next tree <https://www.kernel.org/doc/man-pages/linux-next.html>`_,
647which has all the latest changes. I will make it clear if any
648non-backwards-compatible changes appear.
649
650A lot of time is spent waiting around for ``make`` and the C utilities (which need to reparse all the
651Kconfig files for each defconfig test). Adding some multiprocessing to the test suite would make sense
652too.
653
654Notes
655-----
656
657* This is version 2 of Kconfiglib, which is not backwards-compatible with
658 Kconfiglib 1. For a summary of changes between Kconfiglib 1 and Kconfiglib
659 2, see `kconfiglib-2-changes.txt
660 <https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib-2-changes.txt>`_.
661
662* I sometimes see people add custom output formats, which is pretty
663 straightforward to do (see the implementations of ``write_autoconf()`` and
664 ``write_config()`` for a template, and also the documentation of the
665 ``Symbol.config_string`` property). If you come up with something you think
666 might be useful to other people, I'm happy to take it in upstream. Batteries
667 included and all that.
668
669* Kconfiglib assumes the modules symbol is ``MODULES``, which is backwards-compatible.
670 A warning is printed by default if ``option modules`` is set on some other symbol.
671
672 Let me know if you need proper ``option modules`` support. It wouldn't be that
673 hard to add.
674
675* The test suite failures (should be the only ones) for the following Blackfin
676 defconfigs on e.g. Linux 3.7.0-rc8 are due to
677 `a bug in the C implementation <https://lkml.org/lkml/2012/12/5/458>`_:
678
679 * ``arch/blackfin/configs/CM-BF537U_defconfig``
680 * ``arch/blackfin/configs/BF548-EZKIT_defconfig``
681 * ``arch/blackfin/configs/BF527-EZKIT_defconfig``
682 * ``arch/blackfin/configs/BF527-EZKIT-V2_defconfig``
683 * ``arch/blackfin/configs/TCM-BF537_defconfig``
684
685Thanks
686------
687
688- To `RomaVis <https://github.com/RomaVis>`_, for making
689 `pymenuconfig <https://github.com/RomaVis/pymenuconfig>`_ and suggesting
690 the ``rsource`` keyword.
691
692- To `Mitja Horvat <https://github.com/pinkfluid>`_, for adding support
693 for user-defined styles to the terminal menuconfig.
694
695- To `Philip Craig <https://github.com/philipc>`_ for adding
696 support for the ``allnoconfig_y`` option and fixing an obscure issue
697 with ``comment``\s inside ``choice``\s (that didn't affect correctness but
698 made outputs differ). ``allnoconfig_y`` is used to force certain symbols
699 to ``y`` during ``make allnoconfig`` to improve coverage.
700
701License
702-------
703
704See `LICENSE.txt <https://github.com/ulfalizer/Kconfiglib/blob/master/LICENSE.txt>`_. SPDX license identifiers are used in the
705source code.
706