xref: /aosp_15_r20/external/pigweed/third_party/nanopb/BUILD.gn (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/python.gni")
18import("$dir_pw_build/target_types.gni")
19import("$dir_pw_docgen/docs.gni")
20import("$dir_pw_protobuf_compiler/proto.gni")
21import("nanopb.gni")
22
23pw_doc_group("docs") {
24  sources = [ "docs.rst" ]
25}
26
27# This file defines a GN source_set for an external installation of nanopb.
28# To use, checkout the nanopb source into a directory, then set the build arg
29# dir_pw_third_party_nanopb to point to that directory. The nanopb library
30# will be available in GN at "$dir_pw_third_party/nanopb".
31if (dir_pw_third_party_nanopb != "") {
32  config("includes") {
33    include_dirs = [ dir_pw_third_party_nanopb ]
34  }
35
36  pw_source_set("nanopb") {
37    public_configs = [ ":includes" ]
38    public = [
39      "$dir_pw_third_party_nanopb/pb.h",
40      "$dir_pw_third_party_nanopb/pb_common.h",
41      "$dir_pw_third_party_nanopb/pb_decode.h",
42      "$dir_pw_third_party_nanopb/pb_encode.h",
43    ]
44    public_deps = [ pw_third_party_nanopb_CONFIG ]
45    sources = [
46      "$dir_pw_third_party_nanopb/pb_common.c",
47      "$dir_pw_third_party_nanopb/pb_decode.c",
48      "$dir_pw_third_party_nanopb/pb_encode.c",
49    ]
50  }
51
52  pw_proto_library("proto") {
53    strip_prefix = "$dir_pw_third_party_nanopb/generator/proto"
54    sources = [ "$dir_pw_third_party_nanopb/generator/proto/nanopb.proto" ]
55    python_module_as_package = "nanopb_pb2"
56  }
57
58  # Generates nanopb_pb2.py, which is needed to compile protobufs with Nanopb.
59  pw_python_script("generate_nanopb_proto") {
60    sources = [ "generate_nanopb_proto.py" ]
61    pylintrc = "$dir_pigweed/.pylintrc"
62    mypy_ini = "$dir_pigweed/.mypy.ini"
63    ruff_toml = "$dir_pigweed/.ruff.toml"
64
65    # Path to protoc binary. This variable is relative to the build directory,
66    # so it must be rebased as a source-tree-absolute path.
67    _protoc_binary_path =
68        "//" +
69        rebase_path(pw_protobuf_compiler_PROTOC_BINARY, "//", root_build_dir)
70
71    if (host_os == "win") {
72      if (get_path_info(_protoc_binary_path, "extension") != "exe" &&
73          get_path_info(_protoc_binary_path, "extension") != "bat") {
74        _protoc_binary_path += ".exe"
75      }
76    }
77
78    inputs = [
79      "$dir_pw_third_party_nanopb/pb.h",
80      "$dir_pw_third_party_nanopb/pb_common.h",
81      "$dir_pw_third_party_nanopb/pb_decode.h",
82      "$dir_pw_third_party_nanopb/pb_encode.h",
83      "$dir_pw_third_party_nanopb/pb_common.c",
84      "$dir_pw_third_party_nanopb/pb_decode.c",
85      "$dir_pw_third_party_nanopb/pb_encode.c",
86      "$dir_pw_third_party_nanopb/generator/nanopb_generator.py",
87      "$dir_pw_third_party_nanopb/generator/proto/google/protobuf",
88      "$dir_pw_third_party_nanopb/generator/proto/google/protobuf/descriptor.proto",
89      "$dir_pw_third_party_nanopb/generator/proto/__init__.py",
90      "$dir_pw_third_party_nanopb/generator/proto/nanopb.proto",
91      "$dir_pw_third_party_nanopb/generator/proto/_utils.py",
92      "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb",
93      "$dir_pw_third_party_nanopb/generator/nanopb_generator.py2",
94      "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb-py2",
95      "$dir_pw_third_party_nanopb/generator/protoc",
96      "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb.bat",
97      "$dir_pw_third_party_nanopb/generator/protoc.bat",
98      _protoc_binary_path,
99    ]
100    action = {
101      args = [
102        "--nanopb-root=" +
103            rebase_path(dir_pw_third_party_nanopb, root_build_dir),
104        "--protoc-binary=" + pw_protobuf_compiler_PROTOC_BINARY,
105      ]
106      if (pw_third_party_nanopb_AGGRESSIVE_NANOPB_PB2_REGEN) {
107        args += [ "--aggressive-regen" ]
108      }
109
110      # Nanopb writes a file to:
111      #   $dir_pw_third_party_nanopb/generator/proto/nanopb_pb2.py
112      # Since this is in the source tree, we can't track it as an output. ��
113      # Use a stamp instead.
114      stamp = true
115    }
116  }
117} else {
118  group("nanopb") {
119  }
120  pw_python_group("generate_nanopb_proto") {
121  }
122}
123
124config("disable_error_messages_config") {
125  defines = [ "PB_NO_ERRMSG=1" ]
126}
127
128pw_source_set("disable_error_messages") {
129  public_configs = [ ":disable_error_messages_config" ]
130}
131