BUILD 4.5 KB

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