1# Copyright 2019 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of 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, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Generates proto targets in various languages.""" 16 17load("@rules_proto//proto:defs.bzl", "proto_library") 18 19def _cc_proto_library_name_from_proto_name(name): 20 """Converts proto name to cc_proto_library name. 21 22 Several suffixes will be considered. 23 Args: 24 name: the proto name 25 Returns: 26 The cc_proto_library name. 27 """ 28 name = name.replace("-", "_") 29 if name == "proto": 30 # replace 'proto' with 'cc_proto' 31 return "cc_proto" 32 for suffix in [ 33 ".protolib", 34 "protolib", 35 "proto2lib", 36 "proto_lib", 37 "proto2", 38 "protos", 39 "proto2_lib", 40 "libproto", 41 "genproto", 42 "proto", 43 ]: 44 # replace 'suffix' or '_suffix' with '_cc_proto' 45 if name.endswith("_" + suffix): 46 return name[:-len("_" + suffix)] + "_cc_proto" 47 elif name.endswith(suffix): 48 return name[:-len(suffix)] + "_cc_proto" 49 50 # no match; simply append '_cc_proto' to the end 51 return name + "_cc_proto" 52 53def sapi_proto_library( 54 name, 55 srcs = [], 56 deps = [], 57 alwayslink = False, 58 **kwargs): 59 """Generates proto library and C++ targets. 60 61 Args: 62 name: Name for proto_library and base for the cc_proto_library name, name + 63 "_cc_proto". 64 srcs: Same as proto_library deps. 65 deps: Same as proto_library deps. 66 alwayslink: Same as cc_library. 67 **kwargs: proto_library arguments. 68 """ 69 if kwargs.get("has_services", False): 70 fail("Services are not currently supported.") 71 72 proto_library( 73 name = name, 74 srcs = srcs, 75 deps = deps, 76 ) 77 native.cc_proto_library( 78 name = name + "_sapi_cc_proto", 79 deps = [name], 80 ) 81 native.cc_library( 82 name = _cc_proto_library_name_from_proto_name(name), 83 deps = [name + "_sapi_cc_proto"], 84 alwayslink = alwayslink, 85 **kwargs 86 ) 87