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 in
95   :file:`zephyr/.config`.
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   Furthermore if ``CONF_FILE`` is set as single configuration file of the
153   form :file:`prj_<build>.conf` and if file
154   :file:`boards/<BOARD>_<build>.conf` exists in same folder as file
155   :file:`prj_<build>.conf`, the result of merging :file:`prj_<build>.conf` and
156   :file:`boards/<BOARD>_<build>.conf` is used - note that this feature is
157   deprecated, :ref:`application-file-suffixes` should be used instead.
158
159#. Otherwise, if :file:`boards/<BOARD>.conf` exists in the application
160   configuration directory, the result of merging it with :file:`prj.conf` is
161   used.
162
163#. Otherwise, if board revisions are used and
164   :file:`boards/<BOARD>_<revision>.conf` exists in the application
165   configuration directory, the result of merging it with :file:`prj.conf` and
166   :file:`boards/<BOARD>.conf` is used.
167
168#. Otherwise, :file:`prj.conf` is used from the application configuration
169   directory. If it does not exist then a fatal error will be emitted.
170
171Furthermore, applications can have SoC overlay configuration that is applied to
172it, the file :file:`socs/<SOC>_<BOARD_QUALIFIERS>.conf` will be applied if it exists,
173after the main project configuration has been applied and before any board overlay
174configuration files have been applied.
175
176All configuration files will be taken from the application's configuration
177directory except for files with an absolute path that are given with the
178``CONF_FILE``, ``EXTRA_CONF_FILE``, ``DTC_OVERLAY_FILE``, and
179``EXTRA_DTC_OVERLAY_FILE`` arguments.  For these,
180a file in a Zephyr module can be referred by escaping the Zephyr module dir
181variable like this ``\${ZEPHYR_<module>_MODULE_DIR}/<path-to>/<file>``
182when setting any of said variables in the application's :file:`CMakeLists.txt`.
183
184See :ref:`Application Configuration Directory <application-configuration-directory>`
185on how the application configuration directory is defined.
186
187If a symbol is assigned both in :file:`<BOARD>_defconfig` and in the
188application configuration, the value set in the application configuration takes
189precedence.
190
191The merged configuration is saved to :file:`zephyr/.config` in the build
192directory.
193
194As long as :file:`zephyr/.config` exists and is up-to-date (is newer than any
195``BOARD`` and application configuration files), it will be used in preference
196to producing a new merged configuration. :file:`zephyr/.config` is also the
197configuration that gets modified when making changes in the :ref:`interactive
198configuration interfaces <menuconfig>`.
199
200
201Tracking Kconfig symbols
202************************
203
204It is possible to create Kconfig symbols which takes the default value of
205another Kconfig symbol.
206
207This is valuable when you want a symbol specific to an application or subsystem
208but do not want to rely directly on the common symbol. For example, you might
209want to decouple the settings so they can be independently configured, or to
210ensure you always have a locally named setting, even if the external setting name changes.
211is later changed.
212
213For example, consider the common ``FOO_STRING`` setting where a subsystem wants
214to have a ``SUB_FOO_STRING`` but still allow for customization.
215
216This can be done like this:
217
218.. code-block:: kconfig
219
220    config FOO_STRING
221            string "Foo"
222            default "foo"
223
224    config SUB_FOO_STRING
225            string "Sub-foo"
226            default FOO_STRING
227
228This ensures that the default value of ``SUB_FOO_STRING`` is identical to
229``FOO_STRING`` while still allows users to configure both settings
230independently.
231
232It is also possible to make ``SUB_FOO_STRING`` invisible and thereby keep the
233two symbols in sync, unless the value of the tracking symbol is changed in a
234:file:`defconfig` file.
235
236.. code-block:: kconfig
237
238    config FOO_STRING
239            string "Foo"
240            default "foo"
241
242    config SUB_FOO_STRING
243            string
244            default FOO_STRING
245            help
246              Hidden symbol which follows FOO_STRING
247              Can be changed through *.defconfig files.
248
249
250Configuring invisible Kconfig symbols
251*************************************
252
253When making changes to the default configuration for a board, you might have to
254configure invisible symbols. This is done in
255:file:`boards/<VENDOR>/<BOARD>/Kconfig.defconfig`, which is a regular
256:file:`Kconfig` file.
257
258.. note::
259
260    Assignments in :file:`.config` files have no effect on invisible symbols,
261    so this scheme is not just an organizational issue.
262
263Assigning values in :file:`Kconfig.defconfig` relies on defining a Kconfig
264symbol in multiple locations. As an example, say we want to set ``FOO_WIDTH``
265below to 32:
266
267.. code-block:: kconfig
268
269    config FOO_WIDTH
270    	int
271
272To do this, we extend the definition of ``FOO_WIDTH`` as follows, in
273:file:`Kconfig.defconfig`:
274
275.. code-block:: kconfig
276
277    if BOARD_MY_BOARD
278
279    config FOO_WIDTH
280    	default 32
281
282    endif
283
284.. note::
285
286   Since the type of the symbol (``int``) has already been given at the first
287   definition location, it does not need to be repeated here. Only giving the
288   type once at the "base" definition of the symbol is a good idea for reasons
289   explained in :ref:`kconfig_shorthands`.
290
291``default`` values in :file:`Kconfig.defconfig` files have priority over
292``default`` values given on the "base" definition of a symbol. Internally, this
293is implemented by including the :file:`Kconfig.defconfig` files first. Kconfig
294uses the first ``default`` with a satisfied condition, where an empty condition
295corresponds to ``if y`` (is always satisfied).
296
297Note that conditions from surrounding top-level ``if``\ s are propagated to
298symbol properties, so the above ``default`` is equivalent to
299``default 32 if BOARD_MY_BOARD``.
300
301.. _multiple_symbol_definitions:
302
303Multiple symbol definitions
304---------------------------
305
306When a symbol is defined in multiple locations, each definition acts as an
307independent symbol that happens to share the same name. This means that
308properties are not appended to previous definitions. If the conditions
309for **ANY** definition result in the symbol resolving to ``y``, the symbol
310will be ``y``. It is therefore not possible to make the dependencies of a
311symbol more restrictive by defining it in multiple locations.
312
313For example, the dependencies of the symbol ``FOO`` below are satisfied if
314either ``DEP1`` **OR** ``DEP2`` are true, it does not require both:
315
316.. code-block:: none
317
318   config FOO
319      ...
320      depends on DEP1
321
322   config FOO
323      ...
324      depends on DEP2
325
326.. warning::
327   Symbols without explicit dependencies still follow the above rule. A
328   symbol without any dependencies will result in the symbol always being
329   assignable. The definition below will result in ``FOO`` always being
330   enabled by default, regardless of the value of ``DEP1``.
331
332   .. code-block:: kconfig
333
334      config FOO
335         bool "FOO"
336         depends on DEP1
337
338      config FOO
339         default y
340
341   This dependency weakening can be avoided with the :ref:`configdefault
342   <kconfig_extensions>` extension if the desire is only to add a new default
343   without modifying any other behaviour of the symbol.
344
345.. note::
346   When making changes to :file:`Kconfig.defconfig` files, always check the
347   symbol's direct dependencies in one of the :ref:`interactive configuration
348   interfaces <menuconfig>` afterwards. It is often necessary to repeat
349   dependencies from the base definition of the symbol to avoid weakening a
350   symbol's dependencies.
351
352
353Motivation for Kconfig.defconfig files
354--------------------------------------
355
356One motivation for this configuration scheme is to avoid making fixed
357``BOARD``-specific settings configurable in the interactive configuration
358interfaces. If all board configuration were done via :file:`<BOARD>_defconfig`,
359all symbols would have to be visible, as values given in
360:file:`<BOARD>_defconfig` have no effect on invisible symbols.
361
362Having fixed settings be user-configurable would clutter up the configuration
363interfaces and make them harder to understand, and would make it easier to
364accidentally create broken configurations.
365
366When dealing with fixed board-specific settings, also consider whether they
367should be handled via :ref:`devicetree <dt-guide>` instead.
368
369
370Configuring choices
371-------------------
372
373There are two ways to configure a Kconfig ``choice``:
374
3751. By setting one of the choice symbols to ``y`` in a configuration file.
376
377   Setting one choice symbol to ``y`` automatically gives all other choice
378   symbols the value ``n``.
379
380   If multiple choice symbols are set to ``y``, only the last one set to ``y``
381   will be honored (the rest will get the value ``n``). This allows a choice
382   selection from a board :file:`defconfig` file to be overridden from an
383   application :file:`prj.conf` file.
384
3852. By changing the ``default`` of the choice in :file:`Kconfig.defconfig`.
386
387   As with symbols, changing the default for a choice is done by defining the
388   choice in multiple locations. For this to work, the choice must have a name.
389
390   As an example, assume that a choice has the following base definition (here,
391   the name of the choice is ``FOO``):
392
393   .. code-block:: kconfig
394
395       choice FOO
396           bool "Foo choice"
397           default B
398
399       config A
400           bool "A"
401
402       config B
403           bool "B"
404
405       endchoice
406
407   To change the default symbol of ``FOO`` to ``A``, you would add the
408   following definition to :file:`Kconfig.defconfig`:
409
410   .. code-block:: kconfig
411
412       choice FOO
413           default A
414       endchoice
415
416The :file:`Kconfig.defconfig` method should be used when the dependencies of
417the choice might not be satisfied. In that case, you're setting the default
418selection whenever the user makes the choice visible.
419
420
421More Kconfig resources
422======================
423
424The :ref:`kconfig_tips_and_tricks` page has some tips for writing Kconfig
425files.
426
427The :zephyr_file:`kconfiglib.py <scripts/kconfig/kconfiglib.py>` docstring
428(at the top of the file) goes over how symbol values are calculated in detail.
429