From ff46379efe295b0dce683b829e9738f8ebba05cb Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 6 Jul 2022 09:06:27 -0700 Subject: [PATCH] Prepare build for C++ generator. PiperOrigin-RevId: 459265699 --- BUILD | 22 ++++++++++++ bazel/upb_proto_library.bzl | 69 +++++++++++++++++++++---------------- upbc/BUILD | 2 ++ 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/BUILD b/BUILD index ca11f42b19..3c164641c2 100644 --- a/BUILD +++ b/BUILD @@ -318,6 +318,28 @@ cc_library( ], ) +# Common support code for C++ generated code. +cc_library( + name = "generated_cpp_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", + hdrs = [ + "upb/decode.h", + "upb/decode_fast.h", + "upb/encode.h", + "upb/msg.h", + "upb/msg_internal.h", + "upb/port_def.inc", + "upb/port_undef.inc", + "upb/upb.hpp", + ], + copts = UPB_DEFAULT_COPTS, + visibility = ["//visibility:public"], + deps = [ + ":mini_table", + ":table_internal", + ":upb", + ], +) + cc_library( name = "generated_reflection_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", hdrs = [ diff --git a/bazel/upb_proto_library.bzl b/bazel/upb_proto_library.bzl index d444ae2090..b19d7408b1 100644 --- a/bazel/upb_proto_library.bzl +++ b/bazel/upb_proto_library.bzl @@ -84,6 +84,7 @@ def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos): name: Unique name used to generate output files. hdrs: Public headers that can be #included from other rules. srcs: C/C++ source files. + copts: Additional options for cc compilation. dep_ccinfos: CcInfo providers of dependencies we should build/link against. Returns: @@ -116,6 +117,8 @@ def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos): compilation_contexts = compilation_contexts, **blaze_only_args ) + + # buildifier: disable=unused-variable (linking_context, linking_outputs) = cc_common.create_linking_context_from_compilation_outputs( actions = ctx.actions, name = name, @@ -133,7 +136,8 @@ def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos): # Build setting for whether fasttable code generation is enabled ############### -_FastTableEnabled = provider( +_FastTableEnabledInfo = provider( + "Provides fasttable configuration", fields = { "enabled": "whether fasttable is enabled", }, @@ -146,7 +150,7 @@ def fasttable_enabled_impl(ctx): # TODO(haberman): check that the target CPU supports fasttable. pass - return _FastTableEnabled(enabled = raw_setting) + return _FastTableEnabledInfo(enabled = raw_setting) upb_fasttable_enabled = rule( implementation = fasttable_enabled_impl, @@ -155,14 +159,15 @@ upb_fasttable_enabled = rule( # Dummy rule to expose select() copts to aspects ############################## -_UpbProtoLibraryCopts = provider( +UpbProtoLibraryCoptsInfo = provider( + "Provides copts for upb proto targets", fields = { "copts": "copts for upb_proto_library()", }, ) def upb_proto_library_copts_impl(ctx): - return _UpbProtoLibraryCopts(copts = ctx.attr.copts) + return UpbProtoLibraryCoptsInfo(copts = ctx.attr.copts) upb_proto_library_copts = rule( implementation = upb_proto_library_copts_impl, @@ -172,16 +177,20 @@ upb_proto_library_copts = rule( # upb_proto_library / upb_proto_reflection_library shared code ################# GeneratedSrcsInfo = provider( + "Provides generated headers and sources", fields = { "srcs": "list of srcs", "hdrs": "list of hdrs", }, ) -_UpbWrappedCcInfo = provider(fields = ["cc_info"]) -_UpbDefsWrappedCcInfo = provider(fields = ["cc_info"]) -_WrappedGeneratedSrcsInfo = provider(fields = ["srcs"]) -_WrappedDefsGeneratedSrcsInfo = provider(fields = ["srcs"]) +UpbWrappedCcInfo = provider("Provider for cc_info for protos", fields = ["cc_info"]) +_UpbDefsWrappedCcInfo = provider("Provider for cc_info for protos", fields = ["cc_info"]) +UpbWrappedGeneratedSrcsInfo = provider("Provider for generated sources", fields = ["srcs"]) +_WrappedDefsGeneratedSrcsInfo = provider( + "Provider for generated reflective sources", + fields = ["srcs"], +) def _compile_upb_protos(ctx, generator, proto_info, proto_sources): if len(proto_sources) == 0: @@ -193,7 +202,7 @@ def _compile_upb_protos(ctx, generator, proto_info, proto_sources): hdrs = [_generate_output_file(ctx, name, ext + ".h") for name in proto_sources] transitive_sets = proto_info.transitive_descriptor_sets.to_list() fasttable_enabled = (hasattr(ctx.attr, "_fasttable_enabled") and - ctx.attr._fasttable_enabled[_FastTableEnabled].enabled) + ctx.attr._fasttable_enabled[_FastTableEnabledInfo].enabled) codegen_params = "fasttable:" if fasttable_enabled else "" ctx.actions.run( inputs = depset( @@ -221,18 +230,18 @@ def _upb_proto_rule_impl(ctx): if _WrappedDefsGeneratedSrcsInfo in dep: srcs = dep[_WrappedDefsGeneratedSrcsInfo].srcs - elif _WrappedGeneratedSrcsInfo in dep: - srcs = dep[_WrappedGeneratedSrcsInfo].srcs + elif UpbWrappedGeneratedSrcsInfo in dep: + srcs = dep[UpbWrappedGeneratedSrcsInfo].srcs else: - fail("proto_library rule must generate _WrappedGeneratedSrcsInfo or " + + fail("proto_library rule must generate UpbWrappedGeneratedSrcsInfo or " + "_WrappedDefsGeneratedSrcsInfo (aspect should have handled this).") if _UpbDefsWrappedCcInfo in dep: cc_info = dep[_UpbDefsWrappedCcInfo].cc_info - elif _UpbWrappedCcInfo in dep: - cc_info = dep[_UpbWrappedCcInfo].cc_info + elif UpbWrappedCcInfo in dep: + cc_info = dep[UpbWrappedCcInfo].cc_info else: - fail("proto_library rule must generate _UpbWrappedCcInfo or " + + fail("proto_library rule must generate UpbWrappedCcInfo or " + "_UpbDefsWrappedCcInfo (aspect should have handled this).") lib = cc_info.linking_context.linker_inputs.to_list()[0].libraries[0] @@ -252,24 +261,24 @@ def _upb_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider): files = _compile_upb_protos(ctx, generator, proto_info, proto_info.direct_sources) deps = ctx.rule.attr.deps + getattr(ctx.attr, "_" + generator) 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[UpbWrappedCcInfo].cc_info for dep in deps if UpbWrappedCcInfo in dep] dep_ccinfos += [dep[_UpbDefsWrappedCcInfo].cc_info for dep in deps if _UpbDefsWrappedCcInfo in dep] if generator == "upbdefs": - if _UpbWrappedCcInfo not in target: - fail("Target should have _UpbWrappedCcInfo provider") - dep_ccinfos += [target[_UpbWrappedCcInfo].cc_info] + 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 + "." + generator, hdrs = files.hdrs, srcs = files.srcs, - copts = ctx.attr._copts[_UpbProtoLibraryCopts].copts, + copts = ctx.attr._copts[UpbProtoLibraryCoptsInfo].copts, dep_ccinfos = dep_ccinfos, ) return [cc_provider(cc_info = cc_info), file_provider(srcs = files)] -def _upb_proto_library_aspect_impl(target, ctx): - return _upb_proto_aspect_impl(target, ctx, "upb", _UpbWrappedCcInfo, _WrappedGeneratedSrcsInfo) +def upb_proto_library_aspect_impl(target, ctx): + return _upb_proto_aspect_impl(target, ctx, "upb", UpbWrappedCcInfo, UpbWrappedGeneratedSrcsInfo) def _upb_proto_reflection_library_aspect_impl(target, ctx): return _upb_proto_aspect_impl(target, ctx, "upbdefs", _UpbDefsWrappedCcInfo, _WrappedDefsGeneratedSrcsInfo) @@ -285,7 +294,7 @@ def _maybe_add(d): # upb_proto_library() ########################################################## -_upb_proto_library_aspect = aspect( +upb_proto_library_aspect = aspect( attrs = _maybe_add({ "_copts": attr.label( default = "//:upb_proto_library_copts__for_generated_code_only_do_not_use", @@ -308,10 +317,10 @@ _upb_proto_library_aspect = aspect( ]), "_fasttable_enabled": attr.label(default = "//:fasttable_enabled"), }), - implementation = _upb_proto_library_aspect_impl, + implementation = upb_proto_library_aspect_impl, provides = [ - _UpbWrappedCcInfo, - _WrappedGeneratedSrcsInfo, + UpbWrappedCcInfo, + UpbWrappedGeneratedSrcsInfo, ], attr_aspects = ["deps"], fragments = ["cpp"], @@ -324,7 +333,7 @@ upb_proto_library = rule( implementation = _upb_proto_rule_impl, attrs = { "deps": attr.label_list( - aspects = [_upb_proto_library_aspect], + aspects = [upb_proto_library_aspect], allow_rules = ["proto_library"], providers = [ProtoInfo], ), @@ -363,8 +372,8 @@ _upb_proto_reflection_library_aspect = aspect( _WrappedDefsGeneratedSrcsInfo, ], required_aspect_providers = [ - _UpbWrappedCcInfo, - _WrappedGeneratedSrcsInfo, + UpbWrappedCcInfo, + UpbWrappedGeneratedSrcsInfo, ], attr_aspects = ["deps"], fragments = ["cpp"], @@ -378,7 +387,7 @@ upb_proto_reflection_library = rule( attrs = { "deps": attr.label_list( aspects = [ - _upb_proto_library_aspect, + upb_proto_library_aspect, _upb_proto_reflection_library_aspect, ], allow_rules = ["proto_library"], diff --git a/upbc/BUILD b/upbc/BUILD index 6d538282b2..1a3951bf95 100644 --- a/upbc/BUILD +++ b/upbc/BUILD @@ -90,6 +90,7 @@ cc_library( "file_layout.h", ], copts = UPB_DEFAULT_CPPOPTS, + visibility = ["//third_party/upb/protos_generator:__pkg__"], deps = [ ":common", "//:mini_table", @@ -109,6 +110,7 @@ cc_library( "keywords.h", ], copts = UPB_DEFAULT_CPPOPTS, + visibility = ["//third_party/upb/protos_generator:__pkg__"], ) cc_binary(