Bind library code generation tutorial for Bazel

This tutorial is for driver authors developing out-of-tree using the SDK who want to use the bind library code generation feature that has been explained in detail on this page:

This guide assumes familiarity with the concepts from that page.

What is the same and what is different?

Most of the concepts and samples laid out in the linked tutorial will also apply to users of the SDK.

The only differences are:

  • No Rust target is generated currently.
  • Instead of :{target_name}_cpp the C++ library (cc_library) target is :{target_name}_cc.
  • The Bazel target for the C++ library fuchsia_bind_cc_library will need to be manually added if the bind library is not from the SDK.

An example

This example will show a manually defined bind library with a dependency on an SDK bind library so that it can show using both types. The bind libraries will be used in a child driver's bind rules, and the C++ library from the bind libraries will be used in a parent driver's code when creating a child node for the child driver to bind to.

BUILD.bazel

The bind library

# This is a bind library that we manually define.
fuchsia_bind_library(
    name = "fuchsia.gizmo.library",
    srcs = [
        "testlibrary.bind",
    ],
    deps = [
        "@fuchsia_sdk//bind/fuchsia.acpi",  # An SDK bind library.
    ],
)

# We have to create the C++ library for it manually as well.
fuchsia_bind_cc_library(
    name = "fuchsia.gizmo.library_cc",
    library = ":fuchsia.gizmo.library",
    # Has to have the C++ libraries of all the deps of the bind library.
    deps = [
        "@fuchsia_sdk//bind/fuchsia.acpi:fuchsia.acpi_cc",  # An SDK bind library's C++ lib.
    ],
)

The parent driver

cc_binary(
    name = "parent_driver",
    srcs = [
        "parent-driver.cc",
        "parent-driver.h",
    ],
    linkshared = True,
    deps = [
        # This is a C++ lib from our manually created bind library.
        ":fuchsia.gizmo.library_cc",
        # This is a C++ lib from our manually created FIDL based bind library.
        ":fuchsia.gizmo.protocol_bindlib_cc",
        ":fuchsia.gizmo.protocol_cc",
        # This is a C++ lib from an SDK FIDL based bind library.
        "@fuchsia_sdk//fidl/fuchsia.device.fs:fuchsia.device.fs_bindlib_cc",
        "@fuchsia_sdk//pkg/driver2-llcpp",
        "@fuchsia_sdk//pkg/sys_component_llcpp",
    ],
)

parent-driver.cc

#include <bind/fuchsia/device/fs/cpp/bind.h>
#include <bind/fuchsia/gizmo/library/cpp/bind.h>
#include <bind/fuchsia/gizmo/protocol/cpp/bind.h>

uint32_t key_a = 1 /* BIND_PROTOCOL */;
uint32_t val_a = bind_fuchsia_acpi::BIND_PROTOCOL_DEVICE;
std::string key_b = bind_fuchsia_acpi::HID;
std::string val_b = "GOOG";
std::string key_c = bind_fuchsia_device_fs::EXPORTER;
std::string val_c = bind_fuchsia_device_fs::EXPORTER_ZIRCONTRANSPORT;
std::string key_d = bind_fuchsia_gizmo_library::NAME;
std::string val_d = "GIZMO";
std::string key_e = bind_fuchsia_gizmo_protocol::TESTINGPROTOCOL;
std::string val_e = bind_fuchsia_gizmo_protocol::TESTINGPROTOCOL_ZIRCONTRANSPORT;

child-driver.bind

using fuchsia.acpi;
using fuchsia.device.fs;
using fuchsia.gizmo.library;
using fuchsia.gizmo.protocol;

fuchsia.BIND_PROTOCOL == fuchsia.acpi.BIND_PROTOCOL.DEVICE;
fuchsia.acpi.hid == "GOOG";
fuchsia.device.fs.Exporter == fuchsia.device.fs.Exporter.ZirconTransport;
fuchsia.gizmo.library.Name == "GIZMO";
fuchsia.gizmo.protocol.TestingProtocol == fuchsia.gizmo.protocol.TestingProtocol.ZirconTransport;