• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  config("disable_warnings") {
37    cflags = [
38      # Fixed in upstream (https://github.com/nanopb/nanopb/pull/1022), but that
39      # PR is not yet in any released version of nanopb (as of 0.4.9.1).
40      "-Wno-switch-enum",
41    ]
42    visibility = [ ":*" ]
43  }
44
45  pw_source_set("nanopb") {
46    public_configs = [
47      ":disable_warnings",
48      ":includes",
49    ]
50    public = [
51      "$dir_pw_third_party_nanopb/pb.h",
52      "$dir_pw_third_party_nanopb/pb_common.h",
53      "$dir_pw_third_party_nanopb/pb_decode.h",
54      "$dir_pw_third_party_nanopb/pb_encode.h",
55    ]
56    public_deps = [ pw_third_party_nanopb_CONFIG ]
57    sources = [
58      "$dir_pw_third_party_nanopb/pb_common.c",
59      "$dir_pw_third_party_nanopb/pb_decode.c",
60      "$dir_pw_third_party_nanopb/pb_encode.c",
61    ]
62  }
63
64  pw_proto_library("proto") {
65    strip_prefix = "$dir_pw_third_party_nanopb/generator/proto"
66    sources = [ "$dir_pw_third_party_nanopb/generator/proto/nanopb.proto" ]
67    python_module_as_package = "nanopb_pb2"
68  }
69
70  # Generates nanopb_pb2.py, which is needed to compile protobufs with Nanopb.
71  pw_python_script("generate_nanopb_proto") {
72    sources = [ "generate_nanopb_proto.py" ]
73    pylintrc = "$dir_pigweed/.pylintrc"
74    mypy_ini = "$dir_pigweed/.mypy.ini"
75    ruff_toml = "$dir_pigweed/.ruff.toml"
76
77    # Path to protoc binary. This variable is relative to the build directory,
78    # so it must be rebased as a source-tree-absolute path.
79    _protoc_binary_path =
80        "//" +
81        rebase_path(pw_protobuf_compiler_PROTOC_BINARY, "//", root_build_dir)
82
83    if (host_os == "win") {
84      if (get_path_info(_protoc_binary_path, "extension") != "exe" &&
85          get_path_info(_protoc_binary_path, "extension") != "bat") {
86        _protoc_binary_path += ".exe"
87      }
88    }
89
90    inputs = [
91      "$dir_pw_third_party_nanopb/pb.h",
92      "$dir_pw_third_party_nanopb/pb_common.h",
93      "$dir_pw_third_party_nanopb/pb_decode.h",
94      "$dir_pw_third_party_nanopb/pb_encode.h",
95      "$dir_pw_third_party_nanopb/pb_common.c",
96      "$dir_pw_third_party_nanopb/pb_decode.c",
97      "$dir_pw_third_party_nanopb/pb_encode.c",
98      "$dir_pw_third_party_nanopb/generator/nanopb_generator.py",
99      "$dir_pw_third_party_nanopb/generator/proto/google/protobuf",
100      "$dir_pw_third_party_nanopb/generator/proto/google/protobuf/descriptor.proto",
101      "$dir_pw_third_party_nanopb/generator/proto/__init__.py",
102      "$dir_pw_third_party_nanopb/generator/proto/nanopb.proto",
103      "$dir_pw_third_party_nanopb/generator/proto/_utils.py",
104      "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb",
105      "$dir_pw_third_party_nanopb/generator/nanopb_generator.py2",
106      "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb-py2",
107      "$dir_pw_third_party_nanopb/generator/protoc",
108      "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb.bat",
109      "$dir_pw_third_party_nanopb/generator/protoc.bat",
110      _protoc_binary_path,
111    ]
112    action = {
113      args = [
114        "--nanopb-root=" +
115            rebase_path(dir_pw_third_party_nanopb, root_build_dir),
116        "--protoc-binary=" + pw_protobuf_compiler_PROTOC_BINARY,
117      ]
118      if (pw_third_party_nanopb_AGGRESSIVE_NANOPB_PB2_REGEN) {
119        args += [ "--aggressive-regen" ]
120      }
121
122      # Nanopb writes a file to:
123      #   $dir_pw_third_party_nanopb/generator/proto/nanopb_pb2.py
124      # Since this is in the source tree, we can't track it as an output. ��
125      # Use a stamp instead.
126      stamp = true
127    }
128  }
129} else {
130  group("nanopb") {
131  }
132  pw_python_group("generate_nanopb_proto") {
133  }
134}
135
136config("disable_error_messages_config") {
137  defines = [ "PB_NO_ERRMSG=1" ]
138}
139
140pw_source_set("disable_error_messages") {
141  public_configs = [ ":disable_error_messages_config" ]
142}
143