Skip to content

Commit a0096b7

Browse files
ParaZeracfriedt
authored andcommitted
doc: update KConfig style guidelines
Add/update guidelines on how KConfig files should be written and organized by identifying a common style currenlty used in most areas of the project and formalizing it. This should provide an aid for future merge request, code reviews and refactorings to enable the acting party to better enforce consistency in KConfig files. Signed-off-by: Tim Schrader <[email protected]>
1 parent a43f48b commit a0096b7

File tree

5 files changed

+206
-9
lines changed

5 files changed

+206
-9
lines changed

doc/contribute/style/kconfig.rst

Lines changed: 123 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,126 @@
33
Kconfig Style Guidelines
44
########################
55

6-
* Line length of 100 columns or fewer.
7-
* Indent with tabs, except for ``help`` entry text which should be placed at
8-
one tab plus two extra spaces.
9-
* Leave a single empty line between option declarations.
10-
* Use Statements like ``select`` carefully, see
11-
:ref:`kconfig_tips_and_tricks` for more information.
12-
* Format comments as ``# Comment`` rather than ``#Comment``
13-
* Insert an empty line before/after each top-level ``if`` and ``endif``
14-
statement.
6+
This document provides style guidelines for writing Kconfig files in the Zephyr
7+
project. Following these guidelines ensures consistency and readability across
8+
the codebase, making it easier for developers to understand and maintain
9+
configuration options.
10+
11+
The following sections provide guidelines with examples to illustrate
12+
proper Kconfig formatting and naming conventions.
13+
14+
15+
Basic Formatting Rules
16+
**********************
17+
18+
When writing Kconfig files, follow these basic formatting rules:
19+
20+
* **Line length**: Keep lines to 100 columns or fewer.
21+
* **Indentation**: Use tabs for indentation, except for ``help`` entry text
22+
which should be placed at one tab plus two extra spaces.
23+
* **Spacing**: Leave a single empty line between option declarations.
24+
* **Comments**: Format comments as ``# Comment`` rather than ``#Comment``.
25+
* **Conditional blocks**: Insert an empty line before/after each top-level
26+
``if`` and ``endif`` statement.
27+
28+
For guidance on using statements like ``select``, see
29+
:ref:`kconfig_tips_and_tricks` for more information.
30+
31+
Symbol Naming and Structure
32+
***************************
33+
34+
The following examples demonstrate proper Kconfig symbol naming and structure:
35+
36+
.. literalinclude:: kconfig_demo_simple.txt
37+
:language: kconfig
38+
:start-after: start-after-here
39+
40+
.. literalinclude:: kconfig_demo_complex.txt
41+
:language: kconfig
42+
:start-after: start-after-here
43+
44+
45+
Naming Conventions
46+
******************
47+
48+
* As a general rule, symbols concerning the same component should be distinct
49+
from other symbols. This can usually be accomplished by using a common
50+
prefix. This prefix can be a simple keyword or, as in the case for drivers,
51+
several keywords for more precision.
52+
53+
* A common prefix usually indicates the subsystem or component the symbol
54+
belongs to.
55+
56+
* An enabling symbol name should consist of keywords to provide the context
57+
of the symbol in a scope from most general to most specific
58+
(e.g. *Driver Type* -> *Driver Name*).
59+
60+
* The prompt for an enabling symbol should use the same logic as the symbol
61+
name itself but use the keywords in reversed order.
62+
63+
* Adhering to this style makes searching for symbols easier in the UI
64+
because one can filter by a scope keyword.
65+
66+
* When the enabling symbol is dependent on a devicetree node, consider
67+
depending on the automatically created ``DT_HAS_<node>_ENABLED`` symbol.
68+
69+
The specific formats by subtree:
70+
71+
* **Drivers (/drivers)**: Use the format ``{Driver Type}_{Driver Name}`` for
72+
symbols and ``{Driver Name} {Driver Type} driver`` for prompts.
73+
74+
* **Sensors (/drivers/sensors)**: Use the format ``SENSOR_{Sensor Name}`` for symbols
75+
and ``{Sensor Name} {Sensor Type} sensor driver`` for prompts.
76+
77+
* **Architecture (/arch)**: Many symbols are shared across architectures. Before
78+
creating new symbols, check if similar ones already exist in other
79+
architectures.
80+
81+
Examples
82+
========
83+
84+
.. note::
85+
86+
The following examples only show the symbol and prompt lines for brevity.
87+
88+
**Driver Examples:**
89+
90+
.. literalinclude:: kconfig_example_driver.txt
91+
:language: kconfig
92+
:start-after: start-after-here
93+
94+
**Sensor Examples:**
95+
96+
.. literalinclude:: kconfig_example_sensor.txt
97+
:language: kconfig
98+
:start-after: start-after-here
99+
100+
Configuration Symbol Organization
101+
*********************************
102+
103+
When a feature uses config symbols to configure its behavior:
104+
105+
* Use ``menuconfig`` instead of ``config`` to define the enabling feature
106+
(even when the config symbol has no prompt).
107+
108+
* Encapsulate the config symbols in an ``if`` statement to declare their
109+
dependency on the enabling symbol (this automatically groups these
110+
symbols under the enabling symbol in the UI).
111+
112+
* Prefix the config symbol with the enabling symbol's name for scope and
113+
context.
114+
115+
* In the prompt of the config symbol, describe what the symbol configures
116+
without repeating the scope keywords, because the grouping in the UI
117+
provides this context.
118+
119+
File Organization
120+
*****************
121+
122+
When organizing Kconfig files:
123+
124+
* Keep the Kconfig file close to the source files it configures.
125+
126+
* When dealing with large Kconfig files (e.g. with many config symbols),
127+
consider grouping (some of) them into a separate file and import it using the
128+
``source`` directive to improve readability.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Example of a components Kconfig file with one enabling symbol and two config symbols
6+
7+
# start-after-here
8+
9+
# Symbol to enable/disable the feature ("enabling symbol")
10+
menuconfig ADC_AD4114
11+
bool "AD4114 ADC driver"
12+
depends on DT_HAS_ADI_AD4114_ADC_ENABLED
13+
select SPI
14+
default y
15+
help
16+
Enable the AD4114 ADC driver.
17+
18+
if ADC_AD4114
19+
20+
# Symbol to configure the behavior of the feature ("config symbol")
21+
config ADC_AD4114_ACQUISITION_THREAD_STACK_SIZE
22+
int "Stack size for the data acquisition thread"
23+
default 512
24+
help
25+
Size of the stack used for the internal data acquisition
26+
thread.
27+
28+
# Symbol to configure the behavior of the feature ("config symbol")
29+
config ADC_AD4114_ACQUISITION_THREAD_PRIO
30+
int "Priority for the data acquisition thread"
31+
default 0
32+
help
33+
Priority level for the internal ADC data acquisition thread.
34+
35+
endif # ADC_AD4114
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Example of a components Kconfig file with one enabling symbol
6+
7+
# start-after-here
8+
9+
# Symbol to enable/disable the feature ("enabling symbol")
10+
config ADC_AD405X
11+
bool "AD405X ADC driver"
12+
depends on DT_HAS_ADI_AD4052_ADC_ENABLED || DT_HAS_ADI_AD4050_ADC_ENABLED
13+
select SPI
14+
default y
15+
help
16+
Enable ADC driver for AD405X.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Examples of Kconfig symbols with prompt to enable drivers
6+
7+
# start-after-here
8+
9+
config ADC_AD405X
10+
bool "AD405X ADC driver"
11+
12+
config CAN_MAX32
13+
bool "MAX32 CAN driver"
14+
15+
config I2C_EMUL
16+
bool "I2C emulator driver"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Examples of Kconfig symbols with prompt to enable sensor drivers
6+
7+
# start-after-here
8+
9+
config SENSOR_BMP581
10+
bool "BMP581 barometric pressure sensor driver"
11+
12+
config SENSOR_TMP007
13+
bool "TMP007 infrared thermopile sensor driver"
14+
15+
config SENSOR_LM75
16+
bool "LM75 temperature sensor driver"

0 commit comments

Comments
 (0)