Finish migration of hpb_proto_library.bzl to use proto_common

PiperOrigin-RevId: 686085793
pull/18815/head
Hong Shin 5 months ago committed by Copybara-Service
parent 7c84a6af5e
commit c006c62e71
  1. 82
      hpb/bazel/hpb_proto_library.bzl

@ -16,32 +16,10 @@ load("//bazel/common:proto_common.bzl", "proto_common")
def upb_use_cpp_toolchain():
return use_cpp_toolchain()
# Generic support code #########################################################
def _get_real_short_path(file):
# For some reason, files from other archives have short paths that look like:
# ../com_google_protobuf/google/protobuf/descriptor.proto
short_path = file.short_path
if short_path.startswith("../"):
second_slash = short_path.index("/", 3)
short_path = short_path[second_slash + 1:]
# Sometimes it has another few prefixes like:
# _virtual_imports/any_proto/google/protobuf/any.proto
# benchmarks/_virtual_imports/100_msgs_proto/benchmarks/100_msgs.proto
# We want just google/protobuf/any.proto.
virtual_imports = "_virtual_imports/"
if virtual_imports in short_path:
short_path = short_path.split(virtual_imports)[1].split("/", 1)[1]
return short_path
def _get_real_root(file):
real_short_path = _get_real_short_path(file)
return file.path[:-len(real_short_path) - 1]
def _filter_none(elems):
return [e for e in elems if e]
# TODO: b/373443334 - consolidate
def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos):
"""Like cc_library(), but callable from rules.
@ -113,12 +91,13 @@ hpb_proto_library_copts = rule(
_UpbCcWrappedCcInfo = provider("Provider for cc_info for hpb", fields = ["cc_info"])
_WrappedCcGeneratedSrcsInfo = provider("Provider for generated sources", fields = ["srcs"])
def _compile_upb_cc_protos(ctx, generator, proto_info, proto_sources):
def _get_lang_toolchain(ctx):
return ctx.attr._hpb_lang_toolchain[proto_common.ProtoLangToolchainInfo]
def _compile_upb_cc_protos(ctx, proto_info, proto_sources):
if len(proto_sources) == 0:
return GeneratedSrcsInfo(srcs = [], hdrs = [])
tool = getattr(ctx.executable, "_gen_" + generator)
srcs = []
srcs += proto_common.declare_generated_files(
ctx.actions,
@ -137,28 +116,15 @@ def _compile_upb_cc_protos(ctx, generator, proto_info, proto_sources):
extension = ".upb.fwd.h",
proto_info = proto_info,
)
transitive_sets = proto_info.transitive_descriptor_sets.to_list()
args = ctx.actions.args()
args.use_param_file(param_file_arg = "@%s")
args.set_param_file_format("multiline")
args.add("--" + generator + "_out=" + _get_real_root(srcs[0]))
args.add("--plugin=protoc-gen-" + generator + "=" + tool.path)
args.add("--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]))
args.add_all(proto_sources, map_each = _get_real_short_path)
ctx.actions.run(
inputs = depset(
direct = [proto_info.direct_descriptor_set],
transitive = [proto_info.transitive_descriptor_sets],
),
tools = [tool],
outputs = srcs + hdrs,
executable = ctx.executable._protoc,
arguments = [args],
progress_message = "Generating hpb protocol buffers for :" + ctx.label.name,
proto_common.compile(
actions = ctx.actions,
proto_info = proto_info,
proto_lang_toolchain_info = _get_lang_toolchain(ctx),
generated_files = srcs + hdrs,
experimental_exec_group = "proto_compiler",
)
return GeneratedSrcsInfo(srcs = srcs, hdrs = hdrs)
def _upb_cc_proto_rule_impl(ctx):
@ -192,10 +158,10 @@ def _upb_cc_proto_rule_impl(ctx):
cc_info,
]
def _upb_cc_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider):
def _upb_cc_proto_aspect_impl(target, ctx, cc_provider, file_provider):
proto_info = target[ProtoInfo]
files = _compile_upb_cc_protos(ctx, generator, proto_info, proto_info.direct_sources)
deps = ctx.rule.attr.deps + getattr(ctx.attr, "_" + generator)
files = _compile_upb_cc_protos(ctx, proto_info, proto_info.direct_sources)
deps = ctx.rule.attr.deps + ctx.attr._upbprotos
dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep]
dep_ccinfos += [dep[UpbWrappedCcInfo].cc_info for dep in deps if UpbWrappedCcInfo in dep]
dep_ccinfos += [dep[_UpbCcWrappedCcInfo].cc_info for dep in deps if _UpbCcWrappedCcInfo in dep]
@ -204,7 +170,7 @@ def _upb_cc_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider
dep_ccinfos.append(target[UpbWrappedCcInfo].cc_info)
cc_info = _cc_library_func(
ctx = ctx,
name = ctx.rule.attr.name + "." + generator,
name = ctx.rule.attr.name + ".upbprotos",
hdrs = files.hdrs,
srcs = files.srcs,
copts = ctx.attr._ccopts[HpbProtoLibraryCoptsInfo].copts,
@ -213,22 +179,15 @@ def _upb_cc_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider
return [cc_provider(cc_info = cc_info), file_provider(srcs = files)]
def _upb_cc_proto_library_aspect_impl(target, ctx):
return _upb_cc_proto_aspect_impl(target, ctx, "upbprotos", _UpbCcWrappedCcInfo, _WrappedCcGeneratedSrcsInfo)
return _upb_cc_proto_aspect_impl(target, ctx, _UpbCcWrappedCcInfo, _WrappedCcGeneratedSrcsInfo)
_upb_cc_proto_library_aspect = aspect(
attrs = {
"_ccopts": attr.label(
default = "//hpb:hpb_proto_library_copts",
),
"_gen_upbprotos": attr.label(
executable = True,
cfg = "exec",
default = "//src/google/protobuf/compiler/hpb:protoc-gen-hpb",
),
"_protoc": attr.label(
executable = True,
cfg = "exec",
default = "//net/proto2/compiler/public:protocol_compiler",
"_hpb_lang_toolchain": attr.label(
default = "//src/google/protobuf/compiler/hpb:toolchain",
),
"_cc_toolchain": attr.label(
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
@ -245,6 +204,9 @@ _upb_cc_proto_library_aspect = aspect(
],
),
},
exec_groups = {
"proto_compiler": exec_group(),
},
implementation = _upb_cc_proto_library_aspect_impl,
provides = [
_UpbCcWrappedCcInfo,

Loading…
Cancel
Save