Skip to content

Instantly share code, notes, and snippets.

@andyfriesen
Created July 30, 2023 17:20
Show Gist options
  • Save andyfriesen/731baabd7cec5d1c24c53cc0691ca249 to your computer and use it in GitHub Desktop.
Save andyfriesen/731baabd7cec5d1c24c53cc0691ca249 to your computer and use it in GitHub Desktop.
Trying to work out how to get a single mega-compilation-database out of Buck. This is a pretty naive approach, but I think it works?
# compdb/BUCK
python_bootstrap_binary(
name = "combine_compdbs",
main = "combine_compdbs.py",
visibility = ["PUBLIC"]
)
# compdb/combine_compdbs.py
#!/usr/bin/env python3
import sys
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument("database", nargs='+')
parser.add_argument('--output', '-o', dest='output', required=True)
def main(argv):
args = parser.parse_args(argv)
result = {}
for fn in args.database:
content = json.load(open(fn, 'r'))
for row in content:
result[row['file']] = row
res = list(result.values())
json.dump(res, open(args.output, 'w'), indent = 4)
if __name__ == '__main__':
main(sys.argv[1:])
# compdb/defs.bzl
def _impl(ctx: "context"):
output = ctx.actions.declare_output('compile_commands.json')
cmd = [ctx.attrs._combine_compdbs[RunInfo]]
cmd.extend(ctx.attrs.databases)
cmd.append(cmd_args(output.as_output(), format="--output={}"))
ctx.actions.run(
cmd,
category="combine_compilation_databases",
)
return [DefaultInfo(default_output=output)]
combine_compdbs = rule(
impl=_impl,
attrs = {
"databases": attrs.list(attrs.source()),
"_combine_compdbs": attrs.default_only(attrs.exec_dep(providers = [RunInfo], default="//compdb:combine_compdbs"))
}
)
# example BUCK file. Can go any old place.
load("//compdb:defs.bzl", "combine_compdbs")
combine_compdbs(
name='compile-commands',
databases=[
'//:foo[compilation-database]',
'//:bar[compilation-database]',
'//:baz[compilation-database]',
'//:quux[compilation-database]',
],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment