I need some KBuild
implementation details advice related to building of external modules.
Linux Kernel 5.0.0-32
Here is my LKM
Makefile:
obj-m += pfsw.o
pfsw-objs := src/init.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
Looking at the implementation of scripts/Makefile.build
and printing the debug output with -d
option I found that the target of this makefile being executed is __build
:
__build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
$(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
$(subdir-ym) $(always)
@:
Since I'm building External LKM the only prerequisites are $(obj-m)
and $(modorder-target)
. I got their values from the database:
obj-m := /home/memyself/lkm/procfs_write_perm/pfsw.o
modorder-target := /home/memyself/lkm/procfs_write_perm/modules.order
So to execute __build
the prerequisite /home/memyself/lkm/procfs_write_perm/pfsw.o
have to be built first. There is the following $(obj)/%.o:
pattern rule defined in Makefile.build
:
$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
$(call cmd,force_checksrc)
$(call if_changed_rule,cc_o_c)
I added debug output to print the name of the target automatic variable:
$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
@echo "$@"
$(call cmd,force_checksrc)
$(call cmd,force_check_kmsg)
$(call if_changed_rule,cc_o_c)
and expected /home/memyself/lkm/procfs_write_perm/pfsw.o
to be printed, but actually /home/memyself/lkm/procfs_write_perm/src/init.o
was printed.
This looks like some magic...
QUESTION: Why is building the target /home/memyself/lkm/procfs_write_perm/pfsw.o
causes building /home/memyself/lkm/procfs_write_perm/src/init.o
? Where does it specified in the code?
I understand that there is the real-obj-m
variable containing exactly the value, but grep
ing the code base I did not find it depending on something...