+ parent = os.path.dirname(os.path.abspath(root))
+ if parent == root:
+ raise RuntimeError("Could not find compile_commands.json")
+ return find_nearest_compilation_database(parent)
+
+
+def find_nearest(path, target):
+ candidates = [
+ os.path.join(path, target),
+ os.path.join(path, 'build', target),
+ os.path.join(path, 'output', target),
+ ]
+ for candidate in candidates:
+ if os.path.isfile(candidate) or os.path.isdir(candidate):
+ logging.info("Found nearest " + target + " at " + candidate)
+ return candidate
+ parent = os.path.dirname(os.path.abspath(path))
+ if parent == path:
+ raise RuntimeError("Could not find " + target)
+ return find_nearest(parent, target)
+
+
+def flags_for_include(root):
+ try:
+ include_path = find_nearest(root, 'include')
+ flags = []
+ for dirroot, dirnames, filenames in os.walk(include_path):
+ for dir_path in dirnames:
+ real_path = os.path.join(dirroot, dir_path)
+ flags = flags + ["-I" + real_path]
+ return flags
+ except Exception as err:
+ logging.info("Error while looking flags for includes in root: " + root)
+ logging.error(err)
+ return None
+
+
+def get_compilation_database(root):
+ try:
+ compilation_db_path = find_nearest_compilation_database(root)
+ compilation_db_dir = os.path.dirname(compilation_db_path)
+ logging.info("Set compilation database directory to " + compilation_db_dir)
+ db = ycm_core.CompilationDatabase(compilation_db_dir)
+ if db is None:
+ logging.info("Compilation database file found but unable to load")
+ return None
+ return db
+ except Exception as err:
+ logging.info("Error while trying to find compilation database: " + root)
+ logging.error(err)
+ return None
+
+
+def Settings(**kwargs):
+ if kwargs['language'] != 'cfamily':
+ return {}
+
+ print(kwargs)
+ client_data = kwargs['client_data']
+ root = client_data.get('getcwd()', '.')
+ filename = kwargs['filename']
+
+ database = get_compilation_database(root)
+ if database:
+ filename = find_similar_file_in_database(database.database_directory,
+ filename)
+ compilation_info = database.GetCompilationInfoForFile(filename)
+ print(compilation_info)
+ if not compilation_info.compiler_flags_:
+ return {} #TODO use default flags
+ final_flags = list(compilation_info.compiler_flags_)
+ include_path_relative_to_dir = compilation_info.compiler_working_dir_
+ else:
+ final_flags = BASE_FLAGS
+ include_flags = flags_for_include(root)
+ if include_flags:
+ final_flags += include_flags
+ final_flags += generate_qt_flags()
+ final_flags += ['-I', root,
+ '-I', root + '/include']
+ include_path_relative_to_dir = root