[gnome-builder] compile-commands: add support for system include dirs



commit 227415a5f3c1cb88eea1518f92a5f2503b779b16
Author: Christian Hergert <chergert redhat com>
Date:   Fri Nov 24 23:17:04 2017 -0800

    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.

 src/libide/buildsystem/ide-compile-commands.c |   29 ++++++++++++++++--------
 src/libide/buildsystem/ide-compile-commands.h |    1 +
 src/plugins/cmake/gbp-cmake-build-system.c    |   27 +++++++++++++++++------
 src/plugins/meson/gbp-meson-build-system.c    |   28 +++++++++++++++++-------
 src/tests/test-ide-compile-commands.c         |    6 ++--
 5 files changed, 63 insertions(+), 28 deletions(-)
---
diff --git a/src/libide/buildsystem/ide-compile-commands.c b/src/libide/buildsystem/ide-compile-commands.c
index 105da8a..61a2543 100644
--- a/src/libide/buildsystem/ide-compile-commands.c
+++ b/src/libide/buildsystem/ide-compile-commands.c
@@ -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);
 
diff --git a/src/libide/buildsystem/ide-compile-commands.h b/src/libide/buildsystem/ide-compile-commands.h
index 1298a1a..84aee6e 100644
--- a/src/libide/buildsystem/ide-compile-commands.h
+++ b/src/libide/buildsystem/ide-compile-commands.h
@@ -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);
 
diff --git a/src/plugins/cmake/gbp-cmake-build-system.c b/src/plugins/cmake/gbp-cmake-build-system.c
index c49d750..c38107f 100644
--- a/src/plugins/cmake/gbp-cmake-build-system.c
+++ b/src/plugins/cmake/gbp-cmake-build-system.c
@@ -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
diff --git a/src/plugins/meson/gbp-meson-build-system.c b/src/plugins/meson/gbp-meson-build-system.c
index afed646..216c470 100644
--- a/src/plugins/meson/gbp-meson-build-system.c
+++ b/src/plugins/meson/gbp-meson-build-system.c
@@ -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
diff --git a/src/tests/test-ide-compile-commands.c b/src/tests/test-ide-compile-commands.c
index 86f926e..f4663eb 100644
--- a/src/tests/test-ide-compile-commands.c
+++ b/src/tests/test-ide-compile-commands.c
@@ -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");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]