Mastering Linux Kernel Makefiles: A Deep Dive
In the Linux kernel build system, the Makefile hierarchy works in conjunction with the .config file to determine which code will be compiled. Here's how it works based on the provided examples:
1. The .config File
The .config file is generated by the kernel configuration tool (e.g., make menuconfig). It contains a list of configuration options in the form of CONFIG_ variables. For example:
CONFIG_SPI=y
CONFIG_SPI_DEBUG=n
CONFIG_SPI_MASTER=y
These variables are used to conditionally include or exclude code during the build process.
2. Top-Level Makefile
The top-level Makefile in the kernel source tree includes various subdirectories and their respective Makefiles. For example:
obj-$(CONFIG_SPI) += spi/
This line means:
3. Subdirectory Makefile
Inside the spi/ directory, there is another Makefile that further specifies what to build:
SPDX-License-Identifier: GPL-2.0
Makefile for kernel SPI drivers.
ccflags-$(CONFIG_SPI_DEBUG) := -DDEBUG
obj-$(CONFIG_SPI_MASTER) += spi.o
4. How It All Works Together
For example:
Summary
The hierarchy of Makefiles and the .config file work together to conditionally include or exclude code based on the configuration options. This modular approach allows the build system to be flexible and only compile the necessary components, reducing build time and binary size.