Skip to content

Commit

Permalink
Support target collections
Browse files Browse the repository at this point in the history
This change adds two main features:

1. You can configure compile commands for certain targets or groups of
   targets to go into separate files

For embedded systems development in particular, `clangd` doesn't work
well with `compile_commands.json` generated for multiple targets. For
example, if you have a build target that runs on your host machine in
one configuration and another that runs on device in another
configuration, `compile_commands.json` will contain multiple conflicting
compile commands for the same source files. `clangd` will just use the
first one it finds, which may not be the one you want to use for code
intelligence. It's convenient to have separate compile commands files
for each target, and switch the file `clangd` uses depending on how you
want to navigate your code.

By providing the `target_collections` argument, you can associate
targets with named collections. Separate compile commands files will be
generated for each of the specified collections, and will be placed in
subdirectories with the specified names. This is most useful when you
associate multiple targets with a collection, for example, to configure
code intelligence to use the compile commands for all of the targets
that build for one architecture or device.

This means that you can now specify a target more than once, generating
compile commands for builds of the same target but with different flags
(e.g.  `--platform`). Before, you implicitly could only specify each
target once since the targets were dict keys.

2. You can specify a different output path

If you are generating multiple compile commands files, its preferable
not to output them into the workspace root. So you can specify a
separate output path, relative to the workspace root.

This patch doesn't change any existing behavior; if you don't add either
of the new arguments to your invocation of `refresh_compile_commands`,
everything will work exactly as it did before.
  • Loading branch information
chadnorvell committed Jul 13, 2024
1 parent a14ad3a commit 1a7cde6
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 23 deletions.
59 changes: 47 additions & 12 deletions refresh.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Similarly, when upgrading, please search for that MIN_PY= tag.


import collections
import concurrent.futures
import enum
import functools # MIN_PY=3.9: Replace `functools.lru_cache(maxsize=None)` with `functools.cache`.
Expand Down Expand Up @@ -1405,20 +1406,54 @@ def main():
# End: template filled by Bazel
]

compile_command_entries = []
for (target, flags) in target_flag_pairs:
compile_command_entries.extend(_get_commands(target, flags))

if not compile_command_entries:
target_collections = {
# Begin: template filled by Bazel
{target_collections}
# End: template filled by Bazel
}

# Associates lists of compile commands with compile command "collections".
# __all__ is a special case: It contains all generated compile commands.
# Any other collections defined in `target_collections` will contain only
# the compile commands for the targets defined for those collections.
compile_command_sets = collections.defaultdict(list)

for target_key, target_data in target_flag_pairs:
target, flags = target_data
commands = list(_get_commands(target, flags))
# If the target is assigned to any collections, put the compile commands in the compile command sets for those collections.
collections_for_target = [collection_name for collection_name, target_keys in target_collections.items() if target_key in target_keys]
for collection_name in collections_for_target:
compile_command_sets[collection_name].extend(commands)
# Also put them into the main file.
compile_command_sets['__all__'].extend(commands)

if len(compile_command_sets) <= 1 and len(compile_command_sets['__all__']) == 0:
log_error(""">>> Not (over)writing compile_commands.json, since no commands were extracted and an empty file is of no use.
There should be actionable warnings, above, that led to this.""")
sys.exit(1)


if not (root_dir := pathlib.Path({out_dir})).exists():
root_dir.mkdir(parents=True)

# Chain output into compile_commands.json
with open('compile_commands.json', 'w') as output_file:
json.dump(
compile_command_entries,
output_file,
indent=2, # Yay, human readability!
check_circular=False # For speed.
)
for collection in compile_command_sets:
# If the target doesn't have a specified file name, put it into the "catch all"
# compilation database.
if collection == '__all__':
file_path = root_dir / "compile_commands.json"
# Otherwise, write the database to the specific target file.
else:
target_dir = root_dir / collection
target_dir.mkdir(exist_ok=True)
file_path = target_dir / "compile_commands.json"

if (len(compile_command_sets[collection]) > 0):
with open(file_path, 'w') as output_file:
json.dump(
compile_command_sets[collection],
output_file,
indent=2, # Yay, human readability!
check_circular=False # For speed.
)
150 changes: 139 additions & 11 deletions refresh_compile_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ refresh_compile_commands(
# ^ excluding headers will speed up compile_commands.json generation *considerably* because we won't need to preprocess your code to figure out which headers you use.
# However, if you use clangd and are looking for speed, we strongly recommend you follow the instructions below instead, since clangd is going to regularly infer the wrong commands for headers and give you lots of annoyingly unnecessary red squigglies.
# Need to create separate files for specific targets? Give those targets a name and their compile commands file will be written into a subdirectory with that name.
# target_collections = {
# "host": "//:host_build",
# "target": "//:target_build",
# }
# You can define target collections, sort of like "meta-targets" that contain combination of compile commands from the specified targets.
# This is useful for multi-architecture projects, where you might be building the same files in multiple different ways depending on what architecture or device the code will run on.
# It only makes sense to get code intelligence for a consistent build, so you can produce consistent compile commands with target collections.
# Targets only need to be specified in either `targets` or `target_collections`; there's no need to add them to both.
# target_collections = {
# "host": [
# "//:host_build",
# "//:host_tests",
# ],
# "target": [
# ["//:target_build", "--platform=//:target_device --important_flag"],
# ],
# }
# Need to write compile commands to some directory other than the workspace root? Provide a path relative to the workspace root. This is useful if you use target collections.
# out_dir = ".compile_commands"
# Need things to run faster? [Either for compile_commands.json generation or clangd indexing.]
# First: You might be able to refresh compile_commands.json slightly less often, making the current runtime okay.
# If you're adding files, clangd should make pretty decent guesses at completions, using commands from nearby files. And if you're deleting files, there's not a problem. So you may not need to rerun refresh.py on every change to BUILD files. Instead, maybe refresh becomes something you run every so often when you can spare the time, making the current runtime okay.
Expand All @@ -62,34 +86,127 @@ load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
def refresh_compile_commands(
name,
targets = None,
target_collections = None,
out_dir = None,
exclude_headers = None,
exclude_external_sources = False,
**kwargs): # For the other common attributes. Tags, compatible_with, etc. https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes.

# Given `targets` that may be absent, or be a single string target:
# targets = "//:some_target"
#
# ... or be a list of string targets:
# targets = [
# "//:some_target",
# "//:another_target"
# ]
#
# ... or be a dict of string targets and build flags:
# targets = {
# "//:some_target": "--flag ...",
# "//:another_target": "--arg ..."
# }
#
# And given `target_collections` that may be absent, or have a collection name associated with a list of string targets:
# target_collections = {
# "host": [
# "//:host_target",
# ...
# ],
# "device": [
# "//:device_target",
# ...
# ]
# }
#
# ... or have collection names associated with lists of string targets with build flags:
# target_collections = {
# "host": [
# ("//:host_target", "--flag ..."),
# ("//:test_target", "--platform=//:host_platform"),
# ...
# ],
# "device": [
# ("//:device_target", "--arg ..."),
# ("//:test_target", "--platform=//:device_platform"),
# ...
# ]
# }
#
# ... we want to produce a `string_dict_list` that we can pass into the Python script template to assemble the `target_flag_pairs`.
# A simple dict with target name keys isn't adequate, because we can specify a target more than once with different build flags (see the last example above).
# So we assemble a dict where the keys are unique to each target+flag combination, and the values are a list of strings in this format: [<target>, <flags>]
#
# That takes care of the `target_flag_pairs`, which determines which targets to generate compile commands for.
#
# Associating compile commands with target collections is easier; we pass `target_collections`, but with the target names and flags concatenated in the same way
# as described above. This lets us associate each target+flag combination we generate compile commands for with its membership(s) in target collections.

target_collection_targets_list = []
serialized_target_collections = {}

if target_collections:
# Convert the targets specified in `target_collections` into the format we'll use with `targets`, so we can combine them into one list of targets to generate compile commands for.
for targets_for_collection in target_collections.values():
# You can specify a bare string target if the collection only has one target.
if type(targets_for_collection) != "list":
targets_for_collection = [targets_for_collection]

for target in targets_for_collection:
# The target can either be a plain string target name, or a tuple of a target name and build flags, similar to the format of `targets`.
if type(target) == "list":
target_name, flags = target
else:
target_name = target
flags = ""

target_data = (_make_label_absolute(target_name), flags)

# Targets may appear in multiple collections. We don't want duplicates in the final list, but Starlark doesn't have Python's set class. So we de-duplicate manually.
if target_data not in target_collection_targets_list:
target_collection_targets_list.append(target_data)

# Assemble the association between target collections and their targets.
for collection, targets_for_collection in target_collections.items():
serialized_targets = []

for target in targets_for_collection:
if type(target) == "list":
# If the target has flags, concat them with the target name. That's how we'll associate compile commands with collections on the Python side.
serialized_targets.append("".join(target))
else:
serialized_targets.append(target)

serialized_target_collections[collection] = serialized_targets

target_collection_targets = {"{}{}".format(target, flags): [target, flags] for target, flags in target_collection_targets_list}

# 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.
if not targets: # Default to all targets in main workspace
targets = {"@//...": ""}
if not targets and not target_collections: # Default to all targets in main workspace
targets = {"@//...": ["@//...", ""]}
elif not targets: # In this case, targets were defined only in `target_collections`
targets = target_collection_targets
elif type(targets) == "select": # Allow select: https://bazel.build/reference/be/functions#select
# Pass select() to _expand_template to make it work
# see https://bazel.build/docs/configurable-attributes#faq-select-macro
pass
elif type(targets) == "list": # Allow specifying a list of targets w/o arguments
targets = {target: "" for target in targets}
absolute_targets = [_make_label_absolute(target) for target in targets]
targets = {target: [target, ""] for target in absolute_targets} | target_collection_targets
elif type(targets) != "dict": # Assume they've supplied a single string/label and wrap it
targets = {targets: ""}

# Make any package-relative labels absolute
targets = {
target if target.startswith("/") or target.startswith("@") else "{}//{}:{}".format(native.repository_name(), native.package_name(), target.removeprefix(":")): flags for target, flags in targets.items()
}
targets = {targets: [targets, ""]} | target_collection_targets
else: # Assume that they've provided a dict of targets with flags
absolute_targets = {_make_label_absolute(target): flags for target, flags in targets.items()}
targets = {"{}{}".format(target, flags): [target, flags] for target, flags in absolute_targets.items()} | target_collection_targets

# Create a wrapper script that prints a helpful error message if the python version is too old, generated from check_python_version.template.py
version_checker_script_name = name + ".check_python_version.py"
_check_python_version(name = version_checker_script_name, to_run = name)

# 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, labels_to_collections = serialized_target_collections, out_dir = out_dir, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, **kwargs)

# Combine them so the wrapper calls the main script
native.py_binary(
Expand All @@ -101,6 +218,13 @@ def refresh_compile_commands(
**kwargs
)

def _make_label_absolute(label):
# Make any package-relative labels absolute
return label if label.startswith("/") or label.startswith("@") else "{}//{}:{}".format(native.repository_name(), native.package_name(), label.removeprefix(":"))

def _expand_target_collection_targets(targets):
return "[\n" + "\n".join([" '{}',".format(target) for target in targets]) + "\n ]"

def _expand_template_impl(ctx):
"""Inject targets of interest--and other settings--into refresh.template.py, and set it up to be run."""
script = ctx.actions.declare_file(ctx.attr.name)
Expand All @@ -111,7 +235,9 @@ def _expand_template_impl(ctx):
substitutions = {
# Note, don't delete whitespace. Correctly doing multiline indenting.
" {target_flag_pairs}": "\n".join([" {},".format(pair) for pair in ctx.attr.labels_to_flags.items()]),
" {target_collections}": "\n".join([" '{}': {},".format(collection_name, _expand_target_collection_targets(targets)) for collection_name, targets in ctx.attr.labels_to_collections.items()]),
" {windows_default_include_paths}": "\n".join([" %r," % path for path in find_cpp_toolchain(ctx).built_in_include_directories]), # find_cpp_toolchain is from https://docs.bazel.build/versions/main/integrating-with-rules-cc.html
"{out_dir}": repr(ctx.attr.out_dir),
"{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),
Expand All @@ -121,7 +247,9 @@ def _expand_template_impl(ctx):

_expand_template = rule(
attrs = {
"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.
"labels_to_flags": attr.string_list_dict(mandatory = True), # string keys instead of label_keyed because Bazel doesn't support parsing wildcard target patterns (..., *, :all) in BUILD attributes.
"labels_to_collections": attr.string_list_dict(),
"out_dir": attr.string(default = "."),
"exclude_external_sources": attr.bool(default = False),
"exclude_headers": attr.string(values = ["all", "external", ""]), # "" needed only for compatibility with Bazel < 3.6.0
"_script_template": attr.label(allow_single_file = True, default = "refresh.template.py"),
Expand Down

0 comments on commit 1a7cde6

Please sign in to comment.