Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion docs/users/how_tos/compilation_database.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ id: compilation_database
title: Compilation databases
---

# Generating compilation databases
# Compilation databases

Compilation databases are JSON files that Clang-based tooling (e.g.
clangd and clang-tidy) consume to know what flags (in particular related
to include paths) are used to build C++ files.

## Generating compilation databases

You can generate compilation databases for consumption by tools such as clangd
and clang-tidy by running the following BXL script:
Expand Down Expand Up @@ -43,3 +49,23 @@ ln -sf $(buck2 bxl comp_db.bxl:gen -- --targets ...) $(git rev-parse --show-topl
Providing better ergonomics for BXL scripts (such as enabling something like
`buck2 comp_db`) is being discussed
[here](https://github.com/facebook/buck2/issues/86).

## Tools jumping to `buck-out`

You may notice that your tools (e.g. clangd's Go To Definition feature)
jump to symlinks into `buck-out`, rather than into the source tree.

This is often problematic, because `buck-out` is not under source
control (so all VCS-related editor tools fail), and text editors
typically handle those symlinks poorly (e.g. VS Code will keep separate
tabs for the symlink and the source header, and Vim will reuse an
existing symlink buffer even when explicitly opening the source header).

The fix for this is to use header maps instead. To enable header maps,
set `header_mode = HeaderMode("header_map_only")` in your
`CxxToolchainInfo`.

Note that header maps are not supported by GCC. This should not be a
problem since compilation databases are Clang technology, but you can
setup a [build constraint](../../rule_authors/configurations.md) and
conditionally set the header mode based on it if needed.
4 changes: 0 additions & 4 deletions prelude/cxx/tools/compilation_database.bxl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ load("@prelude//utils:utils.bzl", "flatten")

def _make_entry(ctx: bxl.Context, target: Label, compile_command: CxxSrcCompileCommand, add_target_name: bool) -> dict:
args = compile_command.cxx_compile_cmd.base_compile_cmd.copy()

# This prevents clangd from jumping into `buck-out` when using Go To
# Definition, which significantly improves user experience.
args.add(["-I", "."])
args.add(compile_command.cxx_compile_cmd.argsfile.cmd_form)
args.add(compile_command.args)
ctx.output.ensure_multiple(args)
Expand Down
Loading