xref: /OK3568_Linux_fs/kernel/Documentation/kbuild/kconfig-macro-language.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun======================
2*4882a593SmuzhiyunKconfig macro language
3*4882a593Smuzhiyun======================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunConcept
6*4882a593Smuzhiyun-------
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunThe basic idea was inspired by Make. When we look at Make, we notice sort of
9*4882a593Smuzhiyuntwo languages in one. One language describes dependency graphs consisting of
10*4882a593Smuzhiyuntargets and prerequisites. The other is a macro language for performing textual
11*4882a593Smuzhiyunsubstitution.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunThere is clear distinction between the two language stages. For example, you
14*4882a593Smuzhiyuncan write a makefile like follows::
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun    APP := foo
17*4882a593Smuzhiyun    SRC := foo.c
18*4882a593Smuzhiyun    CC := gcc
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun    $(APP): $(SRC)
21*4882a593Smuzhiyun            $(CC) -o $(APP) $(SRC)
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunThe macro language replaces the variable references with their expanded form,
24*4882a593Smuzhiyunand handles as if the source file were input like follows::
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun    foo: foo.c
27*4882a593Smuzhiyun            gcc -o foo foo.c
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunThen, Make analyzes the dependency graph and determines the targets to be
30*4882a593Smuzhiyunupdated.
31*4882a593Smuzhiyun
32*4882a593SmuzhiyunThe idea is quite similar in Kconfig - it is possible to describe a Kconfig
33*4882a593Smuzhiyunfile like this::
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun    CC := gcc
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun    config CC_HAS_FOO
38*4882a593Smuzhiyun            def_bool $(shell, $(srctree)/scripts/gcc-check-foo.sh $(CC))
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunThe macro language in Kconfig processes the source file into the following
41*4882a593Smuzhiyunintermediate::
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun    config CC_HAS_FOO
44*4882a593Smuzhiyun            def_bool y
45*4882a593Smuzhiyun
46*4882a593SmuzhiyunThen, Kconfig moves onto the evaluation stage to resolve inter-symbol
47*4882a593Smuzhiyundependency as explained in kconfig-language.rst.
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunVariables
51*4882a593Smuzhiyun---------
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunLike in Make, a variable in Kconfig works as a macro variable.  A macro
54*4882a593Smuzhiyunvariable is expanded "in place" to yield a text string that may then be
55*4882a593Smuzhiyunexpanded further. To get the value of a variable, enclose the variable name in
56*4882a593Smuzhiyun$( ). The parentheses are required even for single-letter variable names; $X is
57*4882a593Smuzhiyuna syntax error. The curly brace form as in ${CC} is not supported either.
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunThere are two types of variables: simply expanded variables and recursively
60*4882a593Smuzhiyunexpanded variables.
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunA simply expanded variable is defined using the := assignment operator. Its
63*4882a593Smuzhiyunrighthand side is expanded immediately upon reading the line from the Kconfig
64*4882a593Smuzhiyunfile.
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunA recursively expanded variable is defined using the = assignment operator.
67*4882a593SmuzhiyunIts righthand side is simply stored as the value of the variable without
68*4882a593Smuzhiyunexpanding it in any way. Instead, the expansion is performed when the variable
69*4882a593Smuzhiyunis used.
70*4882a593Smuzhiyun
71*4882a593SmuzhiyunThere is another type of assignment operator; += is used to append text to a
72*4882a593Smuzhiyunvariable. The righthand side of += is expanded immediately if the lefthand
73*4882a593Smuzhiyunside was originally defined as a simple variable. Otherwise, its evaluation is
74*4882a593Smuzhiyundeferred.
75*4882a593Smuzhiyun
76*4882a593SmuzhiyunThe variable reference can take parameters, in the following form::
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun  $(name,arg1,arg2,arg3)
79*4882a593Smuzhiyun
80*4882a593SmuzhiyunYou can consider the parameterized reference as a function. (more precisely,
81*4882a593Smuzhiyun"user-defined function" in contrast to "built-in function" listed below).
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunUseful functions must be expanded when they are used since the same function is
84*4882a593Smuzhiyunexpanded differently if different parameters are passed. Hence, a user-defined
85*4882a593Smuzhiyunfunction is defined using the = assignment operator. The parameters are
86*4882a593Smuzhiyunreferenced within the body definition with $(1), $(2), etc.
87*4882a593Smuzhiyun
88*4882a593SmuzhiyunIn fact, recursively expanded variables and user-defined functions are the same
89*4882a593Smuzhiyuninternally. (In other words, "variable" is "function with zero argument".)
90*4882a593SmuzhiyunWhen we say "variable" in a broad sense, it includes "user-defined function".
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunBuilt-in functions
94*4882a593Smuzhiyun------------------
95*4882a593Smuzhiyun
96*4882a593SmuzhiyunLike Make, Kconfig provides several built-in functions. Every function takes a
97*4882a593Smuzhiyunparticular number of arguments.
98*4882a593Smuzhiyun
99*4882a593SmuzhiyunIn Make, every built-in function takes at least one argument. Kconfig allows
100*4882a593Smuzhiyunzero argument for built-in functions, such as $(fileno), $(lineno). You could
101*4882a593Smuzhiyunconsider those as "built-in variable", but it is just a matter of how we call
102*4882a593Smuzhiyunit after all. Let's say "built-in function" here to refer to natively supported
103*4882a593Smuzhiyunfunctionality.
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunKconfig currently supports the following built-in functions.
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun - $(shell,command)
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun  The "shell" function accepts a single argument that is expanded and passed
110*4882a593Smuzhiyun  to a subshell for execution. The standard output of the command is then read
111*4882a593Smuzhiyun  and returned as the value of the function. Every newline in the output is
112*4882a593Smuzhiyun  replaced with a space. Any trailing newlines are deleted. The standard error
113*4882a593Smuzhiyun  is not returned, nor is any program exit status.
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun - $(info,text)
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun  The "info" function takes a single argument and prints it to stdout.
118*4882a593Smuzhiyun  It evaluates to an empty string.
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun - $(warning-if,condition,text)
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun  The "warning-if" function takes two arguments. If the condition part is "y",
123*4882a593Smuzhiyun  the text part is sent to stderr. The text is prefixed with the name of the
124*4882a593Smuzhiyun  current Kconfig file and the current line number.
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun - $(error-if,condition,text)
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun  The "error-if" function is similar to "warning-if", but it terminates the
129*4882a593Smuzhiyun  parsing immediately if the condition part is "y".
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun - $(filename)
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun  The 'filename' takes no argument, and $(filename) is expanded to the file
134*4882a593Smuzhiyun  name being parsed.
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun - $(lineno)
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun  The 'lineno' takes no argument, and $(lineno) is expanded to the line number
139*4882a593Smuzhiyun  being parsed.
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun
142*4882a593SmuzhiyunMake vs Kconfig
143*4882a593Smuzhiyun---------------
144*4882a593Smuzhiyun
145*4882a593SmuzhiyunKconfig adopts Make-like macro language, but the function call syntax is
146*4882a593Smuzhiyunslightly different.
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunA function call in Make looks like this::
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun  $(func-name arg1,arg2,arg3)
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunThe function name and the first argument are separated by at least one
153*4882a593Smuzhiyunwhitespace. Then, leading whitespaces are trimmed from the first argument,
154*4882a593Smuzhiyunwhile whitespaces in the other arguments are kept. You need to use a kind of
155*4882a593Smuzhiyuntrick to start the first parameter with spaces. For example, if you want
156*4882a593Smuzhiyunto make "info" function print "  hello", you can write like follows::
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun  empty :=
159*4882a593Smuzhiyun  space := $(empty) $(empty)
160*4882a593Smuzhiyun  $(info $(space)$(space)hello)
161*4882a593Smuzhiyun
162*4882a593SmuzhiyunKconfig uses only commas for delimiters, and keeps all whitespaces in the
163*4882a593Smuzhiyunfunction call. Some people prefer putting a space after each comma delimiter::
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun  $(func-name, arg1, arg2, arg3)
166*4882a593Smuzhiyun
167*4882a593SmuzhiyunIn this case, "func-name" will receive " arg1", " arg2", " arg3". The presence
168*4882a593Smuzhiyunof leading spaces may matter depending on the function. The same applies to
169*4882a593SmuzhiyunMake - for example, $(subst .c, .o, $(sources)) is a typical mistake; it
170*4882a593Smuzhiyunreplaces ".c" with " .o".
171*4882a593Smuzhiyun
172*4882a593SmuzhiyunIn Make, a user-defined function is referenced by using a built-in function,
173*4882a593Smuzhiyun'call', like this::
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun    $(call my-func,arg1,arg2,arg3)
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunKconfig invokes user-defined functions and built-in functions in the same way.
178*4882a593SmuzhiyunThe omission of 'call' makes the syntax shorter.
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunIn Make, some functions treat commas verbatim instead of argument separators.
181*4882a593SmuzhiyunFor example, $(shell echo hello, world) runs the command "echo hello, world".
182*4882a593SmuzhiyunLikewise, $(info hello, world) prints "hello, world" to stdout. You could say
183*4882a593Smuzhiyunthis is _useful_ inconsistency.
184*4882a593Smuzhiyun
185*4882a593SmuzhiyunIn Kconfig, for simpler implementation and grammatical consistency, commas that
186*4882a593Smuzhiyunappear in the $( ) context are always delimiters. It means::
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun  $(shell, echo hello, world)
189*4882a593Smuzhiyun
190*4882a593Smuzhiyunis an error because it is passing two parameters where the 'shell' function
191*4882a593Smuzhiyunaccepts only one. To pass commas in arguments, you can use the following trick::
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun  comma := ,
194*4882a593Smuzhiyun  $(shell, echo hello$(comma) world)
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun
197*4882a593SmuzhiyunCaveats
198*4882a593Smuzhiyun-------
199*4882a593Smuzhiyun
200*4882a593SmuzhiyunA variable (or function) cannot be expanded across tokens. So, you cannot use
201*4882a593Smuzhiyuna variable as a shorthand for an expression that consists of multiple tokens.
202*4882a593SmuzhiyunThe following works::
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun    RANGE_MIN := 1
205*4882a593Smuzhiyun    RANGE_MAX := 3
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun    config FOO
208*4882a593Smuzhiyun            int "foo"
209*4882a593Smuzhiyun            range $(RANGE_MIN) $(RANGE_MAX)
210*4882a593Smuzhiyun
211*4882a593SmuzhiyunBut, the following does not work::
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun    RANGES := 1 3
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun    config FOO
216*4882a593Smuzhiyun            int "foo"
217*4882a593Smuzhiyun            range $(RANGES)
218*4882a593Smuzhiyun
219*4882a593SmuzhiyunA variable cannot be expanded to any keyword in Kconfig.  The following does
220*4882a593Smuzhiyunnot work::
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun    MY_TYPE := tristate
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun    config FOO
225*4882a593Smuzhiyun            $(MY_TYPE) "foo"
226*4882a593Smuzhiyun            default y
227*4882a593Smuzhiyun
228*4882a593SmuzhiyunObviously from the design, $(shell command) is expanded in the textual
229*4882a593Smuzhiyunsubstitution phase. You cannot pass symbols to the 'shell' function.
230*4882a593Smuzhiyun
231*4882a593SmuzhiyunThe following does not work as expected::
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun    config ENDIAN_FLAG
234*4882a593Smuzhiyun            string
235*4882a593Smuzhiyun            default "-mbig-endian" if CPU_BIG_ENDIAN
236*4882a593Smuzhiyun            default "-mlittle-endian" if CPU_LITTLE_ENDIAN
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun    config CC_HAS_ENDIAN_FLAG
239*4882a593Smuzhiyun            def_bool $(shell $(srctree)/scripts/gcc-check-flag ENDIAN_FLAG)
240*4882a593Smuzhiyun
241*4882a593SmuzhiyunInstead, you can do like follows so that any function call is statically
242*4882a593Smuzhiyunexpanded::
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun    config CC_HAS_ENDIAN_FLAG
245*4882a593Smuzhiyun            bool
246*4882a593Smuzhiyun            default $(shell $(srctree)/scripts/gcc-check-flag -mbig-endian) if CPU_BIG_ENDIAN
247*4882a593Smuzhiyun            default $(shell $(srctree)/scripts/gcc-check-flag -mlittle-endian) if CPU_LITTLE_ENDIAN
248