-
Notifications
You must be signed in to change notification settings - Fork 3
/
.ycm_extra_conf.py
131 lines (108 loc) · 4.61 KB
/
.ycm_extra_conf.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
# 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 os
import subprocess
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
logger = logging.getLogger(__name__)
DIR_OF_THIS_SCRIPT = os.path.abspath(os.path.dirname(__file__))
DIR_OF_THIRD_PARTY = os.path.join(DIR_OF_THIS_SCRIPT, "tmol/extern")
SOURCE_EXTENSIONS = {"c++": [".cpp", ".cxx", ".cc", "c"], "cuda": [".cu"]}
HEADER_EXTENSIONS = [".h", ".hxx", "hpp", ".hh"]
# Load the expected torch and tmol include paths via direct calls to the target
# module. This assumes the modules are avail in the env (eg. setup.py develop).
torch_paths = subprocess.check_output(
"python -c "
"'import torch.utils.cpp_extension; "
'print("\\n".join( '
"torch.utils.cpp_extension.include_paths(torch.cuda.is_available())"
"))'",
shell=True,
universal_newlines=True,
).splitlines()
tmol_paths = subprocess.check_output(
"python -c 'import tmol.extern; "
'print("\\n".join(tmol.include_paths())); '
'print("\\n".join(tmol.extern.include_paths())); \'',
shell=True,
universal_newlines=True,
).splitlines()
# 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.
flags = (
["-fexceptions", "-DNDEBUG", "-std=c++14", "-isystem", get_python_inc()]
+ ["-I" + p for p in torch_paths]
+ ["-I" + p for p in tmol_paths]
)
def is_header_file(filename):
extension = os.path.splitext(filename)[1]
return extension in HEADER_EXTENSIONS
def find_corresponding_source_file(filename):
if is_header_file(filename):
basename = os.path.splitext(filename)[0]
for _filetype, extensions in SOURCE_EXTENSIONS.items():
for extension in extensions:
replacement_file = basename + extension
if os.path.exists(replacement_file):
return replacement_file
return filename
def file_language(filename):
extension = os.path.splitext(filename)[1]
for language, extensions in SOURCE_EXTENSIONS.items():
if extension in extensions:
return language
return "c++"
def Settings(**kwargs):
logger.info("Settings: %s" % kwargs)
if kwargs["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 = find_corresponding_source_file(kwargs["filename"])
result = {
"flags": flags + ["-x", file_language(filename)],
"include_paths_relative_to_dir": DIR_OF_THIS_SCRIPT,
"override_filename": filename,
}
else:
result = {}
logger.info("Result: %s" % result)
return result
if __name__ == "__main__":
print(Settings(language="cfamily", filename="foo.cpp"))