third_party: Add nanopb BUILD.gn file

This change creates a Pigweed third_party directory and defines a GN
build file for nanopb which exposes a pw_source_set for the nanopb
library from a build arg pointing to a local nanopb installation.

Change-Id: I6779203626abce7821dc3fcef6dcbc11c319b43c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/15763
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/modules.gni b/modules.gni
index 4c23d5b..061c05c 100644
--- a/modules.gni
+++ b/modules.gni
@@ -67,6 +67,7 @@
       get_path_info("pw_sys_io_baremetal_stm32f429", "abspath")
   dir_pw_sys_io_stdio = get_path_info("pw_sys_io_stdio", "abspath")
   dir_pw_target_runner = get_path_info("pw_target_runner", "abspath")
+  dir_pw_third_party = get_path_info("third_party", "abspath")
   dir_pw_tokenizer = get_path_info("pw_tokenizer", "abspath")
   dir_pw_toolchain = get_path_info("pw_toolchain", "abspath")
   dir_pw_trace = get_path_info("pw_trace", "abspath")
diff --git a/pw_protobuf_compiler/proto.gni b/pw_protobuf_compiler/proto.gni
index d2652b5..9ddcfa3 100644
--- a/pw_protobuf_compiler/proto.gni
+++ b/pw_protobuf_compiler/proto.gni
@@ -18,7 +18,7 @@
 import("$dir_pw_build/input_group.gni")
 import("$dir_pw_build/python_script.gni")
 import("$dir_pw_build/target_types.gni")
-import("nanopb.gni")
+import("$dir_pw_third_party/nanopb/nanopb.gni")
 declare_args() {
   # Generators with which to compile protobuf code. These are used by the
   # pw_proto_library template to determine which build targets to create.
@@ -162,8 +162,8 @@
     all_dependent_configs = [ ":$_include_config_target" ]
     deps = [ ":$_gen_target" ]
     public_deps = [
-                    dir_pw_third_party_nanopb,
                     "$dir_pw_rpc:nanopb_server",
+                    "$dir_pw_third_party/nanopb",
                   ] + invoker.gen_deps
     public = get_target_outputs(":$_gen_target")
   }
@@ -244,7 +244,7 @@
   pw_source_set(target_name) {
     all_dependent_configs = [ ":$_include_config_target" ]
     deps = [ ":$_gen_target" ]
-    public_deps = [ dir_pw_third_party_nanopb ] + invoker.gen_deps
+    public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.gen_deps
     sources = get_target_outputs(":$_gen_target")
     public = filter_include(sources, [ "*.pb.h" ])
   }
diff --git a/pw_rpc/nanopb/BUILD.gn b/pw_rpc/nanopb/BUILD.gn
index 3eca8e3..223ab01 100644
--- a/pw_rpc/nanopb/BUILD.gn
+++ b/pw_rpc/nanopb/BUILD.gn
@@ -17,7 +17,7 @@
 
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
-import("$dir_pw_protobuf_compiler/nanopb.gni")
+import("$dir_pw_third_party/nanopb/nanopb.gni")
 import("$dir_pw_unit_test/test.gni")
 config("overrides") {
   include_dirs = [ "public_overrides" ]
@@ -36,7 +36,7 @@
   public_deps = [ "..:server_library_deps" ]
 
   if (dir_pw_third_party_nanopb != "") {
-    public_deps += [ dir_pw_third_party_nanopb ]
+    public_deps += [ "$dir_pw_third_party/nanopb" ]
   }
 
   visibility = [ "../*" ]
diff --git a/targets/host/target_toolchains.gni b/targets/host/target_toolchains.gni
index 98b094f..9450d12 100644
--- a/targets/host/target_toolchains.gni
+++ b/targets/host/target_toolchains.gni
@@ -15,8 +15,8 @@
 # gn-format disable
 import("//build_overrides/pigweed.gni")
 
-import("$dir_pw_protobuf_compiler/nanopb.gni")
 import("$dir_pw_protobuf_compiler/proto.gni")
+import("$dir_pw_third_party/nanopb/nanopb.gni")
 import("$dir_pw_toolchain/host_clang/toolchains.gni")
 import("$dir_pw_toolchain/host_gcc/toolchains.gni")
 _host_common = {
diff --git a/third_party/nanopb/BUILD.gn b/third_party/nanopb/BUILD.gn
new file mode 100644
index 0000000..7d2a33a
--- /dev/null
+++ b/third_party/nanopb/BUILD.gn
@@ -0,0 +1,47 @@
+# 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.
+
+# gn-format disable
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_build/target_types.gni")
+import("nanopb.gni")
+
+# This file defines a GN source_set for an external installation of nanopb.
+# To use, checkout the nanopb source into a directory, then set the build arg
+# dir_pw_third_party_nanopb to point to that directory. The nanopb library
+# will be available in GN at "$dir_pw_third_party/nanopb".
+if (dir_pw_third_party_nanopb != "") {
+  config("includes") {
+    include_dirs = [ dir_pw_third_party_nanopb ]
+  }
+
+  pw_source_set("nanopb") {
+    public_configs = [ ":includes" ]
+    public = [
+      "$dir_pw_third_party_nanopb/pb.h",
+      "$dir_pw_third_party_nanopb/pb_common.h",
+      "$dir_pw_third_party_nanopb/pb_decode.h",
+      "$dir_pw_third_party_nanopb/pb_encode.h",
+    ]
+    sources = [
+      "$dir_pw_third_party_nanopb/pb_common.c",
+      "$dir_pw_third_party_nanopb/pb_decode.c",
+      "$dir_pw_third_party_nanopb/pb_encode.c",
+    ]
+  }
+} else {
+  group("nanopb") {
+  }
+}
diff --git a/pw_protobuf_compiler/nanopb.gni b/third_party/nanopb/nanopb.gni
similarity index 85%
rename from pw_protobuf_compiler/nanopb.gni
rename to third_party/nanopb/nanopb.gni
index 3c3be32..7cda7c0 100644
--- a/pw_protobuf_compiler/nanopb.gni
+++ b/third_party/nanopb/nanopb.gni
@@ -14,6 +14,7 @@
 
 declare_args() {
   # If compiling protos for nanopb, this variable is set to the path to the
-  # nanopb installation.
+  # nanopb installation. When set, a pw_source_set for the nanopb library is
+  # created at "$dir_pw_third_party/nanopb".
   dir_pw_third_party_nanopb = ""
 }