|
3 | 3 | Kconfig Style Guidelines |
4 | 4 | ######################## |
5 | 5 |
|
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. |
0 commit comments