From a901da7c31ba4bb7f48415dddf688d5439ec285e Mon Sep 17 00:00:00 2001 From: Ziyu Wu Date: Thu, 8 Feb 2024 18:34:50 +0800 Subject: [PATCH] Update post --- source/_posts/2024-02-kbuild.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/source/_posts/2024-02-kbuild.md b/source/_posts/2024-02-kbuild.md index b861956..05ba9e3 100644 --- a/source/_posts/2024-02-kbuild.md +++ b/source/_posts/2024-02-kbuild.md @@ -9,8 +9,6 @@ permalink: /kbuild/ 最近在研究 Linux 内核是如何被构建出来的。 - - ## 什么是 kbuild system? 总所周知,Linux 内核可以说是世界最成功的操作系统了,它已经运行在全世界几乎每一个角落。 @@ -156,7 +154,13 @@ menuconfig: $(obj)/mconf - 当我保存之后,生成的 `.config` 文件里有什么? - `.config` 文件会如何被 kbuild 使用? - +第一个问题,这些配置项是定义在各处的 Kconfig 文件里的,用的是一种 kbuild 专属的配置语言。 + +mconf 的任务就是读取根目录的 Kconfig 文件,而这个 Kconfig 文件自己也会把各处的 kconfig 文件给 include 进来。 + +然后 mconf 还会读取各处已经存在的配置信息,比如说之前执行过配置产生的 `.config` 文件,等等。 + +然后就是渲染出图形界面,接受用户的交互,最后保存,产生一个新的 `.config` 文件。 ### mconf 的工作细节 @@ -674,6 +678,8 @@ vmlinux_link() ## 依赖追踪 + + 文章最开头说了,一个好的构建系统必定是要高效的,一定是可以最大程度避免重复编译的。 先思考一个问题,什么时候某个 `.o` 文件需要被重新编译。 @@ -688,12 +694,14 @@ vmlinux_link() 第二点也很好理解,如果我突然不想把某个驱动链接进内核中,那么我会修改 `.config` 文件,那么相关的构建和链接都要重新执行; -第三点,我们在构建的时候会加一些参数,如果这些参数发生变化了,那么相关的构建肯定要重新执行。 +第三点,每一次的构建都可能会附带一些参数,比如说 gcc 的编译参数,如果这些参数发生变化了,那么这条构建命令肯定也要重新执行。 后面两点则是由 kbuild 通过一定的机制来实现的。 -- `fixdep`:The secret behind this is that fixdep will parse the depfile (.d file), then parse all the dependency files inside, search the text for all the CONFIG_ strings, convert them to the corresponding empty header file, and add them to the target's prerequisites. Every time the configuration changes, the corresponding empty header file will be updated, too, so kbuild can detect that change and rebuild the target that depends on it. Because the command line is also recorded, it is easy to compare the last and current compiling parameters. -- `.cmd` 文件和 `if_changed` 函数:当调用 `if_changed` 函数的时候,它会判断该命令的参数是否发生变化,如果发生了变化,那么就执行该命令,并且把命令记录在 `.cmd` 文件中,当下一次再调用这个函数的时候,它就会先读取该文件来判断命令是否发生改变。 +- `fixdep`:The secret behind this is that fixdep will parse the depfile (.d file), then parse all the dependency files inside, search the text for all the CONFIG\_ strings, convert them to the corresponding empty header file, and add them to the target's prerequisites. Every time the configuration changes, the corresponding empty header file will be updated, too, so kbuild can detect that change and rebuild the target that depends on it. Because the command line is also recorded, it is easy to compare the last and current compiling parameters. +- `.cmd` 文件和 `if_changed` 函数:当调用 `if_changed` 函数的时候,它会判断该命令的参数是否发生变化,如果发生了变化,那么就执行该命令,并且把命令记录在 `.cmd` 文件中,当下一次再调用这个函数的时候,它就会先读取该文件来判断命令是否发生改变。 + +具体的工作原理等我过几天再详细聊聊。 ## 从 kbuild 中,我们可以学习到什么? @@ -708,4 +716,4 @@ vmlinux_link() - [Kernel Build System](https://docs.kernel.org/kbuild/kbuild.html) - [Makefile Tutorial](https://makefiletutorial.com/) - [Differences Between vmlinux, vmlinuz, vmlinux.bin, zimage, and bzimage](https://www.baeldung.com/linux/kernel-images) -- [Exploring the Linux kernel: The secrets of Kconfig/kbuild](https://opensource.com/article/18/10/kbuild-and-kconfig) \ No newline at end of file +- [Exploring the Linux kernel: The secrets of Kconfig/kbuild](https://opensource.com/article/18/10/kbuild-and-kconfig)