• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Copyright (C) 2021 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("//build/bazel/rules:proto_file_utils.bzl", "proto_file_utils")
18load("@bazel_skylib//lib:paths.bzl", "paths")
19load(":library.bzl", "java_library")
20
21def _java_proto_sources_gen_rule_impl(ctx):
22    out_flags = []
23    plugin_executable = None
24    out_arg = None
25    if ctx.attr.plugin:
26        plugin_executable = ctx.executable.plugin
27    else:
28        out_arg = "--java_out"
29        if ctx.attr.out_format:
30            out_flags.append(ctx.attr.out_format)
31
32    srcs = []
33    for dep in ctx.attr.deps:
34        proto_info = dep[ProtoInfo]
35        out_jar = _generate_java_proto_action(
36            proto_info = proto_info,
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_info,
80        protoc,
81        ctx,
82        plugin_executable,
83        out_arg,
84        out_flags):
85    return proto_file_utils.generate_jar_proto_action(
86        proto_info,
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        target_compatible_with = [],
100        out_format = None,
101        proto_dep = None):
102    proto_sources_name = name + "_proto_gen"
103
104    _java_proto_sources_gen(
105        name = proto_sources_name,
106        deps = deps,
107        plugin = plugin,
108        out_format = out_format,
109    )
110
111    if proto_dep:
112        deps = [proto_dep]
113    else:
114        deps = []
115
116    java_library(
117        name = name,
118        srcs = [proto_sources_name],
119        deps = deps,
120        target_compatible_with = target_compatible_with,
121    )
122
123def java_nano_proto_library(
124        name,
125        deps = [],
126        plugin = "//external/protobuf:protoc-gen-javanano",
127        target_compatible_with = []):
128    _java_proto_library(
129        name,
130        deps = deps,
131        plugin = plugin,
132        target_compatible_with = target_compatible_with,
133        proto_dep = "//external/protobuf:libprotobuf-java-nano",
134    )
135
136def java_micro_proto_library(
137        name,
138        deps = [],
139        plugin = "//external/protobuf:protoc-gen-javamicro",
140        target_compatible_with = []):
141    _java_proto_library(
142        name,
143        deps = deps,
144        plugin = plugin,
145        target_compatible_with = target_compatible_with,
146        proto_dep = "//external/protobuf:libprotobuf-java-micro",
147    )
148
149def java_lite_proto_library(
150        name,
151        deps = [],
152        plugin = None,
153        target_compatible_with = []):
154    _java_proto_library(
155        name,
156        deps = deps,
157        plugin = plugin,
158        target_compatible_with = target_compatible_with,
159        out_format = "lite",
160        proto_dep = "//external/protobuf:libprotobuf-java-lite",
161    )
162
163def java_stream_proto_library(
164        name,
165        deps = [],
166        plugin = "//frameworks/base/tools/streaming_proto:protoc-gen-javastream",
167        target_compatible_with = []):
168    _java_proto_library(
169        name,
170        deps = deps,
171        plugin = plugin,
172        target_compatible_with = target_compatible_with,
173    )
174
175def java_proto_library(
176        name,
177        deps = [],
178        plugin = None,
179        target_compatible_with = []):
180    _java_proto_library(
181        name,
182        deps = deps,
183        plugin = plugin,
184        target_compatible_with = target_compatible_with,
185        proto_dep = "//external/protobuf:libprotobuf-java-full",
186    )
187