pw_rpc: Adds Bazel support for RPC

Adds initial support for c++ RPC libraries. This change only
supports the raw RPC protocol.

Change-Id: Icdecc14f1b8be03120330094793315f807deb3ec
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/43921
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/WORKSPACE b/WORKSPACE
index 9c25067..b37b8e3 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -54,7 +54,7 @@
 protobuf_deps()
 
 # Setup tools to build custom grpc rules.
-# Regquired by: pigweed.
+# Required by: pigweed.
 # Used in modules: //pw_protobuf
 http_archive(
     name = "rules_proto_grpc",
@@ -63,7 +63,11 @@
     urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/1.0.2.tar.gz"],
 )
 
-load("@rules_proto_grpc//:repositories.bzl", "rules_proto_grpc_repos", "rules_proto_grpc_toolchains")
+load(
+    "@rules_proto_grpc//:repositories.bzl",
+    "rules_proto_grpc_repos",
+    "rules_proto_grpc_toolchains",
+)
 
 rules_proto_grpc_toolchains()
 
diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
index 8253457..27adade 100755
--- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
@@ -234,7 +234,9 @@
     '//pw_malloc_freelist/...',
     '//pw_polyfill/...',
     '//pw_preprocessor/...',
+    '//pw_protobuf/...',
     '//pw_protobuf_compiler/...',
+    '//pw_rpc/...',
     '//pw_span/...',
     '//pw_status/...',
     '//pw_stream/...',
diff --git a/pw_protobuf/BUILD b/pw_protobuf/BUILD.bazel
similarity index 91%
rename from pw_protobuf/BUILD
rename to pw_protobuf/BUILD.bazel
index 32b5877..0492731 100644
--- a/pw_protobuf/BUILD
+++ b/pw_protobuf/BUILD.bazel
@@ -18,7 +18,6 @@
     "pw_cc_library",
     "pw_cc_test",
 )
-load("@rules_python//python:defs.bzl", "py_binary")
 load("@rules_proto_grpc//:plugin.bzl", "proto_plugin")
 load("//pw_protobuf_compiler:proto.bzl", "pw_proto_library")
 
@@ -147,21 +146,12 @@
     ]),
 )
 
-py_binary(
-    name = "plugin",
-    srcs = glob(["py/pw_protobuf/*.py"]),
-    imports = ["py"],
-    main = "py/pw_protobuf/plugin.py",
-    python_version = "PY3",
-    deps = ["@com_google_protobuf//:protobuf_python"],
-)
-
 proto_plugin(
     name = "pw_cc_plugin",
     outputs = [
         "{protopath}.pwpb.h",
     ],
     protoc_plugin_name = "pwpb",
-    tool = "@pigweed//pw_protobuf:plugin",
+    tool = "@pigweed//pw_protobuf/py:plugin",
     visibility = ["//visibility:public"],
 )
diff --git a/pw_protobuf/py/BUILD.bazel b/pw_protobuf/py/BUILD.bazel
new file mode 100644
index 0000000..8607322
--- /dev/null
+++ b/pw_protobuf/py/BUILD.bazel
@@ -0,0 +1,37 @@
+# Copyright 2021 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+load("@rules_python//python:defs.bzl", "py_binary", "py_library")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])  # Apache License 2.0
+
+py_library(
+    name = "plugin_library",
+    srcs = glob(["pw_protobuf/*.py"]),
+    imports = ["."],
+    deps = ["@com_google_protobuf//:protobuf_python"],
+)
+
+py_binary(
+    name = "plugin",
+    srcs = ["pw_protobuf/plugin.py"],
+    imports = ["."],
+    python_version = "PY3",
+    deps = [
+        ":plugin_library",
+        "@com_google_protobuf//:protobuf_python",
+    ],
+)
diff --git a/pw_protobuf_compiler/docs.rst b/pw_protobuf_compiler/docs.rst
index 91c3310..0c93ae6 100644
--- a/pw_protobuf_compiler/docs.rst
+++ b/pw_protobuf_compiler/docs.rst
@@ -381,6 +381,12 @@
 .. code:: cpp
 
   #include "my_protos/bar.pwpb.h"
+  // and/or RPC headers
+  #include "my_protos/bar.raw_rpc.pb.h
+
+.. note::
+
+  Currently only raw RPC is supported by the Bazel build.
 
 **Supported Codegen**
 
@@ -388,6 +394,5 @@
 sub-targets generated by a ``pw_proto_library``.
 
 * ``${NAME}.pwpb`` - Generated C++ pw_protobuf code
-* ``${NAME}.nanopb`` - Generated C++ nanopb code (requires Nanopb)
-* ``${NAME}.nanopb_rpc`` - Generated C++ Nanopb pw_rpc code (requires Nanopb)
 * ``${NAME}.raw_rpc`` - Generated C++ raw pw_rpc code (no protobuf library)
+
diff --git a/pw_protobuf_compiler/proto.bzl b/pw_protobuf_compiler/proto.bzl
index 5273c46..9bcdbd1 100644
--- a/pw_protobuf_compiler/proto.bzl
+++ b/pw_protobuf_compiler/proto.bzl
@@ -35,6 +35,7 @@
             providers = [ProtoPluginInfo],
             default = [
                 Label("@pigweed//pw_protobuf:pw_cc_plugin"),
+                Label("@pigweed//pw_rpc:pw_cc_plugin"),
             ],
         ),
         _prefix = attr.string(
diff --git a/pw_protobuf_compiler/py/BUILD.bazel b/pw_protobuf_compiler/py/BUILD.bazel
new file mode 100644
index 0000000..9fe72cd
--- /dev/null
+++ b/pw_protobuf_compiler/py/BUILD.bazel
@@ -0,0 +1,31 @@
+# Copyright 2020 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+load("@rules_python//python:defs.bzl", "py_library")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])  # Apache License 2.0
+
+py_library(
+    name = "pw_protobuf_compiler",
+    srcs = [
+        "pw_protobuf_compiler/__init__.py",
+        "pw_protobuf_compiler/generate_protos.py",
+        "pw_protobuf_compiler/proto_target_invalid.py",
+        "pw_protobuf_compiler/python_protos.py",
+    ],
+    imports = ["."],
+    deps = ["//pw_protobuf/py:plugin"],
+)
diff --git a/pw_rpc/BUILD b/pw_rpc/BUILD.bazel
similarity index 81%
rename from pw_rpc/BUILD
rename to pw_rpc/BUILD.bazel
index b2cd789..30759ca 100644
--- a/pw_rpc/BUILD
+++ b/pw_rpc/BUILD.bazel
@@ -12,11 +12,14 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
+load("@rules_proto//proto:defs.bzl", "proto_library")
 load(
     "//pw_build:pigweed.bzl",
     "pw_cc_library",
     "pw_cc_test",
 )
+load("@rules_proto_grpc//:plugin.bzl", "proto_plugin")
+load("//pw_protobuf_compiler:proto.bzl", "pw_proto_library")
 
 package(default_visibility = ["//visibility:public"])
 
@@ -34,6 +37,7 @@
     ],
     deps = [
         ":common",
+        "//pw_containers:intrusive_list",
     ],
 )
 
@@ -59,6 +63,8 @@
     includes = ["public"],
     deps = [
         ":common",
+        ":internal_packet_pwpb",
+        "//pw_containers",
     ],
 )
 
@@ -87,7 +93,9 @@
     ],
     includes = ["public"],
     deps = [
+        ":internal_packet_pwpb",
         "//pw_assert",
+        "//pw_bytes",
         "//pw_log",
         "//pw_span",
         "//pw_status",
@@ -112,8 +120,13 @@
         "pw_rpc_private/internal_test_utils.h",
         "pw_rpc_private/method_impl_tester.h",
     ],
-    visibility = ["//visibility:private"],
+    includes = [
+        ".",
+        "public",
+    ],
+    visibility = [":__subpackages__"],
     deps = [
+        ":client",
         ":server",
         "//pw_span",
     ],
@@ -181,8 +194,8 @@
     name = "channel_test",
     srcs = ["channel_test.cc"],
     deps = [
+        ":internal_test_utils",
         ":server",
-        ":test_utils_test_server",
     ],
 )
 
@@ -201,6 +214,7 @@
     srcs = ["client_server_test.cc"],
     deps = [
         ":client_server",
+        ":internal_test_utils",
         "//pw_rpc/raw:method_union",
     ],
 )
@@ -208,7 +222,7 @@
 proto_library(
     name = "packet_proto",
     srcs = [
-        "pw_rpc_protos/internal/packet.proto",
+        "internal/packet.proto",
     ],
 )
 
@@ -235,3 +249,35 @@
         "//pw_assert",
     ],
 )
+
+proto_library(
+    name = "internal_packet_proto",
+    srcs = ["internal/packet.proto"],
+    visibility = ["//visibility:private"],
+)
+
+pw_proto_library(
+    name = "internal_packet_pwpb",
+    deps = [":internal_packet_proto"],
+)
+
+proto_library(
+    name = "pw_rpc_test_proto",
+    srcs = ["pw_rpc_test_protos/test.proto"],
+    strip_import_prefix = "//pw_rpc",
+)
+
+pw_proto_library(
+    name = "pw_rpc_test_pwpb",
+    deps = [":pw_rpc_test_proto"],
+)
+
+proto_plugin(
+    name = "pw_cc_plugin",
+    outputs = [
+        "{protopath}.raw_rpc.pb.h",
+    ],
+    protoc_plugin_name = "raw_rpc",
+    tool = "@pigweed//pw_rpc/py:plugin",
+    visibility = ["//visibility:public"],
+)
diff --git a/pw_rpc/py/BUILD.bazel b/pw_rpc/py/BUILD.bazel
new file mode 100644
index 0000000..c1ff8d2
--- /dev/null
+++ b/pw_rpc/py/BUILD.bazel
@@ -0,0 +1,39 @@
+# Copyright 2020 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+load("@rules_python//python:defs.bzl", "py_binary")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])  # Apache License 2.0
+
+py_binary(
+    name = "plugin",
+    srcs = glob(
+        ["pw_rpc/*.py"],
+        exclude = [
+            "pw_rpc/client.py",
+            "pw_rpc/__init__.py",
+        ],
+    ),
+    imports = ["."],
+    main = "pw_rpc/plugin_raw.py",
+    python_version = "PY3",
+    deps = [
+        "//pw_protobuf/py:plugin_library",
+        "//pw_protobuf_compiler/py:pw_protobuf_compiler",
+        "//pw_status/py:pw_status",
+        "@com_google_protobuf//:protobuf_python",
+    ],
+)
diff --git a/pw_rpc/raw/BUILD b/pw_rpc/raw/BUILD.bazel
similarity index 85%
rename from pw_rpc/raw/BUILD
rename to pw_rpc/raw/BUILD.bazel
index d983b14..9d7aa28 100644
--- a/pw_rpc/raw/BUILD
+++ b/pw_rpc/raw/BUILD.bazel
@@ -30,8 +30,10 @@
     hdrs = [
         "public/pw_rpc/internal/raw_method.h",
     ],
+    includes = ["public"],
     deps = [
         "//pw_bytes",
+        "//pw_rpc:internal_packet_pwpb",
         "//pw_rpc:server",
     ],
 )
@@ -65,7 +67,9 @@
     ],
     deps = [
         ":method_union",
+        ":test_method_context",
         "//pw_protobuf",
+        "//pw_rpc:pw_rpc_test_pwpb",
     ],
 )
 
@@ -78,6 +82,7 @@
         ":method_union",
         "//pw_protobuf",
         "//pw_rpc:internal_test_utils",
+        "//pw_rpc:pw_rpc_test_pwpb",
     ],
 )
 
@@ -90,12 +95,16 @@
         ":method_union",
         "//pw_protobuf",
         "//pw_rpc:internal_test_utils",
+        "//pw_rpc:pw_rpc_test_pwpb",
     ],
 )
 
 pw_cc_test(
     name = "stub_generation_test",
     srcs = ["stub_generation_test.cc"],
-    # TODO(hepler): Figure out proto BUILD integration.
-    # deps = ["..:test_protos.raw_rpc"],
+    deps = [
+        "//pw_rpc:pw_rpc_test_pwpb",
+        "//pw_rpc:server",
+        "//pw_rpc/raw:method_union",
+    ],
 )