ycm_extra_conf.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. import os
  31. import ycm_core
  32. # These are the compilation flags that will be used in case there's no
  33. # compilation database set (by default, one is not set).
  34. # CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
  35. flags = [
  36. '-Wall',
  37. '-Wextra',
  38. '-Werror',
  39. '-Wc++14-compat',
  40. '-Wno-long-long',
  41. '-Wno-variadic-macros',
  42. '-fexceptions',
  43. '-DNDEBUG',
  44. # You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM
  45. # source code needs it.
  46. '-DUSE_CLANG_COMPLETER',
  47. # THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
  48. # language to use when compiling headers. So it will guess. Badly. So C++
  49. # headers will be compiled as C headers. You don't want that so ALWAYS specify
  50. # a "-std=<something>".
  51. # For a C project, you would set this to something like 'c99' instead of
  52. # 'c++11'.
  53. '-std=c++11',
  54. # ...and the same thing goes for the magic -x option which specifies the
  55. # language that the files to be compiled are written in. This is mostly
  56. # relevant for c++ headers.
  57. # For a C project, you would set this to 'c' instead of 'c++'.
  58. '-x',
  59. 'c++',
  60. '-isystem',
  61. '../BoostParts',
  62. '-isystem',
  63. # This path will only work on OS X, but extra paths that don't exist are not
  64. # harmful
  65. '/System/Library/Frameworks/Python.framework/Headers',
  66. # This path points to the pip installed python frameworks
  67. '-isystem',
  68. '/opt/local/Library/Frameworks/Python.framework/Headers',
  69. '-isystem',
  70. '../llvm/include',
  71. '-isystem',
  72. '../llvm/tools/clang/include',
  73. '-I',
  74. '.',
  75. '-I',
  76. './ClangCompleter',
  77. '-isystem',
  78. './tests/gmock/gtest',
  79. '-isystem',
  80. './tests/gmock/gtest/include',
  81. '-isystem',
  82. './tests/gmock',
  83. '-isystem',
  84. './tests/gmock/include',
  85. '-isystem',
  86. '/usr/include',
  87. '-isystem',
  88. '/usr/local/include',
  89. '-isystem',
  90. '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1',
  91. '-isystem',
  92. '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
  93. # This path holds the root headers, as mentioned above, it es not harmful if
  94. # the path doesn't exist
  95. '-I',
  96. '/opt/local/include/root',
  97. '-I',
  98. '/opt/ROOT/5.34.18/include/root',
  99. '-isystem',
  100. '/opt/ROOT/5.34.18/include/root',
  101. ]
  102. # Set this to the absolute path to the folder (NOT the file!) containing the
  103. # compile_commands.json file to use that instead of 'flags'. See here for
  104. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
  105. #
  106. # Most projects will NOT need to set this to anything; you can just change the
  107. # 'flags' list of compilation flags. Notice that YCM itself uses that approach.
  108. compilation_database_folder = ''
  109. if os.path.exists( compilation_database_folder ):
  110. database = ycm_core.CompilationDatabase( compilation_database_folder )
  111. else:
  112. database = None
  113. SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
  114. def DirectoryOfThisScript():
  115. return os.path.dirname( os.path.abspath( __file__ ) )
  116. def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  117. if not working_directory:
  118. return list( flags )
  119. new_flags = []
  120. make_next_absolute = False
  121. path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  122. for flag in flags:
  123. new_flag = flag
  124. if make_next_absolute:
  125. make_next_absolute = False
  126. if not flag.startswith( '/' ):
  127. new_flag = os.path.join( working_directory, flag )
  128. for path_flag in path_flags:
  129. if flag == path_flag:
  130. make_next_absolute = True
  131. break
  132. if flag.startswith( path_flag ):
  133. path = flag[ len( path_flag ): ]
  134. new_flag = path_flag + os.path.join( working_directory, path )
  135. break
  136. if new_flag:
  137. new_flags.append( new_flag )
  138. return new_flags
  139. def IsHeaderFile( filename ):
  140. extension = os.path.splitext( filename )[ 1 ]
  141. return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
  142. def GetCompilationInfoForFile( filename ):
  143. # The compilation_commands.json file generated by CMake does not have entries
  144. # for header files. So we do our best by asking the db for flags for a
  145. # corresponding source file, if any. If one exists, the flags for that file
  146. # should be good enough.
  147. if IsHeaderFile( filename ):
  148. basename = os.path.splitext( filename )[ 0 ]
  149. for extension in SOURCE_EXTENSIONS:
  150. replacement_file = basename + extension
  151. if os.path.exists( replacement_file ):
  152. compilation_info = database.GetCompilationInfoForFile(
  153. replacement_file )
  154. if compilation_info.compiler_flags_:
  155. return compilation_info
  156. return None
  157. return database.GetCompilationInfoForFile( filename )
  158. def FlagsForFile( filename, **kwargs ):
  159. if database:
  160. # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  161. # python list, but a "list-like" StringVec object
  162. compilation_info = GetCompilationInfoForFile( filename )
  163. if not compilation_info:
  164. return None
  165. final_flags = MakeRelativePathsInFlagsAbsolute(
  166. compilation_info.compiler_flags_,
  167. compilation_info.compiler_working_dir_ )
  168. # NOTE: This is just for YouCompleteMe; it's highly likely that your project
  169. # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
  170. # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
  171. try:
  172. final_flags.remove( '-stdlib=libc++' )
  173. except ValueError:
  174. pass
  175. else:
  176. relative_to = DirectoryOfThisScript()
  177. final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
  178. return {
  179. 'flags': final_flags,
  180. 'do_cache': True
  181. }