13 '-isystem', '/usr/include',
14 '-isystem', '/usr/local/include',
17 def DirectoryOfThisScript():
18 return os
.path
.dirname(os
.path
.abspath(__file__
))
20 compilation_database_folder
= DirectoryOfThisScript() + '/build'
22 if os
.path
.exists( compilation_database_folder
):
23 database
= ycm_core
.CompilationDatabase( compilation_database_folder
)
27 SOURCE_EXTENSIONS
= [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
29 def DirectoryOfThisScript():
30 return os
.path
.dirname( os
.path
.abspath( __file__
) )
32 def MakeRelativePathsInFlagsAbsolute( flags
, working_directory
):
33 if not working_directory
:
36 make_next_absolute
= False
37 path_flags
= [ '-isystem', '-I', '-iquote', '--sysroot=' ]
41 if make_next_absolute
:
42 make_next_absolute
= False
43 if not flag
.startswith( '/' ):
44 new_flag
= os
.path
.join( working_directory
, flag
)
46 for path_flag
in path_flags
:
48 make_next_absolute
= True
51 if flag
.startswith( path_flag
):
52 path
= flag
[ len( path_flag
): ]
53 new_flag
= path_flag
+ os
.path
.join( working_directory
, path
)
57 new_flags
.append( new_flag
)
61 def IsHeaderFile( filename
):
62 extension
= os
.path
.splitext( filename
)[ 1 ]
63 return extension
in [ '.h', '.hxx', '.hpp', '.hh' ]
66 def GetCompilationInfoForFile( filename
):
67 # The compilation_commands.json file generated by CMake does not have entries
68 # for header files. So we do our best by asking the db for flags for a
69 # corresponding source file, if any. If one exists, the flags for that file
70 # should be good enough.
71 if IsHeaderFile( filename
):
72 basename
= os
.path
.splitext( filename
)[ 0 ]
73 for extension
in SOURCE_EXTENSIONS
:
74 replacement_file
= basename
+ extension
75 if os
.path
.exists( replacement_file
):
76 compilation_info
= database
.GetCompilationInfoForFile(
78 if compilation_info
.compiler_flags_
:
79 return compilation_info
81 return database
.GetCompilationInfoForFile( filename
)
84 def FlagsForFile( filename
, **kwargs
):
86 # Bear in mind that compilation_info.compiler_flags_ does NOT return a
87 # python list, but a "list-like" StringVec object
88 compilation_info
= GetCompilationInfoForFile( filename
)
89 if not compilation_info
:
92 final_flags
= MakeRelativePathsInFlagsAbsolute(
93 compilation_info
.compiler_flags_
,
94 compilation_info
.compiler_working_dir_
)
96 # NOTE: This is just for YouCompleteMe; it's highly likely that your project
97 # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
98 # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
100 final_flags
.remove( '-stdlib=libc++' )
104 relative_to
= DirectoryOfThisScript()
105 final_flags
= MakeRelativePathsInFlagsAbsolute( flags
, relative_to
)
108 'flags': final_flags
,