Low-fat Skylark rules – saving memory with depsets

In my previous post on aspects, I used a Bazel aspect to generate a simple Makefile for a project. In particular, I passed a list of .o files up the tree like so: dotos = [ctx.label.name + “.o”] for dep in ctx.rule.attr.deps: # Create a new array by concatenating this .o with all previous .o’s.Continue reading “Low-fat Skylark rules – saving memory with depsets”

Custom, locally-sourced output filenames

Skylark lets you use templates in your output file name, e.g., this would create a file called target.timestamp: touch = rule( outputs = {“date_and_time”: “%{name}.timestamp”}, implementation = _impl, ) So if you had touch(name = “foo”) in a BUILD file and built :foo, you’d get foo.timestamp. I’d always used %{name}, but I found out theContinue reading “Custom, locally-sourced output filenames”

Communicating between Bazel rules: how to use Skylark providers

Rules in Bazel often need information from their dependencies. My previous post touched on a special case of this: figuring out what a dependency’s runfiles are. However, Skylark is actually capable of passing arbitrary information between rules using a system known as providers. Suppose we have a rule, analyze_flavors, that figures out what all ofContinue reading “Communicating between Bazel rules: how to use Skylark providers”

Collecting transitive runfiles with skylark

Bazel has a concept it calls runfiles for files that a binary uses during execution. For example, a binary might need to read in a CSV, an ssh key, or a .json file. These files are generally specified separately from your sources for a couple of reasons: Bazel can understand that it is a runtime,Continue reading “Collecting transitive runfiles with skylark”