Nathaniel Brough | 9d9a20d | 2020-08-04 16:46:56 +0800 | [diff] [blame] | 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 | load("//pw_build:pigweed.bzl", "pw_cc_library") |
| 15 | load("@rules_proto//proto:defs.bzl", "ProtoInfo") |
| 16 | load("@rules_proto_grpc//:plugin.bzl", "ProtoPluginInfo") |
| 17 | load( |
| 18 | "@rules_proto_grpc//:aspect.bzl", |
| 19 | "ProtoLibraryAspectNodeInfo", |
| 20 | "proto_compile_aspect_attrs", |
| 21 | "proto_compile_aspect_impl", |
| 22 | "proto_compile_attrs", |
| 23 | "proto_compile_impl", |
| 24 | ) |
| 25 | |
| 26 | # Create aspect for cc_proto_compile |
| 27 | cc_proto_compile_aspect = aspect( |
| 28 | implementation = proto_compile_aspect_impl, |
| 29 | provides = [ProtoLibraryAspectNodeInfo], |
| 30 | attr_aspects = ["deps"], |
| 31 | attrs = dict( |
| 32 | proto_compile_aspect_attrs, |
| 33 | _plugins = attr.label_list( |
| 34 | doc = "List of protoc plugins to apply", |
| 35 | providers = [ProtoPluginInfo], |
| 36 | default = [ |
| 37 | Label("@pigweed//pw_protobuf:pw_cc_plugin"), |
Nathaniel Brough | b4f4e7a | 2021-05-06 14:49:55 +0800 | [diff] [blame^] | 38 | Label("@pigweed//pw_rpc:pw_cc_plugin"), |
Nathaniel Brough | 9d9a20d | 2020-08-04 16:46:56 +0800 | [diff] [blame] | 39 | ], |
| 40 | ), |
| 41 | _prefix = attr.string( |
| 42 | doc = "String used to disambiguate aspects when generating outputs", |
| 43 | default = "cc_proto_compile_aspect", |
| 44 | ), |
| 45 | ), |
| 46 | toolchains = [str(Label("@rules_proto_grpc//protobuf:toolchain_type"))], |
| 47 | ) |
| 48 | |
| 49 | # Create compile rule to apply aspect |
| 50 | _rule = rule( |
| 51 | implementation = proto_compile_impl, |
| 52 | attrs = dict( |
| 53 | proto_compile_attrs, |
| 54 | deps = attr.label_list( |
| 55 | mandatory = True, |
| 56 | providers = [ProtoInfo, ProtoLibraryAspectNodeInfo], |
| 57 | aspects = [cc_proto_compile_aspect], |
| 58 | ), |
| 59 | ), |
| 60 | ) |
| 61 | |
| 62 | # Create macro for converting attrs and passing to compile |
| 63 | def _cc_proto_compile(**kwargs): |
| 64 | _rule( |
| 65 | verbose_string = "{}".format(kwargs.get("verbose", 0)), |
| 66 | merge_directories = True, |
| 67 | **{k: v for k, v in kwargs.items() if k != "merge_directories"} |
| 68 | ) |
| 69 | |
| 70 | def pw_proto_library(**kwargs): |
| 71 | """ Embedded friendly replacement for native.cc_proto_library |
| 72 | |
| 73 | This Protobuf implementation is designed to run on embedded |
| 74 | computers. Because of this the cc API differs from the standard |
| 75 | Protobuf cc plugin. The generated headers in this library are not a drop in |
| 76 | replacement for the standard cc_proto_library. |
| 77 | |
| 78 | Args: |
| 79 | **kwargs: Equivalent inputs to cc_proto_library |
| 80 | """ |
| 81 | |
| 82 | # Compile protos |
| 83 | name_pb = kwargs.get("name") + "_pb" |
| 84 | _cc_proto_compile( |
| 85 | name = name_pb, |
| 86 | # Forward deps and verbose tags to implementation |
| 87 | **{k: v for (k, v) in kwargs.items() if k in ("deps", "verbose")} |
| 88 | ) |
| 89 | |
| 90 | # Create cc_library |
| 91 | pw_cc_library( |
| 92 | name = kwargs.get("name"), |
| 93 | srcs = [name_pb], |
| 94 | deps = [ |
| 95 | "@pigweed//pw_protobuf", |
| 96 | ], |
| 97 | includes = [name_pb], |
| 98 | strip_include_prefix = ".", |
| 99 | visibility = kwargs.get("visibility"), |
| 100 | linkstatic = 1, |
| 101 | ) |