Skip to content
makefiles.txt 46.6 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
	Kbuild does not provide any smart way to support building a
	target specified in boot/. Therefore arch/$(ARCH)/Makefile shall
	call make manually to build a target in boot/.

	The recommended approach is to include shortcuts in
	arch/$(ARCH)/Makefile, and use the full path when calling down
	into the arch/$(ARCH)/boot/Makefile.

	Example:
		#arch/x86/Makefile
		boot := arch/x86/boot
Linus Torvalds's avatar
Linus Torvalds committed
		bzImage: vmlinux
			$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@

	"$(Q)$(MAKE) $(build)=<dir>" is the recommended way to invoke
	make in a subdirectory.

Randy Dunlap's avatar
Randy Dunlap committed
	There are no rules for naming architecture-specific targets,
Linus Torvalds's avatar
Linus Torvalds committed
	but executing "make help" will list all relevant targets.
	To support this, $(archhelp) must be defined.
Linus Torvalds's avatar
Linus Torvalds committed

	Example:
		#arch/x86/Makefile
Linus Torvalds's avatar
Linus Torvalds committed
		define archhelp
		  echo  '* bzImage      - Image (arch/$(ARCH)/boot/bzImage)'
Linus Torvalds's avatar
Linus Torvalds committed

	When make is executed without arguments, the first goal encountered
	will be built. In the top level Makefile the first goal present
	is all:.
	An architecture shall always, per default, build a bootable image.
	In "make help", the default goal is highlighted with a '*'.
Linus Torvalds's avatar
Linus Torvalds committed
	Add a new prerequisite to all: to select a default goal different
	from vmlinux.

	Example:
		#arch/x86/Makefile
Linus Torvalds's avatar
Linus Torvalds committed

	When "make" is executed without arguments, bzImage will be built.

--- 6.6 Building non-kbuild targets
	extra-y specifies additional targets created in the current
Linus Torvalds's avatar
Linus Torvalds committed
	directory, in addition to any targets specified by obj-*.

	Listing all targets in extra-y is required for two purposes:
	1) Enable kbuild to check changes in command lines
	   - When $(call if_changed,xxx) is used
	2) kbuild knows what files to delete during "make clean"

	Example:
		#arch/x86/kernel/Makefile
Linus Torvalds's avatar
Linus Torvalds committed
		extra-y := head.o init_task.o

	In this example, extra-y is used to list object files that
	shall be built, but shall not be linked as part of built-in.a.
Linus Torvalds's avatar
Linus Torvalds committed

--- 6.7 Commands useful for building a boot image
Linus Torvalds's avatar
Linus Torvalds committed

	Kbuild provides a few macros that are useful when building a
	boot image.

    if_changed

	if_changed is the infrastructure used for the following commands.

	Usage:
		target: source(s) FORCE
			$(call if_changed,ld/objcopy/gzip/...)
Linus Torvalds's avatar
Linus Torvalds committed

	When the rule is evaluated, it is checked to see if any files
Randy Dunlap's avatar
Randy Dunlap committed
	need an update, or the command line has changed since the last
Linus Torvalds's avatar
Linus Torvalds committed
	invocation. The latter will force a rebuild if any options
	to the executable have changed.
	Any target that utilises if_changed must be listed in $(targets),
	otherwise the command line check will fail, and the target will
	always be built.
	Assignments to $(targets) are without $(obj)/ prefix.
	if_changed may be used in conjunction with custom commands as
	defined in 6.8 "Custom kbuild commands".
Linus Torvalds's avatar
Linus Torvalds committed
	Note: It is a typical mistake to forget the FORCE prerequisite.
	Another common pitfall is that whitespace is sometimes
	significant; for instance, the below will fail (note the extra space
	after the comma):
		target: source(s) FORCE
	#WRONG!#	$(call if_changed, ld/objcopy/gzip/...)
Linus Torvalds's avatar
Linus Torvalds committed

        Note: if_changed should not be used more than once per target.
              It stores the executed command in a corresponding .cmd
        file and multiple calls would result in overwrites and
        unwanted results when the target is up to date and only the
        tests on changed commands trigger execution of commands.

Linus Torvalds's avatar
Linus Torvalds committed
    ld
	Link target. Often, LDFLAGS_$@ is used to set specific options to ld.
Linus Torvalds's avatar
Linus Torvalds committed
	Example:
		#arch/x86/boot/Makefile
Linus Torvalds's avatar
Linus Torvalds committed
		LDFLAGS_bootsect := -Ttext 0x0 -s --oformat binary
		LDFLAGS_setup    := -Ttext 0x0 -s --oformat binary -e begtext

		targets += setup setup.o bootsect bootsect.o
		$(obj)/setup $(obj)/bootsect: %: %.o FORCE
			$(call if_changed,ld)

	In this example, there are two possible targets, requiring different
	options to the linker. The linker options are specified using the
Linus Torvalds's avatar
Linus Torvalds committed
	LDFLAGS_$@ syntax - one for each potential target.
	$(targets) are assigned all potential targets, by which kbuild knows
Linus Torvalds's avatar
Linus Torvalds committed
	the targets and will:
		1) check for commandline changes
		2) delete target during make clean

	The ": %: %.o" part of the prerequisite is a shorthand that
	frees us from listing the setup.o and bootsect.o files.
	Note: It is a common mistake to forget the "targets :=" assignment,
Linus Torvalds's avatar
Linus Torvalds committed
	      resulting in the target file being recompiled for no
	      obvious reason.

    objcopy
	Copy binary. Uses OBJCOPYFLAGS usually specified in
	arch/$(ARCH)/Makefile.
	OBJCOPYFLAGS_$@ may be used to set additional options.

    gzip
	Compress target. Use maximum compression to compress target.

	Example:
		#arch/x86/boot/compressed/Makefile
		$(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE
			$(call if_changed,gzip)

	Create flattened device tree blob object suitable for linking
	into vmlinux. Device tree blobs linked into vmlinux are placed
	in an init section in the image. Platform code *must* copy the
	blob to non-init memory prior to calling unflatten_device_tree().

	To use this command, simply add *.dtb into obj-y or targets, or make
	some other target depend on %.dtb
	A central rule exists to create $(obj)/%.dtb from $(src)/%.dts;
	architecture Makefiles do no need to explicitly write out that rule.
	Example:
		targets += $(dtb-y)
		DTC_FLAGS ?= -p 1024
Linus Torvalds's avatar
Linus Torvalds committed

--- 6.8 Custom kbuild commands
Linus Torvalds's avatar
Linus Torvalds committed

	When kbuild is executing with KBUILD_VERBOSE=0, then only a shorthand
Linus Torvalds's avatar
Linus Torvalds committed
	of a command is normally displayed.
	To enable this behaviour for custom commands kbuild requires
	two variables to be set:
	quiet_cmd_<command>	- what shall be echoed
	      cmd_<command>	- the command to execute

	Example:
		#
		quiet_cmd_image = BUILD   $@
		      cmd_image = $(obj)/tools/build $(BUILDFLAGS) \
		                                     $(obj)/vmlinux.bin > $@

		targets += bzImage
		$(obj)/bzImage: $(obj)/vmlinux.bin $(obj)/tools/build FORCE
			$(call if_changed,image)
			@echo 'Kernel: $@ is ready'

	When updating the $(obj)/bzImage target, the line
Linus Torvalds's avatar
Linus Torvalds committed

	BUILD    arch/x86/boot/bzImage
Linus Torvalds's avatar
Linus Torvalds committed

	will be displayed with "make KBUILD_VERBOSE=0".
Linus Torvalds's avatar
Linus Torvalds committed

--- 6.9 Preprocessing linker scripts
Linus Torvalds's avatar
Linus Torvalds committed

	When the vmlinux image is built, the linker script
Linus Torvalds's avatar
Linus Torvalds committed
	arch/$(ARCH)/kernel/vmlinux.lds is used.
	The script is a preprocessed variant of the file vmlinux.lds.S
	located in the same directory.
	kbuild knows .lds files and includes a rule *lds.S -> *lds.
Linus Torvalds's avatar
Linus Torvalds committed
	Example:
		#arch/x86/kernel/Makefile
Linus Torvalds's avatar
Linus Torvalds committed
		always := vmlinux.lds
Linus Torvalds's avatar
Linus Torvalds committed
		#Makefile
		export CPPFLAGS_vmlinux.lds += -P -C -U$(ARCH)

	The assignment to $(always) is used to tell kbuild to build the
	target vmlinux.lds.
	The assignment to $(CPPFLAGS_vmlinux.lds) tells kbuild to use the
Linus Torvalds's avatar
Linus Torvalds committed
	specified options when building the target vmlinux.lds.
	When building the *.lds target, kbuild uses the variables:
	KBUILD_CPPFLAGS	: Set in top-level Makefile
	cppflags-y	: May be set in the kbuild makefile
	CPPFLAGS_$(@F)  : Target-specific flags.
Linus Torvalds's avatar
Linus Torvalds committed
	                  Note that the full filename is used in this
	                  assignment.

	The kbuild infrastructure for *lds files is used in several
Randy Dunlap's avatar
Randy Dunlap committed
	architecture-specific files.
Linus Torvalds's avatar
Linus Torvalds committed

--- 6.10 Generic header files

	The directory include/asm-generic contains the header files
	that may be shared between individual architectures.
	The recommended approach how to use a generic header file is
	to list the file in the Kbuild file.
	See "7.2 generic-y" for further info on syntax etc.
--- 6.11 Post-link pass

	If the file arch/xxx/Makefile.postlink exists, this makefile
	will be invoked for post-link objects (vmlinux and modules.ko)
	for architectures to run post-link passes on. Must also handle
	the clean target.

	This pass runs after kallsyms generation. If the architecture
	needs to modify symbol locations, rather than manipulate the
	kallsyms, it may be easier to add another postlink target for
	.tmp_vmlinux? targets to be called from link-vmlinux.sh.

	For example, powerpc uses this to check relocation sanity of
	the linked vmlinux file.

=== 7 Kbuild syntax for exported headers

The kernel includes a set of headers that is exported to userspace.
Many headers can be exported as-is but other headers require a
minimal pre-processing before they are ready for user-space.
The pre-processing does:
- drop kernel-specific annotations
- drop include of compiler.h
- drop all sections that are kernel internal (guarded by ifdef __KERNEL__)
All headers under include/uapi/, include/generated/uapi/,
arch/<arch>/include/uapi/ and arch/<arch>/include/generated/uapi/
A Kbuild file may be defined under arch/<arch>/include/uapi/asm/ and
arch/<arch>/include/asm/ to list asm files coming from asm-generic.
See subsequent chapter for the syntax of the Kbuild file.
--- 7.1 no-export-headers
	no-export-headers is essentially used by include/uapi/linux/Kbuild to
	avoid exporting specific headers (e.g. kvm.h) on architectures that do
	not support it. It should be avoided as much as possible.
--- 7.2 generic-y

	If an architecture uses a verbatim copy of a header from
	include/asm-generic then this is listed in the file
	arch/$(ARCH)/include/asm/Kbuild like this:

		Example:
			#arch/x86/include/asm/Kbuild
			generic-y += termios.h
			generic-y += rtc.h

	During the prepare phase of the build a wrapper include
	file is generated in the directory:

		arch/$(ARCH)/include/generated/asm

	When a header is exported where the architecture uses
	the generic header a similar wrapper is generated as part
	of the set of exported headers in the directory:

		usr/include/asm

	The generated wrapper will in both cases look like the following:

		Example: termios.h
			#include <asm-generic/termios.h>
--- 7.3 generated-y

	If an architecture generates other header files alongside generic-y
	wrappers, generated-y specifies them.

	This prevents them being treated as stale asm-generic wrappers and
	removed.

		Example:
			#arch/x86/include/asm/Kbuild
			generated-y += syscalls_32.h

--- 7.4 mandatory-y
	mandatory-y is essentially used by include/(uapi/)asm-generic/Kbuild.asm
	to define the minimum set of ASM headers that all architectures must have.

	This works like optional generic-y. If a mandatory header is missing
	in arch/$(ARCH)/include/(uapi/)/asm, Kbuild will automatically generate
	a wrapper of the asm-generic one.

	The convention is to list one subdir per line and
	preferably in alphabetic order.

=== 8 Kbuild Variables
Linus Torvalds's avatar
Linus Torvalds committed

The top Makefile exports the following variables:

    VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION

	These variables define the current kernel version.  A few arch
	Makefiles actually use these values directly; they should use
	$(KERNELRELEASE) instead.

	$(VERSION), $(PATCHLEVEL), and $(SUBLEVEL) define the basic
	three-part version number, such as "2", "4", and "0".  These three
	values are always numeric.

	$(EXTRAVERSION) defines an even tinier sublevel for pre-patches
	or additional patches.	It is usually some non-numeric string
	such as "-pre4", and is often blank.

    KERNELRELEASE

	$(KERNELRELEASE) is a single string such as "2.4.0-pre4", suitable
	for constructing installation directory names or showing in
	version strings.  Some arch Makefiles use it for this purpose.

    ARCH

	This variable defines the target architecture, such as "i386",
	"arm", or "sparc". Some kbuild Makefiles test $(ARCH) to
	determine which files to compile.

	By default, the top Makefile sets $(ARCH) to be the same as the
	host system architecture.  For a cross build, a user may
	override the value of $(ARCH) on the command line:

	    make ARCH=m68k ...


    INSTALL_PATH

	This variable defines a place for the arch Makefiles to install
	the resident kernel image and System.map file.
Randy Dunlap's avatar
Randy Dunlap committed
	Use this for architecture-specific install targets.
Linus Torvalds's avatar
Linus Torvalds committed

    INSTALL_MOD_PATH, MODLIB

	$(INSTALL_MOD_PATH) specifies a prefix to $(MODLIB) for module
	installation.  This variable is not defined in the Makefile but
	may be passed in by the user if desired.

	$(MODLIB) specifies the directory for module installation.
	The top Makefile defines $(MODLIB) to
	$(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE).  The user may
	override this value on the command line if desired.

	If this variable is specified, it will cause modules to be stripped
	after they are installed.  If INSTALL_MOD_STRIP is '1', then the
	default option --strip-debug will be used.  Otherwise, the
	INSTALL_MOD_STRIP value will be used as the option(s) to the strip
	command.
=== 9 Makefile language
Linus Torvalds's avatar
Linus Torvalds committed

The kernel Makefiles are designed to be run with GNU Make.  The Makefiles
Linus Torvalds's avatar
Linus Torvalds committed
use only the documented features of GNU Make, but they do use many
GNU extensions.

GNU Make supports elementary list-processing functions.  The kernel
Makefiles use a novel style of list building and manipulation with few
"if" statements.

GNU Make has two assignment operators, ":=" and "=".  ":=" performs
immediate evaluation of the right-hand side and stores an actual string
into the left-hand side.  "=" is like a formula definition; it stores the
right-hand side in an unevaluated form and then evaluates this form each
time the left-hand side is used.

There are some cases where "=" is appropriate.  Usually, though, ":="
is the right choice.

Linus Torvalds's avatar
Linus Torvalds committed

Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net>
Updates by Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
Updates by Sam Ravnborg <sam@ravnborg.org>
Language QA by Jan Engelhardt <jengelh@gmx.de>
Linus Torvalds's avatar
Linus Torvalds committed

Linus Torvalds's avatar
Linus Torvalds committed

- Describe how kbuild supports shipped files with _shipped.
Linus Torvalds's avatar
Linus Torvalds committed
- Generating offset header files.
- Add more variables to section 7?