Skip to content

Commit

Permalink
feat(bazel): create toolchain alias rule for browser metadata targets
Browse files Browse the repository at this point in the history
Creates a toolchain alais rule for exposing template variables extracted
from browser metadata (such as chromium or firefox)

These toolchain aliases can then be consumed in the `toolchains`
attribute of _all_ rules (as this is a common attribute supported
by all rules in Bazel). The toolchain aliases within the `toolchains`
attribute of rules, such as a `genrule` can then be used to access
certain named files through Make variable expansion within Bazel.
  • Loading branch information
devversion committed Nov 5, 2021
1 parent 4c02c1b commit 8c73f43
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bazel/browsers/browser_configure.bzl
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
load("@io_bazel_rules_webtesting//web/internal:metadata.bzl", "metadata")
load("@io_bazel_rules_webtesting//web/internal:provider.bzl", "WebTestInfo")

"""Converts the specified label to a manifest path"""
NamedFilesInfo = provider(
doc = "Provider exposing the named files of an extracted browser archive.",
fields = {
"value": "Dictionary of keys and their corresponding manifest paths",
},
)

def _label_to_manifest_path(label):
"""Converts the specified label to a manifest path"""
if label.package != "":
return "%s/%s" % (label.workspace_name, label.package)
return label.workspace_name

"""Implementation of the `browser_configure` rule."""

def _browser_configure_impl(ctx):
"""Implementation of the `browser_configure` rule."""
named_files = {}
base_dir = _label_to_manifest_path(ctx.label)

Expand All @@ -32,6 +37,7 @@ def _browser_configure_impl(ctx):
return [
DefaultInfo(runfiles = ctx.runfiles(files = ctx.files.files)),
WebTestInfo(metadata = ctx.outputs.web_test_metadata),
NamedFilesInfo(value = ctx.attr.named_files),
]

"""
Expand Down
29 changes: 29 additions & 0 deletions bazel/browsers/browser_toolchain_alias.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("//bazel/browsers:browser_configure.bzl", "NamedFilesInfo")

def _browser_toolchain_alias_impl(ctx):
base_path = ctx.attr.metadata.label.workspace_name
named_files = ctx.attr.metadata[NamedFilesInfo].value
template_variables = {}

for key, value in named_files.items():
template_variables[key] = "external/%s/%s" % (base_path, value)

return [
platform_common.TemplateVariableInfo(template_variables),
]

browser_toolchain_alias = rule(
doc = """
Exposes a toolchain alias exposing the template variables for all named files
included in the browser metadata. These variables can then be consumed within Bazel
make variable expansion.
""",
implementation = _browser_toolchain_alias_impl,
attrs = {
"metadata": attr.label(
doc = """Metadata target that provides the browser named files.""",
mandatory = True,
providers = [NamedFilesInfo, DefaultInfo],
),
},
)

0 comments on commit 8c73f43

Please sign in to comment.