1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,197 @@ |
1 |
+# This file is NOT licensed under the GPLv3, which is the license for the rest |
|
2 |
+# of YouCompleteMe. |
|
3 |
+# |
|
4 |
+# Here's the license text for this file: |
|
5 |
+# |
|
6 |
+# This is free and unencumbered software released into the public domain. |
|
7 |
+# |
|
8 |
+# Anyone is free to copy, modify, publish, use, compile, sell, or |
|
9 |
+# distribute this software, either in source code form or as a compiled |
|
10 |
+# binary, for any purpose, commercial or non-commercial, and by any |
|
11 |
+# means. |
|
12 |
+# |
|
13 |
+# In jurisdictions that recognize copyright laws, the author or authors |
|
14 |
+# of this software dedicate any and all copyright interest in the |
|
15 |
+# software to the public domain. We make this dedication for the benefit |
|
16 |
+# of the public at large and to the detriment of our heirs and |
|
17 |
+# successors. We intend this dedication to be an overt act of |
|
18 |
+# relinquishment in perpetuity of all present and future rights to this |
|
19 |
+# software under copyright law. |
|
20 |
+# |
|
21 |
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
22 |
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
23 |
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
24 |
+# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
|
25 |
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|
26 |
+# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
27 |
+# OTHER DEALINGS IN THE SOFTWARE. |
|
28 |
+# |
|
29 |
+# For more information, please refer to <http://unlicense.org/> |
|
30 |
+ |
|
31 |
+import os |
|
32 |
+import ycm_core |
|
33 |
+ |
|
34 |
+# These are the compilation flags that will be used in case there's no |
|
35 |
+# compilation database set (by default, one is not set). |
|
36 |
+# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR. |
|
37 |
+flags = [ |
|
38 |
+'-Wall', |
|
39 |
+'-Wextra', |
|
40 |
+'-Werror', |
|
41 |
+'-Wno-long-long', |
|
42 |
+'-Wno-variadic-macros', |
|
43 |
+'-fexceptions', |
|
44 |
+'-DNDEBUG', |
|
45 |
+# You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM |
|
46 |
+# source code needs it. |
|
47 |
+'-DUSE_CLANG_COMPLETER', |
|
48 |
+# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which |
|
49 |
+# language to use when compiling headers. So it will guess. Badly. So C++ |
|
50 |
+# headers will be compiled as C headers. You don't want that so ALWAYS specify |
|
51 |
+# a "-std=<something>". |
|
52 |
+# For a C project, you would set this to something like 'c99' instead of |
|
53 |
+# 'c++11'. |
|
54 |
+'-std=c++11', |
|
55 |
+# ...and the same thing goes for the magic -x option which specifies the |
|
56 |
+# language that the files to be compiled are written in. This is mostly |
|
57 |
+# relevant for c++ headers. |
|
58 |
+# For a C project, you would set this to 'c' instead of 'c++'. |
|
59 |
+'-x', |
|
60 |
+'c++', |
|
61 |
+'-isystem', |
|
62 |
+'../BoostParts', |
|
63 |
+'-isystem', |
|
64 |
+# This path will only work on OS X, but extra paths that don't exist are not |
|
65 |
+# harmful |
|
66 |
+'/System/Library/Frameworks/Python.framework/Headers', |
|
67 |
+'-isystem', |
|
68 |
+'../llvm/include', |
|
69 |
+'-isystem', |
|
70 |
+'../llvm/tools/clang/include', |
|
71 |
+'-I', |
|
72 |
+'.', |
|
73 |
+'-I', |
|
74 |
+'./ClangCompleter', |
|
75 |
+'-isystem', |
|
76 |
+'./tests/gmock/gtest', |
|
77 |
+'-isystem', |
|
78 |
+'./tests/gmock/gtest/include', |
|
79 |
+'-isystem', |
|
80 |
+'./tests/gmock', |
|
81 |
+'-isystem', |
|
82 |
+'./tests/gmock/include', |
|
83 |
+] |
|
84 |
+ |
|
85 |
+extra_flags = [ |
|
86 |
+ '-Wno-write-strings', |
|
87 |
+ '-Wno-unused-parameter', |
|
88 |
+ '-I./', |
|
89 |
+ '-I./src', |
|
90 |
+ '-I/usr/local/include/cjson' |
|
91 |
+] |
|
92 |
+ |
|
93 |
+flags += extra_flags |
|
94 |
+ |
|
95 |
+ |
|
96 |
+# Set this to the absolute path to the folder (NOT the file!) containing the |
|
97 |
+# compile_commands.json file to use that instead of 'flags'. See here for |
|
98 |
+# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html |
|
99 |
+# |
|
100 |
+# You can get CMake to generate this file for you by adding: |
|
101 |
+# set( CMAKE_EXPORT_COMPILE_COMMANDS 1 ) |
|
102 |
+# to your CMakeLists.txt file. |
|
103 |
+# |
|
104 |
+# Most projects will NOT need to set this to anything; you can just change the |
|
105 |
+# 'flags' list of compilation flags. Notice that YCM itself uses that approach. |
|
106 |
+compilation_database_folder = '' |
|
107 |
+ |
|
108 |
+if os.path.exists( compilation_database_folder ): |
|
109 |
+ database = ycm_core.CompilationDatabase( compilation_database_folder ) |
|
110 |
+else: |
|
111 |
+ database = None |
|
112 |
+ |
|
113 |
+SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] |
|
114 |
+ |
|
115 |
+def DirectoryOfThisScript(): |
|
116 |
+ return os.path.dirname( os.path.abspath( __file__ ) ) |
|
117 |
+ |
|
118 |
+ |
|
119 |
+def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): |
|
120 |
+ if not working_directory: |
|
121 |
+ return list( flags ) |
|
122 |
+ new_flags = [] |
|
123 |
+ make_next_absolute = False |
|
124 |
+ path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] |
|
125 |
+ for flag in flags: |
|
126 |
+ new_flag = flag |
|
127 |
+ |
|
128 |
+ if make_next_absolute: |
|
129 |
+ make_next_absolute = False |
|
130 |
+ if not flag.startswith( '/' ): |
|
131 |
+ new_flag = os.path.join( working_directory, flag ) |
|
132 |
+ |
|
133 |
+ for path_flag in path_flags: |
|
134 |
+ if flag == path_flag: |
|
135 |
+ make_next_absolute = True |
|
136 |
+ break |
|
137 |
+ |
|
138 |
+ if flag.startswith( path_flag ): |
|
139 |
+ path = flag[ len( path_flag ): ] |
|
140 |
+ new_flag = path_flag + os.path.join( working_directory, path ) |
|
141 |
+ break |
|
142 |
+ |
|
143 |
+ if new_flag: |
|
144 |
+ new_flags.append( new_flag ) |
|
145 |
+ return new_flags |
|
146 |
+ |
|
147 |
+ |
|
148 |
+def IsHeaderFile( filename ): |
|
149 |
+ extension = os.path.splitext( filename )[ 1 ] |
|
150 |
+ return extension in [ '.h', '.hxx', '.hpp', '.hh' ] |
|
151 |
+ |
|
152 |
+ |
|
153 |
+def GetCompilationInfoForFile( filename ): |
|
154 |
+ # The compilation_commands.json file generated by CMake does not have entries |
|
155 |
+ # for header files. So we do our best by asking the db for flags for a |
|
156 |
+ # corresponding source file, if any. If one exists, the flags for that file |
|
157 |
+ # should be good enough. |
|
158 |
+ if IsHeaderFile( filename ): |
|
159 |
+ basename = os.path.splitext( filename )[ 0 ] |
|
160 |
+ for extension in SOURCE_EXTENSIONS: |
|
161 |
+ replacement_file = basename + extension |
|
162 |
+ if os.path.exists( replacement_file ): |
|
163 |
+ compilation_info = database.GetCompilationInfoForFile( |
|
164 |
+ replacement_file ) |
|
165 |
+ if compilation_info.compiler_flags_: |
|
166 |
+ return compilation_info |
|
167 |
+ return None |
|
168 |
+ return database.GetCompilationInfoForFile( filename ) |
|
169 |
+ |
|
170 |
+ |
|
171 |
+def FlagsForFile( filename, **kwargs ): |
|
172 |
+ if database: |
|
173 |
+ # Bear in mind that compilation_info.compiler_flags_ does NOT return a |
|
174 |
+ # python list, but a "list-like" StringVec object |
|
175 |
+ compilation_info = GetCompilationInfoForFile( filename ) |
|
176 |
+ if not compilation_info: |
|
177 |
+ return None |
|
178 |
+ |
|
179 |
+ final_flags = MakeRelativePathsInFlagsAbsolute( |
|
180 |
+ compilation_info.compiler_flags_, |
|
181 |
+ compilation_info.compiler_working_dir_ ) |
|
182 |
+ |
|
183 |
+ # NOTE: This is just for YouCompleteMe; it's highly likely that your project |
|
184 |
+ # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR |
|
185 |
+ # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT. |
|
186 |
+ try: |
|
187 |
+ final_flags.remove( '-stdlib=libc++' ) |
|
188 |
+ except ValueError: |
|
189 |
+ pass |
|
190 |
+ else: |
|
191 |
+ relative_to = DirectoryOfThisScript() |
|
192 |
+ final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) |
|
193 |
+ |
|
194 |
+ return { |
|
195 |
+ 'flags': final_flags, |
|
196 |
+ 'do_cache': True |
|
197 |
+ } |