Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import os
  2. import ycm_core
  3.  
  4. flags = [
  5. '-x',
  6. 'c',
  7. ]
  8.  
  9.  
  10. # Set this to the absolute path to the folder (NOT the file!) containing the
  11. # compile_commands.json file to use that instead of 'flags'. See here for
  12. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
  13. #
  14. # You can get CMake to generate this file for you by adding:
  15. # set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
  16. # to your CMakeLists.txt file.
  17. #
  18. # Most projects will NOT need to set this to anything; you can just change the
  19. # 'flags' list of compilation flags. Notice that YCM itself uses that approach.
  20. compilation_database_folder = ''
  21.  
  22. if os.path.exists( compilation_database_folder ):
  23. database = ycm_core.CompilationDatabase( compilation_database_folder )
  24. else:
  25. database = None
  26.  
  27. SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
  28.  
  29. def DirectoryOfThisScript():
  30. return os.path.dirname( os.path.abspath( __file__ ) )
  31.  
  32.  
  33. def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  34. if not working_directory:
  35. return list( flags )
  36. new_flags = []
  37. make_next_absolute = False
  38. path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  39. for flag in flags:
  40. new_flag = flag
  41.  
  42. if make_next_absolute:
  43. make_next_absolute = False
  44. if not flag.startswith( '/' ):
  45. new_flag = os.path.join( working_directory, flag )
  46.  
  47. for path_flag in path_flags:
  48. if flag == path_flag:
  49. make_next_absolute = True
  50. break
  51.  
  52. if flag.startswith( path_flag ):
  53. path = flag[ len( path_flag ): ]
  54. new_flag = path_flag + os.path.join( working_directory, path )
  55. break
  56.  
  57. if new_flag:
  58. new_flags.append( new_flag )
  59. return new_flags
  60.  
  61.  
  62. def IsHeaderFile( filename ):
  63. extension = os.path.splitext( filename )[ 1 ]
  64. return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
  65.  
  66.  
  67. def GetCompilationInfoForFile( filename ):
  68. # The compilation_commands.json file generated by CMake does not have entries
  69. # for header files. So we do our best by asking the db for flags for a
  70. # corresponding source file, if any. If one exists, the flags for that file
  71. # should be good enough.
  72. if IsHeaderFile( filename ):
  73. basename = os.path.splitext( filename )[ 0 ]
  74. for extension in SOURCE_EXTENSIONS:
  75. replacement_file = basename + extension
  76. if os.path.exists( replacement_file ):
  77. compilation_info = database.GetCompilationInfoForFile(
  78. replacement_file )
  79. if compilation_info.compiler_flags_:
  80. return compilation_info
  81. return None
  82. return database.GetCompilationInfoForFile( filename )
  83.  
  84.  
  85. def FlagsForFile( filename, **kwargs ):
  86. if database:
  87. # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  88. # python list, but a "list-like" StringVec object
  89. compilation_info = GetCompilationInfoForFile( filename )
  90. if not compilation_info:
  91. return None
  92.  
  93. final_flags = MakeRelativePathsInFlagsAbsolute(
  94. compilation_info.compiler_flags_,
  95. compilation_info.compiler_working_dir_ )
  96.  
  97. else:
  98. relative_to = DirectoryOfThisScript()
  99. final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
  100.  
  101. return {
  102. 'flags': final_flags,
  103. 'do_cache': True
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement