diff --git a/refresh.template.py b/refresh.template.py index 194f365..ab95c18 100644 --- a/refresh.template.py +++ b/refresh.template.py @@ -1415,7 +1415,7 @@ def main(): sys.exit(1) # Chain output into compile_commands.json - with open('compile_commands.json', 'w') as output_file: + with open(os.path.join({output_dir}, 'compile_commands.json'), 'w') as output_file: json.dump( compile_command_entries, output_file, diff --git a/refresh_compile_commands.bzl b/refresh_compile_commands.bzl index 0210d42..48e7107 100644 --- a/refresh_compile_commands.bzl +++ b/refresh_compile_commands.bzl @@ -64,6 +64,7 @@ def refresh_compile_commands( targets = None, exclude_headers = None, exclude_external_sources = False, + output_dir = None, **kwargs): # For the other common attributes. Tags, compatible_with, etc. https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes. # Convert the various, acceptable target shorthands into the dictionary format # In Python, `type(x) == y` is an antipattern, but [Starlark doesn't support inheritance](https://bazel.build/rules/language), so `isinstance` doesn't exist, and this is the correct way to switch on type. @@ -89,7 +90,7 @@ def refresh_compile_commands( # Generate the core, runnable python script from refresh.template.py script_name = name + ".py" - _expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, **kwargs) + _expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, output_dir = output_dir, **kwargs) # Combine them so the wrapper calls the main script native.py_binary( @@ -115,12 +116,14 @@ def _expand_template_impl(ctx): "{exclude_headers}": repr(ctx.attr.exclude_headers), "{exclude_external_sources}": repr(ctx.attr.exclude_external_sources), "{print_args_executable}": repr(ctx.executable._print_args_executable.path), + "{output_dir}": repr(ctx.attr.output_dir), }, ) return DefaultInfo(files = depset([script])) _expand_template = rule( attrs = { + "output_dir": attr.string(mandatory=False), "labels_to_flags": attr.string_dict(mandatory = True), # string keys instead of label_keyed because Bazel doesn't support parsing wildcard target patterns (..., *, :all) in BUILD attributes. "exclude_external_sources": attr.bool(default = False), "exclude_headers": attr.string(values = ["all", "external", ""]), # "" needed only for compatibility with Bazel < 3.6.0