Kconfig - Tips and Best Practices
This page covers some Kconfig best practices and explains some Kconfig behaviors and features that might be cryptic or that are easily overlooked.
When deciding whether something belongs in Kconfig, it helps to distinguish between symbols that have prompts and symbols that don’t.
If a symbol has a prompt (e.g.
bool "Enable foo"), then the user can change
the symbol’s value in the
guiconfig interface (see
Interactive Kconfig interfaces), or by manually editing configuration files. Conversely, a
symbol without a prompt can never be changed directly by the user, not even by
manually editing configuration files.
Only put a prompt on a symbol if it makes sense for the user to change its value.
Symbols without prompts are called hidden or invisible symbols, because
they don’t show up in
guiconfig. Symbols that have
prompts can also be invisible, when their dependencies are not satisfied.
Symbols without prompts can’t be configured directly by the user (they derive their value from other symbols), so less restrictions apply to them. If some derived setting is easier to calculate in Kconfig than e.g. during the build, then do it in Kconfig, but keep the distinction between symbols with and without prompts in mind.
See the optional prompts section for a way to deal with settings that are fixed on some machines and configurable on other machines.
In Zephyr, Kconfig configuration is done after selecting a target board. In general, it does not make sense to use Kconfig for a value that corresponds to a fixed machine-specific setting. Usually, such settings should be handled via devicetree instead.
In particular, avoid adding new Kconfig options of the following types:
For example, if you are writing an I2C device driver, avoid creating an option
MY_DEVICE_I2C_BUS_NAME for specifying the bus node your device is
controlled by. See Device drivers that depend on other devices for alternatives.
Similarly, if your application depends on a hardware-specific PWM device to
control an RGB LED, avoid creating an option like
Applications that depend on board-specific devices for alternatives.
For example, avoid Kconfig options specifying a GPIO pin.
An alternative applicable to device drivers is to define a GPIO specifier with type phandle-array in the device binding, and using the GPIO devicetree API from C. Similar advice applies to other cases where devicetree.h provides Hardware specific APIs for referring to other nodes in the system. Search the source code for drivers using these APIs for examples.
For applications, see Blinky for a devicetree-based alternative.
select statement is used to force one symbol to
y whenever another
y. For example, the following code forces
config CONSOLE bool "Console support" ... config USB_CONSOLE bool "USB console support" select CONSOLE
This section covers some pitfalls and good uses for
select might seem like a generally useful feature at first, but can cause
configuration issues if overused.
For example, say that a new dependency is added to the
above, by a developer who is unaware of the
USB_CONSOLE symbol (or simply
forgot about it):
config CONSOLE bool "Console support" depends on STRING_ROUTINES
USB_CONSOLE now forces
y, even if
To fix the problem, the
STRING_ROUTINES dependency needs to be added to
USB_CONSOLE as well:
config USB_CONSOLE bool "USB console support" select CONSOLE depends on STRING_ROUTINES ... config STRING_ROUTINES bool "Include string routines"
More insidious cases with dependencies inherited from
statements are common.
An alternative attempt to solve the issue might be to turn the
config CONSOLE bool "Console support" select STRING_ROUTINES ... config USB_CONSOLE bool "USB console support" select CONSOLE
In practice, this often amplifies the problem, because any dependencies added
STRING_ROUTINES now need to be copied to both
In general, whenever the dependencies of a symbol are updated, the dependencies of all symbols that (directly or indirectly) select it have to be updated as well. This is very often overlooked in practice, even for the simplest case above.
Chains of symbols selecting each other should be avoided in particular, except for simple helper symbols, as covered below in Using select for helper symbols.
Liberal use of
select also tends to make Kconfig files harder to read, both
due to the extra dependencies and due to the non-local nature of
which hides ways in which a symbol might get enabled.
For the example in the previous section, a better solution is usually to turn
select into a
config CONSOLE bool "Console support" ... config USB_CONSOLE bool "USB console support" depends on CONSOLE
This makes it impossible to generate an invalid configuration, and means that dependencies only ever have to be updated in a single spot.
An objection to using
depends on here might be that configuration files
USB_CONSOLE now also need to enable
This comes down to a trade-off, but if enabling
CONSOLE is the norm, then a
mitigation is to make
CONSOLE default to
config CONSOLE bool "Console support" default y
This gives just a single assignment in configuration files:
Note that configuration files that do not want
CONSOLE enabled now have to
explicitly disable it:
A good and safe use of
select is for setting “helper” symbols that capture
some condition. Such helper symbols should preferably have no prompt or
For example, a helper symbol for indicating that a particular CPU/SoC has an FPU could be defined as follows:
config CPU_HAS_FPU bool help If y, the CPU has an FPU ... config SOC_FOO bool "FOO SoC" select CPU_HAS_FPU ... config SOC_BAR bool "BAR SoC" select CPU_HAS_FPU
This makes it possible for other symbols to check for FPU support in a generic way, without having to look for particular architectures:
config FPU bool "Support floating point operations" depends on CPU_HAS_FPU
The alternative would be to have dependencies like the following, possibly duplicated in several spots:
config FPU bool "Support floating point operations" depends on SOC_FOO || SOC_BAR || ...
Invisible helper symbols can also be useful without
select. For example,
the following code defines a helper symbol that has the value
y if the
machine has some arbitrarily-defined “large” amount of memory:
config LARGE_MEM def_bool MEM_SIZE >= 64
This is short for the following:
config LARGE_MEM bool default MEM_SIZE >= 64
In summary, here are some recommended practices for
Avoid selecting symbols with prompts or dependencies. Prefer
depends on. If
depends oncauses annoying bloat in configuration files, consider adding a Kconfig default for the most common value.
Rare exceptions might include cases where you’re sure that the dependencies of the selecting and selected symbol will never drift out of sync, e.g. when dealing with two simple symbols defined close to one another within the same
Common sense applies, but be aware that
selectoften causes issues in practice.
depends onis usually a cleaner and safer solution.
Select simple helper symbols without prompts and dependencies however much you like. They’re a great tool for simplifying Kconfig files.
if blocks add dependencies to each item within the
if, as if
on was used.
A common misunderstanding related to
if is to think that the following code
conditionally includes the file
if DEP source "Kconfig.other" endif
In reality, there are no conditional includes in Kconfig.
if has no special
meaning around a
Conditional includes would be impossible to implement, because
conditions may contain (either directly or indirectly) forward references to
symbols that haven’t been defined yet.
Kconfig.other above contains this definition:
config FOO bool "Support foo"
In this case,
FOO will end up with this definition:
config FOO bool "Support foo" depends on DEP
Note that it is redundant to add
depends on DEP to the definition of
Kconfig.other, because the
DEP dependency has already
been added by
In general, try to avoid adding redundant dependencies. They can make the structure of the Kconfig files harder to understand, and also make changes more error-prone, since it can be hard to spot that the same dependency is added twice.
Assignments to hidden (promptless, also called invisible) symbols in
configuration files are always ignored. Hidden symbols get their value
indirectly from other symbols, via e.g.
A common source of confusion is opening the output configuration file
zephyr/.config), seeing a bunch of assignments to hidden symbols,
and assuming that those assignments must be respected when the configuration is
read back in by Kconfig. In reality, all assignments to hidden symbols in
zephyr/.config are ignored by Kconfig, like for other configuration
To understand why
zephyr/.config still includes assignments to hidden
symbols, it helps to realize that
zephyr/.config serves two separate
It holds the saved configuration, and
it holds configuration output.
zephyr/.configis parsed by the CMake files to let them query configuration settings, for example.
The assignments to hidden symbols in
zephyr/.config are just
configuration output. Kconfig itself ignores assignments to hidden symbols when
calculating symbol values.
A minimal configuration, which can be generated from within the menuconfig and guiconfig interfaces, could be considered closer to just a saved configuration, without the full configuration output.
depends on works not just for
bool symbols, but also for
hex symbols (and for choices).
The Kconfig definitions below will hide the
FOO_DEVICE_FREQUENCY symbol and
disable any configuration output for it when
FOO_DEVICE is disabled.
config FOO_DEVICE bool "Foo device" config FOO_DEVICE_FREQUENCY int "Foo device frequency" depends on FOO_DEVICE
In general, it’s a good idea to check that only relevant symbols are ever shown
guiconfig interface. Having
show up when
FOO_DEVICE is disabled (and possibly hidden) makes the
relationship between the symbols harder to understand, even if code never looks
FOO_DEVICE is disabled.
After you make Kconfig changes, you can use the
scripts/kconfig/lint.py script to check for some potential
issues, like unused symbols and symbols that are impossible to enable. Use
--help to see available options.
Some checks are necessarily a bit heuristic, so a symbol being flagged by a
check does not necessarily mean there’s a problem. If a check returns a false
positive e.g. due to token pasting in C (
When investigating an unknown symbol
FOO_BAR, it is a good idea to run
git grep FOO_BAR to look for references. It is also a good idea to search
for some components of the symbol name with e.g.
git grep FOO and
git grep BAR, as it can help uncover token pasting.
This section gives some style recommendations and explains some common Kconfig shorthands.
If a sequence of symbols/choices share a common dependency, the dependency can
be factored out with an
As an example, consider the following code:
config FOO bool "Foo" depends on DEP config BAR bool "Bar" depends on DEP choice prompt "Choice" depends on DEP config BAZ bool "Baz" config QAZ bool "Qaz" endchoice
DEP dependency can be factored out like this:
if DEP config FOO bool "Foo" config BAR bool "Bar" choice prompt "Choice" config BAZ bool "Baz" config QAZ bool "Qaz" endchoice endif # DEP
Internally, the second version of the code is transformed into the first.
If a sequence of symbols/choices with shared dependencies are all in the same menu, the dependency can be put on the menu itself:
menu "Foo features" depends on FOO_SUPPORT config FOO_FEATURE_1 bool "Foo feature 1" config FOO_FEATURE_2 bool "Foo feature 2" endmenu
n, the entire menu disappears.
bool symbols implicitly default to
string symbols implicitly
default to the empty string. Therefore,
default n and
default "" are
(almost) always redundant.
The recommended style in Zephyr is to skip redundant defaults for
string symbols. That also generates clearer documentation: (Implicitly
defaults to n instead of n if <dependencies, possibly inherited>).
The one case where
default "" is not redundant is when
defining a symbol in multiple locations and wanting to override e.g. a
default y on a later definition.
Defaults should always be given for
hex symbols, however, as
they implicitly default to the empty string. This is partly for compatibility
with the C Kconfig tools, though an implicit 0 default might be less likely to
be what was intended compared to other symbol types as well.
Kconfig has two shorthands that deal with prompts and defaults.
<type> "prompt"is a shorthand for giving a symbol/choice a type and a prompt at the same time. These two definitions are equal:
config FOO bool "foo"
config FOO bool prompt "foo"
The first style, with the shorthand, is preferred in Zephyr.
def_<type> <value>is a shorthand for giving a type and a value at the same time. These two definitions are equal:
config FOO def_bool BAR && BAZ
config FOO bool default BAR && BAZ
Using both the
<type> "prompt" and the
def_<type> <value> shorthand in
the same definition is redundant, since it gives the type twice.
def_<type> <value> shorthand is generally only useful for symbols
without prompts, and somewhat obscure.
For a symbol defined in multiple locations (e.g., in a
file in Zephyr), it is best to only give the symbol type for the “base”
definition of the symbol, and to use
default (instead of
value) for the remaining definitions. That way, if the base definition of
the symbol is removed, the symbol ends up without a type, which generates a
warning that points to the other definitions. That makes the extra
definitions easier to discover and remove.
For a Kconfig symbol that enables a driver/subsystem FOO, consider having just “Foo” as the prompt, instead of “Enable Foo support” or the like. It will usually be clear in the context of an option that can be toggled on/off, and makes things consistent.
A few formatting nits, to help keep things consistent:
Use this format for any header comments at the top of
# <Overview of symbols defined in the file, preferably in plain English> (Blank line) # Copyright (c) 2019 ... # SPDX-License-Identifier: <License> (Blank line) (Kconfig definitions)
Format comments as
# Commentrather than
Put a blank line before/after each top-level
Use a single tab for each indentation
Indent help text with two extra spaces
This section lists some more obscure Kconfig behaviors and features that might still come in handy.
imply statement is similar to
select, but respects dependencies and
doesn’t force a value. For example, the following code could be used to enable
USB keyboard support by default on the FOO SoC, while still allowing the user
to turn it off:
config SOC_FOO bool "FOO SoC" imply USB_KEYBOARD ... config USB_KEYBOARD bool "USB keyboard support"
imply acts like a suggestion, whereas
select forces a value.
A condition can be put on a symbol’s prompt to make it optionally configurable
by the user. For example, a value
MASK that’s hardcoded to 0xFF on some
boards and configurable on others could be expressed as follows:
config MASK hex "Bitmask" if HAS_CONFIGURABLE_MASK default 0xFF
This is short for the following:
config MASK hex prompt "Bitmask" if HAS_CONFIGURABLE_MASK default 0xFF
HAS_CONFIGURABLE_MASK helper symbol would get selected by boards to
MASK is configurable. When
MASK is configurable, it will
also default to 0xFF.
Defining a choice with the
optional keyword allows the whole choice to be
toggled off to select none of the symbols:
choice prompt "Use legacy protocol" optional config LEGACY_PROTOCOL_1 bool "Legacy protocol 1" config LEGACY_PROTOCOL_2 bool "Legacy protocol 2" endchoice
menuconfig interface, this will be displayed e.g. as
[*] Use legacy protocol (Legacy protocol 1) --->, where the choice can be
toggled off to enable neither of the symbols.
visible if condition on a menu hides the menu and all the symbols
within it, while still allowing symbol default values to kick in.
As a motivating example, consider the following code:
menu "Foo subsystem" depends on HAS_CONFIGURABLE_FOO config FOO_SETTING_1 int "Foo setting 1" default 1 config FOO_SETTING_2 int "Foo setting 2" default 2 endmenu
n, no configuration output is generated
FOO_SETTING_2, as the code above is logically
equivalent to the following code:
config FOO_SETTING_1 int "Foo setting 1" default 1 depends on HAS_CONFIGURABLE_FOO config FOO_SETTING_2 int "Foo setting 2" default 2 depends on HAS_CONFIGURABLE_FOO
If we want the symbols to still get their default values even when
n, but not be configurable by the user, then we
visible if instead:
menu "Foo subsystem" visible if HAS_CONFIGURABLE_FOO config FOO_SETTING_1 int "Foo setting 1" default 1 config FOO_SETTING_2 int "Foo setting 2" default 2 endmenu
This is logically equivalent to the following:
config FOO_SETTING_1 int "Foo setting 1" if HAS_CONFIGURABLE_FOO default 1 config FOO_SETTING_2 int "Foo setting 2" if HAS_CONFIGURABLE_FOO default 2
See the optional prompts section for the meaning of the conditions on the prompts.
n, we now get the following configuration
output for the symbols, instead of no output:
... CONFIG_FOO_SETTING_1=1 CONFIG_FOO_SETTING_2=2 ...
The Intro to symbol values section in the Kconfiglib docstring goes over how symbols values are calculated in more detail.