Last active
May 21, 2018 17:22
-
-
Save hoxnox/ec45bca3ff1dcfcc0b74da4bc735a26a to your computer and use it in GitHub Desktop.
ycm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import ycm_core | |
import sys | |
import re | |
import glob | |
import logging | |
_logger = logging.getLogger(__name__) | |
def DirectoryOfThisScript(): | |
return os.path.dirname( os.path.abspath( __file__ ) ) | |
flags = [ | |
'-I'+DirectoryOfThisScript()+'/src', | |
'-I'+DirectoryOfThisScript()+'/include', | |
'-I'+DirectoryOfThisScript()+'/build', | |
'-I'+DirectoryOfThisScript()+'/tests', | |
'-x', 'c++' | |
] | |
conan_flags = json.loads(open('conan_ycm_flags.json', "r").read()) | |
flags.extend(conan_flags["flags"]) | |
flags.extend(conan_flags["defines"]) | |
flags.extend(conan_flags["includes"]) | |
compilation_database_folder = DirectoryOfThisScript()+'/build' | |
if os.path.exists( compilation_database_folder ): | |
database = ycm_core.CompilationDatabase( compilation_database_folder ) | |
else: | |
database = None | |
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] | |
def GetAbsolutePath(include_path, working_directory): | |
if os.path.isabs(include_path): | |
return include_path | |
return os.path.join(working_directory, include_path) | |
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): | |
if not working_directory: | |
return list( flags ) | |
new_flags = [] | |
make_next_absolute = False | |
path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] | |
for flag in flags: | |
new_flag = flag | |
if make_next_absolute: | |
make_next_absolute = False | |
new_flag = GetAbsolutePath(flag, working_directory) | |
for path_flag in path_flags: | |
if flag == path_flag: | |
make_next_absolute = True | |
break | |
if flag.startswith( path_flag ): | |
path = flag[ len( path_flag ): ] | |
new_flag = flag[:len(path_flag)] + GetAbsolutePath(path, working_directory) | |
break | |
if new_flag: | |
new_flags.append( new_flag ) | |
return new_flags | |
def IsHeaderFile( filename ): | |
extension = os.path.splitext( filename )[ 1 ] | |
return extension in [ '.h', '.hxx', '.hpp', '.hh' ] | |
def GetCompilationInfoForFile( filename ): | |
# The compilation_commands.json file generated by CMake does not have entries | |
# for header files. So we do our best by asking the db for flags for a | |
# corresponding source file, if any. If one exists, the flags for that file | |
# should be good enough. | |
if IsHeaderFile( filename ): | |
basename = os.path.splitext( filename )[ 0 ] | |
for extension in SOURCE_EXTENSIONS: | |
replacement_file = basename + extension | |
if os.path.exists( replacement_file ): | |
compilation_info = database.GetCompilationInfoForFile( | |
replacement_file ) | |
if compilation_info.compiler_flags_: | |
return compilation_info | |
# grep all source file | |
if IsHeaderFile( filename ): | |
basename = os.path.basename(filename) | |
for extension in SOURCE_EXTENSIONS: | |
for root, dirs, files in os.walk(DirectoryOfThisScript()): | |
for file in files: | |
if file.endswith(extension): | |
fname = os.path.join(root, file) | |
ifile = open(fname, "r") | |
for line in ifile: | |
if line.find(basename) != -1: | |
_logger.info("Using %s for file %s" % (fname, filename)) | |
compilation_info = database.GetCompilationInfoForFile(fname) | |
if compilation_info.compiler_flags_: | |
return compilation_info | |
return None | |
return database.GetCompilationInfoForFile( filename ) | |
def FlagsForFile( filename, **kwargs ): | |
relative_to = None | |
compiler_flags = None | |
if database: | |
# Bear in mind that compilation_info.compiler_flags_ does NOT return a | |
# python list, but a "list-like" StringVec object | |
compilation_info = GetCompilationInfoForFile( filename ) | |
if compilation_info is None: | |
relative_to = DirectoryOfThisScript() | |
compiler_flags = flags | |
else: | |
relative_to = compilation_info.compiler_working_dir_ | |
compiler_flags = compilation_info.compiler_flags_ | |
else: | |
relative_to = DirectoryOfThisScript() | |
compiler_flags = flags | |
final_flags = MakeRelativePathsInFlagsAbsolute( compiler_flags, relative_to ) | |
for flag in final_flags: | |
if flag.startswith("-W"): | |
final_flags.remove(flag) | |
_logger.info("Final flags for %s are %s" % (filename, ' '.join(final_flags))) | |
return { | |
'flags': final_flags + ["-I/usr/include", "-I/usr/include/c++/7"], | |
'do_cache': True | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment