forked from ycm-core/ycmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ycm_extra_conf_obi.py
executable file
·436 lines (369 loc) · 15 KB
/
ycm_extra_conf_obi.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python3
# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
from distutils.sysconfig import get_python_inc
import platform
import os
import io
import fnmatch
import logging
import subprocess
import os.path as p
from pprint import pformat as PF
from pprint import pprint as PP
logging.basicConfig(level=logging.INFO)
DIR_OF_THIS_SCRIPT = p.abspath( p.dirname( __file__ ) )
DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
DIR_OF_WATCHDOG_DEPS = p.join( DIR_OF_THIRD_PARTY, 'watchdog_deps' )
throw_exceptions = True
base_flags = [
# u'-x', u'c++'
]
base_flags_files=[
'.ycm_base_flags',
os.path.expanduser('~') + os.path.sep + '.obi-config' + os.path.sep + 'ycm_base_flags'
]
# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
fallback_flags = [
# u'-std=c++17',
# u'-Wall',
# u'-Wextra',
# u'-Werror',
# u'-Wno-long-long',
# u'-Wno-variadic-macros',
# u'-fexceptions',
# u'-ferror-limit=10000',
# u'-DNDEBUG',
]
fallback_flags_files=[
'.ycm_fallback_flags',
os.path.expanduser('~') + os.path.sep + '.obi-config' + os.path.sep + 'ycm_fallback_flags'
]
extra_flags_files=[
'.clang_complete',
]
source_ext = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
header_ext = [ '.hpp', '.hxx', '.hh', '.h' ]
## helper functions ###########################################################
def remove_all_but_last(predicate, list_to_modify):
first = True
for index in range(len(list_to_modify)-1, -1, -1):
if predicate(list_to_modify[index]):
if first:
first = False
continue
list_to_modify.pop(index)
###############################################################################
class FakeInfo(object):
def __init__(self):
self.compiler_flags_ = []
self.include_dirs = []
self.compiler_flags_end = []
self.compiler_working_dir_ = None
def __bool__(self):
return self.compiler_flags_ != None
def add_info(self,info):
if not self.compiler_flags_:
start=None
end=None
self.compiler_working_dir_ = info.compiler_working_dir_
for item in info.compiler_flags_:
if item.startswith("-I") and not start:
start=True
elif start:
end=True
if not start:
self.compiler_flags_.append(item)
if not end:
self.include_dirs.append(item)
else:
self.compiler_flags_end.append(item)
else:
for item in info.compiler_flags_:
if item.startswith("-I") and item not in self.include_dirs:
self.include_dirs += item
def close(self):
self.compiler_flags_ += self.include_dirs
self.compiler_flags_ += self.compiler_flags_end
# class FakeInfo
## finding stuff
def find_closest_path(path, target):
logging.debug("find_closest_path: path {0}, target {1}".format(path, target))
candidate = os.path.join(path, target)
if(os.path.isfile(candidate) or os.path.isdir(candidate)):
logging.info("closest " + target + " at " + candidate)
return candidate;
else:
parent = os.path.dirname(os.path.abspath(path));
if(parent == path):
#end recursion
return None
return find_closest_path(parent, target)
def find_closest_db(path, target):
candidates = [ os.path.join(path, target)
, os.path.join(path+"-build","current", target)
, os.path.join(path+"-build", target)
, os.path.join(path,"build", target)
]
for candidate in candidates:
logging.debug("checking: " + candidate)
if(os.path.isfile(candidate) or os.path.isdir(candidate)):
#logging.info("closest " + target + " at " + candidate)
print("compile_commands.json: closest " + target + " at " + candidate)
return candidate, path;
# not found in this iteration -> recurse
parent = os.path.dirname(os.path.abspath(path));
if(parent == path):
#end recursion
print("compile_commands.json: not found")
return None, None
logging.debug("recuse: " + parent)
return find_closest_db(parent, target)
def flags_for_closest_include(filename):
flags = []
include_path = find_closest_path(filename, 'include')
if include_path:
logging.info("found include dir")
flags.append("-I")
flags.append(include_path)
else:
logging.info("no include dir found")
return flags
def find_database(filename):
# Do NOT import ycm_core at module scope.
import ycm_core
compilation_db_path , source_path = find_closest_db(filename, 'compile_commands.json')
if not compilation_db_path:
return None, None
compilation_db_dir = os.path.dirname(compilation_db_path)
logging.info("Set compilation database directory to " + compilation_db_dir)
compilation_db = ycm_core.CompilationDatabase(compilation_db_dir)
if not compilation_db:
logging.info("Compilation database file found but unable to load")
return None, None
return compilation_db, source_path
def flags_from_file(flags_file_candidate, source_file):
flag_file = None
logging.info("trying extra file: {}".format(flags_file_candidate))
if os.path.isabs(flags_file_candidate) and os.path.exists(flags_file_candidate):
flag_file = flags_file_candidate
else:
flag_file = find_closest_path(source_file, flags_file_candidate)
if not flag_file:
return []
with io.open(flag_file, mode="r", encoding="utf-8") as fh:
logging.info("adding flags from {}".format(flag_file))
directory = os.path.dirname(flag_file)
new_flags = []
for line in fh:
if not line.startswith("#"):
logging.debug("adding flag {}".format(line.rstrip()))
new_flags.append(line.rstrip())
if source_file:
return make_relative_flags_to_absolute(new_flags, directory)
else:
return new_flags
def flags_from_file_list(file_list, source_file):
flags = []
for candidate in file_list:
new_flags = flags_from_file(candidate, source_file)
if new_flags:
flags += new_flags
logging.info("adding {} from file {}".format(str(new_flags), candidate))
return flags
return flags
## finding stuff - end
## simple helper
def is_header(filename):
extension = os.path.splitext(filename)[1]
return extension in header_ext
def make_relative_flags_to_absolute(flags, working_directory):
if not working_directory:
return list(flags)
new_flags = []
make_next_absolute = False
path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
for flag in flags:
new_flag = flag
if make_next_absolute:
make_next_absolute = False
if not flag.startswith('/'):
new_flag = os.path.join(working_directory, flag)
for path_flag in path_flags:
if flag == path_flag:
make_next_absolute = True
break
if flag.startswith(path_flag):
path = flag[ len(path_flag): ]
new_flag = path_flag + os.path.join(working_directory, path)
break
if new_flag:
new_flags.append(new_flag)
return new_flags
def string_vector_to_list(string_vector):
return [ str(x) for x in string_vector ]
def string_vector_to_str(string_vector):
return str([ str(x) for x in string_vector ])
## simple helper - end
## using database / c interface
def get_flags_from_compilation_database(database, filename):
#try:
compilation_info, override_file = get_info_for_file_from_database(database, filename)
#except Exception as e:
# logging.info("No compilation info for " + filename + " in compilation database")
# if throw_exceptions:
# raise e
# else:
# return None, None
if not compilation_info:
logging.info("No compilation info for " + filename + " in compilation database")
return None, None
#if not compilation_info.compiler_flags_:
if not string_vector_to_list(compilation_info.compiler_flags_):
logging.error("flags empty")
logging.error("tried to use database in {}".format(database))
logging.error(compilation_info.compiler_flags_)
return None, None
logging.info("found CompilationInfo for : " + filename)
logging.debug("dir" + PF(dir(compilation_info)))
logging.debug("workingdir:" + str(compilation_info.compiler_working_dir_))
logging.debug("flags: " + string_vector_to_str(compilation_info.compiler_flags_))
return make_relative_flags_to_absolute(
compilation_info.compiler_flags_,
compilation_info.compiler_working_dir_), override_file
def get_info_for_file_from_database(database, filename):
logging.debug("database:" + PF(database))
if not is_header(filename):
logging.info("is cpp file")
return database.GetCompilationInfoForFile(filename), None
else:
#find matching source file
logging.info("is header file")
basename = os.path.splitext(filename)[0]
for extension in source_ext:
replacement_file = basename + extension
if os.path.exists(replacement_file):
compilation_info = database.GetCompilationInfoForFile(replacement_file)
if compilation_info.compiler_flags_:
return compilation_info, replacement_file
#use any source file in the headers directory
dpath = os.path.dirname(filename)
for f in os.listdir(dpath):
#skip non source files
extension = os.path.splitext(f)[1]
if extension not in source_ext:
continue
#get and join compilation for all cpp files in dir
fpath = dpath + os.path.sep + f
compilation_info = FakeInfo()
tmp_info = database.GetCompilationInfoForFile(fpath)
if tmp_info:
compilation_info.add_info(tmp_info)
compilation_info.close()
if compilation_info.compiler_flags_:
return compilation_info, None
return None, None
## using database - end
# Called by vim Plugin
def Settings( **kwargs ):
language = kwargs[ 'language' ]
if language == 'cfamily':
# If the file is a header, try to find the corresponding source file and
# retrieve its flags from the compilation database if using one. This is
# necessary since compilation databases don't have entries for header files.
# In addition, use this source file as the translation unit. This makes it
# possible to jump from a declaration in the header file to its definition
# in the corresponding source file.
filename = kwargs[ 'filename' ]
logging.info("filename:" + filename)
final_flags = [] + base_flags
final_flags += flags_from_file_list(base_flags_files, filename) #use this for tool-chaing etc
extra_flags = flags_from_file_list(extra_flags_files, filename)
final_flags += extra_flags
# try to use compile comamnds json
override_file = None
db_used = False
database, source_dir = find_database(filename)
db_flags=None
if database:
db_flags, override_file = get_flags_from_compilation_database(database, filename)
if db_flags:
final_flags += db_flags
else:
#TODO fix me
source_dir = 'FIXME - UNKNOWN SOURCE DIR'
logging.warning("using fallback strategy to get flags")
final_flags += fallback_flags
final_flags += flags_for_closest_include(filename)
# ensure we can override std with extra file
final_flags += [ x for x in extra_flags if x.startswith("-std=") ]
# remove `-std=` duplicates
remove_all_but_last(lambda x : x.startswith("-std="), final_flags)
rv = {
'flags': final_flags,
'include_paths_relative_to_dir': source_dir,
}
if override_file:
rv['override_filename'] = override_file
return rv
if language == 'python':
return {
#'interpreter_path': PathToPythonUsedDuringBuild() original
'interpreter_path': '/usr/bin/python3'
}
return {}
def PythonSysPath( **kwargs ):
sys_path = kwargs[ 'sys_path' ]
interpreter_path = kwargs[ 'interpreter_path' ]
major_version = subprocess.check_output( [
interpreter_path, '-c', 'import sys; print( sys.version_info[ 0 ] )' ]
).rstrip().decode( 'utf8' )
sys_path[ 0:0 ] = [ p.join( DIR_OF_THIS_SCRIPT ),
p.join( DIR_OF_THIRD_PARTY, 'bottle' ),
p.join( DIR_OF_THIRD_PARTY, 'regex-build' ),
p.join( DIR_OF_THIRD_PARTY, 'frozendict' ),
p.join( DIR_OF_THIRD_PARTY, 'jedi_deps', 'jedi' ),
p.join( DIR_OF_THIRD_PARTY, 'jedi_deps', 'parso' ),
p.join( DIR_OF_WATCHDOG_DEPS, 'watchdog', 'build', 'lib3' ),
p.join( DIR_OF_WATCHDOG_DEPS, 'pathtools' ),
p.join( DIR_OF_THIRD_PARTY, 'waitress' ) ]
sys_path.append( p.join( DIR_OF_THIRD_PARTY, 'jedi_deps', 'numpydoc' ) )
return sys_path
# For testing
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
import sys
for filename in sys.argv[1:]:
print("file: " + filename +"\n")
input_dict = { "filename" : filename , "language" : "cfamily" }
settings = Settings(**input_dict)
print("final result:\n" + PF(settings))