• 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"""
16Java compile action
17"""
18
19load("//java/common:java_semantics.bzl", "semantics")
20load("//java/private:java_common_internal.bzl", _compile_private_for_builtins = "compile")
21
22visibility("private")
23
24def _filter_strict_deps(mode):
25    return "error" if mode in ["strict", "default"] else mode
26
27def _collect_plugins(deps, plugins):
28    transitive_processor_jars = []
29    transitive_processor_data = []
30    for plugin in plugins:
31        transitive_processor_jars.append(plugin.plugins.processor_jars)
32        transitive_processor_data.append(plugin.plugins.processor_data)
33    for dep in deps:
34        transitive_processor_jars.append(dep.plugins.processor_jars)
35        transitive_processor_data.append(dep.plugins.processor_data)
36    return struct(
37        processor_jars = depset(transitive = transitive_processor_jars),
38        processor_data = depset(transitive = transitive_processor_data),
39    )
40
41def compile_action(
42        ctx,
43        output_class_jar,
44        output_source_jar,
45        source_files = [],
46        source_jars = [],
47        deps = [],
48        runtime_deps = [],
49        plugins = [],
50        exports = [],
51        exported_plugins = [],
52        resources = [],
53        resource_jars = [],
54        classpath_resources = [],
55        native_libraries = [],
56        javacopts = [],
57        neverlink = False,
58        strict_deps = "ERROR",
59        enable_compile_jar_action = True,
60        add_exports = [],
61        add_opens = [],
62        bootclasspath = None,
63        javabuilder_jvm_flags = None):
64    """
65    Creates actions that compile Java sources, produce source jar, and produce header jar and returns JavaInfo.
66
67    Use this call when you need the most basic and consistent Java compilation.
68
69    Most parameters correspond to attributes on a java_library (srcs, deps,
70    plugins, resources ...) except they are more strict, for example:
71
72    - Where java_library's srcs attribute allows mixing of .java, .srcjar, and
73     .properties files the arguments accepted by this call should be strictly
74     separated into source_files, source_jars, and resources parameter.
75    - deps parameter accepts only JavaInfo providers and plugins parameter only
76     JavaPluginInfo
77
78    The call creates following actions and files:
79    - compiling Java sources to a class jar (output_class_jar parameter)
80    - a source jar (output_source_jar parameter)
81    - optionally a jar containing plugin generated classes when plugins are present
82    - optionally a jar containing plugin generated sources
83    - jdeps file containing dependencies used during compilation
84    - other files used to speed up incremental builds:
85         - a header jar - a jar containing only method signatures without implementation
86         - compile jdeps - dependencies used during header compilation
87
88    The returned JavaInfo provider may be used as a "fully-qualified" dependency
89    to a java_library.
90
91    Args:
92      ctx: (RuleContext) Used to register the actions.
93      output_class_jar: (File) Output class .jar file. The file needs to be declared.
94      output_source_jar: (File) Output source .jar file. The file needs to be declared.
95      source_files: (list[File]) A list of .java source files to compile.
96        At least one of source_files or source_jars parameter must be specified.
97      source_jars: (list[File]) A list of .jar or .srcjar files containing
98        source files to compile.
99        At least one of source_files or source_jars parameter must be specified.
100      deps: (list[JavaInfo]) A list of dependencies.
101      runtime_deps: (list[JavaInfo]) A list of runtime dependencies.
102      plugins: (list[JavaPluginInfo]) A list of plugins.
103      exports: (list[JavaInfo]) A list of exports.
104      exported_plugins: (list[JavaInfo]) A list of exported plugins.
105      resources: (list[File]) A list of resources.
106      resource_jars: (list[File]) A list of jars to unpack.
107      classpath_resources: (list[File]) A list of classpath resources.
108      native_libraries: (list[CcInfo]) C++ native library dependencies that are
109        needed for this library.
110      javacopts: (list[str]) A list of the desired javac options. The options
111        may contain `$(location ..)` templates that will be expanded.
112      neverlink: (bool) Whether or not this library should be used only for
113        compilation and not at runtime.
114      strict_deps: (str) A string that specifies how to handle strict deps.
115        Possible values: 'OFF', 'ERROR', 'WARN' and 'DEFAULT'. For more details
116        see https://bazel.build/docs/user-manual#strict-java-deps.
117        By default 'ERROR'.
118      enable_compile_jar_action: (bool) Enables header compilation or ijar
119        creation. If set to False, it forces use of the full class jar in the
120        compilation classpaths of any dependants. Doing so is intended for use
121        by non-library targets such as binaries that do not have dependants.
122      add_exports: (list[str]) Allow this library to access the given <module>/<package>.
123      add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
124      bootclasspath: (BootClassPathInfo) The set of JDK APIs to compile this library against.
125      javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
126
127    Returns:
128      ((JavaInfo, {files_to_build: list[File],
129                   runfiles: list[File],
130                   compilation_classpath: list[File],
131                   plugins: {processor_jars,
132                             processor_data: depset[File]}}))
133      A tuple with JavaInfo provider and additional compilation info.
134
135      Files_to_build may include an empty .jar file when there are no sources
136      or resources present, whereas runfiles in this case are empty.
137    """
138
139    java_info = _compile_private_for_builtins(
140        ctx,
141        output = output_class_jar,
142        java_toolchain = semantics.find_java_toolchain(ctx),
143        source_files = source_files,
144        source_jars = source_jars,
145        resources = resources,
146        resource_jars = resource_jars,
147        classpath_resources = classpath_resources,
148        plugins = plugins,
149        deps = deps,
150        native_libraries = native_libraries,
151        runtime_deps = runtime_deps,
152        exports = exports,
153        exported_plugins = exported_plugins,
154        javac_opts = [ctx.expand_location(opt) for opt in javacopts],
155        neverlink = neverlink,
156        output_source_jar = output_source_jar,
157        strict_deps = _filter_strict_deps(strict_deps),
158        enable_compile_jar_action = enable_compile_jar_action,
159        add_exports = add_exports,
160        add_opens = add_opens,
161        bootclasspath = bootclasspath,
162        javabuilder_jvm_flags = javabuilder_jvm_flags,
163    )
164
165    compilation_info = struct(
166        files_to_build = [output_class_jar],
167        runfiles = [output_class_jar] if source_files or source_jars or resources else [],
168        # TODO(ilist): collect compile_jars from JavaInfo in deps & exports
169        compilation_classpath = java_info.compilation_info.compilation_classpath,
170        javac_options = java_info.compilation_info.javac_options,
171        plugins = _collect_plugins(deps, plugins),
172    )
173
174    return java_info, compilation_info
175