1====================== 2Linux Kernel Makefiles 3====================== 4 5This document describes the Linux kernel Makefiles. 6 7.. Table of Contents 8 9 === 1 Overview 10 === 2 Who does what 11 === 3 The kbuild files 12 --- 3.1 Goal definitions 13 --- 3.2 Built-in object goals - obj-y 14 --- 3.3 Loadable module goals - obj-m 15 --- 3.4 Objects which export symbols 16 --- 3.5 Library file goals - lib-y 17 --- 3.6 Descending down in directories 18 --- 3.7 Non-builtin vmlinux targets - extra-y 19 --- 3.8 Always built goals - always-y 20 --- 3.9 Compilation flags 21 --- 3.10 Dependency tracking 22 --- 3.11 Custom Rules 23 --- 3.12 Command change detection 24 --- 3.13 $(CC) support functions 25 --- 3.14 $(LD) support functions 26 --- 3.15 Script Invocation 27 28 === 4 Host Program support 29 --- 4.1 Simple Host Program 30 --- 4.2 Composite Host Programs 31 --- 4.3 Using C++ for host programs 32 --- 4.4 Controlling compiler options for host programs 33 --- 4.5 When host programs are actually built 34 35 === 5 Userspace Program support 36 --- 5.1 Simple Userspace Program 37 --- 5.2 Composite Userspace Programs 38 --- 5.3 Controlling compiler options for userspace programs 39 --- 5.4 When userspace programs are actually built 40 41 === 6 Kbuild clean infrastructure 42 43 === 7 Architecture Makefiles 44 --- 7.1 Set variables to tweak the build to the architecture 45 --- 7.2 Add prerequisites to archheaders 46 --- 7.3 Add prerequisites to archprepare 47 --- 7.4 List directories to visit when descending 48 --- 7.5 Architecture-specific boot images 49 --- 7.6 Building non-kbuild targets 50 --- 7.7 Commands useful for building a boot image 51 --- 7.8 <deleted> 52 --- 7.9 Preprocessing linker scripts 53 --- 7.10 Generic header files 54 --- 7.11 Post-link pass 55 56 === 8 Kbuild syntax for exported headers 57 --- 8.1 no-export-headers 58 --- 8.2 generic-y 59 --- 8.3 generated-y 60 --- 8.4 mandatory-y 61 62 === 9 Kbuild Variables 63 === 10 Makefile language 64 === 11 Credits 65 === 12 TODO 66 671 Overview 68========== 69 70The Makefiles have five parts:: 71 72 Makefile the top Makefile. 73 .config the kernel configuration file. 74 arch/$(SRCARCH)/Makefile the arch Makefile. 75 scripts/Makefile.* common rules etc. for all kbuild Makefiles. 76 kbuild Makefiles exist in every subdirectory 77 78The top Makefile reads the .config file, which comes from the kernel 79configuration process. 80 81The top Makefile is responsible for building two major products: vmlinux 82(the resident kernel image) and modules (any module files). 83It builds these goals by recursively descending into the subdirectories of 84the kernel source tree. 85The list of subdirectories which are visited depends upon the kernel 86configuration. The top Makefile textually includes an arch Makefile 87with the name arch/$(SRCARCH)/Makefile. The arch Makefile supplies 88architecture-specific information to the top Makefile. 89 90Each subdirectory has a kbuild Makefile which carries out the commands 91passed down from above. The kbuild Makefile uses information from the 92.config file to construct various file lists used by kbuild to build 93any built-in or modular targets. 94 95scripts/Makefile.* contains all the definitions/rules etc. that 96are used to build the kernel based on the kbuild makefiles. 97 98 992 Who does what 100=============== 101 102People have four different relationships with the kernel Makefiles. 103 104*Users* are people who build kernels. These people type commands such as 105"make menuconfig" or "make". They usually do not read or edit 106any kernel Makefiles (or any other source files). 107 108*Normal developers* are people who work on features such as device 109drivers, file systems, and network protocols. These people need to 110maintain the kbuild Makefiles for the subsystem they are 111working on. In order to do this effectively, they need some overall 112knowledge about the kernel Makefiles, plus detailed knowledge about the 113public interface for kbuild. 114 115*Arch developers* are people who work on an entire architecture, such 116as sparc or ia64. Arch developers need to know about the arch Makefile 117as well as kbuild Makefiles. 118 119*Kbuild developers* are people who work on the kernel build system itself. 120These people need to know about all aspects of the kernel Makefiles. 121 122This document is aimed towards normal developers and arch developers. 123 124 1253 The kbuild files 126================== 127 128Most Makefiles within the kernel are kbuild Makefiles that use the 129kbuild infrastructure. This chapter introduces the syntax used in the 130kbuild makefiles. 131The preferred name for the kbuild files are 'Makefile' but 'Kbuild' can 132be used and if both a 'Makefile' and a 'Kbuild' file exists, then the 'Kbuild' 133file will be used. 134 135Section 3.1 "Goal definitions" is a quick intro; further chapters provide 136more details, with real examples. 137 1383.1 Goal definitions 139-------------------- 140 141 Goal definitions are the main part (heart) of the kbuild Makefile. 142 These lines define the files to be built, any special compilation 143 options, and any subdirectories to be entered recursively. 144 145 The most simple kbuild makefile contains one line: 146 147 Example:: 148 149 obj-y += foo.o 150 151 This tells kbuild that there is one object in that directory, named 152 foo.o. foo.o will be built from foo.c or foo.S. 153 154 If foo.o shall be built as a module, the variable obj-m is used. 155 Therefore the following pattern is often used: 156 157 Example:: 158 159 obj-$(CONFIG_FOO) += foo.o 160 161 $(CONFIG_FOO) evaluates to either y (for built-in) or m (for module). 162 If CONFIG_FOO is neither y nor m, then the file will not be compiled 163 nor linked. 164 1653.2 Built-in object goals - obj-y 166--------------------------------- 167 168 The kbuild Makefile specifies object files for vmlinux 169 in the $(obj-y) lists. These lists depend on the kernel 170 configuration. 171 172 Kbuild compiles all the $(obj-y) files. It then calls 173 "$(AR) rcSTP" to merge these files into one built-in.a file. 174 This is a thin archive without a symbol table. It will be later 175 linked into vmlinux by scripts/link-vmlinux.sh 176 177 The order of files in $(obj-y) is significant. Duplicates in 178 the lists are allowed: the first instance will be linked into 179 built-in.a and succeeding instances will be ignored. 180 181 Link order is significant, because certain functions 182 (module_init() / __initcall) will be called during boot in the 183 order they appear. So keep in mind that changing the link 184 order may e.g. change the order in which your SCSI 185 controllers are detected, and thus your disks are renumbered. 186 187 Example:: 188 189 #drivers/isdn/i4l/Makefile 190 # Makefile for the kernel ISDN subsystem and device drivers. 191 # Each configuration option enables a list of files. 192 obj-$(CONFIG_ISDN_I4L) += isdn.o 193 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o 194 1953.3 Loadable module goals - obj-m 196--------------------------------- 197 198 $(obj-m) specifies object files which are built as loadable 199 kernel modules. 200 201 A module may be built from one source file or several source 202 files. In the case of one source file, the kbuild makefile 203 simply adds the file to $(obj-m). 204 205 Example:: 206 207 #drivers/isdn/i4l/Makefile 208 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o 209 210 Note: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to 'm' 211 212 If a kernel module is built from several source files, you specify 213 that you want to build a module in the same way as above; however, 214 kbuild needs to know which object files you want to build your 215 module from, so you have to tell it by setting a $(<module_name>-y) 216 variable. 217 218 Example:: 219 220 #drivers/isdn/i4l/Makefile 221 obj-$(CONFIG_ISDN_I4L) += isdn.o 222 isdn-y := isdn_net_lib.o isdn_v110.o isdn_common.o 223 224 In this example, the module name will be isdn.o. Kbuild will 225 compile the objects listed in $(isdn-y) and then run 226 "$(LD) -r" on the list of these files to generate isdn.o. 227 228 Due to kbuild recognizing $(<module_name>-y) for composite objects, 229 you can use the value of a `CONFIG_` symbol to optionally include an 230 object file as part of a composite object. 231 232 Example:: 233 234 #fs/ext2/Makefile 235 obj-$(CONFIG_EXT2_FS) += ext2.o 236 ext2-y := balloc.o dir.o file.o ialloc.o inode.o ioctl.o \ 237 namei.o super.o symlink.o 238 ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o \ 239 xattr_trusted.o 240 241 In this example, xattr.o, xattr_user.o and xattr_trusted.o are only 242 part of the composite object ext2.o if $(CONFIG_EXT2_FS_XATTR) 243 evaluates to 'y'. 244 245 Note: Of course, when you are building objects into the kernel, 246 the syntax above will also work. So, if you have CONFIG_EXT2_FS=y, 247 kbuild will build an ext2.o file for you out of the individual 248 parts and then link this into built-in.a, as you would expect. 249 2503.4 Objects which export symbols 251-------------------------------- 252 253 No special notation is required in the makefiles for 254 modules exporting symbols. 255 2563.5 Library file goals - lib-y 257------------------------------ 258 259 Objects listed with obj-* are used for modules, or 260 combined in a built-in.a for that specific directory. 261 There is also the possibility to list objects that will 262 be included in a library, lib.a. 263 All objects listed with lib-y are combined in a single 264 library for that directory. 265 Objects that are listed in obj-y and additionally listed in 266 lib-y will not be included in the library, since they will 267 be accessible anyway. 268 For consistency, objects listed in lib-m will be included in lib.a. 269 270 Note that the same kbuild makefile may list files to be built-in 271 and to be part of a library. Therefore the same directory 272 may contain both a built-in.a and a lib.a file. 273 274 Example:: 275 276 #arch/x86/lib/Makefile 277 lib-y := delay.o 278 279 This will create a library lib.a based on delay.o. For kbuild to 280 actually recognize that there is a lib.a being built, the directory 281 shall be listed in libs-y. 282 283 See also "7.4 List directories to visit when descending". 284 285 Use of lib-y is normally restricted to `lib/` and `arch/*/lib`. 286 2873.6 Descending down in directories 288---------------------------------- 289 290 A Makefile is only responsible for building objects in its own 291 directory. Files in subdirectories should be taken care of by 292 Makefiles in these subdirs. The build system will automatically 293 invoke make recursively in subdirectories, provided you let it know of 294 them. 295 296 To do so, obj-y and obj-m are used. 297 ext2 lives in a separate directory, and the Makefile present in fs/ 298 tells kbuild to descend down using the following assignment. 299 300 Example:: 301 302 #fs/Makefile 303 obj-$(CONFIG_EXT2_FS) += ext2/ 304 305 If CONFIG_EXT2_FS is set to either 'y' (built-in) or 'm' (modular) 306 the corresponding obj- variable will be set, and kbuild will descend 307 down in the ext2 directory. 308 309 Kbuild uses this information not only to decide that it needs to visit 310 the directory, but also to decide whether or not to link objects from 311 the directory into vmlinux. 312 313 When Kbuild descends into the directory with 'y', all built-in objects 314 from that directory are combined into the built-in.a, which will be 315 eventually linked into vmlinux. 316 317 When Kbuild descends into the directory with 'm', in contrast, nothing 318 from that directory will be linked into vmlinux. If the Makefile in 319 that directory specifies obj-y, those objects will be left orphan. 320 It is very likely a bug of the Makefile or of dependencies in Kconfig. 321 322 Kbuild also supports dedicated syntax, subdir-y and subdir-m, for 323 descending into subdirectories. It is a good fit when you know they 324 do not contain kernel-space objects at all. A typical usage is to let 325 Kbuild descend into subdirectories to build tools. 326 327 Examples:: 328 329 # scripts/Makefile 330 subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins 331 subdir-$(CONFIG_MODVERSIONS) += genksyms 332 subdir-$(CONFIG_SECURITY_SELINUX) += selinux 333 334 Unlike obj-y/m, subdir-y/m does not need the trailing slash since this 335 syntax is always used for directories. 336 337 It is good practice to use a `CONFIG_` variable when assigning directory 338 names. This allows kbuild to totally skip the directory if the 339 corresponding `CONFIG_` option is neither 'y' nor 'm'. 340 3413.7 Non-builtin vmlinux targets - extra-y 342----------------------------------------- 343 344 extra-y specifies targets which are needed for building vmlinux, 345 but not combined into built-in.a. 346 347 Examples are: 348 349 1) head objects 350 351 Some objects must be placed at the head of vmlinux. They are 352 directly linked to vmlinux without going through built-in.a 353 A typical use-case is an object that contains the entry point. 354 355 arch/$(SRCARCH)/Makefile should specify such objects as head-y. 356 357 Discussion: 358 Given that we can control the section order in the linker script, 359 why do we need head-y? 360 361 2) vmlinux linker script 362 363 The linker script for vmlinux is located at 364 arch/$(SRCARCH)/kernel/vmlinux.lds 365 366 Example:: 367 368 # arch/x86/kernel/Makefile 369 extra-y := head_$(BITS).o 370 extra-y += head$(BITS).o 371 extra-y += ebda.o 372 extra-y += platform-quirks.o 373 extra-y += vmlinux.lds 374 375 $(extra-y) should only contain targets needed for vmlinux. 376 377 Kbuild skips extra-y when vmlinux is apparently not a final goal. 378 (e.g. 'make modules', or building external modules) 379 380 If you intend to build targets unconditionally, always-y (explained 381 in the next section) is the correct syntax to use. 382 3833.8 Always built goals - always-y 384--------------------------------- 385 386 always-y specifies targets which are literally always built when 387 Kbuild visits the Makefile. 388 389 Example:: 390 # ./Kbuild 391 offsets-file := include/generated/asm-offsets.h 392 always-y += $(offsets-file) 393 3943.9 Compilation flags 395--------------------- 396 397 ccflags-y, asflags-y and ldflags-y 398 These three flags apply only to the kbuild makefile in which they 399 are assigned. They are used for all the normal cc, as and ld 400 invocations happening during a recursive build. 401 Note: Flags with the same behaviour were previously named: 402 EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS. 403 They are still supported but their usage is deprecated. 404 405 ccflags-y specifies options for compiling with $(CC). 406 407 Example:: 408 409 # drivers/acpi/acpica/Makefile 410 ccflags-y := -Os -D_LINUX -DBUILDING_ACPICA 411 ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT 412 413 This variable is necessary because the top Makefile owns the 414 variable $(KBUILD_CFLAGS) and uses it for compilation flags for the 415 entire tree. 416 417 asflags-y specifies assembler options. 418 419 Example:: 420 421 #arch/sparc/kernel/Makefile 422 asflags-y := -ansi 423 424 ldflags-y specifies options for linking with $(LD). 425 426 Example:: 427 428 #arch/cris/boot/compressed/Makefile 429 ldflags-y += -T $(srctree)/$(src)/decompress_$(arch-y).lds 430 431 subdir-ccflags-y, subdir-asflags-y 432 The two flags listed above are similar to ccflags-y and asflags-y. 433 The difference is that the subdir- variants have effect for the kbuild 434 file where they are present and all subdirectories. 435 Options specified using subdir-* are added to the commandline before 436 the options specified using the non-subdir variants. 437 438 Example:: 439 440 subdir-ccflags-y := -Werror 441 442 ccflags-remove-y, asflags-remove-y 443 These flags are used to remove particular flags for the compiler, 444 assembler invocations. 445 446 Example:: 447 448 ccflags-remove-$(CONFIG_MCOUNT) += -pg 449 450 CFLAGS_$@, AFLAGS_$@ 451 CFLAGS_$@ and AFLAGS_$@ only apply to commands in current 452 kbuild makefile. 453 454 $(CFLAGS_$@) specifies per-file options for $(CC). The $@ 455 part has a literal value which specifies the file that it is for. 456 457 CFLAGS_$@ has the higher priority than ccflags-remove-y; CFLAGS_$@ 458 can re-add compiler flags that were removed by ccflags-remove-y. 459 460 Example:: 461 462 # drivers/scsi/Makefile 463 CFLAGS_aha152x.o = -DAHA152X_STAT -DAUTOCONF 464 465 This line specify compilation flags for aha152x.o. 466 467 $(AFLAGS_$@) is a similar feature for source files in assembly 468 languages. 469 470 AFLAGS_$@ has the higher priority than asflags-remove-y; AFLAGS_$@ 471 can re-add assembler flags that were removed by asflags-remove-y. 472 473 Example:: 474 475 # arch/arm/kernel/Makefile 476 AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 477 AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312 478 AFLAGS_iwmmxt.o := -Wa,-mcpu=iwmmxt 479 480 4813.10 Dependency tracking 482------------------------ 483 484 Kbuild tracks dependencies on the following: 485 486 1) All prerequisite files (both `*.c` and `*.h`) 487 2) `CONFIG_` options used in all prerequisite files 488 3) Command-line used to compile target 489 490 Thus, if you change an option to $(CC) all affected files will 491 be re-compiled. 492 4933.11 Custom Rules 494----------------- 495 496 Custom rules are used when the kbuild infrastructure does 497 not provide the required support. A typical example is 498 header files generated during the build process. 499 Another example are the architecture-specific Makefiles which 500 need custom rules to prepare boot images etc. 501 502 Custom rules are written as normal Make rules. 503 Kbuild is not executing in the directory where the Makefile is 504 located, so all custom rules shall use a relative 505 path to prerequisite files and target files. 506 507 Two variables are used when defining custom rules: 508 509 $(src) 510 $(src) is a relative path which points to the directory 511 where the Makefile is located. Always use $(src) when 512 referring to files located in the src tree. 513 514 $(obj) 515 $(obj) is a relative path which points to the directory 516 where the target is saved. Always use $(obj) when 517 referring to generated files. 518 519 Example:: 520 521 #drivers/scsi/Makefile 522 $(obj)/53c8xx_d.h: $(src)/53c7,8xx.scr $(src)/script_asm.pl 523 $(CPP) -DCHIP=810 - < $< | ... $(src)/script_asm.pl 524 525 This is a custom rule, following the normal syntax 526 required by make. 527 528 The target file depends on two prerequisite files. References 529 to the target file are prefixed with $(obj), references 530 to prerequisites are referenced with $(src) (because they are not 531 generated files). 532 533 $(kecho) 534 echoing information to user in a rule is often a good practice 535 but when execution "make -s" one does not expect to see any output 536 except for warnings/errors. 537 To support this kbuild defines $(kecho) which will echo out the 538 text following $(kecho) to stdout except if "make -s" is used. 539 540 Example:: 541 542 # arch/arm/Makefile 543 $(BOOT_TARGETS): vmlinux 544 $(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@ 545 @$(kecho) ' Kernel: $(boot)/$@ is ready' 546 547 When kbuild is executing with KBUILD_VERBOSE=0, then only a shorthand 548 of a command is normally displayed. 549 To enable this behaviour for custom commands kbuild requires 550 two variables to be set:: 551 552 quiet_cmd_<command> - what shall be echoed 553 cmd_<command> - the command to execute 554 555 Example:: 556 557 # lib/Makefile 558 quiet_cmd_crc32 = GEN $@ 559 cmd_crc32 = $< > $@ 560 561 $(obj)/crc32table.h: $(obj)/gen_crc32table 562 $(call cmd,crc32) 563 564 When updating the $(obj)/crc32table.h target, the line: 565 566 GEN lib/crc32table.h 567 568 will be displayed with "make KBUILD_VERBOSE=0". 569 5703.12 Command change detection 571----------------------------- 572 573 When the rule is evaluated, timestamps are compared between the target 574 and its prerequisite files. GNU Make updates the target when any of the 575 prerequisites is newer than that. 576 577 The target should be rebuilt also when the command line has changed 578 since the last invocation. This is not supported by Make itself, so 579 Kbuild achieves this by a kind of meta-programming. 580 581 if_changed is the macro used for this purpose, in the following form:: 582 583 quiet_cmd_<command> = ... 584 cmd_<command> = ... 585 586 <target>: <source(s)> FORCE 587 $(call if_changed,<command>) 588 589 Any target that utilizes if_changed must be listed in $(targets), 590 otherwise the command line check will fail, and the target will 591 always be built. 592 593 If the target is already listed in the recognized syntax such as 594 obj-y/m, lib-y/m, extra-y/m, always-y/m, hostprogs, userprogs, Kbuild 595 automatically adds it to $(targets). Otherwise, the target must be 596 explicitly added to $(targets). 597 598 Assignments to $(targets) are without $(obj)/ prefix. if_changed may be 599 used in conjunction with custom rules as defined in "3.11 Custom Rules". 600 601 Note: It is a typical mistake to forget the FORCE prerequisite. 602 Another common pitfall is that whitespace is sometimes significant; for 603 instance, the below will fail (note the extra space after the comma):: 604 605 target: source(s) FORCE 606 607 **WRONG!** $(call if_changed, objcopy) 608 609 Note: 610 if_changed should not be used more than once per target. 611 It stores the executed command in a corresponding .cmd 612 file and multiple calls would result in overwrites and 613 unwanted results when the target is up to date and only the 614 tests on changed commands trigger execution of commands. 615 6163.13 $(CC) support functions 617---------------------------- 618 619 The kernel may be built with several different versions of 620 $(CC), each supporting a unique set of features and options. 621 kbuild provides basic support to check for valid options for $(CC). 622 $(CC) is usually the gcc compiler, but other alternatives are 623 available. 624 625 as-option 626 as-option is used to check if $(CC) -- when used to compile 627 assembler (`*.S`) files -- supports the given option. An optional 628 second option may be specified if the first option is not supported. 629 630 Example:: 631 632 #arch/sh/Makefile 633 cflags-y += $(call as-option,-Wa$(comma)-isa=$(isa-y),) 634 635 In the above example, cflags-y will be assigned the option 636 -Wa$(comma)-isa=$(isa-y) if it is supported by $(CC). 637 The second argument is optional, and if supplied will be used 638 if first argument is not supported. 639 640 as-instr 641 as-instr checks if the assembler reports a specific instruction 642 and then outputs either option1 or option2 643 C escapes are supported in the test instruction 644 Note: as-instr-option uses KBUILD_AFLAGS for assembler options 645 646 cc-option 647 cc-option is used to check if $(CC) supports a given option, and if 648 not supported to use an optional second option. 649 650 Example:: 651 652 #arch/x86/Makefile 653 cflags-y += $(call cc-option,-march=pentium-mmx,-march=i586) 654 655 In the above example, cflags-y will be assigned the option 656 -march=pentium-mmx if supported by $(CC), otherwise -march=i586. 657 The second argument to cc-option is optional, and if omitted, 658 cflags-y will be assigned no value if first option is not supported. 659 Note: cc-option uses KBUILD_CFLAGS for $(CC) options 660 661 cc-option-yn 662 cc-option-yn is used to check if gcc supports a given option 663 and return 'y' if supported, otherwise 'n'. 664 665 Example:: 666 667 #arch/ppc/Makefile 668 biarch := $(call cc-option-yn, -m32) 669 aflags-$(biarch) += -a32 670 cflags-$(biarch) += -m32 671 672 In the above example, $(biarch) is set to y if $(CC) supports the -m32 673 option. When $(biarch) equals 'y', the expanded variables $(aflags-y) 674 and $(cflags-y) will be assigned the values -a32 and -m32, 675 respectively. 676 Note: cc-option-yn uses KBUILD_CFLAGS for $(CC) options 677 678 cc-disable-warning 679 cc-disable-warning checks if gcc supports a given warning and returns 680 the commandline switch to disable it. This special function is needed, 681 because gcc 4.4 and later accept any unknown -Wno-* option and only 682 warn about it if there is another warning in the source file. 683 684 Example:: 685 686 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) 687 688 In the above example, -Wno-unused-but-set-variable will be added to 689 KBUILD_CFLAGS only if gcc really accepts it. 690 691 cc-ifversion 692 cc-ifversion tests the version of $(CC) and equals the fourth parameter 693 if version expression is true, or the fifth (if given) if the version 694 expression is false. 695 696 Example:: 697 698 #fs/reiserfs/Makefile 699 ccflags-y := $(call cc-ifversion, -lt, 0402, -O1) 700 701 In this example, ccflags-y will be assigned the value -O1 if the 702 $(CC) version is less than 4.2. 703 cc-ifversion takes all the shell operators: 704 -eq, -ne, -lt, -le, -gt, and -ge 705 The third parameter may be a text as in this example, but it may also 706 be an expanded variable or a macro. 707 708 cc-cross-prefix 709 cc-cross-prefix is used to check if there exists a $(CC) in path with 710 one of the listed prefixes. The first prefix where there exist a 711 prefix$(CC) in the PATH is returned - and if no prefix$(CC) is found 712 then nothing is returned. 713 Additional prefixes are separated by a single space in the 714 call of cc-cross-prefix. 715 This functionality is useful for architecture Makefiles that try 716 to set CROSS_COMPILE to well-known values but may have several 717 values to select between. 718 It is recommended only to try to set CROSS_COMPILE if it is a cross 719 build (host arch is different from target arch). And if CROSS_COMPILE 720 is already set then leave it with the old value. 721 722 Example:: 723 724 #arch/m68k/Makefile 725 ifneq ($(SUBARCH),$(ARCH)) 726 ifeq ($(CROSS_COMPILE),) 727 CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu-) 728 endif 729 endif 730 7313.14 $(LD) support functions 732---------------------------- 733 734 ld-option 735 ld-option is used to check if $(LD) supports the supplied option. 736 ld-option takes two options as arguments. 737 The second argument is an optional option that can be used if the 738 first option is not supported by $(LD). 739 740 Example:: 741 742 #Makefile 743 LDFLAGS_vmlinux += $(call ld-option, -X) 744 7453.15 Script invocation 746---------------------- 747 748 Make rules may invoke scripts to build the kernel. The rules shall 749 always provide the appropriate interpreter to execute the script. They 750 shall not rely on the execute bits being set, and shall not invoke the 751 script directly. For the convenience of manual script invocation, such 752 as invoking ./scripts/checkpatch.pl, it is recommended to set execute 753 bits on the scripts nonetheless. 754 755 Kbuild provides variables $(CONFIG_SHELL), $(AWK), $(PERL), 756 and $(PYTHON3) to refer to interpreters for the respective 757 scripts. 758 759 Example:: 760 761 #Makefile 762 cmd_depmod = $(CONFIG_SHELL) $(srctree)/scripts/depmod.sh $(DEPMOD) \ 763 $(KERNELRELEASE) 764 7654 Host Program support 766====================== 767 768Kbuild supports building executables on the host for use during the 769compilation stage. 770Two steps are required in order to use a host executable. 771 772The first step is to tell kbuild that a host program exists. This is 773done utilising the variable "hostprogs". 774 775The second step is to add an explicit dependency to the executable. 776This can be done in two ways. Either add the dependency in a rule, 777or utilise the variable "always-y". 778Both possibilities are described in the following. 779 7804.1 Simple Host Program 781----------------------- 782 783 In some cases there is a need to compile and run a program on the 784 computer where the build is running. 785 The following line tells kbuild that the program bin2hex shall be 786 built on the build host. 787 788 Example:: 789 790 hostprogs := bin2hex 791 792 Kbuild assumes in the above example that bin2hex is made from a single 793 c-source file named bin2hex.c located in the same directory as 794 the Makefile. 795 7964.2 Composite Host Programs 797--------------------------- 798 799 Host programs can be made up based on composite objects. 800 The syntax used to define composite objects for host programs is 801 similar to the syntax used for kernel objects. 802 $(<executable>-objs) lists all objects used to link the final 803 executable. 804 805 Example:: 806 807 #scripts/lxdialog/Makefile 808 hostprogs := lxdialog 809 lxdialog-objs := checklist.o lxdialog.o 810 811 Objects with extension .o are compiled from the corresponding .c 812 files. In the above example, checklist.c is compiled to checklist.o 813 and lxdialog.c is compiled to lxdialog.o. 814 815 Finally, the two .o files are linked to the executable, lxdialog. 816 Note: The syntax <executable>-y is not permitted for host-programs. 817 8184.3 Using C++ for host programs 819------------------------------- 820 821 kbuild offers support for host programs written in C++. This was 822 introduced solely to support kconfig, and is not recommended 823 for general use. 824 825 Example:: 826 827 #scripts/kconfig/Makefile 828 hostprogs := qconf 829 qconf-cxxobjs := qconf.o 830 831 In the example above the executable is composed of the C++ file 832 qconf.cc - identified by $(qconf-cxxobjs). 833 834 If qconf is composed of a mixture of .c and .cc files, then an 835 additional line can be used to identify this. 836 837 Example:: 838 839 #scripts/kconfig/Makefile 840 hostprogs := qconf 841 qconf-cxxobjs := qconf.o 842 qconf-objs := check.o 843 8444.4 Controlling compiler options for host programs 845-------------------------------------------------- 846 847 When compiling host programs, it is possible to set specific flags. 848 The programs will always be compiled utilising $(HOSTCC) passed 849 the options specified in $(KBUILD_HOSTCFLAGS). 850 To set flags that will take effect for all host programs created 851 in that Makefile, use the variable HOST_EXTRACFLAGS. 852 853 Example:: 854 855 #scripts/lxdialog/Makefile 856 HOST_EXTRACFLAGS += -I/usr/include/ncurses 857 858 To set specific flags for a single file the following construction 859 is used: 860 861 Example:: 862 863 #arch/ppc64/boot/Makefile 864 HOSTCFLAGS_piggyback.o := -DKERNELBASE=$(KERNELBASE) 865 866 It is also possible to specify additional options to the linker. 867 868 Example:: 869 870 #scripts/kconfig/Makefile 871 HOSTLDLIBS_qconf := -L$(QTDIR)/lib 872 873 When linking qconf, it will be passed the extra option 874 "-L$(QTDIR)/lib". 875 8764.5 When host programs are actually built 877----------------------------------------- 878 879 Kbuild will only build host-programs when they are referenced 880 as a prerequisite. 881 This is possible in two ways: 882 883 (1) List the prerequisite explicitly in a custom rule. 884 885 Example:: 886 887 #drivers/pci/Makefile 888 hostprogs := gen-devlist 889 $(obj)/devlist.h: $(src)/pci.ids $(obj)/gen-devlist 890 ( cd $(obj); ./gen-devlist ) < $< 891 892 The target $(obj)/devlist.h will not be built before 893 $(obj)/gen-devlist is updated. Note that references to 894 the host programs in custom rules must be prefixed with $(obj). 895 896 (2) Use always-y 897 898 When there is no suitable custom rule, and the host program 899 shall be built when a makefile is entered, the always-y 900 variable shall be used. 901 902 Example:: 903 904 #scripts/lxdialog/Makefile 905 hostprogs := lxdialog 906 always-y := $(hostprogs) 907 908 Kbuild provides the following shorthand for this: 909 910 hostprogs-always-y := lxdialog 911 912 This will tell kbuild to build lxdialog even if not referenced in 913 any rule. 914 9155 Userspace Program support 916=========================== 917 918Just like host programs, Kbuild also supports building userspace executables 919for the target architecture (i.e. the same architecture as you are building 920the kernel for). 921 922The syntax is quite similar. The difference is to use "userprogs" instead of 923"hostprogs". 924 9255.1 Simple Userspace Program 926---------------------------- 927 928 The following line tells kbuild that the program bpf-direct shall be 929 built for the target architecture. 930 931 Example:: 932 933 userprogs := bpf-direct 934 935 Kbuild assumes in the above example that bpf-direct is made from a 936 single C source file named bpf-direct.c located in the same directory 937 as the Makefile. 938 9395.2 Composite Userspace Programs 940-------------------------------- 941 942 Userspace programs can be made up based on composite objects. 943 The syntax used to define composite objects for userspace programs is 944 similar to the syntax used for kernel objects. 945 $(<executable>-objs) lists all objects used to link the final 946 executable. 947 948 Example:: 949 950 #samples/seccomp/Makefile 951 userprogs := bpf-fancy 952 bpf-fancy-objs := bpf-fancy.o bpf-helper.o 953 954 Objects with extension .o are compiled from the corresponding .c 955 files. In the above example, bpf-fancy.c is compiled to bpf-fancy.o 956 and bpf-helper.c is compiled to bpf-helper.o. 957 958 Finally, the two .o files are linked to the executable, bpf-fancy. 959 Note: The syntax <executable>-y is not permitted for userspace programs. 960 9615.3 Controlling compiler options for userspace programs 962------------------------------------------------------- 963 964 When compiling userspace programs, it is possible to set specific flags. 965 The programs will always be compiled utilising $(CC) passed 966 the options specified in $(KBUILD_USERCFLAGS). 967 To set flags that will take effect for all userspace programs created 968 in that Makefile, use the variable userccflags. 969 970 Example:: 971 972 # samples/seccomp/Makefile 973 userccflags += -I usr/include 974 975 To set specific flags for a single file the following construction 976 is used: 977 978 Example:: 979 980 bpf-helper-userccflags += -I user/include 981 982 It is also possible to specify additional options to the linker. 983 984 Example:: 985 986 # net/bpfilter/Makefile 987 bpfilter_umh-userldflags += -static 988 989 When linking bpfilter_umh, it will be passed the extra option -static. 990 9915.4 When userspace programs are actually built 992---------------------------------------------- 993 994 Kbuild builds userspace programs only when told to do so. 995 There are two ways to do this. 996 997 (1) Add it as the prerequisite of another file 998 999 Example:: 1000 1001 #net/bpfilter/Makefile 1002 userprogs := bpfilter_umh 1003 $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh 1004 1005 $(obj)/bpfilter_umh is built before $(obj)/bpfilter_umh_blob.o 1006 1007 (2) Use always-y 1008 1009 Example:: 1010 1011 userprogs := binderfs_example 1012 always-y := $(userprogs) 1013 1014 Kbuild provides the following shorthand for this: 1015 1016 userprogs-always-y := binderfs_example 1017 1018 This will tell Kbuild to build binderfs_example when it visits this 1019 Makefile. 1020 10216 Kbuild clean infrastructure 1022============================= 1023 1024"make clean" deletes most generated files in the obj tree where the kernel 1025is compiled. This includes generated files such as host programs. 1026Kbuild knows targets listed in $(hostprogs), $(always-y), $(always-m), 1027$(always-), $(extra-y), $(extra-) and $(targets). They are all deleted 1028during "make clean". Files matching the patterns "*.[oas]", "*.ko", plus 1029some additional files generated by kbuild are deleted all over the kernel 1030source tree when "make clean" is executed. 1031 1032Additional files or directories can be specified in kbuild makefiles by use of 1033$(clean-files). 1034 1035 Example:: 1036 1037 #lib/Makefile 1038 clean-files := crc32table.h 1039 1040When executing "make clean", the file "crc32table.h" will be deleted. 1041Kbuild will assume files to be in the same relative directory as the 1042Makefile, except if prefixed with $(objtree). 1043 1044To exclude certain files or directories from make clean, use the 1045$(no-clean-files) variable. 1046 1047Usually kbuild descends down in subdirectories due to "obj-* := dir/", 1048but in the architecture makefiles where the kbuild infrastructure 1049is not sufficient this sometimes needs to be explicit. 1050 1051 Example:: 1052 1053 #arch/x86/boot/Makefile 1054 subdir- := compressed 1055 1056The above assignment instructs kbuild to descend down in the 1057directory compressed/ when "make clean" is executed. 1058 1059To support the clean infrastructure in the Makefiles that build the 1060final bootimage there is an optional target named archclean: 1061 1062 Example:: 1063 1064 #arch/x86/Makefile 1065 archclean: 1066 $(Q)$(MAKE) $(clean)=arch/x86/boot 1067 1068When "make clean" is executed, make will descend down in arch/x86/boot, 1069and clean as usual. The Makefile located in arch/x86/boot/ may use 1070the subdir- trick to descend further down. 1071 1072Note 1: arch/$(SRCARCH)/Makefile cannot use "subdir-", because that file is 1073included in the top level makefile, and the kbuild infrastructure 1074is not operational at that point. 1075 1076Note 2: All directories listed in core-y, libs-y, drivers-y and net-y will 1077be visited during "make clean". 1078 10797 Architecture Makefiles 1080======================== 1081 1082The top level Makefile sets up the environment and does the preparation, 1083before starting to descend down in the individual directories. 1084The top level makefile contains the generic part, whereas 1085arch/$(SRCARCH)/Makefile contains what is required to set up kbuild 1086for said architecture. 1087To do so, arch/$(SRCARCH)/Makefile sets up a number of variables and defines 1088a few targets. 1089 1090When kbuild executes, the following steps are followed (roughly): 1091 10921) Configuration of the kernel => produce .config 10932) Store kernel version in include/linux/version.h 10943) Updating all other prerequisites to the target prepare: 1095 - Additional prerequisites are specified in arch/$(SRCARCH)/Makefile 10964) Recursively descend down in all directories listed in 1097 init-* core* drivers-* net-* libs-* and build all targets. 1098 - The values of the above variables are expanded in arch/$(SRCARCH)/Makefile. 10995) All object files are then linked and the resulting file vmlinux is 1100 located at the root of the obj tree. 1101 The very first objects linked are listed in head-y, assigned by 1102 arch/$(SRCARCH)/Makefile. 11036) Finally, the architecture-specific part does any required post processing 1104 and builds the final bootimage. 1105 - This includes building boot records 1106 - Preparing initrd images and the like 1107 1108 11097.1 Set variables to tweak the build to the architecture 1110-------------------------------------------------------- 1111 1112 KBUILD_LDFLAGS 1113 Generic $(LD) options 1114 1115 Flags used for all invocations of the linker. 1116 Often specifying the emulation is sufficient. 1117 1118 Example:: 1119 1120 #arch/s390/Makefile 1121 KBUILD_LDFLAGS := -m elf_s390 1122 1123 Note: ldflags-y can be used to further customise 1124 the flags used. See section 3.7. 1125 1126 LDFLAGS_vmlinux 1127 Options for $(LD) when linking vmlinux 1128 1129 LDFLAGS_vmlinux is used to specify additional flags to pass to 1130 the linker when linking the final vmlinux image. 1131 LDFLAGS_vmlinux uses the LDFLAGS_$@ support. 1132 1133 Example:: 1134 1135 #arch/x86/Makefile 1136 LDFLAGS_vmlinux := -e stext 1137 1138 OBJCOPYFLAGS 1139 objcopy flags 1140 1141 When $(call if_changed,objcopy) is used to translate a .o file, 1142 the flags specified in OBJCOPYFLAGS will be used. 1143 $(call if_changed,objcopy) is often used to generate raw binaries on 1144 vmlinux. 1145 1146 Example:: 1147 1148 #arch/s390/Makefile 1149 OBJCOPYFLAGS := -O binary 1150 1151 #arch/s390/boot/Makefile 1152 $(obj)/image: vmlinux FORCE 1153 $(call if_changed,objcopy) 1154 1155 In this example, the binary $(obj)/image is a binary version of 1156 vmlinux. The usage of $(call if_changed,xxx) will be described later. 1157 1158 KBUILD_AFLAGS 1159 Assembler flags 1160 1161 Default value - see top level Makefile 1162 Append or modify as required per architecture. 1163 1164 Example:: 1165 1166 #arch/sparc64/Makefile 1167 KBUILD_AFLAGS += -m64 -mcpu=ultrasparc 1168 1169 KBUILD_CFLAGS 1170 $(CC) compiler flags 1171 1172 Default value - see top level Makefile 1173 Append or modify as required per architecture. 1174 1175 Often, the KBUILD_CFLAGS variable depends on the configuration. 1176 1177 Example:: 1178 1179 #arch/x86/boot/compressed/Makefile 1180 cflags-$(CONFIG_X86_32) := -march=i386 1181 cflags-$(CONFIG_X86_64) := -mcmodel=small 1182 KBUILD_CFLAGS += $(cflags-y) 1183 1184 Many arch Makefiles dynamically run the target C compiler to 1185 probe supported options:: 1186 1187 #arch/x86/Makefile 1188 1189 ... 1190 cflags-$(CONFIG_MPENTIUMII) += $(call cc-option,\ 1191 -march=pentium2,-march=i686) 1192 ... 1193 # Disable unit-at-a-time mode ... 1194 KBUILD_CFLAGS += $(call cc-option,-fno-unit-at-a-time) 1195 ... 1196 1197 1198 The first example utilises the trick that a config option expands 1199 to 'y' when selected. 1200 1201 KBUILD_AFLAGS_KERNEL 1202 Assembler options specific for built-in 1203 1204 $(KBUILD_AFLAGS_KERNEL) contains extra C compiler flags used to compile 1205 resident kernel code. 1206 1207 KBUILD_AFLAGS_MODULE 1208 Assembler options specific for modules 1209 1210 $(KBUILD_AFLAGS_MODULE) is used to add arch-specific options that 1211 are used for assembler. 1212 1213 From commandline AFLAGS_MODULE shall be used (see kbuild.rst). 1214 1215 KBUILD_CFLAGS_KERNEL 1216 $(CC) options specific for built-in 1217 1218 $(KBUILD_CFLAGS_KERNEL) contains extra C compiler flags used to compile 1219 resident kernel code. 1220 1221 KBUILD_CFLAGS_MODULE 1222 Options for $(CC) when building modules 1223 1224 $(KBUILD_CFLAGS_MODULE) is used to add arch-specific options that 1225 are used for $(CC). 1226 From commandline CFLAGS_MODULE shall be used (see kbuild.rst). 1227 1228 KBUILD_LDFLAGS_MODULE 1229 Options for $(LD) when linking modules 1230 1231 $(KBUILD_LDFLAGS_MODULE) is used to add arch-specific options 1232 used when linking modules. This is often a linker script. 1233 1234 From commandline LDFLAGS_MODULE shall be used (see kbuild.rst). 1235 1236 KBUILD_LDS 1237 1238 The linker script with full path. Assigned by the top-level Makefile. 1239 1240 KBUILD_LDS_MODULE 1241 1242 The module linker script with full path. Assigned by the top-level 1243 Makefile and additionally by the arch Makefile. 1244 1245 KBUILD_VMLINUX_OBJS 1246 1247 All object files for vmlinux. They are linked to vmlinux in the same 1248 order as listed in KBUILD_VMLINUX_OBJS. 1249 1250 KBUILD_VMLINUX_LIBS 1251 1252 All .a "lib" files for vmlinux. KBUILD_VMLINUX_OBJS and 1253 KBUILD_VMLINUX_LIBS together specify all the object files used to 1254 link vmlinux. 1255 12567.2 Add prerequisites to archheaders 1257------------------------------------ 1258 1259 The archheaders: rule is used to generate header files that 1260 may be installed into user space by "make header_install". 1261 1262 It is run before "make archprepare" when run on the 1263 architecture itself. 1264 1265 12667.3 Add prerequisites to archprepare 1267------------------------------------ 1268 1269 The archprepare: rule is used to list prerequisites that need to be 1270 built before starting to descend down in the subdirectories. 1271 This is usually used for header files containing assembler constants. 1272 1273 Example:: 1274 1275 #arch/arm/Makefile 1276 archprepare: maketools 1277 1278 In this example, the file target maketools will be processed 1279 before descending down in the subdirectories. 1280 See also chapter XXX-TODO that describes how kbuild supports 1281 generating offset header files. 1282 1283 12847.4 List directories to visit when descending 1285--------------------------------------------- 1286 1287 An arch Makefile cooperates with the top Makefile to define variables 1288 which specify how to build the vmlinux file. Note that there is no 1289 corresponding arch-specific section for modules; the module-building 1290 machinery is all architecture-independent. 1291 1292 1293 head-y, core-y, libs-y, drivers-y 1294 $(head-y) lists objects to be linked first in vmlinux. 1295 1296 $(libs-y) lists directories where a lib.a archive can be located. 1297 1298 The rest list directories where a built-in.a object file can be 1299 located. 1300 1301 Then the rest follows in this order: 1302 1303 $(core-y), $(libs-y), $(drivers-y) 1304 1305 The top level Makefile defines values for all generic directories, 1306 and arch/$(SRCARCH)/Makefile only adds architecture-specific 1307 directories. 1308 1309 Example:: 1310 1311 # arch/sparc/Makefile 1312 core-y += arch/sparc/ 1313 1314 libs-y += arch/sparc/prom/ 1315 libs-y += arch/sparc/lib/ 1316 1317 drivers-$(CONFIG_PM) += arch/sparc/power/ 1318 13197.5 Architecture-specific boot images 1320------------------------------------- 1321 1322 An arch Makefile specifies goals that take the vmlinux file, compress 1323 it, wrap it in bootstrapping code, and copy the resulting files 1324 somewhere. This includes various kinds of installation commands. 1325 The actual goals are not standardized across architectures. 1326 1327 It is common to locate any additional processing in a boot/ 1328 directory below arch/$(SRCARCH)/. 1329 1330 Kbuild does not provide any smart way to support building a 1331 target specified in boot/. Therefore arch/$(SRCARCH)/Makefile shall 1332 call make manually to build a target in boot/. 1333 1334 The recommended approach is to include shortcuts in 1335 arch/$(SRCARCH)/Makefile, and use the full path when calling down 1336 into the arch/$(SRCARCH)/boot/Makefile. 1337 1338 Example:: 1339 1340 #arch/x86/Makefile 1341 boot := arch/x86/boot 1342 bzImage: vmlinux 1343 $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@ 1344 1345 "$(Q)$(MAKE) $(build)=<dir>" is the recommended way to invoke 1346 make in a subdirectory. 1347 1348 There are no rules for naming architecture-specific targets, 1349 but executing "make help" will list all relevant targets. 1350 To support this, $(archhelp) must be defined. 1351 1352 Example:: 1353 1354 #arch/x86/Makefile 1355 define archhelp 1356 echo '* bzImage - Compressed kernel image (arch/x86/boot/bzImage)' 1357 endif 1358 1359 When make is executed without arguments, the first goal encountered 1360 will be built. In the top level Makefile the first goal present 1361 is all:. 1362 An architecture shall always, per default, build a bootable image. 1363 In "make help", the default goal is highlighted with a '*'. 1364 Add a new prerequisite to all: to select a default goal different 1365 from vmlinux. 1366 1367 Example:: 1368 1369 #arch/x86/Makefile 1370 all: bzImage 1371 1372 When "make" is executed without arguments, bzImage will be built. 1373 13747.7 Commands useful for building a boot image 1375--------------------------------------------- 1376 1377 Kbuild provides a few macros that are useful when building a 1378 boot image. 1379 1380 ld 1381 Link target. Often, LDFLAGS_$@ is used to set specific options to ld. 1382 1383 Example:: 1384 1385 #arch/x86/boot/Makefile 1386 LDFLAGS_bootsect := -Ttext 0x0 -s --oformat binary 1387 LDFLAGS_setup := -Ttext 0x0 -s --oformat binary -e begtext 1388 1389 targets += setup setup.o bootsect bootsect.o 1390 $(obj)/setup $(obj)/bootsect: %: %.o FORCE 1391 $(call if_changed,ld) 1392 1393 In this example, there are two possible targets, requiring different 1394 options to the linker. The linker options are specified using the 1395 LDFLAGS_$@ syntax - one for each potential target. 1396 $(targets) are assigned all potential targets, by which kbuild knows 1397 the targets and will: 1398 1399 1) check for commandline changes 1400 2) delete target during make clean 1401 1402 The ": %: %.o" part of the prerequisite is a shorthand that 1403 frees us from listing the setup.o and bootsect.o files. 1404 1405 Note: 1406 It is a common mistake to forget the "targets :=" assignment, 1407 resulting in the target file being recompiled for no 1408 obvious reason. 1409 1410 objcopy 1411 Copy binary. Uses OBJCOPYFLAGS usually specified in 1412 arch/$(SRCARCH)/Makefile. 1413 OBJCOPYFLAGS_$@ may be used to set additional options. 1414 1415 gzip 1416 Compress target. Use maximum compression to compress target. 1417 1418 Example:: 1419 1420 #arch/x86/boot/compressed/Makefile 1421 $(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE 1422 $(call if_changed,gzip) 1423 1424 dtc 1425 Create flattened device tree blob object suitable for linking 1426 into vmlinux. Device tree blobs linked into vmlinux are placed 1427 in an init section in the image. Platform code *must* copy the 1428 blob to non-init memory prior to calling unflatten_device_tree(). 1429 1430 To use this command, simply add `*.dtb` into obj-y or targets, or make 1431 some other target depend on `%.dtb` 1432 1433 A central rule exists to create `$(obj)/%.dtb` from `$(src)/%.dts`; 1434 architecture Makefiles do no need to explicitly write out that rule. 1435 1436 Example:: 1437 1438 targets += $(dtb-y) 1439 DTC_FLAGS ?= -p 1024 1440 14417.9 Preprocessing linker scripts 1442-------------------------------- 1443 1444 When the vmlinux image is built, the linker script 1445 arch/$(SRCARCH)/kernel/vmlinux.lds is used. 1446 The script is a preprocessed variant of the file vmlinux.lds.S 1447 located in the same directory. 1448 kbuild knows .lds files and includes a rule `*lds.S` -> `*lds`. 1449 1450 Example:: 1451 1452 #arch/x86/kernel/Makefile 1453 extra-y := vmlinux.lds 1454 1455 The assignment to extra-y is used to tell kbuild to build the 1456 target vmlinux.lds. 1457 The assignment to $(CPPFLAGS_vmlinux.lds) tells kbuild to use the 1458 specified options when building the target vmlinux.lds. 1459 1460 When building the `*.lds` target, kbuild uses the variables:: 1461 1462 KBUILD_CPPFLAGS : Set in top-level Makefile 1463 cppflags-y : May be set in the kbuild makefile 1464 CPPFLAGS_$(@F) : Target-specific flags. 1465 Note that the full filename is used in this 1466 assignment. 1467 1468 The kbuild infrastructure for `*lds` files is used in several 1469 architecture-specific files. 1470 14717.10 Generic header files 1472------------------------- 1473 1474 The directory include/asm-generic contains the header files 1475 that may be shared between individual architectures. 1476 The recommended approach how to use a generic header file is 1477 to list the file in the Kbuild file. 1478 See "8.2 generic-y" for further info on syntax etc. 1479 14807.11 Post-link pass 1481------------------- 1482 1483 If the file arch/xxx/Makefile.postlink exists, this makefile 1484 will be invoked for post-link objects (vmlinux and modules.ko) 1485 for architectures to run post-link passes on. Must also handle 1486 the clean target. 1487 1488 This pass runs after kallsyms generation. If the architecture 1489 needs to modify symbol locations, rather than manipulate the 1490 kallsyms, it may be easier to add another postlink target for 1491 .tmp_vmlinux? targets to be called from link-vmlinux.sh. 1492 1493 For example, powerpc uses this to check relocation sanity of 1494 the linked vmlinux file. 1495 14968 Kbuild syntax for exported headers 1497------------------------------------ 1498 1499The kernel includes a set of headers that is exported to userspace. 1500Many headers can be exported as-is but other headers require a 1501minimal pre-processing before they are ready for user-space. 1502The pre-processing does: 1503 1504- drop kernel-specific annotations 1505- drop include of compiler.h 1506- drop all sections that are kernel internal (guarded by `ifdef __KERNEL__`) 1507 1508All headers under include/uapi/, include/generated/uapi/, 1509arch/<arch>/include/uapi/ and arch/<arch>/include/generated/uapi/ 1510are exported. 1511 1512A Kbuild file may be defined under arch/<arch>/include/uapi/asm/ and 1513arch/<arch>/include/asm/ to list asm files coming from asm-generic. 1514See subsequent chapter for the syntax of the Kbuild file. 1515 15168.1 no-export-headers 1517--------------------- 1518 1519 no-export-headers is essentially used by include/uapi/linux/Kbuild to 1520 avoid exporting specific headers (e.g. kvm.h) on architectures that do 1521 not support it. It should be avoided as much as possible. 1522 15238.2 generic-y 1524------------- 1525 1526 If an architecture uses a verbatim copy of a header from 1527 include/asm-generic then this is listed in the file 1528 arch/$(SRCARCH)/include/asm/Kbuild like this: 1529 1530 Example:: 1531 1532 #arch/x86/include/asm/Kbuild 1533 generic-y += termios.h 1534 generic-y += rtc.h 1535 1536 During the prepare phase of the build a wrapper include 1537 file is generated in the directory:: 1538 1539 arch/$(SRCARCH)/include/generated/asm 1540 1541 When a header is exported where the architecture uses 1542 the generic header a similar wrapper is generated as part 1543 of the set of exported headers in the directory:: 1544 1545 usr/include/asm 1546 1547 The generated wrapper will in both cases look like the following: 1548 1549 Example: termios.h:: 1550 1551 #include <asm-generic/termios.h> 1552 15538.3 generated-y 1554--------------- 1555 1556 If an architecture generates other header files alongside generic-y 1557 wrappers, generated-y specifies them. 1558 1559 This prevents them being treated as stale asm-generic wrappers and 1560 removed. 1561 1562 Example:: 1563 1564 #arch/x86/include/asm/Kbuild 1565 generated-y += syscalls_32.h 1566 15678.4 mandatory-y 1568--------------- 1569 1570 mandatory-y is essentially used by include/(uapi/)asm-generic/Kbuild 1571 to define the minimum set of ASM headers that all architectures must have. 1572 1573 This works like optional generic-y. If a mandatory header is missing 1574 in arch/$(SRCARCH)/include/(uapi/)/asm, Kbuild will automatically 1575 generate a wrapper of the asm-generic one. 1576 15779 Kbuild Variables 1578================== 1579 1580The top Makefile exports the following variables: 1581 1582 VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION 1583 These variables define the current kernel version. A few arch 1584 Makefiles actually use these values directly; they should use 1585 $(KERNELRELEASE) instead. 1586 1587 $(VERSION), $(PATCHLEVEL), and $(SUBLEVEL) define the basic 1588 three-part version number, such as "2", "4", and "0". These three 1589 values are always numeric. 1590 1591 $(EXTRAVERSION) defines an even tinier sublevel for pre-patches 1592 or additional patches. It is usually some non-numeric string 1593 such as "-pre4", and is often blank. 1594 1595 KERNELRELEASE 1596 $(KERNELRELEASE) is a single string such as "2.4.0-pre4", suitable 1597 for constructing installation directory names or showing in 1598 version strings. Some arch Makefiles use it for this purpose. 1599 1600 ARCH 1601 This variable defines the target architecture, such as "i386", 1602 "arm", or "sparc". Some kbuild Makefiles test $(ARCH) to 1603 determine which files to compile. 1604 1605 By default, the top Makefile sets $(ARCH) to be the same as the 1606 host system architecture. For a cross build, a user may 1607 override the value of $(ARCH) on the command line:: 1608 1609 make ARCH=m68k ... 1610 1611 SRCARCH 1612 This variable specifies the directory in arch/ to build. 1613 1614 ARCH and SRCARCH may not necessarily match. A couple of arch 1615 directories are biarch, that is, a single `arch/*/` directory supports 1616 both 32-bit and 64-bit. 1617 1618 For example, you can pass in ARCH=i386, ARCH=x86_64, or ARCH=x86. 1619 For all of them, SRCARCH=x86 because arch/x86/ supports both i386 and 1620 x86_64. 1621 1622 INSTALL_PATH 1623 This variable defines a place for the arch Makefiles to install 1624 the resident kernel image and System.map file. 1625 Use this for architecture-specific install targets. 1626 1627 INSTALL_MOD_PATH, MODLIB 1628 $(INSTALL_MOD_PATH) specifies a prefix to $(MODLIB) for module 1629 installation. This variable is not defined in the Makefile but 1630 may be passed in by the user if desired. 1631 1632 $(MODLIB) specifies the directory for module installation. 1633 The top Makefile defines $(MODLIB) to 1634 $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE). The user may 1635 override this value on the command line if desired. 1636 1637 INSTALL_MOD_STRIP 1638 If this variable is specified, it will cause modules to be stripped 1639 after they are installed. If INSTALL_MOD_STRIP is '1', then the 1640 default option --strip-debug will be used. Otherwise, the 1641 INSTALL_MOD_STRIP value will be used as the option(s) to the strip 1642 command. 1643 1644 164510 Makefile language 1646==================== 1647 1648The kernel Makefiles are designed to be run with GNU Make. The Makefiles 1649use only the documented features of GNU Make, but they do use many 1650GNU extensions. 1651 1652GNU Make supports elementary list-processing functions. The kernel 1653Makefiles use a novel style of list building and manipulation with few 1654"if" statements. 1655 1656GNU Make has two assignment operators, ":=" and "=". ":=" performs 1657immediate evaluation of the right-hand side and stores an actual string 1658into the left-hand side. "=" is like a formula definition; it stores the 1659right-hand side in an unevaluated form and then evaluates this form each 1660time the left-hand side is used. 1661 1662There are some cases where "=" is appropriate. Usually, though, ":=" 1663is the right choice. 1664 166511 Credits 1666========== 1667 1668- Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net> 1669- Updates by Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de> 1670- Updates by Sam Ravnborg <sam@ravnborg.org> 1671- Language QA by Jan Engelhardt <jengelh@gmx.de> 1672 167312 TODO 1674======= 1675 1676- Describe how kbuild supports shipped files with _shipped. 1677- Generating offset header files. 1678- Add more variables to chapters 7 or 9? 1679