| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Defines `driver_bind_bytecode()`, which compiles a driver's bind rules.""" | |
| load("@fuchsia_rules_common//:utils.bzl", "make_resource_struct") | |
| load("@fuchsia_rules_common//bind:providers.bzl", "FuchsiaBindLibraryInfo") | |
| load("@fuchsia_rules_common//packages:providers.bzl", "FuchsiaPackageResourcesInfo") | |
| load(":providers.bzl", "DriverBindBytecodeInfo", "DriverDmlBindInfo") | |
| visibility([ | |
| "//build/bazel/rules/...", | |
| "//examples/...", | |
| "//sdk/...", | |
| "//src/...", | |
| "//vendor/...", | |
| "//zircon/...", | |
| ]) | |
| def _driver_bind_bytecode_impl(ctx): | |
| src = ctx.file.src | |
| disable_autobind = ctx.attr.disable_autobind | |
| if ctx.attr.dml: | |
| if src: | |
| fail("{}: `src` and `dml` are mutually exclusive.".format(ctx.label)) | |
| dml_bind = ctx.attr.dml[DriverDmlBindInfo].bind | |
| if dml_bind: | |
| src = dml_bind | |
| else: | |
| # Matches GN: a DML file without bind rules disables autobind. | |
| disable_autobind = True | |
| if not src and not disable_autobind: | |
| fail("{}: needs `src`, `dml`, or `disable_autobind = True`.".format(ctx.label)) | |
| if not src and ctx.attr.deps: | |
| fail("{}: `deps` cannot be set without bind rules (`src` or a `dml` target with bind rules).".format(ctx.label)) | |
| bind_library_sources = depset( | |
| transitive = [dep[FuchsiaBindLibraryInfo].transitive_sources for dep in ctx.attr.deps], | |
| ) | |
| bytecode = ctx.actions.declare_file(ctx.attr.bind_output or (ctx.label.name + ".bindbc")) | |
| args = ctx.actions.args() | |
| args.add("compile") | |
| args.add("--lint") | |
| args.add("--output", bytecode) | |
| if disable_autobind: | |
| args.add("--disable-autobind") | |
| if src: | |
| args.add(src) | |
| # `--include` takes a variable number of values, so it comes last. | |
| args.add_all("--include", bind_library_sources, omit_if_empty = True) | |
| ctx.actions.run( | |
| executable = ctx.executable._bindc, | |
| arguments = [args], | |
| inputs = depset([src] if src else [], transitive = [bind_library_sources]), | |
| outputs = [bytecode], | |
| mnemonic = "BindcCompile", | |
| progress_message = "Compiling bind rules for %{label}", | |
| ) | |
| return [ | |
| DefaultInfo(files = depset([bytecode])), | |
| DriverBindBytecodeInfo( | |
| src = src, | |
| bind_library_sources = bind_library_sources, | |
| ), | |
| FuchsiaPackageResourcesInfo(resources = [ | |
| make_resource_struct(src = bytecode, dest = "meta/bind/" + bytecode.basename), | |
| ]), | |
| ] | |
| driver_bind_bytecode = rule( | |
| doc = """Compiles a driver's bind rules into bind bytecode. | |
| Bind rules are supplied via `src` (a standalone `.bind` file) or `dml` (a | |
| `dml_outputs()` target), which are mutually exclusive. When | |
| `disable_autobind = True` is set without `src` or `dml`, `bindc` emits | |
| empty bind bytecode with the `DISABLE_AUTOBIND` header flag so the driver | |
| never binds automatically and binds unconditionally when requested | |
| manually. Combining `disable_autobind = True` with `src` or `dml` compiles | |
| those bind rules for manual binding only. | |
| The bytecode is installed at `meta/bind/{bind_output}` in the package of | |
| the `driver_component()` that lists this target in its `deps`. Test the | |
| bind rules with `driver_bind_bytecode_test()`. | |
| GN equivalent: `driver_bind_rules()` in //build/bind/bind.gni. | |
| """, | |
| implementation = _driver_bind_bytecode_impl, | |
| attrs = { | |
| "src": attr.label( | |
| doc = """The `.bind` file with the driver's bind rules. | |
| Mutually exclusive with `dml`; optional when `disable_autobind = True`. | |
| GN equivalent: `rules` | |
| """, | |
| allow_single_file = [".bind"], | |
| ), | |
| "dml": attr.label( | |
| doc = """A `dml_outputs()` target whose generated `.bind` file provides the bind rules. | |
| Mutually exclusive with `src`; optional when `disable_autobind = True`. | |
| Can be combined with `disable_autobind = True` to compile the | |
| generated bind rules for manual binding only; if the `dml_outputs()` | |
| target itself disabled autobind (generating no `.bind` file), | |
| autobind is disabled automatically. | |
| GN equivalent: `dml_label` | |
| """, | |
| providers = [DriverDmlBindInfo], | |
| ), | |
| "deps": attr.label_list( | |
| doc = """`bind_library()` targets imported by `src` or by the `.bind` file generated by `dml`. | |
| Cannot be set when there are no bind rules. | |
| """, | |
| providers = [FuchsiaBindLibraryInfo], | |
| ), | |
| "bind_output": attr.string( | |
| doc = "Name of the bytecode file. Defaults to `{name}.bindbc`.", | |
| ), | |
| "disable_autobind": attr.bool( | |
| doc = """Disables automatic binding so the driver only binds on explicit request. | |
| May be combined with `src` or `dml` (evaluates the bind rules only | |
| on manual bind requests), or used without `src` or `dml` (emits | |
| empty `DISABLE_AUTOBIND` bytecode that binds unconditionally when | |
| manually requested). | |
| """, | |
| default = False, | |
| ), | |
| "_bindc": attr.label( | |
| default = "//tools/bindc:bin", | |
| executable = True, | |
| cfg = "exec", | |
| ), | |
| }, | |
| provides = [DriverBindBytecodeInfo, FuchsiaPackageResourcesInfo], | |
| ) |