1*58b9f456SAndroid Build Coastguard Workerimport os 2*58b9f456SAndroid Build Coastguard Workerimport ycm_core 3*58b9f456SAndroid Build Coastguard Worker 4*58b9f456SAndroid Build Coastguard Worker# These are the compilation flags that will be used in case there's no 5*58b9f456SAndroid Build Coastguard Worker# compilation database set (by default, one is not set). 6*58b9f456SAndroid Build Coastguard Worker# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR. 7*58b9f456SAndroid Build Coastguard Workerflags = [ 8*58b9f456SAndroid Build Coastguard Worker'-Wall', 9*58b9f456SAndroid Build Coastguard Worker'-Werror', 10*58b9f456SAndroid Build Coastguard Worker'-pedantic-errors', 11*58b9f456SAndroid Build Coastguard Worker'-std=c++0x', 12*58b9f456SAndroid Build Coastguard Worker'-fno-strict-aliasing', 13*58b9f456SAndroid Build Coastguard Worker'-O3', 14*58b9f456SAndroid Build Coastguard Worker'-DNDEBUG', 15*58b9f456SAndroid Build Coastguard Worker# ...and the same thing goes for the magic -x option which specifies the 16*58b9f456SAndroid Build Coastguard Worker# language that the files to be compiled are written in. This is mostly 17*58b9f456SAndroid Build Coastguard Worker# relevant for c++ headers. 18*58b9f456SAndroid Build Coastguard Worker# For a C project, you would set this to 'c' instead of 'c++'. 19*58b9f456SAndroid Build Coastguard Worker'-x', 'c++', 20*58b9f456SAndroid Build Coastguard Worker'-I', 'include', 21*58b9f456SAndroid Build Coastguard Worker'-isystem', '/usr/include', 22*58b9f456SAndroid Build Coastguard Worker'-isystem', '/usr/local/include', 23*58b9f456SAndroid Build Coastguard Worker] 24*58b9f456SAndroid Build Coastguard Worker 25*58b9f456SAndroid Build Coastguard Worker 26*58b9f456SAndroid Build Coastguard Worker# Set this to the absolute path to the folder (NOT the file!) containing the 27*58b9f456SAndroid Build Coastguard Worker# compile_commands.json file to use that instead of 'flags'. See here for 28*58b9f456SAndroid Build Coastguard Worker# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html 29*58b9f456SAndroid Build Coastguard Worker# 30*58b9f456SAndroid Build Coastguard Worker# Most projects will NOT need to set this to anything; you can just change the 31*58b9f456SAndroid Build Coastguard Worker# 'flags' list of compilation flags. Notice that YCM itself uses that approach. 32*58b9f456SAndroid Build Coastguard Workercompilation_database_folder = '' 33*58b9f456SAndroid Build Coastguard Worker 34*58b9f456SAndroid Build Coastguard Workerif os.path.exists( compilation_database_folder ): 35*58b9f456SAndroid Build Coastguard Worker database = ycm_core.CompilationDatabase( compilation_database_folder ) 36*58b9f456SAndroid Build Coastguard Workerelse: 37*58b9f456SAndroid Build Coastguard Worker database = None 38*58b9f456SAndroid Build Coastguard Worker 39*58b9f456SAndroid Build Coastguard WorkerSOURCE_EXTENSIONS = [ '.cc' ] 40*58b9f456SAndroid Build Coastguard Worker 41*58b9f456SAndroid Build Coastguard Workerdef DirectoryOfThisScript(): 42*58b9f456SAndroid Build Coastguard Worker return os.path.dirname( os.path.abspath( __file__ ) ) 43*58b9f456SAndroid Build Coastguard Worker 44*58b9f456SAndroid Build Coastguard Worker 45*58b9f456SAndroid Build Coastguard Workerdef MakeRelativePathsInFlagsAbsolute( flags, working_directory ): 46*58b9f456SAndroid Build Coastguard Worker if not working_directory: 47*58b9f456SAndroid Build Coastguard Worker return list( flags ) 48*58b9f456SAndroid Build Coastguard Worker new_flags = [] 49*58b9f456SAndroid Build Coastguard Worker make_next_absolute = False 50*58b9f456SAndroid Build Coastguard Worker path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] 51*58b9f456SAndroid Build Coastguard Worker for flag in flags: 52*58b9f456SAndroid Build Coastguard Worker new_flag = flag 53*58b9f456SAndroid Build Coastguard Worker 54*58b9f456SAndroid Build Coastguard Worker if make_next_absolute: 55*58b9f456SAndroid Build Coastguard Worker make_next_absolute = False 56*58b9f456SAndroid Build Coastguard Worker if not flag.startswith( '/' ): 57*58b9f456SAndroid Build Coastguard Worker new_flag = os.path.join( working_directory, flag ) 58*58b9f456SAndroid Build Coastguard Worker 59*58b9f456SAndroid Build Coastguard Worker for path_flag in path_flags: 60*58b9f456SAndroid Build Coastguard Worker if flag == path_flag: 61*58b9f456SAndroid Build Coastguard Worker make_next_absolute = True 62*58b9f456SAndroid Build Coastguard Worker break 63*58b9f456SAndroid Build Coastguard Worker 64*58b9f456SAndroid Build Coastguard Worker if flag.startswith( path_flag ): 65*58b9f456SAndroid Build Coastguard Worker path = flag[ len( path_flag ): ] 66*58b9f456SAndroid Build Coastguard Worker new_flag = path_flag + os.path.join( working_directory, path ) 67*58b9f456SAndroid Build Coastguard Worker break 68*58b9f456SAndroid Build Coastguard Worker 69*58b9f456SAndroid Build Coastguard Worker if new_flag: 70*58b9f456SAndroid Build Coastguard Worker new_flags.append( new_flag ) 71*58b9f456SAndroid Build Coastguard Worker return new_flags 72*58b9f456SAndroid Build Coastguard Worker 73*58b9f456SAndroid Build Coastguard Worker 74*58b9f456SAndroid Build Coastguard Workerdef IsHeaderFile( filename ): 75*58b9f456SAndroid Build Coastguard Worker extension = os.path.splitext( filename )[ 1 ] 76*58b9f456SAndroid Build Coastguard Worker return extension in [ '.h', '.hxx', '.hpp', '.hh' ] 77*58b9f456SAndroid Build Coastguard Worker 78*58b9f456SAndroid Build Coastguard Worker 79*58b9f456SAndroid Build Coastguard Workerdef GetCompilationInfoForFile( filename ): 80*58b9f456SAndroid Build Coastguard Worker # The compilation_commands.json file generated by CMake does not have entries 81*58b9f456SAndroid Build Coastguard Worker # for header files. So we do our best by asking the db for flags for a 82*58b9f456SAndroid Build Coastguard Worker # corresponding source file, if any. If one exists, the flags for that file 83*58b9f456SAndroid Build Coastguard Worker # should be good enough. 84*58b9f456SAndroid Build Coastguard Worker if IsHeaderFile( filename ): 85*58b9f456SAndroid Build Coastguard Worker basename = os.path.splitext( filename )[ 0 ] 86*58b9f456SAndroid Build Coastguard Worker for extension in SOURCE_EXTENSIONS: 87*58b9f456SAndroid Build Coastguard Worker replacement_file = basename + extension 88*58b9f456SAndroid Build Coastguard Worker if os.path.exists( replacement_file ): 89*58b9f456SAndroid Build Coastguard Worker compilation_info = database.GetCompilationInfoForFile( 90*58b9f456SAndroid Build Coastguard Worker replacement_file ) 91*58b9f456SAndroid Build Coastguard Worker if compilation_info.compiler_flags_: 92*58b9f456SAndroid Build Coastguard Worker return compilation_info 93*58b9f456SAndroid Build Coastguard Worker return None 94*58b9f456SAndroid Build Coastguard Worker return database.GetCompilationInfoForFile( filename ) 95*58b9f456SAndroid Build Coastguard Worker 96*58b9f456SAndroid Build Coastguard Worker 97*58b9f456SAndroid Build Coastguard Workerdef FlagsForFile( filename, **kwargs ): 98*58b9f456SAndroid Build Coastguard Worker if database: 99*58b9f456SAndroid Build Coastguard Worker # Bear in mind that compilation_info.compiler_flags_ does NOT return a 100*58b9f456SAndroid Build Coastguard Worker # python list, but a "list-like" StringVec object 101*58b9f456SAndroid Build Coastguard Worker compilation_info = GetCompilationInfoForFile( filename ) 102*58b9f456SAndroid Build Coastguard Worker if not compilation_info: 103*58b9f456SAndroid Build Coastguard Worker return None 104*58b9f456SAndroid Build Coastguard Worker 105*58b9f456SAndroid Build Coastguard Worker final_flags = MakeRelativePathsInFlagsAbsolute( 106*58b9f456SAndroid Build Coastguard Worker compilation_info.compiler_flags_, 107*58b9f456SAndroid Build Coastguard Worker compilation_info.compiler_working_dir_ ) 108*58b9f456SAndroid Build Coastguard Worker else: 109*58b9f456SAndroid Build Coastguard Worker relative_to = DirectoryOfThisScript() 110*58b9f456SAndroid Build Coastguard Worker final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) 111*58b9f456SAndroid Build Coastguard Worker 112*58b9f456SAndroid Build Coastguard Worker return { 113*58b9f456SAndroid Build Coastguard Worker 'flags': final_flags, 114*58b9f456SAndroid Build Coastguard Worker 'do_cache': True 115*58b9f456SAndroid Build Coastguard Worker } 116