rizsotto / Bear

Bear is a tool that generates a compilation database for clang tooling.
GNU General Public License v3.0
4.64k stars 306 forks source link

How can I create the compile_commands.json for the Bear project? #570

Closed iyht closed 2 months ago

iyht commented 2 months ago

I apologize if this is a silly question. I'm new to this project and attempting to build Bear using CMake. I think it should generate the compile_commands.json file with the CMake argument -DCMAKE_EXPORT_COMPILE_COMMANDS=ON, but it doesn't seem to be doing so. I also tried adding set(CMAKE_EXPORT_COMPILE_COMMANDS ON) in the CMakeLists.txt, but that didn't work either. How can I create compile_commands.json for the Bear project?

Thank you for any help!

jbmaillet commented 2 months ago

It seems you put yourself in a chicken and egg situation. Or maybe you misunderstood the purpose of Bear? The purpose of Bear is not to generate a compile_commands.json for itself. It is to have a tool, named Bear, that will allow you to generate some compile_commands.json files later on, on some other code that you would compile.

So compile Bear as described, and then use it on some other projects for which you would like to have a compilation database.

iyht commented 2 months ago

Thank you for your response. I apologize if I didn't express myself clearly. I understand that the purpose of Bear is to generate a compile database for non-CMake projects.

I was trying to dig into the Bear C++ code. As the Bear is C++ and CMake project itself, so without using any tool(Bear, compiledb ...), the CMake itself should(if I understand correctly) generate the compile_commands.json if I execute cmake command as below

mkdir build && cd build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..

Or if CMakeList.txt have set(CMAKE_EXPORT_COMPILE_COMMANDS ON) flag set.

Despite following the build instructions for Bear and setting the necessary CMake flag, I could not locate the compile_commands.json within Bear's codebase. So that I'm curious whether this is expected or if there might be an error on my part, and how the developer of Bear generates compile_commands.json for the Bear's code?

rizsotto commented 2 months ago

@iyht this project using CMake submodules. You need to pass the CMAKE_EXPORT_COMPILE_COMMANDS=ON to the subproject. And will find the compilation database in one of the subdirectory of the build dir.

https://github.com/rizsotto/Bear/blob/master/CMakeLists.txt#L48