Driver and Subsystem Makefiles
The Makefiles in the driver and subsystem directories are used to compile the correct files — in accordance with the configuration in .config — and to direct the compilation flow to the required subdirectories. The Kbuild framework makes the creation of such Makefiles relatively easy. Only the following line is needed to generate an object file for permanent compilation into the kernel (regardless of the configuration):
By reference to the filename, Kbuild automatically detects that the source file is file.c and invokes the C compiler with the appropriate options to generate the binary object file if it is not already present or if the source file has been modified after generation of an old version of the object file. The generated file is also automatically included when the kernel is linked by the linker.
This approach can also be adopted if there are several object files. The specified files must then be separated by blanks.
4This can be set explicitly in the Makefile, specified by means of a shell variable in the environment, or passed as a parameter for
If there is a choice of linking in kernel components or not (in other words, if configuration is controlled by a bool query), the Makefile must react accordingly to the user's selection. The configuration symbol in the Makefile can be used for this purpose, as the following example taken from the Makefile in the kernel/ directory illustrates:
obj-y = sched.o fork.o exec_domain.o panic.o printk.o profile.o \ exit.o itimer.o time.o softirq.o resource.o \
sysctl.o capability.o ptrace.o timer.o user.o user_namespace.o \
signal.o sys.o kmod.o workqueue.o pid.o \
rcupdate.o extable.o params.o posix-timers.o \
kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
hrtimer.o rwsem.o latency.o nsproxy.o srcu.o \
utsname.o notifier.o obj-$(CONFIG_SYSCTL) += sysctl_check.o obj-$(CONFIG_STACKTRACE) += stacktrace.o obj-y += time/
obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o obj-$(CONFIG_SMP) += cpu.o spinlock.o obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o obj-$(CONFIG_MODULES) +=module.o obj-$(CONFIG_KALLSYMS) += kallsyms.o obj-$(CONFIG_PM) += power/
obj-$(CONFIG_SYSCTL) += utsname_sysctl.o obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o obj-$(CONFIG_TASKSTATS) += taskstats.o tsacct.o obj-$(CONFIG_MARKERS) +=marker.o
The files that are always compiled are at the top of the list. The files below them are not compiled by Kbuild unless their configuration symbol is set to y. For example, if module support is configured, the corresponding line expands to the following:
Note the use of += instead of a normal equal sign (=), which causes the object to be added to the target obj-y.
If module support is not configured, the line expands as follows: obj-n += module.o
All files of the target obj-n are ignored by the Kbuild system and are therefore not compiled. The following line for power management is particularly interesting:
Here the target is not a file but a directory. If config_pm is set, Kbuild switches to the kernel/power/ file during compilation and processes the Makefile that it contains.
Kbuild links all the object files of a directory that are contained in the target obj-y into an overall object file built-in.o, which is subsequently linked into the finished kernel.5
Modules fit seamlessly into this mechanism, as the following Ext3 Makefile example demonstrates:
# Makefile for the linux ext3-filesystem routines.
obj-$(CONFIG_EXT3_FS) += ext3.o ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \ ioctl.o namei.o super.o symlink.o hash.o resize.o ext3_jbd.o ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o ext3-$(CONFIG_EXT3_FS_SECURITY) += xattr_security.o
If the Ext3 filesystem is compiled as a module and config_ext3_fs therefore expands to m, the standard target obj-m stipulates that a file named ext3.o must be generated. The contents of this object file are defined by a further explicit target called ext3-y.
The kernel employs indirect specification of the source files rather than direct specification in obj-m so that additional features (whether enabled or not) can be taken into account. (The corresponding configuration symbols in the Kconfig mechanism are described by a bool selection, and a tristate is used for the main symbol config_ext3_fs.)
If, for example, extended attributes are to be used, config_ext3_fs_xattr expands to y, and this produces the following line in the Makefile:
ext3-y += xattr.o xattr_user.o xattr_trusted.o
This links the additionally required object files into the object file and clearly indicates why the indirect target ext3-y is used. If the following had been used, there would be two targets (obj-y and obj-m):
obj-$(CONFIG_EXT3_FS)} += xattr.o xattr_user.o xattr_trusted.o As a result, the additional files would not be included in the standard Ext3 object.
Of course, the indirect approach also works when Ext3 is permanently compiled into the kernel.
Continue reading here: B4 Useful Tools
Was this article helpful?