Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | """ |
| 16 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 17 | """ |
| 18 | Implementation of the rule to call checkstyle |
| 19 | """ |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 20 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 21 | def _checkstyle_impl(ctx): |
| 22 | classpath = "" |
| 23 | need_colon = False |
| 24 | for file in ctx.files._classpath: |
| 25 | if need_colon: |
| 26 | classpath += ":" |
| 27 | need_colon = True |
| 28 | classpath += file.path |
| 29 | |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 30 | java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo] |
| 31 | java_exe_path = java_runtime.java_executable_runfiles_path |
| 32 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 33 | cmd = " ".join( |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 34 | ["%s -cp %s com.puppycrawl.tools.checkstyle.Main" % (java_exe_path, classpath)] + |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 35 | ["-c %s" % ctx.attr._config.files.to_list()[0].path] + |
| 36 | [src_file.path for src_file in ctx.files.srcs], |
| 37 | ) |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 38 | |
| 39 | ctx.actions.write( |
| 40 | output = ctx.outputs.executable, |
| 41 | content = cmd, |
| 42 | ) |
| 43 | |
| 44 | inputs = (ctx.files.srcs + |
| 45 | ctx.files._classpath + |
| 46 | ctx.attr._config.files.to_list() + |
| 47 | ctx.attr._suppressions.files.to_list() + |
| 48 | ctx.attr._java_header.files.to_list()) |
| 49 | |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 50 | runfiles = ctx.runfiles( |
| 51 | files = inputs, |
| 52 | transitive_files = java_runtime.files, |
| 53 | ) |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 54 | return [DefaultInfo(runfiles = runfiles)] |
| 55 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 56 | """ |
| 57 | Rule definition for calling checkstyle |
| 58 | """ |
| 59 | _execute_checkstyle_test = rule( |
| 60 | test = True, |
| 61 | attrs = { |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 62 | "_classpath": attr.label_list(default = [ |
| 63 | Label("@checkstyle//jar"), |
| 64 | Label("@commons_beanutils//jar"), |
| 65 | Label("@commons_cli//jar"), |
| 66 | Label("@commons_collections//jar"), |
| 67 | Label("@antlr//jar"), |
| 68 | Label("@com_google_guava_guava//jar"), |
| 69 | Label("@commons_logging//jar"), |
| 70 | ]), |
Ray Milkey | 8358d76 | 2019-03-26 11:04:34 -0700 | [diff] [blame] | 71 | "srcs": attr.label_list(allow_files = [".java"]), |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 72 | "_config": attr.label(default = Label("//tools/build/conf:checkstyle_xml")), |
| 73 | "_suppressions": attr.label(default = Label("//tools/build/conf:suppressions_xml")), |
| 74 | "_java_header": attr.label(default = Label("//tools/build/conf:onos_java_header")), |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 75 | "_jdk": attr.label( |
| 76 | default = Label("@bazel_tools//tools/jdk:current_java_runtime"), |
| 77 | providers = [java_common.JavaRuntimeInfo], |
| 78 | ), |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 79 | }, |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 80 | implementation = _checkstyle_impl, |
| 81 | ) |
| 82 | |
| 83 | """ |
| 84 | Macro to instantiate the checkstyle rule for a given set of sources. |
| 85 | |
| 86 | Args: |
| 87 | name: name of the target to generate. Required. |
| 88 | srcs: list of source file targets to run checkstyle on. Required. |
| 89 | size: test size constraint. Optional, defaults to "small" |
| 90 | """ |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 91 | |
Ray Milkey | b7949e7 | 2018-06-19 18:31:02 -0700 | [diff] [blame] | 92 | def checkstyle_test(name, srcs): |
| 93 | _execute_checkstyle_test(name = name, srcs = srcs, size = "small") |