@@ -8,9 +8,9 @@ def _run_tidy(
8
8
additional_deps ,
9
9
config ,
10
10
flags ,
11
- compilation_contexts ,
12
11
infile ,
13
12
discriminator ,
13
+ additional_files ,
14
14
additional_inputs ):
15
15
cc_toolchain = find_cpp_toolchain (ctx )
16
16
direct_inputs = (
@@ -23,9 +23,7 @@ def _run_tidy(
23
23
24
24
inputs = depset (
25
25
direct = direct_inputs ,
26
- transitive =
27
- [compilation_context .headers for compilation_context in compilation_contexts ] +
28
- [cc_toolchain .all_files ],
26
+ transitive = [additional_files , cc_toolchain .all_files ],
29
27
)
30
28
31
29
args = ctx .actions .args ()
@@ -53,42 +51,18 @@ def _run_tidy(
53
51
# start args passed to the compiler
54
52
args .add ("--" )
55
53
56
- # add args specified by the toolchain, on the command line and rule copts
57
- args .add_all (flags )
58
-
59
- for compilation_context in compilation_contexts :
60
- # add defines
61
- for define in compilation_context .defines .to_list ():
62
- args .add ("-D" + define )
63
-
64
- for define in compilation_context .local_defines .to_list ():
65
- args .add ("-D" + define )
66
-
67
- # add includes
68
- for i in compilation_context .framework_includes .to_list ():
69
- args .add ("-F" + i )
70
-
71
- for i in compilation_context .includes .to_list ():
72
- args .add ("-I" + i )
73
-
74
- args .add_all (compilation_context .quote_includes .to_list (), before_each = "-iquote" )
75
-
76
- args .add_all (compilation_context .system_includes .to_list (), before_each = "-isystem" )
77
-
78
- args .add_all (compilation_context .external_includes .to_list (), before_each = "-isystem" )
79
-
80
54
ctx .actions .run (
81
55
inputs = inputs ,
82
56
outputs = [outfile ],
83
57
executable = wrapper ,
84
- arguments = [args ],
58
+ arguments = [args ] + flags ,
85
59
mnemonic = "ClangTidy" ,
86
60
use_default_shell_env = True ,
87
61
progress_message = "Run clang-tidy on {}" .format (infile .short_path ),
88
62
)
89
63
return outfile
90
64
91
- def _rule_sources ( ctx , include_headers ):
65
+ def rule_sources ( attr , include_headers ):
92
66
header_extensions = (
93
67
".h" ,
94
68
".hh" ,
@@ -117,18 +91,18 @@ def _rule_sources(ctx, include_headers):
117
91
return False
118
92
119
93
srcs = []
120
- if hasattr (ctx . rule . attr , "srcs" ):
121
- for src in ctx . rule . attr .srcs :
94
+ if hasattr (attr , "srcs" ):
95
+ for src in attr .srcs :
122
96
srcs += [src for src in src .files .to_list () if src .is_source and check_valid_file_type (src )]
123
- if hasattr (ctx . rule . attr , "hdrs" ):
124
- for hdr in ctx . rule . attr .hdrs :
97
+ if hasattr (attr , "hdrs" ):
98
+ for hdr in attr .hdrs :
125
99
srcs += [hdr for hdr in hdr .files .to_list () if hdr .is_source and check_valid_file_type (hdr )]
126
100
if include_headers :
127
101
return srcs
128
102
else :
129
103
return [src for src in srcs if not src .basename .endswith (header_extensions )]
130
104
131
- def _toolchain_flags (ctx , action_name = ACTION_NAMES .cpp_compile ):
105
+ def toolchain_flags (ctx , action_name = ACTION_NAMES .cpp_compile ):
132
106
cc_toolchain = find_cpp_toolchain (ctx )
133
107
feature_configuration = cc_common .configure_features (
134
108
ctx = ctx ,
@@ -151,7 +125,47 @@ def _toolchain_flags(ctx, action_name = ACTION_NAMES.cpp_compile):
151
125
)
152
126
return flags
153
127
154
- def _safe_flags (flags ):
128
+ def deps_flags (ctx , deps , * , escape ):
129
+ compilation_contexts = [dep [CcInfo ].compilation_context for dep in deps ]
130
+ additional_files = depset (transitive = [
131
+ compilation_context .headers
132
+ for compilation_context in compilation_contexts
133
+ ])
134
+
135
+ flags = []
136
+ for compilation_context in compilation_contexts :
137
+ # add defines
138
+ for define in compilation_context .defines .to_list ():
139
+ if escape :
140
+ flags .append ("-D'" + define + "'" )
141
+ else :
142
+ flags .append ("-D" + define )
143
+
144
+ for define in compilation_context .local_defines .to_list ():
145
+ if escape :
146
+ flags .append ("-D'" + define + "'" )
147
+ else :
148
+ flags .append ("-D" + define )
149
+
150
+ # add includes
151
+ for i in compilation_context .framework_includes .to_list ():
152
+ flags .append ("-F" + i )
153
+
154
+ for i in compilation_context .includes .to_list ():
155
+ flags .append ("-I" + i )
156
+
157
+ for i in compilation_context .quote_includes .to_list ():
158
+ flags .extend (["-iquote" , i ])
159
+
160
+ for i in compilation_context .system_includes .to_list ():
161
+ flags .extend (["-isystem" , i ])
162
+
163
+ for i in compilation_context .external_includes .to_list ():
164
+ flags .extend (["-isystem" , i ])
165
+
166
+ return flags , additional_files
167
+
168
+ def safe_flags (flags ):
155
169
# Some flags might be used by GCC, but not understood by Clang.
156
170
# Remove them here, to allow users to run clang-tidy, without having
157
171
# a clang toolchain configured (that would produce a good command line with --compiler clang)
@@ -162,7 +176,7 @@ def _safe_flags(flags):
162
176
163
177
return [flag for flag in flags if flag not in unsupported_flags ]
164
178
165
- def _is_c_translation_unit (src , tags ):
179
+ def is_c_translation_unit (src , tags ):
166
180
"""Judge if a source file is for C.
167
181
168
182
Args:
@@ -201,24 +215,21 @@ def _clang_tidy_aspect_impl(target, ctx):
201
215
additional_deps = ctx .attr ._clang_tidy_additional_deps
202
216
config = ctx .attr ._clang_tidy_config .files .to_list ()[0 ]
203
217
204
- compilation_contexts = [target [CcInfo ].compilation_context ]
205
- if hasattr (ctx .rule .attr , "implementation_deps" ):
206
- compilation_contexts .extend ([implementation_dep [CcInfo ].compilation_context for implementation_dep in ctx .rule .attr .implementation_deps ])
207
-
218
+ deps = [target ] + getattr (ctx .rule .attr , "implementation_deps" , [])
219
+ rule_flags , additional_files = deps_flags (ctx , deps , escape = False )
208
220
copts = ctx .rule .attr .copts if hasattr (ctx .rule .attr , "copts" ) else []
209
- rule_flags = []
210
221
for copt in copts :
211
222
rule_flags .append (ctx .expand_make_variables (
212
223
"copts" ,
213
224
copt ,
214
225
{},
215
226
))
216
227
217
- c_flags = _safe_flags ( _toolchain_flags (ctx , ACTION_NAMES .c_compile ) + rule_flags ) + ["-xc" ]
218
- cxx_flags = _safe_flags ( _toolchain_flags (ctx , ACTION_NAMES .cpp_compile ) + rule_flags ) + ["-xc++" ]
228
+ c_flags = safe_flags ( toolchain_flags (ctx , ACTION_NAMES .c_compile ) + rule_flags ) + ["-xc" ]
229
+ cxx_flags = safe_flags ( toolchain_flags (ctx , ACTION_NAMES .cpp_compile ) + rule_flags ) + ["-xc++" ]
219
230
220
231
include_headers = "no-clang-tidy-headers" not in ctx .rule .attr .tags
221
- srcs = _rule_sources (ctx , include_headers )
232
+ srcs = rule_sources (ctx . rule . attr , include_headers )
222
233
223
234
outputs = [
224
235
_run_tidy (
@@ -227,10 +238,10 @@ def _clang_tidy_aspect_impl(target, ctx):
227
238
exe ,
228
239
additional_deps ,
229
240
config ,
230
- c_flags if _is_c_translation_unit (src , ctx .rule .attr .tags ) else cxx_flags ,
231
- compilation_contexts ,
241
+ c_flags if is_c_translation_unit (src , ctx .rule .attr .tags ) else cxx_flags ,
232
242
src ,
233
243
target .label .name ,
244
+ additional_files ,
234
245
additional_inputs = getattr (ctx .rule .attr , "additional_compiler_inputs" , []),
235
246
)
236
247
for src in srcs
0 commit comments