1// Copyright 2022 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 15package { 16 default_applicable_licenses: ["external_pigweed_license"], 17} 18 19cc_library_static { 20 name: "pw_protobuf", 21 cpp_std: "c++20", 22 vendor_available: true, 23 export_include_dirs: ["public"], 24 defaults: [ 25 "pw_android_common_backends", 26 ], 27 header_libs: [ 28 "fuchsia_sdk_lib_fit", 29 "fuchsia_sdk_lib_stdcompat", 30 "pw_assert", 31 "pw_log", 32 ], 33 export_header_lib_headers: [ 34 "pw_assert", 35 "pw_log", 36 ], 37 host_supported: true, 38 srcs: [ 39 "decoder.cc", 40 "encoder.cc", 41 "find.cc", 42 "map_utils.cc", 43 "message.cc", 44 "stream_decoder.cc", 45 ], 46 static_libs: [ 47 "pw_bytes", 48 "pw_containers", 49 "pw_function", 50 "pw_polyfill", 51 "pw_preprocessor", 52 "pw_result", 53 "pw_span", 54 "pw_status", 55 "pw_stream", 56 "pw_string", 57 "pw_toolchain", 58 "pw_varint", 59 ], 60 export_static_lib_headers: [ 61 "pw_bytes", 62 "pw_containers", 63 "pw_function", 64 "pw_preprocessor", 65 "pw_result", 66 "pw_span", 67 "pw_status", 68 "pw_stream", 69 "pw_toolchain", 70 "pw_varint", 71 ], 72} 73 74cc_library_headers { 75 name: "pw_protobuf_pwpb", 76 cpp_std: "c++20", 77 vendor_available: true, 78 host_supported: true, 79 generated_headers: [ 80 "google_protobuf_descriptor_pwpb_h", 81 "pw_protobuf_protos_common_pwpb_h", 82 ], 83 export_generated_headers: [ 84 "google_protobuf_descriptor_pwpb_h", 85 "pw_protobuf_protos_common_pwpb_h", 86 ], 87} 88 89genrule { 90 name: "pw_protobuf_protos_common_nanopb_h", 91 srcs: [":pw_protobuf_common_proto_with_prefix"], 92 cmd: "python3 $(location pw_protobuf_compiler_py) " + 93 "--proto-path=external/pigweed/pw_protobuf/ " + 94 "--out-dir=$$(dirname $(location pw_protobuf_protos/common.pb.h)) " + 95 "--plugin-path=$(location protoc-gen-nanopb) " + 96 "--compile-dir=$$(dirname $(in)) " + 97 "--sources $(in) " + 98 "--language nanopb " + 99 "--no-experimental-proto3-optional " + 100 "--protoc=$(location aprotoc) ", 101 out: [ 102 "pw_protobuf_protos/common.pb.h", 103 ], 104 tools: [ 105 "aprotoc", 106 "protoc-gen-nanopb", 107 "pw_protobuf_compiler_py", 108 ], 109} 110 111// Copies the proto files to a prefix directory to add the prefix to the 112// compiled proto. The prefix is taken from the directory name of the first 113// item listen in out. 114genrule_defaults { 115 name: "pw_protobuf_add_prefix_to_proto", 116 cmd: "out_files=($(out)); prefix=$$(dirname $${out_files[0]}); " + 117 "mkdir -p $${prefix}; cp -t $${prefix} $(in);", 118} 119 120filegroup { 121 name: "pw_protobuf_common_proto", 122 srcs: [ 123 "pw_protobuf_protos/common.proto", 124 ], 125} 126 127genrule { 128 name: "pw_protobuf_common_proto_with_prefix", 129 defaults: ["pw_protobuf_add_prefix_to_proto"], 130 srcs: [ 131 "pw_protobuf_protos/common.proto", 132 ], 133 out: [ 134 "pw_protobuf/pw_protobuf_protos/common.proto", 135 ], 136} 137 138genrule { 139 name: "pw_protobuf_protos_common_pwpb_h", 140 srcs: [":pw_protobuf_common_proto_with_prefix"], 141 cmd: "python3 $(location pw_protobuf_compiler_py) " + 142 "--proto-path=external/pigweed/pw_protobuf/ " + 143 "--proto-path=external/protobuf/src/ " + 144 "--out-dir=$$(dirname $(location pw_protobuf_protos/common.pwpb.h)) " + 145 "--plugin-path=$(location pw_protobuf_plugin_py) " + 146 "--compile-dir=$$(dirname $(in)) " + 147 "--sources $(in) " + 148 "--language pwpb " + 149 "--no-experimental-proto3-optional " + 150 "--protoc=$(location aprotoc) ", 151 out: [ 152 "pw_protobuf_protos/common.pwpb.h", 153 ], 154 tools: [ 155 "aprotoc", 156 "pw_protobuf_plugin_py", 157 "pw_protobuf_compiler_py", 158 ], 159} 160 161// Generate the google/protobuf/descriptor.pwpb.h which is commonly imported. 162genrule { 163 name: "google_protobuf_descriptor_pwpb_h", 164 // The libprotobuf-internal-descriptor-proto filegroup is unavailable so 165 // instead filter for just the descriptor.proto. 166 srcs: [":libprotobuf-internal-protos"], 167 cmd: "in_files=($(in)); compile_dir=$$(dirname $${in_files[0]}); " + 168 "proto_files=(); " + 169 "for f in \"$${in_files[@]}\"; do " + 170 "if [[ \"$${f##*descriptor.}\" == \"proto\" ]]; then " + 171 "proto_files+=(\"$${f}\"); fi; done; " + 172 "python3 $(location pw_protobuf_compiler_py) " + 173 "--proto-path=external/protobuf/src/ " + 174 "--out-dir=$$(dirname $(location google/protobuf/descriptor.pwpb.h)) " + 175 "--plugin-path=$(location pw_protobuf_plugin_py) " + 176 "--compile-dir=$${compile_dir} " + 177 "--sources $${proto_files} " + 178 "--language pwpb " + 179 "--no-experimental-proto3-optional " + 180 "--protoc=$(location aprotoc) ", 181 out: [ 182 "google/protobuf/descriptor.pwpb.h", 183 ], 184 tools: [ 185 "aprotoc", 186 "pw_protobuf_plugin_py", 187 "pw_protobuf_compiler_py", 188 ], 189} 190 191genrule { 192 name: "pw_protobuf_codegen_protos_py", 193 srcs: ["pw_protobuf_codegen_protos/codegen_options.proto"], 194 cmd: "$(location aprotoc) " + 195 "-I$$(dirname $(in)) " + 196 "--python_out=$(genDir) " + 197 "$(in)", 198 out: [ 199 "codegen_options_pb2.py", 200 ], 201 tools: [ 202 "aprotoc", 203 ], 204} 205 206python_library_host { 207 name: "pw_protobuf_codegen_protos_py_lib", 208 srcs: [ 209 ":pw_protobuf_codegen_protos_py", 210 ], 211 pkg_path: "pw_protobuf_codegen_protos", 212} 213 214genrule { 215 name: "pw_protobuf_protos_py", 216 srcs: [ 217 "pw_protobuf_protos/common.proto", 218 "pw_protobuf_protos/field_options.proto", 219 "pw_protobuf_protos/status.proto", 220 ":libprotobuf-internal-protos", 221 ], 222 cmd: "$(location aprotoc) " + 223 "-I$$(dirname $(location pw_protobuf_protos/common.proto)) " + 224 "-Iexternal/protobuf/src/ " + 225 "--python_out=$(genDir) " + 226 "$(in)", 227 out: [ 228 "common_pb2.py", 229 "field_options_pb2.py", 230 "status_pb2.py", 231 ], 232 tools: [ 233 "aprotoc", 234 ], 235} 236 237python_library_host { 238 name: "pw_protobuf_protos_py_lib", 239 srcs: [ 240 ":pw_protobuf_protos_py", 241 ], 242 pkg_path: "pw_protobuf_protos", 243} 244