blob: 108582e5cbf45c445cdf591be4c72fd2d090483f [file] [log] [blame]
Adam Cozzette501ecec2023-09-26 14:36:20 -07001"""upb_c_proto_library() exposes upb's generated C API for protobuf (foo.upb.h)"""
2
3load("//bazel:upb_minitable_proto_library.bzl", "UpbMinitableCcInfo", "upb_minitable_proto_library_aspect")
4load("//bazel:upb_proto_library_internal/aspect.bzl", "upb_proto_aspect_impl")
5load("//bazel:upb_proto_library_internal/cc_library_func.bzl", "upb_use_cpp_toolchain")
6load("//bazel:upb_proto_library_internal/rule.bzl", "upb_proto_rule_impl")
7
8UpbWrappedCcInfo = provider(
9 "Provider for cc_info for protos",
10 fields = ["cc_info", "cc_info_with_thunks"],
11)
12
13_UpbWrappedGeneratedSrcsInfo = provider(
14 "Provider for generated sources",
15 fields = ["srcs"],
16)
17
18def _upb_c_proto_library_aspect_impl(target, ctx):
19 return upb_proto_aspect_impl(
20 target = target,
21 ctx = ctx,
22 generator = "upb",
23 cc_provider = UpbWrappedCcInfo,
24 dep_cc_provider = UpbMinitableCcInfo,
25 file_provider = _UpbWrappedGeneratedSrcsInfo,
26 provide_cc_shared_library_hints = False,
27 )
28
29upb_c_proto_library_aspect = aspect(
30 attrs = {
31 "_copts": attr.label(
32 default = "//upb:upb_proto_library_copts__for_generated_code_only_do_not_use",
33 ),
Joshua Haberman2c7193b2023-09-27 13:26:21 -070034 "_upb_toolchain": attr.label(
Adam Cozzette12c7bb02023-09-28 12:54:11 -070035 default = Label("//upb_generator:protoc-gen-upb_toolchain"),
Adam Cozzette501ecec2023-09-26 14:36:20 -070036 ),
37 "_cc_toolchain": attr.label(
38 default = "@bazel_tools//tools/cpp:current_cc_toolchain",
39 ),
Adam Cozzette501ecec2023-09-26 14:36:20 -070040 },
41 implementation = _upb_c_proto_library_aspect_impl,
42 requires = [upb_minitable_proto_library_aspect],
43 required_aspect_providers = [UpbMinitableCcInfo],
44 provides = [
45 UpbWrappedCcInfo,
46 _UpbWrappedGeneratedSrcsInfo,
47 ],
48 attr_aspects = ["deps"],
49 fragments = ["cpp"],
50 toolchains = upb_use_cpp_toolchain(),
Joshua Haberman2c7193b2023-09-27 13:26:21 -070051 exec_groups = {
52 "proto_compiler": exec_group(),
53 },
Adam Cozzette501ecec2023-09-26 14:36:20 -070054)
55
56def _upb_c_proto_library_rule_impl(ctx):
57 return upb_proto_rule_impl(ctx, UpbWrappedCcInfo, _UpbWrappedGeneratedSrcsInfo)
58
59upb_c_proto_library = rule(
60 output_to_genfiles = True,
61 implementation = _upb_c_proto_library_rule_impl,
62 attrs = {
63 "deps": attr.label_list(
64 aspects = [upb_c_proto_library_aspect],
65 allow_rules = ["proto_library"],
66 providers = [ProtoInfo],
67 ),
68 },
69 provides = [CcInfo],
70)