From ba545ba64a15bfa445f221bfb89bc837cfbd1c9f Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 19 Jun 2026 10:21:10 +0200 Subject: [PATCH] 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 --- .clangd | 5 +++++ .gitignore | 1 + CMakeLists.txt | 5 +++++ gen-compile-commands.bat | 8 ++++++++ 4 files changed, 19 insertions(+) create mode 100644 .clangd create mode 100644 gen-compile-commands.bat diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..d1c04ac --- /dev/null +++ b/.clangd @@ -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 diff --git a/.gitignore b/.gitignore index a43d6ef..dc3038f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /bin /build +/build-clangd CMakeUserPresets.json x64 diff --git a/CMakeLists.txt b/CMakeLists.txt index f7df381..4e145af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,11 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) 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 # hook dll land next to each other under /bin (already in .gitignore). set(COOP_OUTPUT_DIR "${CMAKE_SOURCE_DIR}/bin/$") diff --git a/gen-compile-commands.bat b/gen-compile-commands.bat new file mode 100644 index 0000000..788c1f1 --- /dev/null +++ b/gen-compile-commands.bat @@ -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