microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.53k stars 1.56k forks source link

Support multiple compile commands files per project #7029

Open stertingen opened 3 years ago

stertingen commented 3 years ago

Type: LanguageService

This is a feature request.

Describe the bug

Some ROS build systems (catkin_tools, catkin_make_isolated) run CMake separately in different repositories. Thus, passing CMAKE_EXPORT_COMPILE_COMMANDS=ON generates n files for n projects. Merging the files manually is cumbersome, so the ideal solution would be the possibility to specify multiple compile commands files and/or support wildcards in the config.

Expected behavior

This configuration to be valid and all compilation databases merged:

{
    "configurations": [
        {
            "compileCommands": [
              "${workspaceFolder}/build/foo_pkg/compile_commands.json",
              "${workspaceFolder}/build/bare_pkg/compile_commands.json",
              "${workspaceFolder}/build/**/compile_commands.json",
            ]
        }
    ],
}
Colengms commented 3 years ago

Hi @stertingen . Thanks for taking the time to suggest this.

Since the project is using CMake, have you considered using the CMake Tools extension? CMake Tools provides configurations to the C/C++ Extension, and should properly handle the full hierarchy of projects from a single top-level CMakeFiles.txt.

stertingen commented 3 years ago

Hi @stertingen . Thanks for taking the time to suggest this.

Since the project is using CMake, have you considered using the CMake Tools extension? CMake Tools provides configurations to the C/C++ Extension, and should properly handle the full hierarchy of projects from a single top-level CMakeFiles.txt.

There exists a single top-level CMakeLists.txt for catkin_make, but not for the tools I mentioned (catkin_tools & catkin_make_isolated). The point of catkin_make_isolated is avoiding side-effects during the build of packages due to global CMake variables. A single top-level CMakeLists.txt would break that isolation.

Colengms commented 3 years ago

@stertingen With CMake Tools, it's possible to specify an arbitrary CMakeLists.txt location, using cmake.sourceDirectory. Since it sounds like you're not using CMake Tools to build, you could use CMake Tools just to provide configurations to the C/C++ Extension, by pointing it to a CMakeList.txt that includes all of the other project directories.

stertingen commented 3 years ago

@stertingen With CMake Tools, it's possible to specify an arbitrary CMakeLists.txt location, using cmake.sourceDirectory. Since it sounds like you're not using CMake Tools to build, you could use CMake Tools just to provide configurations to the C/C++ Extension, by pointing it to a CMakeList.txt that includes all of the other project directories.

You mean, using add_subdirectory()? This solution sounds rather hacky to me, I think, I stick to this one: https://github.com/catkin/catkin_tools/issues/551#issuecomment-732989966

kjeremy commented 3 years ago

@Colengms I don't think that would work for ROS since the top-level CMakeLists.txt is autogenerated. What I really want is glob support:

"compileCommands": "${workspaceFolder}/build/**/compile_commands.json"
github-actions[bot] commented 3 years ago

This feature request is being closed due to insufficient upvotes. When enough upvotes are received, this issue will be eligible for our backlog.

github-actions[bot] commented 3 years ago

This feature request has received enough votes to be added to our backlog.

Plaba commented 2 years ago

For those of you needing this feature now, an easy hack is to merge all the compile_commands.json into one file. This command worked for me on a ROS project.

cat build/**/compile_commands.json | jq -s 'add' > build/compile_commands.json

After that, set compileCommands to "${workspaceFolder}/build/compile_commands.json" and your multi-cmake project should work

visstro commented 1 year ago

As an alternative, there is also clangd plugin which supports multiple configs files per directory, but also regex on paths.

Example .clangd config file:

Diagnostics:
    UnusedIncludes: Strict

---

If:
    PathExclude: .*/tests/.*
CompileFlags:
    CompilationDatabase: build/production/

---

If:
    PathMatch: .*/tests/.*
CompileFlags:
    CompilationDatabase: build/testing/
MichaelVoelkel commented 1 year ago

I would also love this feature. Well, actually I have a multiple-project-workspace and would like to have one per project. Each project generates its own compile_commands.json (naturally), so each should optimally have its own file. But I would also be fine to just have one for all.

sla89 commented 1 month ago

This may also needs adaption of C_Cpp.default.compileCommands which is currently just a string but should be an array to also support multiple file in a workspace.

The workaround of @visstro currently works! Thanks.