-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathbazel_java_plugin.bzl
More file actions
154 lines (142 loc) · 5.59 KB
/
Copy pathbazel_java_plugin.bzl
File metadata and controls
154 lines (142 loc) · 5.59 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# 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
#
# http://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.
"""
Definition of java_plugin rule.
"""
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
load("//java/common/rules:java_library.bzl", "JAVA_LIBRARY_IMPLICIT_ATTRS")
load("//java/common/rules:java_plugin.bzl", "JAVA_PLUGIN_ATTRS")
load("//java/common/rules:rule_util.bzl", "merge_attrs")
load("//java/common/rules/impl:basic_java_library_impl.bzl", "basic_java_library", "construct_defaultinfo")
load("//java/private:java_info.bzl", "JavaPluginInfo")
def bazel_java_plugin_rule(
ctx,
srcs = [],
data = [],
generates_api = False,
processor_class = "",
deps = [],
plugins = [],
resources = [],
javacopts = [],
neverlink = False,
proguard_specs = [],
add_exports = [],
add_opens = []):
"""Implements java_plugin rule.
Use this call when you need to produce a fully fledged java_plugin from
another rule's implementation.
Args:
ctx: (RuleContext) Used to register the actions.
srcs: (list[File]) The list of source files that are processed to create the target.
data: (list[File]) The list of files needed by this plugin at runtime.
generates_api: (bool) This attribute marks annotation processors that generate API code.
processor_class: (str) The processor class is the fully qualified type of
the class that the Java compiler should use as entry point to the annotation processor.
deps: (list[Target]) The list of other libraries to be linked in to the target.
plugins: (list[Target]) Java compiler plugins to run at compile-time.
resources: (list[File]) A list of data files to include in a Java jar.
javacopts: (list[str]) Extra compiler options for this library.
neverlink: (bool) Whether this library should only be used for compilation and not at runtime.
proguard_specs: (list[File]) Files to be used as Proguard specification.
add_exports: (list[str]) Allow this library to access the given <module>/<package>.
add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
Returns:
(list[provider]) A list containing DefaultInfo, JavaInfo,
InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers.
"""
target, base_info = basic_java_library(
ctx,
srcs,
deps,
[], # runtime_deps
plugins,
[], # exports
[], # exported_plugins
resources,
[], # resource_jars
[], # classpath_resources
javacopts,
neverlink,
proguard_specs = proguard_specs,
add_exports = add_exports,
add_opens = add_opens,
)
java_info = target.pop("JavaInfo")
# Replace JavaInfo with JavaPluginInfo
target["JavaPluginInfo"] = JavaPluginInfo(
runtime_deps = [java_info],
processor_classes = [processor_class] if processor_class else [], # ignore empty string (default)
data = data,
generates_api = generates_api,
)
target["DefaultInfo"] = construct_defaultinfo(
ctx,
base_info.files_to_build,
base_info.runfiles,
neverlink,
)
target["OutputGroupInfo"] = OutputGroupInfo(**base_info.output_groups)
return target
def _proxy(ctx):
return bazel_java_plugin_rule(
ctx,
ctx.files.srcs,
ctx.files.data,
ctx.attr.generates_api,
ctx.attr.processor_class,
ctx.attr.deps,
ctx.attr.plugins,
ctx.files.resources,
ctx.attr.javacopts,
ctx.attr.neverlink,
ctx.files.proguard_specs,
ctx.attr.add_exports,
ctx.attr.add_opens,
).values()
_JAVA_PLUGIN_IMPLICIT_ATTRS = JAVA_LIBRARY_IMPLICIT_ATTRS
java_plugin = rule(
_proxy,
doc = """
<p>
<code>java_plugin</code> defines plugins for the Java compiler run by Bazel. The
only supported kind of plugins are annotation processors. A <code>java_library</code> or
<code>java_binary</code> rule can run plugins by depending on them via the <code>plugins</code>
attribute. A <code>java_library</code> can also automatically export plugins to libraries that
directly depend on it using
<code><a href="#java_library-exported_plugins">exported_plugins</a></code>.
</p>
<h4 id="java_plugin_implicit_outputs">Implicit output targets</h4>
<ul>
<li><code><var>libname</var>.jar</code>: A Java archive.</li>
</ul>
<p>Arguments are a subset of (and with identical semantics to) those of
<a href="${link java_library}">java_library()</a>,
except for the addition of the <code>processor_class</code> and
<code>generates_api</code> arguments.</p>
""",
attrs = merge_attrs(
JAVA_PLUGIN_ATTRS,
_JAVA_PLUGIN_IMPLICIT_ATTRS,
),
provides = [JavaPluginInfo],
outputs = {
"classjar": "lib%{name}.jar",
"sourcejar": "lib%{name}-src.jar",
},
fragments = ["java", "cpp"],
toolchains = [semantics.JAVA_TOOLCHAIN],
subrules = [android_lint_subrule],
)