Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindBpfObject.cmake not injecting "Build BPF object file" step to compile_commands.json #138

Open
yanghanlin opened this issue Dec 18, 2022 · 2 comments

Comments

@yanghanlin
Copy link

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:

# Build BPF object file
add_custom_command(OUTPUT ${BPF_O_FILE}
COMMAND ${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}")

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.

@anakryiko
Copy link
Member

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.

@zouyonghao
Copy link

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}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants