Skip to content

Commit 5abac8d

Browse files
committed
Add a build rule which produces compile_commands.json for clangd
1 parent 2233edb commit 5abac8d

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

.clangd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# clangd configuration for the Bazel-driven C++ build.
2+
#
3+
# Requires a compile_commands.json at the workspace root. Generate it with:
4+
# bazel run //:refresh_compile_commands
5+
#
6+
# See CONTRIBUTING.md ("Editor / clangd setup") for the full workflow.
7+
8+
CompileFlags:
9+
CompilationDatabase: .

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ coverage.xml
8282
*.pt2
8383
examples/torchtrt_aoti_example/torchtrt_aoti_example
8484
CLAUDE.md
85+
86+
# clangd compilation database (generated by `bazel run //:refresh_compile_commands`)
87+
/compile_commands.json
88+
/external/

BUILD.bazel

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")
12
load("@rules_pkg//:pkg.bzl", "pkg_tar", "pkg_zip")
23

34
config_setting(
@@ -162,3 +163,21 @@ alias(
162163
"//conditions:default": ":libtorchtrt_runtime_tar",
163164
}),
164165
)
166+
167+
# ---------------------------------------------------------------------------
168+
# Developer tooling
169+
# ---------------------------------------------------------------------------
170+
# Generates `compile_commands.json` at the workspace root for clangd-based
171+
# editors. Output is gitignored. Run on first checkout and after material
172+
# build-graph changes:
173+
# bazel run //:refresh_compile_commands
174+
# See CONTRIBUTING.md ("Editor / clangd setup") for details.
175+
refresh_compile_commands(
176+
name = "refresh_compile_commands",
177+
tags = ["manual"],
178+
targets = [
179+
"//core/...",
180+
"//cpp/...",
181+
"//tests/...",
182+
],
183+
)

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ Do try to fill an issue with your feature or bug before filling a PR (op support
88

99
Our build system relies on `bazel` (https://bazel.build/). Though there are many ways to install `bazel`, the preferred method is to use `bazelisk` (https://github.com/bazelbuild/bazelisk) which makes it simple to set up the correct version of bazel on the fly. Additional development dependencies can be installed via the `requirements-dev.txt` file.
1010

11+
#### Editor / clangd setup (optional)
12+
13+
For C++ code intelligence (go-to-definition, accurate diagnostics, completions) in any clangd-based editor — e.g. VSCode with the [clangd extension](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd), Cursor, Neovim, Emacs — generate a Bazel-aware compilation database:
14+
15+
```sh
16+
bazel run //:refresh_compile_commands
17+
```
18+
19+
This writes `compile_commands.json` at the workspace root (gitignored). Re-run it after pulling changes that materially affect the build graph (new targets, new headers, dependency bumps). The repo's `.clangd` file picks up the database automatically.
20+
21+
This is opt-in: developers who don't use clangd are unaffected, and the underlying `hedron_compile_commands` extractor is declared as a Bazel `dev_dependency`, so it does not enter the dep graph of consumers building against torch_tensorrt.
22+
1123
#### Communication
1224

1325
The primary location for discussion is GitHub issues and Github discussions. This is the best place for questions about the project and discussion about specific issues.

MODULE.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ git_override(
2323
remote = "https://github.com/narendasan/rules_pkg",
2424
)
2525

26+
# Developer tooling: clangd-compatible compile_commands.json extractor.
27+
# `dev_dependency = True` keeps this out of the dep graph for any project that
28+
# consumes torch_tensorrt as a Bazel module. Refresh with:
29+
# bazel run //:refresh_compile_commands
30+
# See CONTRIBUTING.md ("Editor / clangd setup") for details.
31+
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
32+
git_override(
33+
module_name = "hedron_compile_commands",
34+
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
35+
commit = "0e990032f3c5a866e72615cf67e5ce22186dcb97",
36+
)
37+
2638
new_local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "new_local_repository")
2739

2840
local_torch = use_repo_rule("//toolchains:local_torch.bzl", "local_torch")

0 commit comments

Comments
 (0)