Bazel 中以 FIDL 繫結程式庫的教學課程

本教學課程適用於使用 SDK 的驅動程式庫作者,並希望使用以 FIDL 為基礎的自動產生的繫結程式庫。本教學課程假設您已熟悉下列項目:

相同或不同之處為何?

上方連結的教學課程中提供的大部分概念和範例也適用於 SDK 使用者。

唯一的差別在於:

  • 如果 FIDL 程式庫不是來自 SDK,您必須手動新增以 FIDL 為基礎的繫結程式庫 fuchsia_fidl_bind_library 的 Bazel 目標。
  • 如果 FIDL 程式庫並非來自 SDK,則您必須手動新增繫結程式庫程式碼產生目標 fuchsia_bind_cc_library 的 Bazel 目標。

範例

本節中的範例顯示手動定義的 FIDL 程式庫,以及如何使用產生的繫結程式庫及其相關聯的程式碼,為子項驅動程式庫程式建立繫結規則,以及從父項驅動程式庫存取這些繫結程式庫值所需的常數。

建築大師

FIDL 程式庫

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

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

# We have to manually create the bind library from it.
fuchsia_fidl_bind_library(
    name = "examples.gizmo_bindlib",
    library = ":examples.gizmo",
    visibility = ["//visibility:public"],
)

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

父項驅動程式庫

fuchsia_cc_driver(
    name = "parent_driver",
    srcs = [
        "gizmo_server.h",
        "parent-driver.cc",
        "parent-driver.h",
    ],
    deps = [
        # This is a C++ lib from our manually created bind library.
        "//src/bind_library/lib:examples.gizmo.bind_cc",
        # This is a C++ lib from our manually created FIDL based bind library.
        "//src/bind_library/lib:examples.gizmo_bindlib_cc",
        "//src/bind_library/lib: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/driver_component_cpp",
    ],
)

子項繫結規則

fuchsia_driver_bind_bytecode(
    name = "bind_bytecode",
    output = "child-driver.bindbc",
    rules = "child-driver.bind",
    deps = [
        # This bind library is one we created manually.
        "//src/bind_library/lib:examples.gizmo.bind",
        # This bind library is from a FIDL library that we created manually.
        "//src/bind_library/lib: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/examples/gizmo/bind/cpp/bind.h>
#include <bind/examples/gizmo/cpp/bind.h>
#include <bind/fuchsia/device/fs/cpp/bind.h>

...

properties[4] = fdf::MakeProperty(arena, bind_fuchsia_device_fs::EXPORTER,
                                  bind_fuchsia_device_fs::EXPORTER_ZIRCONTRANSPORT);
properties[5] = fdf::MakeProperty(arena, bind_examples_gizmo::TESTINGPROTOCOL,
                                  bind_examples_gizmo::TESTINGPROTOCOL_ZIRCONTRANSPORT);

sub-driver.bind

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

...

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