1================
2Kconfig Language
3================
4
5Introduction
6------------
7
8The configuration database is a collection of configuration options
9organized in a tree structure::
10
11	+- Code maturity level options
12	|  +- Prompt for development and/or incomplete code/drivers
13	+- General setup
14	|  +- Networking support
15	|  +- System V IPC
16	|  +- BSD Process Accounting
17	|  +- Sysctl support
18	+- Loadable module support
19	|  +- Enable loadable module support
20	|     +- Set version information on all module symbols
21	|     +- Kernel module loader
22	+- ...
23
24Every entry has its own dependencies. These dependencies are used
25to determine the visibility of an entry. Any child entry is only
26visible if its parent entry is also visible.
27
28Menu entries
29------------
30
31Most entries define a config option; all other entries help to organize
32them. A single configuration option is defined like this::
33
34  config MODVERSIONS
35	bool "Set version information on all module symbols"
36	depends on MODULES
37	help
38	  Usually, modules have to be recompiled whenever you switch to a new
39	  kernel.  ...
40
41Every line starts with a key word and can be followed by multiple
42arguments.  "config" starts a new config entry. The following lines
43define attributes for this config option. Attributes can be the type of
44the config option, input prompt, dependencies, help text and default
45values. A config option can be defined multiple times with the same
46name, but every definition can have only a single input prompt and the
47type must not conflict.
48
49Menu attributes
50---------------
51
52A menu entry can have a number of attributes. Not all of them are
53applicable everywhere (see syntax).
54
55- type definition: "bool"/"tristate"/"string"/"hex"/"int"
56
57  Every config option must have a type. There are only two basic types:
58  tristate and string; the other types are based on these two. The type
59  definition optionally accepts an input prompt, so these two examples
60  are equivalent::
61
62	bool "Networking support"
63
64  and::
65
66	bool
67	prompt "Networking support"
68
69- input prompt: "prompt" <prompt> ["if" <expr>]
70
71  Every menu entry can have at most one prompt, which is used to display
72  to the user. Optionally dependencies only for this prompt can be added
73  with "if".
74
75- default value: "default" <expr> ["if" <expr>]
76
77  A config option can have any number of default values. If multiple
78  default values are visible, only the first defined one is active.
79  Default values are not limited to the menu entry where they are
80  defined. This means the default can be defined somewhere else or be
81  overridden by an earlier definition.
82  The default value is only assigned to the config symbol if no other
83  value was set by the user (via the input prompt above). If an input
84  prompt is visible the default value is presented to the user and can
85  be overridden by him.
86  Optionally, dependencies only for this default value can be added with
87  "if".
88
89 The default value deliberately defaults to 'n' in order to avoid bloating the
90 build. With few exceptions, new config options should not change this. The
91 intent is for "make oldconfig" to add as little as possible to the config from
92 release to release.
93
94 Note:
95	Things that merit "default y/m" include:
96
97	a) A new Kconfig option for something that used to always be built
98	   should be "default y".
99
100	b) A new gatekeeping Kconfig option that hides/shows other Kconfig
101	   options (but does not generate any code of its own), should be
102	   "default y" so people will see those other options.
103
104	c) Sub-driver behavior or similar options for a driver that is
105	   "default n". This allows you to provide sane defaults.
106
107	d) Hardware or infrastructure that everybody expects, such as CONFIG_NET
108	   or CONFIG_BLOCK. These are rare exceptions.
109
110- type definition + default value::
111
112	"def_bool"/"def_tristate" <expr> ["if" <expr>]
113
114  This is a shorthand notation for a type definition plus a value.
115  Optionally dependencies for this default value can be added with "if".
116
117- dependencies: "depends on" <expr>
118
119  This defines a dependency for this menu entry. If multiple
120  dependencies are defined, they are connected with '&&'. Dependencies
121  are applied to all other options within this menu entry (which also
122  accept an "if" expression), so these two examples are equivalent::
123
124	bool "foo" if BAR
125	default y if BAR
126
127  and::
128
129	depends on BAR
130	bool "foo"
131	default y
132
133- reverse dependencies: "select" <symbol> ["if" <expr>]
134
135  While normal dependencies reduce the upper limit of a symbol (see
136  below), reverse dependencies can be used to force a lower limit of
137  another symbol. The value of the current menu symbol is used as the
138  minimal value <symbol> can be set to. If <symbol> is selected multiple
139  times, the limit is set to the largest selection.
140  Reverse dependencies can only be used with boolean or tristate
141  symbols.
142
143  Note:
144	select should be used with care. select will force
145	a symbol to a value without visiting the dependencies.
146	By abusing select you are able to select a symbol FOO even
147	if FOO depends on BAR that is not set.
148	In general use select only for non-visible symbols
149	(no prompts anywhere) and for symbols with no dependencies.
150	That will limit the usefulness but on the other hand avoid
151	the illegal configurations all over.
152
153- weak reverse dependencies: "imply" <symbol> ["if" <expr>]
154
155  This is similar to "select" as it enforces a lower limit on another
156  symbol except that the "implied" symbol's value may still be set to n
157  from a direct dependency or with a visible prompt.
158
159  Given the following example::
160
161    config FOO
162	tristate
163	imply BAZ
164
165    config BAZ
166	tristate
167	depends on BAR
168
169  The following values are possible:
170
171	===		===		=============	==============
172	FOO		BAR		BAZ's default	choice for BAZ
173	===		===		=============	==============
174	n		y		n		N/m/y
175	m		y		m		M/y/n
176	y		y		y		Y/n
177	y		n		*		N
178	===		===		=============	==============
179
180  This is useful e.g. with multiple drivers that want to indicate their
181  ability to hook into a secondary subsystem while allowing the user to
182  configure that subsystem out without also having to unset these drivers.
183
184- limiting menu display: "visible if" <expr>
185
186  This attribute is only applicable to menu blocks, if the condition is
187  false, the menu block is not displayed to the user (the symbols
188  contained there can still be selected by other symbols, though). It is
189  similar to a conditional "prompt" attribute for individual menu
190  entries. Default value of "visible" is true.
191
192- numerical ranges: "range" <symbol> <symbol> ["if" <expr>]
193
194  This allows to limit the range of possible input values for int
195  and hex symbols. The user can only input a value which is larger than
196  or equal to the first symbol and smaller than or equal to the second
197  symbol.
198
199- help text: "help" or "---help---"
200
201  This defines a help text. The end of the help text is determined by
202  the indentation level, this means it ends at the first line which has
203  a smaller indentation than the first line of the help text.
204  "---help---" and "help" do not differ in behaviour, "---help---" is
205  used to help visually separate configuration logic from help within
206  the file as an aid to developers.
207
208- misc options: "option" <symbol>[=<value>]
209
210  Various less common options can be defined via this option syntax,
211  which can modify the behaviour of the menu entry and its config
212  symbol. These options are currently possible:
213
214  - "defconfig_list"
215    This declares a list of default entries which can be used when
216    looking for the default configuration (which is used when the main
217    .config doesn't exists yet.)
218
219  - "modules"
220    This declares the symbol to be used as the MODULES symbol, which
221    enables the third modular state for all config symbols.
222    At most one symbol may have the "modules" option set.
223
224  - "allnoconfig_y"
225    This declares the symbol as one that should have the value y when
226    using "allnoconfig". Used for symbols that hide other symbols.
227
228Menu dependencies
229-----------------
230
231Dependencies define the visibility of a menu entry and can also reduce
232the input range of tristate symbols. The tristate logic used in the
233expressions uses one more state than normal boolean logic to express the
234module state. Dependency expressions have the following syntax::
235
236  <expr> ::= <symbol>                           (1)
237           <symbol> '=' <symbol>                (2)
238           <symbol> '!=' <symbol>               (3)
239           <symbol1> '<' <symbol2>              (4)
240           <symbol1> '>' <symbol2>              (4)
241           <symbol1> '<=' <symbol2>             (4)
242           <symbol1> '>=' <symbol2>             (4)
243           '(' <expr> ')'                       (5)
244           '!' <expr>                           (6)
245           <expr> '&&' <expr>                   (7)
246           <expr> '||' <expr>                   (8)
247
248Expressions are listed in decreasing order of precedence.
249
250(1) Convert the symbol into an expression. Boolean and tristate symbols
251    are simply converted into the respective expression values. All
252    other symbol types result in 'n'.
253(2) If the values of both symbols are equal, it returns 'y',
254    otherwise 'n'.
255(3) If the values of both symbols are equal, it returns 'n',
256    otherwise 'y'.
257(4) If value of <symbol1> is respectively lower, greater, lower-or-equal,
258    or greater-or-equal than value of <symbol2>, it returns 'y',
259    otherwise 'n'.
260(5) Returns the value of the expression. Used to override precedence.
261(6) Returns the result of (2-/expr/).
262(7) Returns the result of min(/expr/, /expr/).
263(8) Returns the result of max(/expr/, /expr/).
264
265An expression can have a value of 'n', 'm' or 'y' (or 0, 1, 2
266respectively for calculations). A menu entry becomes visible when its
267expression evaluates to 'm' or 'y'.
268
269There are two types of symbols: constant and non-constant symbols.
270Non-constant symbols are the most common ones and are defined with the
271'config' statement. Non-constant symbols consist entirely of alphanumeric
272characters or underscores.
273Constant symbols are only part of expressions. Constant symbols are
274always surrounded by single or double quotes. Within the quote, any
275other character is allowed and the quotes can be escaped using '\'.
276
277Menu structure
278--------------
279
280The position of a menu entry in the tree is determined in two ways. First
281it can be specified explicitly::
282
283  menu "Network device support"
284	depends on NET
285
286  config NETDEVICES
287	...
288
289  endmenu
290
291All entries within the "menu" ... "endmenu" block become a submenu of
292"Network device support". All subentries inherit the dependencies from
293the menu entry, e.g. this means the dependency "NET" is added to the
294dependency list of the config option NETDEVICES.
295
296The other way to generate the menu structure is done by analyzing the
297dependencies. If a menu entry somehow depends on the previous entry, it
298can be made a submenu of it. First, the previous (parent) symbol must
299be part of the dependency list and then one of these two conditions
300must be true:
301
302- the child entry must become invisible, if the parent is set to 'n'
303- the child entry must only be visible, if the parent is visible::
304
305    config MODULES
306	bool "Enable loadable module support"
307
308    config MODVERSIONS
309	bool "Set version information on all module symbols"
310	depends on MODULES
311
312    comment "module support disabled"
313	depends on !MODULES
314
315MODVERSIONS directly depends on MODULES, this means it's only visible if
316MODULES is different from 'n'. The comment on the other hand is only
317visible when MODULES is set to 'n'.
318
319
320Kconfig syntax
321--------------
322
323The configuration file describes a series of menu entries, where every
324line starts with a keyword (except help texts). The following keywords
325end a menu entry:
326
327- config
328- menuconfig
329- choice/endchoice
330- comment
331- menu/endmenu
332- if/endif
333- source
334
335The first five also start the definition of a menu entry.
336
337config::
338
339	"config" <symbol>
340	<config options>
341
342This defines a config symbol <symbol> and accepts any of above
343attributes as options.
344
345menuconfig::
346
347	"menuconfig" <symbol>
348	<config options>
349
350This is similar to the simple config entry above, but it also gives a
351hint to front ends, that all suboptions should be displayed as a
352separate list of options. To make sure all the suboptions will really
353show up under the menuconfig entry and not outside of it, every item
354from the <config options> list must depend on the menuconfig symbol.
355In practice, this is achieved by using one of the next two constructs::
356
357  (1):
358  menuconfig M
359  if M
360      config C1
361      config C2
362  endif
363
364  (2):
365  menuconfig M
366  config C1
367      depends on M
368  config C2
369      depends on M
370
371In the following examples (3) and (4), C1 and C2 still have the M
372dependency, but will not appear under menuconfig M anymore, because
373of C0, which doesn't depend on M::
374
375  (3):
376  menuconfig M
377      config C0
378  if M
379      config C1
380      config C2
381  endif
382
383  (4):
384  menuconfig M
385  config C0
386  config C1
387      depends on M
388  config C2
389      depends on M
390
391choices::
392
393	"choice" [symbol]
394	<choice options>
395	<choice block>
396	"endchoice"
397
398This defines a choice group and accepts any of the above attributes as
399options. A choice can only be of type bool or tristate.  If no type is
400specified for a choice, its type will be determined by the type of
401the first choice element in the group or remain unknown if none of the
402choice elements have a type specified, as well.
403
404While a boolean choice only allows a single config entry to be
405selected, a tristate choice also allows any number of config entries
406to be set to 'm'. This can be used if multiple drivers for a single
407hardware exists and only a single driver can be compiled/loaded into
408the kernel, but all drivers can be compiled as modules.
409
410A choice accepts another option "optional", which allows to set the
411choice to 'n' and no entry needs to be selected.
412If no [symbol] is associated with a choice, then you can not have multiple
413definitions of that choice. If a [symbol] is associated to the choice,
414then you may define the same choice (i.e. with the same entries) in another
415place.
416
417comment::
418
419	"comment" <prompt>
420	<comment options>
421
422This defines a comment which is displayed to the user during the
423configuration process and is also echoed to the output files. The only
424possible options are dependencies.
425
426menu::
427
428	"menu" <prompt>
429	<menu options>
430	<menu block>
431	"endmenu"
432
433This defines a menu block, see "Menu structure" above for more
434information. The only possible options are dependencies and "visible"
435attributes.
436
437if::
438
439	"if" <expr>
440	<if block>
441	"endif"
442
443This defines an if block. The dependency expression <expr> is appended
444to all enclosed menu entries.
445
446source::
447
448	"source" <prompt>
449
450This reads the specified configuration file. This file is always parsed.
451
452mainmenu::
453
454	"mainmenu" <prompt>
455
456This sets the config program's title bar if the config program chooses
457to use it. It should be placed at the top of the configuration, before any
458other statement.
459
460'#' Kconfig source file comment:
461
462An unquoted '#' character anywhere in a source file line indicates
463the beginning of a source file comment.  The remainder of that line
464is a comment.
465
466
467Kconfig hints
468-------------
469This is a collection of Kconfig tips, most of which aren't obvious at
470first glance and most of which have become idioms in several Kconfig
471files.
472
473Adding common features and make the usage configurable
474~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
475It is a common idiom to implement a feature/functionality that are
476relevant for some architectures but not all.
477The recommended way to do so is to use a config variable named HAVE_*
478that is defined in a common Kconfig file and selected by the relevant
479architectures.
480An example is the generic IOMAP functionality.
481
482We would in lib/Kconfig see::
483
484  # Generic IOMAP is used to ...
485  config HAVE_GENERIC_IOMAP
486
487  config GENERIC_IOMAP
488	depends on HAVE_GENERIC_IOMAP && FOO
489
490And in lib/Makefile we would see::
491
492	obj-$(CONFIG_GENERIC_IOMAP) += iomap.o
493
494For each architecture using the generic IOMAP functionality we would see::
495
496  config X86
497	select ...
498	select HAVE_GENERIC_IOMAP
499	select ...
500
501Note: we use the existing config option and avoid creating a new
502config variable to select HAVE_GENERIC_IOMAP.
503
504Note: the use of the internal config variable HAVE_GENERIC_IOMAP, it is
505introduced to overcome the limitation of select which will force a
506config option to 'y' no matter the dependencies.
507The dependencies are moved to the symbol GENERIC_IOMAP and we avoid the
508situation where select forces a symbol equals to 'y'.
509
510Adding features that need compiler support
511~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512
513There are several features that need compiler support. The recommended way
514to describe the dependency on the compiler feature is to use "depends on"
515followed by a test macro::
516
517  config STACKPROTECTOR
518	bool "Stack Protector buffer overflow detection"
519	depends on $(cc-option,-fstack-protector)
520	...
521
522If you need to expose a compiler capability to makefiles and/or C source files,
523`CC_HAS_` is the recommended prefix for the config option::
524
525  config CC_HAS_STACKPROTECTOR_NONE
526	def_bool $(cc-option,-fno-stack-protector)
527
528Build as module only
529~~~~~~~~~~~~~~~~~~~~
530To restrict a component build to module-only, qualify its config symbol
531with "depends on m".  E.g.::
532
533  config FOO
534	depends on BAR && m
535
536limits FOO to module (=m) or disabled (=n).
537
538Kconfig recursive dependency limitations
539~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
540
541If you've hit the Kconfig error: "recursive dependency detected" you've run
542into a recursive dependency issue with Kconfig, a recursive dependency can be
543summarized as a circular dependency. The kconfig tools need to ensure that
544Kconfig files comply with specified configuration requirements. In order to do
545that kconfig must determine the values that are possible for all Kconfig
546symbols, this is currently not possible if there is a circular relation
547between two or more Kconfig symbols. For more details refer to the "Simple
548Kconfig recursive issue" subsection below. Kconfig does not do recursive
549dependency resolution; this has a few implications for Kconfig file writers.
550We'll first explain why this issues exists and then provide an example
551technical limitation which this brings upon Kconfig developers. Eager
552developers wishing to try to address this limitation should read the next
553subsections.
554
555Simple Kconfig recursive issue
556~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
557
558Read: Documentation/kbuild/Kconfig.recursion-issue-01
559
560Test with::
561
562  make KBUILD_KCONFIG=Documentation/kbuild/Kconfig.recursion-issue-01 allnoconfig
563
564Cumulative Kconfig recursive issue
565~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
566
567Read: Documentation/kbuild/Kconfig.recursion-issue-02
568
569Test with::
570
571  make KBUILD_KCONFIG=Documentation/kbuild/Kconfig.recursion-issue-02 allnoconfig
572
573Practical solutions to kconfig recursive issue
574~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
575
576Developers who run into the recursive Kconfig issue have two options
577at their disposal. We document them below and also provide a list of
578historical issues resolved through these different solutions.
579
580  a) Remove any superfluous "select FOO" or "depends on FOO"
581  b) Match dependency semantics:
582
583	b1) Swap all "select FOO" to "depends on FOO" or,
584
585	b2) Swap all "depends on FOO" to "select FOO"
586
587The resolution to a) can be tested with the sample Kconfig file
588Documentation/kbuild/Kconfig.recursion-issue-01 through the removal
589of the "select CORE" from CORE_BELL_A_ADVANCED as that is implicit already
590since CORE_BELL_A depends on CORE. At times it may not be possible to remove
591some dependency criteria, for such cases you can work with solution b).
592
593The two different resolutions for b) can be tested in the sample Kconfig file
594Documentation/kbuild/Kconfig.recursion-issue-02.
595
596Below is a list of examples of prior fixes for these types of recursive issues;
597all errors appear to involve one or more select's and one or more "depends on".
598
599============    ===================================
600commit          fix
601============    ===================================
60206b718c01208    select A -> depends on A
603c22eacfe82f9    depends on A -> depends on B
6046a91e854442c    select A -> depends on A
605118c565a8f2e    select A -> select B
606f004e5594705    select A -> depends on A
607c7861f37b4c6    depends on A -> (null)
60880c69915e5fb    select A -> (null)              (1)
609c2218e26c0d0    select A -> depends on A        (1)
610d6ae99d04e1c    select A -> depends on A
61195ca19cf8cbf    select A -> depends on A
6128f057d7bca54    depends on A -> (null)
6138f057d7bca54    depends on A -> select A
614a0701f04846e    select A -> depends on A
6150c8b92f7f259    depends on A -> (null)
616e4e9e0540928    select A -> depends on A        (2)
6177453ea886e87    depends on A > (null)           (1)
6187b1fff7e4fdf    select A -> depends on A
61986c747d2a4f0    select A -> depends on A
620d9f9ab51e55e    select A -> depends on A
6210c51a4d8abd6    depends on A -> select A        (3)
622e98062ed6dc4    select A -> depends on A        (3)
62391e5d284a7f1    select A -> (null)
624============    ===================================
625
626(1) Partial (or no) quote of error.
627(2) That seems to be the gist of that fix.
628(3) Same error.
629
630Future kconfig work
631~~~~~~~~~~~~~~~~~~~
632
633Work on kconfig is welcomed on both areas of clarifying semantics and on
634evaluating the use of a full SAT solver for it. A full SAT solver can be
635desirable to enable more complex dependency mappings and / or queries,
636for instance on possible use case for a SAT solver could be that of handling
637the current known recursive dependency issues. It is not known if this would
638address such issues but such evaluation is desirable. If support for a full SAT
639solver proves too complex or that it cannot address recursive dependency issues
640Kconfig should have at least clear and well defined semantics which also
641addresses and documents limitations or requirements such as the ones dealing
642with recursive dependencies.
643
644Further work on both of these areas is welcomed on Kconfig. We elaborate
645on both of these in the next two subsections.
646
647Semantics of Kconfig
648~~~~~~~~~~~~~~~~~~~~
649
650The use of Kconfig is broad, Linux is now only one of Kconfig's users:
651one study has completed a broad analysis of Kconfig use in 12 projects [0]_.
652Despite its widespread use, and although this document does a reasonable job
653in documenting basic Kconfig syntax a more precise definition of Kconfig
654semantics is welcomed. One project deduced Kconfig semantics through
655the use of the xconfig configurator [1]_. Work should be done to confirm if
656the deduced semantics matches our intended Kconfig design goals.
657
658Having well defined semantics can be useful for tools for practical
659evaluation of depenencies, for instance one such use known case was work to
660express in boolean abstraction of the inferred semantics of Kconfig to
661translate Kconfig logic into boolean formulas and run a SAT solver on this to
662find dead code / features (always inactive), 114 dead features were found in
663Linux using this methodology [1]_ (Section 8: Threats to validity).
664
665Confirming this could prove useful as Kconfig stands as one of the the leading
666industrial variability modeling languages [1]_ [2]_. Its study would help
667evaluate practical uses of such languages, their use was only theoretical
668and real world requirements were not well understood. As it stands though
669only reverse engineering techniques have been used to deduce semantics from
670variability modeling languages such as Kconfig [3]_.
671
672.. [0] http://www.eng.uwaterloo.ca/~shshe/kconfig_semantics.pdf
673.. [1] http://gsd.uwaterloo.ca/sites/default/files/vm-2013-berger.pdf
674.. [2] http://gsd.uwaterloo.ca/sites/default/files/ase241-berger_0.pdf
675.. [3] http://gsd.uwaterloo.ca/sites/default/files/icse2011.pdf
676
677Full SAT solver for Kconfig
678~~~~~~~~~~~~~~~~~~~~~~~~~~~
679
680Although SAT solvers [4]_ haven't yet been used by Kconfig directly, as noted
681in the previous subsection, work has been done however to express in boolean
682abstraction the inferred semantics of Kconfig to translate Kconfig logic into
683boolean formulas and run a SAT solver on it [5]_. Another known related project
684is CADOS [6]_ (former VAMOS [7]_) and the tools, mainly undertaker [8]_, which
685has been introduced first with [9]_.  The basic concept of undertaker is to
686exract variability models from Kconfig, and put them together with a
687propositional formula extracted from CPP #ifdefs and build-rules into a SAT
688solver in order to find dead code, dead files, and dead symbols. If using a SAT
689solver is desirable on Kconfig one approach would be to evaluate repurposing
690such efforts somehow on Kconfig. There is enough interest from mentors of
691existing projects to not only help advise how to integrate this work upstream
692but also help maintain it long term. Interested developers should visit:
693
694http://kernelnewbies.org/KernelProjects/kconfig-sat
695
696.. [4] http://www.cs.cornell.edu/~sabhar/chapters/SATSolvers-KR-Handbook.pdf
697.. [5] http://gsd.uwaterloo.ca/sites/default/files/vm-2013-berger.pdf
698.. [6] https://cados.cs.fau.de
699.. [7] https://vamos.cs.fau.de
700.. [8] https://undertaker.cs.fau.de
701.. [9] https://www4.cs.fau.de/Publications/2011/tartler_11_eurosys.pdf
702