irenicus09

ycm_extra_conf.py

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