protobuf.bzl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED
  2. def _GetPath(ctx, path):
  3. if ctx.label.workspace_root:
  4. return ctx.label.workspace_root + '/' + path
  5. else:
  6. return path
  7. def _GenDir(ctx):
  8. if not ctx.attr.includes:
  9. return ctx.label.workspace_root
  10. if not ctx.attr.includes[0]:
  11. return _GetPath(ctx, ctx.label.package)
  12. if not ctx.label.package:
  13. return _GetPath(ctx, ctx.attr.includes[0])
  14. return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0])
  15. def _CcOuts(srcs, use_grpc_plugin=False):
  16. ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] + \
  17. [s[:-len(".proto")] + ".pb.cc" for s in srcs]
  18. if use_grpc_plugin:
  19. ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] + \
  20. [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs]
  21. return ret
  22. def _PyOuts(srcs):
  23. return [s[:-len(".proto")] + "_pb2.py" for s in srcs]
  24. def _RelativeOutputPath(path, include):
  25. if include == None:
  26. return path
  27. if not path.startswith(include):
  28. fail("Include path %s isn't part of the path %s." % (include, path))
  29. if include and include[-1] != '/':
  30. include = include + '/'
  31. path = path[len(include):]
  32. if not path.startswith(PACKAGE_NAME):
  33. fail("The package %s is not within the path %s" % (PACKAGE_NAME, path))
  34. if not PACKAGE_NAME:
  35. return path
  36. return path[len(PACKAGE_NAME)+1:]
  37. def _proto_gen_impl(ctx):
  38. """General implementation for generating protos"""
  39. srcs = ctx.files.srcs
  40. deps = []
  41. deps += ctx.files.srcs
  42. gen_dir = _GenDir(ctx)
  43. if gen_dir:
  44. import_flags = ["-I" + gen_dir]
  45. else:
  46. import_flags = ["-I."]
  47. for dep in ctx.attr.deps:
  48. import_flags += dep.proto.import_flags
  49. deps += dep.proto.deps
  50. args = []
  51. if ctx.attr.gen_cc:
  52. args += ["--cpp_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
  53. if ctx.attr.gen_py:
  54. args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
  55. if ctx.executable.grpc_cpp_plugin:
  56. args += ["--plugin=protoc-gen-grpc=" + ctx.executable.grpc_cpp_plugin.path]
  57. args += ["--grpc_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
  58. if args:
  59. ctx.action(
  60. inputs=srcs + deps,
  61. outputs=ctx.outputs.outs,
  62. arguments=args + import_flags + [s.path for s in srcs],
  63. executable=ctx.executable.protoc,
  64. )
  65. return struct(
  66. proto=struct(
  67. srcs=srcs,
  68. import_flags=import_flags,
  69. deps=deps,
  70. ),
  71. )
  72. _proto_gen = rule(
  73. attrs = {
  74. "srcs": attr.label_list(allow_files = True),
  75. "deps": attr.label_list(providers = ["proto"]),
  76. "includes": attr.string_list(),
  77. "protoc": attr.label(
  78. cfg = HOST_CFG,
  79. executable = True,
  80. single_file = True,
  81. mandatory = True,
  82. ),
  83. "grpc_cpp_plugin": attr.label(
  84. cfg = HOST_CFG,
  85. executable = True,
  86. single_file = True,
  87. ),
  88. "gen_cc": attr.bool(),
  89. "gen_py": attr.bool(),
  90. "outs": attr.output_list(),
  91. },
  92. output_to_genfiles = True,
  93. implementation = _proto_gen_impl,
  94. )
  95. def cc_proto_library(
  96. name,
  97. srcs=[],
  98. deps=[],
  99. cc_libs=[],
  100. include=None,
  101. protoc="//:protoc",
  102. internal_bootstrap_hack=False,
  103. use_grpc_plugin=False,
  104. default_runtime="//:protobuf",
  105. **kargs):
  106. """Bazel rule to create a C++ protobuf library from proto source files
  107. NOTE: the rule is only an internal workaround to generate protos. The
  108. interface may change and the rule may be removed when bazel has introduced
  109. the native rule.
  110. Args:
  111. name: the name of the cc_proto_library.
  112. srcs: the .proto files of the cc_proto_library.
  113. deps: a list of dependency labels; must be cc_proto_library.
  114. cc_libs: a list of other cc_library targets depended by the generated
  115. cc_library.
  116. include: a string indicating the include path of the .proto files.
  117. protoc: the label of the protocol compiler to generate the sources.
  118. internal_bootstrap_hack: a flag indicate the cc_proto_library is used only
  119. for bootstraping. When it is set to True, no files will be generated.
  120. The rule will simply be a provider for .proto files, so that other
  121. cc_proto_library can depend on it.
  122. use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin
  123. when processing the proto files.
  124. default_runtime: the implicitly default runtime which will be depended on by
  125. the generated cc_library target.
  126. **kargs: other keyword arguments that are passed to cc_library.
  127. """
  128. includes = []
  129. if include != None:
  130. includes = [include]
  131. if internal_bootstrap_hack:
  132. # For pre-checked-in generated files, we add the internal_bootstrap_hack
  133. # which will skip the codegen action.
  134. _proto_gen(
  135. name=name + "_genproto",
  136. srcs=srcs,
  137. deps=[s + "_genproto" for s in deps],
  138. includes=includes,
  139. protoc=protoc,
  140. visibility=["//visibility:public"],
  141. )
  142. # An empty cc_library to make rule dependency consistent.
  143. native.cc_library(
  144. name=name,
  145. **kargs)
  146. return
  147. grpc_cpp_plugin = None
  148. if use_grpc_plugin:
  149. grpc_cpp_plugin = "//external:grpc_cpp_plugin"
  150. outs = _CcOuts(srcs, use_grpc_plugin)
  151. _proto_gen(
  152. name=name + "_genproto",
  153. srcs=srcs,
  154. deps=[s + "_genproto" for s in deps],
  155. includes=includes,
  156. protoc=protoc,
  157. grpc_cpp_plugin=grpc_cpp_plugin,
  158. gen_cc=1,
  159. outs=outs,
  160. visibility=["//visibility:public"],
  161. )
  162. if default_runtime and not default_runtime in cc_libs:
  163. cc_libs += [default_runtime]
  164. if use_grpc_plugin:
  165. cc_libs += ["//external:grpc_lib"]
  166. native.cc_library(
  167. name=name,
  168. srcs=outs,
  169. deps=cc_libs + deps,
  170. includes=includes,
  171. **kargs)
  172. def internal_gen_well_known_protos_java(srcs):
  173. """Bazel rule to generate the gen_well_known_protos_java genrule
  174. Args:
  175. srcs: the well known protos
  176. """
  177. root = Label("%s//protobuf_java" % (REPOSITORY_NAME)).workspace_root
  178. if root == "":
  179. include = " -Isrc "
  180. else:
  181. include = " -I%s/src " % root
  182. native.genrule(
  183. name = "gen_well_known_protos_java",
  184. srcs = srcs,
  185. outs = [
  186. "wellknown.srcjar",
  187. ],
  188. cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" +
  189. " %s $(SRCS) " % include +
  190. " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar",
  191. tools = [":protoc"],
  192. )
  193. def py_proto_library(
  194. name,
  195. srcs=[],
  196. deps=[],
  197. py_libs=[],
  198. py_extra_srcs=[],
  199. include=None,
  200. default_runtime="//:protobuf_python",
  201. protoc="//:protoc",
  202. **kargs):
  203. """Bazel rule to create a Python protobuf library from proto source files
  204. NOTE: the rule is only an internal workaround to generate protos. The
  205. interface may change and the rule may be removed when bazel has introduced
  206. the native rule.
  207. Args:
  208. name: the name of the py_proto_library.
  209. srcs: the .proto files of the py_proto_library.
  210. deps: a list of dependency labels; must be py_proto_library.
  211. py_libs: a list of other py_library targets depended by the generated
  212. py_library.
  213. py_extra_srcs: extra source files that will be added to the output
  214. py_library. This attribute is used for internal bootstrapping.
  215. include: a string indicating the include path of the .proto files.
  216. default_runtime: the implicitly default runtime which will be depended on by
  217. the generated py_library target.
  218. protoc: the label of the protocol compiler to generate the sources.
  219. **kargs: other keyword arguments that are passed to cc_library.
  220. """
  221. outs = _PyOuts(srcs)
  222. includes = []
  223. if include != None:
  224. includes = [include]
  225. _proto_gen(
  226. name=name + "_genproto",
  227. srcs=srcs,
  228. deps=[s + "_genproto" for s in deps],
  229. includes=includes,
  230. protoc=protoc,
  231. gen_py=1,
  232. outs=outs,
  233. visibility=["//visibility:public"],
  234. )
  235. if default_runtime and not default_runtime in py_libs + deps:
  236. py_libs += [default_runtime]
  237. native.py_library(
  238. name=name,
  239. srcs=outs+py_extra_srcs,
  240. deps=py_libs+deps,
  241. imports=includes,
  242. **kargs)
  243. def internal_protobuf_py_tests(
  244. name,
  245. modules=[],
  246. **kargs):
  247. """Bazel rules to create batch tests for protobuf internal.
  248. Args:
  249. name: the name of the rule.
  250. modules: a list of modules for tests. The macro will create a py_test for
  251. each of the parameter with the source "google/protobuf/%s.py"
  252. kargs: extra parameters that will be passed into the py_test.
  253. """
  254. for m in modules:
  255. s = "python/google/protobuf/internal/%s.py" % m
  256. native.py_test(
  257. name="py_%s" % m,
  258. srcs=[s],
  259. main=s,
  260. **kargs)