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( 18 "@com_github_grpc_grpc//bazel:python_rules.bzl", 19 "py_grpc_library", 20 "py_proto_library", 21) 22load("@rules_proto//proto:defs.bzl", "proto_library") 23load("@rules_python//python:defs.bzl", "py_library", "py_test") 24load("//:python_rules_test.bzl", "python_rules_test_suite") 25 26package( 27 default_testonly = 1, 28) 29 30proto_library( 31 name = "helloworld_proto", 32 srcs = ["helloworld.proto"], 33 deps = [ 34 ":hello_dep_proto", 35 "@com_google_protobuf//:duration_proto", 36 "@com_google_protobuf//:timestamp_proto", 37 ], 38) 39 40# Test that .proto files can be located in strict subdiretories of the package. 41proto_library( 42 name = "hello_dep_proto", 43 srcs = ["subdir/hello_dep.proto"], 44) 45 46py_proto_library( 47 name = "helloworld_py_pb2", 48 deps = [":helloworld_proto"], 49) 50 51py_grpc_library( 52 name = "helloworld_py_pb2_grpc", 53 srcs = [":helloworld_proto"], 54 deps = [":helloworld_py_pb2"], 55) 56 57py_proto_library( 58 name = "duration_py_pb2", 59 deps = ["@com_google_protobuf//:duration_proto"], 60) 61 62py_proto_library( 63 name = "timestamp_py_pb2", 64 deps = ["@com_google_protobuf//:timestamp_proto"], 65) 66 67py_test( 68 name = "import_test", 69 srcs = ["helloworld.py"], 70 main = "helloworld.py", 71 python_version = "PY3", 72 deps = [ 73 ":duration_py_pb2", 74 ":helloworld_py_pb2", 75 ":helloworld_py_pb2_grpc", 76 ":timestamp_py_pb2", 77 ], 78) 79 80# Test compatibility of py_proto_library and py_grpc_library rules with 81# proto_library targets as deps when the latter use import_prefix and/or 82# strip_import_prefix arguments 83# 84# See namespaced/upper/example for more encompassing tests. 85proto_library( 86 name = "helloworld_moved_proto", 87 srcs = ["helloworld.proto"], 88 import_prefix = "google/cloud", 89 strip_import_prefix = "", 90 deps = [ 91 ":hello_dep_proto", 92 "@com_google_protobuf//:duration_proto", 93 "@com_google_protobuf//:timestamp_proto", 94 ], 95) 96 97py_proto_library( 98 name = "helloworld_moved_py_pb2", 99 deps = [":helloworld_moved_proto"], 100) 101 102py_grpc_library( 103 name = "helloworld_moved_py_pb2_grpc", 104 srcs = [":helloworld_moved_proto"], 105 deps = [":helloworld_moved_py_pb2"], 106) 107 108py_test( 109 name = "import_moved_test", 110 srcs = ["helloworld_moved.py"], 111 main = "helloworld_moved.py", 112 python_version = "PY3", 113 deps = [ 114 ":duration_py_pb2", 115 ":helloworld_moved_py_pb2", 116 ":helloworld_moved_py_pb2_grpc", 117 ":timestamp_py_pb2", 118 ], 119) 120 121# Test that a py_proto_library wrapping a proto_library in another package can 122# be imported from the package that contains the py_proto_library *AND* from 123# the package that contains the proto_library. 124py_proto_library( 125 name = "subpackage_py_pb2", 126 deps = ["//in_subpackage:subpackage_proto"], 127) 128 129py_test( 130 name = "import_from_this_package_subpackage_test", 131 srcs = ["import_from_this_package.py"], 132 main = "import_from_this_package.py", 133 python_version = "PY3", 134 deps = [ 135 ":subpackage_py_pb2", 136 ], 137) 138 139py_test( 140 name = "import_from_proto_library_package_test", 141 srcs = ["import_from_proto_library_package.py"], 142 main = "import_from_proto_library_package.py", 143 python_version = "PY3", 144 deps = [ 145 ":subpackage_py_pb2", 146 ], 147) 148 149py_test( 150 name = "import_from_grpcio_reflection_test", 151 srcs = ["import_from_grpcio_reflection.py"], 152 main = "import_from_grpcio_reflection.py", 153 python_version = "PY3", 154 deps = [ 155 "@com_github_grpc_grpc//src/python/grpcio_reflection/grpc_reflection/v1alpha:grpc_reflection", 156 ], 157) 158 159# Test that a py_proto_library can be successfully imported without requiring 160# explicit dependencies on unused dependencies of the generated code. 161py_test( 162 name = "transitive_proto_dep_test", 163 srcs = ["transitive_proto_dep.py"], 164 main = "transitive_proto_dep.py", 165 python_version = "PY3", 166 deps = [ 167 ":helloworld_py_pb2", 168 ], 169) 170 171python_rules_test_suite( 172 name = "python_rules_test", 173) 174 175# Test that grpc_library attribute replaces grpcio dependency on 176# py_grpc_library targets 177 178py_library( 179 name = "grpc_library_replacement", 180 srcs = ["grpc_library_replacement.py"], 181) 182 183py_grpc_library( 184 name = "helloworld_py_pb2_grpc_library_changed", 185 srcs = [":helloworld_proto"], 186 grpc_library = ":grpc_library_replacement", 187 deps = [":helloworld_py_pb2"], 188) 189 190py_test( 191 name = "grpc_library_replacement_test", 192 srcs = ["grpc_library_replacement_test.py"], 193 main = "grpc_library_replacement_test.py", 194 python_version = "PY3", 195 deps = [ 196 ":helloworld_py_pb2_grpc_library_changed", 197 ], 198) 199