|
| 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 | +""" |
| 16 | +Starlark rule for generating descriptors that is compatible with Protolite Messages. |
| 17 | +This is an implementation detail. Clients should use 'java_lite_proto_cel_library' instead. |
| 18 | +""" |
| 19 | + |
| 20 | +load("@rules_java//java:defs.bzl", "java_library") |
| 21 | +load("@rules_proto//proto:defs.bzl", "proto_descriptor_set") |
| 22 | +load("//publish:cel_version.bzl", "CEL_VERSION") |
| 23 | +load("@com_google_protobuf//bazel:java_lite_proto_library.bzl", "java_lite_proto_library") |
| 24 | + |
| 25 | +def java_lite_proto_cel_library_impl( |
| 26 | + name, |
| 27 | + java_descriptor_class_name, |
| 28 | + proto_src, |
| 29 | + java_proto_library_dep, |
| 30 | + debug = False): |
| 31 | + """Generates a CelLiteDescriptor |
| 32 | +
|
| 33 | + Args: |
| 34 | + name: name of this target. |
| 35 | + java_descriptor_class_name: Name of the generated descriptor java class. |
| 36 | + proto_src: Name of the proto_library target. |
| 37 | + 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. |
| 38 | + debug: (optional) If true, prints additional information during codegen for debugging purposes. |
| 39 | + """ |
| 40 | + if not name: |
| 41 | + fail("You must provide a name.") |
| 42 | + |
| 43 | + if not java_descriptor_class_name: |
| 44 | + fail("You must provide a descriptor_class_prefix.") |
| 45 | + |
| 46 | + if not proto_src: |
| 47 | + fail("You must provide a proto_library dependency.") |
| 48 | + |
| 49 | + _generate_cel_lite_descriptor_class( |
| 50 | + name, |
| 51 | + java_descriptor_class_name, |
| 52 | + proto_src, |
| 53 | + debug, |
| 54 | + ) |
| 55 | + |
| 56 | + if not java_proto_library_dep: |
| 57 | + java_proto_library_dep = name + "_java_lite_proto_dep" |
| 58 | + java_lite_proto_library( |
| 59 | + name = java_proto_library_dep, |
| 60 | + deps = [proto_src], |
| 61 | + ) |
| 62 | + |
| 63 | + descriptor_codegen_deps = [ |
| 64 | + "//protobuf:cel_lite_descriptor", |
| 65 | + java_proto_library_dep, |
| 66 | + ] |
| 67 | + |
| 68 | + java_library( |
| 69 | + name = name, |
| 70 | + srcs = [":" + name + "_cel_lite_descriptor"], |
| 71 | + deps = descriptor_codegen_deps, |
| 72 | + ) |
| 73 | + |
| 74 | +def _generate_cel_lite_descriptor_class( |
| 75 | + name, |
| 76 | + descriptor_class_name, |
| 77 | + proto_src, |
| 78 | + debug): |
| 79 | + outfile = "%s.java" % descriptor_class_name |
| 80 | + |
| 81 | + transitive_descriptor_set_name = "%s_transitive_descriptor_set" % name |
| 82 | + proto_descriptor_set( |
| 83 | + name = transitive_descriptor_set_name, |
| 84 | + deps = [proto_src], |
| 85 | + ) |
| 86 | + |
| 87 | + direct_descriptor_set_name = proto_src |
| 88 | + |
| 89 | + debug_flag = "--debug" if debug else "" |
| 90 | + |
| 91 | + cmd = ( |
| 92 | + "$(location //protobuf:cel_lite_descriptor_generator) " + |
| 93 | + "--descriptor $(location %s) " % direct_descriptor_set_name + |
| 94 | + "--transitive_descriptor_set $(location %s) " % transitive_descriptor_set_name + |
| 95 | + "--descriptor_class_name %s " % descriptor_class_name + |
| 96 | + "--out $(location %s) " % outfile + |
| 97 | + "--version %s " % CEL_VERSION + |
| 98 | + debug_flag |
| 99 | + ) |
| 100 | + |
| 101 | + native.genrule( |
| 102 | + name = name + "_cel_lite_descriptor", |
| 103 | + srcs = [ |
| 104 | + transitive_descriptor_set_name, |
| 105 | + direct_descriptor_set_name, |
| 106 | + ], |
| 107 | + cmd = cmd, |
| 108 | + outs = [outfile], |
| 109 | + tools = ["//protobuf:cel_lite_descriptor_generator"], |
| 110 | + ) |
0 commit comments