Linux 内核为模块生成编译-commands.json

Linux Kernel generate compile-commands.json for module

问题: 大多数宏定义甚至头文件都不会被 IDE 查找,因为 IDE 中未指定包含路径配置。它禁止自动完成和导航。

这是我的 Makefile:

#-Wno-declaration-after-statement
ccflags-y := -std=gnu11 -Wno-declaration-after-statement -Werror
obj-m += pfsw.o
pfsw-objs := src/init.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

I 运行 make V=1 并注意到编译命令以及包含路径实际上非常麻烦(计算 Linux 具体 -include 参数):

gcc -Wp,-MD,/home/memyself/lkm/procfs_write/src/.init.o.d  -nostdinc 
   -isystem /usr/lib/gcc/x86_64-linux-gnu/7/include  
   -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi
   -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi 
   -include ./include/linux/kconfig.h -Iubuntu/include  -include ./include/linux/compiler_types.h 
   -D__KERNEL__
   //tons of options ommitted...
   -c -o /home/memyself/lkm/procfs_write/src/init.o
    /home/memyself/lkm/procfs_write/src/init.c

问题:有没有办法生成compile-command.json来通知IDE包含路径?或者唯一的解决方案是手动将包含路径逐一传递给 IDE?

由于 CLang 有很多不同的工具,包括一些分析代码的工具,因此需要 compile-command.json。这就是为什么 Google 的 Tom Roeder 提供了一个 scripts/clang-tools/gen_compile_commands.py 在提交 b30204640192(“脚本:添加一个工具来生成 compile_commands.json 文件”)为此。

注意,内核必须在列表中编译一次才能使这个脚本工作。

P.S。我想你正在尝试 MS Visual Studio 代码?

感谢我的同事 Alex,他是它的用户并告诉我存在这样的脚本。