forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiwyu-check.py
executable file
·199 lines (165 loc) · 6.52 KB
/
iwyu-check.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
#!/usr/bin/env -S python3 -B
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
import glob
import json
import logging
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
import click
import coloredlogs
# Supported log levels, mapping string values required for argument
# parsing into logging constants
__LOG_LEVELS__ = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warn': logging.WARN,
'fatal': logging.FATAL,
}
root_dir = os.path.dirname(os.path.realpath(__file__))
proj_root_dir = os.path.join(Path(root_dir).parent.parent)
def find_program(names):
for name in names:
found = shutil.which(name)
if found is not None:
return found
@click.command()
@click.option(
'--log-level',
show_default=True,
default='info',
type=click.Choice(__LOG_LEVELS__.keys(), case_sensitive=False),
help='Set the verbosity of script output.')
@click.option(
'--no-log-timestamps',
default=False,
is_flag=True,
help='Skip timestamps in log output.')
@click.option(
'--compile-commands-glob',
show_default=True,
default=os.path.join(proj_root_dir, "out", "debug",
"compile_commands*.json"),
help='Set global pattern for compile_commands.json files.')
@click.option(
'--mapping-file-dir',
help='Set directory with iwyu.imp mapping file.')
@click.option(
'--iwyu-args',
show_default=True,
default="-Xiwyu --no_fwd_decls",
help='Set custom arg(s) for include what you use.')
@click.option(
'--clang-args',
default="",
help='Set custom arg(s) for clang.')
@click.argument(
'source',
nargs=-1,
type=click.Path(exists=True))
def main(compile_commands_glob, source, mapping_file_dir,
iwyu_args, clang_args, log_level, no_log_timestamps):
# Ensures somewhat pretty logging of what is going on
log_fmt = '%(asctime)s %(levelname)-7s %(message)s'
if no_log_timestamps:
log_fmt = '%(levelname)-7s %(message)s'
coloredlogs.install(level=__LOG_LEVELS__[log_level], fmt=log_fmt)
# checking if a program IWYU exists
iwyu = find_program(('iwyu_tool', 'iwyu_tool.py'))
if iwyu is None:
logging.error("Can't find IWYU")
sys.exit(1)
# For iterating how many files had problems with includes
warning_in_files = 0
platform = ""
compile_commands_glob = glob.glob(compile_commands_glob)
if not compile_commands_glob:
logging.error("Can't find compile_commands.json file(s)")
sys.exit(1)
for compile_commands in compile_commands_glob:
compile_commands_path = os.path.dirname(compile_commands)
compile_commands_file = os.path.join(
compile_commands_path, "compile_commands.json")
logging.debug("Copy compile command file %s to %s",
compile_commands, compile_commands_file)
with contextlib.suppress(shutil.SameFileError):
shutil.copyfile(compile_commands, compile_commands_file)
# Prase json file for find target name
with open(compile_commands, 'r') as json_data:
json_data = json.load(json_data)
for key in json_data:
find_re = re.search(r'^.*/src/platform*?\/(.*)/.*$', key['file'])
if find_re is not None:
platform = find_re.group(1)
break
if not platform:
logging.error("Can't find platform")
sys.exit(1)
if not mapping_file_dir:
mapping_file_dir = os.path.join(root_dir, "platforms", platform)
# Platform specific clang arguments, as some platform
# may needed some hacks for clang compiler for work properly.
platform_clang_args = []
if platform == "Tizen":
platform_clang_args = [
"--target=arm-linux-gnueabi",
"-I$TIZEN_SDK_TOOLCHAIN/arm-tizen-linux-gnueabi/include/c++/9.2.0",
"-I$TIZEN_SDK_TOOLCHAIN/arm-tizen-linux-gnueabi/include/c++/9.2.0/arm-tizen-linux-gnueabi",
"-I$TIZEN_SDK_TOOLCHAIN/lib/gcc/arm-tizen-linux-gnueabi/9.2.0/include",
]
# TODO: Add another platform for easy scanning
# Actually works scanning for platform: tizen, darwin, linux other not tested yet.
command_arr = [
iwyu,
"-p", compile_commands_path, *source,
"--", iwyu_args,
"-Xiwyu", "--mapping_file=" + mapping_file_dir + "/iwyu.imp",
] + platform_clang_args + [clang_args]
logging.info("Used compile commands: %s", compile_commands)
logging.info("Scanning includes for platform: %s", platform)
logging.info("Scanning sources(s): %s", ", ".join(source))
logging.debug("Command: %s", " ".join(command_arr))
status = subprocess.Popen(" ".join(command_arr),
shell=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
logging.info("============== IWYU output start ================")
logger = logging.info
for line in status.stdout:
line = line.rstrip()
if re.search(r"^warning:", line):
logger = logging.warning
elif re.search(r"should (add|remove)? these lines:$", line):
logger = logging.warning
elif re.search(r"has correct #includes/fwd-decls\)$", line):
logger = logging.info
elif re.search(r"^The full include-list for", line):
logger = logging.warning
warning_in_files += 1
logger("%s", line)
logging.info("============== IWYU output end ================")
if warning_in_files:
logging.error("Number of files with include issues: %d",
warning_in_files)
sys.exit(2)
else:
logging.info("Every include looks good!")
if __name__ == '__main__':
main(auto_envvar_prefix='CHIP')