Skip to content

Commit 3d9f6dc

Browse files
honglookercopybara-github
authored andcommitted
rm upb_cc_proto_library.bzl
PiperOrigin-RevId: 673832587
1 parent 9a9e5cf commit 3d9f6dc

File tree

6 files changed

+259
-283
lines changed

6 files changed

+259
-283
lines changed

hpb/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# https://developers.google.com/open-source/licenses/bsd
77

88
load(
9-
"//hpb/bazel:upb_cc_proto_library.bzl",
9+
"//hpb/bazel:hpb_proto_library.bzl",
1010
"upb_cc_proto_library_copts",
1111
)
1212
load(

hpb/backend/upb/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ cc_test(
4747
copts = UPB_DEFAULT_CPPOPTS,
4848
deps = [
4949
":interop",
50-
"//src/google/protobuf/compiler/hpb/tests:test_model_upb_cc_proto",
50+
"//src/google/protobuf/compiler/hpb/tests:test_model_hpb_proto",
5151
"//src/google/protobuf/compiler/hpb/tests:test_model_upb_proto",
5252
"//upb:mem",
5353
"//upb:message",

hpb/bazel/BUILD

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ bzl_library(
1515
name = "hpb_proto_library_bzl",
1616
srcs = ["hpb_proto_library.bzl"],
1717
visibility = ["//visibility:public"],
18-
deps = [
19-
":upb_cc_proto_library_bzl",
20-
"//bazel:upb_proto_library_bzl",
21-
"@bazel_skylib//lib:paths",
22-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
23-
],
24-
)
25-
26-
bzl_library(
27-
name = "upb_cc_proto_library_bzl",
28-
srcs = ["upb_cc_proto_library.bzl"],
29-
visibility = ["//visibility:public"],
3018
deps = [
3119
"//bazel:upb_proto_library_bzl",
3220
"@bazel_skylib//lib:paths",

hpb/bazel/hpb_proto_library.bzl

Lines changed: 256 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,261 @@
99
- hpb_proto_library()
1010
"""
1111

12-
load("//hpb/bazel:upb_cc_proto_library.bzl", "upb_cc_proto_library")
12+
load("@bazel_skylib//lib:paths.bzl", "paths")
13+
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
14+
load("//bazel:upb_proto_library.bzl", "GeneratedSrcsInfo", "UpbWrappedCcInfo", "upb_proto_library_aspect")
15+
16+
def upb_use_cpp_toolchain():
17+
return use_cpp_toolchain()
18+
19+
# Generic support code #########################################################
20+
21+
def _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+
38+
def _get_real_root(file):
39+
real_short_path = _get_real_short_path(file)
40+
return file.path[:-len(real_short_path) - 1]
41+
42+
def _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+
49+
def _filter_none(elems):
50+
return [e for e in elems if e]
51+
52+
def _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+
105+
UpbCcProtoLibraryCoptsInfo = provider(
106+
"Provides copts for upb cc proto targets",
107+
fields = {
108+
"copts": "copts for upb_cc_proto_library()",
109+
},
110+
)
111+
112+
def upb_cc_proto_library_copts_impl(ctx):
113+
return UpbCcProtoLibraryCoptsInfo(copts = ctx.attr.copts)
114+
115+
upb_cc_proto_library_copts = rule(
116+
implementation = upb_cc_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+
123+
def _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+
155+
def _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+
186+
def _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[UpbCcProtoLibraryCoptsInfo].copts,
202+
dep_ccinfos = dep_ccinfos,
203+
)
204+
return [cc_provider(cc_info = cc_info), file_provider(srcs = files)]
205+
206+
def _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:upb_cc_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+
252+
upb_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:upb_cc_proto_library_copts",
265+
),
266+
},
267+
)
13268

14269
hpb_proto_library = upb_cc_proto_library

0 commit comments

Comments
 (0)