kconfig-macro-language.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. ======================
  2. Kconfig macro language
  3. ======================
  4. Concept
  5. -------
  6. The basic idea was inspired by Make. When we look at Make, we notice sort of
  7. two languages in one. One language describes dependency graphs consisting of
  8. targets and prerequisites. The other is a macro language for performing textual
  9. substitution.
  10. There is clear distinction between the two language stages. For example, you
  11. can write a makefile like follows::
  12. APP := foo
  13. SRC := foo.c
  14. CC := gcc
  15. $(APP): $(SRC)
  16. $(CC) -o $(APP) $(SRC)
  17. The macro language replaces the variable references with their expanded form,
  18. and handles as if the source file were input like follows::
  19. foo: foo.c
  20. gcc -o foo foo.c
  21. Then, Make analyzes the dependency graph and determines the targets to be
  22. updated.
  23. The idea is quite similar in Kconfig - it is possible to describe a Kconfig
  24. file like this::
  25. CC := gcc
  26. config CC_HAS_FOO
  27. def_bool $(shell, $(srctree)/scripts/gcc-check-foo.sh $(CC))
  28. The macro language in Kconfig processes the source file into the following
  29. intermediate::
  30. config CC_HAS_FOO
  31. def_bool y
  32. Then, Kconfig moves onto the evaluation stage to resolve inter-symbol
  33. dependency as explained in kconfig-language.rst.
  34. Variables
  35. ---------
  36. Like in Make, a variable in Kconfig works as a macro variable. A macro
  37. variable is expanded "in place" to yield a text string that may then be
  38. expanded further. To get the value of a variable, enclose the variable name in
  39. $( ). The parentheses are required even for single-letter variable names; $X is
  40. a syntax error. The curly brace form as in ${CC} is not supported either.
  41. There are two types of variables: simply expanded variables and recursively
  42. expanded variables.
  43. A simply expanded variable is defined using the := assignment operator. Its
  44. righthand side is expanded immediately upon reading the line from the Kconfig
  45. file.
  46. A recursively expanded variable is defined using the = assignment operator.
  47. Its righthand side is simply stored as the value of the variable without
  48. expanding it in any way. Instead, the expansion is performed when the variable
  49. is used.
  50. There is another type of assignment operator; += is used to append text to a
  51. variable. The righthand side of += is expanded immediately if the lefthand
  52. side was originally defined as a simple variable. Otherwise, its evaluation is
  53. deferred.
  54. The variable reference can take parameters, in the following form::
  55. $(name,arg1,arg2,arg3)
  56. You can consider the parameterized reference as a function. (more precisely,
  57. "user-defined function" in contrast to "built-in function" listed below).
  58. Useful functions must be expanded when they are used since the same function is
  59. expanded differently if different parameters are passed. Hence, a user-defined
  60. function is defined using the = assignment operator. The parameters are
  61. referenced within the body definition with $(1), $(2), etc.
  62. In fact, recursively expanded variables and user-defined functions are the same
  63. internally. (In other words, "variable" is "function with zero argument".)
  64. When we say "variable" in a broad sense, it includes "user-defined function".
  65. Built-in functions
  66. ------------------
  67. Like Make, Kconfig provides several built-in functions. Every function takes a
  68. particular number of arguments.
  69. In Make, every built-in function takes at least one argument. Kconfig allows
  70. zero argument for built-in functions, such as $(fileno), $(lineno). You could
  71. consider those as "built-in variable", but it is just a matter of how we call
  72. it after all. Let's say "built-in function" here to refer to natively supported
  73. functionality.
  74. Kconfig currently supports the following built-in functions.
  75. - $(shell,command)
  76. The "shell" function accepts a single argument that is expanded and passed
  77. to a subshell for execution. The standard output of the command is then read
  78. and returned as the value of the function. Every newline in the output is
  79. replaced with a space. Any trailing newlines are deleted. The standard error
  80. is not returned, nor is any program exit status.
  81. - $(info,text)
  82. The "info" function takes a single argument and prints it to stdout.
  83. It evaluates to an empty string.
  84. - $(warning-if,condition,text)
  85. The "warning-if" function takes two arguments. If the condition part is "y",
  86. the text part is sent to stderr. The text is prefixed with the name of the
  87. current Kconfig file and the current line number.
  88. - $(error-if,condition,text)
  89. The "error-if" function is similar to "warning-if", but it terminates the
  90. parsing immediately if the condition part is "y".
  91. - $(filename)
  92. The 'filename' takes no argument, and $(filename) is expanded to the file
  93. name being parsed.
  94. - $(lineno)
  95. The 'lineno' takes no argument, and $(lineno) is expanded to the line number
  96. being parsed.
  97. Make vs Kconfig
  98. ---------------
  99. Kconfig adopts Make-like macro language, but the function call syntax is
  100. slightly different.
  101. A function call in Make looks like this::
  102. $(func-name arg1,arg2,arg3)
  103. The function name and the first argument are separated by at least one
  104. whitespace. Then, leading whitespaces are trimmed from the first argument,
  105. while whitespaces in the other arguments are kept. You need to use a kind of
  106. trick to start the first parameter with spaces. For example, if you want
  107. to make "info" function print " hello", you can write like follows::
  108. empty :=
  109. space := $(empty) $(empty)
  110. $(info $(space)$(space)hello)
  111. Kconfig uses only commas for delimiters, and keeps all whitespaces in the
  112. function call. Some people prefer putting a space after each comma delimiter::
  113. $(func-name, arg1, arg2, arg3)
  114. In this case, "func-name" will receive " arg1", " arg2", " arg3". The presence
  115. of leading spaces may matter depending on the function. The same applies to
  116. Make - for example, $(subst .c, .o, $(sources)) is a typical mistake; it
  117. replaces ".c" with " .o".
  118. In Make, a user-defined function is referenced by using a built-in function,
  119. 'call', like this::
  120. $(call my-func,arg1,arg2,arg3)
  121. Kconfig invokes user-defined functions and built-in functions in the same way.
  122. The omission of 'call' makes the syntax shorter.
  123. In Make, some functions treat commas verbatim instead of argument separators.
  124. For example, $(shell echo hello, world) runs the command "echo hello, world".
  125. Likewise, $(info hello, world) prints "hello, world" to stdout. You could say
  126. this is _useful_ inconsistency.
  127. In Kconfig, for simpler implementation and grammatical consistency, commas that
  128. appear in the $( ) context are always delimiters. It means::
  129. $(shell, echo hello, world)
  130. is an error because it is passing two parameters where the 'shell' function
  131. accepts only one. To pass commas in arguments, you can use the following trick::
  132. comma := ,
  133. $(shell, echo hello$(comma) world)
  134. Caveats
  135. -------
  136. A variable (or function) cannot be expanded across tokens. So, you cannot use
  137. a variable as a shorthand for an expression that consists of multiple tokens.
  138. The following works::
  139. RANGE_MIN := 1
  140. RANGE_MAX := 3
  141. config FOO
  142. int "foo"
  143. range $(RANGE_MIN) $(RANGE_MAX)
  144. But, the following does not work::
  145. RANGES := 1 3
  146. config FOO
  147. int "foo"
  148. range $(RANGES)
  149. A variable cannot be expanded to any keyword in Kconfig. The following does
  150. not work::
  151. MY_TYPE := tristate
  152. config FOO
  153. $(MY_TYPE) "foo"
  154. default y
  155. Obviously from the design, $(shell command) is expanded in the textual
  156. substitution phase. You cannot pass symbols to the 'shell' function.
  157. The following does not work as expected::
  158. config ENDIAN_FLAG
  159. string
  160. default "-mbig-endian" if CPU_BIG_ENDIAN
  161. default "-mlittle-endian" if CPU_LITTLE_ENDIAN
  162. config CC_HAS_ENDIAN_FLAG
  163. def_bool $(shell $(srctree)/scripts/gcc-check-flag ENDIAN_FLAG)
  164. Instead, you can do like follows so that any function call is statically
  165. expanded::
  166. config CC_HAS_ENDIAN_FLAG
  167. bool
  168. default $(shell $(srctree)/scripts/gcc-check-flag -mbig-endian) if CPU_BIG_ENDIAN
  169. default $(shell $(srctree)/scripts/gcc-check-flag -mlittle-endian) if CPU_LITTLE_ENDIAN