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 "--no-experimental-editions " + 101 "--pwpb-no-oneof-callbacks " + 102 "--protoc=$(location aprotoc) ", 103 out: [ 104 "pw_protobuf_protos/common.pb.h", 105 ], 106 tools: [ 107 "aprotoc", 108 "protoc-gen-nanopb", 109 "pw_protobuf_compiler_py", 110 ], 111} 112 113// Copies the proto files to a prefix directory to add the prefix to the 114// compiled proto. The prefix is taken from the directory name of the first 115// item listen in out. 116genrule_defaults { 117 name: "pw_protobuf_add_prefix_to_proto", 118 cmd: "out_files=($(out)); prefix=$$(dirname $${out_files[0]}); " + 119 "mkdir -p $${prefix}; cp -t $${prefix} $(in);", 120} 121 122filegroup { 123 name: "pw_protobuf_common_proto", 124 srcs: [ 125 "pw_protobuf_protos/common.proto", 126 ], 127} 128 129genrule { 130 name: "pw_protobuf_common_proto_with_prefix", 131 defaults: ["pw_protobuf_add_prefix_to_proto"], 132 srcs: [ 133 "pw_protobuf_protos/common.proto", 134 ], 135 out: [ 136 "pw_protobuf/pw_protobuf_protos/common.proto", 137 ], 138} 139 140genrule { 141 name: "pw_protobuf_protos_common_pwpb_h", 142 srcs: [":pw_protobuf_common_proto_with_prefix"], 143 cmd: "python3 $(location pw_protobuf_compiler_py) " + 144 "--proto-path=external/pigweed/pw_protobuf/ " + 145 "--proto-path=external/protobuf/src/ " + 146 "--out-dir=$$(dirname $(location pw_protobuf_protos/common.pwpb.h)) " + 147 "--plugin-path=$(location pw_protobuf_plugin_py) " + 148 "--compile-dir=$$(dirname $(in)) " + 149 "--sources $(in) " + 150 "--language pwpb " + 151 "--no-experimental-proto3-optional " + 152 "--no-experimental-editions " + 153 "--pwpb-no-oneof-callbacks " + 154 "--protoc=$(location aprotoc) ", 155 out: [ 156 "pw_protobuf_protos/common.pwpb.h", 157 ], 158 tools: [ 159 "aprotoc", 160 "pw_protobuf_plugin_py", 161 "pw_protobuf_compiler_py", 162 ], 163} 164 165// Generate the google/protobuf/descriptor.pwpb.h which is commonly imported. 166genrule { 167 name: "google_protobuf_descriptor_pwpb_h", 168 // The libprotobuf-internal-descriptor-proto filegroup is unavailable so 169 // instead filter for just the descriptor.proto. 170 srcs: [":libprotobuf-internal-protos"], 171 cmd: "in_files=($(in)); compile_dir=$$(dirname $${in_files[0]}); " + 172 "proto_files=(); " + 173 "for f in \"$${in_files[@]}\"; do " + 174 "if [[ \"$${f##*descriptor.}\" == \"proto\" ]]; then " + 175 "proto_files+=(\"$${f}\"); fi; done; " + 176 "python3 $(location pw_protobuf_compiler_py) " + 177 "--proto-path=external/protobuf/src/ " + 178 "--out-dir=$$(dirname $(location google/protobuf/descriptor.pwpb.h)) " + 179 "--plugin-path=$(location pw_protobuf_plugin_py) " + 180 "--compile-dir=$${compile_dir} " + 181 "--sources $${proto_files} " + 182 "--language pwpb " + 183 "--no-experimental-proto3-optional " + 184 "--no-experimental-editions " + 185 "--pwpb-no-oneof-callbacks " + 186 "--protoc=$(location aprotoc) ", 187 out: [ 188 "google/protobuf/descriptor.pwpb.h", 189 ], 190 tools: [ 191 "aprotoc", 192 "pw_protobuf_plugin_py", 193 "pw_protobuf_compiler_py", 194 ], 195} 196 197genrule { 198 name: "pw_protobuf_codegen_protos_py", 199 srcs: ["pw_protobuf_codegen_protos/codegen_options.proto"], 200 cmd: "$(location aprotoc) " + 201 "-I$$(dirname $(in)) " + 202 "--python_out=$(genDir) " + 203 "$(in)", 204 out: [ 205 "codegen_options_pb2.py", 206 ], 207 tools: [ 208 "aprotoc", 209 ], 210} 211 212python_library_host { 213 name: "pw_protobuf_codegen_protos_py_lib", 214 srcs: [ 215 ":pw_protobuf_codegen_protos_py", 216 ], 217 pkg_path: "pw_protobuf_codegen_protos", 218} 219 220genrule { 221 name: "pw_protobuf_protos_py", 222 srcs: [ 223 "pw_protobuf_protos/common.proto", 224 "pw_protobuf_protos/field_options.proto", 225 "pw_protobuf_protos/status.proto", 226 ":libprotobuf-internal-protos", 227 ], 228 cmd: "$(location aprotoc) " + 229 "-I$$(dirname $(location pw_protobuf_protos/common.proto)) " + 230 "-Iexternal/protobuf/src/ " + 231 "--python_out=$(genDir) " + 232 "$(in)", 233 out: [ 234 "common_pb2.py", 235 "field_options_pb2.py", 236 "status_pb2.py", 237 ], 238 tools: [ 239 "aprotoc", 240 ], 241} 242 243python_library_host { 244 name: "pw_protobuf_protos_py_lib", 245 srcs: [ 246 ":pw_protobuf_protos_py", 247 ], 248 pkg_path: "pw_protobuf_protos", 249} 250