blob: 8e6e37fc51d990f2e33d246797e9465f5b82b03d [file] [log] [blame]
Ray Milkeyb7949e72018-06-19 18:31:02 -07001"""
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 Milkeyb7949e72018-06-19 18:31:02 -070017"""
18 Implementation of the rule to call checkstyle
19"""
Ray Milkey5063f5b2018-08-15 16:22:30 -070020
Ray Milkeyb7949e72018-06-19 18:31:02 -070021def _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 Casconed33d3b42019-06-18 12:12:36 -070030 java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo]
31 java_exe_path = java_runtime.java_executable_runfiles_path
32
Ray Milkeyb7949e72018-06-19 18:31:02 -070033 cmd = " ".join(
Carmelo Casconed33d3b42019-06-18 12:12:36 -070034 ["%s -cp %s com.puppycrawl.tools.checkstyle.Main" % (java_exe_path, classpath)] +
Ray Milkey5063f5b2018-08-15 16:22:30 -070035 ["-c %s" % ctx.attr._config.files.to_list()[0].path] +
36 [src_file.path for src_file in ctx.files.srcs],
37 )
Ray Milkeyb7949e72018-06-19 18:31:02 -070038
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 Casconed33d3b42019-06-18 12:12:36 -070050 runfiles = ctx.runfiles(
51 files = inputs,
52 transitive_files = java_runtime.files,
53 )
Ray Milkeyb7949e72018-06-19 18:31:02 -070054 return [DefaultInfo(runfiles = runfiles)]
55
Ray Milkeyb7949e72018-06-19 18:31:02 -070056"""
57 Rule definition for calling checkstyle
58"""
59_execute_checkstyle_test = rule(
60 test = True,
61 attrs = {
Ray Milkey5063f5b2018-08-15 16:22:30 -070062 "_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 Milkey8358d762019-03-26 11:04:34 -070071 "srcs": attr.label_list(allow_files = [".java"]),
Ray Milkey5063f5b2018-08-15 16:22:30 -070072 "_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 Casconed33d3b42019-06-18 12:12:36 -070075 "_jdk": attr.label(
76 default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
77 providers = [java_common.JavaRuntimeInfo],
78 ),
Ray Milkeyb7949e72018-06-19 18:31:02 -070079 },
Ray Milkeyb7949e72018-06-19 18:31:02 -070080 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 Milkey5063f5b2018-08-15 16:22:30 -070091
Ray Milkeyb7949e72018-06-19 18:31:02 -070092def checkstyle_test(name, srcs):
93 _execute_checkstyle_test(name = name, srcs = srcs, size = "small")