1# Copyright 2020 The TensorFlow Authors. All rights reserved. 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# http://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""" 16Utilities for building grpc and proto libraries from googleapis. 17""" 18 19load("@rules_cc//cc:defs.bzl", native_cc_proto_library = "cc_proto_library") 20load("@com_github_grpc_grpc//bazel:generate_cc.bzl", "generate_cc") 21 22def _tf_cc_headers(ctx): 23 if len(ctx.attr.deps) != 1: 24 fail("deps must have exactly 1 photo_library") 25 return [ 26 CcInfo( 27 compilation_context = ctx.attr.deps[0][CcInfo].compilation_context, 28 ), 29 DefaultInfo( 30 files = ctx.attr.deps[0][CcInfo].compilation_context.headers, 31 ), 32 ] 33 34tf_cc_headers = rule( 35 implementation = _tf_cc_headers, 36 attrs = { 37 "deps": attr.label_list(providers = [CcInfo]), 38 }, 39) 40 41def cc_proto_library(name, deps): 42 """Generates a cc library and a header only cc library from a proto library 43 44 Args: 45 name: the name of the cc_library 46 deps: a list that contains exactly one proto_library 47 """ 48 native_cc_proto_library( 49 name = name, 50 deps = deps, 51 visibility = ["//visibility:public"], 52 ) 53 tf_cc_headers( 54 name = name + "_headers_only", 55 deps = [":" + name], 56 visibility = ["//visibility:public"], 57 ) 58 59def cc_grpc_library(name, srcs, deps, service_namespace = "grpc", **kwargs): 60 """Generates a cc library with grpc implementation and cc proto headers 61 62 Args: 63 name: the name of the cc_grpc_library to be created 64 srcs: the proto_libraries used to generate the cc_grpc_library 65 deps: the dependencies used to link into this cc_grpc_library, defined by 66 cc_proto_library 67 **kwargs: other args not used, for compatibility only 68 """ 69 if len(srcs) != 1: 70 fail("srcs must have exactly 1 photo_library", "srcs") 71 codegen_grpc_target = "_" + name + "_grpc_codegen" 72 generate_cc( 73 name = codegen_grpc_target, 74 srcs = srcs, 75 flags = [ 76 "services_namespace=" + service_namespace, 77 ], 78 plugin = "@com_github_grpc_grpc//src/compiler:grpc_cpp_plugin", 79 well_known_protos = True, 80 generate_mocks = True, 81 ) 82 83 grpc_proto_dep = "@com_github_grpc_grpc//:grpc++_codegen_proto" 84 native.cc_library( 85 name = name, 86 srcs = [":" + codegen_grpc_target], 87 hdrs = [":" + codegen_grpc_target], 88 deps = [dep + "_headers_only" for dep in deps] + [grpc_proto_dep], 89 visibility = ["//visibility:public"], 90 ) 91