|
| 1 | +import os |
| 2 | +import ycm_core |
| 3 | + |
| 4 | +flags = [ |
| 5 | +'-Wall', |
| 6 | +'-Wextra', |
| 7 | +'-Werror', |
| 8 | +'-pedantic', |
| 9 | +'-std=c++11', |
| 10 | +'-x', |
| 11 | +'c++', |
| 12 | +] |
| 13 | + |
| 14 | +compilation_database_folder = '' |
| 15 | + |
| 16 | +if os.path.exists( compilation_database_folder ): |
| 17 | + database = ycm_core.CompilationDatabase( compilation_database_folder ) |
| 18 | +else: |
| 19 | + database = None |
| 20 | + |
| 21 | +SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] |
| 22 | + |
| 23 | +def DirectoryOfThisScript(): |
| 24 | + return os.path.dirname( os.path.abspath( __file__ ) ) |
| 25 | + |
| 26 | + |
| 27 | +def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): |
| 28 | + if not working_directory: |
| 29 | + return list( flags ) |
| 30 | + new_flags = [] |
| 31 | + make_next_absolute = False |
| 32 | + path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] |
| 33 | + for flag in flags: |
| 34 | + new_flag = flag |
| 35 | + |
| 36 | + if make_next_absolute: |
| 37 | + make_next_absolute = False |
| 38 | + if not flag.startswith( '/' ): |
| 39 | + new_flag = os.path.join( working_directory, flag ) |
| 40 | + |
| 41 | + for path_flag in path_flags: |
| 42 | + if flag == path_flag: |
| 43 | + make_next_absolute = True |
| 44 | + break |
| 45 | + |
| 46 | + if flag.startswith( path_flag ): |
| 47 | + path = flag[ len( path_flag ): ] |
| 48 | + new_flag = path_flag + os.path.join( working_directory, path ) |
| 49 | + break |
| 50 | + |
| 51 | + if new_flag: |
| 52 | + new_flags.append( new_flag ) |
| 53 | + return new_flags |
| 54 | + |
| 55 | + |
| 56 | +def IsHeaderFile( filename ): |
| 57 | + extension = os.path.splitext( filename )[ 1 ] |
| 58 | + return extension in [ '.h', '.hxx', '.hpp', '.hh' ] |
| 59 | + |
| 60 | + |
| 61 | +def GetCompilationInfoForFile( filename ): |
| 62 | + if IsHeaderFile( filename ): |
| 63 | + basename = os.path.splitext( filename )[ 0 ] |
| 64 | + for extension in SOURCE_EXTENSIONS: |
| 65 | + replacement_file = basename + extension |
| 66 | + if os.path.exists( replacement_file ): |
| 67 | + compilation_info = database.GetCompilationInfoForFile( |
| 68 | + replacement_file ) |
| 69 | + if compilation_info.compiler_flags_: |
| 70 | + return compilation_info |
| 71 | + return None |
| 72 | + return database.GetCompilationInfoForFile( filename ) |
| 73 | + |
| 74 | + |
| 75 | +def FlagsForFile( filename, **kwargs ): |
| 76 | + if database: |
| 77 | + compilation_info = GetCompilationInfoForFile( filename ) |
| 78 | + if not compilation_info: |
| 79 | + return None |
| 80 | + |
| 81 | + final_flags = MakeRelativePathsInFlagsAbsolute( |
| 82 | + compilation_info.compiler_flags_, |
| 83 | + compilation_info.compiler_working_dir_ ) |
| 84 | + |
| 85 | + else: |
| 86 | + relative_to = DirectoryOfThisScript() |
| 87 | + final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) |
| 88 | + |
| 89 | + return { |
| 90 | + 'flags': final_flags, |
| 91 | + 'do_cache': True |
| 92 | + } |
0 commit comments