hpb_proto_library: properly handle alias libraries (srcs empty, reexport deps)

PiperOrigin-RevId: 689464159
pull/18948/head
Hong Shin 3 months ago committed by Copybara-Service
parent 3c918201f9
commit 7ef7eebbfc
  1. 31
      hpb/bazel/hpb_proto_library.bzl
  2. 22
      hpb_generator/tests/test_hpb_bzl_alias.cc

@ -159,8 +159,6 @@ def _upb_cc_proto_rule_impl(ctx):
]
def _upb_cc_proto_aspect_impl(target, ctx, cc_provider, file_provider):
proto_info = target[ProtoInfo]
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]
@ -168,15 +166,26 @@ def _upb_cc_proto_aspect_impl(target, ctx, cc_provider, file_provider):
if UpbWrappedCcInfo not in target:
fail("Target should have UpbWrappedCcInfo provider")
dep_ccinfos.append(target[UpbWrappedCcInfo].cc_info)
cc_info = _cc_library_func(
ctx = ctx,
name = ctx.rule.attr.name + ".upbprotos",
hdrs = files.hdrs,
srcs = files.srcs,
copts = ctx.attr._ccopts[HpbProtoLibraryCoptsInfo].copts,
dep_ccinfos = dep_ccinfos,
)
return [cc_provider(cc_info = cc_info), file_provider(srcs = files)]
proto_info = target[ProtoInfo]
if not getattr(ctx.rule.attr, "srcs", []):
# This target doesn't declare any sources, reexport all its deps instead.
# This is known as an "alias library":
# https://bazel.build/versions/6.4.0/reference/be/protocol-buffer#proto_library.srcs
return [cc_provider(
cc_info = cc_common.merge_cc_infos(direct_cc_infos = dep_ccinfos),
), file_provider(srcs = GeneratedSrcsInfo(srcs = [], hdrs = []))]
else:
files = _compile_upb_cc_protos(ctx, proto_info, proto_info.direct_sources)
cc_info = _cc_library_func(
ctx = ctx,
name = ctx.rule.attr.name + ".upbprotos",
hdrs = files.hdrs,
srcs = files.srcs,
copts = ctx.attr._ccopts[HpbProtoLibraryCoptsInfo].copts,
dep_ccinfos = dep_ccinfos,
)
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, _UpbCcWrappedCcInfo, _WrappedCcGeneratedSrcsInfo)

@ -0,0 +1,22 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <gtest/gtest.h>
#include "google/protobuf/compiler/hpb/tests/set_alias.upb.proto.h"
#include "google/protobuf/hpb/arena.h"
#include "google/protobuf/hpb/hpb.h"
namespace {
using hpb_unittest::protos::Child;
TEST(BzlCode, CheckBzlAlias) {
hpb::Arena arena;
auto child = hpb::CreateMessage<Child>(arena);
child.set_peeps(12);
ASSERT_EQ(child.peeps(), 12);
}
} // namespace
Loading…
Cancel
Save