blob: b651dd249f77c74c7702fa1024fb26bdf151eaf6 [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
30 cmd = " ".join(
Ray Milkey5063f5b2018-08-15 16:22:30 -070031 ["java -cp %s com.puppycrawl.tools.checkstyle.Main" % classpath] +
32 ["-c %s" % ctx.attr._config.files.to_list()[0].path] +
33 [src_file.path for src_file in ctx.files.srcs],
34 )
Ray Milkeyb7949e72018-06-19 18:31:02 -070035
36 ctx.actions.write(
37 output = ctx.outputs.executable,
38 content = cmd,
39 )
40
41 inputs = (ctx.files.srcs +
42 ctx.files._classpath +
43 ctx.attr._config.files.to_list() +
44 ctx.attr._suppressions.files.to_list() +
45 ctx.attr._java_header.files.to_list())
46
47 runfiles = ctx.runfiles(files = inputs)
48 return [DefaultInfo(runfiles = runfiles)]
49
Ray Milkeyb7949e72018-06-19 18:31:02 -070050"""
51 Rule definition for calling checkstyle
52"""
53_execute_checkstyle_test = rule(
54 test = True,
55 attrs = {
Ray Milkey5063f5b2018-08-15 16:22:30 -070056 "_classpath": attr.label_list(default = [
57 Label("@checkstyle//jar"),
58 Label("@commons_beanutils//jar"),
59 Label("@commons_cli//jar"),
60 Label("@commons_collections//jar"),
61 Label("@antlr//jar"),
62 Label("@com_google_guava_guava//jar"),
63 Label("@commons_logging//jar"),
64 ]),
Ray Milkey16fbcff2019-03-26 11:04:34 -070065 "srcs": attr.label_list(allow_files = [".java"]),
Ray Milkey5063f5b2018-08-15 16:22:30 -070066 "_config": attr.label(default = Label("//tools/build/conf:checkstyle_xml")),
67 "_suppressions": attr.label(default = Label("//tools/build/conf:suppressions_xml")),
68 "_java_header": attr.label(default = Label("//tools/build/conf:onos_java_header")),
Ray Milkeyb7949e72018-06-19 18:31:02 -070069 },
Ray Milkeyb7949e72018-06-19 18:31:02 -070070 implementation = _checkstyle_impl,
71)
72
73"""
74 Macro to instantiate the checkstyle rule for a given set of sources.
75
76 Args:
77 name: name of the target to generate. Required.
78 srcs: list of source file targets to run checkstyle on. Required.
79 size: test size constraint. Optional, defaults to "small"
80"""
Ray Milkey5063f5b2018-08-15 16:22:30 -070081
Ray Milkeyb7949e72018-06-19 18:31:02 -070082def checkstyle_test(name, srcs):
83 _execute_checkstyle_test(name = name, srcs = srcs, size = "small")