|
| 1 | +import os |
| 2 | +import os.path |
| 3 | +import logging |
| 4 | +import ycm_core |
| 5 | + |
| 6 | +BASE_FLAGS = [ |
| 7 | + '-xc++', |
| 8 | + '-Wall', |
| 9 | + '-Wextra', |
| 10 | + '-Werror', |
| 11 | + '-std=c++11', |
| 12 | + '-I.', |
| 13 | + '-isystem/usr/lib/', |
| 14 | + '-isystem/usr/include/', |
| 15 | +] |
| 16 | + |
| 17 | +SOURCE_EXTENSIONS = [ |
| 18 | + '.cpp', |
| 19 | + '.cxx', |
| 20 | + '.cc', |
| 21 | + '.c', |
| 22 | + '.m', |
| 23 | + '.mm' |
| 24 | +] |
| 25 | + |
| 26 | +HEADER_EXTENSIONS = [ |
| 27 | + '.h', |
| 28 | + '.hxx', |
| 29 | + '.hpp', |
| 30 | + '.hh', |
| 31 | + '.icc', |
| 32 | + '.tcc', |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +def IsHeaderFile(filename): |
| 37 | + extension = os.path.splitext(filename)[1] |
| 38 | + return extension in HEADER_EXTENSIONS |
| 39 | + |
| 40 | + |
| 41 | +def GetCompilationInfoForFile(database, filename): |
| 42 | + if IsHeaderFile(filename): |
| 43 | + basename = os.path.splitext(filename)[0] |
| 44 | + for extension in SOURCE_EXTENSIONS: |
| 45 | + replacement_file = basename + extension |
| 46 | + if os.path.exists(replacement_file): |
| 47 | + compilation_info = database.GetCompilationInfoForFile( |
| 48 | + replacement_file) |
| 49 | + if compilation_info.compiler_flags_: |
| 50 | + return compilation_info |
| 51 | + return None |
| 52 | + return database.GetCompilationInfoForFile(filename) |
| 53 | + |
| 54 | + |
| 55 | +def FindNearest(path, target): |
| 56 | + candidate = os.path.join(path, target) |
| 57 | + if(os.path.isfile(candidate) or os.path.isdir(candidate)): |
| 58 | + logging.info("Found nearest " + target + " at " + candidate) |
| 59 | + return candidate |
| 60 | + else: |
| 61 | + parent = os.path.dirname(os.path.abspath(path)) |
| 62 | + if(parent == path): |
| 63 | + raise RuntimeError("Could not find " + target) |
| 64 | + return FindNearest(parent, target) |
| 65 | + |
| 66 | + |
| 67 | +def MakeRelativePathsInFlagsAbsolute(flags, working_directory): |
| 68 | + if not working_directory: |
| 69 | + return list(flags) |
| 70 | + new_flags = [] |
| 71 | + make_next_absolute = False |
| 72 | + path_flags = ['-isystem', '-I', '-iquote', '--sysroot='] |
| 73 | + for flag in flags: |
| 74 | + new_flag = flag |
| 75 | + |
| 76 | + if make_next_absolute: |
| 77 | + make_next_absolute = False |
| 78 | + if not flag.startswith('/'): |
| 79 | + new_flag = os.path.join(working_directory, flag) |
| 80 | + |
| 81 | + for path_flag in path_flags: |
| 82 | + if flag == path_flag: |
| 83 | + make_next_absolute = True |
| 84 | + break |
| 85 | + |
| 86 | + if flag.startswith(path_flag): |
| 87 | + path = flag[len(path_flag):] |
| 88 | + new_flag = path_flag + os.path.join(working_directory, path) |
| 89 | + break |
| 90 | + |
| 91 | + if new_flag: |
| 92 | + new_flags.append(new_flag) |
| 93 | + return new_flags |
| 94 | + |
| 95 | + |
| 96 | +def FlagsForClangComplete(root): |
| 97 | + try: |
| 98 | + clang_complete_path = FindNearest(root, '.clang_complete') |
| 99 | + clang_complete_flags = open( |
| 100 | + clang_complete_path, 'r').read().splitlines() |
| 101 | + return clang_complete_flags |
| 102 | + except: |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | +def FlagsForInclude(root): |
| 107 | + try: |
| 108 | + include_path = FindNearest(root, 'include') |
| 109 | + flags = [] |
| 110 | + for dirroot, dirnames, filenames in os.walk(include_path): |
| 111 | + for dir_path in dirnames: |
| 112 | + real_path = os.path.join(dirroot, dir_path) |
| 113 | + flags = flags + ["-I" + real_path] |
| 114 | + return flags |
| 115 | + except: |
| 116 | + return None |
| 117 | + |
| 118 | + |
| 119 | +def FlagsForCompilationDatabase(root, filename): |
| 120 | + try: |
| 121 | + compilation_db_path = FindNearest( |
| 122 | + os.path.join(root, 'build'), 'compile_commands.json') |
| 123 | + compilation_db_dir = os.path.dirname(compilation_db_path) |
| 124 | + logging.info( |
| 125 | + "Set compilation database directory to " + compilation_db_dir) |
| 126 | + compilation_db = ycm_core.CompilationDatabase(compilation_db_dir) |
| 127 | + if not compilation_db: |
| 128 | + logging.info("Compilation database file found but unable to load") |
| 129 | + return None |
| 130 | + compilation_info = GetCompilationInfoForFile(compilation_db, filename) |
| 131 | + if not compilation_info: |
| 132 | + logging.info( |
| 133 | + "No compilation info for " + filename + " in compilation database") |
| 134 | + return None |
| 135 | + return MakeRelativePathsInFlagsAbsolute( |
| 136 | + compilation_info.compiler_flags_, |
| 137 | + compilation_info.compiler_working_dir_) |
| 138 | + except: |
| 139 | + return None |
| 140 | + |
| 141 | + |
| 142 | +def FlagsForFile(filename): |
| 143 | + root = os.path.realpath(filename) |
| 144 | + compilation_db_flags = FlagsForCompilationDatabase(root, filename) |
| 145 | + if compilation_db_flags: |
| 146 | + final_flags = compilation_db_flags |
| 147 | + else: |
| 148 | + final_flags = BASE_FLAGS |
| 149 | + clang_flags = FlagsForClangComplete(root) |
| 150 | + if clang_flags: |
| 151 | + final_flags = final_flags + clang_flags |
| 152 | + include_flags = FlagsForInclude(root) |
| 153 | + if include_flags: |
| 154 | + final_flags = final_flags + include_flags |
| 155 | + return { |
| 156 | + 'flags': final_flags, |
| 157 | + 'do_cache': True |
| 158 | + } |
0 commit comments