|
| 1 | +import os |
| 2 | +import ycm_core |
| 3 | + |
| 4 | +from clang_helpers import PrepareClangFlags |
| 5 | + |
| 6 | +def DirectoryOfThisScript(): |
| 7 | + return os.path.dirname(os.path.abspath(__file__)) |
| 8 | + |
| 9 | +# This is the single most important line in this script. Everything else is just nice to have but |
| 10 | +# not strictly necessary. |
| 11 | +compilation_database_folder = DirectoryOfThisScript() + "/debug" |
| 12 | + |
| 13 | +# This provides a safe fall-back if no compilation commands are available. You could also add a |
| 14 | +# includes relative to your project directory, for example. |
| 15 | +flags = [ |
| 16 | + '-Wall', |
| 17 | + '-std=c++11', |
| 18 | + '-stdlib=libc++', |
| 19 | + '-x', |
| 20 | + 'c++', |
| 21 | + '-I', |
| 22 | + '.', |
| 23 | + '-isystem', '/usr/local/include', |
| 24 | + '-isystem', '/usr/include', |
| 25 | + '-I.', |
| 26 | +] |
| 27 | + |
| 28 | +if compilation_database_folder: |
| 29 | + database = ycm_core.CompilationDatabase(compilation_database_folder) |
| 30 | +else: |
| 31 | + database = None |
| 32 | + |
| 33 | +SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] |
| 34 | + |
| 35 | +def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): |
| 36 | + if not working_directory: |
| 37 | + return list( flags ) |
| 38 | + new_flags = [] |
| 39 | + make_next_absolute = False |
| 40 | + path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] |
| 41 | + for flag in flags: |
| 42 | + new_flag = flag |
| 43 | + |
| 44 | + if make_next_absolute: |
| 45 | + make_next_absolute = False |
| 46 | + if not flag.startswith( '/' ): |
| 47 | + new_flag = os.path.join( working_directory, flag ) |
| 48 | + |
| 49 | + for path_flag in path_flags: |
| 50 | + if flag == path_flag: |
| 51 | + make_next_absolute = True |
| 52 | + break |
| 53 | + |
| 54 | + if flag.startswith( path_flag ): |
| 55 | + path = flag[ len( path_flag ): ] |
| 56 | + new_flag = path_flag + os.path.join( working_directory, path ) |
| 57 | + break |
| 58 | + |
| 59 | + if new_flag: |
| 60 | + new_flags.append( new_flag ) |
| 61 | + return new_flags |
| 62 | + |
| 63 | + |
| 64 | +def IsHeaderFile( filename ): |
| 65 | + extension = os.path.splitext( filename )[ 1 ] |
| 66 | + return extension in [ '.h', '.hxx', '.hpp', '.hh' ] |
| 67 | + |
| 68 | + |
| 69 | +def GetCompilationInfoForFile( filename ): |
| 70 | + # The compilation_commands.json file generated by CMake does not have entries |
| 71 | + # for header files. So we do our best by asking the db for flags for a |
| 72 | + # corresponding source file, if any. If one exists, the flags for that file |
| 73 | + # should be good enough. |
| 74 | + if IsHeaderFile( filename ): |
| 75 | + basename = os.path.splitext( filename )[ 0 ] |
| 76 | + for extension in SOURCE_EXTENSIONS: |
| 77 | + replacement_file = basename + extension |
| 78 | + if os.path.exists( replacement_file ): |
| 79 | + compilation_info = database.GetCompilationInfoForFile( |
| 80 | + replacement_file ) |
| 81 | + if compilation_info.compiler_flags_: |
| 82 | + return compilation_info |
| 83 | + return None |
| 84 | + return database.GetCompilationInfoForFile( filename ) |
| 85 | + |
| 86 | + |
| 87 | +def FlagsForFile( filename, **kwargs ): |
| 88 | + if database: |
| 89 | + # Bear in mind that compilation_info.compiler_flags_ does NOT return a |
| 90 | + # python list, but a "list-like" StringVec object |
| 91 | + compilation_info = GetCompilationInfoForFile( filename ) |
| 92 | + if not compilation_info: |
| 93 | + return None |
| 94 | + |
| 95 | + final_flags = MakeRelativePathsInFlagsAbsolute( |
| 96 | + compilation_info.compiler_flags_, |
| 97 | + compilation_info.compiler_working_dir_ ) |
| 98 | + |
| 99 | + else: |
| 100 | + relative_to = DirectoryOfThisScript() |
| 101 | + final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) |
| 102 | + |
| 103 | + return { |
| 104 | + 'flags': final_flags, |
| 105 | + 'do_cache': True |
| 106 | + } |
0 commit comments