Skip to content

Commit

Permalink
Add a BXL script for generating compilation databases
Browse files Browse the repository at this point in the history
Summary:
This is a diffent approach than facebook/buck2#510. The main differences are:
- All required dependencies, such as generated code, are materialized, so that tools that use the compilation database work can find those and work properly.
- Files that are included in multiple targets result in multiple entries in the compilation database.

Closes facebook/buck2#307

X-link: facebook/buck2#810

Reviewed By: scottcao

Differential Revision: D66984456

Pulled By: cjhopman

fbshipit-source-id: e53c5cfdcf32e4c34331d7343c06a486a971d27e
  • Loading branch information
cbarrete authored and facebook-github-bot committed Dec 20, 2024
1 parent 809a089 commit a1a6368
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions cxx/tools/compilation_database.bxl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

load("@prelude//utils:utils.bzl", "flatten")
load("@prelude//cxx/comp_db.bzl", "CxxCompilationDbInfo")
load("@prelude//cxx/compile.bzl", "CxxSrcCompileCommand")

def _make_entry(ctx: bxl.Context, compile_command: CxxSrcCompileCommand) -> 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 improces 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)

return {
"arguments": args,
"directory": ctx.fs.abs_path_unsafe(ctx.root()),
"file": compile_command.src,
}

def _impl(ctx: bxl.Context):
actions = ctx.bxl_actions().actions
targets = flatten(ctx.cli_args.targets)

db = []
for _name, target in ctx.analysis(ctx.configured_targets(targets)).items():
comp_db_info = target.providers().get(CxxCompilationDbInfo)
if comp_db_info:
db += [_make_entry(ctx, cc) for cc in comp_db_info.info.values()]

db_file = actions.declare_output("compile_commands.json")
actions.write_json(db_file.as_output(), db, with_inputs = True, pretty = True)
ctx.output.print(ctx.output.ensure(db_file))

generate = bxl_main(
doc = "Generate a compilation database for a set of targets and print its path to stdout",
impl = _impl,
cli_args = {
"targets": cli_args.list(
cli_args.target_expr(),
doc = "Targets to generate the database for",
),
},
)

0 comments on commit a1a6368

Please sign in to comment.