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 64 # Path to protoc binary. This variable is relative to the build directory, 65 # so it must be rebased as a source-tree-absolute path. 66 _protoc_binary_path = 67 "//" + 68 rebase_path(pw_protobuf_compiler_PROTOC_BINARY, "//", root_build_dir) 69 70 if (host_os == "win") { 71 if (get_path_info(_protoc_binary_path, "extension") != "exe" && 72 get_path_info(_protoc_binary_path, "extension") != "bat") { 73 _protoc_binary_path += ".exe" 74 } 75 } 76 77 inputs = [ 78 "$dir_pw_third_party_nanopb/pb.h", 79 "$dir_pw_third_party_nanopb/pb_common.h", 80 "$dir_pw_third_party_nanopb/pb_decode.h", 81 "$dir_pw_third_party_nanopb/pb_encode.h", 82 "$dir_pw_third_party_nanopb/pb_common.c", 83 "$dir_pw_third_party_nanopb/pb_decode.c", 84 "$dir_pw_third_party_nanopb/pb_encode.c", 85 "$dir_pw_third_party_nanopb/generator/nanopb_generator.py", 86 "$dir_pw_third_party_nanopb/generator/proto/google/protobuf", 87 "$dir_pw_third_party_nanopb/generator/proto/google/protobuf/descriptor.proto", 88 "$dir_pw_third_party_nanopb/generator/proto/__init__.py", 89 "$dir_pw_third_party_nanopb/generator/proto/nanopb.proto", 90 "$dir_pw_third_party_nanopb/generator/proto/_utils.py", 91 "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb", 92 "$dir_pw_third_party_nanopb/generator/nanopb_generator.py2", 93 "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb-py2", 94 "$dir_pw_third_party_nanopb/generator/protoc", 95 "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb.bat", 96 "$dir_pw_third_party_nanopb/generator/protoc.bat", 97 _protoc_binary_path, 98 ] 99 action = { 100 args = [ 101 "--nanopb-root=" + 102 rebase_path(dir_pw_third_party_nanopb, root_build_dir), 103 "--protoc-binary=" + pw_protobuf_compiler_PROTOC_BINARY, 104 ] 105 if (pw_third_party_nanopb_AGGRESSIVE_NANOPB_PB2_REGEN) { 106 args += [ "--aggressive-regen" ] 107 } 108 109 # Nanopb writes a file to: 110 # $dir_pw_third_party_nanopb/generator/proto/nanopb_pb2.py 111 # Since this is in the source tree, we can't track it as an output. 112 # Use a stamp instead. 113 stamp = true 114 } 115 } 116} else { 117 group("nanopb") { 118 } 119 pw_python_group("generate_nanopb_proto") { 120 } 121} 122 123config("disable_error_messages_config") { 124 defines = [ "PB_NO_ERRMSG=1" ] 125} 126 127pw_source_set("disable_error_messages") { 128 public_configs = [ ":disable_error_messages_config" ] 129} 130