如何在 Kbuild 中添加对生成的源文件的依赖?

How to add a dependency on a generated source file in Kbuild?

generate_testapi.py 成为我的 Linux 内核模块源代码树中的一个脚本,它摄取 mymod_test.h 并生成一个面向用户空间的接口源文件(ioctl、debugfs,随便你怎么说) , 让我们将其命名为 $(obj)/mymod_test_interfaces.gen.c.

在 Kbuild makefile 中,让 mymod-y 成为包含构成模块的目标文件列表的变量,即

Kbuild-ish 规则是什么样的,它添加了从生成源编译的目标文件作为 mymod 的依赖项并描述了生成过程。

以下是我对此类规则集的第一次幼稚尝试,但没有成功。

obj-m := mymod.o

mymod-y := \
    mymod_kmod.o \
    $(obj)/mymod_test_interfaces.gen.o

$(obj)/mymod_test_interfaces.gen.o: $(src)/mymod_test.h $(src)/generate_testapi.py
    $(src)/generate_testapi.py < $(src)/mymod_test.h > $<

尝试make,结果错误是,没有规则mymod_test_interfaces.gen.o

更新(由于 Alexandre Belloni 的评论)

是的,我还尝试了

形式的生成器规则
$(obj)/mymod_test_interfaces.gen.c: $(src)/mymod_test.h $(src)/generate_testapi.py
    $(src)/generate_testapi.py < $(src)/mymod_test.h > $<

同样的结果,就是不起作用。

还有一个问题:在尝试构建多文件模块时,您不能拥有与模块同名的 C 文件。将 mymod.c 重命名为 mymod-core.c

那么,下面的方法应该有效:

obj-m := mymod.o

mymod-objs := mymod-core.o mymod_test_interfaces.gen.o

$(obj)/mymod_test_interfaces.gen.c: $(src)/mymod_test.h $(src)/generate_testapi.py
    $(src)/generate_testapi.py < $(src)/mymod_test.h > $@

我实际测试了以下内容:

diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 4387ccb79e64..47fdc3a73c75 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -71,3 +71,8 @@ OBJCOPYFLAGS_lkdtm_rodata_objcopy.o := \
                        --rename-section .text=.rodata
 $(obj)/lkdtm_rodata_objcopy.o: $(obj)/lkdtm_rodata.o
        $(call if_changed,objcopy)
+
+obj-m := mymod.o
+mymod-objs := mymod-core.o mymod_test_interfaces.gen.o
+$(obj)/mymod_test_interfaces.gen.c:
+       echo "int variable;" > $@
diff --git a/drivers/misc/mymod-core.c b/drivers/misc/mymod-core.c
new file mode 100644
index 000000000000..b39aa4b9ae8d
--- /dev/null
+++ b/drivers/misc/mymod-core.c
@@ -0,0 +1,19 @@
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+extern int mymod_variable;
+
+static int mymod_probe(struct platform_device *pdev)
+{
+       mymod_variable = 2;
+
+       return 0;
+}
+
+static struct platform_driver mymod_driver = {
+       .driver = {
+               .name = "mymod",
+       },
+       .probe = mymod_probe,
+};
+module_platform_driver(mymod_driver);

它正确地导致:

$ make
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  CHK     include/generated/compile.h
  CC [M]  drivers/misc/mymod-core.o
echo "int variable;" > drivers/misc/mymod_test_interfaces.gen.c
  CC [M]  drivers/misc/mymod_test_interfaces.gen.o
  LD [M]  drivers/misc/mymod.o
  LD      arch/x86/boot/compressed/vmlinux
  ZOFFSET arch/x86/boot/zoffset.h
  AS      arch/x86/boot/header.o
  LD      arch/x86/boot/setup.elf
  OBJCOPY arch/x86/boot/setup.bin
  OBJCOPY arch/x86/boot/vmlinux.bin
  BUILD   arch/x86/boot/bzImage