1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc. All rights reserved.
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
8 Implementation of proto_library rule.
11 load("@bazel_skylib//lib:paths.bzl", "paths")
12 load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
13 load("@proto_bazel_features//:features.bzl", "bazel_features")
14 load("//bazel/common:proto_common.bzl", "proto_common")
15 load("//bazel/common:proto_info.bzl", "ProtoInfo")
16 load("//bazel/private:toolchain_helpers.bzl", "toolchains")
18 STRICT_DEPS_FLAG_TEMPLATE = (
20 "--direct_dependencies_violation_msg=" +
21 "%%s is imported, but %s doesn't directly depend on a proto_library that 'srcs' it."
24 def _check_srcs_package(target_package, srcs):
25 """Check that .proto files in sources are from the same package.
27 This is done to avoid clashes with the generated sources."""
29 #TODO: this does not work with filegroups that contain files that are not in the package
31 if target_package != src.label.package:
32 fail("Proto source with label '%s' must be in same package as consuming rule." % src.label)
34 def _get_import_prefix(ctx):
35 """Gets and verifies import_prefix attribute if it is declared."""
37 import_prefix = ctx.attr.import_prefix
39 if not paths.is_normalized(import_prefix):
40 fail("should be normalized (without uplevel references or '.' path segments)", attr = "import_prefix")
41 if paths.is_absolute(import_prefix):
42 fail("should be a relative path", attr = "import_prefix")
46 def _get_strip_import_prefix(ctx):
47 """Gets and verifies strip_import_prefix."""
49 strip_import_prefix = ctx.attr.strip_import_prefix
51 if not paths.is_normalized(strip_import_prefix):
52 fail("should be normalized (without uplevel references or '.' path segments)", attr = "strip_import_prefix")
54 if paths.is_absolute(strip_import_prefix):
55 strip_import_prefix = strip_import_prefix[1:]
56 else: # Relative to current package
57 strip_import_prefix = _join(ctx.label.package, strip_import_prefix)
59 return strip_import_prefix.removesuffix("/")
61 def _proto_library_impl(ctx):
62 # Verifies attributes.
63 _check_srcs_package(ctx.label.package, ctx.attr.srcs)
65 deps = [dep[ProtoInfo] for dep in ctx.attr.deps]
66 exports = [dep[ProtoInfo] for dep in ctx.attr.exports]
67 import_prefix = _get_import_prefix(ctx)
68 strip_import_prefix = _get_strip_import_prefix(ctx)
69 check_for_reexport = deps + exports if not srcs else exports
70 _PackageSpecificationInfo = bazel_features.globals.PackageSpecificationInfo
71 for proto in check_for_reexport:
72 if getattr(proto, "allow_exports", None):
73 if not _PackageSpecificationInfo:
74 fail("Allowlist checks not supported before Bazel 6.4.0")
75 if not proto.allow_exports[_PackageSpecificationInfo].contains(ctx.label):
76 fail("proto_library '%s' can't be reexported in package '//%s'" % (proto.direct_descriptor_set.owner, ctx.label.package))
78 proto_path, virtual_srcs = _process_srcs(ctx, srcs, import_prefix, strip_import_prefix)
79 descriptor_set = ctx.actions.declare_file(ctx.label.name + "-descriptor-set.proto.bin")
80 proto_info = ProtoInfo(
83 descriptor_set = descriptor_set,
84 proto_path = proto_path,
85 workspace_root = ctx.label.workspace_root,
86 bin_dir = ctx.bin_dir.path,
87 allow_exports = ctx.attr.allow_exports,
90 _write_descriptor_set(ctx, proto_info, deps, exports, descriptor_set)
92 # We assume that the proto sources will not have conflicting artifacts
93 # with the same root relative path
94 data_runfiles = ctx.runfiles(
95 files = [proto_info.direct_descriptor_set],
96 transitive_files = depset(transitive = [proto_info.transitive_sources]),
101 files = depset([proto_info.direct_descriptor_set]),
102 default_runfiles = ctx.runfiles(), # empty
103 data_runfiles = data_runfiles,
107 def _process_srcs(ctx, srcs, import_prefix, strip_import_prefix):
108 """Returns proto_path and sources, optionally symlinking them to _virtual_imports.
111 (str, [File]) A pair of proto_path and virtual_sources.
113 if import_prefix != "" or strip_import_prefix != "":
114 # Use virtual source roots
115 return _symlink_to_virtual_imports(ctx, srcs, import_prefix, strip_import_prefix)
117 # No virtual source roots
121 return "/".join([p for p in path if p != ""])
123 def _symlink_to_virtual_imports(ctx, srcs, import_prefix, strip_import_prefix):
124 """Symlinks srcs to _virtual_imports.
127 A pair proto_path, directs_sources.
129 virtual_imports = _join("_virtual_imports", ctx.label.name)
130 proto_path = _join(ctx.label.package, virtual_imports)
132 if ctx.label.workspace_name == "":
133 full_strip_import_prefix = strip_import_prefix
135 full_strip_import_prefix = _join("..", ctx.label.workspace_name, strip_import_prefix)
136 if full_strip_import_prefix:
137 full_strip_import_prefix += "/"
141 # Remove strip_import_prefix
142 if not src.short_path.startswith(full_strip_import_prefix):
143 fail(".proto file '%s' is not under the specified strip prefix '%s'" %
144 (src.short_path, full_strip_import_prefix))
145 import_path = src.short_path[len(full_strip_import_prefix):]
148 virtual_src = ctx.actions.declare_file(_join(virtual_imports, import_prefix, import_path))
150 output = virtual_src,
152 progress_message = "Symlinking virtual .proto sources for %{label}",
154 virtual_srcs.append(virtual_src)
155 return proto_path, virtual_srcs
157 def _write_descriptor_set(ctx, proto_info, deps, exports, descriptor_set):
158 """Writes descriptor set."""
159 if proto_info.direct_sources == []:
160 ctx.actions.write(descriptor_set, "")
163 dependencies_descriptor_sets = depset(transitive = [dep.transitive_descriptor_sets for dep in deps])
165 args = ctx.actions.args()
167 if ctx.attr._experimental_proto_descriptor_sets_include_source_info[BuildSettingInfo].value:
168 args.add("--include_source_info")
169 args.add("--retain_options")
171 strict_deps = ctx.attr._strict_proto_deps[BuildSettingInfo].value
173 if proto_info.direct_sources:
174 strict_importable_sources = depset(
175 direct = proto_info._direct_proto_sources,
176 transitive = [dep._exported_sources for dep in deps],
179 strict_importable_sources = None
180 if strict_importable_sources:
182 "--direct_dependencies",
183 strict_importable_sources,
184 map_each = proto_common.get_import_path,
187 # Example: `--direct_dependencies a.proto:b.proto`
190 # The proto compiler requires an empty list to turn on strict deps checking
191 args.add("--direct_dependencies=")
193 # Set `-direct_dependencies_violation_msg=`
194 args.add(ctx.label, format = STRICT_DEPS_FLAG_TEMPLATE)
196 strict_imports = ctx.attr._strict_public_imports[BuildSettingInfo].value
198 public_import_protos = depset(transitive = [export._exported_sources for export in exports])
199 if not public_import_protos:
200 # This line is necessary to trigger the check.
201 args.add("--allowed_public_imports=")
204 "--allowed_public_imports",
205 public_import_protos,
206 map_each = proto_common.get_import_path,
209 if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
210 toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
212 fail("Protocol compiler toolchain could not be resolved.")
213 proto_lang_toolchain_info = toolchain.proto
215 proto_lang_toolchain_info = proto_common.ProtoLangToolchainInfo(
216 out_replacement_format_flag = "--descriptor_set_out=%s",
217 output_files = "single",
218 mnemonic = "GenProtoDescriptorSet",
219 progress_message = "Generating Descriptor Set proto_library %{label}",
220 proto_compiler = ctx.executable._proto_compiler,
221 protoc_opts = ctx.fragments.proto.experimental_protoc_opts,
225 proto_common.compile(
228 proto_lang_toolchain_info,
229 generated_files = [descriptor_set],
230 additional_inputs = dependencies_descriptor_sets,
231 additional_args = args,
234 proto_library = rule(
236 # TODO: proto_common docs are missing
237 # TODO: ProtoInfo link doesn't work and docs are missing
239 <p>If using Bazel, please load the rule from <a href="https://github.com/bazelbuild/rules_proto">
240 https://github.com/bazelbuild/rules_proto</a>.
242 <p>Use <code>proto_library</code> to define libraries of protocol buffers which
243 may be used from multiple languages. A <code>proto_library</code> may be listed
244 in the <code>deps</code> clause of supported rules, such as
245 <code>java_proto_library</code>.
247 <p>When compiled on the command-line, a <code>proto_library</code> creates a file
248 named <code>foo-descriptor-set.proto.bin</code>, which is the descriptor set for
249 the messages the rule srcs. The file is a serialized
250 <code>FileDescriptorSet</code>, which is described in
251 <a href="https://developers.google.com/protocol-buffers/docs/techniques#self-description">
252 https://developers.google.com/protocol-buffers/docs/techniques#self-description</a>.
254 <p>It only contains information about the <code>.proto</code> files directly
255 mentioned by a <code>proto_library</code> rule; the collection of transitive
256 descriptor sets is available through the
257 <code>[ProtoInfo].transitive_descriptor_sets</code> Starlark provider.
258 See documentation in <code>proto_info.bzl</code>.
260 <p>Recommended code organization:
262 <li>One <code>proto_library</code> rule per <code>.proto</code> file.
263 <li>A file named <code>foo.proto</code> will be in a rule named <code>foo_proto</code>,
264 which is located in the same package.
265 <li>A <code>[language]_proto_library</code> that wraps a <code>proto_library</code>
266 named <code>foo_proto</code> should be called <code>foo_[language]_proto</code>,
267 and be located in the same package.
270 "srcs": attr.label_list(
271 allow_files = [".proto", ".protodevel"],
272 flags = ["DIRECT_COMPILE_TIME_INPUT"],
273 # TODO: Should .protodevel be advertised or deprecated?
275 The list of <code>.proto</code> and <code>.protodevel</code> files that are
276 processed to create the target. This is usually a non empty list. One usecase
277 where <code>srcs</code> can be empty is an <i>alias-library</i>. This is a
278 proto_library rule having one or more other proto_library in <code>deps</code>.
279 This pattern can be used to e.g. export a public api under a persistent name.""",
281 "deps": attr.label_list(
282 providers = [ProtoInfo],
284 The list of other <code>proto_library</code> rules that the target depends upon.
285 A <code>proto_library</code> may only depend on other <code>proto_library</code>
286 targets. It may not depend on language-specific libraries.""",
288 "exports": attr.label_list(
289 providers = [ProtoInfo],
291 List of proto_library targets that can be referenced via "import public" in the
293 It's an error if you use "import public" but do not list the corresponding library
294 in the exports attribute.
295 Note that you have list the library both in deps and exports since not all
296 lang_proto_library implementations have been changed yet.""",
298 "strip_import_prefix": attr.string(
301 The prefix to strip from the paths of the .proto files in this rule.
303 <p>When set, .proto source files in the <code>srcs</code> attribute of this rule are
304 accessible at their path with this prefix cut off.
306 <p>If it's a relative path (not starting with a slash), it's taken as a package-relative
307 one. If it's an absolute one, it's understood as a repository-relative path.
309 <p>The prefix in the <code>import_prefix</code> attribute is added after this prefix is
312 "import_prefix": attr.string(
314 The prefix to add to the paths of the .proto files in this rule.
316 <p>When set, the .proto source files in the <code>srcs</code> attribute of this rule are
317 accessible at is the value of this attribute prepended to their repository-relative path.
319 <p>The prefix in the <code>strip_import_prefix</code> attribute is removed before this
322 "allow_exports": attr.label(
324 providers = [bazel_features.globals.PackageSpecificationInfo] if bazel_features.globals.PackageSpecificationInfo else [],
326 An optional allowlist that prevents proto library to be reexported or used in
327 lang_proto_library that is not in one of the listed packages.""",
329 "data": attr.label_list(
331 flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
333 # buildifier: disable=attr-license (calling attr.license())
334 "licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
335 "_experimental_proto_descriptor_sets_include_source_info": attr.label(
336 default = "//bazel/private:experimental_proto_descriptor_sets_include_source_info",
338 "_strict_proto_deps": attr.label(
340 "//bazel/private:strict_proto_deps",
342 "_strict_public_imports": attr.label(
343 default = "//bazel/private:strict_public_imports",
345 } | toolchains.if_legacy_toolchain({
346 "_proto_compiler": attr.label(
350 default = configuration_field("proto", "proto_compiler"),
352 }), # buildifier: disable=attr-licenses (attribute called licenses)
353 fragments = ["proto"],
354 provides = [ProtoInfo],
355 toolchains = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN),