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"""Creates the android lint action for java rules""" 16 17load("//java/common:java_semantics.bzl", "semantics") 18 19# copybara: default visibility 20 21def _tokenize_opts(opts_depset): 22 opts = reversed(opts_depset.to_list()) 23 return semantics.tokenize_javacopts(opts) 24 25def _android_lint_action(ctx, source_files, source_jars, compilation_info): 26 """ 27 Creates an action that runs Android lint against Java source files. 28 29 You need to add `ANDROID_LINT_IMPLICIT_ATTRS` to any rule or aspect using this call. 30 31 To lint generated source jars (java_info.java_outputs.gen_source_jar) 32 add them to the `source_jar` parameter. 33 34 `compilation_info` parameter should supply the classpath and Javac options 35 that were used during Java compilation. 36 37 The Android lint tool is obtained from Java toolchain. 38 39 Args: 40 ctx: (RuleContext) Used to register the action. 41 source_files: (list[File]) A list of .java source files 42 source_jars: (list[File]) A list of .jar or .srcjar files containing 43 source files. It should also include generated source jars. 44 compilation_info: (struct) Information about compilation. 45 46 Returns: 47 (None|File) The Android lint output file or None if no source files were 48 present. 49 """ 50 51 # assuming that linting is enabled for all java rules i.e. 52 # --experimental_limit_android_lint_to_android_constrained_java=false 53 54 # --experimental_run_android_lint_on_java_rules= is checked in basic_java_library.bzl 55 56 if not (source_files or source_jars): 57 return None 58 59 toolchain = semantics.find_java_toolchain(ctx) 60 java_runtime = toolchain.java_runtime 61 linter = toolchain._android_linter 62 if not linter: 63 # TODO(hvd): enable after enabling in tests 64 # fail("android linter not set in java_toolchain") 65 return None 66 67 args = ctx.actions.args() 68 69 executable = linter.tool.executable 70 transitive_inputs = [] 71 if executable.extension != "jar": 72 tools = [linter.tool] 73 transitive_inputs.append(linter.data) 74 args_list = [args] 75 else: 76 jvm_args = ctx.actions.args() 77 jvm_args.add_all(toolchain.jvm_opt) 78 jvm_args.add_all(linter.jvm_opts) 79 jvm_args.add("-jar", executable) 80 executable = java_runtime.java_executable_exec_path 81 tools = [java_runtime.files, linter.tool.executable] 82 transitive_inputs.append(linter.data) 83 args_list = [jvm_args, args] 84 85 classpath = compilation_info.compilation_classpath 86 87 # TODO(hvd): get from toolchain if we need this - probably android only 88 bootclasspath_aux = [] 89 if bootclasspath_aux: 90 classpath = depset(transitive = [classpath, bootclasspath_aux]) 91 transitive_inputs.append(classpath) 92 93 bootclasspath = toolchain.bootclasspath 94 transitive_inputs.append(bootclasspath) 95 96 transitive_inputs.append(compilation_info.plugins.processor_jars) 97 transitive_inputs.append(compilation_info.plugins.processor_data) 98 args.add_all("--sources", source_files) 99 args.add_all("--source_jars", source_jars) 100 args.add_all("--bootclasspath", bootclasspath) 101 args.add_all("--classpath", classpath) 102 args.add_all("--lint_rules", compilation_info.plugins.processor_jars) 103 args.add("--target_label", ctx.label) 104 105 javac_opts = compilation_info.javac_options 106 if javac_opts: 107 # wrap in a list so that map_each passes the depset to _tokenize_opts 108 args.add_all("--javacopts", [javac_opts], map_each = _tokenize_opts) 109 args.add("--") 110 111 args.add("--lintopts") 112 args.add_all(linter.lint_opts) 113 114 for package_config in linter.package_config: 115 if package_config.matches(package_config.package_specs, ctx.label): 116 # wrap in a list so that map_each passes the depset to _tokenize_opts 117 package_opts = [package_config.javac_opts] 118 args.add_all(package_opts, map_each = _tokenize_opts) 119 transitive_inputs.append(package_config.data) 120 121 android_lint_out = ctx.actions.declare_file("%s_android_lint_output.xml" % ctx.label.name) 122 args.add("--xml", android_lint_out) 123 124 args.set_param_file_format(format = "multiline") 125 args.use_param_file(param_file_arg = "@%s", use_always = True) 126 ctx.actions.run( 127 mnemonic = "AndroidLint", 128 progress_message = semantics.LINT_PROGRESS_MESSAGE, 129 executable = executable, 130 inputs = depset( 131 # TODO(b/213551463) benchmark using a transitive depset instead 132 source_files + source_jars, 133 transitive = transitive_inputs, 134 ), 135 outputs = [android_lint_out], 136 tools = tools, 137 arguments = args_list, 138 execution_requirements = {"supports-workers": "1"}, 139 ) 140 return android_lint_out 141 142android_lint_subrule = subrule( 143 implementation = _android_lint_action, 144 toolchains = [semantics.JAVA_TOOLCHAIN_TYPE], 145) 146