1.. _kconfig_tips_and_tricks: 2 3Kconfig - Tips and Best Practices 4################################# 5 6This page covers some Kconfig best practices and explains some Kconfig 7behaviors and features that might be cryptic or that are easily overlooked. 8 9.. note:: 10 11 The official Kconfig documentation is `kconfig-language.rst 12 <https://www.kernel.org/doc/html/latest/kbuild/kconfig-language.html>`__ 13 and `kconfig-macro-language.rst 14 <https://www.kernel.org/doc/html/latest/kbuild/kconfig-macro-language.html>`__. 15 16.. contents:: 17 :local: 18 :depth: 2 19 20 21What to turn into Kconfig options 22********************************* 23 24When deciding whether something belongs in Kconfig, it helps to distinguish 25between symbols that have prompts and symbols that don't. 26 27If a symbol has a prompt (e.g. ``bool "Enable foo"``), then the user can change 28the symbol's value in the ``menuconfig`` or ``guiconfig`` interface (see 29:ref:`menuconfig`), or by manually editing configuration files. Conversely, a 30symbol without a prompt can never be changed directly by the user, not even by 31manually editing configuration files. 32 33Only put a prompt on a symbol if it makes sense for the user to change its 34value. 35 36Symbols without prompts are called *hidden* or *invisible* symbols, because 37they don't show up in ``menuconfig`` and ``guiconfig``. Symbols that have 38prompts can also be invisible, when their dependencies are not satisfied. 39 40Symbols without prompts can't be configured directly by the user (they derive 41their value from other symbols), so less restrictions apply to them. If some 42derived setting is easier to calculate in Kconfig than e.g. during the build, 43then do it in Kconfig, but keep the distinction between symbols with and 44without prompts in mind. 45 46See the `optional prompts`_ section for a way to deal with settings that are 47fixed on some machines and configurable on other machines. 48 49What not to turn into Kconfig options 50************************************* 51 52In Zephyr, Kconfig configuration is done after selecting a target board. In 53general, it does not make sense to use Kconfig for a value that corresponds to 54a fixed machine-specific setting. Usually, such settings should be handled via 55:ref:`devicetree <dt-guide>` instead. 56 57In particular, avoid adding new Kconfig options of the following types: 58 59Options that specify a device in the system by name 60=================================================== 61 62For example, if you are writing an I2C device driver, avoid creating an option 63named ``MY_DEVICE_I2C_BUS_NAME`` for specifying the bus node your device is 64controlled by. See :ref:`dt-drivers-that-depend` for alternatives. 65 66Similarly, if your application depends on a hardware-specific PWM device to 67control an RGB LED, avoid creating an option like ``MY_PWM_DEVICE_NAME``. See 68:ref:`dt-apps-that-depend` for alternatives. 69 70Options that specify fixed hardware configuration 71================================================= 72 73For example, avoid Kconfig options specifying a GPIO pin. 74 75An alternative applicable to device drivers is to define a GPIO specifier with 76type phandle-array in the device binding, and using the 77:ref:`devicetree-gpio-api` devicetree API from C. Similar advice applies to 78other cases where devicetree.h provides :ref:`devicetree-hw-api` for referring 79to other nodes in the system. Search the source code for drivers using these 80APIs for examples. 81 82An application-specific devicetree :ref:`binding <dt-bindings>` to identify 83board specific properties may be appropriate. See 84:zephyr_file:`tests/drivers/gpio/gpio_basic_api` for an example. 85 86For applications, see :zephyr:code-sample:`blinky` for a devicetree-based alternative. 87 88``select`` statements 89********************* 90 91The ``select`` statement is used to force one symbol to ``y`` whenever another 92symbol is ``y``. For example, the following code forces ``CONSOLE`` to ``y`` 93whenever ``USB_CONSOLE`` is ``y``: 94 95.. code-block:: kconfig 96 97 config CONSOLE 98 bool "Console support" 99 100 ... 101 102 config USB_CONSOLE 103 bool "USB console support" 104 select CONSOLE 105 106This section covers some pitfalls and good uses for ``select``. 107 108 109``select`` pitfalls 110=================== 111 112``select`` might seem like a generally useful feature at first, but can cause 113configuration issues if overused. 114 115For example, say that a new dependency is added to the ``CONSOLE`` symbol 116above, by a developer who is unaware of the ``USB_CONSOLE`` symbol (or simply 117forgot about it): 118 119.. code-block:: kconfig 120 121 config CONSOLE 122 bool "Console support" 123 depends on STRING_ROUTINES 124 125Enabling ``USB_CONSOLE`` now forces ``CONSOLE`` to ``y``, even if 126``STRING_ROUTINES`` is ``n``. 127 128To fix the problem, the ``STRING_ROUTINES`` dependency needs to be added to 129``USB_CONSOLE`` as well: 130 131.. code-block:: kconfig 132 133 config USB_CONSOLE 134 bool "USB console support" 135 select CONSOLE 136 depends on STRING_ROUTINES 137 138 ... 139 140 config STRING_ROUTINES 141 bool "Include string routines" 142 143More insidious cases with dependencies inherited from ``if`` and ``menu`` 144statements are common. 145 146An alternative attempt to solve the issue might be to turn the ``depends on`` 147into another ``select``: 148 149.. code-block:: kconfig 150 151 config CONSOLE 152 bool "Console support" 153 select STRING_ROUTINES 154 155 ... 156 157 config USB_CONSOLE 158 bool "USB console support" 159 select CONSOLE 160 161In practice, this often amplifies the problem, because any dependencies added 162to ``STRING_ROUTINES`` now need to be copied to both ``CONSOLE`` and 163``USB_CONSOLE``. 164 165In general, whenever the dependencies of a symbol are updated, the dependencies 166of all symbols that (directly or indirectly) select it have to be updated as 167well. This is very often overlooked in practice, even for the simplest case 168above. 169 170Chains of symbols selecting each other should be avoided in particular, except 171for simple helper symbols, as covered below in :ref:`good_select_use`. 172 173Liberal use of ``select`` also tends to make Kconfig files harder to read, both 174due to the extra dependencies and due to the non-local nature of ``select``, 175which hides ways in which a symbol might get enabled. 176 177 178Alternatives to ``select`` 179========================== 180 181For the example in the previous section, a better solution is usually to turn 182the ``select`` into a ``depends on``: 183 184.. code-block:: kconfig 185 186 config CONSOLE 187 bool "Console support" 188 189 ... 190 191 config USB_CONSOLE 192 bool "USB console support" 193 depends on CONSOLE 194 195This makes it impossible to generate an invalid configuration, and means that 196dependencies only ever have to be updated in a single spot. 197 198An objection to using ``depends on`` here might be that configuration files 199that enable ``USB_CONSOLE`` now also need to enable ``CONSOLE``: 200 201.. code-block:: cfg 202 203 CONFIG_CONSOLE=y 204 CONFIG_USB_CONSOLE=y 205 206This comes down to a trade-off, but if enabling ``CONSOLE`` is the norm, then a 207mitigation is to make ``CONSOLE`` default to ``y``: 208 209.. code-block:: kconfig 210 211 config CONSOLE 212 bool "Console support" 213 default y 214 215This gives just a single assignment in configuration files: 216 217.. code-block:: cfg 218 219 CONFIG_USB_CONSOLE=y 220 221Note that configuration files that do not want ``CONSOLE`` enabled now have to 222explicitly disable it: 223 224.. code-block:: cfg 225 226 CONFIG_CONSOLE=n 227 228 229.. _good_select_use: 230 231Using ``select`` for helper symbols 232=================================== 233 234A good and safe use of ``select`` is for setting "helper" symbols that capture 235some condition. Such helper symbols should preferably have no prompt or 236dependencies. 237 238For example, a helper symbol for indicating that a particular CPU/SoC has an 239FPU could be defined as follows: 240 241.. code-block:: kconfig 242 243 config CPU_HAS_FPU 244 bool 245 help 246 If y, the CPU has an FPU 247 248 ... 249 250 config SOC_FOO 251 bool "FOO SoC" 252 select CPU_HAS_FPU 253 254 ... 255 256 config SOC_BAR 257 bool "BAR SoC" 258 select CPU_HAS_FPU 259 260This makes it possible for other symbols to check for FPU support in a generic 261way, without having to look for particular architectures: 262 263.. code-block:: kconfig 264 265 config FPU 266 bool "Support floating point operations" 267 depends on CPU_HAS_FPU 268 269The alternative would be to have dependencies like the following, possibly 270duplicated in several spots: 271 272.. code-block:: kconfig 273 274 config FPU 275 bool "Support floating point operations" 276 depends on SOC_FOO || SOC_BAR || ... 277 278Invisible helper symbols can also be useful without ``select``. For example, 279the following code defines a helper symbol that has the value ``y`` if the 280machine has some arbitrarily-defined "large" amount of memory: 281 282.. code-block:: kconfig 283 284 config LARGE_MEM 285 def_bool MEM_SIZE >= 64 286 287.. note:: 288 289 This is short for the following: 290 291 .. code-block:: kconfig 292 293 config LARGE_MEM 294 bool 295 default MEM_SIZE >= 64 296 297 298``select`` recommendations 299========================== 300 301In summary, here are some recommended practices for ``select``: 302 303- Avoid selecting symbols with prompts or dependencies. Prefer ``depends on``. 304 If ``depends on`` causes annoying bloat in configuration files, consider 305 adding a Kconfig default for the most common value. 306 307 Rare exceptions might include cases where you're sure that the dependencies 308 of the selecting and selected symbol will never drift out of sync, e.g. when 309 dealing with two simple symbols defined close to one another within the same 310 ``if``. 311 312 Common sense applies, but be aware that ``select`` often causes issues in 313 practice. ``depends on`` is usually a cleaner and safer solution. 314 315- Select simple helper symbols without prompts and dependencies however much 316 you like. They're a great tool for simplifying Kconfig files. 317 318 319(Lack of) conditional includes 320****************************** 321 322``if`` blocks add dependencies to each item within the ``if``, as if ``depends 323on`` was used. 324 325A common misunderstanding related to ``if`` is to think that the following code 326conditionally includes the file :file:`Kconfig.other`: 327 328.. code-block:: kconfig 329 330 if DEP 331 source "Kconfig.other" 332 endif 333 334In reality, there are no conditional includes in Kconfig. ``if`` has no special 335meaning around a ``source``. 336 337.. note:: 338 339 Conditional includes would be impossible to implement, because ``if`` 340 conditions may contain (either directly or indirectly) forward references to 341 symbols that haven't been defined yet. 342 343Say that :file:`Kconfig.other` above contains this definition: 344 345.. code-block:: kconfig 346 347 config FOO 348 bool "Support foo" 349 350In this case, ``FOO`` will end up with this definition: 351 352.. code-block:: kconfig 353 354 config FOO 355 bool "Support foo" 356 depends on DEP 357 358Note that it is redundant to add ``depends on DEP`` to the definition of 359``FOO`` in :file:`Kconfig.other`, because the ``DEP`` dependency has already 360been added by ``if DEP``. 361 362In general, try to avoid adding redundant dependencies. They can make the 363structure of the Kconfig files harder to understand, and also make changes more 364error-prone, since it can be hard to spot that the same dependency is added 365twice. 366 367 368"Stuck" symbols in menuconfig and guiconfig 369******************************************* 370 371There is a common subtle gotcha related to interdependent configuration symbols 372with prompts. Consider these symbols: 373 374.. code-block:: kconfig 375 376 config FOO 377 bool "Foo" 378 379 config STACK_SIZE 380 hex "Stack size" 381 default 0x200 if FOO 382 default 0x100 383 384Assume that the intention here is to use a larger stack whenever ``FOO`` is 385enabled, and that the configuration initially has ``FOO`` disabled. Also, 386remember that Zephyr creates an initial configuration in :file:`zephyr/.config` 387in the build directory by merging configuration files (including e.g. 388:file:`prj.conf`). This configuration file exists before 389``menuconfig`` or ``guiconfig`` is run. 390 391When first entering the configuration interface, the value of ``STACK_SIZE`` is 3920x100, as expected. After enabling ``FOO``, you might reasonably expect the 393value of ``STACK_SIZE`` to change to 0x200, but it stays as 0x100. 394 395To understand what's going on, remember that ``STACK_SIZE`` has a prompt, 396meaning it is user-configurable, and consider that all Kconfig has to go on 397from the initial configuration is this: 398 399.. code-block:: cfg 400 401 CONFIG_STACK_SIZE=0x100 402 403Since Kconfig can't know if the 0x100 value came from a ``default`` or was 404typed in by the user, it has to assume that it came from the user. Since 405``STACK_SIZE`` is user-configurable, the value from the configuration file is 406respected, and any symbol defaults are ignored. This is why the value of 407``STACK_SIZE`` appears to be "frozen" at 0x100 when toggling ``FOO``. 408 409The right fix depends on what the intention is. Here's some different scenarios 410with suggestions: 411 412- If ``STACK_SIZE`` can always be derived automatically and does not need to be 413 user-configurable, then just remove the prompt: 414 415 .. code-block:: kconfig 416 417 config STACK_SIZE 418 hex 419 default 0x200 if FOO 420 default 0x100 421 422 Symbols without prompts ignore any value from the saved configuration. 423 424- If ``STACK_SIZE`` should usually be user-configurable, but needs to be set to 425 0x200 when ``FOO`` is enabled, then disable its prompt when ``FOO`` is 426 enabled, as described in `optional prompts`_: 427 428 .. code-block:: kconfig 429 430 config STACK_SIZE 431 hex "Stack size" if !FOO 432 default 0x200 if FOO 433 default 0x100 434 435- If ``STACK_SIZE`` should usually be derived automatically, but needs to be 436 set to a custom value in rare circumstances, then add another option for 437 making ``STACK_SIZE`` user-configurable: 438 439 .. code-block:: kconfig 440 441 config CUSTOM_STACK_SIZE 442 bool "Use a custom stack size" 443 help 444 Enable this if you need to use a custom stack size. When disabled, a 445 suitable stack size is calculated automatically. 446 447 config STACK_SIZE 448 hex "Stack size" if CUSTOM_STACK_SIZE 449 default 0x200 if FOO 450 default 0x100 451 452 As long as ``CUSTOM_STACK_SIZE`` is disabled, ``STACK_SIZE`` will ignore the 453 value from the saved configuration. 454 455It is a good idea to try out changes in the ``menuconfig`` or ``guiconfig`` 456interface, to make sure that things behave the way you expect. This is 457especially true when making moderately complex changes like these. 458 459 460Assignments to promptless symbols in configuration files 461******************************************************** 462 463Assignments to hidden (promptless, also called *invisible*) symbols in 464configuration files are always ignored. Hidden symbols get their value 465indirectly from other symbols, via e.g. ``default`` and ``select``. 466 467A common source of confusion is opening the output configuration file 468(:file:`zephyr/.config`), seeing a bunch of assignments to hidden symbols, 469and assuming that those assignments must be respected when the configuration is 470read back in by Kconfig. In reality, all assignments to hidden symbols in 471:file:`zephyr/.config` are ignored by Kconfig, like for other configuration 472files. 473 474To understand why :file:`zephyr/.config` still includes assignments to hidden 475symbols, it helps to realize that :file:`zephyr/.config` serves two separate 476purposes: 477 4781. It holds the saved configuration, and 479 4802. it holds configuration output. :file:`zephyr/.config` is parsed by the CMake 481 files to let them query configuration settings, for example. 482 483The assignments to hidden symbols in :file:`zephyr/.config` are just 484configuration output. Kconfig itself ignores assignments to hidden symbols when 485calculating symbol values. 486 487.. note:: 488 489 A *minimal configuration*, which can be generated from within the 490 :ref:`menuconfig and guiconfig interfaces <menuconfig>`, could be considered 491 closer to just a saved configuration, without the full configuration output. 492 493 494``depends on`` and ``string``/``int``/``hex`` symbols 495***************************************************** 496 497``depends on`` works not just for ``bool`` symbols, but also for ``string``, 498``int``, and ``hex`` symbols (and for choices). 499 500The Kconfig definitions below will hide the ``FOO_DEVICE_FREQUENCY`` symbol and 501disable any configuration output for it when ``FOO_DEVICE`` is disabled. 502 503.. code-block:: kconfig 504 505 config FOO_DEVICE 506 bool "Foo device" 507 508 config FOO_DEVICE_FREQUENCY 509 int "Foo device frequency" 510 depends on FOO_DEVICE 511 512In general, it's a good idea to check that only relevant symbols are ever shown 513in the ``menuconfig``/``guiconfig`` interface. Having ``FOO_DEVICE_FREQUENCY`` 514show up when ``FOO_DEVICE`` is disabled (and possibly hidden) makes the 515relationship between the symbols harder to understand, even if code never looks 516at ``FOO_DEVICE_FREQUENCY`` when ``FOO_DEVICE`` is disabled. 517 518 519``menuconfig`` symbols 520********************** 521 522If the definition of a symbol ``FOO`` is immediately followed by other symbols 523that depend on ``FOO``, then those symbols become children of ``FOO``. If 524``FOO`` is defined with ``config FOO``, then the children are shown indented 525relative to ``FOO``. Defining ``FOO`` with ``menuconfig FOO`` instead puts the 526children in a separate menu rooted at ``FOO``. 527 528``menuconfig`` has no effect on evaluation. It's just a display option. 529 530``menuconfig`` can cut down on the number of menus and make the menu structure 531easier to navigate. For example, say you have the following definitions: 532 533.. code-block:: kconfig 534 535 menu "Foo subsystem" 536 537 config FOO_SUBSYSTEM 538 bool "Foo subsystem" 539 540 if FOO_SUBSYSTEM 541 542 config FOO_FEATURE_1 543 bool "Foo feature 1" 544 545 config FOO_FEATURE_2 546 bool "Foo feature 2" 547 548 config FOO_FREQUENCY 549 int "Foo frequency" 550 551 ... lots of other FOO-related symbols 552 553 endif # FOO_SUBSYSTEM 554 555 endmenu 556 557In this case, it's probably better to get rid of the ``menu`` and turn 558``FOO_SUBSYSTEM`` into a ``menuconfig`` symbol: 559 560.. code-block:: kconfig 561 562 menuconfig FOO_SUBSYSTEM 563 bool "Foo subsystem" 564 565 if FOO_SUBSYSTEM 566 567 config FOO_FEATURE_1 568 bool "Foo feature 1" 569 570 config FOO_FEATURE_2 571 bool "Foo feature 2" 572 573 config FOO_FREQUENCY 574 int "Foo frequency" 575 576 ... lots of other FOO-related symbols 577 578 endif # FOO_SUBSYSTEM 579 580In the ``menuconfig`` interface, this will be displayed as follows: 581 582.. code-block:: none 583 584 [*] Foo subsystem ---> 585 586Note that making a symbol without children a ``menuconfig`` is meaningless. It 587should be avoided, because it looks identical to a symbol with all children 588invisible: 589 590.. code-block:: none 591 592 [*] I have no children ---- 593 [*] All my children are invisible ---- 594 595 596Commas in macro arguments 597************************* 598 599Kconfig uses commas to separate macro arguments. 600This means a construct like this will fail: 601 602.. code-block:: kconfig 603 604 config FOO 605 bool 606 default y if $(dt_chosen_enabled,"zephyr,bar") 607 608To solve this problem, create a variable with the text and use this variable as 609argument, as follows: 610 611.. code-block:: kconfig 612 613 DT_CHOSEN_ZEPHYR_BAR := zephyr,bar 614 615 config FOO 616 bool 617 default y if $(dt_chosen_enabled,$(DT_CHOSEN_ZEPHYR_BAR)) 618 619 620Checking changes in menuconfig/guiconfig 621**************************************** 622 623When adding new symbols or making other changes to Kconfig files, it is a good 624idea to look up the symbols in :ref:`menuconfig or guiconfig <menuconfig>` 625afterwards. To get to a symbol quickly, use the jump-to feature (press 626:kbd:`/`). 627 628Here are some things to check: 629 630* Are the symbols placed in a good spot? Check that they appear in a menu where 631 they make sense, close to related symbols. 632 633 If one symbol depends on another, then it's often a good idea to place it 634 right after the symbol it depends on. It will then be shown indented relative 635 to the symbol it depends on in the ``menuconfig`` interface, and in a 636 separate menu rooted at the symbol in ``guiconfig``. This also works if 637 several symbols are placed after the symbol they depend on. 638 639* Is it easy to guess what the symbols do from their prompts? 640 641* If many symbols are added, do all combinations of values they can be set to 642 make sense? 643 644 For example, if two symbols ``FOO_SUPPORT`` and ``NO_FOO_SUPPORT`` are added, 645 and both can be enabled at the same time, then that makes a nonsensical 646 configuration. In this case, it's probably better to have a single 647 ``FOO_SUPPORT`` symbol. 648 649* Are there any duplicated dependencies? 650 651 This can be checked by selecting a symbol and pressing :kbd:`?` to view the 652 symbol information. If there are duplicated dependencies, then use the 653 ``Included via ...`` path shown in the symbol information to figure out where 654 they come from. 655 656 657Checking changes with :file:`scripts/kconfig/lint.py` 658***************************************************** 659 660After you make Kconfig changes, you can use the 661:zephyr_file:`scripts/kconfig/lint.py` script to check for some potential 662issues, like unused symbols and symbols that are impossible to enable. Use 663``--help`` to see available options. 664 665Some checks are necessarily a bit heuristic, so a symbol being flagged by a 666check does not necessarily mean there's a problem. If a check returns a false 667positive e.g. due to token pasting in C (``CONFIG_FOO_##index##_BAR``), just 668ignore it. 669 670When investigating an unknown symbol ``FOO_BAR``, it is a good idea to run 671``git grep FOO_BAR`` to look for references. It is also a good idea to search 672for some components of the symbol name with e.g. ``git grep FOO`` and 673``git grep BAR``, as it can help uncover token pasting. 674 675 676Style recommendations and shorthands 677************************************ 678 679This section gives some style recommendations and explains some common Kconfig 680shorthands. 681 682 683Factoring out common dependencies 684================================= 685 686If a sequence of symbols/choices share a common dependency, the dependency can 687be factored out with an ``if``. 688 689As an example, consider the following code: 690 691.. code-block:: kconfig 692 693 config FOO 694 bool "Foo" 695 depends on DEP 696 697 config BAR 698 bool "Bar" 699 depends on DEP 700 701 choice 702 prompt "Choice" 703 depends on DEP 704 705 config BAZ 706 bool "Baz" 707 708 config QAZ 709 bool "Qaz" 710 711 endchoice 712 713Here, the ``DEP`` dependency can be factored out like this: 714 715.. code-block:: kconfig 716 717 if DEP 718 719 config FOO 720 bool "Foo" 721 722 config BAR 723 bool "Bar" 724 725 choice 726 prompt "Choice" 727 728 config BAZ 729 bool "Baz" 730 731 config QAZ 732 bool "Qaz" 733 734 endchoice 735 736 endif # DEP 737 738.. note:: 739 740 Internally, the second version of the code is transformed into the first. 741 742If a sequence of symbols/choices with shared dependencies are all in the same 743menu, the dependency can be put on the menu itself: 744 745.. code-block:: kconfig 746 747 menu "Foo features" 748 depends on FOO_SUPPORT 749 750 config FOO_FEATURE_1 751 bool "Foo feature 1" 752 753 config FOO_FEATURE_2 754 bool "Foo feature 2" 755 756 endmenu 757 758If ``FOO_SUPPORT`` is ``n``, the entire menu disappears. 759 760 761Redundant defaults 762================== 763 764``bool`` symbols implicitly default to ``n``, and ``string`` symbols implicitly 765default to the empty string. Therefore, ``default n`` and ``default ""`` are 766(almost) always redundant. 767 768The recommended style in Zephyr is to skip redundant defaults for ``bool`` and 769``string`` symbols. That also generates clearer documentation: (*Implicitly 770defaults to n* instead of *n if <dependencies, possibly inherited>*). 771 772Defaults *should* always be given for ``int`` and ``hex`` symbols, however, as 773they implicitly default to the empty string. This is partly for compatibility 774with the C Kconfig tools, though an implicit 0 default might be less likely to 775be what was intended compared to other symbol types as well. 776 777The one case where ``default n``/``default ""`` is not redundant is when 778defining a symbol in multiple locations and wanting to override e.g. a 779``default y`` on a later definition. Note that a ``default n`` does not override 780a previously defined ``default y``. 781 782That is, FOO will be set to ``n`` in the example below. If the ``default n`` was 783omitted in the first definition, FOO would have been set to ``y``. 784 785 .. code-block:: kconfig 786 787 config FOO 788 bool "foo" 789 default n 790 791 config FOO 792 bool "foo" 793 default y 794 795In the following example FOO will get the value ``y``. 796 797 .. code-block:: kconfig 798 799 config FOO 800 bool "foo" 801 default y 802 803 config FOO 804 bool "foo" 805 default n 806 807.. _kconfig_shorthands: 808 809Common Kconfig shorthands 810========================= 811 812Kconfig has two shorthands that deal with prompts and defaults. 813 814- ``<type> "prompt"`` is a shorthand for giving a symbol/choice a type and a 815 prompt at the same time. These two definitions are equal: 816 817 .. code-block:: kconfig 818 819 config FOO 820 bool "foo" 821 822 .. code-block:: kconfig 823 824 config FOO 825 bool 826 prompt "foo" 827 828 The first style, with the shorthand, is preferred in Zephyr. 829 830- ``def_<type> <value>`` is a shorthand for giving a type and a value at the 831 same time. These two definitions are equal: 832 833 .. code-block:: kconfig 834 835 config FOO 836 def_bool BAR && BAZ 837 838 .. code-block:: kconfig 839 840 config FOO 841 bool 842 default BAR && BAZ 843 844Using both the ``<type> "prompt"`` and the ``def_<type> <value>`` shorthand in 845the same definition is redundant, since it gives the type twice. 846 847The ``def_<type> <value>`` shorthand is generally only useful for symbols 848without prompts, and somewhat obscure. 849 850.. note:: 851 852 For a symbol defined in multiple locations (e.g., in a ``Kconfig.defconfig`` 853 file in Zephyr), it is best to only give the symbol type for the "base" 854 definition of the symbol, and to use ``default`` (instead of ``def_<type> 855 value``) for the remaining definitions. That way, if the base definition of 856 the symbol is removed, the symbol ends up without a type, which generates a 857 warning that points to the other definitions. That makes the extra 858 definitions easier to discover and remove. 859 860 861Prompt strings 862============== 863 864For a Kconfig symbol that enables a driver/subsystem FOO, consider having just 865"Foo" as the prompt, instead of "Enable Foo support" or the like. It will 866usually be clear in the context of an option that can be toggled on/off, and 867makes things consistent. 868 869 870Header comments and other nits 871============================== 872 873A few formatting nits, to help keep things consistent: 874 875- Use this format for any header comments at the top of ``Kconfig`` files: 876 877 .. code-block:: none 878 879 # <Overview of symbols defined in the file, preferably in plain English> 880 (Blank line) 881 # Copyright (c) 2019 ... 882 # SPDX-License-Identifier: <License> 883 (Blank line) 884 (Kconfig definitions) 885 886- Format comments as ``# Comment`` rather than ``#Comment`` 887 888- Put a blank line before/after each top-level ``if`` and ``endif`` 889 890- Use a single tab for each indentation 891 892- Indent help text with two extra spaces 893 894 895Lesser-known/used Kconfig features 896********************************** 897 898This section lists some more obscure Kconfig behaviors and features that might 899still come in handy. 900 901 902The ``imply`` statement 903======================= 904 905The ``imply`` statement is similar to ``select``, but respects dependencies and 906doesn't force a value. For example, the following code could be used to enable 907USB keyboard support by default on the FOO SoC, while still allowing the user 908to turn it off: 909 910.. code-block:: kconfig 911 912 config SOC_FOO 913 bool "FOO SoC" 914 imply USB_KEYBOARD 915 916 ... 917 918 config USB_KEYBOARD 919 bool "USB keyboard support" 920 921``imply`` acts like a suggestion, whereas ``select`` forces a value. 922 923 924Optional prompts 925================ 926 927A condition can be put on a symbol's prompt to make it optionally configurable 928by the user. For example, a value ``MASK`` that's hardcoded to 0xFF on some 929boards and configurable on others could be expressed as follows: 930 931.. code-block:: kconfig 932 933 config MASK 934 hex "Bitmask" if HAS_CONFIGURABLE_MASK 935 default 0xFF 936 937.. note:: 938 939 This is short for the following: 940 941 .. code-block:: kconfig 942 943 config MASK 944 hex 945 prompt "Bitmask" if HAS_CONFIGURABLE_MASK 946 default 0xFF 947 948The ``HAS_CONFIGURABLE_MASK`` helper symbol would get selected by boards to 949indicate that ``MASK`` is configurable. When ``MASK`` is configurable, it will 950also default to 0xFF. 951 952 953Optional choices 954================ 955 956Defining a choice with the ``optional`` keyword allows the whole choice to be 957toggled off to select none of the symbols: 958 959.. code-block:: kconfig 960 961 choice 962 prompt "Use legacy protocol" 963 optional 964 965 config LEGACY_PROTOCOL_1 966 bool "Legacy protocol 1" 967 968 config LEGACY_PROTOCOL_2 969 bool "Legacy protocol 2" 970 971 endchoice 972 973In the ``menuconfig`` interface, this will be displayed e.g. as 974``[*] Use legacy protocol (Legacy protocol 1) --->``, where the choice can be 975toggled off to enable neither of the symbols. 976 977 978``visible if`` conditions 979========================= 980 981Putting a ``visible if`` condition on a menu hides the menu and all the symbols 982within it, while still allowing symbol default values to kick in. 983 984As a motivating example, consider the following code: 985 986.. code-block:: kconfig 987 988 menu "Foo subsystem" 989 depends on HAS_CONFIGURABLE_FOO 990 991 config FOO_SETTING_1 992 int "Foo setting 1" 993 default 1 994 995 config FOO_SETTING_2 996 int "Foo setting 2" 997 default 2 998 999 endmenu 1000 1001When ``HAS_CONFIGURABLE_FOO`` is ``n``, no configuration output is generated 1002for ``FOO_SETTING_1`` and ``FOO_SETTING_2``, as the code above is logically 1003equivalent to the following code: 1004 1005.. code-block:: kconfig 1006 1007 config FOO_SETTING_1 1008 int "Foo setting 1" 1009 default 1 1010 depends on HAS_CONFIGURABLE_FOO 1011 1012 config FOO_SETTING_2 1013 int "Foo setting 2" 1014 default 2 1015 depends on HAS_CONFIGURABLE_FOO 1016 1017If we want the symbols to still get their default values even when 1018``HAS_CONFIGURABLE_FOO`` is ``n``, but not be configurable by the user, then we 1019can use ``visible if`` instead: 1020 1021.. code-block:: kconfig 1022 1023 menu "Foo subsystem" 1024 visible if HAS_CONFIGURABLE_FOO 1025 1026 config FOO_SETTING_1 1027 int "Foo setting 1" 1028 default 1 1029 1030 config FOO_SETTING_2 1031 int "Foo setting 2" 1032 default 2 1033 1034 endmenu 1035 1036This is logically equivalent to the following: 1037 1038.. code-block:: kconfig 1039 1040 config FOO_SETTING_1 1041 int "Foo setting 1" if HAS_CONFIGURABLE_FOO 1042 default 1 1043 1044 config FOO_SETTING_2 1045 int "Foo setting 2" if HAS_CONFIGURABLE_FOO 1046 default 2 1047 1048.. note:: 1049 1050 See the `optional prompts`_ section for the meaning of the conditions on the 1051 prompts. 1052 1053When ``HAS_CONFIGURABLE`` is ``n``, we now get the following configuration 1054output for the symbols, instead of no output: 1055 1056.. code-block:: cfg 1057 1058 ... 1059 CONFIG_FOO_SETTING_1=1 1060 CONFIG_FOO_SETTING_2=2 1061 ... 1062 1063 1064Other resources 1065*************** 1066 1067The *Intro to symbol values* section in the `Kconfiglib docstring 1068<https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`__ goes 1069over how symbols values are calculated in more detail. 1070