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_library rule. 17""" 18 19load("//java/common/rules/impl:basic_java_library_impl.bzl", "basic_java_library", "construct_defaultinfo") 20 21# copybara: default visibility 22 23def bazel_java_library_rule( 24 ctx, 25 srcs = [], 26 deps = [], 27 runtime_deps = [], 28 plugins = [], 29 exports = [], 30 exported_plugins = [], 31 resources = [], 32 javacopts = [], 33 neverlink = False, 34 proguard_specs = [], 35 add_exports = [], 36 add_opens = [], 37 bootclasspath = None, 38 javabuilder_jvm_flags = None): 39 """Implements java_library. 40 41 Use this call when you need to produce a fully fledged java_library from 42 another rule's implementation. 43 44 Args: 45 ctx: (RuleContext) Used to register the actions. 46 srcs: (list[File]) The list of source files that are processed to create the target. 47 deps: (list[Target]) The list of other libraries to be linked in to the target. 48 runtime_deps: (list[Target]) Libraries to make available to the final binary or test at runtime only. 49 plugins: (list[Target]) Java compiler plugins to run at compile-time. 50 exports: (list[Target]) Exported libraries. 51 exported_plugins: (list[Target]) The list of `java_plugin`s (e.g. annotation 52 processors) to export to libraries that directly depend on this library. 53 resources: (list[File]) A list of data files to include in a Java jar. 54 javacopts: (list[str]) Extra compiler options for this library. 55 neverlink: (bool) Whether this library should only be used for compilation and not at runtime. 56 proguard_specs: (list[File]) Files to be used as Proguard specification. 57 add_exports: (list[str]) Allow this library to access the given <module>/<package>. 58 add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>. 59 bootclasspath: (Target) The JDK APIs to compile this library against. 60 javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder. 61 Returns: 62 (dict[str, provider]) A list containing DefaultInfo, JavaInfo, 63 InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers. 64 """ 65 if not srcs and deps: 66 fail("deps not allowed without srcs; move to runtime_deps?") 67 68 target, base_info = basic_java_library( 69 ctx, 70 srcs, 71 deps, 72 runtime_deps, 73 plugins, 74 exports, 75 exported_plugins, 76 resources, 77 [], # resource_jars 78 [], # class_pathresources 79 javacopts, 80 neverlink, 81 proguard_specs = proguard_specs, 82 add_exports = add_exports, 83 add_opens = add_opens, 84 bootclasspath = bootclasspath, 85 javabuilder_jvm_flags = javabuilder_jvm_flags, 86 ) 87 88 target["DefaultInfo"] = construct_defaultinfo( 89 ctx, 90 base_info.files_to_build, 91 base_info.runfiles, 92 neverlink, 93 exports, 94 runtime_deps, 95 ) 96 target["OutputGroupInfo"] = OutputGroupInfo(**base_info.output_groups) 97 98 return target 99