1# gRPC Bazel BUILD file. 2# 3# Copyright 2019 The gRPC authors. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17load("@rules_proto//proto:defs.bzl", "proto_library") 18load( 19 "@com_github_grpc_grpc//bazel:python_rules.bzl", 20 "py_grpc_library", 21 "py_proto_library", 22) 23load("//:python_rules_test.bzl", "python_rules_test_suite") 24 25package( 26 default_testonly = 1, 27) 28 29proto_library( 30 name = "helloworld_proto", 31 srcs = ["helloworld.proto"], 32 deps = [ 33 ":hello_dep_proto", 34 "@com_google_protobuf//:duration_proto", 35 "@com_google_protobuf//:timestamp_proto", 36 ], 37) 38 39# Test that .proto files can be located in strict subdiretories of the package. 40proto_library( 41 name = "hello_dep_proto", 42 srcs = ["subdir/hello_dep.proto"], 43) 44 45py_proto_library( 46 name = "helloworld_py_pb2", 47 deps = [":helloworld_proto"], 48) 49 50py_grpc_library( 51 name = "helloworld_py_pb2_grpc", 52 srcs = [":helloworld_proto"], 53 deps = [":helloworld_py_pb2"], 54) 55 56py_proto_library( 57 name = "duration_py_pb2", 58 deps = ["@com_google_protobuf//:duration_proto"], 59) 60 61py_proto_library( 62 name = "timestamp_py_pb2", 63 deps = ["@com_google_protobuf//:timestamp_proto"], 64) 65 66py_test( 67 name = "import_test", 68 srcs = ["helloworld.py"], 69 main = "helloworld.py", 70 python_version = "PY3", 71 deps = [ 72 ":duration_py_pb2", 73 ":helloworld_py_pb2", 74 ":helloworld_py_pb2_grpc", 75 ":timestamp_py_pb2", 76 ], 77) 78 79# Test compatibility of py_proto_library and py_grpc_library rules with 80# proto_library targets as deps when the latter use import_prefix and/or 81# strip_import_prefix arguments 82# 83# See namespaced/upper/example for more encompassing tests. 84proto_library( 85 name = "helloworld_moved_proto", 86 srcs = ["helloworld.proto"], 87 import_prefix = "google/cloud", 88 strip_import_prefix = "", 89 deps = [ 90 ":hello_dep_proto", 91 "@com_google_protobuf//:duration_proto", 92 "@com_google_protobuf//:timestamp_proto", 93 ], 94) 95 96py_proto_library( 97 name = "helloworld_moved_py_pb2", 98 deps = [":helloworld_moved_proto"], 99) 100 101py_grpc_library( 102 name = "helloworld_moved_py_pb2_grpc", 103 srcs = [":helloworld_moved_proto"], 104 deps = [":helloworld_moved_py_pb2"], 105) 106 107py_test( 108 name = "import_moved_test", 109 srcs = ["helloworld_moved.py"], 110 main = "helloworld_moved.py", 111 python_version = "PY3", 112 deps = [ 113 ":duration_py_pb2", 114 ":helloworld_moved_py_pb2", 115 ":helloworld_moved_py_pb2_grpc", 116 ":timestamp_py_pb2", 117 ], 118) 119 120# Test that a py_proto_library wrapping a proto_library in another package can 121# be imported from the package that contains the py_proto_library *AND* from 122# the package that contains the proto_library. 123py_proto_library( 124 name = "subpackage_py_pb2", 125 deps = ["//in_subpackage:subpackage_proto"], 126) 127 128py_test( 129 name = "import_from_this_package_subpackage_test", 130 srcs = ["import_from_this_package.py"], 131 main = "import_from_this_package.py", 132 python_version = "PY3", 133 deps = [ 134 ":subpackage_py_pb2", 135 ], 136) 137 138py_test( 139 name = "import_from_proto_library_package_test", 140 srcs = ["import_from_proto_library_package.py"], 141 main = "import_from_proto_library_package.py", 142 python_version = "PY3", 143 deps = [ 144 ":subpackage_py_pb2", 145 ], 146) 147 148py_test( 149 name = "import_from_grpcio_reflection_test", 150 srcs = ["import_from_grpcio_reflection.py"], 151 main = "import_from_grpcio_reflection.py", 152 python_version = "PY3", 153 deps = [ 154 "@com_github_grpc_grpc//src/python/grpcio_reflection/grpc_reflection/v1alpha:grpc_reflection", 155 ], 156) 157 158# Test that a py_proto_library can be successfully imported without requiring 159# explicit dependencies on unused dependencies of the generated code. 160py_test( 161 name = "transitive_proto_dep_test", 162 srcs = ["transitive_proto_dep.py"], 163 main = "transitive_proto_dep.py", 164 python_version = "PY3", 165 deps = [ 166 ":helloworld_py_pb2", 167 ], 168) 169 170python_rules_test_suite( 171 name = "python_rules_test", 172) 173 174# Test that grpc_library attribute replaces grpcio dependency on 175# py_grpc_library targets 176 177py_library( 178 name = "grpc_library_replacement", 179 srcs = ["grpc_library_replacement.py"], 180) 181 182py_grpc_library( 183 name = "helloworld_py_pb2_grpc_library_changed", 184 srcs = [":helloworld_proto"], 185 grpc_library = ":grpc_library_replacement", 186 deps = [":helloworld_py_pb2"], 187) 188 189py_test( 190 name = "grpc_library_replacement_test", 191 srcs = ["grpc_library_replacement_test.py"], 192 main = "grpc_library_replacement_test.py", 193 python_version = "PY3", 194 deps = [ 195 ":helloworld_py_pb2_grpc_library_changed", 196 ], 197) 198