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