forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_compilation_database.py
executable file
·53 lines (41 loc) · 1.65 KB
/
gen_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
#!/usr/bin/env python
import argparse
import os
import json
def generateCompilationDatabase():
os.system("bazel/gen_compilation_database.sh //source/... //test/... //tools/...")
def isCompileTarget(target, args):
filename = target["file"]
if not args.include_headers:
for ext in (".h", ".hh", ".hpp", ".hxx"):
if filename.endswith(".h") or filename.endswith(ext):
return False
if not args.include_genfiles:
if filename.startswith("bazel-out/"):
return False
if not args.include_external:
if filename.startswith("external/"):
return False
return True
def modifyCompileCommand(target):
cxx, options = target["command"].split(" ", 1)
# Workaround for bazel added C++11 options, those doesn't affect build itself but
# clang-tidy will misinterpret them.
options = options.replace("-std=c++0x ", "")
options = options.replace("-std=c++11 ", "")
target["command"] = " ".join(["clang++", options])
return target
def fixCompilationDatabase(args):
with open("compile_commands.json", "r") as db_file:
db = json.load(db_file)
db = [modifyCompileCommand(target) for target in db if isCompileTarget(target, args)]
with open("compile_commands.json", "w") as db_file:
json.dump(db, db_file, indent=2)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate JSON compilation database')
parser.add_argument('--include_external', type=bool, default=False)
parser.add_argument('--include_genfiles', type=bool, default=False)
parser.add_argument('--include_headers', type=bool, default=False)
args = parser.parse_args()
generateCompilationDatabase()
fixCompilationDatabase(args)