| 1 |
# Copyright (c) 2009-2021, Google LLC |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are met: |
| 6 |
# * Redistributions of source code must retain the above copyright |
| 7 |
# notice, this list of conditions and the following disclaimer. |
| 8 |
# * Redistributions in binary form must reproduce the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer in the |
| 10 |
# documentation and/or other materials provided with the distribution. |
| 11 |
# * Neither the name of Google LLC nor the |
| 12 |
# names of its contributors may be used to endorse or promote products |
| 13 |
# derived from this software without specific prior written permission. |
| 14 |
# |
| 15 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 |
# DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY |
| 19 |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
|
| 26 |
"""Public rules for using upb protos: |
| 27 |
- upb_proto_library() |
| 28 |
- upb_proto_reflection_library() |
| 29 |
""" |
| 30 |
|
| 31 |
load("@bazel_skylib//lib:paths.bzl", "paths") |
| 32 |
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") |
| 33 |
|
| 34 |
# Generic support code ######################################################### |
| 35 |
|
| 36 |
# begin:github_only |
| 37 |
_is_google3 = False |
| 38 |
# end:github_only |
| 39 |
|
| 40 |
# begin:google_only |
| 41 |
# _is_google3 = True |
| 42 |
# end:google_only |
| 43 |
|
| 44 |
def _get_real_short_path(file): |
| 45 |
# For some reason, files from other archives have short paths that look like: |
| 46 |
# ../com_google_protobuf/google/protobuf/descriptor.proto |
| 47 |
short_path = file.short_path |
| 48 |
if short_path.startswith("../"): |
| 49 |
second_slash = short_path.index("/", 3) |
| 50 |
short_path = short_path[second_slash + 1:] |
| 51 |
|
| 52 |
# Sometimes it has another few prefixes like: |
| 53 |
# _virtual_imports/any_proto/google/protobuf/any.proto |
| 54 |
# benchmarks/_virtual_imports/100_msgs_proto/benchmarks/100_msgs.proto |
| 55 |
# We want just google/protobuf/any.proto. |
| 56 |
virtual_imports = "_virtual_imports/" |
| 57 |
if virtual_imports in short_path: |
| 58 |
short_path = short_path.split(virtual_imports)[1].split("/", 1)[1] |
| 59 |
return short_path |
| 60 |
|
| 61 |
def _get_real_root(file): |
| 62 |
real_short_path = _get_real_short_path(file) |
| 63 |
return file.path[:-len(real_short_path) - 1] |
| 64 |
|
| 65 |
def _generate_output_file(ctx, src, extension): |
| 66 |
real_short_path = _get_real_short_path(src) |
| 67 |
real_short_path = paths.relativize(real_short_path, ctx.label.package) |
| 68 |
output_filename = paths.replace_extension(real_short_path, extension) |
| 69 |
ret = ctx.actions.declare_file(output_filename) |
| 70 |
return ret |
| 71 |
|
| 72 |
def _filter_none(elems): |
| 73 |
out = [] |
| 74 |
for elem in elems: |
| 75 |
if elem: |
| 76 |
out.append(elem) |
| 77 |
return out |
| 78 |
|
| 79 |
def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos): |
| 80 |
"""Like cc_library(), but callable from rules. |
| 81 |
|
| 82 |
Args: |
| 83 |
ctx: Rule context. |
| 84 |
name: Unique name used to generate output files. |
| 85 |
hdrs: Public headers that can be #included from other rules. |
| 86 |
srcs: C/C++ source files. |
| 87 |
dep_ccinfos: CcInfo providers of dependencies we should build/link against. |
| 88 |
|
| 89 |
Returns: |
| 90 |
CcInfo provider for this compilation. |
| 91 |
""" |
| 92 |
|
| 93 |
compilation_contexts = [info.compilation_context for info in dep_ccinfos] |
| 94 |
linking_contexts = [info.linking_context for info in dep_ccinfos] |
| 95 |
toolchain = find_cpp_toolchain(ctx) |
| 96 |
feature_configuration = cc_common.configure_features( |
| 97 |
ctx = ctx, |
| 98 |
cc_toolchain = toolchain, |
| 99 |
requested_features = ctx.features, |
| 100 |
unsupported_features = ctx.disabled_features, |
| 101 |
) |
| 102 |
|
| 103 |
blaze_only_args = {} |
| 104 |
|
| 105 |
if _is_google3: |
| 106 |
blaze_only_args["grep_includes"] = ctx.file._grep_includes |
| 107 |
|
| 108 |
(compilation_context, compilation_outputs) = cc_common.compile( |
| 109 |
actions = ctx.actions, |
| 110 |
feature_configuration = feature_configuration, |
| 111 |
cc_toolchain = toolchain, |
| 112 |
name = name, |
| 113 |
srcs = srcs, |
| 114 |
public_hdrs = hdrs, |
| 115 |
user_compile_flags = copts, |
| 116 |
compilation_contexts = compilation_contexts, |
| 117 |
**blaze_only_args |
| 118 |
) |
| 119 |
(linking_context, linking_outputs) = cc_common.create_linking_context_from_compilation_outputs( |
| 120 |
actions = ctx.actions, |
| 121 |
name = name, |
| 122 |
feature_configuration = feature_configuration, |
| 123 |
cc_toolchain = toolchain, |
| 124 |
compilation_outputs = compilation_outputs, |
| 125 |
linking_contexts = linking_contexts, |
| 126 |
**blaze_only_args |
| 127 |
) |
| 128 |
|
| 129 |
return CcInfo( |
| 130 |
compilation_context = compilation_context, |
| 131 |
linking_context = linking_context, |
| 132 |
) |
| 133 |
|
| 134 |
# Build setting for whether fasttable code generation is enabled ############### |
| 135 |
|
| 136 |
_FastTableEnabled = provider( |
| 137 |
fields = { |
| 138 |
"enabled": "whether fasttable is enabled", |
| 139 |
}, |
| 140 |
) |
| 141 |
|
| 142 |
def fasttable_enabled_impl(ctx): |
| 143 |
raw_setting = ctx.build_setting_value |
| 144 |
|
| 145 |
if raw_setting: |
| 146 |
# TODO(haberman): check that the target CPU supports fasttable. |
| 147 |
pass |
| 148 |
|
| 149 |
return _FastTableEnabled(enabled = raw_setting) |
| 150 |
|
| 151 |
upb_fasttable_enabled = rule( |
| 152 |
implementation = fasttable_enabled_impl, |
| 153 |
build_setting = config.bool(flag = True), |
| 154 |
) |
| 155 |
|
| 156 |
# Dummy rule to expose select() copts to aspects ############################## |
| 157 |
|
| 158 |
_UpbProtoLibraryCopts = provider( |
| 159 |
fields = { |
| 160 |
"copts": "copts for upb_proto_library()", |
| 161 |
}, |
| 162 |
) |
| 163 |
|
| 164 |
def upb_proto_library_copts_impl(ctx): |
| 165 |
return _UpbProtoLibraryCopts(copts = ctx.attr.copts) |
| 166 |
|
| 167 |
upb_proto_library_copts = rule( |
| 168 |
implementation = upb_proto_library_copts_impl, |
| 169 |
attrs = {"copts": attr.string_list(default = [])}, |
| 170 |
) |
| 171 |
|
| 172 |
# upb_proto_library / upb_proto_reflection_library shared code ################# |
| 173 |
|
| 174 |
GeneratedSrcsInfo = provider( |
| 175 |
fields = { |
| 176 |
"srcs": "list of srcs", |
| 177 |
"hdrs": "list of hdrs", |
| 178 |
}, |
| 179 |
) |
| 180 |
|
| 181 |
_UpbWrappedCcInfo = provider(fields = ["cc_info"]) |
| 182 |
_UpbDefsWrappedCcInfo = provider(fields = ["cc_info"]) |
| 183 |
_WrappedGeneratedSrcsInfo = provider(fields = ["srcs"]) |
| 184 |
_WrappedDefsGeneratedSrcsInfo = provider(fields = ["srcs"]) |
| 185 |
|
| 186 |
def _compile_upb_protos(ctx, generator, proto_info, proto_sources): |
| 187 |
if len(proto_sources) == 0: |
| 188 |
return GeneratedSrcsInfo(srcs = [], hdrs = []) |
| 189 |
|
| 190 |
ext = "." + generator |
| 191 |
tool = getattr(ctx.executable, "_gen_" + generator) |
| 192 |
srcs = [_generate_output_file(ctx, name, ext + ".c") for name in proto_sources] |
| 193 |
hdrs = [_generate_output_file(ctx, name, ext + ".h") for name in proto_sources] |
| 194 |
transitive_sets = proto_info.transitive_descriptor_sets.to_list() |
| 195 |
fasttable_enabled = (hasattr(ctx.attr, "_fasttable_enabled") and |
| 196 |
ctx.attr._fasttable_enabled[_FastTableEnabled].enabled) |
| 197 |
codegen_params = "fasttable:" if fasttable_enabled else "" |
| 198 |
ctx.actions.run( |
| 199 |
inputs = depset( |
| 200 |
direct = [proto_info.direct_descriptor_set], |
| 201 |
transitive = [proto_info.transitive_descriptor_sets], |
| 202 |
), |
| 203 |
tools = [tool], |
| 204 |
outputs = srcs + hdrs, |
| 205 |
executable = ctx.executable._protoc, |
| 206 |
arguments = [ |
| 207 |
"--" + generator + "_out=" + codegen_params + _get_real_root(srcs[0]), |
| 208 |
"--plugin=protoc-gen-" + generator + "=" + tool.path, |
| 209 |
"--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]), |
| 210 |
] + |
| 211 |
[_get_real_short_path(file) for file in proto_sources], |
| 212 |
progress_message = "Generating upb protos for :" + ctx.label.name, |
| 213 |
mnemonic = "GenUpbProtos", |
| 214 |
) |
| 215 |
return GeneratedSrcsInfo(srcs = srcs, hdrs = hdrs) |
| 216 |
|
| 217 |
def _upb_proto_rule_impl(ctx): |
| 218 |
if len(ctx.attr.deps) != 1: |
| 219 |
fail("only one deps dependency allowed.") |
| 220 |
dep = ctx.attr.deps[0] |
| 221 |
|
| 222 |
if _WrappedDefsGeneratedSrcsInfo in dep: |
| 223 |
srcs = dep[_WrappedDefsGeneratedSrcsInfo].srcs |
| 224 |
elif _WrappedGeneratedSrcsInfo in dep: |
| 225 |
srcs = dep[_WrappedGeneratedSrcsInfo].srcs |
| 226 |
else: |
| 227 |
fail("proto_library rule must generate _WrappedGeneratedSrcsInfo or " + |
| 228 |
"_WrappedDefsGeneratedSrcsInfo (aspect should have handled this).") |
| 229 |
|
| 230 |
if _UpbDefsWrappedCcInfo in dep: |
| 231 |
cc_info = dep[_UpbDefsWrappedCcInfo].cc_info |
| 232 |
elif _UpbWrappedCcInfo in dep: |
| 233 |
cc_info = dep[_UpbWrappedCcInfo].cc_info |
| 234 |
else: |
| 235 |
fail("proto_library rule must generate _UpbWrappedCcInfo or " + |
| 236 |
"_UpbDefsWrappedCcInfo (aspect should have handled this).") |
| 237 |
|
| 238 |
lib = cc_info.linking_context.linker_inputs.to_list()[0].libraries[0] |
| 239 |
files = _filter_none([ |
| 240 |
lib.static_library, |
| 241 |
lib.pic_static_library, |
| 242 |
lib.dynamic_library, |
| 243 |
]) |
| 244 |
return [ |
| 245 |
DefaultInfo(files = depset(files + srcs.hdrs + srcs.srcs)), |
| 246 |
srcs, |
| 247 |
cc_info, |
| 248 |
] |
| 249 |
|
| 250 |
def _upb_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider): |
| 251 |
proto_info = target[ProtoInfo] |
| 252 |
files = _compile_upb_protos(ctx, generator, proto_info, proto_info.direct_sources) |
| 253 |
deps = ctx.rule.attr.deps + getattr(ctx.attr, "_" + generator) |
| 254 |
dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep] |
| 255 |
dep_ccinfos += [dep[_UpbWrappedCcInfo].cc_info for dep in deps if _UpbWrappedCcInfo in dep] |
| 256 |
dep_ccinfos += [dep[_UpbDefsWrappedCcInfo].cc_info for dep in deps if _UpbDefsWrappedCcInfo in dep] |
| 257 |
if generator == "upbdefs": |
| 258 |
if _UpbWrappedCcInfo not in target: |
| 259 |
fail("Target should have _UpbWrappedCcInfo provider") |
| 260 |
dep_ccinfos += [target[_UpbWrappedCcInfo].cc_info] |
| 261 |
cc_info = _cc_library_func( |
| 262 |
ctx = ctx, |
| 263 |
name = ctx.rule.attr.name + "." + generator, |
| 264 |
hdrs = files.hdrs, |
| 265 |
srcs = files.srcs, |
| 266 |
copts = ctx.attr._copts[_UpbProtoLibraryCopts].copts, |
| 267 |
dep_ccinfos = dep_ccinfos, |
| 268 |
) |
| 269 |
return [cc_provider(cc_info = cc_info), file_provider(srcs = files)] |
| 270 |
|
| 271 |
def _upb_proto_library_aspect_impl(target, ctx): |
| 272 |
return _upb_proto_aspect_impl(target, ctx, "upb", _UpbWrappedCcInfo, _WrappedGeneratedSrcsInfo) |
| 273 |
|
| 274 |
def _upb_proto_reflection_library_aspect_impl(target, ctx): |
| 275 |
return _upb_proto_aspect_impl(target, ctx, "upbdefs", _UpbDefsWrappedCcInfo, _WrappedDefsGeneratedSrcsInfo) |
| 276 |
|
| 277 |
def _maybe_add(d): |
| 278 |
if _is_google3: |
| 279 |
d["_grep_includes"] = attr.label( |
| 280 |
allow_single_file = True, |
| 281 |
cfg = "exec", |
| 282 |
default = "@bazel_tools//tools/cpp:grep-includes", |
| 283 |
) |
| 284 |
return d |
| 285 |
|
| 286 |
# upb_proto_library() ########################################################## |
| 287 |
|
| 288 |
_upb_proto_library_aspect = aspect( |
| 289 |
attrs = _maybe_add({ |
| 290 |
"_copts": attr.label( |
| 291 |
default = "//:upb_proto_library_copts__for_generated_code_only_do_not_use", |
| 292 |
), |
| 293 |
"_gen_upb": attr.label( |
| 294 |
executable = True, |
| 295 |
cfg = "exec", |
| 296 |
default = "//upbc:protoc-gen-upb", |
| 297 |
), |
| 298 |
"_protoc": attr.label( |
| 299 |
executable = True, |
| 300 |
cfg = "exec", |
| 301 |
default = "@com_google_protobuf//:protoc", |
| 302 |
), |
| 303 |
"_cc_toolchain": attr.label( |
| 304 |
default = "@bazel_tools//tools/cpp:current_cc_toolchain", |
| 305 |
), |
| 306 |
"_upb": attr.label_list(default = [ |
| 307 |
"//:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", |
| 308 |
]), |
| 309 |
"_fasttable_enabled": attr.label(default = "//:fasttable_enabled"), |
| 310 |
}), |
| 311 |
implementation = _upb_proto_library_aspect_impl, |
| 312 |
provides = [ |
| 313 |
_UpbWrappedCcInfo, |
| 314 |
_WrappedGeneratedSrcsInfo, |
| 315 |
], |
| 316 |
attr_aspects = ["deps"], |
| 317 |
fragments = ["cpp"], |
| 318 |
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], |
| 319 |
incompatible_use_toolchain_transition = True, |
| 320 |
) |
| 321 |
|
| 322 |
upb_proto_library = rule( |
| 323 |
output_to_genfiles = True, |
| 324 |
implementation = _upb_proto_rule_impl, |
| 325 |
attrs = { |
| 326 |
"deps": attr.label_list( |
| 327 |
aspects = [_upb_proto_library_aspect], |
| 328 |
allow_rules = ["proto_library"], |
| 329 |
providers = [ProtoInfo], |
| 330 |
), |
| 331 |
}, |
| 332 |
) |
| 333 |
|
| 334 |
# upb_proto_reflection_library() ############################################### |
| 335 |
|
| 336 |
_upb_proto_reflection_library_aspect = aspect( |
| 337 |
attrs = _maybe_add({ |
| 338 |
"_copts": attr.label( |
| 339 |
default = "//:upb_proto_library_copts__for_generated_code_only_do_not_use", |
| 340 |
), |
| 341 |
"_gen_upbdefs": attr.label( |
| 342 |
executable = True, |
| 343 |
cfg = "exec", |
| 344 |
default = "//upbc:protoc-gen-upbdefs", |
| 345 |
), |
| 346 |
"_protoc": attr.label( |
| 347 |
executable = True, |
| 348 |
cfg = "exec", |
| 349 |
default = "@com_google_protobuf//:protoc", |
| 350 |
), |
| 351 |
"_cc_toolchain": attr.label( |
| 352 |
default = "@bazel_tools//tools/cpp:current_cc_toolchain", |
| 353 |
), |
| 354 |
"_upbdefs": attr.label_list( |
| 355 |
default = [ |
| 356 |
"//:generated_reflection_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", |
| 357 |
], |
| 358 |
), |
| 359 |
}), |
| 360 |
implementation = _upb_proto_reflection_library_aspect_impl, |
| 361 |
provides = [ |
| 362 |
_UpbDefsWrappedCcInfo, |
| 363 |
_WrappedDefsGeneratedSrcsInfo, |
| 364 |
], |
| 365 |
required_aspect_providers = [ |
| 366 |
_UpbWrappedCcInfo, |
| 367 |
_WrappedGeneratedSrcsInfo, |
| 368 |
], |
| 369 |
attr_aspects = ["deps"], |
| 370 |
fragments = ["cpp"], |
| 371 |
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], |
| 372 |
incompatible_use_toolchain_transition = True, |
| 373 |
) |
| 374 |
|
| 375 |
upb_proto_reflection_library = rule( |
| 376 |
output_to_genfiles = True, |
| 377 |
implementation = _upb_proto_rule_impl, |
| 378 |
attrs = { |
| 379 |
"deps": attr.label_list( |
| 380 |
aspects = [ |
| 381 |
_upb_proto_library_aspect, |
| 382 |
_upb_proto_reflection_library_aspect, |
| 383 |
], |
| 384 |
allow_rules = ["proto_library"], |
| 385 |
providers = [ProtoInfo], |
| 386 |
), |
| 387 |
}, |
| 388 |
) |