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 Makefiles 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 20 21 === 4 Host Program support 22 --- 4.1 Simple Host Program 23 --- 4.2 Composite Host Programs 24 --- 4.3 Defining shared libraries 25 --- 4.4 Using C++ for host programs 26 --- 4.5 Controlling compiler options for host programs 27 --- 4.6 When host programs are actually built 28 29 === 5 Kbuild clean infrastructure 30 31 === 6 Architecture Makefiles 32 --- 6.1 Set variables to tweak the build to the architecture 33 --- 6.2 Add prerequisites to prepare: 34 --- 6.3 List directories to visit when descending 35 --- 6.4 Architecture specific boot images 36 --- 6.5 Building non-kbuild targets 37 --- 6.6 Commands useful for building a boot image 38 --- 6.7 Custom kbuild commands 39 40 === 7 Kbuild Variables 41 === 8 Makefile language 42 === 9 Credits 43 === 10 TODO 44 45=== 1 Overview 46 47The Makefiles have five parts: 48 49 Makefile the top Makefile. 50 .config the kernel configuration file. 51 arch/$(ARCH)/Makefile the arch Makefile. 52 scripts/Makefile.* common rules etc. for all kbuild Makefiles. 53 kbuild Makefiles there are about 500 of these. 54 55The top Makefile reads the .config file, which comes from the kernel 56configuration process. 57 58The top Makefile is responsible for building two major products: vmlinux 59(the resident kernel image) and modules (any module files). 60It builds these goals by recursively descending into the subdirectories of 61the kernel source tree. 62The list of subdirectories which are visited depends upon the kernel 63configuration. The top Makefile textually includes an arch Makefile 64with the name arch/$(ARCH)/Makefile. The arch Makefile supplies 65architecture-specific information to the top Makefile. 66 67Each subdirectory has a kbuild Makefile which carries out the commands 68passed down from above. The kbuild Makefile uses information from the 69.config file to construct various file lists used by kbuild to build 70any built-in or modular targets. 71 72scripts/Makefile.* contains all the definitions/rules etc. that 73are used to build the kernel based on the kbuild makefiles. 74 75 76=== 2 Who does what 77 78People have four different relationships with the kernel Makefiles. 79 80*Users* are people who build kernels. These people type commands such as 81"make menuconfig" or "make". They usually do not read or edit 82any kernel Makefiles (or any other source files). 83 84*Normal developers* are people who work on features such as device 85drivers, file systems, and network protocols. These people need to 86maintain the kbuild Makefiles for the subsystem that they are 87working on. In order to do this effectively, they need some overall 88knowledge about the kernel Makefiles, plus detailed knowledge about the 89public interface for kbuild. 90 91*Arch developers* are people who work on an entire architecture, such 92as sparc or ia64. Arch developers need to know about the arch Makefile 93as well as kbuild Makefiles. 94 95*Kbuild developers* are people who work on the kernel build system itself. 96These people need to know about all aspects of the kernel Makefiles. 97 98This document is aimed towards normal developers and arch developers. 99 100 101=== 3 The kbuild Makefiles 102 103Most Makefiles within the kernel are kbuild Makefiles that use the 104kbuild infrastructure. This chapter introduce the syntax used in the 105kbuild makefiles. 106 107Section 3.1 "Goal definitions" is a quick intro, further chapters provide 108more details, with real examples. 109 110--- 3.1 Goal definitions 111 112 Goal definitions are the main part (heart) of the kbuild Makefile. 113 These lines define the files to be built, any special compilation 114 options, and any subdirectories to be entered recursively. 115 116 The most simple kbuild makefile contains one line: 117 118 Example: 119 obj-y += foo.o 120 121 This tell kbuild that there is one object in that directory named 122 foo.o. foo.o will be build from foo.c or foo.S. 123 124 If foo.o shall be built as a module, the variable obj-m is used. 125 Therefore the following pattern is often used: 126 127 Example: 128 obj-$(CONFIG_FOO) += foo.o 129 130 $(CONFIG_FOO) evaluates to either y (for built-in) or m (for module). 131 If CONFIG_FOO is neither y nor m, then the file will not be compiled 132 nor linked. 133 134--- 3.2 Built-in object goals - obj-y 135 136 The kbuild Makefile specifies object files for vmlinux 137 in the lists $(obj-y). These lists depend on the kernel 138 configuration. 139 140 Kbuild compiles all the $(obj-y) files. It then calls 141 "$(LD) -r" to merge these files into one built-in.o file. 142 built-in.o is later linked into vmlinux by the parent Makefile. 143 144 The order of files in $(obj-y) is significant. Duplicates in 145 the lists are allowed: the first instance will be linked into 146 built-in.o and succeeding instances will be ignored. 147 148 Link order is significant, because certain functions 149 (module_init() / __initcall) will be called during boot in the 150 order they appear. So keep in mind that changing the link 151 order may e.g. change the order in which your SCSI 152 controllers are detected, and thus you disks are renumbered. 153 154 Example: 155 #drivers/isdn/i4l/Makefile 156 # Makefile for the kernel ISDN subsystem and device drivers. 157 # Each configuration option enables a list of files. 158 obj-$(CONFIG_ISDN) += isdn.o 159 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o 160 161--- 3.3 Loadable module goals - obj-m 162 163 $(obj-m) specify object files which are built as loadable 164 kernel modules. 165 166 A module may be built from one source file or several source 167 files. In the case of one source file, the kbuild makefile 168 simply adds the file to $(obj-m). 169 170 Example: 171 #drivers/isdn/i4l/Makefile 172 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o 173 174 Note: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to 'm' 175 176 If a kernel module is built from several source files, you specify 177 that you want to build a module in the same way as above. 178 179 Kbuild needs to know which the parts that you want to build your 180 module from, so you have to tell it by setting an 181 $(<module_name>-objs) variable. 182 183 Example: 184 #drivers/isdn/i4l/Makefile 185 obj-$(CONFIG_ISDN) += isdn.o 186 isdn-objs := isdn_net_lib.o isdn_v110.o isdn_common.o 187 188 In this example, the module name will be isdn.o. Kbuild will 189 compile the objects listed in $(isdn-objs) and then run 190 "$(LD) -r" on the list of these files to generate isdn.o. 191 192 Kbuild recognises objects used for composite objects by the suffix 193 -objs, and the suffix -y. This allows the Makefiles to use 194 the value of a CONFIG_ symbol to determine if an object is part 195 of a composite object. 196 197 Example: 198 #fs/ext2/Makefile 199 obj-$(CONFIG_EXT2_FS) += ext2.o 200 ext2-y := balloc.o bitmap.o 201 ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o 202 203 In this example xattr.o is only part of the composite object 204 ext2.o, if $(CONFIG_EXT2_FS_XATTR) evaluates to 'y'. 205 206 Note: Of course, when you are building objects into the kernel, 207 the syntax above will also work. So, if you have CONFIG_EXT2_FS=y, 208 kbuild will build an ext2.o file for you out of the individual 209 parts and then link this into built-in.o, as you would expect. 210 211--- 3.4 Objects which export symbols 212 213 No special notation is required in the makefiles for 214 modules exporting symbols. 215 See also Documentation/modules.txt. 216 217--- 3.5 Library file goals - lib-y 218 219 Objects listed with obj-* is used for modules or 220 are combined in a built-in.o for that specific directory. 221 There is also the possibility to list objects that will 222 be included in a library, lib.a. 223 All objects listed with lib-y are combined in a single 224 library for that directory. 225 Objects that are listed in obj-y and additional listed in 226 lib-y will not be included in the library, since they will anyway 227 be accessible. 228 For consistency objects listed in lib-m will be included in lib.a. 229 230 Note that the same kbuild makefile may list files to be built-in 231 and to be part of a library. Therefore the same directory 232 may contain both a built-in.o and a lib.a file. 233 234 Example: 235 #arch/i386/lib/Makefile 236 lib-y := checksum.o delay.o 237 238 This will create a library lib.a based on checksum.o and delay.o. 239 For kbuild to actually recognize that there is a lib.a being build 240 the directory shall be listed in libs-y. 241 See also "6.3 List directories to visit when descending". 242 243 Usage of lib-y is normally restricted to lib/ and arch/*/lib. 244 245--- 3.6 Descending down in directories 246 247 A Makefile is only responsible for building objects in its own 248 directory. Files in subdirectories should be taken care of by 249 Makefiles in these subdirs. The build system will automatically 250 invoke make recursively in subdirectories, provided you let it know of 251 them. 252 253 To do so obj-y and obj-m are used. 254 ext2 lives in a separate directory, and the Makefile present in fs/ 255 tells kbuild to descend down using the following assignment. 256 257 Example: 258 #fs/Makefile 259 obj-$(CONfIG_EXT2_FS) += ext2/ 260 261 If CONFIG_EXT2_FS is set to either 'y' (built-in) or 'm' (modular) 262 the corresponding obj- variable will be set, and kbuild will descend 263 down in the ext2 directory. 264 Kbuild only uses this information to decide that it needs to visit 265 the directory, it is the Makefile in the subdirectory that 266 specifies what is modules and what is built-in. 267 268 It is good practice to use a CONFIG_ variable when assigning directory 269 names. This allows kbuild to totally skip the directory if the 270 corresponding CONFIG_ option is neither 'y' nor 'm'. 271 272--- 3.7 Compilation flags 273 274 EXTRA_CFLAGS, EXTRA_AFLAGS, EXTRA_LDFLAGS, EXTRA_ARFLAGS 275 276 All the EXTRA_ variables apply only to the kbuild makefile 277 where they are assigned. The EXTRA_ variables apply to all 278 commands executed in the kbuild makefile. 279 280 $(EXTRA_CFLAGS) specifies options for compiling C files with 281 $(CC). 282 283 Example: 284 # drivers/sound/emu10k1/Makefile 285 EXTRA_CFLAGS += -I$(obj) 286 ifdef DEBUG 287 EXTRA_CFLAGS += -DEMU10K1_DEBUG 288 endif 289 290 291 This variable is necessary because the top Makefile owns the 292 variable $(CFLAGS) and uses it for compilation flags for the 293 entire tree. 294 295 $(EXTRA_AFLAGS) is a similar string for per-directory options 296 when compiling assembly language source. 297 298 Example: 299 #arch/x86_64/kernel/Makefile 300 EXTRA_AFLAGS := -traditional 301 302 303 $(EXTRA_LDFLAGS) and $(EXTRA_ARFLAGS) are similar strings for 304 per-directory options to $(LD) and $(AR). 305 306 Example: 307 #arch/m68k/fpsp040/Makefile 308 EXTRA_LDFLAGS := -x 309 310 CFLAGS_$@, AFLAGS_$@ 311 312 CFLAGS_$@ and AFLAGS_$@ only apply to commands in current 313 kbuild makefile. 314 315 $(CFLAGS_$@) specifies per-file options for $(CC). The $@ 316 part has a literal value which specifies the file that it is for. 317 318 Example: 319 # drivers/scsi/Makefile 320 CFLAGS_aha152x.o = -DAHA152X_STAT -DAUTOCONF 321 CFLAGS_gdth.o = # -DDEBUG_GDTH=2 -D__SERIAL__ -D__COM2__ \ 322 -DGDTH_STATISTICS 323 CFLAGS_seagate.o = -DARBITRATE -DPARITY -DSEAGATE_USE_ASM 324 325 These three lines specify compilation flags for aha152x.o, 326 gdth.o, and seagate.o 327 328 $(AFLAGS_$@) is a similar feature for source files in assembly 329 languages. 330 331 Example: 332 # arch/arm/kernel/Makefile 333 AFLAGS_head-armv.o := -DTEXTADDR=$(TEXTADDR) -traditional 334 AFLAGS_head-armo.o := -DTEXTADDR=$(TEXTADDR) -traditional 335 336--- 3.9 Dependency tracking 337 338 Kbuild track dependencies on the following: 339 1) All prerequisite files (both *.c and *.h) 340 2) CONFIG_ options used in all prerequisite files 341 3) Command-line used to compile target 342 343 Thus, if you change an option to $(CC) all affected files will 344 be re-compiled. 345 346--- 3.10 Special Rules 347 348 Special rules are used when the kbuild infrastructure does 349 not provide the required support. A typical example is 350 header files generated during the build process. 351 Another example is the architecture specific Makefiles which 352 needs special rules to prepare boot images etc. 353 354 Special rules are written as normal Make rules. 355 Kbuild is not executing in the directory where the Makefile is 356 located, so all special rules shall provide a relative 357 path to prerequisite files and target files. 358 359 Two variables are used when defining special rules: 360 361 $(src) 362 $(src) is a relative path which points to the directory 363 where the Makefile is located. Always use $(src) when 364 referring to files located in the src tree. 365 366 $(obj) 367 $(obj) is a relative path which points to the directory 368 where the target is saved. Always use $(obj) when 369 referring to generated files. 370 371 Example: 372 #drivers/scsi/Makefile 373 $(obj)/53c8xx_d.h: $(src)/53c7,8xx.scr $(src)/script_asm.pl 374 $(CPP) -DCHIP=810 - < $< | ... $(src)/script_asm.pl 375 376 This is a special rule, following the normal syntax 377 required by make. 378 The target file depends on two prerequisite files. References 379 to the target file are prefixed with $(obj), references 380 to prerequisites are referenced with $(src) (because they are not 381 generated files). 382 383 384=== 4 Host Program support 385 386Kbuild supports building executables on the host for use during the 387compilation stage. 388Two steps are required in order to use a host executable. 389 390The first step is to tell kbuild that a host program exists. This is 391done utilising the variable host-prog. 392 393The second step is to add an explicit dependency to the executable. 394This can be done in two ways. Either add the dependency in a rule, 395or utilise the variable $(always). 396Both possibilities are described in the following. 397 398--- 4.1 Simple Host Program 399 400 In some cases there is a need to compile and run a program on the 401 computer where the build is running. 402 The following line tells kbuild that the program bin2hex shall be 403 built on the build host. 404 405 Example: 406 host-progs := bin2hex 407 408 Kbuild assumes in the above example that bin2hex is made from a single 409 c-source file named bin2hex.c located in the same directory as 410 the Makefile. 411 412--- 4.2 Composite Host Programs 413 414 Host programs can be made up based on composite objects. 415 The syntax used to define composite objetcs for host programs is 416 similar to the syntax used for kernel objects. 417 $(<executeable>-objs) list all objects used to link the final 418 executable. 419 420 Example: 421 #scripts/lxdialog/Makefile 422 host-progs := lxdialog 423 lxdialog-objs := checklist.o lxdialog.o 424 425 Objects with extension .o are compiled from the corresponding .c 426 files. In the above example checklist.c is compiled to checklist.o 427 and lxdialog.c is compiled to lxdialog.o. 428 Finally the two .o files are linked to the executable, lxdialog. 429 Note: The syntax <executable>-y is not permitted for host-programs. 430 431--- 4.3 Defining shared libraries 432 433 Objects with extension .so are considered shared libraries, and 434 will be compiled as position independent objects. 435 Kbuild provides support for shared libraries, but the usage 436 shall be restricted. 437 In the following example the libkconfig.so shared library is used 438 to link the executable conf. 439 440 Example: 441 #scripts/kconfig/Makefile 442 host-progs := conf 443 conf-objs := conf.o libkconfig.so 444 libkconfig-objs := expr.o type.o 445 446 Shared libraries always require a corresponding -objs line, and 447 in the example above the shared library libkconfig is composed by 448 the two objects expr.o and type.o. 449 expr.o and type.o will be built as position independent code and 450 linked as a shared library libkconfig.so. C++ is not supported for 451 shared libraries. 452 453--- 4.4 Using C++ for host programs 454 455 kbuild offers support for host programs written in C++. This was 456 introduced solely to support kconfig, and is not recommended 457 for general use. 458 459 Example: 460 #scripts/kconfig/Makefile 461 host-progs := qconf 462 qconf-cxxobjs := qconf.o 463 464 In the example above the executable is composed of the C++ file 465 qconf.cc - identified by $(qconf-cxxobjs). 466 467 If qconf is composed by a mixture of .c and .cc files, then an 468 additional line can be used to identify this. 469 470 Example: 471 #scripts/kconfig/Makefile 472 host-progs := qconf 473 qconf-cxxobjs := qconf.o 474 qconf-objs := check.o 475 476--- 4.5 Controlling compiler options for host programs 477 478 When compiling host programs, it is possible to set specific flags. 479 The programs will always be compiled utilising $(HOSTCC) passed 480 the options specified in $(HOSTCFLAGS). 481 To set flags that will take effect for all host programs created 482 in that Makefile use the variable HOST_EXTRACFLAGS. 483 484 Example: 485 #scripts/lxdialog/Makefile 486 HOST_EXTRACFLAGS += -I/usr/include/ncurses 487 488 To set specific flags for a single file the following construction 489 is used: 490 491 Example: 492 #arch/ppc64/boot/Makefile 493 HOSTCFLAGS_piggyback.o := -DKERNELBASE=$(KERNELBASE) 494 495 It is also possible to specify additional options to the linker. 496 497 Example: 498 #scripts/kconfig/Makefile 499 HOSTLOADLIBES_qconf := -L$(QTDIR)/lib 500 501 When linking qconf it will be passed the extra option "-L$(QTDIR)/lib". 502 503--- 4.6 When host programs are actually built 504 505 Kbuild will only build host-programs when they are referenced 506 as a prerequisite. 507 This is possible in two ways: 508 509 (1) List the prerequisite explicitly in a special rule. 510 511 Example: 512 #drivers/pci/Makefile 513 host-progs := gen-devlist 514 $(obj)/devlist.h: $(src)/pci.ids $(obj)/gen-devlist 515 ( cd $(obj); ./gen-devlist ) < $< 516 517 The target $(obj)/devlist.h will not be built before 518 $(obj)/gen-devlist is updated. Note that references to 519 the host programs in special rules must be prefixed with $(obj). 520 521 (2) Use $(always) 522 When there is no suitable special rule, and the host program 523 shall be built when a makefile is entered, the $(always) 524 variable shall be used. 525 526 Example: 527 #scripts/lxdialog/Makefile 528 host-progs := lxdialog 529 always := $(host-progs) 530 531 This will tell kbuild to build lxdialog even if not referenced in 532 any rule. 533 534=== 5 Kbuild clean infrastructure 535 536"make clean" deletes most generated files in the src tree where the kernel 537is compiled. This includes generated files such as host programs. 538Kbuild knows targets listed in $(host-progs), $(always), $(extra-y) and 539$(targets). They are all deleted during "make clean". 540Files matching the patterns "*.[oas]", "*.ko", plus some additional files 541generated by kbuild are deleted all over the kernel src tree when 542"make clean" is executed. 543 544Additional files can be specified in kbuild makefiles by use of $(clean-files). 545 546 Example: 547 #drivers/pci/Makefile 548 clean-files := devlist.h classlist.h 549 550When executing "make clean", the two files "devlist.h classlist.h" will 551be deleted. Kbuild knows that files specified by $(clean-files) are 552located in the same directory as the makefile. 553 554Usually kbuild descends down in subdirectories due to "obj-* := dir/", 555but in the architecture makefiles where the kbuild infrastructure 556is not sufficient this sometimes needs to be explicit. 557 558 Example: 559 #arch/i386/boot/Makefile 560 subdir- := compressed/ 561 562The above assignment instructs kbuild to descend down in the 563directory compressed/ when "make clean" is executed. 564 565To support the clean infrastructure in the Makefiles that builds the 566final bootimage there is an optional target named archclean: 567 568 Example: 569 #arch/i386/Makefile 570 archclean: 571 $(Q)$(MAKE) $(clean)=arch/i386/boot 572 573When "make clean" is executed, make will descend down in arch/i386/boot, 574and clean as usual. The Makefile located in arch/i386/boot/ may use 575the subdir- trick to descend further down. 576 577Note 1: arch/$(ARCH)/Makefile cannot use "subdir-", because that file is 578included in the top level makefile, and the kbuild infrastructure 579is not operational at that point. 580 581Note 2: All directories listed in core-y, libs-y, drivers-y and net-y will 582be visited during "make clean". 583 584=== 6 Architecture Makefiles 585 586The top level Makefile sets up the environment and does the preparation, 587before starting to descend down in the individual directories. 588The top level makefile contains the generic part, whereas the 589arch/$(ARCH)/Makefile contains what is required to set-up kbuild 590to the said architecture. 591To do so arch/$(ARCH)/Makefile sets a number of variables, and defines 592a few targets. 593 594When kbuild executes the following steps are followed (roughly): 5951) Configuration of the kernel => produced .config 5962) Store kernel version in include/linux/version.h 5973) Symlink include/asm to include/asm-$(ARCH) 5984) Updating all other prerequisites to the target prepare: 599 - Additional prerequisites are specified in arch/$(ARCH)/Makefile 6005) Recursively descend down in all directories listed in 601 init-* core* drivers-* net-* libs-* and build all targets. 602 - The value of the above variables are extended in arch/$(ARCH)/Makefile. 6036) All object files are then linked and the resulting file vmlinux is 604 located at the root of the src tree. 605 The very first objects linked are listed in head-y, assigned by 606 arch/$(ARCH)/Makefile. 6077) Finally the architecture specific part does any required post processing 608 and builds the final bootimage. 609 - This includes building boot records 610 - Preparing initrd images and the like 611 612 613--- 6.1 Set variables to tweak the build to the architecture 614 615 LDFLAGS Generic $(LD) options 616 617 Flags used for all invocations of the linker. 618 Often specifying the emulation is sufficient. 619 620 Example: 621 #arch/s390/Makefile 622 LDFLAGS := -m elf_s390 623 Note: EXTRA_LDFLAGS and LDFLAGS_$@ can be used to further customise 624 the flags used. See chapter 7. 625 626 LDFLAGS_MODULE Options for $(LD) when linking modules 627 628 LDFLAGS_MODULE is used to set specific flags for $(LD) when 629 linking the .ko files used for modules. 630 Default is "-r", for relocatable output. 631 632 LDFLAGS_vmlinux Options for $(LD) when linking vmlinux 633 634 LDFLAGS_vmlinux is used to specify additional flags to pass to 635 the linker when linking the final vmlinux. 636 LDFLAGS_vmlinux uses the LDFLAGS_$@ support. 637 638 Example: 639 #arch/i386/Makefile 640 LDFLAGS_vmlinux := -e stext 641 642 LDFLAGS_BLOB Options for $(LD) when linking the initramfs blob 643 644 The image used for initramfs is made during the build process. 645 LDFLAGS_BLOB is used to specify additional flags to be used when 646 creating the initramfs_data.o file. 647 Example: 648 #arch/i386/Makefile 649 LDFLAGS_BLOB := --format binary --oformat elf32-i386 650 651 OBJCOPYFLAGS objcopy flags 652 653 When $(call if_changed,objcopy) is used to translate a .o file, 654 then the flags specified in OBJCOPYFLAGS will be used. 655 $(call if_changed,objcopy) is often used to generate raw binaries on 656 vmlinux. 657 658 Example: 659 #arch/s390/Makefile 660 OBJCOPYFLAGS := -O binary 661 662 #arch/s390/boot/Makefile 663 $(obj)/image: vmlinux FORCE 664 $(call if_changed,objcopy) 665 666 In this example the binary $(obj)/image is a binary version of 667 vmlinux. The usage of $(call if_changed,xxx) will be described later. 668 669 AFLAGS $(AS) assembler flags 670 671 Default value - see top level Makefile 672 Append or modify as required per architecture. 673 674 Example: 675 #arch/sparc64/Makefile 676 AFLAGS += -m64 -mcpu=ultrasparc 677 678 CFLAGS $(CC) compiler flags 679 680 Default value - see top level Makefile 681 Append or modify as required per architecture. 682 683 Often the CFLAGS variable depends on the configuration. 684 685 Example: 686 #arch/i386/Makefile 687 cflags-$(CONFIG_M386) += -march=i386 688 CFLAGS += $(cflags-y) 689 690 Many arch Makefiles dynamically run the target C compiler to 691 probe supported options: 692 693 #arch/i386/Makefile 694 check_gcc = $(shell if $(CC) $(1) -S -o /dev/null -xc \ 695 /dev/null\ > /dev/null 2>&1; then echo "$(1)"; \ 696 else echo "$(2)"; fi) 697 cflags-$(CONFIG_MCYRIXIII) += $(call check_gcc,\ 698 -march=c3,-march=i486) 699 700 CFLAGS += $(cflags-y) 701 702 The above examples both utilise the trick that a config option expands 703 to 'y' when selected. 704 705 CFLAGS_KERNEL $(CC) options specific for built-in 706 707 $(CFLAGS_KERNEL) contains extra C compiler flags used to compile 708 resident kernel code. 709 710 CFLAGS_MODULE $(CC) options specific for modules 711 712 $(CFLAGS_MODULE) contains extra C compiler flags used to compile code 713 for loadable kernel modules. 714 715 716--- 6.2 Add prerequisites to prepare: 717 718 The prepare: rule is used to list prerequisites that needs to be 719 built before starting to descend down in the subdirectories. 720 This is usual header files containing assembler constants. 721 722 Example: 723 #arch/s390/Makefile 724 prepare: include/asm-$(ARCH)/offsets.h 725 726 In this example the file include/asm-$(ARCH)/offsets.h will 727 be built before descending down in the subdirectories. 728 See also chapter XXX-TODO that describe how kbuild supports 729 generating offset header files. 730 731 732--- 6.3 List directories to visit when descending 733 734 An arch Makefile cooperates with the top Makefile to define variables 735 which specify how to build the vmlinux file. Note that there is no 736 corresponding arch-specific section for modules; the module-building 737 machinery is all architecture-independent. 738 739 740 head-y, init-y, core-y, libs-y, drivers-y, net-y 741 742 $(head-y) list objects to be linked first in vmlinux. 743 $(libs-y) list directories where a lib.a archive can be located. 744 The rest list directories where a built-in.o object file can be located. 745 746 $(init-y) objects will be located after $(head-y). 747 Then the rest follows in this order: 748 $(core-y), $(libs-y), $(drivers-y) and $(net-y). 749 750 The top level Makefile define values for all generic directories, 751 and arch/$(ARCH)/Makefile only adds architecture specific directories. 752 753 Example: 754 #arch/sparc64/Makefile 755 core-y += arch/sparc64/kernel/ 756 libs-y += arch/sparc64/prom/ arch/sparc64/lib/ 757 drivers-$(CONFIG_OPROFILE) += arch/sparc64/oprofile/ 758 759 760--- 6.4 Architecture specific boot images 761 762 An arch Makefile specifies goals that take the vmlinux file, compress 763 it, wrap it in bootstrapping code, and copy the resulting files 764 somewhere. This includes various kinds of installation commands. 765 The actual goals are not standardized across architectures. 766 767 It is common to locate any additional processing in a boot/ 768 directory below arch/$(ARCH)/. 769 770 Kbuild does not provide any smart way to support building a 771 target specified in boot/. Therefore arch/$(ARCH)/Makefile shall 772 call make manually to build a target in boot/. 773 774 The recommended approach is to include shortcuts in 775 arch/$(ARCH)/Makefile, and use the full path when calling down 776 into the arch/$(ARCH)/boot/Makefile. 777 778 Example: 779 #arch/i386/Makefile 780 boot := arch/i386/boot 781 bzImage: vmlinux 782 $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@ 783 784 "$(Q)$(MAKE) $(build)=<dir>" is the recommended way to invoke 785 make in a subdirectory. 786 787 There are no rules for naming of the architecture specific targets, 788 but executing "make help" will list all relevant targets. 789 To support this $(archhelp) must be defined. 790 791 Example: 792 #arch/i386/Makefile 793 define archhelp 794 echo '* bzImage - Image (arch/$(ARCH)/boot/bzImage)' 795 endef 796 797 When make is executed without arguments, the first goal encountered 798 will be built. In the top level Makefile the first goal present 799 is all:. 800 An architecture shall always per default build a bootable image. 801 In "make help" the default goal is highlighted with a '*'. 802 Add a new prerequisite to all: to select a default goal different 803 from vmlinux. 804 805 Example: 806 #arch/i386/Makefile 807 all: bzImage 808 809 When "make" is executed without arguments, bzImage will be built. 810 811--- 6.5 Building non-kbuild targets 812 813 extra-y 814 815 extra-y specify additional targets created in current 816 directory, in addition to any targets specified by obj-*. 817 818 Listing all targets in extra-y is required for two purposes: 819 1) Enable kbuild to check changes in command lines 820 - When $(call if_changed,xxx) is used 821 2) kbuild knows what files to delete during "make clean" 822 823 Example: 824 #arch/i386/kernel/Makefile 825 extra-y := head.o init_task.o 826 827 In this example extra-y is used to list object files that 828 shall be built, but shall not be linked as part of built-in.o. 829 830 831--- 6.6 Commands useful for building a boot image 832 833 Kbuild provide a few macros that are useful when building a 834 boot image. 835 836 if_changed 837 838 if_changed is the infrastructure used for the following commands. 839 840 Usage: 841 target: source(s) FORCE 842 $(call if_changed,ld/objcopy/gzip) 843 844 When the rule is evaluated it is checked to see if any files 845 needs an update, or the commandline has changed since last 846 invocation. The latter will force a rebuild if any options 847 to the executable have changed. 848 Any target that utilises if_changed must be listed in $(targets), 849 otherwise the command line check will fail, and the target will 850 always be built. 851 Assignments to $(targets) are without $(obj)/ prefix. 852 if_changed may be used in conjunction with custom commands as 853 defined in 6.7 "Custom kbuild commands". 854 Note: It is a typical mistake to forget the FORCE prerequisite. 855 856 ld 857 Link target. Often LDFLAGS_$@ is used to set specific options to ld. 858 859 objcopy 860 Copy binary. Uses OBJCOPYFLAGS usually specified in 861 arch/$(ARCH)/Makefile. 862 OBJCOPYFLAGS_$@ may be used to set additional options. 863 864 gzip 865 Compress target. Use maximum compression to compress target. 866 867 Example: 868 #arch/i386/boot/Makefile 869 LDFLAGS_bootsect := -Ttext 0x0 -s --oformat binary 870 LDFLAGS_setup := -Ttext 0x0 -s --oformat binary -e begtext 871 872 targets += setup setup.o bootsect bootsect.o 873 $(obj)/setup $(obj)/bootsect: %: %.o FORCE 874 $(call if_changed,ld) 875 876 In this example there is two possible targets, requiring different 877 options to the linker. the linker options are specified using the 878 LDFLAGS_$@ syntax - one for each potential target. 879 $(targets) are assinged all potential targets, herby kbuild knows 880 the targets and will: 881 1) check for commandline changes 882 2) delete target during make clean 883 884 The ": %: %.o" part of the prerequisite is a shorthand that 885 free us from listing the setup.o and bootsect.o files. 886 Note: It is a common mistake to forget the "target :=" assignment, 887 resulting in the target file being recompiled for no 888 obvious reason. 889 890 891--- 6.7 Custom kbuild commands 892 893 When kbuild is executing with KBUILD_VERBOSE=0 then only a shorthand 894 of a command is normally displayed. 895 To enable this behaviour for custom commands kbuild requires 896 two variables to be set: 897 quiet_cmd_<command> - what shall be echoed 898 cmd_<command> - the command to execute 899 900 Example: 901 # 902 quiet_cmd_image = BUILD $@ 903 cmd_image = $(obj)/tools/build $(BUILDFLAGS) \ 904 $(obj)/vmlinux.bin > $@ 905 906 targets += bzImage 907 $(obj)/bzImage: $(obj)/vmlinux.bin $(obj)/tools/build FORCE 908 $(call if_changed,image) 909 @echo 'Kernel: $@ is ready' 910 911 When updating the $(obj)/bzImage target the line: 912 913 BUILD arch/i386/boot/bzImage 914 915 will be displayed with "make KBUILD_VERBOSE=0". 916 917 918=== 7 Kbuild Variables 919 920The top Makefile exports the following variables: 921 922 VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION 923 924 These variables define the current kernel version. A few arch 925 Makefiles actually use these values directly; they should use 926 $(KERNELRELEASE) instead. 927 928 $(VERSION), $(PATCHLEVEL), and $(SUBLEVEL) define the basic 929 three-part version number, such as "2", "4", and "0". These three 930 values are always numeric. 931 932 $(EXTRAVERSION) defines an even tinier sublevel for pre-patches 933 or additional patches. It is usually some non-numeric string 934 such as "-pre4", and is often blank. 935 936 KERNELRELEASE 937 938 $(KERNELRELEASE) is a single string such as "2.4.0-pre4", suitable 939 for constructing installation directory names or showing in 940 version strings. Some arch Makefiles use it for this purpose. 941 942 ARCH 943 944 This variable defines the target architecture, such as "i386", 945 "arm", or "sparc". Some kbuild Makefiles test $(ARCH) to 946 determine which files to compile. 947 948 By default, the top Makefile sets $(ARCH) to be the same as the 949 host system architecture. For a cross build, a user may 950 override the value of $(ARCH) on the command line: 951 952 make ARCH=m68k ... 953 954 955 INSTALL_PATH 956 957 This variable defines a place for the arch Makefiles to install 958 the resident kernel image and System.map file. 959 Use this for architecture specific install targets. 960 961 INSTALL_MOD_PATH, MODLIB 962 963 $(INSTALL_MOD_PATH) specifies a prefix to $(MODLIB) for module 964 installation. This variable is not defined in the Makefile but 965 may be passed in by the user if desired. 966 967 $(MODLIB) specifies the directory for module installation. 968 The top Makefile defines $(MODLIB) to 969 $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE). The user may 970 override this value on the command line if desired. 971 972=== 8 Makefile language 973 974The kernel Makefiles are designed to run with GNU Make. The Makefiles 975use only the documented features of GNU Make, but they do use many 976GNU extensions. 977 978GNU Make supports elementary list-processing functions. The kernel 979Makefiles use a novel style of list building and manipulation with few 980"if" statements. 981 982GNU Make has two assignment operators, ":=" and "=". ":=" performs 983immediate evaluation of the right-hand side and stores an actual string 984into the left-hand side. "=" is like a formula definition; it stores the 985right-hand side in an unevaluated form and then evaluates this form each 986time the left-hand side is used. 987 988There are some cases where "=" is appropriate. Usually, though, ":=" 989is the right choice. 990 991=== 9 Credits 992 993Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net> 994Updates by Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de> 995Updates by Sam Ravnborg <sam@ravnborg.org> 996 997=== 10 TODO 998 999- Describe how kbuild support shipped files with _shipped. 1000- Generating offset header files.
1001- Add more variables to section 7? 1002 1003

