Tooling: generate compile_commands.json for clangd

The Visual Studio CMake generator does not emit compile_commands.json, so
clangd had no include paths or flags and reported hundreds of false errors.
Add a parallel Ninja config (build-clangd/) that produces the database:

- CMAKE_EXPORT_COMPILE_COMMANDS ON (honored by Ninja, ignored by VS)
- gen-compile-commands.bat configures build-clangd/ under vcvars64
- .clangd points clangd's CompilationDatabase at build-clangd/
- ignore /build-clangd

clangd's clang-cl driver auto-resolves the MSVC/Windows SDK system includes,
so the cl.exe-based database is sufficient.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-19 10:21:10 +02:00
parent e38df6cf88
commit ba545ba64a
4 changed files with 19 additions and 0 deletions

5
.clangd Normal file
View File

@@ -0,0 +1,5 @@
# The Visual Studio CMake generator (build/) does not emit compile_commands.json,
# so the Ninja config in build-clangd/ provides it. Regenerate after changing
# sources or include dirs with: gen-compile-commands.bat
CompileFlags:
CompilationDatabase: build-clangd

1
.gitignore vendored
View File

@@ -4,6 +4,7 @@
/bin /bin
/build /build
/build-clangd
CMakeUserPresets.json CMakeUserPresets.json
x64 x64

View File

@@ -13,6 +13,11 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
# Emit compile_commands.json so clangd resolves includes/flags. Ignored by the
# Visual Studio generator, so configure the Ninja build (build-clangd/) for this;
# .clangd points clangd at that compilation database.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Single output dir keeps the donor-launch / inject workflow simple: host exe and # Single output dir keeps the donor-launch / inject workflow simple: host exe and
# hook dll land next to each other under /bin (already in .gitignore). # hook dll land next to each other under /bin (already in .gitignore).
set(COOP_OUTPUT_DIR "${CMAKE_SOURCE_DIR}/bin/$<CONFIG>") set(COOP_OUTPUT_DIR "${CMAKE_SOURCE_DIR}/bin/$<CONFIG>")

8
gen-compile-commands.bat Normal file
View File

@@ -0,0 +1,8 @@
@echo off
REM Generates compile_commands.json for clangd via a Ninja build (the Visual
REM Studio generator can't emit one). Run from the repo root.
setlocal
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" || exit /b 1
set "NINJA=C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe"
cmake -S . -B build-clangd -G Ninja -DCMAKE_MAKE_PROGRAM="%NINJA%" -DCMAKE_BUILD_TYPE=Debug -DCOOP_BUILD_HOOK=ON || exit /b 1
echo COMPILE_COMMANDS_DONE