|
| 1 | +# Generated by YCM Generator at 2016-12-09 15:35:30.893900 |
| 2 | + |
| 3 | +# This file is NOT licensed under the GPLv3, which is the license for the rest |
| 4 | +# of YouCompleteMe. |
| 5 | +# |
| 6 | +# Here's the license text for this file: |
| 7 | +# |
| 8 | +# This is free and unencumbered software released into the public domain. |
| 9 | +# |
| 10 | +# Anyone is free to copy, modify, publish, use, compile, sell, or |
| 11 | +# distribute this software, either in source code form or as a compiled |
| 12 | +# binary, for any purpose, commercial or non-commercial, and by any |
| 13 | +# means. |
| 14 | +# |
| 15 | +# In jurisdictions that recognize copyright laws, the author or authors |
| 16 | +# of this software dedicate any and all copyright interest in the |
| 17 | +# software to the public domain. We make this dedication for the benefit |
| 18 | +# of the public at large and to the detriment of our heirs and |
| 19 | +# successors. We intend this dedication to be an overt act of |
| 20 | +# relinquishment in perpetuity of all present and future rights to this |
| 21 | +# software under copyright law. |
| 22 | +# |
| 23 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 26 | +# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 27 | +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 28 | +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 29 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 30 | +# |
| 31 | +# For more information, please refer to <http://unlicense.org/> |
| 32 | + |
| 33 | +import os |
| 34 | +import ycm_core |
| 35 | + |
| 36 | +flags = [ |
| 37 | + '-x', |
| 38 | + 'c++', |
| 39 | + '-DBY_THREAD', |
| 40 | + '-DCPPTEST', |
| 41 | + '-Wall', |
| 42 | + '-std=c++14', |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +# Set this to the absolute path to the folder (NOT the file!) containing the |
| 47 | +# compile_commands.json file to use that instead of 'flags'. See here for |
| 48 | +# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html |
| 49 | +# |
| 50 | +# You can get CMake to generate this file for you by adding: |
| 51 | +# set( CMAKE_EXPORT_COMPILE_COMMANDS 1 ) |
| 52 | +# to your CMakeLists.txt file. |
| 53 | +# |
| 54 | +# Most projects will NOT need to set this to anything; you can just change the |
| 55 | +# 'flags' list of compilation flags. Notice that YCM itself uses that approach. |
| 56 | +compilation_database_folder = '' |
| 57 | + |
| 58 | +if os.path.exists( compilation_database_folder ): |
| 59 | + database = ycm_core.CompilationDatabase( compilation_database_folder ) |
| 60 | +else: |
| 61 | + database = None |
| 62 | + |
| 63 | +SOURCE_EXTENSIONS = [ '.C', '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] |
| 64 | + |
| 65 | +def DirectoryOfThisScript(): |
| 66 | + return os.path.dirname( os.path.abspath( __file__ ) ) |
| 67 | + |
| 68 | + |
| 69 | +def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): |
| 70 | + if not working_directory: |
| 71 | + return list( flags ) |
| 72 | + new_flags = [] |
| 73 | + make_next_absolute = False |
| 74 | + path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] |
| 75 | + for flag in flags: |
| 76 | + new_flag = flag |
| 77 | + |
| 78 | + if make_next_absolute: |
| 79 | + make_next_absolute = False |
| 80 | + if not flag.startswith( '/' ): |
| 81 | + new_flag = os.path.join( working_directory, flag ) |
| 82 | + |
| 83 | + for path_flag in path_flags: |
| 84 | + if flag == path_flag: |
| 85 | + make_next_absolute = True |
| 86 | + break |
| 87 | + |
| 88 | + if flag.startswith( path_flag ): |
| 89 | + path = flag[ len( path_flag ): ] |
| 90 | + new_flag = path_flag + os.path.join( working_directory, path ) |
| 91 | + break |
| 92 | + |
| 93 | + if new_flag: |
| 94 | + new_flags.append( new_flag ) |
| 95 | + return new_flags |
| 96 | + |
| 97 | + |
| 98 | +def IsHeaderFile( filename ): |
| 99 | + extension = os.path.splitext( filename )[ 1 ] |
| 100 | + return extension in [ '.H', '.h', '.hxx', '.hpp', '.hh' ] |
| 101 | + |
| 102 | + |
| 103 | +def GetCompilationInfoForFile( filename ): |
| 104 | + # The compilation_commands.json file generated by CMake does not have entries |
| 105 | + # for header files. So we do our best by asking the db for flags for a |
| 106 | + # corresponding source file, if any. If one exists, the flags for that file |
| 107 | + # should be good enough. |
| 108 | + if IsHeaderFile( filename ): |
| 109 | + basename = os.path.splitext( filename )[ 0 ] |
| 110 | + for extension in SOURCE_EXTENSIONS: |
| 111 | + replacement_file = basename + extension |
| 112 | + if os.path.exists( replacement_file ): |
| 113 | + compilation_info = database.GetCompilationInfoForFile( |
| 114 | + replacement_file ) |
| 115 | + if compilation_info.compiler_flags_: |
| 116 | + return compilation_info |
| 117 | + return None |
| 118 | + return database.GetCompilationInfoForFile( filename ) |
| 119 | + |
| 120 | + |
| 121 | +def FlagsForFile( filename, **kwargs ): |
| 122 | + if database: |
| 123 | + # Bear in mind that compilation_info.compiler_flags_ does NOT return a |
| 124 | + # python list, but a "list-like" StringVec object |
| 125 | + compilation_info = GetCompilationInfoForFile( filename ) |
| 126 | + if not compilation_info: |
| 127 | + return None |
| 128 | + |
| 129 | + final_flags = MakeRelativePathsInFlagsAbsolute( |
| 130 | + compilation_info.compiler_flags_, |
| 131 | + compilation_info.compiler_working_dir_ ) |
| 132 | + |
| 133 | + else: |
| 134 | + relative_to = DirectoryOfThisScript() |
| 135 | + final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) |
| 136 | + |
| 137 | + return { |
| 138 | + 'flags': final_flags, |
| 139 | + 'do_cache': True |
| 140 | + } |
| 141 | + |
0 commit comments