uapi: export all headers under uapi directories
[linux-block.git] / Documentation / kbuild / makefiles.txt
CommitLineData
1da177e4
LT
1Linux Kernel Makefiles
2
3This document describes the Linux kernel Makefiles.
4
5=== Table of Contents
6
7 === 1 Overview
8 === 2 Who does what
9 === 3 The kbuild files
10 --- 3.1 Goal definitions
11 --- 3.2 Built-in object goals - obj-y
12 --- 3.3 Loadable module goals - obj-m
13 --- 3.4 Objects which export symbols
14 --- 3.5 Library file goals - lib-y
15 --- 3.6 Descending down in directories
16 --- 3.7 Compilation flags
17 --- 3.8 Command line dependency
18 --- 3.9 Dependency tracking
19 --- 3.10 Special Rules
20a468b5 20 --- 3.11 $(CC) support functions
691ef3e7 21 --- 3.12 $(LD) support functions
1da177e4
LT
22
23 === 4 Host Program support
24 --- 4.1 Simple Host Program
25 --- 4.2 Composite Host Programs
62e22107
MY
26 --- 4.3 Using C++ for host programs
27 --- 4.4 Controlling compiler options for host programs
28 --- 4.5 When host programs are actually built
29 --- 4.6 Using hostprogs-$(CONFIG_FOO)
1da177e4
LT
30
31 === 5 Kbuild clean infrastructure
32
33 === 6 Architecture Makefiles
34 --- 6.1 Set variables to tweak the build to the architecture
052ad274
PA
35 --- 6.2 Add prerequisites to archheaders:
36 --- 6.3 Add prerequisites to archprepare:
37 --- 6.4 List directories to visit when descending
38 --- 6.5 Architecture-specific boot images
39 --- 6.6 Building non-kbuild targets
40 --- 6.7 Commands useful for building a boot image
41 --- 6.8 Custom kbuild commands
42 --- 6.9 Preprocessing linker scripts
43 --- 6.10 Generic header files
fbe6e37d 44 --- 6.11 Post-link pass
1da177e4 45
c7bb349e 46 === 7 Kbuild syntax for exported headers
fcc8487d 47 --- 7.1 no-export-headers
40f1d4c2 48 --- 7.2 genhdr-y
bd73a328
ND
49 --- 7.3 generic-y
50 --- 7.4 generated-y
fcc8487d
ND
51 --- 7.5 mandatory-y
52 --- 7.6 subdir-y
c7bb349e
SR
53
54 === 8 Kbuild Variables
55 === 9 Makefile language
56 === 10 Credits
57 === 11 TODO
1da177e4
LT
58
59=== 1 Overview
60
61The Makefiles have five parts:
62
63 Makefile the top Makefile.
64 .config the kernel configuration file.
65 arch/$(ARCH)/Makefile the arch Makefile.
66 scripts/Makefile.* common rules etc. for all kbuild Makefiles.
67 kbuild Makefiles there are about 500 of these.
68
69The top Makefile reads the .config file, which comes from the kernel
70configuration process.
71
72The top Makefile is responsible for building two major products: vmlinux
73(the resident kernel image) and modules (any module files).
74It builds these goals by recursively descending into the subdirectories of
75the kernel source tree.
76The list of subdirectories which are visited depends upon the kernel
77configuration. The top Makefile textually includes an arch Makefile
78with the name arch/$(ARCH)/Makefile. The arch Makefile supplies
79architecture-specific information to the top Makefile.
80
81Each subdirectory has a kbuild Makefile which carries out the commands
82passed down from above. The kbuild Makefile uses information from the
39e6e9cf 83.config file to construct various file lists used by kbuild to build
1da177e4
LT
84any built-in or modular targets.
85
86scripts/Makefile.* contains all the definitions/rules etc. that
87are used to build the kernel based on the kbuild makefiles.
88
89
90=== 2 Who does what
91
92People have four different relationships with the kernel Makefiles.
93
94*Users* are people who build kernels. These people type commands such as
95"make menuconfig" or "make". They usually do not read or edit
96any kernel Makefiles (or any other source files).
97
98*Normal developers* are people who work on features such as device
99drivers, file systems, and network protocols. These people need to
a07f6033 100maintain the kbuild Makefiles for the subsystem they are
1da177e4
LT
101working on. In order to do this effectively, they need some overall
102knowledge about the kernel Makefiles, plus detailed knowledge about the
103public interface for kbuild.
104
105*Arch developers* are people who work on an entire architecture, such
106as sparc or ia64. Arch developers need to know about the arch Makefile
107as well as kbuild Makefiles.
108
109*Kbuild developers* are people who work on the kernel build system itself.
110These people need to know about all aspects of the kernel Makefiles.
111
112This document is aimed towards normal developers and arch developers.
113
114
115=== 3 The kbuild files
116
117Most Makefiles within the kernel are kbuild Makefiles that use the
a07f6033 118kbuild infrastructure. This chapter introduces the syntax used in the
1da177e4 119kbuild makefiles.
172c3ae3 120The preferred name for the kbuild files are 'Makefile' but 'Kbuild' can
a07f6033 121be used and if both a 'Makefile' and a 'Kbuild' file exists, then the 'Kbuild'
172c3ae3 122file will be used.
1da177e4
LT
123
124Section 3.1 "Goal definitions" is a quick intro, further chapters provide
125more details, with real examples.
126
127--- 3.1 Goal definitions
128
129 Goal definitions are the main part (heart) of the kbuild Makefile.
130 These lines define the files to be built, any special compilation
131 options, and any subdirectories to be entered recursively.
132
133 The most simple kbuild makefile contains one line:
134
135 Example:
136 obj-y += foo.o
137
5c811e59 138 This tells kbuild that there is one object in that directory, named
1da177e4
LT
139 foo.o. foo.o will be built from foo.c or foo.S.
140
141 If foo.o shall be built as a module, the variable obj-m is used.
142 Therefore the following pattern is often used:
143
144 Example:
145 obj-$(CONFIG_FOO) += foo.o
146
147 $(CONFIG_FOO) evaluates to either y (for built-in) or m (for module).
148 If CONFIG_FOO is neither y nor m, then the file will not be compiled
149 nor linked.
150
151--- 3.2 Built-in object goals - obj-y
152
153 The kbuild Makefile specifies object files for vmlinux
a07f6033 154 in the $(obj-y) lists. These lists depend on the kernel
1da177e4
LT
155 configuration.
156
157 Kbuild compiles all the $(obj-y) files. It then calls
158 "$(LD) -r" to merge these files into one built-in.o file.
159 built-in.o is later linked into vmlinux by the parent Makefile.
160
161 The order of files in $(obj-y) is significant. Duplicates in
162 the lists are allowed: the first instance will be linked into
163 built-in.o and succeeding instances will be ignored.
164
165 Link order is significant, because certain functions
166 (module_init() / __initcall) will be called during boot in the
167 order they appear. So keep in mind that changing the link
a07f6033
JE
168 order may e.g. change the order in which your SCSI
169 controllers are detected, and thus your disks are renumbered.
1da177e4
LT
170
171 Example:
172 #drivers/isdn/i4l/Makefile
173 # Makefile for the kernel ISDN subsystem and device drivers.
174 # Each configuration option enables a list of files.
2f5a2f81 175 obj-$(CONFIG_ISDN_I4L) += isdn.o
1da177e4
LT
176 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
177
178--- 3.3 Loadable module goals - obj-m
179
39fed701 180 $(obj-m) specifies object files which are built as loadable
1da177e4
LT
181 kernel modules.
182
183 A module may be built from one source file or several source
184 files. In the case of one source file, the kbuild makefile
185 simply adds the file to $(obj-m).
186
187 Example:
188 #drivers/isdn/i4l/Makefile
189 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
190
191 Note: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to 'm'
192
193 If a kernel module is built from several source files, you specify
4f827280
MM
194 that you want to build a module in the same way as above; however,
195 kbuild needs to know which object files you want to build your
196 module from, so you have to tell it by setting a $(<module_name>-y)
197 variable.
1da177e4
LT
198
199 Example:
200 #drivers/isdn/i4l/Makefile
4f827280
MM
201 obj-$(CONFIG_ISDN_I4L) += isdn.o
202 isdn-y := isdn_net_lib.o isdn_v110.o isdn_common.o
1da177e4
LT
203
204 In this example, the module name will be isdn.o. Kbuild will
4f827280 205 compile the objects listed in $(isdn-y) and then run
1da177e4
LT
206 "$(LD) -r" on the list of these files to generate isdn.o.
207
4f827280
MM
208 Due to kbuild recognizing $(<module_name>-y) for composite objects,
209 you can use the value of a CONFIG_ symbol to optionally include an
210 object file as part of a composite object.
1da177e4
LT
211
212 Example:
213 #fs/ext2/Makefile
4f827280
MM
214 obj-$(CONFIG_EXT2_FS) += ext2.o
215 ext2-y := balloc.o dir.o file.o ialloc.o inode.o ioctl.o \
216 namei.o super.o symlink.o
217 ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o \
218 xattr_trusted.o
219
220 In this example, xattr.o, xattr_user.o and xattr_trusted.o are only
221 part of the composite object ext2.o if $(CONFIG_EXT2_FS_XATTR)
222 evaluates to 'y'.
1da177e4
LT
223
224 Note: Of course, when you are building objects into the kernel,
225 the syntax above will also work. So, if you have CONFIG_EXT2_FS=y,
226 kbuild will build an ext2.o file for you out of the individual
227 parts and then link this into built-in.o, as you would expect.
228
229--- 3.4 Objects which export symbols
230
231 No special notation is required in the makefiles for
232 modules exporting symbols.
233
234--- 3.5 Library file goals - lib-y
235
a07f6033 236 Objects listed with obj-* are used for modules, or
1da177e4
LT
237 combined in a built-in.o for that specific directory.
238 There is also the possibility to list objects that will
239 be included in a library, lib.a.
240 All objects listed with lib-y are combined in a single
241 library for that directory.
5d3f083d
ML
242 Objects that are listed in obj-y and additionally listed in
243 lib-y will not be included in the library, since they will
244 be accessible anyway.
a07f6033 245 For consistency, objects listed in lib-m will be included in lib.a.
1da177e4
LT
246
247 Note that the same kbuild makefile may list files to be built-in
248 and to be part of a library. Therefore the same directory
249 may contain both a built-in.o and a lib.a file.
250
251 Example:
2f5a2f81
MM
252 #arch/x86/lib/Makefile
253 lib-y := delay.o
1da177e4 254
2f5a2f81
MM
255 This will create a library lib.a based on delay.o. For kbuild to
256 actually recognize that there is a lib.a being built, the directory
257 shall be listed in libs-y.
052ad274 258 See also "6.4 List directories to visit when descending".
39e6e9cf 259
a07f6033 260 Use of lib-y is normally restricted to lib/ and arch/*/lib.
1da177e4
LT
261
262--- 3.6 Descending down in directories
263
264 A Makefile is only responsible for building objects in its own
265 directory. Files in subdirectories should be taken care of by
266 Makefiles in these subdirs. The build system will automatically
267 invoke make recursively in subdirectories, provided you let it know of
268 them.
269
a07f6033 270 To do so, obj-y and obj-m are used.
1da177e4
LT
271 ext2 lives in a separate directory, and the Makefile present in fs/
272 tells kbuild to descend down using the following assignment.
273
274 Example:
275 #fs/Makefile
276 obj-$(CONFIG_EXT2_FS) += ext2/
277
278 If CONFIG_EXT2_FS is set to either 'y' (built-in) or 'm' (modular)
279 the corresponding obj- variable will be set, and kbuild will descend
280 down in the ext2 directory.
281 Kbuild only uses this information to decide that it needs to visit
282 the directory, it is the Makefile in the subdirectory that
39fed701 283 specifies what is modular and what is built-in.
1da177e4
LT
284
285 It is good practice to use a CONFIG_ variable when assigning directory
286 names. This allows kbuild to totally skip the directory if the
287 corresponding CONFIG_ option is neither 'y' nor 'm'.
288
289--- 3.7 Compilation flags
290
f77bf014 291 ccflags-y, asflags-y and ldflags-y
c95940f2
NK
292 These three flags apply only to the kbuild makefile in which they
293 are assigned. They are used for all the normal cc, as and ld
294 invocations happening during a recursive build.
f77bf014 295 Note: Flags with the same behaviour were previously named:
c95940f2
NK
296 EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS.
297 They are still supported but their usage is deprecated.
1da177e4 298
eb07e1b4 299 ccflags-y specifies options for compiling with $(CC).
1da177e4
LT
300
301 Example:
eb07e1b4
MM
302 # drivers/acpi/Makefile
303 ccflags-y := -Os
304 ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT
1da177e4
LT
305
306 This variable is necessary because the top Makefile owns the
a0f97e06 307 variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
1da177e4
LT
308 entire tree.
309
eb07e1b4 310 asflags-y specifies options for assembling with $(AS).
1da177e4
LT
311
312 Example:
eb07e1b4
MM
313 #arch/sparc/kernel/Makefile
314 asflags-y := -ansi
1da177e4 315
eb07e1b4 316 ldflags-y specifies options for linking with $(LD).
1da177e4
LT
317
318 Example:
eb07e1b4
MM
319 #arch/cris/boot/compressed/Makefile
320 ldflags-y += -T $(srctree)/$(src)/decompress_$(arch-y).lds
1da177e4 321
720097d8 322 subdir-ccflags-y, subdir-asflags-y
eb07e1b4 323 The two flags listed above are similar to ccflags-y and asflags-y.
c95940f2
NK
324 The difference is that the subdir- variants have effect for the kbuild
325 file where they are present and all subdirectories.
326 Options specified using subdir-* are added to the commandline before
327 the options specified using the non-subdir variants.
720097d8
SR
328
329 Example:
330 subdir-ccflags-y := -Werror
331
1da177e4
LT
332 CFLAGS_$@, AFLAGS_$@
333
334 CFLAGS_$@ and AFLAGS_$@ only apply to commands in current
335 kbuild makefile.
336
337 $(CFLAGS_$@) specifies per-file options for $(CC). The $@
338 part has a literal value which specifies the file that it is for.
339
340 Example:
341 # drivers/scsi/Makefile
342 CFLAGS_aha152x.o = -DAHA152X_STAT -DAUTOCONF
343 CFLAGS_gdth.o = # -DDEBUG_GDTH=2 -D__SERIAL__ -D__COM2__ \
344 -DGDTH_STATISTICS
1da177e4 345
eb07e1b4 346 These two lines specify compilation flags for aha152x.o and gdth.o.
1da177e4
LT
347
348 $(AFLAGS_$@) is a similar feature for source files in assembly
349 languages.
350
351 Example:
352 # arch/arm/kernel/Makefile
eb07e1b4
MM
353 AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
354 AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312
355 AFLAGS_iwmmxt.o := -Wa,-mcpu=iwmmxt
356
1da177e4
LT
357
358--- 3.9 Dependency tracking
359
360 Kbuild tracks dependencies on the following:
361 1) All prerequisite files (both *.c and *.h)
362 2) CONFIG_ options used in all prerequisite files
363 3) Command-line used to compile target
364
365 Thus, if you change an option to $(CC) all affected files will
366 be re-compiled.
367
368--- 3.10 Special Rules
369
370 Special rules are used when the kbuild infrastructure does
371 not provide the required support. A typical example is
372 header files generated during the build process.
5c811e59 373 Another example are the architecture-specific Makefiles which
a07f6033 374 need special rules to prepare boot images etc.
1da177e4
LT
375
376 Special rules are written as normal Make rules.
377 Kbuild is not executing in the directory where the Makefile is
378 located, so all special rules shall provide a relative
379 path to prerequisite files and target files.
380
381 Two variables are used when defining special rules:
382
383 $(src)
384 $(src) is a relative path which points to the directory
385 where the Makefile is located. Always use $(src) when
386 referring to files located in the src tree.
387
388 $(obj)
389 $(obj) is a relative path which points to the directory
390 where the target is saved. Always use $(obj) when
391 referring to generated files.
392
393 Example:
394 #drivers/scsi/Makefile
395 $(obj)/53c8xx_d.h: $(src)/53c7,8xx.scr $(src)/script_asm.pl
396 $(CPP) -DCHIP=810 - < $< | ... $(src)/script_asm.pl
397
398 This is a special rule, following the normal syntax
399 required by make.
400 The target file depends on two prerequisite files. References
401 to the target file are prefixed with $(obj), references
402 to prerequisites are referenced with $(src) (because they are not
403 generated files).
404
5410ecc0
MF
405 $(kecho)
406 echoing information to user in a rule is often a good practice
407 but when execution "make -s" one does not expect to see any output
408 except for warnings/errors.
39fed701 409 To support this kbuild defines $(kecho) which will echo out the
5410ecc0
MF
410 text following $(kecho) to stdout except if "make -s" is used.
411
412 Example:
413 #arch/blackfin/boot/Makefile
414 $(obj)/vmImage: $(obj)/vmlinux.gz
415 $(call if_changed,uimage)
416 @$(kecho) 'Kernel: $@ is ready'
417
418
20a468b5
SR
419--- 3.11 $(CC) support functions
420
a07f6033 421 The kernel may be built with several different versions of
20a468b5 422 $(CC), each supporting a unique set of features and options.
39fed701 423 kbuild provides basic support to check for valid options for $(CC).
e95be9a5 424 $(CC) is usually the gcc compiler, but other alternatives are
20a468b5
SR
425 available.
426
427 as-option
a07f6033
JE
428 as-option is used to check if $(CC) -- when used to compile
429 assembler (*.S) files -- supports the given option. An optional
430 second option may be specified if the first option is not supported.
20a468b5
SR
431
432 Example:
433 #arch/sh/Makefile
434 cflags-y += $(call as-option,-Wa$(comma)-isa=$(isa-y),)
435
a07f6033 436 In the above example, cflags-y will be assigned the option
20a468b5
SR
437 -Wa$(comma)-isa=$(isa-y) if it is supported by $(CC).
438 The second argument is optional, and if supplied will be used
439 if first argument is not supported.
440
f86fd306
SR
441 cc-ldoption
442 cc-ldoption is used to check if $(CC) when used to link object files
0b0bf7a3
RM
443 supports the given option. An optional second option may be
444 specified if first option are not supported.
445
446 Example:
25eb650a 447 #arch/x86/kernel/Makefile
f86fd306 448 vsyscall-flags += $(call cc-ldoption, -Wl$(comma)--hash-style=sysv)
0b0bf7a3 449
5c811e59 450 In the above example, vsyscall-flags will be assigned the option
0b0bf7a3
RM
451 -Wl$(comma)--hash-style=sysv if it is supported by $(CC).
452 The second argument is optional, and if supplied will be used
453 if first argument is not supported.
454
e2414910
AK
455 as-instr
456 as-instr checks if the assembler reports a specific instruction
457 and then outputs either option1 or option2
458 C escapes are supported in the test instruction
222d394d 459 Note: as-instr-option uses KBUILD_AFLAGS for $(AS) options
e2414910 460
20a468b5 461 cc-option
39fed701
GU
462 cc-option is used to check if $(CC) supports a given option, and if
463 not supported to use an optional second option.
20a468b5
SR
464
465 Example:
25eb650a 466 #arch/x86/Makefile
20a468b5
SR
467 cflags-y += $(call cc-option,-march=pentium-mmx,-march=i586)
468
5c811e59 469 In the above example, cflags-y will be assigned the option
a07f6033
JE
470 -march=pentium-mmx if supported by $(CC), otherwise -march=i586.
471 The second argument to cc-option is optional, and if omitted,
20a468b5 472 cflags-y will be assigned no value if first option is not supported.
a0f97e06 473 Note: cc-option uses KBUILD_CFLAGS for $(CC) options
20a468b5
SR
474
475 cc-option-yn
39e6e9cf 476 cc-option-yn is used to check if gcc supports a given option
20a468b5
SR
477 and return 'y' if supported, otherwise 'n'.
478
479 Example:
480 #arch/ppc/Makefile
481 biarch := $(call cc-option-yn, -m32)
482 aflags-$(biarch) += -a32
483 cflags-$(biarch) += -m32
39e6e9cf 484
a07f6033
JE
485 In the above example, $(biarch) is set to y if $(CC) supports the -m32
486 option. When $(biarch) equals 'y', the expanded variables $(aflags-y)
487 and $(cflags-y) will be assigned the values -a32 and -m32,
488 respectively.
a0f97e06 489 Note: cc-option-yn uses KBUILD_CFLAGS for $(CC) options
20a468b5
SR
490
491 cc-option-align
a07f6033
JE
492 gcc versions >= 3.0 changed the type of options used to specify
493 alignment of functions, loops etc. $(cc-option-align), when used
494 as prefix to the align options, will select the right prefix:
20a468b5
SR
495 gcc < 3.00
496 cc-option-align = -malign
497 gcc >= 3.00
498 cc-option-align = -falign
39e6e9cf 499
20a468b5 500 Example:
a0f97e06 501 KBUILD_CFLAGS += $(cc-option-align)-functions=4
20a468b5 502
a07f6033
JE
503 In the above example, the option -falign-functions=4 is used for
504 gcc >= 3.00. For gcc < 3.00, -malign-functions=4 is used.
a0f97e06 505 Note: cc-option-align uses KBUILD_CFLAGS for $(CC) options
39e6e9cf 506
8417da6f
MM
507 cc-disable-warning
508 cc-disable-warning checks if gcc supports a given warning and returns
509 the commandline switch to disable it. This special function is needed,
510 because gcc 4.4 and later accept any unknown -Wno-* option and only
511 warn about it if there is another warning in the source file.
512
513 Example:
514 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
515
516 In the above example, -Wno-unused-but-set-variable will be added to
517 KBUILD_CFLAGS only if gcc really accepts it.
518
20a468b5 519 cc-version
a07f6033 520 cc-version returns a numerical version of the $(CC) compiler version.
20a468b5
SR
521 The format is <major><minor> where both are two digits. So for example
522 gcc 3.41 would return 0341.
523 cc-version is useful when a specific $(CC) version is faulty in one
a07f6033 524 area, for example -mregparm=3 was broken in some gcc versions
20a468b5
SR
525 even though the option was accepted by gcc.
526
527 Example:
25eb650a 528 #arch/x86/Makefile
20a468b5 529 cflags-y += $(shell \
665d92e3 530 if [ $(cc-version) -ge 0300 ] ; then \
20a468b5
SR
531 echo "-mregparm=3"; fi ;)
532
a07f6033 533 In the above example, -mregparm=3 is only used for gcc version greater
20a468b5
SR
534 than or equal to gcc 3.0.
535
536 cc-ifversion
6dcb4e5e
MY
537 cc-ifversion tests the version of $(CC) and equals the fourth parameter
538 if version expression is true, or the fifth (if given) if the version
539 expression is false.
20a468b5
SR
540
541 Example:
542 #fs/reiserfs/Makefile
f77bf014 543 ccflags-y := $(call cc-ifversion, -lt, 0402, -O1)
20a468b5 544
f77bf014 545 In this example, ccflags-y will be assigned the value -O1 if the
20a468b5 546 $(CC) version is less than 4.2.
39e6e9cf 547 cc-ifversion takes all the shell operators:
20a468b5
SR
548 -eq, -ne, -lt, -le, -gt, and -ge
549 The third parameter may be a text as in this example, but it may also
550 be an expanded variable or a macro.
551
7015030f
SR
552 cc-fullversion
553 cc-fullversion is useful when the exact version of gcc is needed.
554 One typical use-case is when a specific GCC version is broken.
555 cc-fullversion points out a more specific version than cc-version does.
556
557 Example:
558 #arch/powerpc/Makefile
665d92e3 559 $(Q)if test "$(cc-fullversion)" = "040200" ; then \
7015030f
SR
560 echo -n '*** GCC-4.2.0 cannot compile the 64-bit powerpc ' ; \
561 false ; \
562 fi
563
39fed701
GU
564 In this example for a specific GCC version the build will error out
565 explaining to the user why it stops.
1da177e4 566
910b4046 567 cc-cross-prefix
631bcfbb 568 cc-cross-prefix is used to check if there exists a $(CC) in path with
910b4046
SR
569 one of the listed prefixes. The first prefix where there exist a
570 prefix$(CC) in the PATH is returned - and if no prefix$(CC) is found
571 then nothing is returned.
572 Additional prefixes are separated by a single space in the
573 call of cc-cross-prefix.
631bcfbb
GU
574 This functionality is useful for architecture Makefiles that try
575 to set CROSS_COMPILE to well-known values but may have several
910b4046 576 values to select between.
631bcfbb
GU
577 It is recommended only to try to set CROSS_COMPILE if it is a cross
578 build (host arch is different from target arch). And if CROSS_COMPILE
910b4046
SR
579 is already set then leave it with the old value.
580
581 Example:
582 #arch/m68k/Makefile
583 ifneq ($(SUBARCH),$(ARCH))
584 ifeq ($(CROSS_COMPILE),)
585 CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu-)
586 endif
587 endif
588
691ef3e7
SR
589--- 3.12 $(LD) support functions
590
591 ld-option
592 ld-option is used to check if $(LD) supports the supplied option.
593 ld-option takes two options as arguments.
594 The second argument is an optional option that can be used if the
595 first option is not supported by $(LD).
596
597 Example:
598 #Makefile
5b83df2b 599 LDFLAGS_vmlinux += $(call ld-option, -X)
691ef3e7
SR
600
601
1da177e4
LT
602=== 4 Host Program support
603
604Kbuild supports building executables on the host for use during the
605compilation stage.
606Two steps are required in order to use a host executable.
607
608The first step is to tell kbuild that a host program exists. This is
609done utilising the variable hostprogs-y.
610
611The second step is to add an explicit dependency to the executable.
39e6e9cf 612This can be done in two ways. Either add the dependency in a rule,
1da177e4
LT
613or utilise the variable $(always).
614Both possibilities are described in the following.
615
616--- 4.1 Simple Host Program
617
618 In some cases there is a need to compile and run a program on the
619 computer where the build is running.
620 The following line tells kbuild that the program bin2hex shall be
621 built on the build host.
622
623 Example:
624 hostprogs-y := bin2hex
625
626 Kbuild assumes in the above example that bin2hex is made from a single
627 c-source file named bin2hex.c located in the same directory as
628 the Makefile.
39e6e9cf 629
1da177e4
LT
630--- 4.2 Composite Host Programs
631
632 Host programs can be made up based on composite objects.
633 The syntax used to define composite objects for host programs is
634 similar to the syntax used for kernel objects.
5d3f083d 635 $(<executable>-objs) lists all objects used to link the final
1da177e4
LT
636 executable.
637
638 Example:
639 #scripts/lxdialog/Makefile
39e6e9cf 640 hostprogs-y := lxdialog
1da177e4
LT
641 lxdialog-objs := checklist.o lxdialog.o
642
643 Objects with extension .o are compiled from the corresponding .c
a07f6033 644 files. In the above example, checklist.c is compiled to checklist.o
1da177e4 645 and lxdialog.c is compiled to lxdialog.o.
a07f6033 646 Finally, the two .o files are linked to the executable, lxdialog.
1da177e4
LT
647 Note: The syntax <executable>-y is not permitted for host-programs.
648
62e22107 649--- 4.3 Using C++ for host programs
1da177e4
LT
650
651 kbuild offers support for host programs written in C++. This was
652 introduced solely to support kconfig, and is not recommended
653 for general use.
654
655 Example:
656 #scripts/kconfig/Makefile
657 hostprogs-y := qconf
658 qconf-cxxobjs := qconf.o
659
660 In the example above the executable is composed of the C++ file
661 qconf.cc - identified by $(qconf-cxxobjs).
39e6e9cf 662
39fed701 663 If qconf is composed of a mixture of .c and .cc files, then an
1da177e4
LT
664 additional line can be used to identify this.
665
666 Example:
667 #scripts/kconfig/Makefile
668 hostprogs-y := qconf
669 qconf-cxxobjs := qconf.o
670 qconf-objs := check.o
39e6e9cf 671
62e22107 672--- 4.4 Controlling compiler options for host programs
1da177e4
LT
673
674 When compiling host programs, it is possible to set specific flags.
675 The programs will always be compiled utilising $(HOSTCC) passed
676 the options specified in $(HOSTCFLAGS).
677 To set flags that will take effect for all host programs created
a07f6033 678 in that Makefile, use the variable HOST_EXTRACFLAGS.
1da177e4
LT
679
680 Example:
681 #scripts/lxdialog/Makefile
682 HOST_EXTRACFLAGS += -I/usr/include/ncurses
39e6e9cf 683
1da177e4
LT
684 To set specific flags for a single file the following construction
685 is used:
686
687 Example:
688 #arch/ppc64/boot/Makefile
689 HOSTCFLAGS_piggyback.o := -DKERNELBASE=$(KERNELBASE)
39e6e9cf 690
1da177e4 691 It is also possible to specify additional options to the linker.
39e6e9cf 692
1da177e4
LT
693 Example:
694 #scripts/kconfig/Makefile
695 HOSTLOADLIBES_qconf := -L$(QTDIR)/lib
696
a07f6033
JE
697 When linking qconf, it will be passed the extra option
698 "-L$(QTDIR)/lib".
39e6e9cf 699
62e22107 700--- 4.5 When host programs are actually built
1da177e4
LT
701
702 Kbuild will only build host-programs when they are referenced
703 as a prerequisite.
704 This is possible in two ways:
705
706 (1) List the prerequisite explicitly in a special rule.
707
708 Example:
709 #drivers/pci/Makefile
710 hostprogs-y := gen-devlist
711 $(obj)/devlist.h: $(src)/pci.ids $(obj)/gen-devlist
712 ( cd $(obj); ./gen-devlist ) < $<
713
39e6e9cf 714 The target $(obj)/devlist.h will not be built before
1da177e4
LT
715 $(obj)/gen-devlist is updated. Note that references to
716 the host programs in special rules must be prefixed with $(obj).
717
718 (2) Use $(always)
719 When there is no suitable special rule, and the host program
720 shall be built when a makefile is entered, the $(always)
721 variable shall be used.
722
723 Example:
724 #scripts/lxdialog/Makefile
725 hostprogs-y := lxdialog
726 always := $(hostprogs-y)
727
728 This will tell kbuild to build lxdialog even if not referenced in
729 any rule.
730
62e22107 731--- 4.6 Using hostprogs-$(CONFIG_FOO)
1da177e4 732
39e6e9cf 733 A typical pattern in a Kbuild file looks like this:
1da177e4
LT
734
735 Example:
736 #scripts/Makefile
737 hostprogs-$(CONFIG_KALLSYMS) += kallsyms
738
739 Kbuild knows about both 'y' for built-in and 'm' for module.
39fed701 740 So if a config symbol evaluates to 'm', kbuild will still build
a07f6033
JE
741 the binary. In other words, Kbuild handles hostprogs-m exactly
742 like hostprogs-y. But only hostprogs-y is recommended to be used
743 when no CONFIG symbols are involved.
1da177e4
LT
744
745=== 5 Kbuild clean infrastructure
746
a07f6033 747"make clean" deletes most generated files in the obj tree where the kernel
1da177e4
LT
748is compiled. This includes generated files such as host programs.
749Kbuild knows targets listed in $(hostprogs-y), $(hostprogs-m), $(always),
750$(extra-y) and $(targets). They are all deleted during "make clean".
751Files matching the patterns "*.[oas]", "*.ko", plus some additional files
752generated by kbuild are deleted all over the kernel src tree when
753"make clean" is executed.
754
755Additional files can be specified in kbuild makefiles by use of $(clean-files).
756
757 Example:
97659181
MM
758 #lib/Makefile
759 clean-files := crc32table.h
1da177e4 760
bd55daf4
JR
761When executing "make clean", the file "crc32table.h" will be deleted.
762Kbuild will assume files to be in the same relative directory as the
97659181 763Makefile, except if prefixed with $(objtree).
1da177e4 764
39e6e9cf
BH
765To delete a directory hierarchy use:
766
1da177e4
LT
767 Example:
768 #scripts/package/Makefile
769 clean-dirs := $(objtree)/debian/
770
97659181
MM
771This will delete the directory debian in the toplevel directory, including all
772subdirectories.
1da177e4 773
ef8ff89b
MM
774To exclude certain files from make clean, use the $(no-clean-files) variable.
775This is only a special case used in the top level Kbuild file:
776
777 Example:
778 #Kbuild
779 no-clean-files := $(bounds-file) $(offsets-file)
780
1da177e4
LT
781Usually kbuild descends down in subdirectories due to "obj-* := dir/",
782but in the architecture makefiles where the kbuild infrastructure
783is not sufficient this sometimes needs to be explicit.
784
785 Example:
25eb650a 786 #arch/x86/boot/Makefile
1da177e4
LT
787 subdir- := compressed/
788
789The above assignment instructs kbuild to descend down in the
790directory compressed/ when "make clean" is executed.
791
39fed701 792To support the clean infrastructure in the Makefiles that build the
1da177e4
LT
793final bootimage there is an optional target named archclean:
794
795 Example:
25eb650a 796 #arch/x86/Makefile
1da177e4 797 archclean:
25eb650a 798 $(Q)$(MAKE) $(clean)=arch/x86/boot
1da177e4 799
25eb650a
WG
800When "make clean" is executed, make will descend down in arch/x86/boot,
801and clean as usual. The Makefile located in arch/x86/boot/ may use
1da177e4
LT
802the subdir- trick to descend further down.
803
804Note 1: arch/$(ARCH)/Makefile cannot use "subdir-", because that file is
805included in the top level makefile, and the kbuild infrastructure
806is not operational at that point.
807
808Note 2: All directories listed in core-y, libs-y, drivers-y and net-y will
809be visited during "make clean".
810
811=== 6 Architecture Makefiles
812
813The top level Makefile sets up the environment and does the preparation,
814before starting to descend down in the individual directories.
a07f6033
JE
815The top level makefile contains the generic part, whereas
816arch/$(ARCH)/Makefile contains what is required to set up kbuild
817for said architecture.
818To do so, arch/$(ARCH)/Makefile sets up a number of variables and defines
1da177e4
LT
819a few targets.
820
a07f6033
JE
821When kbuild executes, the following steps are followed (roughly):
8221) Configuration of the kernel => produce .config
1da177e4 8232) Store kernel version in include/linux/version.h
b22ae40e 8243) Updating all other prerequisites to the target prepare:
1da177e4 825 - Additional prerequisites are specified in arch/$(ARCH)/Makefile
b22ae40e 8264) Recursively descend down in all directories listed in
1da177e4 827 init-* core* drivers-* net-* libs-* and build all targets.
a07f6033 828 - The values of the above variables are expanded in arch/$(ARCH)/Makefile.
b22ae40e 8295) All object files are then linked and the resulting file vmlinux is
a07f6033 830 located at the root of the obj tree.
1da177e4
LT
831 The very first objects linked are listed in head-y, assigned by
832 arch/$(ARCH)/Makefile.
b22ae40e 8336) Finally, the architecture-specific part does any required post processing
1da177e4
LT
834 and builds the final bootimage.
835 - This includes building boot records
5c811e59 836 - Preparing initrd images and the like
1da177e4
LT
837
838
839--- 6.1 Set variables to tweak the build to the architecture
840
841 LDFLAGS Generic $(LD) options
842
843 Flags used for all invocations of the linker.
844 Often specifying the emulation is sufficient.
845
846 Example:
847 #arch/s390/Makefile
848 LDFLAGS := -m elf_s390
f77bf014 849 Note: ldflags-y can be used to further customise
a9af3305 850 the flags used. See chapter 3.7.
39e6e9cf 851
1da177e4
LT
852 LDFLAGS_MODULE Options for $(LD) when linking modules
853
854 LDFLAGS_MODULE is used to set specific flags for $(LD) when
855 linking the .ko files used for modules.
856 Default is "-r", for relocatable output.
857
858 LDFLAGS_vmlinux Options for $(LD) when linking vmlinux
859
860 LDFLAGS_vmlinux is used to specify additional flags to pass to
a07f6033 861 the linker when linking the final vmlinux image.
1da177e4
LT
862 LDFLAGS_vmlinux uses the LDFLAGS_$@ support.
863
864 Example:
25eb650a 865 #arch/x86/Makefile
1da177e4
LT
866 LDFLAGS_vmlinux := -e stext
867
868 OBJCOPYFLAGS objcopy flags
869
870 When $(call if_changed,objcopy) is used to translate a .o file,
a07f6033 871 the flags specified in OBJCOPYFLAGS will be used.
1da177e4
LT
872 $(call if_changed,objcopy) is often used to generate raw binaries on
873 vmlinux.
874
875 Example:
876 #arch/s390/Makefile
877 OBJCOPYFLAGS := -O binary
878
879 #arch/s390/boot/Makefile
880 $(obj)/image: vmlinux FORCE
881 $(call if_changed,objcopy)
882
a07f6033 883 In this example, the binary $(obj)/image is a binary version of
1da177e4
LT
884 vmlinux. The usage of $(call if_changed,xxx) will be described later.
885
222d394d 886 KBUILD_AFLAGS $(AS) assembler flags
1da177e4
LT
887
888 Default value - see top level Makefile
889 Append or modify as required per architecture.
890
891 Example:
892 #arch/sparc64/Makefile
222d394d 893 KBUILD_AFLAGS += -m64 -mcpu=ultrasparc
1da177e4 894
a0f97e06 895 KBUILD_CFLAGS $(CC) compiler flags
1da177e4
LT
896
897 Default value - see top level Makefile
898 Append or modify as required per architecture.
899
a0f97e06 900 Often, the KBUILD_CFLAGS variable depends on the configuration.
1da177e4
LT
901
902 Example:
ff4eb04c
PB
903 #arch/x86/boot/compressed/Makefile
904 cflags-$(CONFIG_X86_32) := -march=i386
905 cflags-$(CONFIG_X86_64) := -mcmodel=small
a0f97e06 906 KBUILD_CFLAGS += $(cflags-y)
1da177e4
LT
907
908 Many arch Makefiles dynamically run the target C compiler to
909 probe supported options:
910
25eb650a 911 #arch/x86/Makefile
1da177e4
LT
912
913 ...
914 cflags-$(CONFIG_MPENTIUMII) += $(call cc-option,\
915 -march=pentium2,-march=i686)
916 ...
917 # Disable unit-at-a-time mode ...
a0f97e06 918 KBUILD_CFLAGS += $(call cc-option,-fno-unit-at-a-time)
1da177e4
LT
919 ...
920
921
a07f6033 922 The first example utilises the trick that a config option expands
1da177e4
LT
923 to 'y' when selected.
924
80c00ba9 925 KBUILD_AFLAGS_KERNEL $(AS) options specific for built-in
1da177e4 926
80c00ba9 927 $(KBUILD_AFLAGS_KERNEL) contains extra C compiler flags used to compile
1da177e4
LT
928 resident kernel code.
929
6588169d 930 KBUILD_AFLAGS_MODULE Options for $(AS) when building modules
1da177e4 931
39fed701 932 $(KBUILD_AFLAGS_MODULE) is used to add arch-specific options that
6588169d
SR
933 are used for $(AS).
934 From commandline AFLAGS_MODULE shall be used (see kbuild.txt).
1da177e4 935
80c00ba9
SR
936 KBUILD_CFLAGS_KERNEL $(CC) options specific for built-in
937
938 $(KBUILD_CFLAGS_KERNEL) contains extra C compiler flags used to compile
939 resident kernel code.
940
6588169d
SR
941 KBUILD_CFLAGS_MODULE Options for $(CC) when building modules
942
39fed701 943 $(KBUILD_CFLAGS_MODULE) is used to add arch-specific options that
6588169d
SR
944 are used for $(CC).
945 From commandline CFLAGS_MODULE shall be used (see kbuild.txt).
946
947 KBUILD_LDFLAGS_MODULE Options for $(LD) when linking modules
948
39fed701 949 $(KBUILD_LDFLAGS_MODULE) is used to add arch-specific options
6588169d
SR
950 used when linking modules. This is often a linker script.
951 From commandline LDFLAGS_MODULE shall be used (see kbuild.txt).
39e6e9cf 952
40df759e
MM
953 KBUILD_ARFLAGS Options for $(AR) when creating archives
954
955 $(KBUILD_ARFLAGS) set by the top level Makefile to "D" (deterministic
956 mode) if this option is supported by $(AR).
957
61754c18
MM
958 ARCH_CPPFLAGS, ARCH_AFLAGS, ARCH_CFLAGS Overrides the kbuild defaults
959
960 These variables are appended to the KBUILD_CPPFLAGS,
961 KBUILD_AFLAGS, and KBUILD_CFLAGS, respectively, after the
962 top-level Makefile has set any other flags. This provides a
963 means for an architecture to override the defaults.
964
965
052ad274
PA
966--- 6.2 Add prerequisites to archheaders:
967
968 The archheaders: rule is used to generate header files that
969 may be installed into user space by "make header_install" or
970 "make headers_install_all". In order to support
971 "make headers_install_all", this target has to be able to run
972 on an unconfigured tree, or a tree configured for another
973 architecture.
974
975 It is run before "make archprepare" when run on the
976 architecture itself.
977
978
979--- 6.3 Add prerequisites to archprepare:
1da177e4 980
a07f6033 981 The archprepare: rule is used to list prerequisites that need to be
1da177e4 982 built before starting to descend down in the subdirectories.
a07f6033 983 This is usually used for header files containing assembler constants.
1da177e4
LT
984
985 Example:
5bb78269
SR
986 #arch/arm/Makefile
987 archprepare: maketools
1da177e4 988
a07f6033 989 In this example, the file target maketools will be processed
5bb78269 990 before descending down in the subdirectories.
1da177e4
LT
991 See also chapter XXX-TODO that describe how kbuild supports
992 generating offset header files.
993
994
052ad274 995--- 6.4 List directories to visit when descending
1da177e4
LT
996
997 An arch Makefile cooperates with the top Makefile to define variables
998 which specify how to build the vmlinux file. Note that there is no
999 corresponding arch-specific section for modules; the module-building
1000 machinery is all architecture-independent.
1001
39e6e9cf 1002
1da177e4
LT
1003 head-y, init-y, core-y, libs-y, drivers-y, net-y
1004
a07f6033
JE
1005 $(head-y) lists objects to be linked first in vmlinux.
1006 $(libs-y) lists directories where a lib.a archive can be located.
5c811e59 1007 The rest list directories where a built-in.o object file can be
a07f6033 1008 located.
1da177e4
LT
1009
1010 $(init-y) objects will be located after $(head-y).
1011 Then the rest follows in this order:
1012 $(core-y), $(libs-y), $(drivers-y) and $(net-y).
1013
a07f6033 1014 The top level Makefile defines values for all generic directories,
5c811e59 1015 and arch/$(ARCH)/Makefile only adds architecture-specific directories.
1da177e4
LT
1016
1017 Example:
1018 #arch/sparc64/Makefile
1019 core-y += arch/sparc64/kernel/
1020 libs-y += arch/sparc64/prom/ arch/sparc64/lib/
1021 drivers-$(CONFIG_OPROFILE) += arch/sparc64/oprofile/
1022
1023
052ad274 1024--- 6.5 Architecture-specific boot images
1da177e4
LT
1025
1026 An arch Makefile specifies goals that take the vmlinux file, compress
1027 it, wrap it in bootstrapping code, and copy the resulting files
1028 somewhere. This includes various kinds of installation commands.
1029 The actual goals are not standardized across architectures.
1030
1031 It is common to locate any additional processing in a boot/
1032 directory below arch/$(ARCH)/.
1033
1034 Kbuild does not provide any smart way to support building a
1035 target specified in boot/. Therefore arch/$(ARCH)/Makefile shall
1036 call make manually to build a target in boot/.
1037
1038 The recommended approach is to include shortcuts in
1039 arch/$(ARCH)/Makefile, and use the full path when calling down
1040 into the arch/$(ARCH)/boot/Makefile.
1041
1042 Example:
25eb650a
WG
1043 #arch/x86/Makefile
1044 boot := arch/x86/boot
1da177e4
LT
1045 bzImage: vmlinux
1046 $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
1047
1048 "$(Q)$(MAKE) $(build)=<dir>" is the recommended way to invoke
1049 make in a subdirectory.
1050
5c811e59 1051 There are no rules for naming architecture-specific targets,
1da177e4 1052 but executing "make help" will list all relevant targets.
a07f6033 1053 To support this, $(archhelp) must be defined.
1da177e4
LT
1054
1055 Example:
25eb650a 1056 #arch/x86/Makefile
1da177e4
LT
1057 define archhelp
1058 echo '* bzImage - Image (arch/$(ARCH)/boot/bzImage)'
39e6e9cf 1059 endif
1da177e4
LT
1060
1061 When make is executed without arguments, the first goal encountered
1062 will be built. In the top level Makefile the first goal present
1063 is all:.
a07f6033
JE
1064 An architecture shall always, per default, build a bootable image.
1065 In "make help", the default goal is highlighted with a '*'.
1da177e4
LT
1066 Add a new prerequisite to all: to select a default goal different
1067 from vmlinux.
1068
1069 Example:
25eb650a 1070 #arch/x86/Makefile
39e6e9cf 1071 all: bzImage
1da177e4
LT
1072
1073 When "make" is executed without arguments, bzImage will be built.
1074
052ad274 1075--- 6.6 Building non-kbuild targets
1da177e4
LT
1076
1077 extra-y
1078
39fed701 1079 extra-y specifies additional targets created in the current
1da177e4
LT
1080 directory, in addition to any targets specified by obj-*.
1081
1082 Listing all targets in extra-y is required for two purposes:
1083 1) Enable kbuild to check changes in command lines
1084 - When $(call if_changed,xxx) is used
1085 2) kbuild knows what files to delete during "make clean"
1086
1087 Example:
25eb650a 1088 #arch/x86/kernel/Makefile
1da177e4
LT
1089 extra-y := head.o init_task.o
1090
a07f6033 1091 In this example, extra-y is used to list object files that
1da177e4
LT
1092 shall be built, but shall not be linked as part of built-in.o.
1093
39e6e9cf 1094
052ad274 1095--- 6.7 Commands useful for building a boot image
1da177e4
LT
1096
1097 Kbuild provides a few macros that are useful when building a
1098 boot image.
1099
1100 if_changed
1101
1102 if_changed is the infrastructure used for the following commands.
1103
1104 Usage:
1105 target: source(s) FORCE
ef80f0a1 1106 $(call if_changed,ld/objcopy/gzip/...)
1da177e4 1107
a07f6033 1108 When the rule is evaluated, it is checked to see if any files
5c811e59 1109 need an update, or the command line has changed since the last
1da177e4
LT
1110 invocation. The latter will force a rebuild if any options
1111 to the executable have changed.
1112 Any target that utilises if_changed must be listed in $(targets),
1113 otherwise the command line check will fail, and the target will
1114 always be built.
1115 Assignments to $(targets) are without $(obj)/ prefix.
1116 if_changed may be used in conjunction with custom commands as
052ad274 1117 defined in 6.8 "Custom kbuild commands".
49490571 1118
1da177e4 1119 Note: It is a typical mistake to forget the FORCE prerequisite.
49490571
PBG
1120 Another common pitfall is that whitespace is sometimes
1121 significant; for instance, the below will fail (note the extra space
1122 after the comma):
1123 target: source(s) FORCE
ef80f0a1 1124 #WRONG!# $(call if_changed, ld/objcopy/gzip/...)
1da177e4
LT
1125
1126 ld
a07f6033 1127 Link target. Often, LDFLAGS_$@ is used to set specific options to ld.
39e6e9cf 1128
1da177e4
LT
1129 objcopy
1130 Copy binary. Uses OBJCOPYFLAGS usually specified in
1131 arch/$(ARCH)/Makefile.
1132 OBJCOPYFLAGS_$@ may be used to set additional options.
1133
1134 gzip
1135 Compress target. Use maximum compression to compress target.
1136
1137 Example:
25eb650a 1138 #arch/x86/boot/Makefile
1da177e4
LT
1139 LDFLAGS_bootsect := -Ttext 0x0 -s --oformat binary
1140 LDFLAGS_setup := -Ttext 0x0 -s --oformat binary -e begtext
1141
1142 targets += setup setup.o bootsect bootsect.o
1143 $(obj)/setup $(obj)/bootsect: %: %.o FORCE
1144 $(call if_changed,ld)
1145
a07f6033
JE
1146 In this example, there are two possible targets, requiring different
1147 options to the linker. The linker options are specified using the
1da177e4 1148 LDFLAGS_$@ syntax - one for each potential target.
5d3f083d 1149 $(targets) are assigned all potential targets, by which kbuild knows
1da177e4
LT
1150 the targets and will:
1151 1) check for commandline changes
1152 2) delete target during make clean
1153
1154 The ": %: %.o" part of the prerequisite is a shorthand that
39fed701 1155 frees us from listing the setup.o and bootsect.o files.
ef80f0a1 1156 Note: It is a common mistake to forget the "targets :=" assignment,
1da177e4
LT
1157 resulting in the target file being recompiled for no
1158 obvious reason.
1159
aab94339 1160 dtc
c1410562 1161 Create flattened device tree blob object suitable for linking
aab94339
DB
1162 into vmlinux. Device tree blobs linked into vmlinux are placed
1163 in an init section in the image. Platform code *must* copy the
1164 blob to non-init memory prior to calling unflatten_device_tree().
1165
90b335fb
SW
1166 To use this command, simply add *.dtb into obj-y or targets, or make
1167 some other target depend on %.dtb
aab94339 1168
90b335fb
SW
1169 A central rule exists to create $(obj)/%.dtb from $(src)/%.dts;
1170 architecture Makefiles do no need to explicitly write out that rule.
aab94339 1171
90b335fb
SW
1172 Example:
1173 targets += $(dtb-y)
1174 clean-files += *.dtb
1175 DTC_FLAGS ?= -p 1024
1da177e4 1176
052ad274 1177--- 6.8 Custom kbuild commands
1da177e4 1178
a07f6033 1179 When kbuild is executing with KBUILD_VERBOSE=0, then only a shorthand
1da177e4
LT
1180 of a command is normally displayed.
1181 To enable this behaviour for custom commands kbuild requires
1182 two variables to be set:
1183 quiet_cmd_<command> - what shall be echoed
1184 cmd_<command> - the command to execute
1185
1186 Example:
1187 #
1188 quiet_cmd_image = BUILD $@
1189 cmd_image = $(obj)/tools/build $(BUILDFLAGS) \
1190 $(obj)/vmlinux.bin > $@
1191
1192 targets += bzImage
1193 $(obj)/bzImage: $(obj)/vmlinux.bin $(obj)/tools/build FORCE
1194 $(call if_changed,image)
1195 @echo 'Kernel: $@ is ready'
1196
a07f6033 1197 When updating the $(obj)/bzImage target, the line
1da177e4 1198
25eb650a 1199 BUILD arch/x86/boot/bzImage
1da177e4
LT
1200
1201 will be displayed with "make KBUILD_VERBOSE=0".
39e6e9cf 1202
1da177e4 1203
052ad274 1204--- 6.9 Preprocessing linker scripts
1da177e4 1205
a07f6033 1206 When the vmlinux image is built, the linker script
1da177e4
LT
1207 arch/$(ARCH)/kernel/vmlinux.lds is used.
1208 The script is a preprocessed variant of the file vmlinux.lds.S
1209 located in the same directory.
a07f6033 1210 kbuild knows .lds files and includes a rule *lds.S -> *lds.
39e6e9cf 1211
1da177e4 1212 Example:
25eb650a 1213 #arch/x86/kernel/Makefile
1da177e4 1214 always := vmlinux.lds
39e6e9cf 1215
1da177e4
LT
1216 #Makefile
1217 export CPPFLAGS_vmlinux.lds += -P -C -U$(ARCH)
39e6e9cf
BH
1218
1219 The assignment to $(always) is used to tell kbuild to build the
a07f6033
JE
1220 target vmlinux.lds.
1221 The assignment to $(CPPFLAGS_vmlinux.lds) tells kbuild to use the
1da177e4 1222 specified options when building the target vmlinux.lds.
39e6e9cf 1223
a07f6033 1224 When building the *.lds target, kbuild uses the variables:
06c5040c 1225 KBUILD_CPPFLAGS : Set in top-level Makefile
f77bf014 1226 cppflags-y : May be set in the kbuild makefile
39fed701 1227 CPPFLAGS_$(@F) : Target-specific flags.
1da177e4
LT
1228 Note that the full filename is used in this
1229 assignment.
1230
39fed701 1231 The kbuild infrastructure for *lds files is used in several
5c811e59 1232 architecture-specific files.
1da177e4 1233
052ad274 1234--- 6.10 Generic header files
d8ecc5cd
SR
1235
1236 The directory include/asm-generic contains the header files
1237 that may be shared between individual architectures.
1238 The recommended approach how to use a generic header file is
1239 to list the file in the Kbuild file.
fcc8487d 1240 See "7.3 generic-y" for further info on syntax etc.
d8ecc5cd 1241
fbe6e37d
NP
1242--- 6.11 Post-link pass
1243
1244 If the file arch/xxx/Makefile.postlink exists, this makefile
1245 will be invoked for post-link objects (vmlinux and modules.ko)
1246 for architectures to run post-link passes on. Must also handle
1247 the clean target.
1248
1249 This pass runs after kallsyms generation. If the architecture
1250 needs to modify symbol locations, rather than manipulate the
1251 kallsyms, it may be easier to add another postlink target for
1252 .tmp_vmlinux? targets to be called from link-vmlinux.sh.
1253
1254 For example, powerpc uses this to check relocation sanity of
1255 the linked vmlinux file.
1256
c7bb349e
SR
1257=== 7 Kbuild syntax for exported headers
1258
39fed701 1259The kernel includes a set of headers that is exported to userspace.
c95940f2 1260Many headers can be exported as-is but other headers require a
c7bb349e
SR
1261minimal pre-processing before they are ready for user-space.
1262The pre-processing does:
39fed701 1263- drop kernel-specific annotations
c7bb349e 1264- drop include of compiler.h
c95940f2 1265- drop all sections that are kernel internal (guarded by ifdef __KERNEL__)
c7bb349e 1266
fcc8487d
ND
1267All headers under include/uapi/, include/generated/uapi/,
1268arch/<arch>/include/uapi/asm/ and arch/<arch>/include/generated/uapi/asm/
1269are exported.
c7bb349e 1270
fcc8487d
ND
1271A Kbuild file may be defined under arch/<arch>/include/uapi/asm/ and
1272arch/<arch>/include/asm/ to list asm files coming from asm-generic.
1273See subsequent chapter for the syntax of the Kbuild file.
c7bb349e 1274
fcc8487d 1275 --- 7.1 no-export-headers
c7bb349e 1276
fcc8487d
ND
1277 no-export-headers is essentially used by include/uapi/linux/Kbuild to
1278 avoid exporting specific headers (e.g. kvm.h) on architectures that do
1279 not support it. It should be avoided as much as possible.
c7bb349e 1280
40f1d4c2 1281 --- 7.2 genhdr-y
c7bb349e 1282
fcc8487d 1283 genhdr-y specifies asm files to be generated.
c7bb349e
SR
1284
1285 Example:
fcc8487d
ND
1286 #arch/x86/include/uapi/asm/Kbuild
1287 genhdr-y += unistd_32.h
1288 genhdr-y += unistd_64.h
1289 genhdr-y += unistd_x32.h
1290
c7bb349e 1291
bd73a328 1292 --- 7.3 generic-y
d8ecc5cd
SR
1293
1294 If an architecture uses a verbatim copy of a header from
1295 include/asm-generic then this is listed in the file
1296 arch/$(ARCH)/include/asm/Kbuild like this:
1297
1298 Example:
1299 #arch/x86/include/asm/Kbuild
1300 generic-y += termios.h
1301 generic-y += rtc.h
1302
1303 During the prepare phase of the build a wrapper include
1304 file is generated in the directory:
1305
1306 arch/$(ARCH)/include/generated/asm
1307
1308 When a header is exported where the architecture uses
1309 the generic header a similar wrapper is generated as part
1310 of the set of exported headers in the directory:
1311
1312 usr/include/asm
1313
1314 The generated wrapper will in both cases look like the following:
1315
1316 Example: termios.h
1317 #include <asm-generic/termios.h>
c7bb349e 1318
bd73a328 1319 --- 7.4 generated-y
54b880ca
JH
1320
1321 If an architecture generates other header files alongside generic-y
1322 wrappers, and not included in genhdr-y, then generated-y specifies
1323 them.
1324
1325 This prevents them being treated as stale asm-generic wrappers and
1326 removed.
1327
1328 Example:
1329 #arch/x86/include/asm/Kbuild
1330 generated-y += syscalls_32.h
1331
fcc8487d
ND
1332 --- 7.5 mandatory-y
1333
1334 mandatory-y is essentially used by include/uapi/asm-generic/Kbuild.asm
1335 to define the minimun set of headers that must be exported in
1336 include/asm.
1337
1338 The convention is to list one subdir per line and
1339 preferably in alphabetic order.
1340
1341 --- 7.6 subdir-y
1342
1343 subdir-y may be used to specify a subdirectory to be exported.
1344
1345 Example:
1346 #arch/cris/include/uapi/asm/Kbuild
1347 subdir-y += ../arch-v10/arch/
1348 subdir-y += ../arch-v32/arch/
1349
1350 The convention is to list one subdir per line and
1351 preferably in alphabetic order.
1352
c7bb349e 1353=== 8 Kbuild Variables
1da177e4
LT
1354
1355The top Makefile exports the following variables:
1356
1357 VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
1358
1359 These variables define the current kernel version. A few arch
1360 Makefiles actually use these values directly; they should use
1361 $(KERNELRELEASE) instead.
1362
1363 $(VERSION), $(PATCHLEVEL), and $(SUBLEVEL) define the basic
1364 three-part version number, such as "2", "4", and "0". These three
1365 values are always numeric.
1366
1367 $(EXTRAVERSION) defines an even tinier sublevel for pre-patches
1368 or additional patches. It is usually some non-numeric string
1369 such as "-pre4", and is often blank.
1370
1371 KERNELRELEASE
1372
1373 $(KERNELRELEASE) is a single string such as "2.4.0-pre4", suitable
1374 for constructing installation directory names or showing in
1375 version strings. Some arch Makefiles use it for this purpose.
1376
1377 ARCH
1378
1379 This variable defines the target architecture, such as "i386",
1380 "arm", or "sparc". Some kbuild Makefiles test $(ARCH) to
1381 determine which files to compile.
1382
1383 By default, the top Makefile sets $(ARCH) to be the same as the
1384 host system architecture. For a cross build, a user may
1385 override the value of $(ARCH) on the command line:
1386
1387 make ARCH=m68k ...
1388
1389
1390 INSTALL_PATH
1391
1392 This variable defines a place for the arch Makefiles to install
1393 the resident kernel image and System.map file.
5c811e59 1394 Use this for architecture-specific install targets.
1da177e4
LT
1395
1396 INSTALL_MOD_PATH, MODLIB
1397
1398 $(INSTALL_MOD_PATH) specifies a prefix to $(MODLIB) for module
1399 installation. This variable is not defined in the Makefile but
1400 may be passed in by the user if desired.
1401
1402 $(MODLIB) specifies the directory for module installation.
1403 The top Makefile defines $(MODLIB) to
1404 $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE). The user may
1405 override this value on the command line if desired.
1406
ac031f26
TT
1407 INSTALL_MOD_STRIP
1408
39fed701 1409 If this variable is specified, it will cause modules to be stripped
ac031f26 1410 after they are installed. If INSTALL_MOD_STRIP is '1', then the
39fed701 1411 default option --strip-debug will be used. Otherwise, the
177b241d
GE
1412 INSTALL_MOD_STRIP value will be used as the option(s) to the strip
1413 command.
ac031f26
TT
1414
1415
c7bb349e 1416=== 9 Makefile language
1da177e4 1417
a07f6033 1418The kernel Makefiles are designed to be run with GNU Make. The Makefiles
1da177e4
LT
1419use only the documented features of GNU Make, but they do use many
1420GNU extensions.
1421
1422GNU Make supports elementary list-processing functions. The kernel
1423Makefiles use a novel style of list building and manipulation with few
1424"if" statements.
1425
1426GNU Make has two assignment operators, ":=" and "=". ":=" performs
1427immediate evaluation of the right-hand side and stores an actual string
1428into the left-hand side. "=" is like a formula definition; it stores the
1429right-hand side in an unevaluated form and then evaluates this form each
1430time the left-hand side is used.
1431
1432There are some cases where "=" is appropriate. Usually, though, ":="
1433is the right choice.
1434
c7bb349e 1435=== 10 Credits
1da177e4
LT
1436
1437Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net>
1438Updates by Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
1439Updates by Sam Ravnborg <sam@ravnborg.org>
a07f6033 1440Language QA by Jan Engelhardt <jengelh@gmx.de>
1da177e4 1441
c7bb349e 1442=== 11 TODO
1da177e4 1443
a07f6033 1444- Describe how kbuild supports shipped files with _shipped.
1da177e4
LT
1445- Generating offset header files.
1446- Add more variables to section 7?
1447
39e6e9cf
BH
1448
1449