
This article is an excellent piece from the Kanxue Forum
Author ID on Kanxue Forum: jmpcall
Essential Knowledge
1.1. Basic Syntax of Makefile
If you are not familiar with Makefile syntax, it is recommended to learn it systematically, especially the following points:
(1) Which parts of the Makefile contain shell commands: the command part in the compilation rules ${shell XX}, the XX part in var != XX, and the XX part in $(if …, XX, XX)
(2) Variable expansion: = (deferred assignment), := (immediate assignment), != (value is a shell command), ?= (conditional assignment), += (append)
(3) include: expands the contents of specified other Makefiles into the current Makefile -f/-C: execute the specified (directory) Makefile. Executing a Makefile does not start from the first line, but from the specified or default compilation target (the assignment statements before the position target compilation rules are executed only when the corresponding variable needs to be used), where the first target in the Makefile (including included content) is the default target. If no compilation target is specified in the make command line, the default target is executed.
(4) Automatic dependency file inference
(5) Judging dependency updates based on file timestamps and intermediate files (.d, .cmd) to decide whether recompilation is needed
(6) Important built-in functions: $(wildcard pattern) $(patsubst pattern, replacement, text) $(strip string) $(filter pattern, text) $(filter-out pattern, text) $(call func, args..)…
(7) Automatic inferred variables: $@ : compilation target $< : first dependency in the dependency list $^ : all objects in the dependency list $? : all updated files in the dependency file list
For Makefile tutorials, you can refer to the following two: In-depth Analysis of Makefile Series:https://zhuanlan.zhihu.com/p/362640343 (Concise, directly to the core) Write Makefile with me (Chen Hao):https://blog.csdn.net/whitefish520/article/details/103968609 (Classic, super detailed)
1.2. Built-in Functions of Kbuild
The Linux kernel source code contains a set of Makefile programs, this article analyzes based on the Linux-5.2.5 kernel source code, which includes the top Makefile, Makefile in the scripts/ directory, Makefile.build, Makefile.lib, Kbuild.include, Makefile.modpost, kconfig/Makefile, etc., as well as many sub-Makefiles in other directories, collectively referred to as Kbuild. Kbuild is implemented according to the framework design concept, allowing a large number of driver modules included in the kernel itself or provided externally to compile by simply providing a simple Makefile according to the Kbuild framework conventions.
Therefore, understanding the compilation process of the kernel or driver files is actually about understanding the implementation logic of this set of Makefile programs in Kbuild. Since it is a program, it inevitably defines some functions. Many key processes use $(build) and $(if_changed), so the following introduces them separately (the Makefile content analyzed in this article comes from the Linux-5.2.5 kernel source code):
1.2.1. $(build)
◆ Usage: $(Q)$(MAKE) $(build)=xx directory [compilation target]
◆ Internal process of build
◆ Overview of build function
Below is the usage of $(build), and the function of each part:
1.2.2. $(if_changed)
◆ Usage: $(call if_changed, xx)
◆ Internal process of if_changed
◆ Overview of if_changed function
Below is the usage of $(if_changed) and the meaning of its parameters:
Compiling External Modules
2.1. Related Makefile Content
2.2. Overview Process
2.3. Detailed Process
make menuconfig
3.1. Related Makefile Content
3.2. Overview Process
3.3. Detailed Process

Make [all/_all/modules]
4.1. Related Makefile Content
When the make command line specifies the all/_all/modules target, or does not specify a target, it is to generate the vmlinux file, and the vmlinux target indirectly depends on the prepare target, and the compilation rules for the prepare target have a lot of content, so it is introduced separately below:
4.2. Overview Process
4.3. Detailed Process
References
Detailed Series on Linux Kbuild:
https://zhuanlan.zhihu.com/p/362640343
Write Makefile with me:https://blog.csdn.net/whitefish520/article/details/103968609
Click at the end of the article to read the original text to obtain attachments: Some images in the text are cropped based on the content of “Execution Process.txt”, and the rest have been packaged into the “images.zip” file.
Kanxue ID: jmpcall
https://bbs.kanxue.com/user-home-815036.htm
*This article is original by jmpcall from the Kanxue Forum, please indicate the source from the Kanxue community when reprinting
# Previous Recommendations
1. Setting up LLVM environment under Windows
2. In-depth study of smali syntax
3. Android reinforcement and unpacking sharing
4. Flutter reverse engineering exploration
5. A simple practice to understand stack space transfer
6. Remembering an unpacking and repair of a certain shield mobile game