-
Notifications
You must be signed in to change notification settings - Fork 3
/
files_to_compilation_database_py
executable file
·189 lines (162 loc) · 5.92 KB
/
files_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
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
"""A program to generate a compilation database given a list of files,
one per line.
"""
import sys
import os
import argparse
import json
if __name__ == '__main__':
description = 'Generate a Clang compilation database from a list of files.'
c_extensions = [
'.c'
]
h_extensions = [
'.h'
]
cxx_extensions = [
'.cpp',
'.cc',
'.cxx',
'.C'
]
hxx_extensions = [
'.hpp',
'.hh',
'.hxx',
'.H'
]
objc_extensions = [
'.m'
]
objcxx_extensions = [
'.mm'
]
default_extensions = \
c_extensions + \
h_extensions + \
cxx_extensions + \
hxx_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('--extensions',
action='store',
default=default_extensions,
help='A list of additional filename extensions.')
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.')
parser.add_argument('--flags',
action='store',
default='',
help='The flags to pass to the Clang compiler.')
parser.add_argument('--cflags',
action='store',
default='',
help='The flags to pass to the Clang C compiler.')
parser.add_argument('--cxxflags',
action='store',
default='',
help='The flags to pass to the Clang C++ compiler.')
parser.add_argument('--objcflags',
action='store',
default='',
help='The flags to pass to the Clang Objective-C compiler.')
parser.add_argument('--objcxxflags',
action='store',
default='',
help='The flags to pass to the Clang Objective-C++ compiler.')
parser.add_argument('-I', '--include',
action='append',
default=[],
help='An include directory to pass to the Clang compiler.')
parser.add_argument('-D', '--define',
action='append',
default=[],
help='A defines to pass to the Clang compiler.')
parser.add_argument('-U', '--undefine',
action='append',
default=[],
help='An undefine to pass to the Clang compiler.')
args = parser.parse_args()
args.output_filename = os.path.abspath(args.output_filename)
# 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
compiler = 'clang++'
flags = ['-c']
filename = line
# check if the filename extension is supported
n, e = os.path.splitext(filename)
if e not in args.extensions:
continue
flags.append('-o ' + n + '.o')
flags.append(args.flags)
if e in c_extensions:
flags.append('-x c')
flags.append(args.cflags)
if e in h_extensions:
flags.append('-x c-header')
flags.append(args.cflags)
if e in cxx_extensions:
flags.append('-x c++')
flags.append(args.cxxflags)
if e in hxx_extensions:
flags.append('-x c++-header')
flags.append(args.cxxflags)
if e in objc_extensions:
flags.append('-x objective-c')
flags.append(args.objcflags)
if e in objcxx_extensions:
flags.append('-x objective-c++')
flags.append(args.objcxxflags)
flags += ['-I' + s for s in args.include]
flags += ['-D' + s for s in args.define]
flags += ['-U' + s for s in args.undefine]
command = compiler + ' ' + ' '.join(flags) + ' ' + filename
entry = {
'directory': args.root_directory if args.root_directory != "" else os.getcwd (),
'command': command,
'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)