• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Testing function for proto_common module"""
9
10load("//bazel/common:proto_common.bzl", "proto_common")
11
12def _resource_set_callback(_os, inputs_size):
13    return {"memory": 25 + 0.15 * inputs_size, "cpu": 1}
14
15def _impl(ctx):
16    outfile = ctx.actions.declare_file(ctx.attr.name)
17    kwargs = {}
18    if ctx.attr.plugin_output == "single":
19        kwargs["plugin_output"] = outfile.path
20    elif ctx.attr.plugin_output == "multiple":
21        kwargs["plugin_output"] = ctx.bin_dir.path
22    elif ctx.attr.plugin_output == "wrong":
23        kwargs["plugin_output"] = ctx.bin_dir.path + "///"
24    if ctx.attr.additional_args:
25        additional_args = ctx.actions.args()
26        additional_args.add_all(ctx.attr.additional_args)
27        kwargs["additional_args"] = additional_args
28    if ctx.files.additional_tools:
29        kwargs["additional_tools"] = ctx.files.additional_tools
30    if ctx.files.additional_inputs:
31        kwargs["additional_inputs"] = depset(ctx.files.additional_inputs)
32    if ctx.attr.use_resource_set:
33        kwargs["resource_set"] = _resource_set_callback
34    if ctx.attr.progress_message:
35        kwargs["experimental_progress_message"] = ctx.attr.progress_message
36    proto_common.compile(
37        ctx.actions,
38        ctx.attr.proto_dep[ProtoInfo],
39        ctx.attr.toolchain[proto_common.ProtoLangToolchainInfo],
40        [outfile],
41        **kwargs
42    )
43    return [DefaultInfo(files = depset([outfile]))]
44
45compile_rule = rule(
46    _impl,
47    attrs = {
48        "proto_dep": attr.label(),
49        "plugin_output": attr.string(),
50        "toolchain": attr.label(default = ":toolchain"),
51        "additional_args": attr.string_list(),
52        "additional_tools": attr.label_list(cfg = "exec"),
53        "additional_inputs": attr.label_list(allow_files = True),
54        "use_resource_set": attr.bool(),
55        "progress_message": attr.string(),
56    },
57)
58