1# Copyright (c) 2024 Google LLC 2# All rights reserved. 3# 4# Use of this source code is governed by a BSD-style 5# license that can be found in the LICENSE file or at 6# https://developers.google.com/open-source/licenses/bsd 7 8"""Public rules for using hpb: 9 - hpb_proto_library() 10""" 11 12load("@bazel_skylib//lib:paths.bzl", "paths") 13load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") 14load("//bazel:upb_proto_library.bzl", "GeneratedSrcsInfo", "UpbWrappedCcInfo", "upb_proto_library_aspect") 15 16def upb_use_cpp_toolchain(): 17 return use_cpp_toolchain() 18 19# Generic support code ######################################################### 20 21def _get_real_short_path(file): 22 # For some reason, files from other archives have short paths that look like: 23 # ../com_google_protobuf/google/protobuf/descriptor.proto 24 short_path = file.short_path 25 if short_path.startswith("../"): 26 second_slash = short_path.index("/", 3) 27 short_path = short_path[second_slash + 1:] 28 29 # Sometimes it has another few prefixes like: 30 # _virtual_imports/any_proto/google/protobuf/any.proto 31 # benchmarks/_virtual_imports/100_msgs_proto/benchmarks/100_msgs.proto 32 # We want just google/protobuf/any.proto. 33 virtual_imports = "_virtual_imports/" 34 if virtual_imports in short_path: 35 short_path = short_path.split(virtual_imports)[1].split("/", 1)[1] 36 return short_path 37 38def _get_real_root(file): 39 real_short_path = _get_real_short_path(file) 40 return file.path[:-len(real_short_path) - 1] 41 42def _generate_output_file(ctx, src, extension): 43 real_short_path = _get_real_short_path(src) 44 real_short_path = paths.relativize(real_short_path, ctx.label.package) 45 output_filename = paths.replace_extension(real_short_path, extension) 46 ret = ctx.actions.declare_file(output_filename) 47 return ret 48 49def _filter_none(elems): 50 return [e for e in elems if e] 51 52def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos): 53 """Like cc_library(), but callable from rules. 54 55 Args: 56 ctx: Rule context. 57 name: Unique name used to generate output files. 58 hdrs: Public headers that can be #included from other rules. 59 srcs: C/C++ source files. 60 copts: Additional options for cc compilation. 61 dep_ccinfos: CcInfo providers of dependencies we should build/link against. 62 63 Returns: 64 CcInfo provider for this compilation. 65 """ 66 67 compilation_contexts = [info.compilation_context for info in dep_ccinfos] 68 linking_contexts = [info.linking_context for info in dep_ccinfos] 69 toolchain = find_cpp_toolchain(ctx) 70 feature_configuration = cc_common.configure_features( 71 ctx = ctx, 72 cc_toolchain = toolchain, 73 requested_features = ctx.features, 74 unsupported_features = ctx.disabled_features, 75 ) 76 77 (compilation_context, compilation_outputs) = cc_common.compile( 78 actions = ctx.actions, 79 feature_configuration = feature_configuration, 80 cc_toolchain = toolchain, 81 name = name, 82 srcs = srcs, 83 public_hdrs = hdrs, 84 user_compile_flags = copts, 85 compilation_contexts = compilation_contexts, 86 ) 87 88 # buildifier: disable=unused-variable 89 (linking_context, linking_outputs) = cc_common.create_linking_context_from_compilation_outputs( 90 actions = ctx.actions, 91 name = name, 92 feature_configuration = feature_configuration, 93 cc_toolchain = toolchain, 94 compilation_outputs = compilation_outputs, 95 linking_contexts = linking_contexts, 96 ) 97 98 return CcInfo( 99 compilation_context = compilation_context, 100 linking_context = linking_context, 101 ) 102 103# Dummy rule to expose select() copts to aspects ############################## 104 105HpbProtoLibraryCoptsInfo = provider( 106 "Provides copts for hpb proto targets", 107 fields = { 108 "copts": "copts for hpb_proto_library()", 109 }, 110) 111 112def hpb_proto_library_copts_impl(ctx): 113 return HpbProtoLibraryCoptsInfo(copts = ctx.attr.copts) 114 115hpb_proto_library_copts = rule( 116 implementation = hpb_proto_library_copts_impl, 117 attrs = {"copts": attr.string_list(default = [])}, 118) 119 120_UpbCcWrappedCcInfo = provider("Provider for cc_info for protos", fields = ["cc_info"]) 121_WrappedCcGeneratedSrcsInfo = provider("Provider for generated sources", fields = ["srcs"]) 122 123def _compile_upb_cc_protos(ctx, generator, proto_info, proto_sources): 124 if len(proto_sources) == 0: 125 return GeneratedSrcsInfo(srcs = [], hdrs = []) 126 127 tool = getattr(ctx.executable, "_gen_" + generator) 128 srcs = [_generate_output_file(ctx, name, ".upb.proto.cc") for name in proto_sources] 129 hdrs = [_generate_output_file(ctx, name, ".upb.proto.h") for name in proto_sources] 130 hdrs += [_generate_output_file(ctx, name, ".upb.fwd.h") for name in proto_sources] 131 transitive_sets = proto_info.transitive_descriptor_sets.to_list() 132 133 args = ctx.actions.args() 134 args.use_param_file(param_file_arg = "@%s") 135 args.set_param_file_format("multiline") 136 137 args.add("--" + generator + "_out=" + _get_real_root(srcs[0])) 138 args.add("--plugin=protoc-gen-" + generator + "=" + tool.path) 139 args.add("--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets])) 140 args.add_all(proto_sources, map_each = _get_real_short_path) 141 142 ctx.actions.run( 143 inputs = depset( 144 direct = [proto_info.direct_descriptor_set], 145 transitive = [proto_info.transitive_descriptor_sets], 146 ), 147 tools = [tool], 148 outputs = srcs + hdrs, 149 executable = ctx.executable._protoc, 150 arguments = [args], 151 progress_message = "Generating upb cc protos for :" + ctx.label.name, 152 ) 153 return GeneratedSrcsInfo(srcs = srcs, hdrs = hdrs) 154 155def _upb_cc_proto_rule_impl(ctx): 156 if len(ctx.attr.deps) != 1: 157 fail("only one deps dependency allowed.") 158 dep = ctx.attr.deps[0] 159 160 if _WrappedCcGeneratedSrcsInfo in dep: 161 srcs = dep[_WrappedCcGeneratedSrcsInfo].srcs 162 else: 163 fail("proto_library rule must generate _WrappedCcGeneratedSrcsInfo (aspect should have " + 164 "handled this).") 165 166 if _UpbCcWrappedCcInfo in dep: 167 cc_info = dep[_UpbCcWrappedCcInfo].cc_info 168 elif UpbWrappedCcInfo in dep: 169 cc_info = dep[UpbWrappedCcInfo].cc_info 170 else: 171 fail("proto_library rule must generate UpbWrappedCcInfo or " + 172 "_UpbCcWrappedCcInfo (aspect should have handled this).") 173 174 lib = cc_info.linking_context.linker_inputs.to_list()[0].libraries[0] 175 files = _filter_none([ 176 lib.static_library, 177 lib.pic_static_library, 178 lib.dynamic_library, 179 ]) 180 return [ 181 DefaultInfo(files = depset(files + srcs.hdrs + srcs.srcs)), 182 srcs, 183 cc_info, 184 ] 185 186def _upb_cc_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider): 187 proto_info = target[ProtoInfo] 188 files = _compile_upb_cc_protos(ctx, generator, proto_info, proto_info.direct_sources) 189 deps = ctx.rule.attr.deps + getattr(ctx.attr, "_" + generator) 190 dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep] 191 dep_ccinfos += [dep[UpbWrappedCcInfo].cc_info for dep in deps if UpbWrappedCcInfo in dep] 192 dep_ccinfos += [dep[_UpbCcWrappedCcInfo].cc_info for dep in deps if _UpbCcWrappedCcInfo in dep] 193 if UpbWrappedCcInfo not in target: 194 fail("Target should have UpbWrappedCcInfo provider") 195 dep_ccinfos.append(target[UpbWrappedCcInfo].cc_info) 196 cc_info = _cc_library_func( 197 ctx = ctx, 198 name = ctx.rule.attr.name + "." + generator, 199 hdrs = files.hdrs, 200 srcs = files.srcs, 201 copts = ctx.attr._ccopts[HpbProtoLibraryCoptsInfo].copts, 202 dep_ccinfos = dep_ccinfos, 203 ) 204 return [cc_provider(cc_info = cc_info), file_provider(srcs = files)] 205 206def _upb_cc_proto_library_aspect_impl(target, ctx): 207 return _upb_cc_proto_aspect_impl(target, ctx, "upbprotos", _UpbCcWrappedCcInfo, _WrappedCcGeneratedSrcsInfo) 208 209_upb_cc_proto_library_aspect = aspect( 210 attrs = { 211 "_ccopts": attr.label( 212 default = "//hpb:hpb_proto_library_copts", 213 ), 214 "_gen_upbprotos": attr.label( 215 executable = True, 216 cfg = "exec", 217 default = "//src/google/protobuf/compiler/hpb:protoc-gen-upb-protos", 218 ), 219 "_protoc": attr.label( 220 executable = True, 221 cfg = "exec", 222 default = "//net/proto2/compiler/public:protocol_compiler", 223 ), 224 "_cc_toolchain": attr.label( 225 default = "@bazel_tools//tools/cpp:current_cc_toolchain", 226 ), 227 "_upbprotos": attr.label_list( 228 default = [ 229 # TODO: Add dependencies for cc runtime (absl/string etc..) 230 "//upb:generated_cpp_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", 231 "//hpb:generated_hpb_support", 232 "@com_google_absl//absl/strings", 233 "@com_google_absl//absl/status:statusor", 234 "//hpb:repeated_field", 235 "//protos", 236 ], 237 ), 238 }, 239 implementation = _upb_cc_proto_library_aspect_impl, 240 provides = [ 241 _UpbCcWrappedCcInfo, 242 _WrappedCcGeneratedSrcsInfo, 243 ], 244 required_aspect_providers = [ 245 UpbWrappedCcInfo, 246 ], 247 attr_aspects = ["deps"], 248 fragments = ["cpp"], 249 toolchains = upb_use_cpp_toolchain(), 250) 251 252upb_cc_proto_library = rule( 253 implementation = _upb_cc_proto_rule_impl, 254 attrs = { 255 "deps": attr.label_list( 256 aspects = [ 257 upb_proto_library_aspect, 258 _upb_cc_proto_library_aspect, 259 ], 260 allow_rules = ["proto_library"], 261 providers = [ProtoInfo], 262 ), 263 "_ccopts": attr.label( 264 default = "//hpb:hpb_proto_library_copts", 265 ), 266 }, 267) 268 269hpb_proto_library = upb_cc_proto_library 270