From e1889c70741d420da819554933eba90189cedaef Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 16 Jan 2026 21:09:28 +0100 Subject: [PATCH] Convenience function for interacting with compile_commands_impl Why: Simplify integration src/compile_commands.bzl into src/per_file.bzl What: - Introduce get_compile_commands() function which returns only compile_commands.json File object and performs optional checks --- src/compile_commands.bzl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/compile_commands.bzl b/src/compile_commands.bzl index 24aa2652..76c9f0c4 100644 --- a/src/compile_commands.bzl +++ b/src/compile_commands.bzl @@ -418,6 +418,27 @@ def compile_commands_impl(ctx): ), ] +def get_compile_commands(ctx, check=True): + """ Returns the compilation database file + + Returns: + compile_commands.json File object + """ + compile_commands = None + source_files = None + for output in compile_commands_impl(ctx): + if type(output) == "DefaultInfo": + compile_commands = output.files.to_list()[0] + source_files = output.default_runfiles.files.to_list() + # FIXME: there should be just one file + if check and not compile_commands: + fail("Failed to generate compile_commands.json file!") + if check and not source_files: + fail("Failed to collect source files!") + if check and compile_commands != ctx.outputs.compile_commands: + fail("Seems compile_commands.json file is incorrect!") + return compile_commands + _compile_commands = rule( implementation = compile_commands_impl, attrs = {