1.. _setting_configuration_values:
2
3Setting Kconfig configuration values
4####################################
5
6The :ref:`menuconfig and guiconfig interfaces <menuconfig>` can be used to test
7out configurations during application development. This page explains how to
8make settings permanent.
9
10All Kconfig options can be searched in the :ref:`Kconfig search page
11<kconfig-search>`.
12
13.. note::
14
15   Before making changes to Kconfig files, it's a good idea to also go through
16   the :ref:`kconfig_tips_and_tricks` page.
17
18
19Visible and invisible Kconfig symbols
20*************************************
21
22When making Kconfig changes, it's important to understand the difference
23between *visible* and *invisible* symbols.
24
25- A visible symbol is a symbol defined with a prompt. Visible symbols show
26  up in the interactive configuration interfaces (hence *visible*), and can be
27  set in configuration files.
28
29  Here's an example of a visible symbol:
30
31  .. code-block:: kconfig
32
33     config FPU
34     	bool "Support floating point operations"
35     	depends on HAS_FPU
36
37  The symbol is shown like this in ``menuconfig``, where it can be toggled:
38
39  .. code-block:: none
40
41     [ ] Support floating point operations
42
43- An *invisible* symbol is a symbol without a prompt. Invisible symbols are
44  not shown in the interactive configuration interfaces, and users have no
45  direct control over their value. They instead get their value from defaults
46  or from other symbols.
47
48  Here's an example of an invisible symbol:
49
50  .. code-block:: kconfig
51
52     config CPU_HAS_FPU
53     	bool
54     	help
55     	  This symbol is y if the CPU has a hardware floating point unit.
56
57  In this case, ``CPU_HAS_FPU`` is enabled through other symbols having
58  ``select CPU_HAS_FPU``.
59
60
61Setting symbols in configuration files
62**************************************
63
64Visible symbols can be configured by setting them in configuration files. The
65initial configuration is produced by merging a :file:`*_defconfig` file for the
66board with application settings, usually from :file:`prj.conf`. See
67:ref:`initial-conf` below for more details.
68
69Assignments in configuration files use this syntax:
70
71.. code-block:: cfg
72
73   CONFIG_<symbol name>=<value>
74
75There should be no spaces around the equals sign.
76
77``bool`` symbols can be enabled or disabled by setting them to ``y`` or ``n``,
78respectively. The ``FPU`` symbol from the example above could be enabled like
79this:
80
81.. code-block:: cfg
82
83   CONFIG_FPU=y
84
85.. note::
86
87   A boolean symbol can also be set to ``n`` with a comment formatted like
88   this:
89
90   .. code-block:: cfg
91
92      # CONFIG_SOME_OTHER_BOOL is not set
93
94   This is the format you will see in the merged configuration
95   saved to :file:`zephyr/.config` in the build directory.
96
97   This style is accepted for historical reasons: Kconfig configuration files
98   can be parsed as makefiles (though Zephyr doesn't use this). Having
99   ``n``-valued symbols correspond to unset variables simplifies tests in Make.
100
101Other symbol types are assigned like this:
102
103.. code-block:: cfg
104
105   CONFIG_SOME_STRING="cool value"
106   CONFIG_SOME_INT=123
107
108Comments use a #:
109
110.. code-block:: cfg
111
112   # This is a comment
113
114Assignments in configuration files are only respected if the dependencies for
115the symbol are satisfied. A warning is printed otherwise. To figure out what
116the dependencies of a symbol are, use one of the :ref:`interactive
117configuration interfaces <menuconfig>` (you can jump directly to a symbol with
118:kbd:`/`), or look up the symbol in the :ref:`Kconfig search page
119<kconfig-search>`.
120
121
122.. _initial-conf:
123
124The Initial Configuration
125*************************
126
127The initial configuration for an application comes from merging configuration
128settings from three sources:
129
1301. A ``BOARD``-specific configuration file stored in
131   :file:`boards/<VENDOR>/<BOARD>/<BOARD>_defconfig`
132
1332. Any CMake cache entries prefix with ``CONFIG_``
134
1353. The application configuration
136
137The application configuration can come from the sources below (each file is
138known as a Kconfig fragment, which are then merged to get the final
139configuration used for a particular build). By default, :file:`prj.conf` is
140used.
141
142#. If ``CONF_FILE`` is set, the configuration file(s) specified in it are
143   merged and used as the application configuration. ``CONF_FILE`` can be set
144   in various ways:
145
146   1. In :file:`CMakeLists.txt`, before calling ``find_package(Zephyr)``
147
148   2. By passing ``-DCONF_FILE=<conf file(s)>``, either directly or via ``west``
149
150   3. From the CMake variable cache
151
152#. Otherwise, if :file:`boards/<BOARD>.conf` exists in the application
153   configuration directory, the result of merging it with :file:`prj.conf` is
154   used.
155
156#. Otherwise, if board revisions are used and
157   :file:`boards/<BOARD>_<revision>.conf` exists in the application
158   configuration directory, the result of merging it with :file:`prj.conf` and
159   :file:`boards/<BOARD>.conf` is used.
160
161#. Otherwise, :file:`prj.conf` is used from the application configuration
162   directory. If it does not exist then a fatal error will be emitted.
163
164Furthermore, applications can have SoC overlay configuration that is applied to
165it, the file :file:`socs/<SOC>_<BOARD_QUALIFIERS>.conf` will be applied if it exists,
166after the main project configuration has been applied and before any board overlay
167configuration files have been applied.
168
169All configuration files will be taken from the application's configuration
170directory except for files with an absolute path that are given with the
171``CONF_FILE``, ``EXTRA_CONF_FILE``, ``DTC_OVERLAY_FILE``, and
172``EXTRA_DTC_OVERLAY_FILE`` arguments.  For these,
173a file in a Zephyr module can be referred by escaping the Zephyr module dir
174variable like this ``\${ZEPHYR_<module>_MODULE_DIR}/<path-to>/<file>``
175when setting any of said variables in the application's :file:`CMakeLists.txt`.
176
177See :ref:`Application Configuration Directory <application-configuration-directory>`
178on how the application configuration directory is defined.
179
180If a symbol is assigned both in :file:`<BOARD>_defconfig` and in the
181application configuration, the value set in the application configuration takes
182precedence.
183
184The merged configuration is saved to :file:`zephyr/.config` in the build
185directory.
186
187As long as :file:`zephyr/.config` exists and is up-to-date (is newer than any
188``BOARD`` and application configuration files), it will be used in preference
189to producing a new merged configuration. :file:`zephyr/.config` is also the
190configuration that gets modified when making changes in the :ref:`interactive
191configuration interfaces <menuconfig>`.
192
193
194Tracking Kconfig symbols
195************************
196
197It is possible to create Kconfig symbols which takes the default value of
198another Kconfig symbol.
199
200This is valuable when you want a symbol specific to an application or subsystem
201but do not want to rely directly on the common symbol. For example, you might
202want to decouple the settings so they can be independently configured, or to
203ensure you always have a locally named setting, even if the external setting name changes.
204is later changed.
205
206For example, consider the common ``FOO_STRING`` setting where a subsystem wants
207to have a ``SUB_FOO_STRING`` but still allow for customization.
208
209This can be done like this:
210
211.. code-block:: kconfig
212
213    config FOO_STRING
214            string "Foo"
215            default "foo"
216
217    config SUB_FOO_STRING
218            string "Sub-foo"
219            default FOO_STRING
220
221This ensures that the default value of ``SUB_FOO_STRING`` is identical to
222``FOO_STRING`` while still allows users to configure both settings
223independently.
224
225It is also possible to make ``SUB_FOO_STRING`` invisible and thereby keep the
226two symbols in sync, unless the value of the tracking symbol is changed in a
227:file:`defconfig` file.
228
229.. code-block:: kconfig
230
231    config FOO_STRING
232            string "Foo"
233            default "foo"
234
235    config SUB_FOO_STRING
236            string
237            default FOO_STRING
238            help
239              Hidden symbol which follows FOO_STRING
240              Can be changed through *.defconfig files.
241
242
243Configuring invisible Kconfig symbols
244*************************************
245
246When making changes to the default configuration for a board, you might have to
247configure invisible symbols. This is done in
248:file:`boards/<VENDOR>/<BOARD>/Kconfig.defconfig`, which is a regular
249:file:`Kconfig` file.
250
251.. note::
252
253    Assignments in :file:`.config` files have no effect on invisible symbols,
254    so this scheme is not just an organizational issue.
255
256Assigning values in :file:`Kconfig.defconfig` relies on defining a Kconfig
257symbol in multiple locations. As an example, say we want to set ``FOO_WIDTH``
258below to 32:
259
260.. code-block:: kconfig
261
262    config FOO_WIDTH
263    	int
264
265To do this, we extend the definition of ``FOO_WIDTH`` as follows, in
266:file:`Kconfig.defconfig`:
267
268.. code-block:: kconfig
269
270    if BOARD_MY_BOARD
271
272    config FOO_WIDTH
273    	default 32
274
275    endif
276
277.. note::
278
279   Since the type of the symbol (``int``) has already been given at the first
280   definition location, it does not need to be repeated here. Only giving the
281   type once at the "base" definition of the symbol is a good idea for reasons
282   explained in :ref:`kconfig_shorthands`.
283
284``default`` values in :file:`Kconfig.defconfig` files have priority over
285``default`` values given on the "base" definition of a symbol. Internally, this
286is implemented by including the :file:`Kconfig.defconfig` files first. Kconfig
287uses the first ``default`` with a satisfied condition, where an empty condition
288corresponds to ``if y`` (is always satisfied).
289
290Note that conditions from surrounding top-level ``if``\ s are propagated to
291symbol properties, so the above ``default`` is equivalent to
292``default 32 if BOARD_MY_BOARD``.
293
294.. _multiple_symbol_definitions:
295
296Multiple symbol definitions
297---------------------------
298
299When a symbol is defined in multiple locations, each definition acts as an
300independent symbol that happens to share the same name. This means that
301properties are not appended to previous definitions. If the conditions
302for **ANY** definition result in the symbol resolving to ``y``, the symbol
303will be ``y``. It is therefore not possible to make the dependencies of a
304symbol more restrictive by defining it in multiple locations.
305
306For example, the dependencies of the symbol ``FOO`` below are satisfied if
307either ``DEP1`` **OR** ``DEP2`` are true, it does not require both:
308
309.. code-block:: none
310
311   config FOO
312      ...
313      depends on DEP1
314
315   config FOO
316      ...
317      depends on DEP2
318
319.. warning::
320   Symbols without explicit dependencies still follow the above rule. A
321   symbol without any dependencies will result in the symbol always being
322   assignable. The definition below will result in ``FOO`` always being
323   enabled by default, regardless of the value of ``DEP1``.
324
325   .. code-block:: kconfig
326
327      config FOO
328         bool "FOO"
329         depends on DEP1
330
331      config FOO
332         default y
333
334   This dependency weakening can be avoided with the :ref:`configdefault
335   <kconfig_extensions>` extension if the desire is only to add a new default
336   without modifying any other behaviour of the symbol.
337
338.. note::
339   When making changes to :file:`Kconfig.defconfig` files, always check the
340   symbol's direct dependencies in one of the :ref:`interactive configuration
341   interfaces <menuconfig>` afterwards. It is often necessary to repeat
342   dependencies from the base definition of the symbol to avoid weakening a
343   symbol's dependencies.
344
345
346Motivation for Kconfig.defconfig files
347--------------------------------------
348
349One motivation for this configuration scheme is to avoid making fixed
350``BOARD``-specific settings configurable in the interactive configuration
351interfaces. If all board configuration were done via :file:`<BOARD>_defconfig`,
352all symbols would have to be visible, as values given in
353:file:`<BOARD>_defconfig` have no effect on invisible symbols.
354
355Having fixed settings be user-configurable would clutter up the configuration
356interfaces and make them harder to understand, and would make it easier to
357accidentally create broken configurations.
358
359When dealing with fixed board-specific settings, also consider whether they
360should be handled via :ref:`devicetree <dt-guide>` instead.
361
362
363Configuring choices
364-------------------
365
366There are two ways to configure a Kconfig ``choice``:
367
3681. By setting one of the choice symbols to ``y`` in a configuration file.
369
370   Setting one choice symbol to ``y`` automatically gives all other choice
371   symbols the value ``n``.
372
373   If multiple choice symbols are set to ``y``, only the last one set to ``y``
374   will be honored (the rest will get the value ``n``). This allows a choice
375   selection from a board :file:`defconfig` file to be overridden from an
376   application :file:`prj.conf` file.
377
3782. By changing the ``default`` of the choice in :file:`Kconfig.defconfig`.
379
380   As with symbols, changing the default for a choice is done by defining the
381   choice in multiple locations. For this to work, the choice must have a name.
382
383   As an example, assume that a choice has the following base definition (here,
384   the name of the choice is ``FOO``):
385
386   .. code-block:: kconfig
387
388       choice FOO
389           bool "Foo choice"
390           default B
391
392       config A
393           bool "A"
394
395       config B
396           bool "B"
397
398       endchoice
399
400   To change the default symbol of ``FOO`` to ``A``, you would add the
401   following definition to :file:`Kconfig.defconfig`:
402
403   .. code-block:: kconfig
404
405       choice FOO
406           default A
407       endchoice
408
409The :file:`Kconfig.defconfig` method should be used when the dependencies of
410the choice might not be satisfied. In that case, you're setting the default
411selection whenever the user makes the choice visible.
412
413
414More Kconfig resources
415======================
416
417The :ref:`kconfig_tips_and_tricks` page has some tips for writing Kconfig
418files.
419
420The :zephyr_file:`kconfiglib.py <scripts/kconfig/kconfiglib.py>` docstring
421(at the top of the file) goes over how symbol values are calculated in detail.
422