• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Bazel Authors. All rights reserved.
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
15"""
16Definition of java_plugin rule.
17"""
18
19load("//java/common:java_semantics.bzl", "semantics")
20load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
21load("//java/common/rules:java_library.bzl", "JAVA_LIBRARY_IMPLICIT_ATTRS")
22load("//java/common/rules:java_plugin.bzl", "JAVA_PLUGIN_ATTRS")
23load("//java/common/rules:rule_util.bzl", "merge_attrs")
24load("//java/common/rules/impl:basic_java_library_impl.bzl", "basic_java_library", "construct_defaultinfo")
25load("//java/private:java_info.bzl", "JavaPluginInfo")
26
27def bazel_java_plugin_rule(
28        ctx,
29        srcs = [],
30        data = [],
31        generates_api = False,
32        processor_class = "",
33        deps = [],
34        plugins = [],
35        resources = [],
36        javacopts = [],
37        neverlink = False,
38        proguard_specs = [],
39        add_exports = [],
40        add_opens = []):
41    """Implements java_plugin rule.
42
43    Use this call when you need to produce a fully fledged java_plugin from
44    another rule's implementation.
45
46    Args:
47      ctx: (RuleContext) Used to register the actions.
48      srcs: (list[File]) The list of source files that are processed to create the target.
49      data: (list[File]) The list of files needed by this plugin at runtime.
50      generates_api: (bool) This attribute marks annotation processors that generate API code.
51      processor_class: (str) The processor class is the fully qualified type of
52        the class that the Java compiler should use as entry point to the annotation processor.
53      deps: (list[Target]) The list of other libraries to be linked in to the target.
54      plugins: (list[Target]) Java compiler plugins to run at compile-time.
55      resources: (list[File]) A list of data files to include in a Java jar.
56      javacopts: (list[str]) Extra compiler options for this library.
57      neverlink: (bool) Whether this library should only be used for compilation and not at runtime.
58      proguard_specs: (list[File]) Files to be used as Proguard specification.
59      add_exports: (list[str]) Allow this library to access the given <module>/<package>.
60      add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
61    Returns:
62      (list[provider]) A list containing DefaultInfo, JavaInfo,
63        InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers.
64    """
65    target, base_info = basic_java_library(
66        ctx,
67        srcs,
68        deps,
69        [],  # runtime_deps
70        plugins,
71        [],  # exports
72        [],  # exported_plugins
73        resources,
74        [],  # resource_jars
75        [],  # classpath_resources
76        javacopts,
77        neverlink,
78        proguard_specs = proguard_specs,
79        add_exports = add_exports,
80        add_opens = add_opens,
81    )
82    java_info = target.pop("JavaInfo")
83
84    # Replace JavaInfo with JavaPluginInfo
85    target["JavaPluginInfo"] = JavaPluginInfo(
86        runtime_deps = [java_info],
87        processor_class = processor_class if processor_class else None,  # ignore empty string (default)
88        data = data,
89        generates_api = generates_api,
90    )
91    target["DefaultInfo"] = construct_defaultinfo(
92        ctx,
93        base_info.files_to_build,
94        base_info.runfiles,
95        neverlink,
96    )
97    target["OutputGroupInfo"] = OutputGroupInfo(**base_info.output_groups)
98
99    return target
100
101def _proxy(ctx):
102    return bazel_java_plugin_rule(
103        ctx,
104        ctx.files.srcs,
105        ctx.files.data,
106        ctx.attr.generates_api,
107        ctx.attr.processor_class,
108        ctx.attr.deps,
109        ctx.attr.plugins,
110        ctx.files.resources,
111        ctx.attr.javacopts,
112        ctx.attr.neverlink,
113        ctx.files.proguard_specs,
114        ctx.attr.add_exports,
115        ctx.attr.add_opens,
116    ).values()
117
118_JAVA_PLUGIN_IMPLICIT_ATTRS = JAVA_LIBRARY_IMPLICIT_ATTRS
119
120java_plugin = rule(
121    _proxy,
122    doc = """
123<p>
124  <code>java_plugin</code> defines plugins for the Java compiler run by Bazel. The
125  only supported kind of plugins are annotation processors. A <code>java_library</code> or
126  <code>java_binary</code> rule can run plugins by depending on them via the <code>plugins</code>
127  attribute. A <code>java_library</code> can also automatically export plugins to libraries that
128  directly depend on it using
129  <code><a href="#java_library-exported_plugins">exported_plugins</a></code>.
130</p>
131
132<h4 id="java_plugin_implicit_outputs">Implicit output targets</h4>
133    <ul>
134      <li><code><var>libname</var>.jar</code>: A Java archive.</li>
135    </ul>
136
137<p>
138  Arguments are identical to <a href="#java_library"><code>java_library</code></a>, except
139  for the addition of the <code>processor_class</code> argument.
140</p>
141    """,
142    attrs = merge_attrs(
143        JAVA_PLUGIN_ATTRS,
144        _JAVA_PLUGIN_IMPLICIT_ATTRS,
145    ),
146    provides = [JavaPluginInfo],
147    outputs = {
148        "classjar": "lib%{name}.jar",
149        "sourcejar": "lib%{name}-src.jar",
150    },
151    fragments = ["java", "cpp"],
152    toolchains = [semantics.JAVA_TOOLCHAIN],
153    subrules = [android_lint_subrule],
154)
155