Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Kernel generate compile-commands.json for module

The problem: Most of macro definition and even header files are not looked up by an IDE because include path is not specified in the IDE configuration. It inhibits autocompletion and navigation.

Here is my 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 ran make V=1 and noticed that the compile command along with include path is actually pretty cumbersome (counting Linux Specific -include for parameters):

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

Question: Is there a way to generate compile-command.json to inform IDE about include paths? Or the only solution is to manually pass the include path to the IDE one by one?

like image 588
St.Antario Avatar asked Aug 26 '26 18:08

St.Antario


1 Answers

Due to CLang has a lot of different tools, including some to analyze the code, the compile-command.json is required. That's why Tom Roeder from Google provided a scripts/clang-tools/gen_compile_commands.py in the commit b30204640192 ("scripts: add a tool to produce a compile_commands.json file") for this.

Note, kernel must be compiled at least once to make this script work.

P.S. I suppose you are trying MS Visual Studio Code?

Kudos to colleague of mine, Alex, who is user of it and told me about existence of such script.

like image 61
0andriy Avatar answered Aug 29 '26 10:08

0andriy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!