[gnome-builder/wip/tintou/toolchain] Simplify the code, add a load method to CMake and Meson toolchains



commit 84937edc7d297b5d4b0d09108205395ebf7ad72c
Author: Corentin Noël <corentin noel collabora co uk>
Date:   Fri Apr 13 16:01:13 2018 +0100

    Simplify the code, add a load method to CMake and Meson toolchains

 src/plugins/cmake/gbp-cmake-toolchain-provider.c |  61 ++++++------
 src/plugins/cmake/gbp-cmake-toolchain.c          |  25 ++---
 src/plugins/cmake/gbp-cmake-toolchain.h          |   5 +-
 src/plugins/meson/gbp-meson-toolchain-provider.c |  12 ++-
 src/plugins/meson/gbp-meson-toolchain.c          | 113 ++++++++---------------
 src/plugins/meson/gbp-meson-toolchain.h          |   8 +-
 6 files changed, 104 insertions(+), 120 deletions(-)
---
diff --git a/src/plugins/cmake/gbp-cmake-toolchain-provider.c 
b/src/plugins/cmake/gbp-cmake-toolchain-provider.c
index 84fd069ec..a3c2222c6 100644
--- a/src/plugins/cmake/gbp-cmake-toolchain-provider.c
+++ b/src/plugins/cmake/gbp-cmake-toolchain-provider.c
@@ -52,45 +52,52 @@ gbp_cmake_toolchain_provider_load_worker (IdeTask      *task,
     {
       GFile *file = g_ptr_array_index (files, i);
       g_autofree gchar *name = NULL;
+      g_autoptr(GError) file_error = NULL;
+      g_autofree gchar *file_path = NULL;
+      g_autofree gchar *file_contents = NULL;
+      gsize file_contents_len;
 
       g_assert (G_IS_FILE (file));
 
       name = g_file_get_basename (file);
+      file_path = g_file_get_path (file);
       /* Cross-compilation files have .cmake extension, we have to blacklist CMakeSystem.cmake
        * in case we are looking into a build folder */
-      if (g_strcmp0(name, "CMakeSystem.cmake") != 0)
+      if (g_strcmp0 (name, "CMakeSystem.cmake") == 0)
+        continue;
+
+      /* Cross-compilation files should at least define CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR */
+      if (g_file_get_contents (file_path, &file_contents, &file_contents_len, &file_error))
         {
-          g_autoptr(GError) file_error = NULL;
-          g_autofree gchar *file_path = g_file_get_path (file);
-          g_autofree gchar *file_contents = NULL;
-          gsize file_contents_len;
-
-          /* Cross-compilation files should at least define CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR */
-          if (g_file_get_contents (file_path,
-                                   &file_contents, &file_contents_len, &file_error))
+          g_autoptr(GbpCMakeToolchain) toolchain = NULL;
+          g_autoptr(GError) load_error = NULL;
+          const gchar *processor_name;
+          const gchar *system_name;
+
+          system_name = g_strstr_len (file_contents,
+                                      file_contents_len,
+                                      "CMAKE_SYSTEM_NAME");
+          if (system_name == NULL)
+            continue;
+
+          processor_name = g_strstr_len (file_contents,
+                                         file_contents_len,
+                                         "CMAKE_SYSTEM_PROCESSOR");
+          if (processor_name == NULL)
+            continue;
+
+          toolchain = gbp_cmake_toolchain_new (context, file);
+          if (!gbp_cmake_toolchain_load (toolchain, file, &load_error))
             {
-              const gchar *system_name = g_strstr_len (file_contents,
-                                                       file_contents_len,
-                                                       "CMAKE_SYSTEM_NAME");
-              if (system_name != NULL)
-                {
-                  const gchar *processor_name = g_strstr_len (file_contents,
-                                                              file_contents_len,
-                                                              "CMAKE_SYSTEM_PROCESSOR");
-                  if (processor_name != NULL)
-                    {
-                      g_autoptr(GbpCMakeToolchain) toolchain = gbp_cmake_toolchain_new (context, file);
-                      if (gbp_cmake_toolchain_verify (toolchain))
-                        g_ptr_array_add (toolchains, g_steal_pointer (&toolchain));
-                    }
-                }
+              g_debug ("Error loading %s : %s", file_path, load_error->message);
+              continue;
             }
+
+          g_ptr_array_add (toolchains, g_steal_pointer (&toolchain));
         }
     }
 
-  ide_task_return_pointer (task,
-                           g_steal_pointer (&toolchains),
-                           (GDestroyNotify)g_ptr_array_unref);
+  ide_task_return_pointer (task, g_steal_pointer (&toolchains), (GDestroyNotify)g_ptr_array_unref);
 }
 
 static void
diff --git a/src/plugins/cmake/gbp-cmake-toolchain.c b/src/plugins/cmake/gbp-cmake-toolchain.c
index 5b13400c7..d253fa693 100644
--- a/src/plugins/cmake/gbp-cmake-toolchain.c
+++ b/src/plugins/cmake/gbp-cmake-toolchain.c
@@ -194,9 +194,10 @@ gbp_cmake_toolchain_get_tool_for_language (IdeToolchain  *toolchain,
 /* It is far easier and more reliable to get the variables from cmake itself,
  * Here is a small projects that exports the content of the cross-file */
 gboolean
-gbp_cmake_toolchain_verify (GbpCMakeToolchain *self)
+gbp_cmake_toolchain_load (GbpCMakeToolchain *self,
+                          GFile             *file,
+                          GError           **error)
 {
-  g_autoptr(GError) error = NULL;
   g_autofree gchar *tmp_dir = NULL;
   g_autofree gchar *toolchain_arg = NULL;
   g_autoptr(IdeSubprocessLauncher) cmake_launcher = NULL;
@@ -207,6 +208,9 @@ gbp_cmake_toolchain_verify (GbpCMakeToolchain *self)
   g_clear_object (&self->verify_cancellable);
   self->verify_cancellable = g_cancellable_new ();
 
+  g_clear_pointer (&self->file_path, g_free);
+  self->file_path = g_file_get_path (file);
+
   tmp_dir = _gbp_cmake_toolchain_deploy_temporary_cmake (self->verify_cancellable);
   toolchain_arg = g_strdup_printf ("-DCMAKE_TOOLCHAIN_FILE=%s", self->file_path);
 
@@ -215,18 +219,15 @@ gbp_cmake_toolchain_verify (GbpCMakeToolchain *self)
   ide_subprocess_launcher_push_argv (cmake_launcher, ".");
   ide_subprocess_launcher_push_argv (cmake_launcher, toolchain_arg);
   ide_subprocess_launcher_set_cwd (cmake_launcher, tmp_dir);
-  cmake_subprocess = ide_subprocess_launcher_spawn (cmake_launcher, self->verify_cancellable, &error);
-  if (!ide_subprocess_wait_check (cmake_subprocess, self->verify_cancellable, &error))
-    {
-      g_debug ("Error Testing CMake Cross-compilation file : %s", self->file_path);
-      return;
-    }
+  cmake_subprocess = ide_subprocess_launcher_spawn (cmake_launcher, self->verify_cancellable, error);
+  if (cmake_subprocess == NULL)
+    return FALSE;
+
+  if (!ide_subprocess_wait_check (cmake_subprocess, self->verify_cancellable, error))
+    return FALSE;
 
   if (!_gbp_cmake_toolchain_parse_keyfile (self, tmp_dir))
-    {
-      g_debug ("Error Testing CMake Cross-compilation file : %s", self->file_path);
-      return;
-    }
+    return FALSE;
 
   return TRUE;
 }
diff --git a/src/plugins/cmake/gbp-cmake-toolchain.h b/src/plugins/cmake/gbp-cmake-toolchain.h
index f886e4c44..4ebfb1eca 100644
--- a/src/plugins/cmake/gbp-cmake-toolchain.h
+++ b/src/plugins/cmake/gbp-cmake-toolchain.h
@@ -32,6 +32,7 @@ GbpCMakeToolchain  *gbp_cmake_toolchain_new           (IdeContext           *con
 const gchar        *gbp_cmake_toolchain_get_file_path (GbpCMakeToolchain    *self);
 void                gbp_cmake_toolchain_set_file_path (GbpCMakeToolchain    *self,
                                                        const gchar          *file_path);
-gboolean            gbp_cmake_toolchain_verify        (GbpCMakeToolchain    *self);
-
+gboolean            gbp_cmake_toolchain_load          (GbpCMakeToolchain    *self,
+                                                       GFile                *file,
+                                                       GError              **error);
 G_END_DECLS
diff --git a/src/plugins/meson/gbp-meson-toolchain-provider.c 
b/src/plugins/meson/gbp-meson-toolchain-provider.c
index 1199faf6d..f1b56cd2e 100644
--- a/src/plugins/meson/gbp-meson-toolchain-provider.c
+++ b/src/plugins/meson/gbp-meson-toolchain-provider.c
@@ -71,7 +71,7 @@ gbp_meson_toolchain_provider_load_worker (IdeTask      *task,
                                      cancellable,
                                      &file_error);
       content_type = g_file_info_get_content_type (file_info);
-      if (g_content_type_is_mime_type (content_type, "text/plain"))
+      if (!g_content_type_is_mime_type (content_type, "text/plain"))
         {
           g_autoptr(GKeyFile) keyfile = g_key_file_new ();
           g_autofree gchar *path = g_file_get_path (file);
@@ -83,7 +83,15 @@ gbp_meson_toolchain_provider_load_worker (IdeTask      *task,
                   (g_key_file_has_group (keyfile, "host_machine") ||
                    g_key_file_has_group (keyfile, "target_machine")))
                 {
-                  g_autoptr(GbpMesonToolchain) toolchain = gbp_meson_toolchain_new (context, file);
+                  g_autoptr(GError) toolchain_error = NULL;
+                  g_autoptr(GbpMesonToolchain) toolchain = gbp_meson_toolchain_new (context);
+
+                  if (!gbp_meson_toolchain_load (toolchain, file, &toolchain_error))
+                    {
+                      g_debug ("Error loading %s: %s", path, toolchain_error->message);
+                      continue;
+                    }
+
                   g_ptr_array_add (toolchains, g_steal_pointer (&toolchain));
                 }
             }
diff --git a/src/plugins/meson/gbp-meson-toolchain.c b/src/plugins/meson/gbp-meson-toolchain.c
index 27bb213e9..c5acd79ef 100644
--- a/src/plugins/meson/gbp-meson-toolchain.c
+++ b/src/plugins/meson/gbp-meson-toolchain.c
@@ -58,52 +58,53 @@ _g_key_file_get_string_quoted (GKeyFile     *key_file,
 }
 
 GbpMesonToolchain *
-gbp_meson_toolchain_new (IdeContext   *context,
-                         GFile        *file)
+gbp_meson_toolchain_new (IdeContext   *context)
+{
+  g_autoptr(GbpMesonToolchain) toolchain = NULL;
+
+  g_return_val_if_fail (IDE_IS_CONTEXT (context), NULL);
+
+  toolchain = g_object_new (GBP_TYPE_MESON_TOOLCHAIN,
+                            "context", context,
+                            NULL);
+
+
+  return g_steal_pointer (&toolchain);
+}
+
+
+gboolean
+gbp_meson_toolchain_load (GbpMesonToolchain  *self,
+                          GFile              *file,
+                          GError            **error)
 {
   g_autofree gchar *path = g_file_get_path (file);
   g_autofree gchar *id = g_strconcat ("meson:", path, NULL);
   g_autofree gchar *arch = NULL;
   g_autofree gchar *system = NULL;
   g_autoptr(GKeyFile) keyfile = g_key_file_new ();
-  g_autoptr(GError) error = NULL;
   g_autoptr(IdeTriplet) triplet = NULL;
-  g_autoptr(GError) read_error = NULL;
   g_autoptr(GError) list_error = NULL;
-  g_autofree gchar *read_result = NULL;
-  g_autoptr(GbpMesonToolchain) toolchain = NULL;
   g_auto(GStrv) binaries = NULL;
 
-  g_return_val_if_fail (IDE_IS_CONTEXT (context), NULL);
-  g_return_val_if_fail (G_IS_FILE (file), NULL);
-
-  if (!g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, &error))
-    {
-      g_warning ("Unable to read KeyFile \"%s\": %s", path, read_error->message);
-      return NULL;
-    }
+  if (!g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, error))
+    return FALSE;
 
-  arch = _g_key_file_get_string_quoted (keyfile, "host_machine", "cpu_family", &read_error);
-  if (read_error != NULL)
-    {
-      g_warning ("Unable to get the \"cpu_family`\" key of the `\"host_machine\" group: %s", 
read_error->message);
-      return NULL;
-    }
+  arch = _g_key_file_get_string_quoted (keyfile, "host_machine", "cpu_family", error);
+  if (arch == NULL)
+    return FALSE;
 
-  system = _g_key_file_get_string_quoted (keyfile, "host_machine", "system", &read_error);
-  if (read_error != NULL)
-    {
-      g_warning ("Unable to get the \"system`\" key of the `\"host_machine\" group: %s", 
read_error->message);
-      return NULL;
-    }
+  system = _g_key_file_get_string_quoted (keyfile, "host_machine", "system", error);
+  if (system == NULL)
+    return FALSE;
 
   triplet = ide_triplet_new_with_triplet (arch, system, NULL);
-  toolchain = g_object_new (GBP_TYPE_MESON_TOOLCHAIN,
-                            "context", context,
-                            "file-path", path,
-                            "id", id,
-                            "host-triplet", triplet,
-                            NULL);
+
+  g_clear_pointer (&self->file_path, g_free);
+  self->file_path = g_steal_pointer (path);
+
+  ide_toolchain_set_id (IDE_TOOLCHAIN(self), id);
+  ide_toolchain_set_host_triplet (IDE_TOOLCHAIN(self), triplet);
 
   binaries = g_key_file_get_keys (keyfile, "binaries", NULL, &list_error);
   for (int i = 0; binaries[i] != NULL; i++)
@@ -113,20 +114,20 @@ gbp_meson_toolchain_new (IdeContext   *context,
       g_autoptr(GError) key_error = NULL;
 
       if (g_strcmp0 (lang, "ar") == 0)
-        toolchain->archiver = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
+        self->archiver = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
       else if (g_strcmp0 (lang, "strip") == 0)
-        toolchain->strip = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
+        self->strip = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
       else if (g_strcmp0 (lang, "pkg_config") == 0)
-        toolchain->pkg_config = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
+        self->pkg_config = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
       else if (g_strcmp0 (lang, "exe_wrapper") == 0)
-        toolchain->exe_wrapper = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
+        self->exe_wrapper = _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error);
       else
-        g_hash_table_insert (toolchain->compilers,
+        g_hash_table_insert (self->compilers,
                              g_strdup (lang),
                              _g_key_file_get_string_quoted (keyfile, "binaries", lang, &key_error));
     }
 
-  return g_steal_pointer (&toolchain);
+  return TRUE;
 }
 
 /**
@@ -147,21 +148,6 @@ gbp_meson_toolchain_get_file_path (GbpMesonToolchain  *self)
   return self->file_path;
 }
 
-void
-gbp_meson_toolchain_set_file_path (GbpMesonToolchain  *self,
-                                   const gchar        *file_path)
-{
-  g_return_if_fail (GBP_IS_MESON_TOOLCHAIN (self));
-  g_return_if_fail (file_path != NULL);
-
-  if (g_strcmp0 (file_path, self->file_path) != 0)
-    {
-      g_clear_pointer (&self->file_path, g_free);
-      self->file_path = g_strdup (file_path);
-      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_FILE_PATH]);
-    }
-}
-
 static void
 gbp_meson_toolchain_finalize (GObject *object)
 {
@@ -190,24 +176,6 @@ gbp_meson_toolchain_get_property (GObject    *object,
     }
 }
 
-static void
-gbp_meson_toolchain_set_property (GObject      *object,
-                                  guint         prop_id,
-                                  const GValue *value,
-                                  GParamSpec   *pspec)
-{
-  GbpMesonToolchain *self = GBP_MESON_TOOLCHAIN (object);
-
-  switch (prop_id)
-    {
-    case PROP_FILE_PATH:
-      gbp_meson_toolchain_set_file_path (self, g_value_get_string (value));
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-    }
-}
-
 static void
 gbp_meson_toolchain_class_init (GbpMesonToolchainClass *klass)
 {
@@ -215,14 +183,13 @@ gbp_meson_toolchain_class_init (GbpMesonToolchainClass *klass)
 
   object_class->finalize = gbp_meson_toolchain_finalize;
   object_class->get_property = gbp_meson_toolchain_get_property;
-  object_class->set_property = gbp_meson_toolchain_set_property;
 
   properties [PROP_FILE_PATH] =
     g_param_spec_string ("file-path",
                          "File path",
                          "The path of the cross-file",
                          NULL,
-                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+                         (G_PARAM_READABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_properties (object_class, N_PROPS, properties);
 }
diff --git a/src/plugins/meson/gbp-meson-toolchain.h b/src/plugins/meson/gbp-meson-toolchain.h
index cebf10951..afd3e1de1 100644
--- a/src/plugins/meson/gbp-meson-toolchain.h
+++ b/src/plugins/meson/gbp-meson-toolchain.h
@@ -27,10 +27,10 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GbpMesonToolchain, gbp_meson_toolchain, GBP, MESON_TOOLCHAIN, IdeToolchain)
 
-GbpMesonToolchain  *gbp_meson_toolchain_new              (IdeContext             *context,
-                                                          GFile                  *file);
+GbpMesonToolchain  *gbp_meson_toolchain_new              (IdeContext             *context);
+gboolean            gbp_meson_toolchain_load             (GbpMesonToolchain      *self,
+                                                          GFile                  *file,
+                                                          GError                **error);
 const gchar        *gbp_meson_toolchain_get_file_path    (GbpMesonToolchain      *self);
-void                gbp_meson_toolchain_set_file_path    (GbpMesonToolchain      *self,
-                                                          const gchar            *file_path);
 
 G_END_DECLS


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