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