1# Copyright 2024 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"""Rule for generating C++ proto libraries using pw_protobuf.""" 15 16load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "use_cpp_toolchain") 17load("@com_google_protobuf//bazel/common:proto_info.bzl", "ProtoInfo") 18load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") 19load("//pw_protobuf_compiler/private:proto.bzl", "compile_proto", "proto_compiler_aspect") 20 21# For Copybara use only 22ADDITIONAL_PWPB_DEPS = [] 23 24# TODO: b/373693434 - The `oneof_callbacks` parameter is temporary to assist 25# with migration. 26def pwpb_proto_library(*, name, deps, oneof_callbacks = True, **kwargs): 27 """A C++ proto library generated using pw_protobuf. 28 29 Attributes: 30 deps: proto_library targets for which to generate this library. 31 """ 32 if oneof_callbacks: 33 _pwpb_proto_library( 34 name = name, 35 protos = deps, 36 deps = [ 37 Label("//pw_assert:assert"), 38 Label("//pw_containers:vector"), 39 Label("//pw_preprocessor"), 40 Label("//pw_protobuf"), 41 Label("//pw_result"), 42 Label("//pw_span"), 43 Label("//pw_status"), 44 Label("//pw_string:string"), 45 ] + ADDITIONAL_PWPB_DEPS, 46 **kwargs 47 ) 48 else: 49 _pwpb_legacy_oneof_proto_library( 50 name = name, 51 protos = deps, 52 deps = [ 53 Label("//pw_assert:assert"), 54 Label("//pw_containers:vector"), 55 Label("//pw_preprocessor"), 56 Label("//pw_protobuf"), 57 Label("//pw_result"), 58 Label("//pw_span"), 59 Label("//pw_status"), 60 Label("//pw_string:string"), 61 ] + ADDITIONAL_PWPB_DEPS, 62 **kwargs 63 ) 64 65_pwpb_proto_compiler_aspect = proto_compiler_aspect( 66 ["pwpb.h"], 67 "//pw_protobuf/py:plugin", 68 ["--exclude-legacy-snake-case-field-name-enums", "--no-legacy-namespace", "--options-file={}"], 69) 70 71_pwpb_proto_library = rule( 72 implementation = compile_proto, 73 attrs = { 74 "deps": attr.label_list( 75 providers = [CcInfo], 76 ), 77 "protos": attr.label_list( 78 providers = [ProtoInfo], 79 aspects = [_pwpb_proto_compiler_aspect], 80 ), 81 }, 82 fragments = ["cpp"], 83 toolchains = use_cpp_toolchain(), 84) 85 86# TODO: b/373693434 - This aspect and its corresponding rule should be removed 87# once oneof callback migration is complete. 88_pwpb_legacy_oneof_compiler_aspect = proto_compiler_aspect( 89 ["pwpb.h"], 90 "//pw_protobuf/py:plugin", 91 ["--exclude-legacy-snake-case-field-name-enums", "--no-oneof-callbacks", "--no-legacy-namespace", "--options-file={}"], 92) 93 94_pwpb_legacy_oneof_proto_library = rule( 95 implementation = compile_proto, 96 attrs = { 97 "deps": attr.label_list( 98 providers = [CcInfo], 99 ), 100 "protos": attr.label_list( 101 providers = [ProtoInfo], 102 aspects = [_pwpb_legacy_oneof_compiler_aspect], 103 ), 104 }, 105 fragments = ["cpp"], 106 toolchains = use_cpp_toolchain(), 107) 108