-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparse_compilation.py
More file actions
163 lines (138 loc) · 4.46 KB
/
parse_compilation.py
File metadata and controls
163 lines (138 loc) · 4.46 KB
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
# Changes from Qualcomm Technologies, Inc. are provided under the following license:
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear
from __future__ import print_function
import argparse
import glob
import json
import multiprocessing
import os
import re
import shutil
import subprocess
import sys
import tempfile
import threading
import traceback
import yaml
is_py2 = sys.version[0] == '2'
if is_py2:
import Queue as queue
else:
import queue as queue
def find_exp_compile_cmds(path):
res = './'
while not os.path.isfile(os.path.join(res, path)):
if os.path.realpath(res) == '/':
sys.exit(1)
res += '../'
return os.path.realpath(res)
def abs_path(f, directory):
if os.path.isabs(f):
return f
return os.path.normpath(os.path.join(directory, f))
def get_cmd_args(f, clang_tidy_bin, checks, tmpdir, build_path,
header_filter, extra_arg, extra_arg_before, quiet,
config):
start = [clang_tidy_bin]
if header_filter is not None:
start.append('-header-filter=' + header_filter)
else:
start.append('-header-filter=^' + build_path + '/.*')
if checks:
start.append('-checks=' + checks)
if tmpdir is not None:
start.append('-export-fixes')
(handle, name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir)
os.close(handle)
start.append(name)
for arg in extra_arg:
start.append('-extra-arg=%s' % arg)
for arg in extra_arg_before:
start.append('-extra-arg-before=%s' % arg)
start.append('-p=' + build_path)
if quiet:
start.append('-quiet')
if config:
start.append('-config=' + config)
start.append(f)
return start
def run(ar, tmpdir, build_path, queue, failed_files):
while True:
name = queue.get()
call = get_cmd_args(name, ar.clang_tidy_binary, ar.checks,
tmpdir, build_path, ar.header_filter,
ar.extra_arg, ar.extra_arg_before,
ar.quiet, ar.config)
sys.stdout.write(' '.join(call) + '\n')
return_code = subprocess.call(call)
if return_code != 0:
failed_files.append(name)
queue.task_done()
def main():
parser = argparse.ArgumentParser(description='')
parser.add_argument('-clang-tidy-binary', metavar='PATH',
default='/usr/bin/clang-tidy-3.5')
parser.add_argument('-clang-apply-replacements-binary', metavar='PATH',
default='clang-apply-replacements')
parser.add_argument('-checks', default=None)
parser.add_argument('-config', default=None)
parser.add_argument('-header-filter', default=None)
parser.add_argument('-j', type=int, default=0)
parser.add_argument('files', nargs='*', default=['.*'])
parser.add_argument('-p', dest='build_path')
parser.add_argument('-extra-arg', dest='extra_arg',
action='append', default=[])
parser.add_argument('-extra-arg-before', dest='extra_arg_before',
action='append', default=[])
parser.add_argument('-quiet', action='store_true')
ar = parser.parse_args()
cmd_args = 'compile_commands.json'
if ar.build_path is not None:
build_path = ar.build_path
else:
build_path = find_exp_compile_cmds(cmd_args)
try:
call = [ar.clang_tidy_binary, '-list-checks']
call.append('-p=' + build_path)
if ar.checks:
call.append('-checks=' + ar.checks)
print(call)
call.append('-')
subprocess.check_call(call)
except:
print("Error: run clang-tidy.", file=sys.stderr)
sys.exit(1)
db = json.load(open(os.path.join(build_path, cmd_args)))
files = [abs_path(entry['file'], entry['directory'])
for entry in db]
max_task = ar.j
if max_task == 0:
max_task = multiprocessing.cpu_count()
tmpdir = None
file_name_re = re.compile('|'.join(ar.files))
return_code = 0
try:
task_queue = queue.Queue(max_task)
failed = []
for _ in range(max_task):
t = threading.Thread(target=run,
args=(ar, tmpdir, build_path, task_queue, failed))
t.daemon = True
t.start()
for name in files:
if file_name_re.search(name):
task_queue.put(name)
task_queue.join()
if len(failed):
return_code = 1
except KeyboardInterrupt:
print('\nCtrl-C!')
if tmpdir:
shutil.rmtree(tmpdir)
os.kill(0, 9)
if tmpdir:
shutil.rmtree(tmpdir)
sys.exit(return_code)
if __name__ == '__main__':
main()