Commit 227415a5 authored by Christian Hergert's avatar Christian Hergert
Browse files

compile-commands: add support for system include dirs

This API allows consumers to pass a series of system include dirs
and have them added to the compile commands. For example, you might
want to have /app/include for Flatpak runtimes.
parent c5c5dada
......@@ -450,9 +450,10 @@ ide_compile_commands_resolve (IdeCompileCommands *self,
static void
ide_compile_commands_filter_c (IdeCompileCommands *self,
const CompileInfo *info,
const gchar * const *system_includes,
gchar ***argv)
{
GPtrArray *ar;
g_autoptr(GPtrArray) ar = NULL;
g_assert (IDE_IS_COMPILE_COMMANDS (self));
g_assert (info != NULL);
......@@ -461,7 +462,13 @@ ide_compile_commands_filter_c (IdeCompileCommands *self,
if (*argv == NULL)
return;
ar = g_ptr_array_new ();
ar = g_ptr_array_new_with_free_func (g_free);
if (system_includes != NULL)
{
for (guint i = 0; system_includes[i]; i++)
g_ptr_array_add (ar, g_strdup_printf ("-I%s", system_includes[i]));
}
for (guint i = 0; (*argv)[i] != NULL; i++)
{
......@@ -513,10 +520,10 @@ ide_compile_commands_filter_c (IdeCompileCommands *self,
}
}
g_free (*argv);
g_ptr_array_add (ar, NULL);
*argv = (gchar **)g_ptr_array_free (ar, FALSE);
g_strfreev (*argv);
*argv = (gchar **)g_ptr_array_free (g_steal_pointer (&ar), FALSE);
}
static void
......@@ -588,6 +595,7 @@ ide_compile_commands_filter_vala (IdeCompileCommands *self,
* ide_compile_commands_lookup:
* @self: An #IdeCompileCommands
* @file: a #GFile representing the file to lookup
* @system_includes: system include dirs if any
* @directory: (out) (optional) (transfer full): A location for a #GFile, or %NULL
* @error: A location for a #GError, or %NULL
*
......@@ -602,10 +610,11 @@ ide_compile_commands_filter_vala (IdeCompileCommands *self,
* Since: 3.28
*/
gchar **
ide_compile_commands_lookup (IdeCompileCommands *self,
GFile *file,
GFile **directory,
GError **error)
ide_compile_commands_lookup (IdeCompileCommands *self,
GFile *file,
const gchar * const *system_includes,
GFile **directory,
GError **error)
{
g_autofree gchar *base = NULL;
const CompileInfo *info;
......@@ -627,7 +636,7 @@ ide_compile_commands_lookup (IdeCompileCommands *self,
return NULL;
if (suffix_is_c_like (dot))
ide_compile_commands_filter_c (self, info, &argv);
ide_compile_commands_filter_c (self, info, system_includes, &argv);
else if (suffix_is_vala (dot))
ide_compile_commands_filter_vala (self, info, &argv);
......
......@@ -48,6 +48,7 @@ gboolean ide_compile_commands_load_finish (IdeCompileCommands *sel
IDE_AVAILABLE_IN_3_28
gchar **ide_compile_commands_lookup (IdeCompileCommands *self,
GFile *file,
const gchar * const *system_includes,
GFile **directory,
GError **error);
......
......@@ -437,7 +437,12 @@ gbp_cmake_build_system_get_build_flags_cb (GObject *object,
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
g_autoptr(GFile) directory = NULL;
g_auto(GStrv) system_includes = NULL;
g_auto(GStrv) build_flags = NULL;
IdeConfigurationManager *config_manager;
IdeContext *context;
IdeConfiguration *config;
IdeRuntime *runtime;
GFile *file;
g_assert (GBP_IS_CMAKE_BUILD_SYSTEM (self));
......@@ -455,15 +460,23 @@ gbp_cmake_build_system_get_build_flags_cb (GObject *object,
file = g_task_get_task_data (task);
g_assert (G_IS_FILE (file));
build_flags = ide_compile_commands_lookup (compile_commands, file, &directory, &error);
/* Get non-standard system includes */
context = ide_object_get_context (IDE_OBJECT (self));
config_manager = ide_context_get_configuration_manager (context);
config = ide_configuration_manager_get_current (config_manager);
if (NULL != (runtime = ide_configuration_get_runtime (config)))
system_includes = ide_runtime_get_system_include_dirs (runtime);
if (build_flags == NULL)
{
g_task_return_error (task, g_steal_pointer (&error));
return;
}
build_flags = ide_compile_commands_lookup (compile_commands,
file,
(const gchar * const *)system_includes,
&directory,
&error);
g_task_return_pointer (task, g_steal_pointer (&build_flags), (GDestroyNotify)g_strfreev);
if (build_flags == NULL)
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_pointer (task, g_steal_pointer (&build_flags), (GDestroyNotify)g_strfreev);
}
static void
......
......@@ -434,7 +434,12 @@ gbp_meson_build_system_get_build_flags_cb (GObject *object,
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
g_autoptr(GFile) directory = NULL;
g_auto(GStrv) system_includes = NULL;
g_auto(GStrv) ret = NULL;
IdeConfigurationManager *config_manager;
IdeContext *context;
IdeConfiguration *config;
IdeRuntime *runtime;
GFile *file;
g_assert (GBP_IS_MESON_BUILD_SYSTEM (self));
......@@ -450,18 +455,25 @@ gbp_meson_build_system_get_build_flags_cb (GObject *object,
}
file = g_task_get_task_data (task);
g_assert (G_IS_FILE (file));
ret = ide_compile_commands_lookup (compile_commands, file, &directory, &error);
/* Get non-standard system includes */
context = ide_object_get_context (IDE_OBJECT (self));
config_manager = ide_context_get_configuration_manager (context);
config = ide_configuration_manager_get_current (config_manager);
if (NULL != (runtime = ide_configuration_get_runtime (config)))
system_includes = ide_runtime_get_system_include_dirs (runtime);
if (ret == NULL)
{
g_task_return_error (task, g_steal_pointer (&error));
return;
}
ret = ide_compile_commands_lookup (compile_commands,
file,
(const gchar * const *)system_includes,
&directory,
&error);
g_task_return_pointer (task, g_steal_pointer (&ret), (GDestroyNotify)g_strfreev);
if (ret == NULL)
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_pointer (task, g_steal_pointer (&ret), (GDestroyNotify)g_strfreev);
}
static void
......
......@@ -37,7 +37,7 @@ test_compile_commands_basic (void)
commands = ide_compile_commands_new ();
/* Test missing info before we've loaded */
g_assert (NULL == ide_compile_commands_lookup (commands, missing, NULL, NULL));
g_assert (NULL == ide_compile_commands_lookup (commands, missing, NULL, NULL, NULL));
/* Now load our test file */
data_path = g_build_filename (TEST_DATA_DIR, "test-ide-compile-commands.json", NULL);
......@@ -48,7 +48,7 @@ test_compile_commands_basic (void)
/* Now lookup a file that should exist in the database */
expected_file = g_file_new_for_path ("/build/gnome-builder/subprojects/libgd/libgd/gd-types-catalog.c");
cmdstrv = ide_compile_commands_lookup (commands, expected_file, &dir, &error);
cmdstrv = ide_compile_commands_lookup (commands, expected_file, NULL, &dir, &error);
g_assert_no_error (error);
g_assert (cmdstrv != NULL);
/* ccache cc should have been removed. */
......@@ -59,7 +59,7 @@ test_compile_commands_basic (void)
/* Vala files don't need to match on exact filename, just something dot vala */
vala = g_file_new_for_path ("whatever.vala");
valastrv = ide_compile_commands_lookup (commands, vala, NULL, &error);
valastrv = ide_compile_commands_lookup (commands, vala, NULL, NULL, &error);
g_assert_no_error (error);
g_assert (valastrv != NULL);
g_assert_cmpstr (valastrv[0], ==, "--pkg");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment