Google celebrates Hispanic Heritage Month. See how.

FIDL-based bind libraries tutorial for Bazel

This tutorial is for driver authors developing out-of-tree using the SDK who want to use FIDL-based auto-generated bind libraries. The tutorial assumes familiarity with the following:

What is the same and what is different?

Most of the concepts and samples laid out in the linked tutorials above will apply to users of the SDK as well.

The only differences are:

  • The Bazel target for the FIDL-based bind library fuchsia_fidl_bind_library needs to be manually added if the FIDL library is not from the SDK.
  • The Bazel target for the bind library code generation target fuchsia_bind_cc_library needs to be manually added if the FIDL library is not from the SDK.

An example

The example in this section shows a manually-defined FIDL library and how to use the generated bind library and its associated generated code to create bind rules for a child driver and access constants for these bind library values from the parent driver.

BUILD.bazel

The FIDL library

# This is a FIDL library that we manually define.
fuchsia_fidl_library(
    name = "fuchsia.examples.gizmo",
    srcs = [
        "testfidl.fidl",
    ],
    library = "fuchsia.examples.gizmo",
)

# The C++ bindings for the FIDL library.
fuchsia_fidl_llcpp_library(
    name = "fuchsia.examples.gizmo_cc",
    library = ":fuchsia.examples.gizmo",
    deps = [
        "@fuchsia_sdk//pkg/fidl_cpp_wire",
    ],
)

# We have to manually create the bind library from it.
fuchsia_fidl_bind_library(
    name = "fuchsia.examples.gizmo_bindlib",
    library = ":fuchsia.examples.gizmo",
)

# We have to manually create the C++ lib for the FIDL based bind library we created.
fuchsia_bind_cc_library(
    name = "fuchsia.examples.gizmo_bindlib_cc",
    library = ":fuchsia.examples.gizmo_bindlib",
)

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.
        "//src/bind_library/lib:fuchsia.examples.gizmo.bind_cc",
        # This is a C++ lib from our manually created FIDL based bind library.
        "//src/bind_library/lib:fuchsia.examples.gizmo_bindlib_cc",
        "//src/bind_library/lib:fuchsia.examples.gizmo_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",
    ],
)

The child bind rules

fuchsia_driver_bytecode_bind_rules(
    name = "bind_bytecode",
    output = "child-driver.bindbc",
    rules = "child-driver.bind",
    deps = [
        # This bind library is one we created manually.
        "//src/bind_library/lib:fuchsia.examples.gizmo.bind",
        # This bind library is from a FIDL library that we created manually.
        "//src/bind_library/lib:fuchsia.examples.gizmo_bindlib",
        # This bind library is from an SDK FIDL library.
        "@fuchsia_sdk//fidl/fuchsia.device.fs:fuchsia.device.fs_bindlib",
    ],
)

parent-driver.cc

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

...

properties[4] = fdf::wire::NodeProperty::Builder(arena)
                    .key(fdf::wire::NodePropertyKey::WithStringValue(
                        arena, bind_fuchsia_device_fs::EXPORTER))
                    .value(fdf::wire::NodePropertyValue::WithEnumValue(
                        arena, bind_fuchsia_device_fs::EXPORTER_ZIRCONTRANSPORT))
                    .Build();
properties[5] = fdf::wire::NodeProperty::Builder(arena)
                    .key(fdf::wire::NodePropertyKey::WithStringValue(
                        arena, bind_fuchsia_examples_gizmo::TESTINGPROTOCOL))
                    .value(fdf::wire::NodePropertyValue::WithEnumValue(
                        arena, bind_fuchsia_examples_gizmo::TESTINGPROTOCOL_ZIRCONTRANSPORT))
                    .Build();

child-driver.bind

using fuchsia.device.fs;
using fuchsia.examples.gizmo;

...

fuchsia.device.fs.Exporter == fuchsia.device.fs.Exporter.ZirconTransport;
fuchsia.examples.gizmo.TestingProtocol == fuchsia.examples.gizmo.TestingProtocol.ZirconTransport;