@@ -17,7 +17,7 @@ def _licence_check_impl(ctx):
17
17
substitutions = {
18
18
"@@LICENCE@@" : "'''" + ctx .attr .licence + "'''" ,
19
19
"@@MATCH_REGEX@@" : "true" if ctx .attr .match_regex else "false" ,
20
- "@@EXCLUDE_PATHS@@" : ', ' .join (['"{}"' .format (pat ) for pat in ctx .attr .exclude_patterns ]),
20
+ "@@EXCLUDE_PATHS@@" : ", " .join (['"{}"' .format (pat ) for pat in ctx .attr .exclude_patterns ]),
21
21
},
22
22
)
23
23
@@ -35,68 +35,94 @@ def _licence_check_impl(ctx):
35
35
command = 'touch "{}"' .format (checker .path ),
36
36
)
37
37
38
+ workspace = ctx .file .workspace .path if ctx .file .workspace else ""
38
39
script = ctx .actions .declare_file (ctx .label .name + ".bash" )
39
40
ctx .actions .expand_template (
40
41
template = ctx .file ._runner ,
41
42
output = script ,
42
43
substitutions = {
43
44
"@@LICENCE_CHECKER@@" : ctx .executable .licence_check .path ,
44
45
"@@CONFIG@@" : config .path ,
46
+ "@@WORKSPACE@@" : workspace ,
45
47
},
46
48
is_executable = True ,
47
49
)
48
50
49
- runfiles = ctx .runfiles (files = [config , checker ], transitive_files = ctx .attr .licence_check .files )
51
+ files = [config , checker ]
52
+ if ctx .file .workspace :
53
+ files .append (ctx .file .workspace )
54
+
55
+ runfiles = ctx .runfiles (files = files , transitive_files = ctx .attr .licence_check .files )
50
56
runfiles = runfiles .merge (
51
- ctx .attr .licence_check .default_runfiles ,
57
+ ctx .attr .licence_check .default_runfiles ,
52
58
)
53
59
54
60
return DefaultInfo (
55
61
runfiles = runfiles ,
56
62
executable = script ,
57
63
)
58
64
65
+ licence_check_attrs = {
66
+ "config" : attr .label (
67
+ allow_single_file = True ,
68
+ doc = "HJSON configuration file override for the licence checker" ,
69
+ ),
70
+ "licence" : attr .string (
71
+ mandatory = True ,
72
+ doc = "Text of the licence header to use" ,
73
+ ),
74
+ "match_regex" : attr .bool (
75
+ default = False ,
76
+ doc = "Whether to use regex-matching for the licence text" ,
77
+ ),
78
+ "exclude_patterns" : attr .string_list (
79
+ default = [],
80
+ doc = "File patterns to exclude from licence enforcement" ,
81
+ ),
82
+ "licence_check" : attr .label (
83
+ default = "//licence-checker" ,
84
+ cfg = "host" ,
85
+ executable = True ,
86
+ doc = "The licence checker executable" ,
87
+ ),
88
+ "workspace" : attr .label (
89
+ allow_single_file = True ,
90
+ doc = "Label of the WORKSPACE file" ,
91
+ ),
92
+ "_runner" : attr .label (
93
+ default = "//rules:licence-checker-runner.template.sh" ,
94
+ allow_single_file = True ,
95
+ ),
96
+ "_config" : attr .label (
97
+ default = "//rules:licence-checker-config.template.hjson" ,
98
+ allow_single_file = True ,
99
+ ),
100
+ }
101
+
59
102
licence_check = rule (
60
103
implementation = _licence_check_impl ,
61
- attrs = {
62
- "config" : attr .label (
63
- allow_single_file = True ,
64
- doc = "HJSON configuration file override for the licence checker" ,
65
- ),
66
- "licence" : attr .string (
67
- mandatory = True ,
68
- doc = "Text of the licence header to use" ,
69
- ),
70
- "match_regex" : attr .bool (
71
- default = False ,
72
- doc = "Whether to use regex-matching for the licence text" ,
73
- ),
74
- "exclude_patterns" : attr .string_list (
75
- default = [],
76
- doc = "File patterns to exclude from licence enforcement" ,
77
- ),
78
- "licence_check" : attr .label (
79
- default = "//licence-checker" ,
80
- cfg = "host" ,
81
- executable = True ,
82
- doc = "The licence checker executable" ,
83
- ),
84
- "_runner" : attr .label (
85
- default = "//rules:licence-checker-runner.template.sh" ,
86
- allow_single_file = True ,
87
- ),
88
- "_config" : attr .label (
89
- default = "//rules:licence-checker-config.template.hjson" ,
90
- allow_single_file = True ,
91
- ),
92
- "_sh_runfiles" : attr .label (
93
- default = "@bazel_tools//tools/bash/runfiles" ,
94
- allow_single_file = True ,
95
- ),
96
- },
104
+ attrs = licence_check_attrs ,
97
105
executable = True ,
98
106
)
99
107
108
+ _licence_test = rule (
109
+ implementation = _licence_check_impl ,
110
+ attrs = licence_check_attrs ,
111
+ test = True ,
112
+ )
113
+
114
+ def _ensure_tag (tags , * tag ):
115
+ for t in tag :
116
+ if t not in tags :
117
+ tags .append (t )
118
+ return tags
119
+
120
+ def licence_test (** kwargs ):
121
+ # Note: the "external" tag is a workaround for bazelbuild#15516.
122
+ tags = kwargs .get ("tags" , [])
123
+ kwargs ["tags" ] = _ensure_tag (tags , "no-sandbox" , "no-cache" , "external" )
124
+ _licence_test (** kwargs )
125
+
100
126
def _yapf_check_impl (ctx ):
101
127
# Hack to make Bazel build the checker correctly.
102
128
#
@@ -173,4 +199,3 @@ yapf_check = rule(
173
199
},
174
200
executable = True ,
175
201
)
176
-
0 commit comments