blob: ae391aa643146cf0898e1795240dbcbe7583b930 [file] [log] [blame]
Jingwen Chenb2a19082018-01-12 18:42:22 -05001load("@bazel_skylib//:lib.bzl", "versions")
2
Damien Martin-Guillerez76547e52016-01-15 14:01:37 +01003def _GetPath(ctx, path):
4 if ctx.label.workspace_root:
5 return ctx.label.workspace_root + '/' + path
6 else:
7 return path
8
Kristina Chodorow4e7ecde2017-01-25 14:10:56 -05009def _IsNewExternal(ctx):
10 # Bazel 0.4.4 and older have genfiles paths that look like:
11 # bazel-out/local-fastbuild/genfiles/external/repo/foo
12 # After the exec root rearrangement, they look like:
13 # ../repo/bazel-out/local-fastbuild/genfiles/foo
14 return ctx.label.workspace_root.startswith("../")
15
Jisi Liu993fb702015-10-19 17:19:49 -070016def _GenDir(ctx):
Kristina Chodorow4e7ecde2017-01-25 14:10:56 -050017 if _IsNewExternal(ctx):
18 # We are using the fact that Bazel 0.4.4+ provides repository-relative paths
19 # for ctx.genfiles_dir.
20 return ctx.genfiles_dir.path + (
21 "/" + ctx.attr.includes[0] if ctx.attr.includes and ctx.attr.includes[0] else "")
22 # This means that we're either in the old version OR the new version in the local repo.
23 # Either way, appending the source path to the genfiles dir works.
24 return ctx.var["GENDIR"] + "/" + _SourceDir(ctx)
25
26def _SourceDir(ctx):
Jisi Liu53a56be2015-10-20 15:18:20 -070027 if not ctx.attr.includes:
Damien Martin-Guillerez76547e52016-01-15 14:01:37 +010028 return ctx.label.workspace_root
Jisi Liu53a56be2015-10-20 15:18:20 -070029 if not ctx.attr.includes[0]:
Damien Martin-Guillerez76547e52016-01-15 14:01:37 +010030 return _GetPath(ctx, ctx.label.package)
Jisi Liu39362b32015-10-14 17:12:11 -070031 if not ctx.label.package:
Damien Martin-Guillerez76547e52016-01-15 14:01:37 +010032 return _GetPath(ctx, ctx.attr.includes[0])
33 return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0])
Jisi Liu39362b32015-10-14 17:12:11 -070034
Andreas Bergmeierbbeb9832016-08-15 16:57:30 +020035def _CcHdrs(srcs, use_grpc_plugin=False):
36 ret = [s[:-len(".proto")] + ".pb.h" for s in srcs]
Manjunath Kudlurf5c73632016-02-25 08:50:50 -080037 if use_grpc_plugin:
Andreas Bergmeierbbeb9832016-08-15 16:57:30 +020038 ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs]
Manjunath Kudlurf5c73632016-02-25 08:50:50 -080039 return ret
Jisi Liu39362b32015-10-14 17:12:11 -070040
Andreas Bergmeierbbeb9832016-08-15 16:57:30 +020041def _CcSrcs(srcs, use_grpc_plugin=False):
42 ret = [s[:-len(".proto")] + ".pb.cc" for s in srcs]
43 if use_grpc_plugin:
44 ret += [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs]
45 return ret
46
47def _CcOuts(srcs, use_grpc_plugin=False):
48 return _CcHdrs(srcs, use_grpc_plugin) + _CcSrcs(srcs, use_grpc_plugin)
49
Mateusz Matejczyk294b5752018-03-11 17:48:10 -040050def _PyOuts(srcs, use_grpc_plugin=False):
51 ret = [s[:-len(".proto")] + "_pb2.py" for s in srcs]
52 if use_grpc_plugin:
53 ret += [s[:-len(".proto")] + "_pb2_grpc.py" for s in srcs]
54 return ret
Jisi Liu39362b32015-10-14 17:12:11 -070055
David Z. Chen02cd45c2016-05-20 16:49:04 -070056def _RelativeOutputPath(path, include, dest=""):
Jisi Liu993fb702015-10-19 17:19:49 -070057 if include == None:
58 return path
59
60 if not path.startswith(include):
61 fail("Include path %s isn't part of the path %s." % (include, path))
62
63 if include and include[-1] != '/':
64 include = include + '/'
David Z. Chen02cd45c2016-05-20 16:49:04 -070065 if dest and dest[-1] != '/':
66 dest = dest + '/'
Jisi Liu993fb702015-10-19 17:19:49 -070067
68 path = path[len(include):]
David Z. Chen02cd45c2016-05-20 16:49:04 -070069 return dest + path
Jisi Liu993fb702015-10-19 17:19:49 -070070
Jisi Liu9c7d9c02015-10-15 10:51:32 -070071def _proto_gen_impl(ctx):
72 """General implementation for generating protos"""
Jisi Liu39362b32015-10-14 17:12:11 -070073 srcs = ctx.files.srcs
74 deps = []
75 deps += ctx.files.srcs
Kristina Chodorow4e7ecde2017-01-25 14:10:56 -050076 source_dir = _SourceDir(ctx)
Fahrzin Hemmati2cfa7612018-03-26 19:09:08 -070077 gen_dir = _GenDir(ctx).rstrip('/')
Kristina Chodorow4e7ecde2017-01-25 14:10:56 -050078 if source_dir:
79 import_flags = ["-I" + source_dir, "-I" + gen_dir]
Jisi Liu53a56be2015-10-20 15:18:20 -070080 else:
81 import_flags = ["-I."]
82
Jisi Liu39362b32015-10-14 17:12:11 -070083 for dep in ctx.attr.deps:
84 import_flags += dep.proto.import_flags
85 deps += dep.proto.deps
86
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -080087 if not ctx.attr.gen_cc and not ctx.attr.gen_py and not ctx.executable.plugin:
88 return struct(
89 proto=struct(
90 srcs=srcs,
91 import_flags=import_flags,
92 deps=deps,
93 ),
94 )
Jisi Liu39362b32015-10-14 17:12:11 -070095
Fahrzin Hemmatib3c2ec72018-03-14 17:49:04 -070096 for src in srcs:
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -080097 args = []
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +090098
Fahrzin Hemmati2cfa7612018-03-26 19:09:08 -070099 in_gen_dir = src.root.path == gen_dir
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -0800100 if in_gen_dir:
101 import_flags_real = []
102 for f in depset(import_flags):
103 path = f.replace('-I', '')
104 import_flags_real.append('-I$(realpath -s %s)' % path)
Manjunath Kudlurf0966a72016-02-22 14:30:43 -0800105
Fahrzin Hemmatiae638962018-03-15 00:37:31 -0700106 outs = []
Fahrzin Hemmatid1403e52018-03-16 13:23:34 -0700107 use_grpc_plugin = (ctx.attr.plugin_language == "grpc" and ctx.attr.plugin)
108 path_tpl = "$(realpath %s)" if in_gen_dir else "%s"
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -0800109 if ctx.attr.gen_cc:
Fahrzin Hemmatid1403e52018-03-16 13:23:34 -0700110 args += [("--cpp_out=" + path_tpl) % gen_dir]
Fahrzin Hemmatiae638962018-03-15 00:37:31 -0700111 outs.extend(_CcOuts([src.basename], use_grpc_plugin=use_grpc_plugin))
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -0800112 if ctx.attr.gen_py:
Fahrzin Hemmatid1403e52018-03-16 13:23:34 -0700113 args += [("--python_out=" + path_tpl) % gen_dir]
Fahrzin Hemmatiae638962018-03-15 00:37:31 -0700114 outs.extend(_PyOuts([src.basename], use_grpc_plugin=use_grpc_plugin))
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -0800115
Fahrzin Hemmatiae638962018-03-15 00:37:31 -0700116 outs = [ctx.actions.declare_file(out, sibling=src) for out in outs]
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -0800117 inputs = [src] + deps
118 if ctx.executable.plugin:
119 plugin = ctx.executable.plugin
120 lang = ctx.attr.plugin_language
121 if not lang and plugin.basename.startswith('protoc-gen-'):
122 lang = plugin.basename[len('protoc-gen-'):]
123 if not lang:
124 fail("cannot infer the target language of plugin", "plugin_language")
125
Fahrzin Hemmatid1403e52018-03-16 13:23:34 -0700126 outdir = "." if in_gen_dir else gen_dir
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -0800127
128 if ctx.attr.plugin_options:
129 outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir
Fahrzin Hemmatid1403e52018-03-16 13:23:34 -0700130 args += [("--plugin=protoc-gen-%s=" + path_tpl) % (lang, plugin.path)]
Fahrzin Hemmati800f8d62017-11-30 22:27:48 -0800131 args += ["--%s_out=%s" % (lang, outdir)]
132 inputs += [plugin]
133
Fahrzin Hemmatid1403e52018-03-16 13:23:34 -0700134 if not in_gen_dir:
135 ctx.action(
136 inputs=inputs,
137 outputs=outs,
138 arguments=args + import_flags + [src.path],
139 executable=ctx.executable.protoc,
140 mnemonic="ProtoCompile",
141 use_default_shell_env=True,
142 )
143 else:
144 for out in outs:
Fahrzin Hemmatib3c2ec72018-03-14 17:49:04 -0700145 orig_command = " ".join(
146 ["$(realpath %s)" % ctx.executable.protoc.path] + args +
147 import_flags_real + ["-I.", src.basename])
148 command = ";".join([
149 'CMD="%s"' % orig_command,
150 "cd %s" % src.dirname,
151 "${CMD}",
152 "cd -",
Fahrzin Hemmatib3c2ec72018-03-14 17:49:04 -0700153 ])
Fahrzin Hemmati2cfa7612018-03-26 19:09:08 -0700154 generated_out = '/'.join([gen_dir, out.basename])
155 if generated_out != out.path:
156 command += ";mv %s %s" % (generated_out, out.path)
Fahrzin Hemmatid1403e52018-03-16 13:23:34 -0700157 ctx.action(
158 inputs=inputs + [ctx.executable.protoc],
159 outputs=[out],
160 command=command,
161 mnemonic="ProtoCompile",
162 use_default_shell_env=True,
163 )
Jisi Liu39362b32015-10-14 17:12:11 -0700164
165 return struct(
166 proto=struct(
Jisi Liu125a91b2015-10-14 17:37:39 -0700167 srcs=srcs,
168 import_flags=import_flags,
169 deps=deps,
170 ),
171 )
Jisi Liu39362b32015-10-14 17:12:11 -0700172
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900173proto_gen = rule(
Jisi Liu39362b32015-10-14 17:12:11 -0700174 attrs = {
Jisi Liuee8131a2015-10-14 17:20:05 -0700175 "srcs": attr.label_list(allow_files = True),
176 "deps": attr.label_list(providers = ["proto"]),
Jisi Liu53a56be2015-10-20 15:18:20 -0700177 "includes": attr.string_list(),
Jisi Liuee8131a2015-10-14 17:20:05 -0700178 "protoc": attr.label(
Vladimir Moskvaa86e6d82016-09-09 13:21:35 +0200179 cfg = "host",
Jisi Liuee8131a2015-10-14 17:20:05 -0700180 executable = True,
James Juddd5f0dac2018-08-14 21:55:35 -0600181 allow_single_file = True,
Jisi Liuee8131a2015-10-14 17:20:05 -0700182 mandatory = True,
183 ),
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900184 "plugin": attr.label(
Vladimir Moskvaa86e6d82016-09-09 13:21:35 +0200185 cfg = "host",
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900186 allow_files = True,
Manjunath Kudlurf0966a72016-02-22 14:30:43 -0800187 executable = True,
Manjunath Kudlurf0966a72016-02-22 14:30:43 -0800188 ),
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900189 "plugin_language": attr.string(),
190 "plugin_options": attr.string_list(),
Jisi Liuee8131a2015-10-14 17:20:05 -0700191 "gen_cc": attr.bool(),
192 "gen_py": attr.bool(),
193 "outs": attr.output_list(),
194 },
195 output_to_genfiles = True,
Jisi Liu9c7d9c02015-10-15 10:51:32 -0700196 implementation = _proto_gen_impl,
Jisi Liu39362b32015-10-14 17:12:11 -0700197)
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900198"""Generates codes from Protocol Buffers definitions.
199
200This rule helps you to implement Skylark macros specific to the target
201language. You should prefer more specific `cc_proto_library `,
202`py_proto_library` and others unless you are adding such wrapper macros.
203
204Args:
205 srcs: Protocol Buffers definition files (.proto) to run the protocol compiler
206 against.
207 deps: a list of dependency labels; must be other proto libraries.
208 includes: a list of include paths to .proto files.
209 protoc: the label of the protocol compiler to generate the sources.
210 plugin: the label of the protocol compiler plugin to be passed to the protocol
211 compiler.
212 plugin_language: the language of the generated sources
213 plugin_options: a list of options to be passed to the plugin
Kristina Chodorow4e7ecde2017-01-25 14:10:56 -0500214 gen_cc: generates C++ sources in addition to the ones from the plugin.
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900215 gen_py: generates Python sources in addition to the ones from the plugin.
216 outs: a list of labels of the expected outputs from the protocol compiler.
217"""
Jisi Liu39362b32015-10-14 17:12:11 -0700218
219def cc_proto_library(
Jisi Liu125a91b2015-10-14 17:37:39 -0700220 name,
221 srcs=[],
Jisi Liu125a91b2015-10-14 17:37:39 -0700222 deps=[],
Jisi Liud8701b52015-10-16 11:44:21 -0700223 cc_libs=[],
Jisi Liu6dac0822015-10-19 14:41:00 -0700224 include=None,
James O'Kane950f5e42018-03-08 22:30:44 -0800225 protoc="@com_google_protobuf//:protoc",
Jisi Liu3101e732015-10-16 12:46:26 -0700226 internal_bootstrap_hack=False,
Manjunath Kudlurf0966a72016-02-22 14:30:43 -0800227 use_grpc_plugin=False,
James O'Kane950f5e42018-03-08 22:30:44 -0800228 default_runtime="@com_google_protobuf//:protobuf",
Jisi Liu125a91b2015-10-14 17:37:39 -0700229 **kargs):
Jisi Liu3101e732015-10-16 12:46:26 -0700230 """Bazel rule to create a C++ protobuf library from proto source files
231
Jisi Liud4bef7d2015-11-02 12:24:32 -0800232 NOTE: the rule is only an internal workaround to generate protos. The
233 interface may change and the rule may be removed when bazel has introduced
234 the native rule.
235
Jisi Liu3101e732015-10-16 12:46:26 -0700236 Args:
237 name: the name of the cc_proto_library.
238 srcs: the .proto files of the cc_proto_library.
239 deps: a list of dependency labels; must be cc_proto_library.
240 cc_libs: a list of other cc_library targets depended by the generated
241 cc_library.
242 include: a string indicating the include path of the .proto files.
243 protoc: the label of the protocol compiler to generate the sources.
244 internal_bootstrap_hack: a flag indicate the cc_proto_library is used only
245 for bootstraping. When it is set to True, no files will be generated.
246 The rule will simply be a provider for .proto files, so that other
247 cc_proto_library can depend on it.
Manjunath Kudlurf0966a72016-02-22 14:30:43 -0800248 use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin
249 when processing the proto files.
Jisi Liube92ffb2015-10-27 15:11:38 -0700250 default_runtime: the implicitly default runtime which will be depended on by
251 the generated cc_library target.
Jisi Liu3101e732015-10-16 12:46:26 -0700252 **kargs: other keyword arguments that are passed to cc_library.
253
254 """
Jisi Liu39362b32015-10-14 17:12:11 -0700255
Jisi Liu53a56be2015-10-20 15:18:20 -0700256 includes = []
257 if include != None:
258 includes = [include]
259
Jisi Liu39362b32015-10-14 17:12:11 -0700260 if internal_bootstrap_hack:
261 # For pre-checked-in generated files, we add the internal_bootstrap_hack
262 # which will skip the codegen action.
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900263 proto_gen(
Jisi Liu125a91b2015-10-14 17:37:39 -0700264 name=name + "_genproto",
265 srcs=srcs,
Jisi Liud8701b52015-10-16 11:44:21 -0700266 deps=[s + "_genproto" for s in deps],
Jisi Liu53a56be2015-10-20 15:18:20 -0700267 includes=includes,
Jisi Liu125a91b2015-10-14 17:37:39 -0700268 protoc=protoc,
Martin Maly8e0c9a32015-12-04 17:44:58 -0800269 visibility=["//visibility:public"],
Jisi Liu39362b32015-10-14 17:12:11 -0700270 )
271 # An empty cc_library to make rule dependency consistent.
272 native.cc_library(
Jisi Liu125a91b2015-10-14 17:37:39 -0700273 name=name,
Jisi Liud8701b52015-10-16 11:44:21 -0700274 **kargs)
Jisi Liu39362b32015-10-14 17:12:11 -0700275 return
276
Manjunath Kudlurf0966a72016-02-22 14:30:43 -0800277 grpc_cpp_plugin = None
278 if use_grpc_plugin:
279 grpc_cpp_plugin = "//external:grpc_cpp_plugin"
280
Andreas Bergmeierbbeb9832016-08-15 16:57:30 +0200281 gen_srcs = _CcSrcs(srcs, use_grpc_plugin)
282 gen_hdrs = _CcHdrs(srcs, use_grpc_plugin)
283 outs = gen_srcs + gen_hdrs
Manjunath Kudlurf5c73632016-02-25 08:50:50 -0800284
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900285 proto_gen(
Jisi Liu125a91b2015-10-14 17:37:39 -0700286 name=name + "_genproto",
287 srcs=srcs,
Jisi Liud8701b52015-10-16 11:44:21 -0700288 deps=[s + "_genproto" for s in deps],
Jisi Liu53a56be2015-10-20 15:18:20 -0700289 includes=includes,
Jisi Liu125a91b2015-10-14 17:37:39 -0700290 protoc=protoc,
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900291 plugin=grpc_cpp_plugin,
292 plugin_language="grpc",
Jisi Liu125a91b2015-10-14 17:37:39 -0700293 gen_cc=1,
294 outs=outs,
Martin Maly8e0c9a32015-12-04 17:44:58 -0800295 visibility=["//visibility:public"],
Jisi Liu39362b32015-10-14 17:12:11 -0700296 )
297
Jisi Liube92ffb2015-10-27 15:11:38 -0700298 if default_runtime and not default_runtime in cc_libs:
Vladimir Moskva4fc93042017-08-07 13:33:03 +0200299 cc_libs = cc_libs + [default_runtime]
Manjunath Kudlurf5c73632016-02-25 08:50:50 -0800300 if use_grpc_plugin:
Vladimir Moskva4fc93042017-08-07 13:33:03 +0200301 cc_libs = cc_libs + ["//external:grpc_lib"]
Jisi Liu6dac0822015-10-19 14:41:00 -0700302
Jisi Liu39362b32015-10-14 17:12:11 -0700303 native.cc_library(
Jisi Liu125a91b2015-10-14 17:37:39 -0700304 name=name,
Andreas Bergmeierbbeb9832016-08-15 16:57:30 +0200305 srcs=gen_srcs,
306 hdrs=gen_hdrs,
Jisi Liud8701b52015-10-16 11:44:21 -0700307 deps=cc_libs + deps,
Jisi Liu6dac0822015-10-19 14:41:00 -0700308 includes=includes,
Jisi Liud8701b52015-10-16 11:44:21 -0700309 **kargs)
Jisi Liu993fb702015-10-19 17:19:49 -0700310
Steven Parkesea188662016-02-25 07:53:19 -0800311def internal_gen_well_known_protos_java(srcs):
312 """Bazel rule to generate the gen_well_known_protos_java genrule
313
314 Args:
315 srcs: the well known protos
316 """
Ittai Zeidman4fcb36c2018-05-21 23:48:10 +0300317 root = Label("%s//protobuf_java" % (native.repository_name())).workspace_root
318 pkg = native.package_name() + "/" if native.package_name() else ""
Steven Parkesea188662016-02-25 07:53:19 -0800319 if root == "":
cgrushko6fffd4a2017-02-08 12:19:40 -0500320 include = " -I%ssrc " % pkg
Steven Parkesea188662016-02-25 07:53:19 -0800321 else:
cgrushko6fffd4a2017-02-08 12:19:40 -0500322 include = " -I%s/%ssrc " % (root, pkg)
Steven Parkesea188662016-02-25 07:53:19 -0800323 native.genrule(
324 name = "gen_well_known_protos_java",
325 srcs = srcs,
326 outs = [
327 "wellknown.srcjar",
328 ],
329 cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" +
330 " %s $(SRCS) " % include +
331 " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar",
332 tools = [":protoc"],
333 )
334
David Z. Chen02cd45c2016-05-20 16:49:04 -0700335def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs):
336 """Macro to copy files to a different directory and then create a filegroup.
337
338 This is used by the //:protobuf_python py_proto_library target to work around
339 an issue caused by Python source files that are part of the same Python
340 package being in separate directories.
341
342 Args:
343 srcs: The source files to copy and add to the filegroup.
344 strip_prefix: Path to the root of the files to copy.
345 dest: The directory to copy the source files into.
346 **kwargs: extra arguments that will be passesd to the filegroup.
347 """
348 outs = [_RelativeOutputPath(s, strip_prefix, dest) for s in srcs]
349
350 native.genrule(
351 name = name + "_genrule",
352 srcs = srcs,
353 outs = outs,
354 cmd = " && ".join(
355 ["cp $(location %s) $(location %s)" %
356 (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs]),
357 )
358
359 native.filegroup(
360 name = name,
361 srcs = outs,
362 **kwargs)
363
Jisi Liu993fb702015-10-19 17:19:49 -0700364def py_proto_library(
365 name,
366 srcs=[],
367 deps=[],
368 py_libs=[],
369 py_extra_srcs=[],
370 include=None,
James O'Kane950f5e42018-03-08 22:30:44 -0800371 default_runtime="@com_google_protobuf//:protobuf_python",
372 protoc="@com_google_protobuf//:protoc",
Wiktor Tomczak0fa31b22016-11-22 20:18:46 +0100373 use_grpc_plugin=False,
Jisi Liu993fb702015-10-19 17:19:49 -0700374 **kargs):
Jisi Liu7b948cc2015-10-19 17:56:27 -0700375 """Bazel rule to create a Python protobuf library from proto source files
376
Jisi Liud4bef7d2015-11-02 12:24:32 -0800377 NOTE: the rule is only an internal workaround to generate protos. The
378 interface may change and the rule may be removed when bazel has introduced
379 the native rule.
380
Jisi Liu7b948cc2015-10-19 17:56:27 -0700381 Args:
382 name: the name of the py_proto_library.
383 srcs: the .proto files of the py_proto_library.
384 deps: a list of dependency labels; must be py_proto_library.
385 py_libs: a list of other py_library targets depended by the generated
386 py_library.
387 py_extra_srcs: extra source files that will be added to the output
388 py_library. This attribute is used for internal bootstrapping.
389 include: a string indicating the include path of the .proto files.
Jisi Liube92ffb2015-10-27 15:11:38 -0700390 default_runtime: the implicitly default runtime which will be depended on by
391 the generated py_library target.
Jisi Liu7b948cc2015-10-19 17:56:27 -0700392 protoc: the label of the protocol compiler to generate the sources.
Wiktor Tomczak0fa31b22016-11-22 20:18:46 +0100393 use_grpc_plugin: a flag to indicate whether to call the Python C++ plugin
394 when processing the proto files.
Jisi Liu7b948cc2015-10-19 17:56:27 -0700395 **kargs: other keyword arguments that are passed to cc_library.
396
397 """
Mateusz Matejczyk294b5752018-03-11 17:48:10 -0400398 outs = _PyOuts(srcs, use_grpc_plugin)
Jisi Liu53a56be2015-10-20 15:18:20 -0700399
400 includes = []
401 if include != None:
402 includes = [include]
403
Wiktor Tomczak0fa31b22016-11-22 20:18:46 +0100404 grpc_python_plugin = None
405 if use_grpc_plugin:
406 grpc_python_plugin = "//external:grpc_python_plugin"
407 # Note: Generated grpc code depends on Python grpc module. This dependency
408 # is not explicitly listed in py_libs. Instead, host system is assumed to
409 # have grpc installed.
410
Yuki Yugui Sonoda5977fb02016-06-01 16:23:15 +0900411 proto_gen(
Jisi Liu993fb702015-10-19 17:19:49 -0700412 name=name + "_genproto",
413 srcs=srcs,
414 deps=[s + "_genproto" for s in deps],
Jisi Liu53a56be2015-10-20 15:18:20 -0700415 includes=includes,
Jisi Liu993fb702015-10-19 17:19:49 -0700416 protoc=protoc,
417 gen_py=1,
418 outs=outs,
Martin Maly8e0c9a32015-12-04 17:44:58 -0800419 visibility=["//visibility:public"],
Wiktor Tomczak0fa31b22016-11-22 20:18:46 +0100420 plugin=grpc_python_plugin,
421 plugin_language="grpc"
Jisi Liu993fb702015-10-19 17:19:49 -0700422 )
423
Jisi Liube92ffb2015-10-27 15:11:38 -0700424 if default_runtime and not default_runtime in py_libs + deps:
Vladimir Moskva4fc93042017-08-07 13:33:03 +0200425 py_libs = py_libs + [default_runtime]
Jisi Liube92ffb2015-10-27 15:11:38 -0700426
Jisi Liu993fb702015-10-19 17:19:49 -0700427 native.py_library(
428 name=name,
Jisi Liua33fa8e2015-10-20 15:30:44 -0700429 srcs=outs+py_extra_srcs,
430 deps=py_libs+deps,
David Z. Chen985c9682016-02-11 18:11:10 -0800431 imports=includes,
Jisi Liu993fb702015-10-19 17:19:49 -0700432 **kargs)
433
434def internal_protobuf_py_tests(
435 name,
436 modules=[],
437 **kargs):
Jisi Liu7b948cc2015-10-19 17:56:27 -0700438 """Bazel rules to create batch tests for protobuf internal.
439
440 Args:
441 name: the name of the rule.
442 modules: a list of modules for tests. The macro will create a py_test for
443 each of the parameter with the source "google/protobuf/%s.py"
444 kargs: extra parameters that will be passed into the py_test.
445
446 """
Jisi Liu993fb702015-10-19 17:19:49 -0700447 for m in modules:
David Z. Chen985c9682016-02-11 18:11:10 -0800448 s = "python/google/protobuf/internal/%s.py" % m
Jisi Liu993fb702015-10-19 17:19:49 -0700449 native.py_test(
450 name="py_%s" % m,
Jisi Liu7b948cc2015-10-19 17:56:27 -0700451 srcs=[s],
452 main=s,
Jisi Liu993fb702015-10-19 17:19:49 -0700453 **kargs)
Fahrzin Hemmati35119e32017-11-28 14:24:53 -0800454
455
456def check_protobuf_required_bazel_version():
457 """For WORKSPACE files, to check the installed version of bazel.
458
459 This ensures bazel supports our approach to proto_library() depending on a
460 copied filegroup. (Fixed in bazel 0.5.4)
461 """
Jingwen Chenb2a19082018-01-12 18:42:22 -0500462 versions.check(minimum_bazel_version = "0.5.4")