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_cc_proto_library()
28"""
29
30load("@bazel_skylib//lib:paths.bzl", "paths")
31load("//bazel:upb_proto_library.bzl", "GeneratedSrcsInfo", "UpbWrappedCcInfo", "upb_proto_library_aspect")
32
33# begin:google_only
34# load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
35#
36# end:google_only
37# begin:github_only
38# Compatibility code for Bazel 4.x. Remove this when we drop support for Bazel 4.x.
39load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
40
41def use_cpp_toolchain():
42    return ["@bazel_tools//tools/cpp:toolchain_type"]
43# end:github_only
44
45# Generic support code #########################################################
46
47# begin:github_only
48_is_google3 = False
49# end:github_only
50
51# begin:google_only
52# _is_google3 = True
53# end:google_only
54
55def _get_real_short_path(file):
56    # For some reason, files from other archives have short paths that look like:
57    #   ../com_google_protobuf/google/protobuf/descriptor.proto
58    short_path = file.short_path
59    if short_path.startswith("../"):
60        second_slash = short_path.index("/", 3)
61        short_path = short_path[second_slash + 1:]
62
63    # Sometimes it has another few prefixes like:
64    #   _virtual_imports/any_proto/google/protobuf/any.proto
65    #   benchmarks/_virtual_imports/100_msgs_proto/benchmarks/100_msgs.proto
66    # We want just google/protobuf/any.proto.
67    virtual_imports = "_virtual_imports/"
68    if virtual_imports in short_path:
69        short_path = short_path.split(virtual_imports)[1].split("/", 1)[1]
70    return short_path
71
72def _get_real_root(file):
73    real_short_path = _get_real_short_path(file)
74    return file.path[:-len(real_short_path) - 1]
75
76def _generate_output_file(ctx, src, extension):
77    real_short_path = _get_real_short_path(src)
78    real_short_path = paths.relativize(real_short_path, ctx.label.package)
79    output_filename = paths.replace_extension(real_short_path, extension)
80    ret = ctx.actions.declare_file(output_filename)
81    return ret
82
83def _filter_none(elems):
84    out = []
85    for elem in elems:
86        if elem:
87            out.append(elem)
88    return out
89
90def _cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos):
91    """Like cc_library(), but callable from rules.
92
93    Args:
94      ctx: Rule context.
95      name: Unique name used to generate output files.
96      hdrs: Public headers that can be #included from other rules.
97      srcs: C/C++ source files.
98      copts: Additional options for cc compilation.
99      dep_ccinfos: CcInfo providers of dependencies we should build/link against.
100
101    Returns:
102      CcInfo provider for this compilation.
103    """
104
105    compilation_contexts = [info.compilation_context for info in dep_ccinfos]
106    linking_contexts = [info.linking_context for info in dep_ccinfos]
107    toolchain = find_cpp_toolchain(ctx)
108    feature_configuration = cc_common.configure_features(
109        ctx = ctx,
110        cc_toolchain = toolchain,
111        requested_features = ctx.features,
112        unsupported_features = ctx.disabled_features,
113    )
114
115    blaze_only_args = {}
116
117    if _is_google3:
118        blaze_only_args["grep_includes"] = ctx.file._grep_includes
119
120    (compilation_context, compilation_outputs) = cc_common.compile(
121        actions = ctx.actions,
122        feature_configuration = feature_configuration,
123        cc_toolchain = toolchain,
124        name = name,
125        srcs = srcs,
126        public_hdrs = hdrs,
127        user_compile_flags = copts,
128        compilation_contexts = compilation_contexts,
129        **blaze_only_args
130    )
131
132    # buildifier: disable=unused-variable
133    (linking_context, linking_outputs) = cc_common.create_linking_context_from_compilation_outputs(
134        actions = ctx.actions,
135        name = name,
136        feature_configuration = feature_configuration,
137        cc_toolchain = toolchain,
138        compilation_outputs = compilation_outputs,
139        linking_contexts = linking_contexts,
140        **blaze_only_args
141    )
142
143    return CcInfo(
144        compilation_context = compilation_context,
145        linking_context = linking_context,
146    )
147
148# Dummy rule to expose select() copts to aspects  ##############################
149
150UpbCcProtoLibraryCoptsInfo = provider(
151    "Provides copts for upb cc proto targets",
152    fields = {
153        "copts": "copts for upb_cc_proto_library()",
154    },
155)
156
157def upb_cc_proto_library_copts_impl(ctx):
158    return UpbCcProtoLibraryCoptsInfo(copts = ctx.attr.copts)
159
160upb_cc_proto_library_copts = rule(
161    implementation = upb_cc_proto_library_copts_impl,
162    attrs = {"copts": attr.string_list(default = [])},
163)
164
165_UpbCcWrappedCcInfo = provider("Provider for cc_info for protos", fields = ["cc_info"])
166_WrappedCcGeneratedSrcsInfo = provider("Provider for generated sources", fields = ["srcs"])
167
168def _compile_upb_cc_protos(ctx, generator, proto_info, proto_sources):
169    if len(proto_sources) == 0:
170        return GeneratedSrcsInfo(srcs = [], hdrs = [])
171
172    tool = getattr(ctx.executable, "_gen_" + generator)
173    srcs = [_generate_output_file(ctx, name, ".upb.proto.cc") for name in proto_sources]
174    hdrs = [_generate_output_file(ctx, name, ".upb.proto.h") for name in proto_sources]
175    hdrs += [_generate_output_file(ctx, name, ".upb.fwd.h") for name in proto_sources]
176    transitive_sets = proto_info.transitive_descriptor_sets.to_list()
177
178    args = ctx.actions.args()
179    args.use_param_file(param_file_arg = "@%s")
180    args.set_param_file_format("multiline")
181
182    args.add("--" + generator + "_out=" + _get_real_root(srcs[0]))
183    args.add("--plugin=protoc-gen-" + generator + "=" + tool.path)
184    args.add("--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]))
185    args.add_all(proto_sources, map_each = _get_real_short_path)
186
187    ctx.actions.run(
188        inputs = depset(
189            direct = [proto_info.direct_descriptor_set],
190            transitive = [proto_info.transitive_descriptor_sets],
191        ),
192        tools = [tool],
193        outputs = srcs + hdrs,
194        executable = ctx.executable._protoc,
195        arguments = [args],
196        progress_message = "Generating upb cc protos for :" + ctx.label.name,
197    )
198    return GeneratedSrcsInfo(srcs = srcs, hdrs = hdrs)
199
200def _upb_cc_proto_rule_impl(ctx):
201    if len(ctx.attr.deps) != 1:
202        fail("only one deps dependency allowed.")
203    dep = ctx.attr.deps[0]
204
205    if _WrappedCcGeneratedSrcsInfo in dep:
206        srcs = dep[_WrappedCcGeneratedSrcsInfo].srcs
207    else:
208        fail("proto_library rule must generate _WrappedCcGeneratedSrcsInfo (aspect should have " +
209             "handled this).")
210
211    if _UpbCcWrappedCcInfo in dep:
212        cc_info = dep[_UpbCcWrappedCcInfo].cc_info
213    elif UpbWrappedCcInfo in dep:
214        cc_info = dep[UpbWrappedCcInfo].cc_info
215    else:
216        fail("proto_library rule must generate UpbWrappedCcInfo or " +
217             "_UpbCcWrappedCcInfo (aspect should have handled this).")
218
219    lib = cc_info.linking_context.linker_inputs.to_list()[0].libraries[0]
220    files = _filter_none([
221        lib.static_library,
222        lib.pic_static_library,
223        lib.dynamic_library,
224    ])
225    return [
226        DefaultInfo(files = depset(files + srcs.hdrs + srcs.srcs)),
227        srcs,
228        cc_info,
229    ]
230
231def _upb_cc_proto_aspect_impl(target, ctx, generator, cc_provider, file_provider):
232    proto_info = target[ProtoInfo]
233    files = _compile_upb_cc_protos(ctx, generator, proto_info, proto_info.direct_sources)
234    deps = ctx.rule.attr.deps + getattr(ctx.attr, "_" + generator)
235    dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep]
236    dep_ccinfos += [dep[UpbWrappedCcInfo].cc_info for dep in deps if UpbWrappedCcInfo in dep]
237    dep_ccinfos += [dep[_UpbCcWrappedCcInfo].cc_info for dep in deps if _UpbCcWrappedCcInfo in dep]
238    if UpbWrappedCcInfo not in target:
239        fail("Target should have UpbWrappedCcInfo provider")
240    dep_ccinfos.append(target[UpbWrappedCcInfo].cc_info)
241    cc_info = _cc_library_func(
242        ctx = ctx,
243        name = ctx.rule.attr.name + "." + generator,
244        hdrs = files.hdrs,
245        srcs = files.srcs,
246        copts = ctx.attr._ccopts[UpbCcProtoLibraryCoptsInfo].copts,
247        dep_ccinfos = dep_ccinfos,
248    )
249    return [cc_provider(cc_info = cc_info), file_provider(srcs = files)]
250
251def _upb_cc_proto_library_aspect_impl(target, ctx):
252    return _upb_cc_proto_aspect_impl(target, ctx, "upbprotos", _UpbCcWrappedCcInfo, _WrappedCcGeneratedSrcsInfo)
253
254def _maybe_add(d):
255    if _is_google3:
256        d["_grep_includes"] = attr.label(
257            allow_single_file = True,
258            cfg = "exec",
259            default = "@bazel_tools//tools/cpp:grep-includes",
260        )
261    return d
262
263_upb_cc_proto_library_aspect = aspect(
264    attrs = _maybe_add({
265        "_ccopts": attr.label(
266            default = "//protos:upb_cc_proto_library_copts__for_generated_code_only_do_not_use",
267        ),
268        "_gen_upbprotos": attr.label(
269            executable = True,
270            cfg = "exec",
271            default = "//protos_generator:protoc-gen-upb-protos",
272        ),
273        "_protoc": attr.label(
274            executable = True,
275            cfg = "exec",
276            default = "@com_google_protobuf//:protoc",
277        ),
278        "_cc_toolchain": attr.label(
279            default = "@bazel_tools//tools/cpp:current_cc_toolchain",
280        ),
281        "_upbprotos": attr.label_list(
282            default = [
283                # TODO: Add dependencies for cc runtime (absl/string etc..)
284                "//:generated_cpp_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
285                "//protos:generated_protos_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
286                "@com_google_absl//absl/strings",
287                "@com_google_absl//absl/status:statusor",
288                "//protos",
289            ],
290        ),
291    }),
292    implementation = _upb_cc_proto_library_aspect_impl,
293    provides = [
294        _UpbCcWrappedCcInfo,
295        _WrappedCcGeneratedSrcsInfo,
296    ],
297    required_aspect_providers = [
298        UpbWrappedCcInfo,
299    ],
300    attr_aspects = ["deps"],
301    fragments = ["cpp"],
302    toolchains = use_cpp_toolchain(),
303    incompatible_use_toolchain_transition = True,
304)
305
306upb_cc_proto_library = rule(
307    output_to_genfiles = True,
308    implementation = _upb_cc_proto_rule_impl,
309    attrs = {
310        "deps": attr.label_list(
311            aspects = [
312                upb_proto_library_aspect,
313                _upb_cc_proto_library_aspect,
314            ],
315            allow_rules = ["proto_library"],
316            providers = [ProtoInfo],
317        ),
318        "_ccopts": attr.label(
319            default = "//protos:upb_cc_proto_library_copts__for_generated_code_only_do_not_use",
320        ),
321    },
322)
323