[bind_library] Organize components into modules

Separate driver components and libraries into individual directories.
Add method comments, clean up imports, and update the README.

Change-Id: I1ee291f10d2290d644801c04ecb2b08450b0ec76
Reviewed-on: https://fuchsia-review.googlesource.com/c/sdk-samples/drivers/+/712292
Reviewed-by: Novin Changizi <novinc@google.com>
Commit-Queue: Dave Smith <smithdave@google.com>
Reviewed-by: Suraj Malhotra <surajmalhotra@google.com>
diff --git a/src/BUILD.bazel b/src/BUILD.bazel
index 8135d79..78b4000 100644
--- a/src/BUILD.bazel
+++ b/src/BUILD.bazel
@@ -14,8 +14,8 @@
     deps = [
         "//src/acpi_multiply/controller:pkg",
         "//src/acpi_multiply/driver:pkg",
-        "//src/bind_library:child_pkg",
-        "//src/bind_library:parent_pkg",
+        "//src/bind_library/child:pkg",
+        "//src/bind_library/parent:pkg",
         "//src/composite_sample:pkg",
         "//src/example_driver:pkg",
         "//src/i2c_temperature/controller:pkg",
diff --git a/src/bind_library/BUILD.bazel b/src/bind_library/BUILD.bazel
deleted file mode 100644
index 221d224..0000000
--- a/src/bind_library/BUILD.bazel
+++ /dev/null
@@ -1,180 +0,0 @@
-# Copyright 2022 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.
-
-load(
-    "@rules_fuchsia//fuchsia:defs.bzl",
-    "fuchsia_bind_cc_library",
-    "fuchsia_bind_library",
-    "fuchsia_component_manifest",
-    "fuchsia_driver_bytecode_bind_rules",
-    "fuchsia_driver_component",
-    "fuchsia_fidl_bind_library",
-    "fuchsia_fidl_library",
-    "fuchsia_fidl_llcpp_library",
-    "fuchsia_package",
-)
-
-#[START fuchsia_gizmo_library]
-# This is a bind library that we manually define.
-fuchsia_bind_library(
-    name = "fuchsia.examples.gizmo.bind",
-    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.examples.gizmo.bind_cc",
-    library = ":fuchsia.examples.gizmo.bind",
-    # 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.
-    ],
-)
-#[END fuchsia_gizmo_library]
-
-#[START fuchsia_gizmo_protocol]
-# 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",
-)
-#[END fuchsia_gizmo_protocol]
-
-cc_binary(
-    name = "child_driver",
-    srcs = [
-        "child-driver.cc",
-        "child-driver.h",
-    ],
-    linkshared = True,
-    deps = [
-        ":fuchsia.examples.gizmo_cc",
-        "@fuchsia_sdk//pkg/driver2-llcpp",
-        "@fuchsia_sdk//pkg/sys_component_llcpp",
-    ],
-)
-
-#[START bind_rules]
-fuchsia_driver_bytecode_bind_rules(
-    name = "child_bind_bytecode",
-    output = "child-driver.bindbc",
-    rules = "child-driver.bind",
-    deps = [
-        # This bind library is one we created manually.
-        ":fuchsia.examples.gizmo.bind",
-        # This bind library is from a FIDL library that we created manually.
-        ":fuchsia.examples.gizmo_bindlib",
-        # This bind library is from an SDK FIDL library.
-        "@fuchsia_sdk//fidl/fuchsia.device.fs:fuchsia.device.fs_bindlib",
-    ],
-)
-#[END bind_rules]
-
-fuchsia_component_manifest(
-    name = "child_manifest",
-    src = "meta/child-driver.cml",
-    includes = [
-        "@fuchsia_sdk//pkg/syslog:client",
-    ],
-)
-
-fuchsia_driver_component(
-    name = "child_component",
-    bind_bytecode = ":child_bind_bytecode",
-    driver_lib = ":child_driver",
-    manifest = ":child_manifest",
-)
-
-fuchsia_package(
-    name = "child_pkg",
-    package_name = "child_pkg",
-    visibility = ["//visibility:public"],
-    deps = [
-        ":child_component",
-    ],
-)
-
-#[START 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.examples.gizmo.bind_cc",
-        # This is a C++ lib from our manually created FIDL based bind library.
-        ":fuchsia.examples.gizmo_bindlib_cc",
-        ":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",
-    ],
-)
-#[END parent_driver]
-
-fuchsia_driver_bytecode_bind_rules(
-    name = "parent_bind_bytecode",
-    output = "parent-driver.bindbc",
-    rules = "parent-driver.bind",
-    deps = [
-        "@fuchsia_sdk//bind/fuchsia.acpi",
-    ],
-)
-
-fuchsia_component_manifest(
-    name = "parent_manifest",
-    src = "meta/parent-driver.cml",
-    includes = [
-        "@fuchsia_sdk//pkg/syslog:client",
-    ],
-)
-
-fuchsia_driver_component(
-    name = "parent_component",
-    bind_bytecode = ":parent_bind_bytecode",
-    driver_lib = ":parent_driver",
-    manifest = ":parent_manifest",
-)
-
-fuchsia_package(
-    name = "parent_pkg",
-    package_name = "parent_pkg",
-    visibility = ["//visibility:public"],
-    deps = [
-        ":parent_component",
-    ],
-)
diff --git a/src/bind_library/README.md b/src/bind_library/README.md
index b439d37..a49c160 100644
--- a/src/bind_library/README.md
+++ b/src/bind_library/README.md
@@ -1,4 +1,4 @@
-# Bind library code generation driver sample
+# Bind library code generation sample
 
 This project is a sample of the bind library code generation feature. It contaains
 two drivers, a parent and child.
@@ -9,35 +9,46 @@
 The child driver will bind to this node by specifying in its bind rules
 the node properties the parent driver has specified.
 
-To run this sample you can use the QEMU emulator which can be started by following steps 1-5 from
-[start-the-emulator](https://fuchsia.dev/fuchsia-src/get-started/sdk/get-started-with-driver#start-the-emulator).
+## Building
 
-Then you can use Bazel to run both the parent and child drivers.
+To build the driver components, run the following commands:
 
-Run the parent:
-
-```posix-terminal
-bazel run --config=fuchsia_x64 //src/bind_library:parent_pkg.parent_component
+```
+tools/bazel build --config=fuchsia_x64 //src/bind_library:parent_pkg
+tools/bazel build --config=fuchsia_x64 //src/bind_library:child_pkg
 ```
 
-Confirm that the parent driver was bound to a node.
+## Running
 
-Run the child:
-```posix-terminal
-bazel run --config=fuchsia_x64 //src/bind_library:child_pkg.child_component
-```
+Use the following commands to load the driver components on a target device:
 
-Confirm that the child driver was bound to a node.
+1.  Load the parent driver component:
 
-You can check the logs of these drivers using:
+    ```
+    tools/bazel run --config=fuchsia_x64 //src/bind_library/parent:pkg.component
+    ```
 
-```posix-terminal
-ffx log --tags driver --filter universe-pkg-drivers
-```
+1.  Run the child driver component:
+
+    ```
+    tools/bazel run --config=fuchsia_x64 //src/bind_library/child:pkg.component
+    ```
+
+1.  Open the device log viewer:
+
+    ```
+    tools/ffx log --tags driver --filter universe-pkg-drivers
+    ```
 
 You should see the following logs:
 
-```none {:.devsite-disable-click-to-copy}
+```
 [parent-driver.cc:128] Received TestingProtocol.Get request.
 [child-driver.cc:46] Succeeded!
 ```
+
+## Source layout
+
+*   `parent/` — Source code of the parent driver component.
+*   `child/` — Source code of the child driver component.
+*   `lib/` — FIDL and bind libraries shared between the two components.
diff --git a/src/bind_library/child/BUILD.bazel b/src/bind_library/child/BUILD.bazel
new file mode 100644
index 0000000..f593394
--- /dev/null
+++ b/src/bind_library/child/BUILD.bazel
@@ -0,0 +1,66 @@
+# Copyright 2022 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.
+
+load(
+    "@rules_fuchsia//fuchsia:defs.bzl",
+    "fuchsia_cc_binary",
+    "fuchsia_component_manifest",
+    "fuchsia_driver_bytecode_bind_rules",
+    "fuchsia_driver_component",
+    "fuchsia_package",
+)
+
+cc_binary(
+    name = "child_driver",
+    srcs = [
+        "child-driver.cc",
+        "child-driver.h",
+    ],
+    linkshared = True,
+    deps = [
+        "//src/bind_library/lib:fuchsia.examples.gizmo_cc",
+        "@fuchsia_sdk//pkg/driver2-llcpp",
+        "@fuchsia_sdk//pkg/sys_component_llcpp",
+    ],
+)
+
+#[START 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",
+    ],
+)
+#[END bind_rules]
+
+fuchsia_component_manifest(
+    name = "manifest",
+    src = "meta/child-driver.cml",
+    includes = [
+        "@fuchsia_sdk//pkg/syslog:client",
+    ],
+)
+
+fuchsia_driver_component(
+    name = "component",
+    bind_bytecode = ":bind_bytecode",
+    driver_lib = ":child_driver",
+    manifest = ":manifest",
+)
+
+fuchsia_package(
+    name = "pkg",
+    package_name = "child_pkg",
+    visibility = ["//visibility:public"],
+    deps = [
+        ":component",
+    ],
+)
diff --git a/src/bind_library/child-driver.bind b/src/bind_library/child/child-driver.bind
similarity index 100%
rename from src/bind_library/child-driver.bind
rename to src/bind_library/child/child-driver.bind
diff --git a/src/bind_library/child-driver.cc b/src/bind_library/child/child-driver.cc
similarity index 97%
rename from src/bind_library/child-driver.cc
rename to src/bind_library/child/child-driver.cc
index f58fdad..ff39398 100644
--- a/src/bind_library/child-driver.cc
+++ b/src/bind_library/child/child-driver.cc
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "src/bind_library/child-driver.h"
+#include "child-driver.h"
 
 #include <fidl/fuchsia.examples.gizmo/cpp/wire.h>
 
diff --git a/src/bind_library/child-driver.h b/src/bind_library/child/child-driver.h
similarity index 100%
rename from src/bind_library/child-driver.h
rename to src/bind_library/child/child-driver.h
diff --git a/src/bind_library/meta/child-driver.cml b/src/bind_library/child/meta/child-driver.cml
similarity index 62%
rename from src/bind_library/meta/child-driver.cml
rename to src/bind_library/child/meta/child-driver.cml
index dcce45a..cf7048f 100644
--- a/src/bind_library/meta/child-driver.cml
+++ b/src/bind_library/child/meta/child-driver.cml
@@ -1,3 +1,6 @@
+// Copyright 2022 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.
 {
     include: [
         'syslog/client.shard.cml',
diff --git a/src/bind_library/lib/BUILD.bazel b/src/bind_library/lib/BUILD.bazel
new file mode 100644
index 0000000..be5bb7c
--- /dev/null
+++ b/src/bind_library/lib/BUILD.bazel
@@ -0,0 +1,70 @@
+# Copyright 2022 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.
+
+load(
+    "@rules_fuchsia//fuchsia:defs.bzl",
+    "fuchsia_bind_cc_library",
+    "fuchsia_bind_library",
+    "fuchsia_fidl_bind_library",
+    "fuchsia_fidl_library",
+    "fuchsia_fidl_llcpp_library",
+)
+
+# TODO(fxbug.dev/106882): Apply visibility to rules once available
+package(default_visibility = ["//visibility:public"])
+
+#[START fuchsia_gizmo_library]
+# This is a bind library that we manually define.
+fuchsia_bind_library(
+    name = "fuchsia.examples.gizmo.bind",
+    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.examples.gizmo.bind_cc",
+    library = ":fuchsia.examples.gizmo.bind",
+    # 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.
+    ],
+)
+#[END fuchsia_gizmo_library]
+
+#[START fuchsia_gizmo_protocol]
+# 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",
+)
+#[END fuchsia_gizmo_protocol]
diff --git a/src/bind_library/testfidl.fidl b/src/bind_library/lib/testfidl.fidl
similarity index 100%
rename from src/bind_library/testfidl.fidl
rename to src/bind_library/lib/testfidl.fidl
diff --git a/src/bind_library/testlibrary.bind b/src/bind_library/lib/testlibrary.bind
similarity index 100%
rename from src/bind_library/testlibrary.bind
rename to src/bind_library/lib/testlibrary.bind
diff --git a/src/bind_library/parent/BUILD.bazel b/src/bind_library/parent/BUILD.bazel
new file mode 100644
index 0000000..ee11259
--- /dev/null
+++ b/src/bind_library/parent/BUILD.bazel
@@ -0,0 +1,67 @@
+# Copyright 2022 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.
+
+load(
+    "@rules_fuchsia//fuchsia:defs.bzl",
+    "fuchsia_cc_binary",
+    "fuchsia_component_manifest",
+    "fuchsia_driver_bytecode_bind_rules",
+    "fuchsia_driver_component",
+    "fuchsia_package",
+)
+
+#[START 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",
+    ],
+)
+#[END parent_driver]
+
+fuchsia_driver_bytecode_bind_rules(
+    name = "bind_bytecode",
+    output = "parent-driver.bindbc",
+    rules = "parent-driver.bind",
+    deps = [
+        "@fuchsia_sdk//bind/fuchsia.acpi",
+    ],
+)
+
+fuchsia_component_manifest(
+    name = "manifest",
+    src = "meta/parent-driver.cml",
+    includes = [
+        "@fuchsia_sdk//pkg/syslog:client",
+    ],
+)
+
+fuchsia_driver_component(
+    name = "component",
+    bind_bytecode = ":bind_bytecode",
+    driver_lib = ":parent_driver",
+    manifest = ":manifest",
+)
+
+fuchsia_package(
+    name = "pkg",
+    package_name = "parent_pkg",
+    visibility = ["//visibility:public"],
+    deps = [
+        ":component",
+    ],
+)
diff --git a/src/bind_library/meta/parent-driver.cml b/src/bind_library/parent/meta/parent-driver.cml
similarity index 71%
rename from src/bind_library/meta/parent-driver.cml
rename to src/bind_library/parent/meta/parent-driver.cml
index be3423d..580696e 100644
--- a/src/bind_library/meta/parent-driver.cml
+++ b/src/bind_library/parent/meta/parent-driver.cml
@@ -1,3 +1,6 @@
+// Copyright 2022 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.
 {
     include: [
         'syslog/client.shard.cml',
diff --git a/src/bind_library/parent-driver.bind b/src/bind_library/parent/parent-driver.bind
similarity index 100%
rename from src/bind_library/parent-driver.bind
rename to src/bind_library/parent/parent-driver.bind
diff --git a/src/bind_library/parent-driver.cc b/src/bind_library/parent/parent-driver.cc
similarity index 98%
rename from src/bind_library/parent-driver.cc
rename to src/bind_library/parent/parent-driver.cc
index dbf8d7b..f5bfc06 100644
--- a/src/bind_library/parent-driver.cc
+++ b/src/bind_library/parent/parent-driver.cc
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "src/bind_library/parent-driver.h"
+#include "parent-driver.h"
 
 // [START bind_imports]
 #include <bind/fuchsia/device/fs/cpp/bind.h>
diff --git a/src/bind_library/parent-driver.h b/src/bind_library/parent/parent-driver.h
similarity index 100%
rename from src/bind_library/parent-driver.h
rename to src/bind_library/parent/parent-driver.h