Commit 643a02bc authored by Jisi Liu's avatar Jisi Liu Committed by GitHub

Merge pull request #1636 from yugui/feature/generic-plugin

Generalize plugin support in Bazel Skylark rule
parents 53387e5f 5977fb0a
...@@ -62,9 +62,19 @@ def _proto_gen_impl(ctx): ...@@ -62,9 +62,19 @@ def _proto_gen_impl(ctx):
if ctx.attr.gen_py: if ctx.attr.gen_py:
args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir] args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
if ctx.executable.grpc_cpp_plugin: if ctx.executable.plugin:
args += ["--plugin=protoc-gen-grpc=" + ctx.executable.grpc_cpp_plugin.path] plugin = ctx.executable.plugin
args += ["--grpc_out=" + ctx.var["GENDIR"] + "/" + gen_dir] lang = ctx.attr.plugin_language
if not lang and plugin.basename.startswith('protoc-gen-'):
lang = plugin.basename[len('protoc-gen-'):]
if not lang:
fail("cannot infer the target language of plugin", "plugin_language")
outdir = ctx.var["GENDIR"] + "/" + gen_dir
if ctx.attr.plugin_options:
outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir
args += ["--plugin=protoc-gen-%s=%s" % (lang, plugin.path)]
args += ["--%s_out=%s" % (lang, outdir)]
if args: if args:
ctx.action( ctx.action(
...@@ -72,6 +82,7 @@ def _proto_gen_impl(ctx): ...@@ -72,6 +82,7 @@ def _proto_gen_impl(ctx):
outputs=ctx.outputs.outs, outputs=ctx.outputs.outs,
arguments=args + import_flags + [s.path for s in srcs], arguments=args + import_flags + [s.path for s in srcs],
executable=ctx.executable.protoc, executable=ctx.executable.protoc,
mnemonic="ProtoCompile",
) )
return struct( return struct(
...@@ -82,7 +93,7 @@ def _proto_gen_impl(ctx): ...@@ -82,7 +93,7 @@ def _proto_gen_impl(ctx):
), ),
) )
_proto_gen = rule( proto_gen = rule(
attrs = { attrs = {
"srcs": attr.label_list(allow_files = True), "srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(providers = ["proto"]), "deps": attr.label_list(providers = ["proto"]),
...@@ -93,11 +104,13 @@ _proto_gen = rule( ...@@ -93,11 +104,13 @@ _proto_gen = rule(
single_file = True, single_file = True,
mandatory = True, mandatory = True,
), ),
"grpc_cpp_plugin": attr.label( "plugin": attr.label(
cfg = HOST_CFG, cfg = HOST_CFG,
allow_files = True,
executable = True, executable = True,
single_file = True,
), ),
"plugin_language": attr.string(),
"plugin_options": attr.string_list(),
"gen_cc": attr.bool(), "gen_cc": attr.bool(),
"gen_py": attr.bool(), "gen_py": attr.bool(),
"outs": attr.output_list(), "outs": attr.output_list(),
...@@ -105,6 +118,26 @@ _proto_gen = rule( ...@@ -105,6 +118,26 @@ _proto_gen = rule(
output_to_genfiles = True, output_to_genfiles = True,
implementation = _proto_gen_impl, implementation = _proto_gen_impl,
) )
"""Generates codes from Protocol Buffers definitions.
This rule helps you to implement Skylark macros specific to the target
language. You should prefer more specific `cc_proto_library `,
`py_proto_library` and others unless you are adding such wrapper macros.
Args:
srcs: Protocol Buffers definition files (.proto) to run the protocol compiler
against.
deps: a list of dependency labels; must be other proto libraries.
includes: a list of include paths to .proto files.
protoc: the label of the protocol compiler to generate the sources.
plugin: the label of the protocol compiler plugin to be passed to the protocol
compiler.
plugin_language: the language of the generated sources
plugin_options: a list of options to be passed to the plugin
gen_cc: generates C++ sources in addition to the ones from the plugin.
gen_py: generates Python sources in addition to the ones from the plugin.
outs: a list of labels of the expected outputs from the protocol compiler.
"""
def cc_proto_library( def cc_proto_library(
name, name,
...@@ -150,7 +183,7 @@ def cc_proto_library( ...@@ -150,7 +183,7 @@ def cc_proto_library(
if internal_bootstrap_hack: if internal_bootstrap_hack:
# For pre-checked-in generated files, we add the internal_bootstrap_hack # For pre-checked-in generated files, we add the internal_bootstrap_hack
# which will skip the codegen action. # which will skip the codegen action.
_proto_gen( proto_gen(
name=name + "_genproto", name=name + "_genproto",
srcs=srcs, srcs=srcs,
deps=[s + "_genproto" for s in deps], deps=[s + "_genproto" for s in deps],
...@@ -170,13 +203,14 @@ def cc_proto_library( ...@@ -170,13 +203,14 @@ def cc_proto_library(
outs = _CcOuts(srcs, use_grpc_plugin) outs = _CcOuts(srcs, use_grpc_plugin)
_proto_gen( proto_gen(
name=name + "_genproto", name=name + "_genproto",
srcs=srcs, srcs=srcs,
deps=[s + "_genproto" for s in deps], deps=[s + "_genproto" for s in deps],
includes=includes, includes=includes,
protoc=protoc, protoc=protoc,
grpc_cpp_plugin=grpc_cpp_plugin, plugin=grpc_cpp_plugin,
plugin_language="grpc",
gen_cc=1, gen_cc=1,
outs=outs, outs=outs,
visibility=["//visibility:public"], visibility=["//visibility:public"],
...@@ -286,7 +320,7 @@ def py_proto_library( ...@@ -286,7 +320,7 @@ def py_proto_library(
if include != None: if include != None:
includes = [include] includes = [include]
_proto_gen( proto_gen(
name=name + "_genproto", name=name + "_genproto",
srcs=srcs, srcs=srcs,
deps=[s + "_genproto" for s in deps], deps=[s + "_genproto" for s in deps],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment