|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Starlark rule for generating descriptors that is compatible with Protolite Messages.""" |
| 16 | + |
| 17 | +load("@rules_java//java:defs.bzl", "java_library") |
| 18 | +load("@rules_proto//proto:defs.bzl", "proto_descriptor_set") |
| 19 | +load("//publish:cel_version.bzl", "CEL_VERSION") |
| 20 | + |
| 21 | +def java_lite_proto_cel_library( |
| 22 | + name, |
| 23 | + java_descriptor_class_prefix, |
| 24 | + deps, |
| 25 | + debug = False): |
| 26 | + """Generates a CelLiteDescriptor |
| 27 | +
|
| 28 | + Args: |
| 29 | + name: name of this target. |
| 30 | + java_descriptor_class_prefix: Prefix name for the generated descriptor java class (ex: 'TestAllTypes' generates 'TestAllTypesCelLiteDescriptor.java'). |
| 31 | + deps: Name of the proto_library target. Only a single proto_library is supported at this time. |
| 32 | + debug: (optional) If true, prints additional information during codegen for debugging purposes. |
| 33 | + """ |
| 34 | + if not name: |
| 35 | + fail("You must provide a name.") |
| 36 | + |
| 37 | + if not java_descriptor_class_prefix: |
| 38 | + fail("You must provide a descriptor_class_prefix.") |
| 39 | + |
| 40 | + if not deps: |
| 41 | + fail("You must provide a proto_library dependency.") |
| 42 | + |
| 43 | + if len(deps) > 1: |
| 44 | + fail("You must provide only one proto_library dependency.") |
| 45 | + |
| 46 | + _generate_cel_lite_descriptor_class( |
| 47 | + name, |
| 48 | + java_descriptor_class_prefix + "CelLiteDescriptor", |
| 49 | + deps[0], |
| 50 | + debug, |
| 51 | + ) |
| 52 | + |
| 53 | + descriptor_codegen_deps = [ |
| 54 | + "//protobuf:cel_lite_descriptor", |
| 55 | + ] |
| 56 | + |
| 57 | + java_library( |
| 58 | + name = name, |
| 59 | + srcs = [":" + name + "_cel_lite_descriptor"], |
| 60 | + deps = deps + descriptor_codegen_deps, |
| 61 | + ) |
| 62 | + |
| 63 | +def _generate_cel_lite_descriptor_class( |
| 64 | + name, |
| 65 | + descriptor_class_name, |
| 66 | + proto_src, |
| 67 | + debug): |
| 68 | + outfile = "%s.java" % descriptor_class_name |
| 69 | + |
| 70 | + transitive_descriptor_set_name = "%s_transitive_descriptor_set" % name |
| 71 | + proto_descriptor_set( |
| 72 | + name = transitive_descriptor_set_name, |
| 73 | + deps = [proto_src], |
| 74 | + ) |
| 75 | + |
| 76 | + direct_descriptor_set_name = proto_src |
| 77 | + |
| 78 | + debug_flag = "--debug" if debug else "" |
| 79 | + |
| 80 | + cmd = ( |
| 81 | + "$(location //protobuf:cel_lite_descriptor_generator) " + |
| 82 | + "--descriptor $(location %s) " % direct_descriptor_set_name + |
| 83 | + "--transitive_descriptor_set $(location %s) " % transitive_descriptor_set_name + |
| 84 | + "--descriptor_class_name %s " % descriptor_class_name + |
| 85 | + "--out $(location %s) " % outfile + |
| 86 | + "--version %s " % CEL_VERSION + |
| 87 | + debug_flag |
| 88 | + ) |
| 89 | + |
| 90 | + native.genrule( |
| 91 | + name = name + "_cel_lite_descriptor", |
| 92 | + srcs = [ |
| 93 | + transitive_descriptor_set_name, |
| 94 | + direct_descriptor_set_name, |
| 95 | + ], |
| 96 | + cmd = cmd, |
| 97 | + outs = [outfile], |
| 98 | + tools = ["//protobuf:cel_lite_descriptor_generator"], |
| 99 | + ) |
0 commit comments