Skip to content
Yiftah Waisman edited this page Oct 21, 2024 · 15 revisions

Info

CMake Tools solution is limited

  • must use CMake as the build system
  • must use VSCode's CMake Tools to build your project (running cmake .., make on your project will not cause CMake Tools to merge the generated db files)

Concept and Implementation Suggestion:

Extension Settings

Option 1: settings.json

  • define the following entries in .vscode/settings.json:
// already implemented
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/merged_compile_commands.json",
// new
"C_cpp.mergeCompileCommands": { 
"sources": [
        "${workspaceFolder}/build/proj1/compile_commands.json",
        "${workspaceFolder}/build/proj2/compile_commands.json",
    ],
    "destination": "${workspaceFolder}/build/merged_compile_commands.json"
},
  • "C_Cpp.default.compileCommands" is not changed (to a list) for backwards compatibility, specifying multiple sources will not inform where the destination file should be saved.
  • "C_Cpp.default.compileCommands" can also be ignored and overridden to C_cpp.mergeCompileCommands.destination (not mandatory)

Option 2: c_cpp_properties.json

"configurations":  [
    {
        // already implemented
        "name":  "My Custom Config",
        "compileCommands":  "${workspaceFolder}/build/merged_compile_commands.json",
        // new
        "mergeCompileCommands":  [
            "${workspaceFolder}/build/proj1/compile_commands.json",
            "${workspaceFolder}/build/proj2/compile_commands.json"
        ]
    }
],

Pros/Cons:

  • todo

Implementation:

  • File watchers are constructed for each entry in "C_cpp.mergeCompileCommands.sources"
  • When a file watcher event is triggered on any of the sources, we try to collect them all and merge to a single file, with the same merging strategy used in CMake Tools.

Why not use paths already defined in .vscode/c_cpp_properties.json configurations?

  • paths defined in c_cpp_properties.json are specific for a configuration, each maybe having one compileCommands path.
  • C_Cpp.default.compileCommands from .vscode/settings.json is only used if the selected configuration didn't define a compileCommands.
  • defining compileCommands in configurations will mask the single merged file as some configuration is always selected.

Why are glob patterns not supported?

  • the CMake Tools extension (probably) supports glob patterns without consuming heavy CPU resources as it only tries to search for compile_commands.json files when a a target build is finished succesfully. (one time per build)
  • the C/C++ extension doesn't know when a build is started (or finished), so it actively observes for changes in the user-defined compileCommands files.
    • continuously stating multiple directories and subdirectories might create a non-negligible and persistent CPU load.
    • continuously stating build directories will create many false positive events during compilation processes that take more than a few seconds.

Error Handling

files defined in "sources" don't exist

  • todo

Number of file watchers exceeded

  • todo