Skip to content

Commit

Permalink
hpb_proto_library: properly handle alias libraries (srcs empty, reexp…
Browse files Browse the repository at this point in the history
…ort deps)

PiperOrigin-RevId: 689464159
  • Loading branch information
honglooker authored and copybara-github committed Oct 24, 2024
1 parent 3c91820 commit 7ef7eeb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
31 changes: 20 additions & 11 deletions hpb/bazel/hpb_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,33 @@ 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]
dep_ccinfos += [dep[_UpbCcWrappedCcInfo].cc_info for dep in deps if _UpbCcWrappedCcInfo in dep]
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)
Expand Down
22 changes: 22 additions & 0 deletions hpb_generator/tests/test_hpb_bzl_alias.cc
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7ef7eeb

Please sign in to comment.