-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathjava_lite_proto_cel_library_impl.bzl
More file actions
132 lines (115 loc) · 4.73 KB
/
java_lite_proto_cel_library_impl.bzl
File metadata and controls
132 lines (115 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Starlark rule for generating descriptors that is compatible with Protolite Messages.
This is an implementation detail. Clients should use 'java_lite_proto_cel_library' instead.
"""
load("@rules_java//java:defs.bzl", "java_library")
load("//publish:cel_version.bzl", "CEL_VERSION")
load("@com_google_protobuf//bazel:java_lite_proto_library.bzl", "java_lite_proto_library")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
def java_lite_proto_cel_library_impl(
name,
deps,
java_proto_library_dep,
constraints = [],
java_descriptor_class_suffix = None,
debug = False):
"""Generates a CelLiteDescriptor
Args:
name: Name of this target.
deps: The list of proto_library rules to generate Java code for.
java_descriptor_class_suffix (optional): Suffix for the Java class name of the generated CEL lite descriptor.
Default is "CelLiteDescriptor".
constraints: (optional) List of strings that denote which environment the produced java_library label is associated in.
java_proto_library_dep: (optional) Uses the provided java_lite_proto_library or java_proto_library to generate the lite descriptors.
If none is provided, java_lite_proto_library is used by default behind the scenes. Most use cases should not need to provide this.
debug: (optional) If true, prints additional information during codegen for debugging purposes.
"""
if not name:
fail("You must provide a name.")
if not deps or len(deps) < 1:
fail("You must provide at least one proto_library dependency.")
generated = name + "_cel_lite_descriptor"
java_lite_proto_cel_library_rule(
name = generated,
debug = debug,
descriptors = deps,
java_descriptor_class_suffix = java_descriptor_class_suffix,
)
if not java_proto_library_dep:
java_proto_library_dep = name + "_java_lite_proto_dep"
java_lite_proto_library(
name = java_proto_library_dep,
deps = deps,
)
descriptor_codegen_deps = [
"//common/annotations",
"//protobuf:cel_lite_descriptor",
java_proto_library_dep,
]
java_library(
name = name,
srcs = [":" + generated],
deps = descriptor_codegen_deps,
)
def _generate_cel_lite_descriptor_class(ctx):
srcjar_output = ctx.actions.declare_file(ctx.attr.name + ".srcjar")
java_file_path = srcjar_output.path
direct_descriptor_set = depset(
direct = [
descriptor[ProtoInfo].direct_descriptor_set
for descriptor in ctx.attr.descriptors
],
)
transitive_descriptor_set = depset(
transitive = [
descriptor[ProtoInfo].transitive_descriptor_sets
for descriptor in ctx.attr.descriptors
],
)
args = ctx.actions.args()
args.add("--version", CEL_VERSION)
args.add_joined("--descriptor_set", direct_descriptor_set, join_with = ",")
args.add_joined("--transitive_descriptor_set", transitive_descriptor_set, join_with = ",")
args.add("--out", java_file_path)
if ctx.attr.java_descriptor_class_suffix:
args.add("--overridden_descriptor_class_suffix", ctx.attr.java_descriptor_class_suffix)
if ctx.attr.debug:
args.add("--debug")
ctx.actions.run(
mnemonic = "CelLiteDescriptorGenerator",
arguments = [args],
inputs = transitive_descriptor_set,
outputs = [srcjar_output],
progress_message = "Generating CelLiteDescriptor for: " + ctx.attr.name,
executable = ctx.executable._tool,
)
return [DefaultInfo(files = depset([srcjar_output]))]
java_lite_proto_cel_library_rule = rule(
implementation = _generate_cel_lite_descriptor_class,
attrs = {
"java_descriptor_class_suffix": attr.string(),
"descriptors": attr.label_list(
providers = [ProtoInfo],
),
"debug": attr.bool(),
"_tool": attr.label(
executable = True,
cfg = "exec",
allow_files = True,
default = Label("//protobuf:cel_lite_descriptor_generator"),
),
},
)