-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_flags.py
More file actions
executable file
·63 lines (52 loc) · 2.3 KB
/
get_flags.py
File metadata and controls
executable file
·63 lines (52 loc) · 2.3 KB
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
#!/usr/bin/env python2
import json
import os
import sys
import re
from string import Template
IGNORED_FLAGS = ['-o', '-c']
try:
base_dir = os.path.abspath(sys.argv[1])
with open(os.path.join(base_dir, 'compile_commands.json')) as f:
db = json.load(f)
flags_dict = {'-x':'c++', '-std=c++17':''}
for entry in db:
args = entry['command']
prev = None
for arg in args.split():
if prev is not None:
if arg.startswith('-'):
if prev not in IGNORED_FLAGS:
flags_dict[prev] = ""
prev = arg
else:
if prev not in IGNORED_FLAGS:
flags_dict[prev] = arg
prev = None
elif arg.startswith('-'):
prev = arg
# Defaul to c++17 only
flags_dict.pop("-std=c++11", None)
flags_dict.pop("-std=c++14", None)
flags_dict.pop("-std=gnu++11", None)
flags_dict.pop("-std=gnu++14", None)
final_flags = "\n".join([' \'{0}\',\n \'{1}\','.format(key, value) for (key, value) in sorted(flags_dict.items())])
final_flags = re.sub(" \'\',\n| \'\',", "", final_flags)
# for key, val in sorted(flags_dict.items()):
# print key, "=>", val
if os.path.exists(os.path.join(base_dir, '.ycm_extra_conf.py')):
print(".ycm_extra_conf already exists. Overwrite? [y/N] "),
response = sys.stdin.readline().strip().lower()
if(response != "y" and response != "yes"):
sys.exit()
print("Now writing .ycm_extra_conf.py in '{}'".format(base_dir))
with open(os.path.join(base_dir, 'ycm_extra_conf.template')) as templ:
with open(os.path.join(base_dir, '.ycm_extra_conf.py'), 'w') as outf:
new_template = Template(templ.read())
new_file = new_template.substitute(flags=final_flags, database=os.path.join(base_dir, ''))
outf.write(new_file)
outf.close()
except(IOError, OSError):
print IOError.message
print OSError.message
print "Bad arguments\nUsage:\n\tpython /some/path/get_flags.py directory_containing compile_commands.json"