Expose generated files from proto_gen via DefaultInfo, and change to new provider syntax. (#9951)

* Switch to new provider syntax instead of structs.
* Expose files via `DefaultInfo`, and use that for `py_proto_library`.
* Several changes from buildifier.
pull/9965/head
David L. Jones 3 years ago committed by GitHub
parent b64cd5ac64
commit d60c0d2d35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 60
      protobuf.bzl

@ -73,10 +73,14 @@ def _RelativeOutputPath(path, include, dest = ""):
path = path[len(include):] path = path[len(include):]
return dest + path return dest + path
ProtoGenInfo = provider(
fields = ["srcs", "import_flags", "deps"],
)
def _proto_gen_impl(ctx): def _proto_gen_impl(ctx):
"""General implementation for generating protos""" """General implementation for generating protos"""
srcs = ctx.files.srcs srcs = ctx.files.srcs
deps = depset(direct=ctx.files.srcs) deps = depset(direct = ctx.files.srcs)
source_dir = _SourceDir(ctx) source_dir = _SourceDir(ctx)
gen_dir = _GenDir(ctx).rstrip("/") gen_dir = _GenDir(ctx).rstrip("/")
import_flags = [] import_flags = []
@ -92,27 +96,34 @@ def _proto_gen_impl(ctx):
if has_generated: if has_generated:
import_flags += ["-I" + gen_dir] import_flags += ["-I" + gen_dir]
import_flags = depset(direct=import_flags) import_flags = depset(direct = import_flags)
for dep in ctx.attr.deps: for dep in ctx.attr.deps:
if type(dep.proto.import_flags) == "list": dep_proto = dep[ProtoGenInfo]
import_flags = depset(transitive=[import_flags], direct=dep.proto.import_flags) if type(dep_proto.import_flags) == "list":
import_flags = depset(
transitive = [import_flags],
direct = dep_proto.import_flags,
)
else: else:
import_flags = depset(transitive=[import_flags, dep.proto.import_flags]) import_flags = depset(
if type(dep.proto.deps) == "list": transitive = [import_flags, dep_proto.import_flags],
deps = depset(transitive=[deps], direct=dep.proto.deps) )
if type(dep_proto.deps) == "list":
deps = depset(transitive = [deps], direct = dep_proto.deps)
else: else:
deps = depset(transitive=[deps, dep.proto.deps]) deps = depset(transitive = [deps, dep_proto.deps])
if not ctx.attr.gen_cc and not ctx.attr.gen_py and not ctx.executable.plugin: if not ctx.attr.gen_cc and not ctx.attr.gen_py and not ctx.executable.plugin:
return struct( return [
proto = struct( ProtoGenInfo(
srcs = srcs, srcs = srcs,
import_flags = import_flags, import_flags = import_flags,
deps = deps, deps = deps,
), ),
) ]
generated_files = []
for src in srcs: for src in srcs:
args = [] args = []
@ -134,6 +145,8 @@ def _proto_gen_impl(ctx):
outs.extend(_PyOuts([src.basename], use_grpc_plugin = use_grpc_plugin)) outs.extend(_PyOuts([src.basename], use_grpc_plugin = use_grpc_plugin))
outs = [ctx.actions.declare_file(out, sibling = src) for out in outs] outs = [ctx.actions.declare_file(out, sibling = src) for out in outs]
generated_files.extend(outs)
inputs = [src] + deps.to_list() inputs = [src] + deps.to_list()
tools = [ctx.executable.protoc] tools = [ctx.executable.protoc]
if ctx.executable.plugin: if ctx.executable.plugin:
@ -186,18 +199,19 @@ def _proto_gen_impl(ctx):
use_default_shell_env = True, use_default_shell_env = True,
) )
return struct( return [
proto = struct( ProtoGenInfo(
srcs = srcs, srcs = srcs,
import_flags = import_flags, import_flags = import_flags,
deps = deps, deps = deps,
), ),
) DefaultInfo(files = depset(generated_files)),
]
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 = [ProtoGenInfo]),
"includes": attr.string_list(), "includes": attr.string_list(),
"protoc": attr.label( "protoc": attr.label(
cfg = "exec", cfg = "exec",
@ -214,7 +228,7 @@ proto_gen = rule(
"plugin_options": attr.string_list(), "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.label_list(),
}, },
output_to_genfiles = True, output_to_genfiles = True,
implementation = _proto_gen_impl, implementation = _proto_gen_impl,
@ -315,7 +329,6 @@ def cc_proto_library(
plugin = grpc_cpp_plugin, plugin = grpc_cpp_plugin,
plugin_language = "grpc", plugin_language = "grpc",
gen_cc = 1, gen_cc = 1,
outs = outs,
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )
@ -456,8 +469,6 @@ internal_gen_kt_protos = rule(
}, },
) )
def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs): def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs):
"""Macro to copy files to a different directory and then create a filegroup. """Macro to copy files to a different directory and then create a filegroup.
@ -479,10 +490,12 @@ def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs):
outs = outs, outs = outs,
cmd_bash = " && ".join( cmd_bash = " && ".join(
["cp $(location %s) $(location %s)" % ["cp $(location %s) $(location %s)" %
(s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs]), (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs],
),
cmd_bat = " && ".join( cmd_bat = " && ".join(
["@copy /Y $(location %s) $(location %s) >NUL" % ["@copy /Y $(location %s) $(location %s) >NUL" %
(s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs]), (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs],
),
) )
native.filegroup( native.filegroup(
@ -525,8 +538,6 @@ def py_proto_library(
**kargs: other keyword arguments that are passed to py_library. **kargs: other keyword arguments that are passed to py_library.
""" """
outs = _PyOuts(srcs, use_grpc_plugin)
includes = [] includes = []
if include != None: if include != None:
includes = [include] includes = [include]
@ -545,7 +556,6 @@ def py_proto_library(
includes = includes, includes = includes,
protoc = protoc, protoc = protoc,
gen_py = 1, gen_py = 1,
outs = outs,
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
plugin = grpc_python_plugin, plugin = grpc_python_plugin,
plugin_language = "grpc", plugin_language = "grpc",
@ -555,7 +565,7 @@ def py_proto_library(
py_libs = py_libs + [default_runtime] py_libs = py_libs + [default_runtime]
py_library( py_library(
name = name, name = name,
srcs = outs + py_extra_srcs, srcs = [name + "_genproto"] + py_extra_srcs,
deps = py_libs + deps, deps = py_libs + deps,
imports = includes, imports = includes,
**kargs **kargs

Loading…
Cancel
Save