nanopb: Add option to set PB_NO_ERRMSG=1

This is useful to reduce RAM usage in some cases, at the cost of less
debugging info.

Testing:
- builds in downstream project, 1k savings

Change-Id: I8d70a39dc6d043d0d77ed5906ce94928c9843b30
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/72261
Reviewed-by: Alexei Frolov <frolv@google.com>
Commit-Queue: Paul Mathieu <paulmathieu@google.com>
diff --git a/docs/BUILD.gn b/docs/BUILD.gn
index ccf779e..2419973 100644
--- a/docs/BUILD.gn
+++ b/docs/BUILD.gn
@@ -168,7 +168,10 @@
 }
 
 group("third_party_docs") {
-  deps = [ "$dir_pigweed/third_party/freertos:docs" ]
+  deps = [
+    "$dir_pigweed/third_party/freertos:docs",
+    "$dir_pigweed/third_party/nanopb:docs",
+  ]
 }
 
 pw_doc_gen("docs") {
diff --git a/third_party/nanopb/BUILD.gn b/third_party/nanopb/BUILD.gn
index c718d9f..8e43b73 100644
--- a/third_party/nanopb/BUILD.gn
+++ b/third_party/nanopb/BUILD.gn
@@ -16,9 +16,14 @@
 
 import("$dir_pw_build/python.gni")
 import("$dir_pw_build/target_types.gni")
+import("$dir_pw_docgen/docs.gni")
 import("$dir_pw_protobuf_compiler/proto.gni")
 import("nanopb.gni")
 
+pw_doc_group("docs") {
+  sources = [ "docs.rst" ]
+}
+
 # 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
@@ -36,6 +41,7 @@
       "$dir_pw_third_party_nanopb/pb_decode.h",
       "$dir_pw_third_party_nanopb/pb_encode.h",
     ]
+    public_deps = [ pw_third_party_nanopb_CONFIG ]
     sources = [
       "$dir_pw_third_party_nanopb/pb_common.c",
       "$dir_pw_third_party_nanopb/pb_decode.c",
@@ -64,3 +70,11 @@
   pw_python_group("generate_nanopb_proto") {
   }
 }
+
+config("disable_error_messages_config") {
+  defines = [ "PB_NO_ERRMSG=1" ]
+}
+
+pw_source_set("disable_error_messages") {
+  public_configs = [ ":disable_error_messages_config" ]
+}
diff --git a/third_party/nanopb/docs.rst b/third_party/nanopb/docs.rst
new file mode 100644
index 0000000..1af7211
--- /dev/null
+++ b/third_party/nanopb/docs.rst
@@ -0,0 +1,29 @@
+.. _module-pw_third_party_nanopb:
+
+======
+Nanopb
+======
+
+The ``$dir_pw_third_party/nanopb/`` module contains Nanopb, a tiny protobuf
+library. It is used by :ref:`module-pw_protobuf_compiler`.
+
+----------------
+GN Build Support
+----------------
+This module provides support to compile Nanopb with GN.
+
+Follow the documentation on :ref:`module-pw_protobuf_compiler` for general
+help on how to use this.
+
+Enabling ``PB_NO_ERRMSG=1``
+---------------------------
+
+In your toolchain configuration, you can use the following:
+
+.. code-block::
+
+  pw_third_party_nanopb_CONFIG = "$dir_pw_third_party/nanopb:disable_error_messages"
+
+
+This will add ``-DPB_NO_ERRMSG=1`` to the build, which disables error messages
+as strings and may save some code space at the expense of ease of debugging.
diff --git a/third_party/nanopb/nanopb.gni b/third_party/nanopb/nanopb.gni
index 7cda7c0..110deba 100644
--- a/third_party/nanopb/nanopb.gni
+++ b/third_party/nanopb/nanopb.gni
@@ -11,10 +11,18 @@
 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 # License for the specific language governing permissions and limitations under
 # the License.
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_build/module_config.gni")
 
 declare_args() {
   # If compiling protos for nanopb, this variable is set to the path to the
   # 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 = ""
+
+  # The build target that overrides the default configuration options for this
+  # module. This should point to a source set that provides defines through a
+  # public config (which may -include a file or add defines directly).
+  pw_third_party_nanopb_CONFIG = pw_build_DEFAULT_MODULE_CONFIG
 }