Skip to content

Commit a40b542

Browse files
authored
Merge pull request #13 from helly25/backport/210-output-dir
Add output_dir parameter to refresh_compile_commands
2 parents b480429 + aaafeb5 commit a40b542

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

refresh.template.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,16 +1467,19 @@ def main():
14671467
There should be actionable warnings, above, that led to this.""")
14681468
sys.exit(1)
14691469

1470+
# Resolve the output path; `output_dir` is the macro parameter (empty == cwd).
1471+
output_path = os.path.join({output_dir}, 'compile_commands.json')
1472+
14701473
# Remove any existing compile_commands.json before opening; handles the common
14711474
# case where it's a symlink (e.g. pointing into a cmake build dir), which would
14721475
# otherwise produce an inscrutable open() error. https://github.com/hedronvision/bazel-compile-commands-extractor/issues/105
14731476
try:
1474-
os.remove('compile_commands.json')
1477+
os.remove(output_path)
14751478
except FileNotFoundError:
14761479
pass
14771480

14781481
# Chain output into compile_commands.json
1479-
with open('compile_commands.json', 'w') as output_file:
1482+
with open(output_path, 'w') as output_file:
14801483
json.dump(
14811484
compile_command_entries,
14821485
output_file,

refresh_compile_commands.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def refresh_compile_commands(
6767
exclude_external_sources = False,
6868
bazel_command = "bazel",
6969
max_threads = None,
70+
output_dir = "",
7071
**kwargs): # For the other common attributes. Tags, compatible_with, etc. https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes.
7172
# Convert the various, acceptable target shorthands into the dictionary format
7273
# 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.
@@ -92,7 +93,7 @@ def refresh_compile_commands(
9293

9394
# Generate the core, runnable python script from refresh.template.py
9495
script_name = name + ".py"
95-
_expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, bazel_command = bazel_command, max_threads = max_threads, **kwargs)
96+
_expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, bazel_command = bazel_command, max_threads = max_threads, output_dir = output_dir, **kwargs)
9697

9798
# Combine them so the wrapper calls the main script.
9899
# Tag "manual" so `bazel build //...` won't try to build this when
@@ -127,6 +128,7 @@ def _expand_template_impl(ctx):
127128
"{exclude_external_sources}": repr(ctx.attr.exclude_external_sources),
128129
"{exclude_headers}": repr(ctx.attr.exclude_headers),
129130
"{max_threads}": repr(ctx.attr.max_threads if ctx.attr.max_threads > 0 else None),
131+
"{output_dir}": repr(ctx.attr.output_dir),
130132
"{print_args_executable}": repr(ctx.executable._print_args_executable.path),
131133
},
132134
)
@@ -139,6 +141,7 @@ _expand_template = rule(
139141
"exclude_headers": attr.string(values = ["all", "external", ""]), # "" needed only for compatibility with Bazel < 3.6.0
140142
"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.
141143
"max_threads": attr.int(default = 0), # 0 means "use the historical default" inside refresh.template.py
144+
"output_dir": attr.string(default = ""), # Empty means: write to the workspace root (cwd, the historical behaviour).
142145
# For Windows INCLUDE. If this were eliminated, for example by the resolution of https://github.com/clangd/clangd/issues/123, we'd be able to just use a macro and skylib's expand_template rule: https://github.com/bazelbuild/bazel-skylib/pull/330
143146
# Once https://github.com/bazelbuild/bazel/pull/17108 is widely released, we should be able to eliminate this and get INCLUDE directly. Perhaps for 7.0? Should be released in the sucessor to 6.0
144147
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),

0 commit comments

Comments
 (0)