5 This document describes the Linux kernel Makefiles.
10 The Makefiles have five parts::
12 Makefile the top Makefile.
13 .config the kernel configuration file.
14 arch/$(SRCARCH)/Makefile the arch Makefile.
15 scripts/Makefile.* common rules etc. for all kbuild Makefiles.
16 kbuild Makefiles exist in every subdirectory
18 The top Makefile reads the .config file, which comes from the kernel
19 configuration process.
21 The top Makefile is responsible for building two major products: vmlinux
22 (the resident kernel image) and modules (any module files).
23 It builds these goals by recursively descending into the subdirectories of
24 the kernel source tree.
26 The list of subdirectories which are visited depends upon the kernel
27 configuration. The top Makefile textually includes an arch Makefile
28 with the name arch/$(SRCARCH)/Makefile. The arch Makefile supplies
29 architecture-specific information to the top Makefile.
31 Each subdirectory has a kbuild Makefile which carries out the commands
32 passed down from above. The kbuild Makefile uses information from the
33 .config file to construct various file lists used by kbuild to build
34 any built-in or modular targets.
36 scripts/Makefile.* contains all the definitions/rules etc. that
37 are used to build the kernel based on the kbuild makefiles.
42 People have four different relationships with the kernel Makefiles.
44 *Users* are people who build kernels. These people type commands such as
45 ``make menuconfig`` or ``make``. They usually do not read or edit
46 any kernel Makefiles (or any other source files).
48 *Normal developers* are people who work on features such as device
49 drivers, file systems, and network protocols. These people need to
50 maintain the kbuild Makefiles for the subsystem they are
51 working on. In order to do this effectively, they need some overall
52 knowledge about the kernel Makefiles, plus detailed knowledge about the
53 public interface for kbuild.
55 *Arch developers* are people who work on an entire architecture, such
56 as sparc or ia64. Arch developers need to know about the arch Makefile
57 as well as kbuild Makefiles.
59 *Kbuild developers* are people who work on the kernel build system itself.
60 These people need to know about all aspects of the kernel Makefiles.
62 This document is aimed towards normal developers and arch developers.
68 Most Makefiles within the kernel are kbuild Makefiles that use the
69 kbuild infrastructure. This chapter introduces the syntax used in the
72 The preferred name for the kbuild files are ``Makefile`` but ``Kbuild`` can
73 be used and if both a ``Makefile`` and a ``Kbuild`` file exists, then the ``Kbuild``
76 Section `Goal definitions`_ is a quick intro; further chapters provide
77 more details, with real examples.
82 Goal definitions are the main part (heart) of the kbuild Makefile.
83 These lines define the files to be built, any special compilation
84 options, and any subdirectories to be entered recursively.
86 The most simple kbuild makefile contains one line:
92 This tells kbuild that there is one object in that directory, named
93 foo.o. foo.o will be built from foo.c or foo.S.
95 If foo.o shall be built as a module, the variable obj-m is used.
96 Therefore the following pattern is often used:
100 obj-$(CONFIG_FOO) += foo.o
102 $(CONFIG_FOO) evaluates to either y (for built-in) or m (for module).
103 If CONFIG_FOO is neither y nor m, then the file will not be compiled
106 Built-in object goals - obj-y
107 -----------------------------
109 The kbuild Makefile specifies object files for vmlinux
110 in the $(obj-y) lists. These lists depend on the kernel
113 Kbuild compiles all the $(obj-y) files. It then calls
114 ``$(AR) rcSTP`` to merge these files into one built-in.a file.
115 This is a thin archive without a symbol table. It will be later
116 linked into vmlinux by scripts/link-vmlinux.sh
118 The order of files in $(obj-y) is significant. Duplicates in
119 the lists are allowed: the first instance will be linked into
120 built-in.a and succeeding instances will be ignored.
122 Link order is significant, because certain functions
123 (module_init() / __initcall) will be called during boot in the
124 order they appear. So keep in mind that changing the link
125 order may e.g. change the order in which your SCSI
126 controllers are detected, and thus your disks are renumbered.
130 #drivers/isdn/i4l/Makefile
131 # Makefile for the kernel ISDN subsystem and device drivers.
132 # Each configuration option enables a list of files.
133 obj-$(CONFIG_ISDN_I4L) += isdn.o
134 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
136 Loadable module goals - obj-m
137 -----------------------------
139 $(obj-m) specifies object files which are built as loadable
142 A module may be built from one source file or several source
143 files. In the case of one source file, the kbuild makefile
144 simply adds the file to $(obj-m).
148 #drivers/isdn/i4l/Makefile
149 obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
151 Note: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to "m"
153 If a kernel module is built from several source files, you specify
154 that you want to build a module in the same way as above; however,
155 kbuild needs to know which object files you want to build your
156 module from, so you have to tell it by setting a $(<module_name>-y)
161 #drivers/isdn/i4l/Makefile
162 obj-$(CONFIG_ISDN_I4L) += isdn.o
163 isdn-y := isdn_net_lib.o isdn_v110.o isdn_common.o
165 In this example, the module name will be isdn.o. Kbuild will
166 compile the objects listed in $(isdn-y) and then run
167 ``$(LD) -r`` on the list of these files to generate isdn.o.
169 Due to kbuild recognizing $(<module_name>-y) for composite objects,
170 you can use the value of a ``CONFIG_`` symbol to optionally include an
171 object file as part of a composite object.
176 obj-$(CONFIG_EXT2_FS) += ext2.o
177 ext2-y := balloc.o dir.o file.o ialloc.o inode.o ioctl.o \
178 namei.o super.o symlink.o
179 ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o \
182 In this example, xattr.o, xattr_user.o and xattr_trusted.o are only
183 part of the composite object ext2.o if $(CONFIG_EXT2_FS_XATTR)
186 Note: Of course, when you are building objects into the kernel,
187 the syntax above will also work. So, if you have CONFIG_EXT2_FS=y,
188 kbuild will build an ext2.o file for you out of the individual
189 parts and then link this into built-in.a, as you would expect.
191 Library file goals - lib-y
192 --------------------------
194 Objects listed with obj-* are used for modules, or
195 combined in a built-in.a for that specific directory.
196 There is also the possibility to list objects that will
197 be included in a library, lib.a.
198 All objects listed with lib-y are combined in a single
199 library for that directory.
200 Objects that are listed in obj-y and additionally listed in
201 lib-y will not be included in the library, since they will
202 be accessible anyway.
203 For consistency, objects listed in lib-m will be included in lib.a.
205 Note that the same kbuild makefile may list files to be built-in
206 and to be part of a library. Therefore the same directory
207 may contain both a built-in.a and a lib.a file.
211 #arch/x86/lib/Makefile
214 This will create a library lib.a based on delay.o. For kbuild to
215 actually recognize that there is a lib.a being built, the directory
216 shall be listed in libs-y.
218 See also `List directories to visit when descending`_.
220 Use of lib-y is normally restricted to ``lib/`` and ``arch/*/lib``.
222 Descending down in directories
223 ------------------------------
225 A Makefile is only responsible for building objects in its own
226 directory. Files in subdirectories should be taken care of by
227 Makefiles in these subdirs. The build system will automatically
228 invoke make recursively in subdirectories, provided you let it know of
231 To do so, obj-y and obj-m are used.
232 ext2 lives in a separate directory, and the Makefile present in fs/
233 tells kbuild to descend down using the following assignment.
238 obj-$(CONFIG_EXT2_FS) += ext2/
240 If CONFIG_EXT2_FS is set to either "y" (built-in) or "m" (modular)
241 the corresponding obj- variable will be set, and kbuild will descend
242 down in the ext2 directory.
244 Kbuild uses this information not only to decide that it needs to visit
245 the directory, but also to decide whether or not to link objects from
246 the directory into vmlinux.
248 When Kbuild descends into the directory with "y", all built-in objects
249 from that directory are combined into the built-in.a, which will be
250 eventually linked into vmlinux.
252 When Kbuild descends into the directory with "m", in contrast, nothing
253 from that directory will be linked into vmlinux. If the Makefile in
254 that directory specifies obj-y, those objects will be left orphan.
255 It is very likely a bug of the Makefile or of dependencies in Kconfig.
257 Kbuild also supports dedicated syntax, subdir-y and subdir-m, for
258 descending into subdirectories. It is a good fit when you know they
259 do not contain kernel-space objects at all. A typical usage is to let
260 Kbuild descend into subdirectories to build tools.
265 subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
266 subdir-$(CONFIG_MODVERSIONS) += genksyms
267 subdir-$(CONFIG_SECURITY_SELINUX) += selinux
269 Unlike obj-y/m, subdir-y/m does not need the trailing slash since this
270 syntax is always used for directories.
272 It is good practice to use a ``CONFIG_`` variable when assigning directory
273 names. This allows kbuild to totally skip the directory if the
274 corresponding ``CONFIG_`` option is neither "y" nor "m".
276 Non-builtin vmlinux targets - extra-y
277 -------------------------------------
279 extra-y specifies targets which are needed for building vmlinux,
280 but not combined into built-in.a.
284 1) vmlinux linker script
286 The linker script for vmlinux is located at
287 arch/$(SRCARCH)/kernel/vmlinux.lds
291 # arch/x86/kernel/Makefile
292 extra-y += vmlinux.lds
294 $(extra-y) should only contain targets needed for vmlinux.
296 Kbuild skips extra-y when vmlinux is apparently not a final goal.
297 (e.g. ``make modules``, or building external modules)
299 If you intend to build targets unconditionally, always-y (explained
300 in the next section) is the correct syntax to use.
302 Always built goals - always-y
303 -----------------------------
305 always-y specifies targets which are literally always built when
306 Kbuild visits the Makefile.
311 offsets-file := include/generated/asm-offsets.h
312 always-y += $(offsets-file)
317 ccflags-y, asflags-y and ldflags-y
318 These three flags apply only to the kbuild makefile in which they
319 are assigned. They are used for all the normal cc, as and ld
320 invocations happening during a recursive build.
321 Note: Flags with the same behaviour were previously named:
322 EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS.
323 They are still supported but their usage is deprecated.
325 ccflags-y specifies options for compiling with $(CC).
329 # drivers/acpi/acpica/Makefile
330 ccflags-y := -Os -D_LINUX -DBUILDING_ACPICA
331 ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT
333 This variable is necessary because the top Makefile owns the
334 variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
337 asflags-y specifies assembler options.
341 #arch/sparc/kernel/Makefile
344 ldflags-y specifies options for linking with $(LD).
348 #arch/cris/boot/compressed/Makefile
349 ldflags-y += -T $(srctree)/$(src)/decompress_$(arch-y).lds
351 subdir-ccflags-y, subdir-asflags-y
352 The two flags listed above are similar to ccflags-y and asflags-y.
353 The difference is that the subdir- variants have effect for the kbuild
354 file where they are present and all subdirectories.
355 Options specified using subdir-* are added to the commandline before
356 the options specified using the non-subdir variants.
360 subdir-ccflags-y := -Werror
362 ccflags-remove-y, asflags-remove-y
363 These flags are used to remove particular flags for the compiler,
364 assembler invocations.
368 ccflags-remove-$(CONFIG_MCOUNT) += -pg
371 CFLAGS_$@ and AFLAGS_$@ only apply to commands in current
374 $(CFLAGS_$@) specifies per-file options for $(CC). The $@
375 part has a literal value which specifies the file that it is for.
377 CFLAGS_$@ has the higher priority than ccflags-remove-y; CFLAGS_$@
378 can re-add compiler flags that were removed by ccflags-remove-y.
382 # drivers/scsi/Makefile
383 CFLAGS_aha152x.o = -DAHA152X_STAT -DAUTOCONF
385 This line specify compilation flags for aha152x.o.
387 $(AFLAGS_$@) is a similar feature for source files in assembly
390 AFLAGS_$@ has the higher priority than asflags-remove-y; AFLAGS_$@
391 can re-add assembler flags that were removed by asflags-remove-y.
395 # arch/arm/kernel/Makefile
396 AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
397 AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312
398 AFLAGS_iwmmxt.o := -Wa,-mcpu=iwmmxt
403 Kbuild tracks dependencies on the following:
405 1) All prerequisite files (both ``*.c`` and ``*.h``)
406 2) ``CONFIG_`` options used in all prerequisite files
407 3) Command-line used to compile target
409 Thus, if you change an option to $(CC) all affected files will
415 Custom rules are used when the kbuild infrastructure does
416 not provide the required support. A typical example is
417 header files generated during the build process.
418 Another example are the architecture-specific Makefiles which
419 need custom rules to prepare boot images etc.
421 Custom rules are written as normal Make rules.
422 Kbuild is not executing in the directory where the Makefile is
423 located, so all custom rules shall use a relative
424 path to prerequisite files and target files.
426 Two variables are used when defining custom rules:
429 $(src) is a relative path which points to the directory
430 where the Makefile is located. Always use $(src) when
431 referring to files located in the src tree.
434 $(obj) is a relative path which points to the directory
435 where the target is saved. Always use $(obj) when
436 referring to generated files.
440 #drivers/scsi/Makefile
441 $(obj)/53c8xx_d.h: $(src)/53c7,8xx.scr $(src)/script_asm.pl
442 $(CPP) -DCHIP=810 - < $< | ... $(src)/script_asm.pl
444 This is a custom rule, following the normal syntax
447 The target file depends on two prerequisite files. References
448 to the target file are prefixed with $(obj), references
449 to prerequisites are referenced with $(src) (because they are not
453 echoing information to user in a rule is often a good practice
454 but when execution ``make -s`` one does not expect to see any output
455 except for warnings/errors.
456 To support this kbuild defines $(kecho) which will echo out the
457 text following $(kecho) to stdout except if ``make -s`` is used.
462 $(BOOT_TARGETS): vmlinux
463 $(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@
464 @$(kecho) ' Kernel: $(boot)/$@ is ready'
466 When kbuild is executing with KBUILD_VERBOSE unset, then only a shorthand
467 of a command is normally displayed.
468 To enable this behaviour for custom commands kbuild requires
469 two variables to be set::
471 quiet_cmd_<command> - what shall be echoed
472 cmd_<command> - the command to execute
477 quiet_cmd_crc32 = GEN $@
480 $(obj)/crc32table.h: $(obj)/gen_crc32table
483 When updating the $(obj)/crc32table.h target, the line::
487 will be displayed with ``make KBUILD_VERBOSE=``.
489 Command change detection
490 ------------------------
492 When the rule is evaluated, timestamps are compared between the target
493 and its prerequisite files. GNU Make updates the target when any of the
494 prerequisites is newer than that.
496 The target should be rebuilt also when the command line has changed
497 since the last invocation. This is not supported by Make itself, so
498 Kbuild achieves this by a kind of meta-programming.
500 if_changed is the macro used for this purpose, in the following form::
502 quiet_cmd_<command> = ...
505 <target>: <source(s)> FORCE
506 $(call if_changed,<command>)
508 Any target that utilizes if_changed must be listed in $(targets),
509 otherwise the command line check will fail, and the target will
512 If the target is already listed in the recognized syntax such as
513 obj-y/m, lib-y/m, extra-y/m, always-y/m, hostprogs, userprogs, Kbuild
514 automatically adds it to $(targets). Otherwise, the target must be
515 explicitly added to $(targets).
517 Assignments to $(targets) are without $(obj)/ prefix. if_changed may be
518 used in conjunction with custom rules as defined in `Custom Rules`_.
520 Note: It is a typical mistake to forget the FORCE prerequisite.
521 Another common pitfall is that whitespace is sometimes significant; for
522 instance, the below will fail (note the extra space after the comma)::
524 target: source(s) FORCE
526 **WRONG!** $(call if_changed, objcopy)
529 if_changed should not be used more than once per target.
530 It stores the executed command in a corresponding .cmd
531 file and multiple calls would result in overwrites and
532 unwanted results when the target is up to date and only the
533 tests on changed commands trigger execution of commands.
535 $(CC) support functions
536 -----------------------
538 The kernel may be built with several different versions of
539 $(CC), each supporting a unique set of features and options.
540 kbuild provides basic support to check for valid options for $(CC).
541 $(CC) is usually the gcc compiler, but other alternatives are
545 as-option is used to check if $(CC) -- when used to compile
546 assembler (``*.S``) files -- supports the given option. An optional
547 second option may be specified if the first option is not supported.
552 cflags-y += $(call as-option,-Wa$(comma)-isa=$(isa-y),)
554 In the above example, cflags-y will be assigned the option
555 -Wa$(comma)-isa=$(isa-y) if it is supported by $(CC).
556 The second argument is optional, and if supplied will be used
557 if first argument is not supported.
560 as-instr checks if the assembler reports a specific instruction
561 and then outputs either option1 or option2
562 C escapes are supported in the test instruction
563 Note: as-instr-option uses KBUILD_AFLAGS for assembler options
566 cc-option is used to check if $(CC) supports a given option, and if
567 not supported to use an optional second option.
572 cflags-y += $(call cc-option,-march=pentium-mmx,-march=i586)
574 In the above example, cflags-y will be assigned the option
575 -march=pentium-mmx if supported by $(CC), otherwise -march=i586.
576 The second argument to cc-option is optional, and if omitted,
577 cflags-y will be assigned no value if first option is not supported.
578 Note: cc-option uses KBUILD_CFLAGS for $(CC) options
581 cc-option-yn is used to check if gcc supports a given option
582 and return "y" if supported, otherwise "n".
587 biarch := $(call cc-option-yn, -m32)
588 aflags-$(biarch) += -a32
589 cflags-$(biarch) += -m32
591 In the above example, $(biarch) is set to y if $(CC) supports the -m32
592 option. When $(biarch) equals "y", the expanded variables $(aflags-y)
593 and $(cflags-y) will be assigned the values -a32 and -m32,
596 Note: cc-option-yn uses KBUILD_CFLAGS for $(CC) options
599 cc-disable-warning checks if gcc supports a given warning and returns
600 the commandline switch to disable it. This special function is needed,
601 because gcc 4.4 and later accept any unknown -Wno-* option and only
602 warn about it if there is another warning in the source file.
606 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
608 In the above example, -Wno-unused-but-set-variable will be added to
609 KBUILD_CFLAGS only if gcc really accepts it.
612 gcc-min-version tests if the value of $(CONFIG_GCC_VERSION) is greater than
613 or equal to the provided value and evaluates to y if so.
617 cflags-$(call gcc-min-version, 70100) := -foo
619 In this example, cflags-y will be assigned the value -foo if $(CC) is gcc and
620 $(CONFIG_GCC_VERSION) is >= 7.1.
623 clang-min-version tests if the value of $(CONFIG_CLANG_VERSION) is greater
624 than or equal to the provided value and evaluates to y if so.
628 cflags-$(call clang-min-version, 110000) := -foo
630 In this example, cflags-y will be assigned the value -foo if $(CC) is clang
631 and $(CONFIG_CLANG_VERSION) is >= 11.0.0.
634 cc-cross-prefix is used to check if there exists a $(CC) in path with
635 one of the listed prefixes. The first prefix where there exist a
636 prefix$(CC) in the PATH is returned - and if no prefix$(CC) is found
637 then nothing is returned.
639 Additional prefixes are separated by a single space in the
640 call of cc-cross-prefix.
642 This functionality is useful for architecture Makefiles that try
643 to set CROSS_COMPILE to well-known values but may have several
644 values to select between.
646 It is recommended only to try to set CROSS_COMPILE if it is a cross
647 build (host arch is different from target arch). And if CROSS_COMPILE
648 is already set then leave it with the old value.
653 ifneq ($(SUBARCH),$(ARCH))
654 ifeq ($(CROSS_COMPILE),)
655 CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu-)
659 $(LD) support functions
660 -----------------------
663 ld-option is used to check if $(LD) supports the supplied option.
664 ld-option takes two options as arguments.
666 The second argument is an optional option that can be used if the
667 first option is not supported by $(LD).
672 LDFLAGS_vmlinux += $(call ld-option, -X)
677 Make rules may invoke scripts to build the kernel. The rules shall
678 always provide the appropriate interpreter to execute the script. They
679 shall not rely on the execute bits being set, and shall not invoke the
680 script directly. For the convenience of manual script invocation, such
681 as invoking ./scripts/checkpatch.pl, it is recommended to set execute
682 bits on the scripts nonetheless.
684 Kbuild provides variables $(CONFIG_SHELL), $(AWK), $(PERL),
685 and $(PYTHON3) to refer to interpreters for the respective
691 cmd_depmod = $(CONFIG_SHELL) $(srctree)/scripts/depmod.sh $(DEPMOD) \
697 Kbuild supports building executables on the host for use during the
700 Two steps are required in order to use a host executable.
702 The first step is to tell kbuild that a host program exists. This is
703 done utilising the variable ``hostprogs``.
705 The second step is to add an explicit dependency to the executable.
706 This can be done in two ways. Either add the dependency in a rule,
707 or utilise the variable ``always-y``.
708 Both possibilities are described in the following.
713 In some cases there is a need to compile and run a program on the
714 computer where the build is running.
716 The following line tells kbuild that the program bin2hex shall be
717 built on the build host.
723 Kbuild assumes in the above example that bin2hex is made from a single
724 c-source file named bin2hex.c located in the same directory as
727 Composite Host Programs
728 -----------------------
730 Host programs can be made up based on composite objects.
731 The syntax used to define composite objects for host programs is
732 similar to the syntax used for kernel objects.
733 $(<executable>-objs) lists all objects used to link the final
738 #scripts/lxdialog/Makefile
739 hostprogs := lxdialog
740 lxdialog-objs := checklist.o lxdialog.o
742 Objects with extension .o are compiled from the corresponding .c
743 files. In the above example, checklist.c is compiled to checklist.o
744 and lxdialog.c is compiled to lxdialog.o.
746 Finally, the two .o files are linked to the executable, lxdialog.
747 Note: The syntax <executable>-y is not permitted for host-programs.
749 Using C++ for host programs
750 ---------------------------
752 kbuild offers support for host programs written in C++. This was
753 introduced solely to support kconfig, and is not recommended
758 #scripts/kconfig/Makefile
760 qconf-cxxobjs := qconf.o
762 In the example above the executable is composed of the C++ file
763 qconf.cc - identified by $(qconf-cxxobjs).
765 If qconf is composed of a mixture of .c and .cc files, then an
766 additional line can be used to identify this.
770 #scripts/kconfig/Makefile
772 qconf-cxxobjs := qconf.o
773 qconf-objs := check.o
775 Using Rust for host programs
776 ----------------------------
778 Kbuild offers support for host programs written in Rust. However,
779 since a Rust toolchain is not mandatory for kernel compilation,
780 it may only be used in scenarios where Rust is required to be
781 available (e.g. when ``CONFIG_RUST`` is enabled).
788 Kbuild will compile ``target`` using ``target.rs`` as the crate root,
789 located in the same directory as the ``Makefile``. The crate may
790 consist of several source files (see ``samples/rust/hostprogs``).
792 Controlling compiler options for host programs
793 ----------------------------------------------
795 When compiling host programs, it is possible to set specific flags.
796 The programs will always be compiled utilising $(HOSTCC) passed
797 the options specified in $(KBUILD_HOSTCFLAGS).
799 To set flags that will take effect for all host programs created
800 in that Makefile, use the variable HOST_EXTRACFLAGS.
804 #scripts/lxdialog/Makefile
805 HOST_EXTRACFLAGS += -I/usr/include/ncurses
807 To set specific flags for a single file the following construction
812 #arch/ppc64/boot/Makefile
813 HOSTCFLAGS_piggyback.o := -DKERNELBASE=$(KERNELBASE)
815 It is also possible to specify additional options to the linker.
819 #scripts/kconfig/Makefile
820 HOSTLDLIBS_qconf := -L$(QTDIR)/lib
822 When linking qconf, it will be passed the extra option
825 When host programs are actually built
826 -------------------------------------
828 Kbuild will only build host-programs when they are referenced
831 This is possible in two ways:
833 (1) List the prerequisite explicitly in a custom rule.
837 #drivers/pci/Makefile
838 hostprogs := gen-devlist
839 $(obj)/devlist.h: $(src)/pci.ids $(obj)/gen-devlist
840 ( cd $(obj); ./gen-devlist ) < $<
842 The target $(obj)/devlist.h will not be built before
843 $(obj)/gen-devlist is updated. Note that references to
844 the host programs in custom rules must be prefixed with $(obj).
848 When there is no suitable custom rule, and the host program
849 shall be built when a makefile is entered, the always-y
850 variable shall be used.
854 #scripts/lxdialog/Makefile
855 hostprogs := lxdialog
856 always-y := $(hostprogs)
858 Kbuild provides the following shorthand for this::
860 hostprogs-always-y := lxdialog
862 This will tell kbuild to build lxdialog even if not referenced in
865 Userspace Program support
866 =========================
868 Just like host programs, Kbuild also supports building userspace executables
869 for the target architecture (i.e. the same architecture as you are building
872 The syntax is quite similar. The difference is to use ``userprogs`` instead of
875 Simple Userspace Program
876 ------------------------
878 The following line tells kbuild that the program bpf-direct shall be
879 built for the target architecture.
883 userprogs := bpf-direct
885 Kbuild assumes in the above example that bpf-direct is made from a
886 single C source file named bpf-direct.c located in the same directory
889 Composite Userspace Programs
890 ----------------------------
892 Userspace programs can be made up based on composite objects.
893 The syntax used to define composite objects for userspace programs is
894 similar to the syntax used for kernel objects.
895 $(<executable>-objs) lists all objects used to link the final
900 #samples/seccomp/Makefile
901 userprogs := bpf-fancy
902 bpf-fancy-objs := bpf-fancy.o bpf-helper.o
904 Objects with extension .o are compiled from the corresponding .c
905 files. In the above example, bpf-fancy.c is compiled to bpf-fancy.o
906 and bpf-helper.c is compiled to bpf-helper.o.
908 Finally, the two .o files are linked to the executable, bpf-fancy.
909 Note: The syntax <executable>-y is not permitted for userspace programs.
911 Controlling compiler options for userspace programs
912 ---------------------------------------------------
914 When compiling userspace programs, it is possible to set specific flags.
915 The programs will always be compiled utilising $(CC) passed
916 the options specified in $(KBUILD_USERCFLAGS).
918 To set flags that will take effect for all userspace programs created
919 in that Makefile, use the variable userccflags.
923 # samples/seccomp/Makefile
924 userccflags += -I usr/include
926 To set specific flags for a single file the following construction
931 bpf-helper-userccflags += -I user/include
933 It is also possible to specify additional options to the linker.
937 # net/bpfilter/Makefile
938 bpfilter_umh-userldflags += -static
940 When linking bpfilter_umh, it will be passed the extra option -static.
942 From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
944 When userspace programs are actually built
945 ------------------------------------------
947 Kbuild builds userspace programs only when told to do so.
948 There are two ways to do this.
950 (1) Add it as the prerequisite of another file
954 #net/bpfilter/Makefile
955 userprogs := bpfilter_umh
956 $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh
958 $(obj)/bpfilter_umh is built before $(obj)/bpfilter_umh_blob.o
964 userprogs := binderfs_example
965 always-y := $(userprogs)
967 Kbuild provides the following shorthand for this::
969 userprogs-always-y := binderfs_example
971 This will tell Kbuild to build binderfs_example when it visits this
974 Kbuild clean infrastructure
975 ===========================
977 ``make clean`` deletes most generated files in the obj tree where the kernel
978 is compiled. This includes generated files such as host programs.
979 Kbuild knows targets listed in $(hostprogs), $(always-y), $(always-m),
980 $(always-), $(extra-y), $(extra-) and $(targets). They are all deleted
981 during ``make clean``. Files matching the patterns ``*.[oas]``, ``*.ko``, plus
982 some additional files generated by kbuild are deleted all over the kernel
983 source tree when ``make clean`` is executed.
985 Additional files or directories can be specified in kbuild makefiles by use of
991 clean-files := crc32table.h
993 When executing ``make clean``, the file ``crc32table.h`` will be deleted.
994 Kbuild will assume files to be in the same relative directory as the
997 To exclude certain files or directories from make clean, use the
998 $(no-clean-files) variable.
1000 Usually kbuild descends down in subdirectories due to ``obj-* := dir/``,
1001 but in the architecture makefiles where the kbuild infrastructure
1002 is not sufficient this sometimes needs to be explicit.
1006 #arch/x86/boot/Makefile
1007 subdir- := compressed
1009 The above assignment instructs kbuild to descend down in the
1010 directory compressed/ when ``make clean`` is executed.
1012 Note 1: arch/$(SRCARCH)/Makefile cannot use ``subdir-``, because that file is
1013 included in the top level makefile. Instead, arch/$(SRCARCH)/Kbuild can use
1016 Note 2: All directories listed in core-y, libs-y, drivers-y and net-y will
1017 be visited during ``make clean``.
1019 Architecture Makefiles
1020 ======================
1022 The top level Makefile sets up the environment and does the preparation,
1023 before starting to descend down in the individual directories.
1025 The top level makefile contains the generic part, whereas
1026 arch/$(SRCARCH)/Makefile contains what is required to set up kbuild
1027 for said architecture.
1029 To do so, arch/$(SRCARCH)/Makefile sets up a number of variables and defines
1032 When kbuild executes, the following steps are followed (roughly):
1034 1) Configuration of the kernel => produce .config
1036 2) Store kernel version in include/linux/version.h
1038 3) Updating all other prerequisites to the target prepare:
1040 - Additional prerequisites are specified in arch/$(SRCARCH)/Makefile
1042 4) Recursively descend down in all directories listed in
1043 init-* core* drivers-* net-* libs-* and build all targets.
1045 - The values of the above variables are expanded in arch/$(SRCARCH)/Makefile.
1047 5) All object files are then linked and the resulting file vmlinux is
1048 located at the root of the obj tree.
1049 The very first objects linked are listed in scripts/head-object-list.txt.
1051 6) Finally, the architecture-specific part does any required post processing
1052 and builds the final bootimage.
1054 - This includes building boot records
1055 - Preparing initrd images and the like
1057 Set variables to tweak the build to the architecture
1058 ----------------------------------------------------
1061 Generic $(LD) options
1063 Flags used for all invocations of the linker.
1064 Often specifying the emulation is sufficient.
1069 KBUILD_LDFLAGS := -m elf_s390
1071 Note: ldflags-y can be used to further customise
1072 the flags used. See `Non-builtin vmlinux targets - extra-y`_.
1075 Options for $(LD) when linking vmlinux
1077 LDFLAGS_vmlinux is used to specify additional flags to pass to
1078 the linker when linking the final vmlinux image.
1080 LDFLAGS_vmlinux uses the LDFLAGS_$@ support.
1085 LDFLAGS_vmlinux := -e stext
1090 When $(call if_changed,objcopy) is used to translate a .o file,
1091 the flags specified in OBJCOPYFLAGS will be used.
1093 $(call if_changed,objcopy) is often used to generate raw binaries on
1099 OBJCOPYFLAGS := -O binary
1101 #arch/s390/boot/Makefile
1102 $(obj)/image: vmlinux FORCE
1103 $(call if_changed,objcopy)
1105 In this example, the binary $(obj)/image is a binary version of
1106 vmlinux. The usage of $(call if_changed,xxx) will be described later.
1111 Default value - see top level Makefile.
1113 Append or modify as required per architecture.
1117 #arch/sparc64/Makefile
1118 KBUILD_AFLAGS += -m64 -mcpu=ultrasparc
1121 $(CC) compiler flags
1123 Default value - see top level Makefile.
1125 Append or modify as required per architecture.
1127 Often, the KBUILD_CFLAGS variable depends on the configuration.
1131 #arch/x86/boot/compressed/Makefile
1132 cflags-$(CONFIG_X86_32) := -march=i386
1133 cflags-$(CONFIG_X86_64) := -mcmodel=small
1134 KBUILD_CFLAGS += $(cflags-y)
1136 Many arch Makefiles dynamically run the target C compiler to
1137 probe supported options::
1142 cflags-$(CONFIG_MPENTIUMII) += $(call cc-option,\
1143 -march=pentium2,-march=i686)
1145 # Disable unit-at-a-time mode ...
1146 KBUILD_CFLAGS += $(call cc-option,-fno-unit-at-a-time)
1150 The first example utilises the trick that a config option expands
1151 to "y" when selected.
1154 $(RUSTC) compiler flags
1156 Default value - see top level Makefile.
1158 Append or modify as required per architecture.
1160 Often, the KBUILD_RUSTFLAGS variable depends on the configuration.
1162 Note that target specification file generation (for ``--target``)
1163 is handled in ``scripts/generate_rust_target.rs``.
1165 KBUILD_AFLAGS_KERNEL
1166 Assembler options specific for built-in
1168 $(KBUILD_AFLAGS_KERNEL) contains extra C compiler flags used to compile
1169 resident kernel code.
1171 KBUILD_AFLAGS_MODULE
1172 Assembler options specific for modules
1174 $(KBUILD_AFLAGS_MODULE) is used to add arch-specific options that
1175 are used for assembler.
1177 From commandline AFLAGS_MODULE shall be used (see kbuild.rst).
1179 KBUILD_CFLAGS_KERNEL
1180 $(CC) options specific for built-in
1182 $(KBUILD_CFLAGS_KERNEL) contains extra C compiler flags used to compile
1183 resident kernel code.
1185 KBUILD_CFLAGS_MODULE
1186 Options for $(CC) when building modules
1188 $(KBUILD_CFLAGS_MODULE) is used to add arch-specific options that
1191 From commandline CFLAGS_MODULE shall be used (see kbuild.rst).
1193 KBUILD_RUSTFLAGS_KERNEL
1194 $(RUSTC) options specific for built-in
1196 $(KBUILD_RUSTFLAGS_KERNEL) contains extra Rust compiler flags used to
1197 compile resident kernel code.
1199 KBUILD_RUSTFLAGS_MODULE
1200 Options for $(RUSTC) when building modules
1202 $(KBUILD_RUSTFLAGS_MODULE) is used to add arch-specific options that
1203 are used for $(RUSTC).
1205 From commandline RUSTFLAGS_MODULE shall be used (see kbuild.rst).
1207 KBUILD_LDFLAGS_MODULE
1208 Options for $(LD) when linking modules
1210 $(KBUILD_LDFLAGS_MODULE) is used to add arch-specific options
1211 used when linking modules. This is often a linker script.
1213 From commandline LDFLAGS_MODULE shall be used (see kbuild.rst).
1216 The linker script with full path. Assigned by the top-level Makefile.
1219 All object files for vmlinux. They are linked to vmlinux in the same
1220 order as listed in KBUILD_VMLINUX_OBJS.
1222 The objects listed in scripts/head-object-list.txt are exceptions;
1223 they are placed before the other objects.
1226 All .a ``lib`` files for vmlinux. KBUILD_VMLINUX_OBJS and
1227 KBUILD_VMLINUX_LIBS together specify all the object files used to
1230 Add prerequisites to archheaders
1231 --------------------------------
1233 The archheaders: rule is used to generate header files that
1234 may be installed into user space by ``make header_install``.
1236 It is run before ``make archprepare`` when run on the
1237 architecture itself.
1239 Add prerequisites to archprepare
1240 --------------------------------
1242 The archprepare: rule is used to list prerequisites that need to be
1243 built before starting to descend down in the subdirectories.
1245 This is usually used for header files containing assembler constants.
1250 archprepare: maketools
1252 In this example, the file target maketools will be processed
1253 before descending down in the subdirectories.
1255 See also chapter XXX-TODO that describes how kbuild supports
1256 generating offset header files.
1258 List directories to visit when descending
1259 -----------------------------------------
1261 An arch Makefile cooperates with the top Makefile to define variables
1262 which specify how to build the vmlinux file. Note that there is no
1263 corresponding arch-specific section for modules; the module-building
1264 machinery is all architecture-independent.
1266 core-y, libs-y, drivers-y
1267 $(libs-y) lists directories where a lib.a archive can be located.
1269 The rest list directories where a built-in.a object file can be
1272 Then the rest follows in this order:
1274 $(core-y), $(libs-y), $(drivers-y)
1276 The top level Makefile defines values for all generic directories,
1277 and arch/$(SRCARCH)/Makefile only adds architecture-specific
1282 # arch/sparc/Makefile
1283 core-y += arch/sparc/
1285 libs-y += arch/sparc/prom/
1286 libs-y += arch/sparc/lib/
1288 drivers-$(CONFIG_PM) += arch/sparc/power/
1290 Architecture-specific boot images
1291 ---------------------------------
1293 An arch Makefile specifies goals that take the vmlinux file, compress
1294 it, wrap it in bootstrapping code, and copy the resulting files
1295 somewhere. This includes various kinds of installation commands.
1296 The actual goals are not standardized across architectures.
1298 It is common to locate any additional processing in a boot/
1299 directory below arch/$(SRCARCH)/.
1301 Kbuild does not provide any smart way to support building a
1302 target specified in boot/. Therefore arch/$(SRCARCH)/Makefile shall
1303 call make manually to build a target in boot/.
1305 The recommended approach is to include shortcuts in
1306 arch/$(SRCARCH)/Makefile, and use the full path when calling down
1307 into the arch/$(SRCARCH)/boot/Makefile.
1312 boot := arch/x86/boot
1314 $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
1316 ``$(Q)$(MAKE) $(build)=<dir>`` is the recommended way to invoke
1317 make in a subdirectory.
1319 There are no rules for naming architecture-specific targets,
1320 but executing ``make help`` will list all relevant targets.
1321 To support this, $(archhelp) must be defined.
1327 echo '* bzImage - Compressed kernel image (arch/x86/boot/bzImage)'
1330 When make is executed without arguments, the first goal encountered
1331 will be built. In the top level Makefile the first goal present
1334 An architecture shall always, per default, build a bootable image.
1335 In ``make help``, the default goal is highlighted with a ``*``.
1337 Add a new prerequisite to all: to select a default goal different
1345 When ``make`` is executed without arguments, bzImage will be built.
1347 Commands useful for building a boot image
1348 -----------------------------------------
1350 Kbuild provides a few macros that are useful when building a
1354 Link target. Often, LDFLAGS_$@ is used to set specific options to ld.
1358 #arch/x86/boot/Makefile
1359 LDFLAGS_bootsect := -Ttext 0x0 -s --oformat binary
1360 LDFLAGS_setup := -Ttext 0x0 -s --oformat binary -e begtext
1362 targets += setup setup.o bootsect bootsect.o
1363 $(obj)/setup $(obj)/bootsect: %: %.o FORCE
1364 $(call if_changed,ld)
1366 In this example, there are two possible targets, requiring different
1367 options to the linker. The linker options are specified using the
1368 LDFLAGS_$@ syntax - one for each potential target.
1370 $(targets) are assigned all potential targets, by which kbuild knows
1371 the targets and will:
1373 1) check for commandline changes
1374 2) delete target during make clean
1376 The ``: %: %.o`` part of the prerequisite is a shorthand that
1377 frees us from listing the setup.o and bootsect.o files.
1380 It is a common mistake to forget the ``targets :=`` assignment,
1381 resulting in the target file being recompiled for no
1385 Copy binary. Uses OBJCOPYFLAGS usually specified in
1386 arch/$(SRCARCH)/Makefile.
1388 OBJCOPYFLAGS_$@ may be used to set additional options.
1391 Compress target. Use maximum compression to compress target.
1395 #arch/x86/boot/compressed/Makefile
1396 $(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE
1397 $(call if_changed,gzip)
1400 Create flattened device tree blob object suitable for linking
1401 into vmlinux. Device tree blobs linked into vmlinux are placed
1402 in an init section in the image. Platform code *must* copy the
1403 blob to non-init memory prior to calling unflatten_device_tree().
1405 To use this command, simply add ``*.dtb`` into obj-y or targets, or make
1406 some other target depend on ``%.dtb``
1408 A central rule exists to create ``$(obj)/%.dtb`` from ``$(src)/%.dts``;
1409 architecture Makefiles do no need to explicitly write out that rule.
1414 DTC_FLAGS ?= -p 1024
1416 Preprocessing linker scripts
1417 ----------------------------
1419 When the vmlinux image is built, the linker script
1420 arch/$(SRCARCH)/kernel/vmlinux.lds is used.
1422 The script is a preprocessed variant of the file vmlinux.lds.S
1423 located in the same directory.
1425 kbuild knows .lds files and includes a rule ``*lds.S`` -> ``*lds``.
1429 #arch/x86/kernel/Makefile
1430 extra-y := vmlinux.lds
1432 The assignment to extra-y is used to tell kbuild to build the
1435 The assignment to $(CPPFLAGS_vmlinux.lds) tells kbuild to use the
1436 specified options when building the target vmlinux.lds.
1438 When building the ``*.lds`` target, kbuild uses the variables::
1440 KBUILD_CPPFLAGS : Set in top-level Makefile
1441 cppflags-y : May be set in the kbuild makefile
1442 CPPFLAGS_$(@F) : Target-specific flags.
1443 Note that the full filename is used in this
1446 The kbuild infrastructure for ``*lds`` files is used in several
1447 architecture-specific files.
1449 Generic header files
1450 --------------------
1452 The directory include/asm-generic contains the header files
1453 that may be shared between individual architectures.
1455 The recommended approach how to use a generic header file is
1456 to list the file in the Kbuild file.
1458 See `generic-y`_ for further info on syntax etc.
1463 If the file arch/xxx/Makefile.postlink exists, this makefile
1464 will be invoked for post-link objects (vmlinux and modules.ko)
1465 for architectures to run post-link passes on. Must also handle
1468 This pass runs after kallsyms generation. If the architecture
1469 needs to modify symbol locations, rather than manipulate the
1470 kallsyms, it may be easier to add another postlink target for
1471 .tmp_vmlinux? targets to be called from link-vmlinux.sh.
1473 For example, powerpc uses this to check relocation sanity of
1474 the linked vmlinux file.
1476 Kbuild syntax for exported headers
1477 ==================================
1479 The kernel includes a set of headers that is exported to userspace.
1480 Many headers can be exported as-is but other headers require a
1481 minimal pre-processing before they are ready for user-space.
1483 The pre-processing does:
1485 - drop kernel-specific annotations
1486 - drop include of compiler.h
1487 - drop all sections that are kernel internal (guarded by ``ifdef __KERNEL__``)
1489 All headers under include/uapi/, include/generated/uapi/,
1490 arch/<arch>/include/uapi/ and arch/<arch>/include/generated/uapi/
1493 A Kbuild file may be defined under arch/<arch>/include/uapi/asm/ and
1494 arch/<arch>/include/asm/ to list asm files coming from asm-generic.
1496 See subsequent chapter for the syntax of the Kbuild file.
1501 no-export-headers is essentially used by include/uapi/linux/Kbuild to
1502 avoid exporting specific headers (e.g. kvm.h) on architectures that do
1503 not support it. It should be avoided as much as possible.
1508 If an architecture uses a verbatim copy of a header from
1509 include/asm-generic then this is listed in the file
1510 arch/$(SRCARCH)/include/asm/Kbuild like this:
1514 #arch/x86/include/asm/Kbuild
1515 generic-y += termios.h
1518 During the prepare phase of the build a wrapper include
1519 file is generated in the directory::
1521 arch/$(SRCARCH)/include/generated/asm
1523 When a header is exported where the architecture uses
1524 the generic header a similar wrapper is generated as part
1525 of the set of exported headers in the directory::
1529 The generated wrapper will in both cases look like the following:
1531 Example: termios.h::
1533 #include <asm-generic/termios.h>
1538 If an architecture generates other header files alongside generic-y
1539 wrappers, generated-y specifies them.
1541 This prevents them being treated as stale asm-generic wrappers and
1546 #arch/x86/include/asm/Kbuild
1547 generated-y += syscalls_32.h
1552 mandatory-y is essentially used by include/(uapi/)asm-generic/Kbuild
1553 to define the minimum set of ASM headers that all architectures must have.
1555 This works like optional generic-y. If a mandatory header is missing
1556 in arch/$(SRCARCH)/include/(uapi/)/asm, Kbuild will automatically
1557 generate a wrapper of the asm-generic one.
1562 The top Makefile exports the following variables:
1564 VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
1565 These variables define the current kernel version. A few arch
1566 Makefiles actually use these values directly; they should use
1567 $(KERNELRELEASE) instead.
1569 $(VERSION), $(PATCHLEVEL), and $(SUBLEVEL) define the basic
1570 three-part version number, such as "2", "4", and "0". These three
1571 values are always numeric.
1573 $(EXTRAVERSION) defines an even tinier sublevel for pre-patches
1574 or additional patches. It is usually some non-numeric string
1575 such as "-pre4", and is often blank.
1578 $(KERNELRELEASE) is a single string such as "2.4.0-pre4", suitable
1579 for constructing installation directory names or showing in
1580 version strings. Some arch Makefiles use it for this purpose.
1583 This variable defines the target architecture, such as "i386",
1584 "arm", or "sparc". Some kbuild Makefiles test $(ARCH) to
1585 determine which files to compile.
1587 By default, the top Makefile sets $(ARCH) to be the same as the
1588 host system architecture. For a cross build, a user may
1589 override the value of $(ARCH) on the command line::
1594 This variable specifies the directory in arch/ to build.
1596 ARCH and SRCARCH may not necessarily match. A couple of arch
1597 directories are biarch, that is, a single ``arch/*/`` directory supports
1598 both 32-bit and 64-bit.
1600 For example, you can pass in ARCH=i386, ARCH=x86_64, or ARCH=x86.
1601 For all of them, SRCARCH=x86 because arch/x86/ supports both i386 and
1605 This variable defines a place for the arch Makefiles to install
1606 the resident kernel image and System.map file.
1607 Use this for architecture-specific install targets.
1609 INSTALL_MOD_PATH, MODLIB
1610 $(INSTALL_MOD_PATH) specifies a prefix to $(MODLIB) for module
1611 installation. This variable is not defined in the Makefile but
1612 may be passed in by the user if desired.
1614 $(MODLIB) specifies the directory for module installation.
1615 The top Makefile defines $(MODLIB) to
1616 $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE). The user may
1617 override this value on the command line if desired.
1620 If this variable is specified, it will cause modules to be stripped
1621 after they are installed. If INSTALL_MOD_STRIP is "1", then the
1622 default option --strip-debug will be used. Otherwise, the
1623 INSTALL_MOD_STRIP value will be used as the option(s) to the strip
1629 The kernel Makefiles are designed to be run with GNU Make. The Makefiles
1630 use only the documented features of GNU Make, but they do use many
1633 GNU Make supports elementary list-processing functions. The kernel
1634 Makefiles use a novel style of list building and manipulation with few
1637 GNU Make has two assignment operators, ``:=`` and ``=``. ``:=`` performs
1638 immediate evaluation of the right-hand side and stores an actual string
1639 into the left-hand side. ``=`` is like a formula definition; it stores the
1640 right-hand side in an unevaluated form and then evaluates this form each
1641 time the left-hand side is used.
1643 There are some cases where ``=`` is appropriate. Usually, though, ``:=``
1644 is the right choice.
1649 - Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net>
1650 - Updates by Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
1651 - Updates by Sam Ravnborg <sam@ravnborg.org>
1652 - Language QA by Jan Engelhardt <jengelh@gmx.de>
1657 - Describe how kbuild supports shipped files with _shipped.
1658 - Generating offset header files.
1659 - Add more variables to chapters 7 or 9?