12 # Set this to the absolute path to the folder (NOT the file!) containing the
13 # compile_commands.json file to use that instead of 'flags'. See here for
14 # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
16 # Most projects will NOT need to set this to anything; you can just change the
17 # 'flags' list of compilation flags. Notice that YCM itself uses that approach.
18 compilation_database_folder = 'obj'
20 if os.path.exists( compilation_database_folder ):
21 database = ycm_core.CompilationDatabase( compilation_database_folder )
25 SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
27 def DirectoryOfThisScript():
28 return os.path.dirname( os.path.abspath( __file__ ) )
31 def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
32 if not working_directory:
35 make_next_absolute = False
36 path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
40 if make_next_absolute:
41 make_next_absolute = False
42 if not flag.startswith( '/' ):
43 new_flag = os.path.join( working_directory, flag )
45 for path_flag in path_flags:
47 make_next_absolute = True
50 if flag.startswith( path_flag ):
51 path = flag[ len( path_flag ): ]
52 new_flag = path_flag + os.path.join( working_directory, path )
56 new_flags.append( new_flag )
60 def IsHeaderFile( filename ):
61 extension = os.path.splitext( filename )[ 1 ]
62 return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
65 def GetCompilationInfoForFile( filename ):
66 # The compilation_commands.json file generated by CMake does not have entries
67 # for header files. So we do our best by asking the db for flags for a
68 # corresponding source file, if any. If one exists, the flags for that file
69 # should be good enough.
70 if IsHeaderFile( filename ):
71 basename = os.path.splitext( filename )[ 0 ]
72 for extension in SOURCE_EXTENSIONS:
73 replacement_file = basename + extension
74 if os.path.exists( replacement_file ):
75 compilation_info = database.GetCompilationInfoForFile(
77 if compilation_info.compiler_flags_:
78 return compilation_info
80 return database.GetCompilationInfoForFile( filename )
83 def FlagsForFile( filename, **kwargs ):
85 # Bear in mind that compilation_info.compiler_flags_ does NOT return a
86 # python list, but a "list-like" StringVec object
87 compilation_info = GetCompilationInfoForFile( filename )
88 if not compilation_info:
91 final_flags = MakeRelativePathsInFlagsAbsolute(
92 compilation_info.compiler_flags_,
93 compilation_info.compiler_working_dir_ )
95 # NOTE: This is just for YouCompleteMe; it's highly likely that your project
96 # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
97 # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
99 final_flags.remove( '-stdlib=libc++' )
103 relative_to = DirectoryOfThisScript()
104 final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
107 'flags': final_flags,