.ycm_extra_conf.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. # THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
  39. # language to use when compiling headers. So it will guess. Badly. So C++
  40. # headers will be compiled as C headers. You don't want that so ALWAYS specify
  41. # a "-std=<something>".
  42. # For a C project, you would set this to something like 'c99' instead of
  43. # 'c++11'.
  44. '-std=gnu90',
  45. # ...and the same thing goes for the magic -x option which specifies the
  46. # language that the files to be compiled are written in. This is mostly
  47. # relevant for c++ headers.
  48. # For a C project, you would set this to 'c' instead of 'c++'.
  49. '-x',
  50. 'c',
  51. '-I', './librabbitmq',
  52. '-I', './librabbitmq/unix',
  53. '-D', 'HAVE_POLL',
  54. ]
  55. # Set this to the absolute path to the folder (NOT the file!) containing the
  56. # compile_commands.json file to use that instead of 'flags'. See here for
  57. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
  58. #
  59. # You can get CMake to generate this file for you by adding:
  60. # set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
  61. # to your CMakeLists.txt file.
  62. #
  63. # Most projects will NOT need to set this to anything; you can just change the
  64. # 'flags' list of compilation flags. Notice that YCM itself uses that approach.
  65. compilation_database_folder = ''
  66. if os.path.exists( compilation_database_folder ):
  67. database = ycm_core.CompilationDatabase( compilation_database_folder )
  68. else:
  69. database = None
  70. SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
  71. def DirectoryOfThisScript():
  72. return os.path.dirname( os.path.abspath( __file__ ) )
  73. def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  74. if not working_directory:
  75. return list( flags )
  76. new_flags = []
  77. make_next_absolute = False
  78. path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  79. for flag in flags:
  80. new_flag = flag
  81. if make_next_absolute:
  82. make_next_absolute = False
  83. if not flag.startswith( '/' ):
  84. new_flag = os.path.join( working_directory, flag )
  85. for path_flag in path_flags:
  86. if flag == path_flag:
  87. make_next_absolute = True
  88. break
  89. if flag.startswith( path_flag ):
  90. path = flag[ len( path_flag ): ]
  91. new_flag = path_flag + os.path.join( working_directory, path )
  92. break
  93. if new_flag:
  94. new_flags.append( new_flag )
  95. return new_flags
  96. def IsHeaderFile( filename ):
  97. extension = os.path.splitext( filename )[ 1 ]
  98. return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
  99. def GetCompilationInfoForFile( filename ):
  100. # The compilation_commands.json file generated by CMake does not have entries
  101. # for header files. So we do our best by asking the db for flags for a
  102. # corresponding source file, if any. If one exists, the flags for that file
  103. # should be good enough.
  104. if IsHeaderFile( filename ):
  105. basename = os.path.splitext( filename )[ 0 ]
  106. for extension in SOURCE_EXTENSIONS:
  107. replacement_file = basename + extension
  108. if os.path.exists( replacement_file ):
  109. compilation_info = database.GetCompilationInfoForFile(
  110. replacement_file )
  111. if compilation_info.compiler_flags_:
  112. return compilation_info
  113. return None
  114. return database.GetCompilationInfoForFile( filename )
  115. def FlagsForFile( filename, **kwargs ):
  116. if database:
  117. # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  118. # python list, but a "list-like" StringVec object
  119. compilation_info = GetCompilationInfoForFile( filename )
  120. if not compilation_info:
  121. relative_to = DirectoryOfThisScript()
  122. return {
  123. 'flags': MakeRelativePathsInFlagsAbsolute( flags, relative_to ),
  124. 'do_cache': True
  125. }
  126. final_flags = MakeRelativePathsInFlagsAbsolute(
  127. compilation_info.compiler_flags_,
  128. compilation_info.compiler_working_dir_ )
  129. else:
  130. relative_to = DirectoryOfThisScript()
  131. final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
  132. return {
  133. 'flags': final_flags,
  134. 'do_cache': True
  135. }