dem*_*guy 17 c linux makefile linux-kernel kbuild
当我正在开发一个Linux驱动程序时,我已经阅读了有关如何通过本文档编写linux kbuild makefile的内容
我知道kbuild系统使用makefile变量,例如obj-y obj-m来确定要构建的内容以及如何构建.
但我感到困惑的是kbuild系统在哪里真正执行构建过程.总之,如果我有obj-m = a.o,那么kbuild系统在哪里解析obj-m并执行gcc a.c?
Ulf*_*zer 26
Kbuild的Makefile并不是最容易阅读的,但这里是一个高级解读(使用4.0-rc3内核):
顶级Makefile可以
include $(srctree)/scripts/Kbuild.include
Run Code Online (Sandbox Code Playgroud)
,$(srctree)顶级内核目录在哪里.
Kbuild.include定义了各种常见的东西和助手.其中包括build:
###
# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
# Usage:
# $(Q)$(MAKE) $(build)=dir
build := -f $(srctree)/scripts/Makefile.build obj
Run Code Online (Sandbox Code Playgroud)
build与命令$(MAKE) $(build)=dir一起使用,以执行目录的构建dir.它利用了scripts/Makefile.build.
返回顶级Makefile,有以下内容:
$(vmlinux-dirs): prepare scripts
$(Q)$(MAKE) $(build)=$@
Run Code Online (Sandbox Code Playgroud)
vmlinux-dirs包含要构建的子目录列表(init,usr,kernel等).$(Q)$(MAKE) $(build)=<subdirectory>将为每个子目录运行.
上面的规则编译内核映像和模块的目标文件.在顶级Makefile中,还有一些额外的模块特定内容:
ifdef CONFIG_MODULES
...
modules: $(vmlinux-dirs) $(if $(KBUILD_BUILTIN),vmlinux) modules.builtin
# Do additional module-specific stuff using
# scripts/Makefile.modpost among other things
# (my comment).
...
...
endif # CONFIG_MODULES
Run Code Online (Sandbox Code Playgroud)现在查看scripts/Makefile.build(使用的Makefile $(build)),它首先初始化obj-*列表和其他各种列表:
# Init all relevant variables used in kbuild files so
# 1) they have correct type
# 2) they do not inherit any value from the environment
obj-y :=
obj-m :=
lib-y :=
lib-m :=
Run Code Online (Sandbox Code Playgroud)
阿位进一步下跌,它加载在其中的kbuild文件obj-y,obj-m等等,都设置:
include $(kbuild-file)
Run Code Online (Sandbox Code Playgroud)
进一步向下是默认规则,其中包含$(obj-y)和$(obj-m)列表作为先决条件:
__build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
$(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
$(subdir-ym) $(always)
@:
Run Code Online (Sandbox Code Playgroud)
的$(obj-y)先决条件来自$(builtin-target),其被定义如下:
builtin-target := $(obj)/built-in.o
...
$(builtin-target): $(obj-y) FORCE
$(call if_changed,link_o_target)
Run Code Online (Sandbox Code Playgroud)
实际建筑似乎是由以下规则执行的:
# Built-in and composite module parts
$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
$(call cmd,force_checksrc)
$(call if_changed_rule,cc_o_c)
Run Code Online (Sandbox Code Playgroud)
if_changed_rule是来自Kbuild.include.该规则最终在以下命令中运行以下命令Makefile.build:
define rule_cc_o_c
$(call echo-cmd,checksrc) $(cmd_checksrc) \
$(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
...
endef
Run Code Online (Sandbox Code Playgroud)
$(cmd_cc_o_c)似乎是实际的编译命令.通常的定义(Makefile.buildAFAICS 有两种可能性)似乎如下:
cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
Run Code Online (Sandbox Code Playgroud)
除非明确使用eg make CC=clang,CC默认设置gcc,如顶层Makefile中所示:
ifneq ($(CC),)
ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1)
COMPILER := clang
else
COMPILER := gcc
endif
export COMPILER
endif
Run Code Online (Sandbox Code Playgroud)我解决这个问题的方法是CTRL-C在内核构建期间执行a 并查看make报告错误的位置.另一个方便的make调试技术是$(warning $(variable))用来打印的值variable.