-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmake_sanitizer.py
372 lines (312 loc) · 11.7 KB
/
cmake_sanitizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
"""
Author : ChrisZZ
Start Date : 2023-02-08 11:00:00
Description : Parse, validate and report bad uses in CMakeLists.txt/xxx.cmake files.
"""
# init logging
import logging
from rich.logging import RichHandler
FORMAT = "%(message)s"
logging.basicConfig(
level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
) # set level=20 or logging.INFO to turn of debug
logger = logging.getLogger("rich")
def print_version_info():
print("--------------------------------------------------------")
print(" CMake Sanitizer: detect bad uses in CMake files")
print(" Author: Zhuo Zhang (imzhuo#foxmail.com)")
print(" Version: 2023.07.15")
print("--------------------------------------------------------")
def get_lines(filepath):
"""
@param filepath path to CMakeLists.txt, or path to xxx.cmake
"""
fin = open(filepath, encoding="UTF-8")
lines = [_.rstrip("\n") for _ in fin.readlines()]
fin.close()
return lines
"""
cmake 的语法包括这几种:
- 函数调用: <函数名字>(<参数列表>), 可以写成多行的形式, 寻找右括号)即可
- set(), unset(), return(), add_definitions(), add_compile_definitions(), include_directories(),
add_compile_options(), cmake_minimum_required(), find_package(), target_include_directories(),
target_link_libraries(), ...
- list(), include(), get_filepath_component()
- find_program(), message(), string()
- file()
- set_property(), get_target_property(), set_target_properties()
- execute_process(), add_custom_command()
- target_sources()
- 自定义函数() / 宏()
- 控制流
- if()/endif()
- if()/elseif()/endif()
- foreach()/endforeach()
- macro()/endmacro()
- function()/endfunction()
"""
control_flow_keywords = [
"if",
"endif",
"elseif",
"foreach",
"endforeach",
"macro",
"endmacro",
"function",
"endfunction",
]
class Node(object):
def __init__(self, identifier, content, start_line_number, end_line_number):
self.identifier = identifier
self.content = content
self.start_line_number = start_line_number
self.end_line_number = end_line_number
def __str__(self):
return self.content
class Ast(object):
def __init__(self, node_lst, filepath):
self.filepath = filepath
self.node_lst = node_lst
self.minimum_cmake_version = None
for node in node_lst:
if node.identifier == "cmake_minimum_required":
self.minimum_cmake_version = node.content.split(" ")[1]
break
def remove_trailing_comments(line):
pos = -1
has_left_quote = False
for i in range(len(line)):
if line[i] == "#" and (not has_left_quote):
pos = i
break
if line[i] == '"': # TODO: consider escaped quote char
has_left_quote = not has_left_quote
if pos == -1:
return line
return line[0:pos]
def parse(filepath):
"""
@param filepath of CMakeLists.txt, or xxx.cmake
@brief parse the given text lines, get list of nodes
return the ast
"""
lines = get_lines(filepath)
num_of_lines = len(lines)
i = 0
node_lst = []
while i < num_of_lines:
line = lines[i]
# print('line {:d} = {:s}'.format(i, line))
lstriped_line = line.lstrip()
# ignore single line comment
if lstriped_line.startswith("#"):
i += 1
continue
lstriped_line = remove_trailing_comments(lstriped_line)
# non-control-flow: functions. maybe single line, maybe multiple line. end with ')'. No nesting (assume no
# trailing comments) actually, `link_directories()` allow nested braces.
left_brace_pos = lstriped_line.find("(")
# print('!!', lstriped_line)
if left_brace_pos > 0:
identifier = lstriped_line[0:left_brace_pos].rstrip()
# print('!! ' + identifier)
if identifier not in control_flow_keywords:
# print('!!' + identifier)
# single line
if lstriped_line.rstrip().endswith(")"):
line_number = i + 1
node = Node(identifier, line, line_number, line_number)
node_lst.append(node)
else:
j = i + 1
content_lines = [line]
while j < num_of_lines:
content_lines.append(lines[j])
if lines[j].rstrip().endswith(")"):
break
j += 1
content = "\n".join(content_lines)
start_line_number = i + 1
end_line_number = j + 1
node = Node(identifier, content, start_line_number, end_line_number)
node_lst.append(node)
i = j
i += 1
# print all node
# print('----------\ndump node info:')
# for node in node_lst:
# print(node.identifier, node.start_line_number, node.end_line_number)
# return node_lst
ast = Ast(node_lst, filepath)
return ast
def check_rule1(ast, node):
# rule1: 不应当使用 add_definitions() 对于 add_definitions(-w) 或 add_definitions(-Wfoo=123) 的用法则更是大坑,
# 无法在 CMakeLists.txt 里获取到指定的 flags, 但 compile_commands.json 中却确实存在, 导致 overlook 工具失效并且难以察觉
ret = True
if node.identifier == "add_definitions":
ret = False
if ("-w" in node.content) or ("-W" in node.content):
logger.error(
"Wrong usage detected in {:s}:{:d}\n{:s}\n".format(
ast.filepath, node.start_line_number, node.content
)
)
logger.info(
"add_definitions() should only be used for macro definitions(e.g. -Dfoo=1),"
"you should not pass compile options (e.g. -w, -Werror=return-type) to it!"
)
else:
logger.warning(
"Not recommended usage detected in {:s}:{:d}\n{:s}".format(
ast.filepath, node.start_line_number, node.content
)
)
if "-D" in node.content:
logger.info(
" Use add_compile_definitions(foo=1) to add preprocessor definitions (cmake>=3.12)"
)
if "-I" in node.content:
logger.info(
" Use include_directories(some_dir) to add include directories."
)
if ("-D" not in node.content) and ("-I" not in node.content):
logger.info(
" Use add_compile_options() to add other options, e.g. -Werror=return-type, -fPIC"
)
logger.info(
" c.f. https://cmake.org/cmake/help/latest/command/add_definitions.html\n"
)
return ret
def check_rule2(ast, node):
# rule2:
# 尽量不要使用具有全局作用域的函数 link_directories
ret = True
if node.identifier in ["link_directories", "link_libraries"]:
ret = False
logger.warning(
"Not recommended usage detected in {:s}:{:d}\n{:s}".format(
ast.filepath, node.start_line_number, node.content
)
)
logger.info(
" Use target_link_libraries(target <PUBLIC|PRIVATE|INTERFACE> dep_target_lst)\n"
)
logger.info(
" or, target_link_libraries(target <PUBLIC|PRIVATE|INTERFACE> /absolute/path/to/lib/file)\n"
)
logger.info(
" or, target_link_directories(target <PUBLIC|PRIVATE|INTERFACE> the_dir)\n"
)
return ret
def check_rule3(ast):
ret = True
# rule3:
# add_compile_options() 是对它之后的 target 生效的, 如果它之前没有任何 target 则不起作用, 应当报 error。
# ref: https://stackoverflow.com/a/40522384/2999096
node_lst = ast.node_lst
add_compile_options_appeared_lines = []
for node in node_lst:
if node.identifier == "add_compile_options":
add_compile_options_appeared_lines.append(node.start_line_number)
if len(add_compile_options_appeared_lines) == 0:
return ret
target_nodes = []
for node in node_lst:
# print(' ', node.identifier)
if node.identifier in ["add_executable", "add_libraries"]:
target_nodes.append(node)
# print('target_create_lines:')
# print(target_create_lines)
# print('add_compile_options_appeared_lines')
# print(add_compile_options_appeared_lines)
for target_node in target_nodes:
for compile_options_line in add_compile_options_appeared_lines:
if target_node.start_line_number < compile_options_line:
ret = False
logger.error(
"add_compile_options() should appear before all targets creation. It appeard in {:s}:{:d}".format(
ast.filepath, compile_options_line
)
)
logger.error(
" while {:s}() appeared in {:s}:{:d}".format(
target_node.identifier,
ast.filepath,
target_node.start_line_number,
)
)
logger.error("You may also use target_compile_options() instead.")
return ret
def parse_set(content):
# 解析 set(xxKey xxValue) 的语句, 得到 xxKey, xxValue
inner_content = content.split("(")[1].split(")")[0]
key, value = inner_content.split(" ")[0], inner_content.split(" ")[1]
return key, value
def check_rule4(ast):
# rule4: 检查 fPIC 全局设置是否正确
ret = True
node_lst = ast.node_lst
for node in node_lst:
if node.identifier == "set":
key, value = parse_set(node.content)
if key == "POSITION_INDEPENDENT_CODE":
logger.error(
"Invalid variable name for setting fPIC: `POSITION_INDEPENDENT_CODE`. It appeard in {:s}:{:d}".format(
ast.filepath, node.start_line_number
)
)
logger.error("The correct one is `CMAKE_POSITION_INDEPENDENT_CODE`")
ret = False
break
return ret
def validate(ast):
node_lst = ast.node_lst
ret = True
if ast.minimum_cmake_version < "3.15":
ret = False
logger.warning(
"Suggested minimum cmake version >= 3.15, detected is {:s}".format(
ast.minimum_cmake_version
)
)
return ret
print("--- checking rule1")
for node in node_lst:
ret = check_rule1(ast, node)
if ret != True:
return ret
print("--- checking rule2")
for node in node_lst:
ret = check_rule2(ast, node)
if ret != True:
return ret
print("--- checking rule3")
ret = check_rule3(ast)
if ret != True:
return ret
print("--- checking rule4")
ret = check_rule4(ast)
if ret != True:
return ret
print("===")
if ret == True:
print("CMake Sanity Result: OK")
else:
print("CMake Sanity Result: Not OK")
return ret
def test_remove_trailing_comments():
s = remove_trailing_comments('set(s "1#2")#555')
assert s == 'set(s "1#2")'
if __name__ == "__main__":
print_version_info()
import sys
if len(sys.argv) > 1:
path = sys.argv[1]
ast = parse(path)
# path = 'cmake_sanitizer_test/test_global_fPIC_with_wrong_var_name/CMakeLists.txt'
# check_rule4(ast)
validate(ast)
else:
print("Usage: python cmake_sanitizer.py /path/to/CMakeLists.txt")