Back to home page

OSCL-LXR

 
 

    


0001 ======================
0002 Kconfig macro language
0003 ======================
0004 
0005 Concept
0006 -------
0007 
0008 The basic idea was inspired by Make. When we look at Make, we notice sort of
0009 two languages in one. One language describes dependency graphs consisting of
0010 targets and prerequisites. The other is a macro language for performing textual
0011 substitution.
0012 
0013 There is clear distinction between the two language stages. For example, you
0014 can write a makefile like follows::
0015 
0016     APP := foo
0017     SRC := foo.c
0018     CC := gcc
0019 
0020     $(APP): $(SRC)
0021             $(CC) -o $(APP) $(SRC)
0022 
0023 The macro language replaces the variable references with their expanded form,
0024 and handles as if the source file were input like follows::
0025 
0026     foo: foo.c
0027             gcc -o foo foo.c
0028 
0029 Then, Make analyzes the dependency graph and determines the targets to be
0030 updated.
0031 
0032 The idea is quite similar in Kconfig - it is possible to describe a Kconfig
0033 file like this::
0034 
0035     CC := gcc
0036 
0037     config CC_HAS_FOO
0038             def_bool $(shell, $(srctree)/scripts/gcc-check-foo.sh $(CC))
0039 
0040 The macro language in Kconfig processes the source file into the following
0041 intermediate::
0042 
0043     config CC_HAS_FOO
0044             def_bool y
0045 
0046 Then, Kconfig moves onto the evaluation stage to resolve inter-symbol
0047 dependency as explained in kconfig-language.rst.
0048 
0049 
0050 Variables
0051 ---------
0052 
0053 Like in Make, a variable in Kconfig works as a macro variable.  A macro
0054 variable is expanded "in place" to yield a text string that may then be
0055 expanded further. To get the value of a variable, enclose the variable name in
0056 $( ). The parentheses are required even for single-letter variable names; $X is
0057 a syntax error. The curly brace form as in ${CC} is not supported either.
0058 
0059 There are two types of variables: simply expanded variables and recursively
0060 expanded variables.
0061 
0062 A simply expanded variable is defined using the := assignment operator. Its
0063 righthand side is expanded immediately upon reading the line from the Kconfig
0064 file.
0065 
0066 A recursively expanded variable is defined using the = assignment operator.
0067 Its righthand side is simply stored as the value of the variable without
0068 expanding it in any way. Instead, the expansion is performed when the variable
0069 is used.
0070 
0071 There is another type of assignment operator; += is used to append text to a
0072 variable. The righthand side of += is expanded immediately if the lefthand
0073 side was originally defined as a simple variable. Otherwise, its evaluation is
0074 deferred.
0075 
0076 The variable reference can take parameters, in the following form::
0077 
0078   $(name,arg1,arg2,arg3)
0079 
0080 You can consider the parameterized reference as a function. (more precisely,
0081 "user-defined function" in contrast to "built-in function" listed below).
0082 
0083 Useful functions must be expanded when they are used since the same function is
0084 expanded differently if different parameters are passed. Hence, a user-defined
0085 function is defined using the = assignment operator. The parameters are
0086 referenced within the body definition with $(1), $(2), etc.
0087 
0088 In fact, recursively expanded variables and user-defined functions are the same
0089 internally. (In other words, "variable" is "function with zero argument".)
0090 When we say "variable" in a broad sense, it includes "user-defined function".
0091 
0092 
0093 Built-in functions
0094 ------------------
0095 
0096 Like Make, Kconfig provides several built-in functions. Every function takes a
0097 particular number of arguments.
0098 
0099 In Make, every built-in function takes at least one argument. Kconfig allows
0100 zero argument for built-in functions, such as $(filename), $(lineno). You could
0101 consider those as "built-in variable", but it is just a matter of how we call
0102 it after all. Let's say "built-in function" here to refer to natively supported
0103 functionality.
0104 
0105 Kconfig currently supports the following built-in functions.
0106 
0107  - $(shell,command)
0108 
0109   The "shell" function accepts a single argument that is expanded and passed
0110   to a subshell for execution. The standard output of the command is then read
0111   and returned as the value of the function. Every newline in the output is
0112   replaced with a space. Any trailing newlines are deleted. The standard error
0113   is not returned, nor is any program exit status.
0114 
0115  - $(info,text)
0116 
0117   The "info" function takes a single argument and prints it to stdout.
0118   It evaluates to an empty string.
0119 
0120  - $(warning-if,condition,text)
0121 
0122   The "warning-if" function takes two arguments. If the condition part is "y",
0123   the text part is sent to stderr. The text is prefixed with the name of the
0124   current Kconfig file and the current line number.
0125 
0126  - $(error-if,condition,text)
0127 
0128   The "error-if" function is similar to "warning-if", but it terminates the
0129   parsing immediately if the condition part is "y".
0130 
0131  - $(filename)
0132 
0133   The 'filename' takes no argument, and $(filename) is expanded to the file
0134   name being parsed.
0135 
0136  - $(lineno)
0137 
0138   The 'lineno' takes no argument, and $(lineno) is expanded to the line number
0139   being parsed.
0140 
0141 
0142 Make vs Kconfig
0143 ---------------
0144 
0145 Kconfig adopts Make-like macro language, but the function call syntax is
0146 slightly different.
0147 
0148 A function call in Make looks like this::
0149 
0150   $(func-name arg1,arg2,arg3)
0151 
0152 The function name and the first argument are separated by at least one
0153 whitespace. Then, leading whitespaces are trimmed from the first argument,
0154 while whitespaces in the other arguments are kept. You need to use a kind of
0155 trick to start the first parameter with spaces. For example, if you want
0156 to make "info" function print "  hello", you can write like follows::
0157 
0158   empty :=
0159   space := $(empty) $(empty)
0160   $(info $(space)$(space)hello)
0161 
0162 Kconfig uses only commas for delimiters, and keeps all whitespaces in the
0163 function call. Some people prefer putting a space after each comma delimiter::
0164 
0165   $(func-name, arg1, arg2, arg3)
0166 
0167 In this case, "func-name" will receive " arg1", " arg2", " arg3". The presence
0168 of leading spaces may matter depending on the function. The same applies to
0169 Make - for example, $(subst .c, .o, $(sources)) is a typical mistake; it
0170 replaces ".c" with " .o".
0171 
0172 In Make, a user-defined function is referenced by using a built-in function,
0173 'call', like this::
0174 
0175     $(call my-func,arg1,arg2,arg3)
0176 
0177 Kconfig invokes user-defined functions and built-in functions in the same way.
0178 The omission of 'call' makes the syntax shorter.
0179 
0180 In Make, some functions treat commas verbatim instead of argument separators.
0181 For example, $(shell echo hello, world) runs the command "echo hello, world".
0182 Likewise, $(info hello, world) prints "hello, world" to stdout. You could say
0183 this is _useful_ inconsistency.
0184 
0185 In Kconfig, for simpler implementation and grammatical consistency, commas that
0186 appear in the $( ) context are always delimiters. It means::
0187 
0188   $(shell, echo hello, world)
0189 
0190 is an error because it is passing two parameters where the 'shell' function
0191 accepts only one. To pass commas in arguments, you can use the following trick::
0192 
0193   comma := ,
0194   $(shell, echo hello$(comma) world)
0195 
0196 
0197 Caveats
0198 -------
0199 
0200 A variable (or function) cannot be expanded across tokens. So, you cannot use
0201 a variable as a shorthand for an expression that consists of multiple tokens.
0202 The following works::
0203 
0204     RANGE_MIN := 1
0205     RANGE_MAX := 3
0206 
0207     config FOO
0208             int "foo"
0209             range $(RANGE_MIN) $(RANGE_MAX)
0210 
0211 But, the following does not work::
0212 
0213     RANGES := 1 3
0214 
0215     config FOO
0216             int "foo"
0217             range $(RANGES)
0218 
0219 A variable cannot be expanded to any keyword in Kconfig.  The following does
0220 not work::
0221 
0222     MY_TYPE := tristate
0223 
0224     config FOO
0225             $(MY_TYPE) "foo"
0226             default y
0227 
0228 Obviously from the design, $(shell command) is expanded in the textual
0229 substitution phase. You cannot pass symbols to the 'shell' function.
0230 
0231 The following does not work as expected::
0232 
0233     config ENDIAN_FLAG
0234             string
0235             default "-mbig-endian" if CPU_BIG_ENDIAN
0236             default "-mlittle-endian" if CPU_LITTLE_ENDIAN
0237 
0238     config CC_HAS_ENDIAN_FLAG
0239             def_bool $(shell $(srctree)/scripts/gcc-check-flag ENDIAN_FLAG)
0240 
0241 Instead, you can do like follows so that any function call is statically
0242 expanded::
0243 
0244     config CC_HAS_ENDIAN_FLAG
0245             bool
0246             default $(shell $(srctree)/scripts/gcc-check-flag -mbig-endian) if CPU_BIG_ENDIAN
0247             default $(shell $(srctree)/scripts/gcc-check-flag -mlittle-endian) if CPU_LITTLE_ENDIAN