2012-08-01 23:50:35 -04:00
|
|
|
import os
|
2013-01-19 21:53:30 -05:00
|
|
|
import ycm_core
|
|
|
|
from clang_helpers import PrepareClangFlags
|
2012-08-01 23:50:35 -04:00
|
|
|
|
2013-01-23 00:05:32 -05:00
|
|
|
# Set this to the absolute path to the folder (NOT the file!) containing the
|
2013-01-19 21:53:30 -05:00
|
|
|
# compilation_database.json file to use that instead of 'flags'. See here for
|
|
|
|
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
|
2013-01-23 00:05:32 -05:00
|
|
|
# Most projects will NOT need to set this to anything; you can just change the
|
|
|
|
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
|
2013-01-19 21:53:30 -05:00
|
|
|
compilation_database_folder = ''
|
|
|
|
|
|
|
|
# These are the compilation flags that will be used in case there's no
|
|
|
|
# compilation database set.
|
2012-08-01 23:50:35 -04:00
|
|
|
flags = [
|
|
|
|
'-Wall',
|
|
|
|
'-Wextra',
|
|
|
|
'-Werror',
|
|
|
|
'-Wc++98-compat',
|
|
|
|
'-Wno-long-long',
|
|
|
|
'-Wno-variadic-macros',
|
2012-08-11 22:46:22 -04:00
|
|
|
'-fexceptions',
|
2012-08-01 23:50:35 -04:00
|
|
|
'-DNDEBUG',
|
2013-01-23 20:23:51 -05:00
|
|
|
'-DUSE_CLANG_COMPLETER',
|
2012-08-05 15:55:03 -04:00
|
|
|
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
|
|
|
|
# language to use when compiling headers. So it will guess. Badly. So C++
|
|
|
|
# headers will be compiled as C headers. You don't want that so ALWAYS specify
|
2013-01-23 00:05:32 -05:00
|
|
|
# a "-std=<something>".
|
|
|
|
# For a C project, you would set this to something like 'c99' instead of
|
|
|
|
# 'c++11'.
|
2012-08-05 15:55:03 -04:00
|
|
|
'-std=c++11',
|
2012-08-11 22:46:22 -04:00
|
|
|
# ...and the same thing goes for the magic -x option which specifies the
|
|
|
|
# language that the files to be compiled are written in. This is mostly
|
|
|
|
# relevant for c++ headers.
|
2013-01-23 00:05:32 -05:00
|
|
|
# For a C project, you would set this to 'c' instead of 'c++'.
|
2012-08-11 22:46:22 -04:00
|
|
|
'-x',
|
|
|
|
'c++',
|
2012-08-01 23:50:35 -04:00
|
|
|
'-isystem',
|
|
|
|
'../BoostParts',
|
|
|
|
'-isystem',
|
|
|
|
# This path will only work on OS X, but extra paths that don't exist are not
|
|
|
|
# harmful
|
|
|
|
'/System/Library/Frameworks/Python.framework/Headers',
|
|
|
|
'-isystem',
|
|
|
|
'../llvm/include',
|
|
|
|
'-isystem',
|
|
|
|
'../llvm/tools/clang/include',
|
|
|
|
'-I',
|
|
|
|
'.',
|
|
|
|
'-isystem',
|
|
|
|
'./tests/gmock/gtest',
|
|
|
|
'-isystem',
|
|
|
|
'./tests/gmock/gtest/include',
|
|
|
|
'-isystem',
|
|
|
|
'./tests/gmock',
|
|
|
|
'-isystem',
|
|
|
|
'./tests/gmock/include'
|
|
|
|
]
|
|
|
|
|
2013-01-19 21:53:30 -05:00
|
|
|
if compilation_database_folder:
|
|
|
|
database = ycm_core.CompilationDatabase( compilation_database_folder )
|
|
|
|
else:
|
|
|
|
database = None
|
|
|
|
|
|
|
|
|
2012-08-01 23:50:35 -04:00
|
|
|
def DirectoryOfThisScript():
|
|
|
|
return os.path.dirname( os.path.abspath( __file__ ) )
|
|
|
|
|
|
|
|
|
2013-01-22 19:30:49 -05:00
|
|
|
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
|
|
|
|
if not working_directory:
|
|
|
|
return flags
|
|
|
|
new_flags = []
|
|
|
|
make_next_absolute = False
|
|
|
|
path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
|
|
|
|
for flag in flags:
|
|
|
|
to_append = flag
|
2012-08-01 23:50:35 -04:00
|
|
|
|
2013-01-22 19:30:49 -05:00
|
|
|
if make_next_absolute:
|
|
|
|
make_next_absolute = False
|
|
|
|
if not flag.startswith( '/' ):
|
|
|
|
to_append = os.path.join( working_directory, flag )
|
|
|
|
|
|
|
|
for path_flag in path_flags:
|
|
|
|
if flag == path_flag:
|
|
|
|
make_next_absolute = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if flag.startswith( path_flag ):
|
|
|
|
path = flag[ len( path_flag ): ]
|
|
|
|
to_append = path_flag + os.path.join( working_directory, path )
|
|
|
|
break
|
|
|
|
|
|
|
|
if to_append:
|
|
|
|
new_flags.append( to_append )
|
|
|
|
return new_flags
|
2012-08-01 23:50:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
def FlagsForFile( filename ):
|
2013-01-19 21:53:30 -05:00
|
|
|
if database:
|
2013-01-22 19:30:49 -05:00
|
|
|
working_directory = database.CompileCommandWorkingDirectoryForFile(
|
|
|
|
filename )
|
2013-01-23 00:05:32 -05:00
|
|
|
# Bear in mind that database.FlagsForFile does NOT return a python list, but
|
|
|
|
# a "list-like" StringVec object
|
2013-01-23 20:23:51 -05:00
|
|
|
compilation_info = database.GetCompilationInfoForFile( filename )
|
2013-01-22 19:30:49 -05:00
|
|
|
final_flags = PrepareClangFlags(
|
2013-01-23 20:23:51 -05:00
|
|
|
MakeRelativePathsInFlagsAbsolute(
|
|
|
|
compilation_info.compiler_flags_,
|
|
|
|
compilation_info.compiler_working_dir_ ),
|
2013-01-22 19:30:49 -05:00
|
|
|
filename )
|
2013-01-19 21:53:30 -05:00
|
|
|
|
|
|
|
# NOTE: This is just for YouCompleteMe; it's highly likely that your project
|
|
|
|
# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
|
|
|
|
# ycm_clang_options IF YOU'RE NOT 100% YOU NEED IT.
|
|
|
|
try:
|
|
|
|
final_flags.remove( '-stdlib=libc++' )
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
2013-01-22 19:30:49 -05:00
|
|
|
relative_to = DirectoryOfThisScript()
|
|
|
|
final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
|
2013-01-19 21:53:30 -05:00
|
|
|
|
2012-08-03 13:26:11 -04:00
|
|
|
return {
|
2013-01-19 21:53:30 -05:00
|
|
|
'flags': final_flags,
|
2012-08-03 13:26:11 -04:00
|
|
|
'do_cache': True
|
|
|
|
}
|