-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommands_to_compilation_database_py
executable file
·180 lines (148 loc) · 5.26 KB
/
commands_to_compilation_database_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
#!/usr/bin/env python3
"""A program to generate a compilation database by parsing build
commands on standard input or an input file. It assumes that a single
line compiles a single source file and the clang compiler is used as
the compiler.
Note that commands for other compilers can be used here, but there is
no guarantee that the options are understood by tools that use the
database.
"""
import sys
import os
import argparse
import re
import json
if __name__ == '__main__':
description = 'Generate a Clang compilation database from compiler commands.'
default_compilers = [
'cc',
'c++',
'clang',
'clang++',
'gcc',
'g++',
'cl',
# for now
'/usr/local/bin/clang-3.4',
'/usr/local/bin/clang++-3.4'
]
c_extensions = [
'.c',
'.h'
]
cxx_extensions = [
'.cpp',
'.hpp',
'.cc',
'.hh',
'.cxx',
'.hxx',
'.C',
'.H',
]
objc_extensions = [
'.m'
]
objcxx_extensions = [
'.mm'
]
default_extensions = \
c_extensions + \
cxx_extensions + \
objc_extensions + \
objcxx_extensions
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--root-directory',
action='store',
default='',
help='The root directory for the project.')
parser.add_argument('--compilers',
action='store',
default=default_compilers,
help='A list of additional compilers.')
parser.add_argument('--extensions',
action='store',
default=default_extensions,
help='A list of additional filename extensions.')
parser.add_argument('--build-tool',
action='store',
default='',
help='The build tool that generated the input.')
parser.add_argument('--compile-command-regex',
action='store',
default='',
help='A regular expression to parse the command line into the compiler, flags, and filename.')
parser.add_argument('--incremental',
action='store_true',
default=False,
help='Incrementally update existing database.')
parser.add_argument('-o', '--output-filename',
action='store',
default='compile_commands.json',
help='The filename of the compilation database.')
args = parser.parse_args()
args.output_filename = os.path.abspath(args.output_filename)
compile_command_regex = None
if args.compile_command_regex == '':
if args.build_tool in ['', 'make']:
compile_command_regex = re.compile(r'^([^ ]+) .* ([^ ]+) +-o +[^ ]+ *$')
elif args.build_tool == 'Boost.Build':
compile_command_regex = re.compile(r'^"([^"]+)" .+ "([^"]+)"$')
else:
if args.build_tool != '':
print('warning: regex overriding build tool option.')
compile_command_regex = re.compile(args.compile_command_regex)
assert compile_command_regex is not None
# create the initial compilation database
compilation_database = []
if args.incremental:
if os.path.exists(args.output_filename):
with open(args.output_filename, 'r') as inputfp:
compilation_database = json.load(inputfp)
# generate a more efficient compilation map by filename
compilation_map = {}
for entry in compilation_database:
f = entry['file']
if not os.path.isabs(f):
f = os.path.join(entry['directory'], f)
compilation_map[f] = entry
# parse the compilation log and update the compilation database
for line in sys.stdin:
line = line.strip()
if line == '':
continue
m = compile_command_regex.match(line)
if m is None:
continue
if len(m.groups()) != 2:
continue
compiler = m.group(1)
filename = m.group(2)
# check if it is a call to a compiler
if compiler not in args.compilers:
continue
# check if the filename extension is supported
n, e = os.path.splitext(filename)
if e not in args.extensions:
continue
entry = {
'directory': args.root_directory if args.root_directory != "" else os.getcwd(),
'command': line,
'file': filename
}
f = entry['file']
if not os.path.isabs(f):
f = os.path.join(entry['directory'], f)
# check for directory/file already in database
compilation_map[f] = entry
# convert the compilation map to a compilation database, sorted by
# the filename
compilation_database = []
for k in sorted(compilation_map.keys()):
compilation_database.append(compilation_map[k])
# print as json
with open(args.output_filename, 'w') as outputfp:
json.dump(compilation_database,
fp=outputfp,
indent=2,
sort_keys=True)