Skip to content

Commit f91b6bc

Browse files
committed
CMake: Set up compile_commands.json automatically
This allows clangd to provide code intelligence. Most completions will work after configuration, although some completions (e.g. involving generated code) will require a build. Generating a compile_commands.json file is non-trivial for Bazel so this should enable a better developer experience for people working on protovalidate-cc. Clangd can be used with a large variety of editors (Vim, Zed, Visual Studio Code, etc.)
1 parent 222d994 commit f91b6bc

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.build*
22
cmake-build-*
3+
.cache

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
*.out
3232
*.app
3333

34+
# Clangd
35+
/.cache
36+
/compile_commands.json
37+
3438
# Additional exclusions
3539
/.tmp/
3640
*.pprof

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ else()
2020
option(PROTOVALIDATE_CC_ENABLE_INSTALL "Enable protovalidate-cc installation targets" ON)
2121
option(PROTOVALIDATE_CC_ENABLE_TESTS "Enable protovalidate-cc unit tests" ON)
2222
option(PROTOVALIDATE_CC_ENABLE_CONFORMANCE "Build conformance runner" ON)
23+
24+
# Set up compile_commands.json automatically when standalone.
25+
option(PROTOVALIDATE_CC_SETUP_COMPILE_COMMANDS "Set up compile_commands.json" ON)
26+
if(PROTOVALIDATE_CC_SETUP_COMPILE_COMMANDS)
27+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
28+
execute_process(
29+
COMMAND ${CMAKE_COMMAND} -E create_symlink
30+
${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
31+
${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json
32+
)
33+
endif()
2334
endif()
2435

2536
if(CMAKE_CXX_STANDARD LESS 17)

0 commit comments

Comments
 (0)