libbpf-bootstrap
libbpf-bootstrap copied to clipboard
FindBpfObject.cmake not injecting "Build BPF object file" step to `compile_commands.json`
In the utility CMake module tools/cmake/FindBpfObject.cmake, the step to compile BPF C source files to BPF object files is done by triggering clang with a custom command:
https://github.com/libbpf/libbpf-bootstrap/blob/be15d32fec262e950fbdbb2887ae9a9b066a5cbf/tools/cmake/FindBpfObject.cmake#L163-L171
However, when I enable the CMAKE_EXPORT_COMPILE_COMMANDS option in my CMakeLists.txt, this step will not appear in compile_commands.json because it is a custom command. And because the C/C++ extension in VS Code relies on compile_commands.json for configurations like header locations, it cannot provide proper assistance to development in BPF C source files (*.bpf.c). Could this step be changed to make CMake manage this part so that it can generate correct compile commands? Or are there other ways to work around this issue?
Thanks in advance.
I'm not familiar with CMake, so if you can investigate this and help with this, it would be great. If CMake supports some way to propagate such commands into compile_commands.json, that would be great.
Hi @YangHanlin , in my project I set the command to the following:
# Build BPF object file
add_custom_command(OUTPUT ${BPF_O_FILE}
COMMAND bear --append --output ${CMAKE_BINARY_DIR}/compile_commands.json -- ${BPFOBJECT_CLANG_EXE} -g -O2 -target bpf -D__TARGET_ARCH_${ARCH}
${CLANG_SYSTEM_INCLUDES} -I${GENERATED_VMLINUX_DIR}
-isystem ${LIBBPF_INCLUDE_DIRS} -c ${BPF_C_FILE} -o ${BPF_O_FILE}
COMMAND_EXPAND_LISTS
VERBATIM
DEPENDS ${BPF_C_FILE}
COMMENT "[clang] Building BPF object: ${name}")