1# See LICENSE file for license 2 3import os 4import ycm_core 5 6 7def DirectoryOfThisScript(): 8 return os.path.dirname( os.path.abspath( __file__ ) ) 9 10 11project_dir = DirectoryOfThisScript() 12 13try: 14 with open('build/_3rdParty/Hunter/install-root-dir') as f: 15 hunter_install_dir = f.readline() 16except IOError: 17 # Ignore if file does not exist 18 hunter_install_dir = '' 19 20flags = [ 21'-Wall', 22'-Werror', 23'-pedantic', 24'-std=c++11', 25'-x', 26'c++', 27'-DBOOST_COROUTINES_NO_DEPRECATION_WARNING=1', 28'-DGTEST_HAS_TR1_TUPLE=0', 29'-DGTEST_USE_OWN_TR1_TUPLE=0', 30'-isystem', 31'/usr/local/include', 32'-I', 33project_dir, 34'-I', 35os.path.join(project_dir, 'build') 36] 37 38if hunter_install_dir: 39 flags += ['-I', hunter_install_dir] 40 41compilation_database_folder = os.path.join(project_dir, 'build') 42 43if os.path.exists( compilation_database_folder ): 44 database = ycm_core.CompilationDatabase( compilation_database_folder ) 45else: 46 database = None 47 48SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] 49 50 51def IsHeaderFile( filename ): 52 extension = os.path.splitext( filename )[ 1 ] 53 return extension in [ '.h', '.hxx', '.hpp', '.hh' ] 54 55 56def GetCompilationInfoForFile( filename ): 57 # The compilation_commands.json file generated by CMake does not have entries 58 # for header files. So we do our best by asking the db for flags for a 59 # corresponding source file, if any. If one exists, the flags for that file 60 # should be good enough. 61 if IsHeaderFile( filename ): 62 basename = os.path.splitext( filename )[ 0 ] 63 for extension in SOURCE_EXTENSIONS: 64 replacement_file = basename + extension 65 if os.path.exists( replacement_file ): 66 compilation_info = database.GetCompilationInfoForFile( 67 replacement_file ) 68 if compilation_info.compiler_flags_: 69 return compilation_info 70 return None 71 return database.GetCompilationInfoForFile( filename ) 72 73 74def FlagsForFile( filename, **kwargs ): 75 if not database: 76 return { 77 'flags': flags, 78 'include_paths_relative_to_dir': DirectoryOfThisScript() 79 } 80 81 compilation_info = GetCompilationInfoForFile( filename ) 82 if not compilation_info: 83 return None 84 85 # Bear in mind that compilation_info.compiler_flags_ does NOT return a 86 # python list, but a "list-like" StringVec object. 87 final_flags = list( compilation_info.compiler_flags_ ) 88 89 return { 90 'flags': final_flags, 91 'include_paths_relative_to_dir': compilation_info.compiler_working_dir_ 92 } 93