• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2021 The Android Open Source Project
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#     http://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
15load("//build/bazel/rules:proto_file_utils.bzl", "proto_file_utils")
16load(":rules.bzl", "java_library")
17
18def _java_proto_sources_gen_rule_impl(ctx):
19    out_flags = []
20    plugin_executable = None
21    out_arg = None
22    if ctx.attr.plugin:
23        plugin_executable = ctx.executable.plugin
24    else:
25        out_arg = "--java_out"
26        if ctx.attr.out_format:
27            out_flags.append(ctx.attr.out_format)
28
29    srcs = []
30    proto_infos = []
31
32    for dep in ctx.attr.deps:
33        proto_infos.append(dep[ProtoInfo])
34
35    out_jar = _generate_java_proto_action(
36        proto_infos = proto_infos,
37        protoc = ctx.executable._protoc,
38        ctx = ctx,
39        out_flags = out_flags,
40        plugin_executable = plugin_executable,
41        out_arg = out_arg,
42    )
43    srcs.append(out_jar)
44
45    return [
46        DefaultInfo(files = depset(direct = srcs)),
47    ]
48
49_java_proto_sources_gen = rule(
50    implementation = _java_proto_sources_gen_rule_impl,
51    attrs = {
52        "deps": attr.label_list(
53            providers = [ProtoInfo],
54            doc = """
55proto_library or any other target exposing ProtoInfo provider with *.proto files
56""",
57            mandatory = True,
58        ),
59        "_protoc": attr.label(
60            default = Label("//external/protobuf:aprotoc"),
61            executable = True,
62            cfg = "exec",
63        ),
64        "plugin": attr.label(
65            executable = True,
66            cfg = "exec",
67        ),
68        "out_format": attr.string(
69            doc = """
70Optional argument specifying the out format, e.g. lite.
71If not provided, defaults to full protos.
72""",
73        ),
74    },
75    toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
76)
77
78def _generate_java_proto_action(
79        proto_infos,
80        protoc,
81        ctx,
82        plugin_executable,
83        out_arg,
84        out_flags):
85    return proto_file_utils.generate_jar_proto_action(
86        proto_infos,
87        protoc,
88        ctx,
89        out_flags,
90        plugin_executable = plugin_executable,
91        out_arg = out_arg,
92        mnemonic = "JavaProtoGen",
93    )
94
95def _java_proto_library(
96        name,
97        deps = [],
98        plugin = None,
99        out_format = None,
100        proto_dep = None,
101        sdk_version = "core_current",
102        **kwargs):
103    proto_sources_name = name + "_proto_gen"
104
105    _java_proto_sources_gen(
106        name = proto_sources_name,
107        deps = deps,
108        plugin = plugin,
109        out_format = out_format,
110        tags = ["manual"],
111    )
112
113    if proto_dep:
114        deps = [proto_dep]
115    else:
116        deps = []
117
118    java_library(
119        name = name,
120        srcs = [proto_sources_name],
121        deps = deps,
122        sdk_version = sdk_version,
123        **kwargs
124    )
125
126def java_nano_proto_library(
127        name,
128        plugin = "//external/protobuf:protoc-gen-javanano",
129        **kwargs):
130    _java_proto_library(
131        name,
132        plugin = plugin,
133        proto_dep = "//external/protobuf:libprotobuf-java-nano",
134        **kwargs
135    )
136
137def java_micro_proto_library(
138        name,
139        plugin = "//external/protobuf:protoc-gen-javamicro",
140        **kwargs):
141    _java_proto_library(
142        name,
143        plugin = plugin,
144        proto_dep = "//external/protobuf:libprotobuf-java-micro",
145        **kwargs
146    )
147
148def java_lite_proto_library(
149        name,
150        plugin = None,
151        **kwargs):
152    _java_proto_library(
153        name,
154        plugin = plugin,
155        out_format = "lite",
156        proto_dep = "//external/protobuf:libprotobuf-java-lite",
157        **kwargs
158    )
159
160def java_stream_proto_library(
161        name,
162        plugin = "//frameworks/base/tools/streaming_proto:protoc-gen-javastream",
163        **kwargs):
164    _java_proto_library(
165        name,
166        plugin = plugin,
167        **kwargs
168    )
169
170def java_proto_library(
171        name,
172        plugin = None,
173        **kwargs):
174    _java_proto_library(
175        name,
176        plugin = plugin,
177        proto_dep = "//external/protobuf:libprotobuf-java-full",
178        **kwargs
179    )
180