Skip to content

Enable CMake "compile_commands.json" Output#3854

Merged
paulromano merged 2 commits intoopenmc-dev:developfrom
jtramm:export-compile-commands
Mar 5, 2026
Merged

Enable CMake "compile_commands.json" Output#3854
paulromano merged 2 commits intoopenmc-dev:developfrom
jtramm:export-compile-commands

Conversation

@jtramm
Copy link
Contributor

@jtramm jtramm commented Mar 5, 2026

Description

Modern code editors like VSCode with the C++ language tools provide "intellisense" and other linter capabilities to allow for error checking and logical exploration of a codebase. Using these tools often involves VSCode automatically running cmake for the project with the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON flag. This generates a compile_commands.json file in the build directory that lists exactly which commands are used to compile each file, e.g.,:

{
  "directory": "/Users/jtramm/openmc/build",
  "command": "/opt/homebrew/opt/llvm/bin/clang++ -DBUILD_TYPE=RelWithDebInfo -DCOMPILER_ID=Clang -DCOMPILER_VERSION=20.1.2 -DH5_BUILT_AS_DYNAMIC_LIB -DUSE_LIBPNG -Dlibopenmc_EXPORTS -I/Users/jtramm/openmc/include -I/Users/jtramm/openmc/build/include -I/Users/jtramm/openmc/vendor/fmt/include -I/Users/jtramm/openmc/vendor/pugixml/src -isystem /opt/homebrew/include -O2 -g -DNDEBUG -std=c++17 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.5.sdk -fPIC -DH5Oget_info_by_idx_vers=1 -DH5O_info_t_vers=1 -o CMakeFiles/libopenmc.dir/src/bremsstrahlung.cpp.o -c /Users/jtramm/openmc/src/bremsstrahlung.cpp",
  "file": "/Users/jtramm/openmc/src/bremsstrahlung.cpp",
  "output": "CMakeFiles/libopenmc.dir/src/bremsstrahlung.cpp.o"
},
{
  "directory": "/Users/jtramm/openmc/build",
  "command": "/opt/homebrew/opt/llvm/bin/clang++ -DBUILD_TYPE=RelWithDebInfo -DCOMPILER_ID=Clang -DCOMPILER_VERSION=20.1.2 -DH5_BUILT_AS_DYNAMIC_LIB -DUSE_LIBPNG -Dlibopenmc_EXPORTS -I/Users/jtramm/openmc/include -I/Users/jtramm/openmc/build/include -I/Users/jtramm/openmc/vendor/fmt/include -I/Users/jtramm/openmc/vendor/pugixml/src -isystem /opt/homebrew/include -O2 -g -DNDEBUG -std=c++17 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.5.sdk -fPIC -DH5Oget_info_by_idx_vers=1 -DH5O_info_t_vers=1 -o CMakeFiles/libopenmc.dir/src/cell.cpp.o -c /Users/jtramm/openmc/src/cell.cpp",
  "file": "/Users/jtramm/openmc/src/cell.cpp",
  "output": "CMakeFiles/libopenmc.dir/src/cell.cpp.o"
},

In turn, many of these intellisense and linter tools will then read this json file so that they can know e.g. exactly which headers to follow, which macros are defined, etc.

OpenMC's CMake doesn't currently run this flag so the editor needs to re-run cmake manually. Not a problem for VSCode, but I'm making a skill for Claude code that uses clangd (basically the same idea as VSCode intellisense) to improve the agent's ability to navigate OpenMC. This requires that a compile_commands.json is present. If it's not there, it makes it a little more complex for the agent as it has to go back and figure out how to compile OpenMC etc. As such, it seems like it doesn't hurt anything to add this flag into CMake so that it's there consistently. The resulting file on my system is only 177 kB, and just sits in the build folder, so doesn't seem objectionable. The build isn't otherwise modified -- you just get the added compile_commands.json file output.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

Enable CMAKE_EXPORT_COMPILE_COMMANDS so that cmake generates a
compile_commands.json file in the build directory. This is used by
clangd, clang-tidy, and other C++ tooling for accurate code navigation
and analysis. No impact on the build itself.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jtramm jtramm requested a review from paulromano March 5, 2026 15:52
Copy link
Contributor

@paulromano paulromano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this so that it still allows the user to override the setting if they explicitly pass -DCMAKE_EXPORT_COMPILE_COMMANDS=OFF. Otherwise good!

@paulromano paulromano enabled auto-merge (squash) March 5, 2026 22:28
@paulromano paulromano merged commit 533f09d into openmc-dev:develop Mar 5, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants