-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathmake_comp_db.py
executable file
·104 lines (81 loc) · 3.02 KB
/
make_comp_db.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
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
"""
Utility to create compilation DBs
$ make_comp_db.py gen --output=entry.json foo.cpp -- g++ -c -fPIC
$ make_comp_db.py gen --output=entry2.json foo2.cpp -- g++ -c -fPIC
$ make_comp_db.py merge --output=comp_db.json entry.json entry2.json
"""
# pyre-unsafe
import argparse
import json
import shlex
import sys
from typing import List
def process_arguments(arguments: List[str]) -> List[str]:
"""
Process arguments to expand argsfiles.
"""
combined_arguments = []
for arg in arguments:
if arg.startswith("@"):
with open(arg[1:]) as argsfile:
# The argsfile's arguments are separated by newlines; we
# don't want those included in the argument list.
lines = [" ".join(shlex.split(line)) for line in argsfile.readlines()]
# Support nested argsfiles.
combined_arguments.extend(process_arguments(lines))
else:
combined_arguments.append(arg)
return combined_arguments
def gen(args: argparse.Namespace) -> None:
"""
Generate a single compilation command in JSON form.
"""
entry = {}
entry["file"] = args.directory + "/" + args.filename
entry["directory"] = "."
entry["arguments"] = process_arguments(args.arguments)
json.dump(entry, args.output, indent=2)
args.output.close()
def merge(args: argparse.Namespace) -> None:
"""
Merge multiple compilation DB commands into a single DB.
"""
entries = []
for entry in args.entries:
if entry.startswith("@"):
with open(entry[1:]) as argsfile:
sub_entries = map(str.strip, argsfile.readlines())
for sub_entry in sub_entries:
with open(sub_entry) as g:
entries.append(json.load(g))
else:
with open(entry) as f:
entries.append(json.load(f))
json.dump(entries, args.output, indent=2)
args.output.close()
def main(argv: List[str]) -> int:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
parser_gen = subparsers.add_parser("gen")
parser_gen.add_argument("--output", type=argparse.FileType("w"), default=sys.stdout)
parser_gen.add_argument("filename")
parser_gen.add_argument("directory")
parser_gen.add_argument("arguments", nargs="*")
parser_gen.set_defaults(func=gen)
parser_merge = subparsers.add_parser("merge")
parser_merge.add_argument(
"--output", type=argparse.FileType("w"), default=sys.stdout
)
parser_merge.add_argument("entries", nargs="*")
parser_merge.set_defaults(func=merge)
args = parser.parse_args(argv[1:])
args.func(args)
return 0
sys.exit(main(sys.argv))