-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile_commands_json_incdirs.py
executable file
·165 lines (129 loc) · 4.88 KB
/
compile_commands_json_incdirs.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
#!/usr/bin/env python3
import json
import argparse
from collections import Counter
import subprocess
import os
import os.path
import re
import sys
import shlex
class CompileCommands(object):
cc_commands = Counter()
inc_dirs = set()
def __init__(self):
pass
def get_inc_dirs(self, curr_dir=True, escape=True):
esc = shlex.quote
if not escape:
esc = lambda s: s
dirs = []
dirs.extend(self.inc_dirs)
if curr_dir:
dirs.append('.')
return [ shlex.quote(os.path.abspath(d)) for d in dirs ]
def load(
self,
filename='compile_commands.json',
load_system_incdir=False,
):
# compile_commands.json :
with open(filename, 'r') as f:
parsed = json.load(f)
for row in parsed:
directory = row['directory'] or '.'
if 'command' in row:
cmd = row['command'].split()[0]
self.cc_commands.update([cmd])
for cmd_part in shlex.split(row['command']):
if cmd_part[0:2] == '-I':
self.inc_dirs.add(cmd_part[2:])
if 'arguments' in row:
self.cc_commands.update([row['arguments'][0]])
inc_dir_next = False
for arg in row['arguments']:
if inc_dir_next:
# ...지난 번 열이 `-I`였을 때: 그냥 추가.
self.inc_dirs.add(arg)
inc_dir_next = False
elif arg == '-I':
# 그냥 `-I` ==> 다음 열이 incdir.
inc_dir_next = True
elif arg.startswith('-I') and len(arg) > 3:
# `-I${dir}`의 형태:
self.inc_dirs.add(arg[2:])
#
if load_system_incdir:
self.load_system_incdir()
# bye: ok
return self
def load_system_incdir(self):
cc = self.cc_commands.most_common()[0][0]
# find inc-dirs from `gcc` | `g++` | `clang++` ...:
lang = 'c'
if '++' in cc:
lang = 'c++'
cmd = f"echo | {cc} -v -x {lang} -E -"
output = subprocess.check_output(cmd, shell=True, encoding='utf-8', stderr=subprocess.STDOUT)
pat_start = re.compile(r"\#include .+ search starts here\:")
pat_end = re.compile(r"End of search list.")
dirs = set()
next_dir_listing = False
for line in output.split(os.linesep):
if pat_start.match(line):
next_dir_listing = True
elif pat_end.match(line):
next_dir_listing = False
elif next_dir_listing:
dirs.add(line.strip())
# print(dirs)
self.inc_dirs.update(dirs)
def __repr__(self):
return f"<CompileCommands: cc_commands={self.cc_commands} inc_dirs={self.inc_dirs}>"
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='compile_commands_json.py',
description='What the program does',
epilog='')
parser.add_argument('-i', '--includes',
help='parse `-I`-options (default: YES)',
default=True,
action='store_true')
parser.add_argument('-y', '--system',
help='system-include-dirs as well',
action='store_true')
parser.add_argument('--ctags',
help='build ctags',
action='store_true')
parser.add_argument('--ctags-etags',
help='build etags by using ctags',
action='store_true')
parser.add_argument('--etags',
help='build etags',
action='store_true')
parser.add_argument('-p', '--print',
help='print include-dirs',
action='store_true')
args = parser.parse_args()
# print(args)
compcmds = CompileCommands()
compcmds.load(load_system_incdir=args.system)
print(compcmds, file=sys.stderr)
if args.ctags:
cmd = 'ctags -R '
cmd = cmd + ' '.join(compcmds.get_inc_dirs())
print(cmd, file=sys.stderr)
subprocess.run(cmd, shell=True)
if args.ctags_etags:
cmd = 'ctags -Re '
cmd = cmd + ' '.join(compcmds.get_inc_dirs())
print(cmd, file=sys.stderr)
subprocess.run(cmd, shell=True)
if args.etags:
cmd = 'etags -R '
cmd = cmd + ' '.join(compcmds.get_inc_dirs())
print(cmd, file=sys.stderr)
subprocess.run(cmd, shell=True)
if args.print:
for inc_dir in compcmds.get_inc_dirs(escape=False):
print(inc_dir)