| 1 |
# gRPC Bazel BUILD file. |
| 2 |
# |
| 3 |
# Copyright 2020 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 |
|
| 17 |
load("@rules_proto//proto:defs.bzl", "proto_library") |
| 18 |
load( |
| 19 |
"@com_github_grpc_grpc//bazel:python_rules.bzl", |
| 20 |
"py_grpc_library", |
| 21 |
"py_proto_library", |
| 22 |
) |
| 23 |
|
| 24 |
_IMPORT_PREFIX = "foo/bar" |
| 25 |
|
| 26 |
_STRIP_PREFIX = "/%s" % package_name() |
| 27 |
|
| 28 |
proto_library( |
| 29 |
name = "import_no_strip_proto", |
| 30 |
srcs = ["namespaced_example.proto"], |
| 31 |
import_prefix = _IMPORT_PREFIX, |
| 32 |
strip_import_prefix = None, |
| 33 |
) |
| 34 |
|
| 35 |
proto_library( |
| 36 |
name = "import_strip_proto", |
| 37 |
srcs = ["namespaced_example.proto"], |
| 38 |
import_prefix = _IMPORT_PREFIX, |
| 39 |
strip_import_prefix = _STRIP_PREFIX, |
| 40 |
) |
| 41 |
|
| 42 |
proto_library( |
| 43 |
name = "no_import_no_strip_proto", |
| 44 |
srcs = ["namespaced_example.proto"], |
| 45 |
import_prefix = None, |
| 46 |
strip_import_prefix = None, |
| 47 |
) |
| 48 |
|
| 49 |
proto_library( |
| 50 |
name = "no_import_strip_proto", |
| 51 |
srcs = ["namespaced_example.proto"], |
| 52 |
import_prefix = None, |
| 53 |
strip_import_prefix = _STRIP_PREFIX, |
| 54 |
) |
| 55 |
|
| 56 |
py_proto_library( |
| 57 |
name = "import_no_strip_py_pb2", |
| 58 |
deps = ["import_no_strip_proto"], |
| 59 |
) |
| 60 |
|
| 61 |
py_grpc_library( |
| 62 |
name = "import_no_strip_py_pb2_grpc", |
| 63 |
srcs = ["import_no_strip_proto"], |
| 64 |
deps = ["import_no_strip_py_pb2"], |
| 65 |
) |
| 66 |
|
| 67 |
py_proto_library( |
| 68 |
name = "import_strip_py_pb2", |
| 69 |
deps = ["import_strip_proto"], |
| 70 |
) |
| 71 |
|
| 72 |
py_grpc_library( |
| 73 |
name = "import_strip_py_pb2_grpc", |
| 74 |
srcs = ["import_strip_proto"], |
| 75 |
deps = ["import_strip_py_pb2"], |
| 76 |
) |
| 77 |
|
| 78 |
py_proto_library( |
| 79 |
name = "no_import_no_strip_py_pb2", |
| 80 |
deps = ["no_import_no_strip_proto"], |
| 81 |
) |
| 82 |
|
| 83 |
py_grpc_library( |
| 84 |
name = "no_import_no_strip_py_pb2_grpc", |
| 85 |
srcs = ["no_import_no_strip_proto"], |
| 86 |
deps = ["no_import_no_strip_py_pb2"], |
| 87 |
) |
| 88 |
|
| 89 |
py_proto_library( |
| 90 |
name = "no_import_strip_py_pb2", |
| 91 |
deps = ["no_import_strip_proto"], |
| 92 |
) |
| 93 |
|
| 94 |
py_grpc_library( |
| 95 |
name = "no_import_strip_py_pb2_grpc", |
| 96 |
srcs = ["no_import_strip_proto"], |
| 97 |
deps = ["no_import_strip_py_pb2"], |
| 98 |
) |
| 99 |
|
| 100 |
################# |
| 101 |
# Namespace Tests |
| 102 |
################# |
| 103 |
|
| 104 |
# Most examples with protos have all proto packages rooted at the workspace root. i.e. google/api has |
| 105 |
# a directory structure: |
| 106 |
# - WORKSPACE |
| 107 |
# - /google |
| 108 |
# - /api |
| 109 |
# - files.proto |
| 110 |
# |
| 111 |
# But if you can't anchor the protos at the root, you have to use strip and import prefixes. This results |
| 112 |
# in the following directory layout for python, which needs to properly be added to the imports. |
| 113 |
# |
| 114 |
# No Import or Strip (Can't compile if there are any proto dependencies) |
| 115 |
# bazel-out/darwin-fastbuild/bin/namespaced/upper/example/namespaced_example_pb2.py |
| 116 |
# |
| 117 |
# No import Prefix (Can't compile if there are any proto dependencies) |
| 118 |
# bazel-out/darwin-fastbuild/bin/namespaced/upper/example/_virtual_imports/namespaced_example_proto/namespaced_example_pb2.py |
| 119 |
# |
| 120 |
# No strip prefix (Can't compile if there are any proto dependencies) |
| 121 |
# bazel-out/darwin-fastbuild/bin/namespaced/upper/example/_virtual_imports/namespaced_example_proto/upper/example/namespaced/upper/example/namespaced_example_pb2.py |
| 122 |
# |
| 123 |
# Both Import and Strip |
| 124 |
# bazel-out/darwin-fastbuild/bin/namespaced/upper/example/_virtual_imports/namespaced_example_proto/upper/example/namespaced_example_pb2.py |
| 125 |
|
| 126 |
py_test( |
| 127 |
name = "import_no_strip_test", |
| 128 |
srcs = ["import_no_strip_test.py"], |
| 129 |
main = "import_no_strip_test.py", |
| 130 |
python_version = "PY3", |
| 131 |
deps = [ |
| 132 |
":import_no_strip_py_pb2", |
| 133 |
":import_no_strip_py_pb2_grpc", |
| 134 |
], |
| 135 |
) |
| 136 |
|
| 137 |
py_test( |
| 138 |
name = "import_strip_test", |
| 139 |
srcs = ["import_strip_test.py"], |
| 140 |
main = "import_strip_test.py", |
| 141 |
python_version = "PY3", |
| 142 |
deps = [ |
| 143 |
":import_strip_py_pb2", |
| 144 |
":import_strip_py_pb2_grpc", |
| 145 |
], |
| 146 |
) |
| 147 |
|
| 148 |
py_test( |
| 149 |
name = "no_import_no_strip_test", |
| 150 |
srcs = ["no_import_no_strip_test.py"], |
| 151 |
main = "no_import_no_strip_test.py", |
| 152 |
python_version = "PY3", |
| 153 |
deps = [ |
| 154 |
":no_import_no_strip_py_pb2", |
| 155 |
":no_import_no_strip_py_pb2_grpc", |
| 156 |
], |
| 157 |
) |
| 158 |
|
| 159 |
py_test( |
| 160 |
name = "no_import_strip_test", |
| 161 |
srcs = ["no_import_strip_test.py"], |
| 162 |
main = "no_import_strip_test.py", |
| 163 |
python_version = "PY3", |
| 164 |
deps = [ |
| 165 |
":no_import_strip_py_pb2", |
| 166 |
":no_import_strip_py_pb2_grpc", |
| 167 |
], |
| 168 |
) |