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- An exemption are buses like I2C and SPI, and following the same thought 319 process things like MFD as well. Drivers on these buses should use 320 ``select`` to allow the automatic activation of the necessary bus drivers 321 when devices on the bus are enabled in the devicetree. 322 323.. code-block:: kconfig 324 325 config ADC_FOO 326 bool "external SPI ADC foo driver" 327 select SPI 328 329(Lack of) conditional includes 330****************************** 331 332``if`` blocks add dependencies to each item within the ``if``, as if ``depends 333on`` was used. 334 335A common misunderstanding related to ``if`` is to think that the following code 336conditionally includes the file :file:`Kconfig.other`: 337 338.. code-block:: kconfig 339 340 if DEP 341 source "Kconfig.other" 342 endif 343 344In reality, there are no conditional includes in Kconfig. ``if`` has no special 345meaning around a ``source``. 346 347.. note:: 348 349 Conditional includes would be impossible to implement, because ``if`` 350 conditions may contain (either directly or indirectly) forward references to 351 symbols that haven't been defined yet. 352 353Say that :file:`Kconfig.other` above contains this definition: 354 355.. code-block:: kconfig 356 357 config FOO 358 bool "Support foo" 359 360In this case, ``FOO`` will end up with this definition: 361 362.. code-block:: kconfig 363 364 config FOO 365 bool "Support foo" 366 depends on DEP 367 368Note that it is redundant to add ``depends on DEP`` to the definition of 369``FOO`` in :file:`Kconfig.other`, because the ``DEP`` dependency has already 370been added by ``if DEP``. 371 372In general, try to avoid adding redundant dependencies. They can make the 373structure of the Kconfig files harder to understand, and also make changes more 374error-prone, since it can be hard to spot that the same dependency is added 375twice. 376 377 378"Stuck" symbols in menuconfig and guiconfig 379******************************************* 380 381There is a common subtle gotcha related to interdependent configuration symbols 382with prompts. Consider these symbols: 383 384.. code-block:: kconfig 385 386 config FOO 387 bool "Foo" 388 389 config STACK_SIZE 390 hex "Stack size" 391 default 0x200 if FOO 392 default 0x100 393 394Assume that the intention here is to use a larger stack whenever ``FOO`` is 395enabled, and that the configuration initially has ``FOO`` disabled. Also, 396remember that Zephyr creates an initial configuration in :file:`zephyr/.config` 397in the build directory by merging configuration files (including e.g. 398:file:`prj.conf`). This configuration file exists before 399``menuconfig`` or ``guiconfig`` is run. 400 401When first entering the configuration interface, the value of ``STACK_SIZE`` is 4020x100, as expected. After enabling ``FOO``, you might reasonably expect the 403value of ``STACK_SIZE`` to change to 0x200, but it stays as 0x100. 404 405To understand what's going on, remember that ``STACK_SIZE`` has a prompt, 406meaning it is user-configurable, and consider that all Kconfig has to go on 407from the initial configuration is this: 408 409.. code-block:: cfg 410 411 CONFIG_STACK_SIZE=0x100 412 413Since Kconfig can't know if the 0x100 value came from a ``default`` or was 414typed in by the user, it has to assume that it came from the user. Since 415``STACK_SIZE`` is user-configurable, the value from the configuration file is 416respected, and any symbol defaults are ignored. This is why the value of 417``STACK_SIZE`` appears to be "frozen" at 0x100 when toggling ``FOO``. 418 419The right fix depends on what the intention is. Here's some different scenarios 420with suggestions: 421 422- If ``STACK_SIZE`` can always be derived automatically and does not need to be 423 user-configurable, then just remove the prompt: 424 425 .. code-block:: kconfig 426 427 config STACK_SIZE 428 hex 429 default 0x200 if FOO 430 default 0x100 431 432 Symbols without prompts ignore any value from the saved configuration. 433 434- If ``STACK_SIZE`` should usually be user-configurable, but needs to be set to 435 0x200 when ``FOO`` is enabled, then disable its prompt when ``FOO`` is 436 enabled, as described in `optional prompts`_: 437 438 .. code-block:: kconfig 439 440 config STACK_SIZE 441 hex "Stack size" if !FOO 442 default 0x200 if FOO 443 default 0x100 444 445- If ``STACK_SIZE`` should usually be derived automatically, but needs to be 446 set to a custom value in rare circumstances, then add another option for 447 making ``STACK_SIZE`` user-configurable: 448 449 .. code-block:: kconfig 450 451 config CUSTOM_STACK_SIZE 452 bool "Use a custom stack size" 453 help 454 Enable this if you need to use a custom stack size. When disabled, a 455 suitable stack size is calculated automatically. 456 457 config STACK_SIZE 458 hex "Stack size" if CUSTOM_STACK_SIZE 459 default 0x200 if FOO 460 default 0x100 461 462 As long as ``CUSTOM_STACK_SIZE`` is disabled, ``STACK_SIZE`` will ignore the 463 value from the saved configuration. 464 465It is a good idea to try out changes in the ``menuconfig`` or ``guiconfig`` 466interface, to make sure that things behave the way you expect. This is 467especially true when making moderately complex changes like these. 468 469 470Assignments to promptless symbols in configuration files 471******************************************************** 472 473Assignments to hidden (promptless, also called *invisible*) symbols in 474configuration files are always ignored. Hidden symbols get their value 475indirectly from other symbols, via e.g. ``default`` and ``select``. 476 477A common source of confusion is opening the output configuration file 478(:file:`zephyr/.config`), seeing a bunch of assignments to hidden symbols, 479and assuming that those assignments must be respected when the configuration is 480read back in by Kconfig. In reality, all assignments to hidden symbols in 481:file:`zephyr/.config` are ignored by Kconfig, like for other configuration 482files. 483 484To understand why :file:`zephyr/.config` still includes assignments to hidden 485symbols, it helps to realize that :file:`zephyr/.config` serves two separate 486purposes: 487 4881. It holds the saved configuration, and 489 4902. it holds configuration output. :file:`zephyr/.config` is parsed by the CMake 491 files to let them query configuration settings, for example. 492 493The assignments to hidden symbols in :file:`zephyr/.config` are just 494configuration output. Kconfig itself ignores assignments to hidden symbols when 495calculating symbol values. 496 497.. note:: 498 499 A *minimal configuration*, which can be generated from within the 500 :ref:`menuconfig and guiconfig interfaces <menuconfig>`, could be considered 501 closer to just a saved configuration, without the full configuration output. 502 503 504``depends on`` and ``string``/``int``/``hex`` symbols 505***************************************************** 506 507``depends on`` works not just for ``bool`` symbols, but also for ``string``, 508``int``, and ``hex`` symbols (and for choices). 509 510The Kconfig definitions below will hide the ``FOO_DEVICE_FREQUENCY`` symbol and 511disable any configuration output for it when ``FOO_DEVICE`` is disabled. 512 513.. code-block:: kconfig 514 515 config FOO_DEVICE 516 bool "Foo device" 517 518 config FOO_DEVICE_FREQUENCY 519 int "Foo device frequency" 520 depends on FOO_DEVICE 521 522In general, it's a good idea to check that only relevant symbols are ever shown 523in the ``menuconfig``/``guiconfig`` interface. Having ``FOO_DEVICE_FREQUENCY`` 524show up when ``FOO_DEVICE`` is disabled (and possibly hidden) makes the 525relationship between the symbols harder to understand, even if code never looks 526at ``FOO_DEVICE_FREQUENCY`` when ``FOO_DEVICE`` is disabled. 527 528 529``menuconfig`` symbols 530********************** 531 532If the definition of a symbol ``FOO`` is immediately followed by other symbols 533that depend on ``FOO``, then those symbols become children of ``FOO``. If 534``FOO`` is defined with ``config FOO``, then the children are shown indented 535relative to ``FOO``. Defining ``FOO`` with ``menuconfig FOO`` instead puts the 536children in a separate menu rooted at ``FOO``. 537 538``menuconfig`` has no effect on evaluation. It's just a display option. 539 540``menuconfig`` can cut down on the number of menus and make the menu structure 541easier to navigate. For example, say you have the following definitions: 542 543.. code-block:: kconfig 544 545 menu "Foo subsystem" 546 547 config FOO_SUBSYSTEM 548 bool "Foo subsystem" 549 550 if FOO_SUBSYSTEM 551 552 config FOO_FEATURE_1 553 bool "Foo feature 1" 554 555 config FOO_FEATURE_2 556 bool "Foo feature 2" 557 558 config FOO_FREQUENCY 559 int "Foo frequency" 560 561 ... lots of other FOO-related symbols 562 563 endif # FOO_SUBSYSTEM 564 565 endmenu 566 567In this case, it's probably better to get rid of the ``menu`` and turn 568``FOO_SUBSYSTEM`` into a ``menuconfig`` symbol: 569 570.. code-block:: kconfig 571 572 menuconfig FOO_SUBSYSTEM 573 bool "Foo subsystem" 574 575 if FOO_SUBSYSTEM 576 577 config FOO_FEATURE_1 578 bool "Foo feature 1" 579 580 config FOO_FEATURE_2 581 bool "Foo feature 2" 582 583 config FOO_FREQUENCY 584 int "Foo frequency" 585 586 ... lots of other FOO-related symbols 587 588 endif # FOO_SUBSYSTEM 589 590In the ``menuconfig`` interface, this will be displayed as follows: 591 592.. code-block:: none 593 594 [*] Foo subsystem ---> 595 596Note that making a symbol without children a ``menuconfig`` is meaningless. It 597should be avoided, because it looks identical to a symbol with all children 598invisible: 599 600.. code-block:: none 601 602 [*] I have no children ---- 603 [*] All my children are invisible ---- 604 605 606Commas in macro arguments 607************************* 608 609Kconfig uses commas to separate macro arguments. 610This means a construct like this will fail: 611 612.. code-block:: kconfig 613 614 config FOO 615 bool 616 default y if $(dt_chosen_enabled,"zephyr,bar") 617 618To solve this problem, create a variable with the text and use this variable as 619argument, as follows: 620 621.. code-block:: kconfig 622 623 DT_CHOSEN_ZEPHYR_BAR := zephyr,bar 624 625 config FOO 626 bool 627 default y if $(dt_chosen_enabled,$(DT_CHOSEN_ZEPHYR_BAR)) 628 629 630Checking changes in menuconfig/guiconfig 631**************************************** 632 633When adding new symbols or making other changes to Kconfig files, it is a good 634idea to look up the symbols in :ref:`menuconfig or guiconfig <menuconfig>` 635afterwards. To get to a symbol quickly, use the jump-to feature (press 636:kbd:`/`). 637 638Here are some things to check: 639 640* Are the symbols placed in a good spot? Check that they appear in a menu where 641 they make sense, close to related symbols. 642 643 If one symbol depends on another, then it's often a good idea to place it 644 right after the symbol it depends on. It will then be shown indented relative 645 to the symbol it depends on in the ``menuconfig`` interface, and in a 646 separate menu rooted at the symbol in ``guiconfig``. This also works if 647 several symbols are placed after the symbol they depend on. 648 649* Is it easy to guess what the symbols do from their prompts? 650 651* If many symbols are added, do all combinations of values they can be set to 652 make sense? 653 654 For example, if two symbols ``FOO_SUPPORT`` and ``NO_FOO_SUPPORT`` are added, 655 and both can be enabled at the same time, then that makes a nonsensical 656 configuration. In this case, it's probably better to have a single 657 ``FOO_SUPPORT`` symbol. 658 659* Are there any duplicated dependencies? 660 661 This can be checked by selecting a symbol and pressing :kbd:`?` to view the 662 symbol information. If there are duplicated dependencies, then use the 663 ``Included via ...`` path shown in the symbol information to figure out where 664 they come from. 665 666 667Checking changes with :file:`scripts/kconfig/lint.py` 668***************************************************** 669 670After you make Kconfig changes, you can use the 671:zephyr_file:`scripts/kconfig/lint.py` script to check for some potential 672issues, like unused symbols and symbols that are impossible to enable. Use 673``--help`` to see available options. 674 675Some checks are necessarily a bit heuristic, so a symbol being flagged by a 676check does not necessarily mean there's a problem. If a check returns a false 677positive e.g. due to token pasting in C (``CONFIG_FOO_##index##_BAR``), just 678ignore it. 679 680When investigating an unknown symbol ``FOO_BAR``, it is a good idea to run 681``git grep FOO_BAR`` to look for references. It is also a good idea to search 682for some components of the symbol name with e.g. ``git grep FOO`` and 683``git grep BAR``, as it can help uncover token pasting. 684 685 686Style recommendations and shorthands 687************************************ 688 689This section gives some style recommendations and explains some common Kconfig 690shorthands. 691 692 693Factoring out common dependencies 694================================= 695 696If a sequence of symbols/choices share a common dependency, the dependency can 697be factored out with an ``if``. 698 699As an example, consider the following code: 700 701.. code-block:: kconfig 702 703 config FOO 704 bool "Foo" 705 depends on DEP 706 707 config BAR 708 bool "Bar" 709 depends on DEP 710 711 choice 712 prompt "Choice" 713 depends on DEP 714 715 config BAZ 716 bool "Baz" 717 718 config QAZ 719 bool "Qaz" 720 721 endchoice 722 723Here, the ``DEP`` dependency can be factored out like this: 724 725.. code-block:: kconfig 726 727 if DEP 728 729 config FOO 730 bool "Foo" 731 732 config BAR 733 bool "Bar" 734 735 choice 736 prompt "Choice" 737 738 config BAZ 739 bool "Baz" 740 741 config QAZ 742 bool "Qaz" 743 744 endchoice 745 746 endif # DEP 747 748.. note:: 749 750 Internally, the second version of the code is transformed into the first. 751 752If a sequence of symbols/choices with shared dependencies are all in the same 753menu, the dependency can be put on the menu itself: 754 755.. code-block:: kconfig 756 757 menu "Foo features" 758 depends on FOO_SUPPORT 759 760 config FOO_FEATURE_1 761 bool "Foo feature 1" 762 763 config FOO_FEATURE_2 764 bool "Foo feature 2" 765 766 endmenu 767 768If ``FOO_SUPPORT`` is ``n``, the entire menu disappears. 769 770 771Redundant defaults 772================== 773 774``bool`` symbols implicitly default to ``n``, and ``string`` symbols implicitly 775default to the empty string. Therefore, ``default n`` and ``default ""`` are 776(almost) always redundant. 777 778The recommended style in Zephyr is to skip redundant defaults for ``bool`` and 779``string`` symbols. That also generates clearer documentation: (*Implicitly 780defaults to n* instead of *n if <dependencies, possibly inherited>*). 781 782Defaults *should* always be given for ``int`` and ``hex`` symbols, however, as 783they implicitly default to the empty string. This is partly for compatibility 784with the C Kconfig tools, though an implicit 0 default might be less likely to 785be what was intended compared to other symbol types as well. 786 787The one case where ``default n``/``default ""`` is not redundant is when 788defining a symbol in multiple locations and wanting to override e.g. a 789``default y`` on a later definition. Note that a ``default n`` does not override 790a previously defined ``default y``. 791 792That is, FOO will be set to ``n`` in the example below. If the ``default n`` was 793omitted in the first definition, FOO would have been set to ``y``. 794 795 .. code-block:: kconfig 796 797 config FOO 798 bool "foo" 799 default n 800 801 config FOO 802 bool "foo" 803 default y 804 805In the following example FOO will get the value ``y``. 806 807 .. code-block:: kconfig 808 809 config FOO 810 bool "foo" 811 default y 812 813 config FOO 814 bool "foo" 815 default n 816 817.. _kconfig_shorthands: 818 819Common Kconfig shorthands 820========================= 821 822Kconfig has two shorthands that deal with prompts and defaults. 823 824- ``<type> "prompt"`` is a shorthand for giving a symbol/choice a type and a 825 prompt at the same time. These two definitions are equal: 826 827 .. code-block:: kconfig 828 829 config FOO 830 bool "foo" 831 832 .. code-block:: kconfig 833 834 config FOO 835 bool 836 prompt "foo" 837 838 The first style, with the shorthand, is preferred in Zephyr. 839 840- ``def_<type> <value>`` is a shorthand for giving a type and a value at the 841 same time. These two definitions are equal: 842 843 .. code-block:: kconfig 844 845 config FOO 846 def_bool BAR && BAZ 847 848 .. code-block:: kconfig 849 850 config FOO 851 bool 852 default BAR && BAZ 853 854Using both the ``<type> "prompt"`` and the ``def_<type> <value>`` shorthand in 855the same definition is redundant, since it gives the type twice. 856 857The ``def_<type> <value>`` shorthand is generally only useful for symbols 858without prompts, and somewhat obscure. 859 860.. note:: 861 862 For a symbol defined in multiple locations (e.g., in a ``Kconfig.defconfig`` 863 file in Zephyr), it is best to only give the symbol type for the "base" 864 definition of the symbol, and to use ``default`` (instead of ``def_<type> 865 value``) for the remaining definitions. That way, if the base definition of 866 the symbol is removed, the symbol ends up without a type, which generates a 867 warning that points to the other definitions. That makes the extra 868 definitions easier to discover and remove. 869 870 871Prompt strings 872============== 873 874For a Kconfig symbol that enables a driver/subsystem FOO, consider having just 875"Foo" as the prompt, instead of "Enable Foo support" or the like. It will 876usually be clear in the context of an option that can be toggled on/off, and 877makes things consistent. 878 879 880Header comments and other nits 881============================== 882 883A few formatting nits, to help keep things consistent: 884 885- Use this format for any header comments at the top of ``Kconfig`` files: 886 887 .. code-block:: none 888 889 # <Overview of symbols defined in the file, preferably in plain English> 890 (Blank line) 891 # Copyright (c) 2019 ... 892 # SPDX-License-Identifier: <License> 893 (Blank line) 894 (Kconfig definitions) 895 896- Format comments as ``# Comment`` rather than ``#Comment`` 897 898- Put a blank line before/after each top-level ``if`` and ``endif`` 899 900- Use a single tab for each indentation 901 902- Indent help text with two extra spaces 903 904 905Lesser-known/used Kconfig features 906********************************** 907 908This section lists some more obscure Kconfig behaviors and features that might 909still come in handy. 910 911 912The ``imply`` statement 913======================= 914 915The ``imply`` statement is similar to ``select``, but respects dependencies and 916doesn't force a value. For example, the following code could be used to enable 917USB keyboard support by default on the FOO SoC, while still allowing the user 918to turn it off: 919 920.. code-block:: kconfig 921 922 config SOC_FOO 923 bool "FOO SoC" 924 imply USB_KEYBOARD 925 926 ... 927 928 config USB_KEYBOARD 929 bool "USB keyboard support" 930 931``imply`` acts like a suggestion, whereas ``select`` forces a value. 932 933 934Optional prompts 935================ 936 937A condition can be put on a symbol's prompt to make it optionally configurable 938by the user. For example, a value ``MASK`` that's hardcoded to 0xFF on some 939boards and configurable on others could be expressed as follows: 940 941.. code-block:: kconfig 942 943 config MASK 944 hex "Bitmask" if HAS_CONFIGURABLE_MASK 945 default 0xFF 946 947.. note:: 948 949 This is short for the following: 950 951 .. code-block:: kconfig 952 953 config MASK 954 hex 955 prompt "Bitmask" if HAS_CONFIGURABLE_MASK 956 default 0xFF 957 958The ``HAS_CONFIGURABLE_MASK`` helper symbol would get selected by boards to 959indicate that ``MASK`` is configurable. When ``MASK`` is configurable, it will 960also default to 0xFF. 961 962 963Optional choices 964================ 965 966Defining a choice with the ``optional`` keyword allows the whole choice to be 967toggled off to select none of the symbols: 968 969.. code-block:: kconfig 970 971 choice 972 prompt "Use legacy protocol" 973 optional 974 975 config LEGACY_PROTOCOL_1 976 bool "Legacy protocol 1" 977 978 config LEGACY_PROTOCOL_2 979 bool "Legacy protocol 2" 980 981 endchoice 982 983In the ``menuconfig`` interface, this will be displayed e.g. as 984``[*] Use legacy protocol (Legacy protocol 1) --->``, where the choice can be 985toggled off to enable neither of the symbols. 986 987 988``visible if`` conditions 989========================= 990 991Putting a ``visible if`` condition on a menu hides the menu and all the symbols 992within it, while still allowing symbol default values to kick in. 993 994As a motivating example, consider the following code: 995 996.. code-block:: kconfig 997 998 menu "Foo subsystem" 999 depends on HAS_CONFIGURABLE_FOO 1000 1001 config FOO_SETTING_1 1002 int "Foo setting 1" 1003 default 1 1004 1005 config FOO_SETTING_2 1006 int "Foo setting 2" 1007 default 2 1008 1009 endmenu 1010 1011When ``HAS_CONFIGURABLE_FOO`` is ``n``, no configuration output is generated 1012for ``FOO_SETTING_1`` and ``FOO_SETTING_2``, as the code above is logically 1013equivalent to the following code: 1014 1015.. code-block:: kconfig 1016 1017 config FOO_SETTING_1 1018 int "Foo setting 1" 1019 default 1 1020 depends on HAS_CONFIGURABLE_FOO 1021 1022 config FOO_SETTING_2 1023 int "Foo setting 2" 1024 default 2 1025 depends on HAS_CONFIGURABLE_FOO 1026 1027If we want the symbols to still get their default values even when 1028``HAS_CONFIGURABLE_FOO`` is ``n``, but not be configurable by the user, then we 1029can use ``visible if`` instead: 1030 1031.. code-block:: kconfig 1032 1033 menu "Foo subsystem" 1034 visible if HAS_CONFIGURABLE_FOO 1035 1036 config FOO_SETTING_1 1037 int "Foo setting 1" 1038 default 1 1039 1040 config FOO_SETTING_2 1041 int "Foo setting 2" 1042 default 2 1043 1044 endmenu 1045 1046This is logically equivalent to the following: 1047 1048.. code-block:: kconfig 1049 1050 config FOO_SETTING_1 1051 int "Foo setting 1" if HAS_CONFIGURABLE_FOO 1052 default 1 1053 1054 config FOO_SETTING_2 1055 int "Foo setting 2" if HAS_CONFIGURABLE_FOO 1056 default 2 1057 1058.. note:: 1059 1060 See the `optional prompts`_ section for the meaning of the conditions on the 1061 prompts. 1062 1063When ``HAS_CONFIGURABLE_FOO`` is ``n``, we now get the following configuration 1064output for the symbols, instead of no output: 1065 1066.. code-block:: cfg 1067 1068 ... 1069 CONFIG_FOO_SETTING_1=1 1070 CONFIG_FOO_SETTING_2=2 1071 ... 1072 1073 1074Other resources 1075*************** 1076 1077The *Intro to symbol values* section in the `Kconfiglib docstring 1078<https://github.com/ulfalizer/Kconfiglib/blob/master/kconfiglib.py>`__ goes 1079over how symbols values are calculated in more detail. 1080