|
| 1 | +# Protocol Buffers - Google's data interchange format |
| 2 | +# Copyright 2024 Google Inc. 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 | +"""Bazel's implementation of cc_proto_library""" |
| 9 | + |
| 10 | +load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain") |
| 11 | +load("//bazel/common:proto_common.bzl", "proto_common") |
| 12 | +load("//bazel/common:proto_info.bzl", "ProtoInfo") |
| 13 | +load("//bazel/private:cc_proto_support.bzl", "cc_proto_compile_and_link") |
| 14 | +load("//bazel/private:toolchain_helpers.bzl", "toolchains") |
| 15 | + |
| 16 | +_CC_PROTO_TOOLCHAIN = "@rules_cc//cc/proto:toolchain_type" |
| 17 | + |
| 18 | +_ProtoCcFilesInfo = provider(fields = ["files"], doc = "Provide cc proto files.") |
| 19 | +_ProtoCcHeaderInfo = provider(fields = ["headers"], doc = "Provide cc proto headers.") |
| 20 | + |
| 21 | +def _get_output_files(actions, proto_info, suffixes): |
| 22 | + result = [] |
| 23 | + for suffix in suffixes: |
| 24 | + result.extend(proto_common.declare_generated_files( |
| 25 | + actions = actions, |
| 26 | + proto_info = proto_info, |
| 27 | + extension = suffix, |
| 28 | + )) |
| 29 | + return result |
| 30 | + |
| 31 | +# TODO: Make this code actually work. |
| 32 | +def _get_strip_include_prefix(ctx, proto_info): |
| 33 | + proto_root = proto_info.proto_source_root |
| 34 | + if proto_root == "." or proto_root == ctx.label.workspace_root: |
| 35 | + return "" |
| 36 | + strip_include_prefix = "" |
| 37 | + if proto_root.startswith(ctx.bin_dir.path): |
| 38 | + proto_root = proto_root[len(ctx.bin_dir.path) + 1:] |
| 39 | + elif proto_root.startswith(ctx.genfiles_dir.path): |
| 40 | + proto_root = proto_root[len(ctx.genfiles_dir.path) + 1:] |
| 41 | + |
| 42 | + if proto_root.startswith(ctx.label.workspace_root): |
| 43 | + proto_root = proto_root[len(ctx.label.workspace_root):] |
| 44 | + |
| 45 | + strip_include_prefix = "//" + proto_root |
| 46 | + return strip_include_prefix |
| 47 | + |
| 48 | +def _aspect_impl(target, ctx): |
| 49 | + proto_info = target[ProtoInfo] |
| 50 | + proto_configuration = ctx.fragments.proto |
| 51 | + |
| 52 | + sources = [] |
| 53 | + headers = [] |
| 54 | + textual_hdrs = [] |
| 55 | + |
| 56 | + proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", _CC_PROTO_TOOLCHAIN) |
| 57 | + should_generate_code = proto_common.experimental_should_generate_code(proto_info, proto_toolchain, "cc_proto_library", target.label) |
| 58 | + |
| 59 | + if should_generate_code: |
| 60 | + if len(proto_info.direct_sources) != 0: |
| 61 | + # Bazel 7 didn't expose cc_proto_library_source_suffixes used by Kythe |
| 62 | + # gradually falling back to .pb.cc |
| 63 | + if type(proto_configuration.cc_proto_library_source_suffixes) == "builtin_function_or_method": |
| 64 | + source_suffixes = [".pb.cc"] |
| 65 | + header_suffixes = [".pb.h"] |
| 66 | + else: |
| 67 | + source_suffixes = proto_configuration.cc_proto_library_source_suffixes |
| 68 | + header_suffixes = proto_configuration.cc_proto_library_header_suffixes |
| 69 | + sources = _get_output_files(ctx.actions, proto_info, source_suffixes) |
| 70 | + headers = _get_output_files(ctx.actions, proto_info, header_suffixes) |
| 71 | + header_provider = _ProtoCcHeaderInfo(headers = depset(headers)) |
| 72 | + else: |
| 73 | + # If this proto_library doesn't have sources, it provides the combined headers of all its |
| 74 | + # direct dependencies. Thus, if a direct dependency does have sources, the generated files |
| 75 | + # are also provided by this library. If a direct dependency does not have sources, it will |
| 76 | + # do the same thing, so that effectively this library looks through all source-less |
| 77 | + # proto_libraries and provides all generated headers of the proto_libraries with sources |
| 78 | + # that it depends on. |
| 79 | + transitive_headers = [] |
| 80 | + for dep in getattr(ctx.rule.attr, "deps", []): |
| 81 | + if _ProtoCcHeaderInfo in dep: |
| 82 | + textual_hdrs.extend(dep[_ProtoCcHeaderInfo].headers.to_list()) |
| 83 | + transitive_headers.append(dep[_ProtoCcHeaderInfo].headers) |
| 84 | + header_provider = _ProtoCcHeaderInfo(headers = depset(transitive = transitive_headers)) |
| 85 | + |
| 86 | + else: # shouldn't generate code |
| 87 | + header_provider = _ProtoCcHeaderInfo(headers = depset()) |
| 88 | + |
| 89 | + proto_common.compile( |
| 90 | + actions = ctx.actions, |
| 91 | + proto_info = proto_info, |
| 92 | + proto_lang_toolchain_info = proto_toolchain, |
| 93 | + generated_files = sources + headers, |
| 94 | + experimental_output_files = "multiple", |
| 95 | + ) |
| 96 | + |
| 97 | + deps = [] |
| 98 | + if proto_toolchain.runtime: |
| 99 | + deps = [proto_toolchain.runtime] |
| 100 | + deps.extend(getattr(ctx.rule.attr, "deps", [])) |
| 101 | + |
| 102 | + cc_info, libraries, temps = cc_proto_compile_and_link( |
| 103 | + ctx = ctx, |
| 104 | + deps = deps, |
| 105 | + sources = sources, |
| 106 | + headers = headers, |
| 107 | + textual_hdrs = textual_hdrs, |
| 108 | + strip_include_prefix = _get_strip_include_prefix(ctx, proto_info), |
| 109 | + ) |
| 110 | + |
| 111 | + return [ |
| 112 | + cc_info, |
| 113 | + _ProtoCcFilesInfo(files = depset(sources + headers + libraries)), |
| 114 | + OutputGroupInfo(temp_files_INTERNAL_ = temps), |
| 115 | + header_provider, |
| 116 | + ] |
| 117 | + |
| 118 | +cc_proto_aspect = aspect( |
| 119 | + implementation = _aspect_impl, |
| 120 | + attr_aspects = ["deps"], |
| 121 | + fragments = ["cpp", "proto"], |
| 122 | + required_providers = [ProtoInfo], |
| 123 | + provides = [CcInfo], |
| 124 | + attrs = toolchains.if_legacy_toolchain({"_aspect_cc_proto_toolchain": attr.label( |
| 125 | + default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"), |
| 126 | + )}), |
| 127 | + toolchains = use_cc_toolchain() + toolchains.use_toolchain(_CC_PROTO_TOOLCHAIN), |
| 128 | +) |
| 129 | + |
| 130 | +def _cc_proto_library_impl(ctx): |
| 131 | + if len(ctx.attr.deps) != 1: |
| 132 | + fail( |
| 133 | + "'deps' attribute must contain exactly one label " + |
| 134 | + "(we didn't name it 'dep' for consistency). " + |
| 135 | + "The main use-case for multiple deps is to create a rule that contains several " + |
| 136 | + "other targets. This makes dependency bloat more likely. It also makes it harder" + |
| 137 | + "to remove unused deps.", |
| 138 | + attr = "deps", |
| 139 | + ) |
| 140 | + dep = ctx.attr.deps[0] |
| 141 | + |
| 142 | + proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", _CC_PROTO_TOOLCHAIN) |
| 143 | + proto_common.check_collocated(ctx.label, dep[ProtoInfo], proto_toolchain) |
| 144 | + |
| 145 | + return [DefaultInfo(files = dep[_ProtoCcFilesInfo].files), dep[CcInfo], dep[OutputGroupInfo]] |
| 146 | + |
| 147 | +cc_proto_library = rule( |
| 148 | + implementation = _cc_proto_library_impl, |
| 149 | + doc = """ |
| 150 | +<p> |
| 151 | +<code>cc_proto_library</code> generates C++ code from <code>.proto</code> files. |
| 152 | +</p> |
| 153 | +
|
| 154 | +<p> |
| 155 | +<code>deps</code> must point to <a href="protocol-buffer.html#proto_library"><code>proto_library |
| 156 | +</code></a> rules. |
| 157 | +</p> |
| 158 | +
|
| 159 | +<p> |
| 160 | +Example: |
| 161 | +</p> |
| 162 | +
|
| 163 | +<pre> |
| 164 | +<code class="lang-starlark"> |
| 165 | +cc_library( |
| 166 | + name = "lib", |
| 167 | + deps = [":foo_cc_proto"], |
| 168 | +) |
| 169 | +
|
| 170 | +cc_proto_library( |
| 171 | + name = "foo_cc_proto", |
| 172 | + deps = [":foo_proto"], |
| 173 | +) |
| 174 | +
|
| 175 | +proto_library( |
| 176 | + name = "foo_proto", |
| 177 | +) |
| 178 | +</code> |
| 179 | +</pre> |
| 180 | +""", |
| 181 | + attrs = { |
| 182 | + "deps": attr.label_list( |
| 183 | + aspects = [cc_proto_aspect], |
| 184 | + allow_rules = ["proto_library"], |
| 185 | + allow_files = False, |
| 186 | + doc = """ |
| 187 | +The list of <a href="protocol-buffer.html#proto_library"><code>proto_library</code></a> |
| 188 | +rules to generate C++ code for.""", |
| 189 | + ), |
| 190 | + } | toolchains.if_legacy_toolchain({ |
| 191 | + "_aspect_cc_proto_toolchain": attr.label( |
| 192 | + default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"), |
| 193 | + ), |
| 194 | + }), |
| 195 | + provides = [CcInfo], |
| 196 | + toolchains = toolchains.use_toolchain(_CC_PROTO_TOOLCHAIN), |
| 197 | +) |
0 commit comments