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

时间:2016-08-30 23:21:39

标签: linux-kernel kbuild

generate_testapi.py成为我的Linux内核模块源代码树中的一个脚本,它会提取mymod_test.h并生成一个面向userland的接口源文件(ioctl,debugfs,你可以命名),并让这个命名为{ {1}}。

在Kbuild makefile中,让$(obj)/mymod_test_interfaces.gen.c成为包含构成模块的目标文件列表的变量,即

Kbuild-ish规则是如何形成的,它将生成源中的目标文件编译作为mymod的依赖项添加,并描述生成过程。

以下,我对这样的规则集的第一次天真尝试不起作用。

mymod-y

尝试使用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

更新(由Alexandre Belloni发表评论)

是的,我也尝试了

形式的生成器规则
mymod_test_interfaces.gen.o

具有相同的结果,即它不起作用。

1 个答案:

答案 0 :(得分:2)

还有另一个问题:在尝试构建多个文件模块时,您不能拥有与模块同名的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