.ycm_extra_conf.py revision 289177
1# Configuration file for YouCompleteMe vim plugin to allow the plugin 2# to determine the compile flags. This file is based on: 3# https://github.com/Valloric/YouCompleteMe/blob/master/cpp/ycm/.ycm_extra_conf.py 4# 5# This is free and unencumbered software released into the public domain. 6# 7# Anyone is free to copy, modify, publish, use, compile, sell, or 8# distribute this software, either in source code form or as a compiled 9# binary, for any purpose, commercial or non-commercial, and by any 10# means. 11# 12# In jurisdictions that recognize copyright laws, the author or authors 13# of this software dedicate any and all copyright interest in the 14# software to the public domain. We make this dedication for the benefit 15# of the public at large and to the detriment of our heirs and 16# successors. We intend this dedication to be an overt act of 17# relinquishment in perpetuity of all present and future rights to this 18# software under copyright law. 19# 20# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 23# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 24# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 25# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26# OTHER DEALINGS IN THE SOFTWARE. 27# 28# For more information, please refer to <http://unlicense.org/> 29 30import os 31import ycm_core 32from clang_helpers import PrepareClangFlags 33 34compilation_database_folder = os.path.dirname(os.path.realpath(__file__)) 35 36if compilation_database_folder: 37 database = ycm_core.CompilationDatabase( compilation_database_folder ) 38else: 39 database = None 40 41def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): 42 if not working_directory: 43 return flags 44 new_flags = [] 45 make_next_absolute = False 46 path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] 47 for flag in flags: 48 new_flag = flag 49 50 if make_next_absolute: 51 make_next_absolute = False 52 if not flag.startswith( '/' ): 53 new_flag = os.path.join( working_directory, flag ) 54 55 for path_flag in path_flags: 56 if flag == path_flag: 57 make_next_absolute = True 58 break 59 60 if flag.startswith( path_flag ): 61 path = flag[ len( path_flag ): ] 62 new_flag = path_flag + os.path.join( working_directory, path ) 63 break 64 65 if new_flag: 66 new_flags.append( new_flag ) 67 return new_flags 68 69 70def FlagsForFile( filename ): 71 if database: 72 # Bear in mind that compilation_info.compiler_flags_ does NOT return a 73 # python list, but a "list-like" StringVec object 74 compilation_info = database.GetCompilationInfoForFile( filename ) 75 final_flags = PrepareClangFlags( 76 MakeRelativePathsInFlagsAbsolute( 77 compilation_info.compiler_flags_, 78 compilation_info.compiler_working_dir_ ), 79 filename ) 80 do_cache = True 81 else: 82 final_flags = [ ] 83 do_cache = False 84 85 return { 86 'flags': final_flags, 87 'do_cache': do_cache 88 } 89