[gnome-builder/wip/tintou/toolchain] meson: make the toolchain creation a build stage



commit 3c5af3c7bcbbc76c48d08adad4caadad23b78274
Author: Corentin Noël <corentin noel collabora co uk>
Date:   Thu Apr 19 16:54:37 2018 +0100

    meson: make the toolchain creation a build stage

 .../meson/gbp-meson-build-stage-cross-file.c       | 230 +++++++++++++++++++++
 .../meson/gbp-meson-build-stage-cross-file.h       |  35 ++++
 src/plugins/meson/gbp-meson-pipeline-addin.c       |  76 +------
 src/plugins/meson/meson.build                      |   2 +
 4 files changed, 274 insertions(+), 69 deletions(-)
---
diff --git a/src/plugins/meson/gbp-meson-build-stage-cross-file.c 
b/src/plugins/meson/gbp-meson-build-stage-cross-file.c
new file mode 100644
index 000000000..8d23c73b7
--- /dev/null
+++ b/src/plugins/meson/gbp-meson-build-stage-cross-file.c
@@ -0,0 +1,230 @@
+/* gbp-meson-build-stage-cross-file.c
+ *
+ * Copyright 2018 Corentin Noël <corentin noel collabora com>
+ * Copyright 2018 Collabora Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "gbp-meson-build-stage-cross-file"
+
+#include "config.h"
+
+#include "gbp-meson-build-stage-cross-file.h"
+
+struct _GbpMesonBuildStageCrossFile
+{
+  IdeBuildStage parent_instance;
+  IdeToolchain *toolchain;
+};
+
+G_DEFINE_TYPE (GbpMesonBuildStageCrossFile, gbp_meson_build_stage_cross_file, IDE_TYPE_BUILD_STAGE)
+
+static void
+_g_key_file_set_string_quoted (GKeyFile *keyfile,
+                               const gchar *group,
+                               const gchar *key,
+                               const gchar *unquoted_value)
+{
+  g_autofree gchar *quoted_value = NULL;
+
+  g_return_if_fail (keyfile != NULL);
+  g_return_if_fail (group != NULL);
+  g_return_if_fail (key != NULL);
+  g_return_if_fail (unquoted_value != NULL);
+
+  quoted_value = g_strdup_printf ("'%s'", unquoted_value);
+  g_key_file_set_string (keyfile, group, key, quoted_value);
+}
+
+static void
+_g_key_file_set_string_array_quoted (GKeyFile *keyfile,
+                                     const gchar *group,
+                                     const gchar *key,
+                                     const gchar *unquoted_value)
+{
+  g_autofree gchar *quoted_value = NULL;
+
+  g_return_if_fail (keyfile != NULL);
+  g_return_if_fail (group != NULL);
+  g_return_if_fail (key != NULL);
+  g_return_if_fail (unquoted_value != NULL);
+
+  quoted_value = g_strdup_printf ("['%s']", unquoted_value);
+  g_key_file_set_string (keyfile, group, key, quoted_value);
+}
+
+static void
+add_lang_executable (const gchar *lang,
+                     const gchar *path,
+                     GKeyFile *keyfile)
+{
+  if (g_strcmp0 (lang, IDE_TOOLCHAIN_TOOL_CPP) == 0)
+    lang = "cpp";
+
+  _g_key_file_set_string_quoted (keyfile, "binaries", lang, path);
+}
+
+static void
+gbp_meson_build_stage_cross_file_query (IdeBuildStage    *stage,
+                                        IdeBuildPipeline *pipeline,
+                                        GCancellable     *cancellable)
+{
+  GbpMesonBuildStageCrossFile *self = (GbpMesonBuildStageCrossFile *)stage;
+  g_autofree gchar *crossbuild_file = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_MESON_BUILD_STAGE_CROSS_FILE (self));
+  g_assert (IDE_IS_BUILD_PIPELINE (pipeline));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  crossbuild_file = gbp_meson_build_stage_cross_file_get_path (self, pipeline);
+  if (!g_file_test (crossbuild_file, G_FILE_TEST_EXISTS))
+    {
+      ide_build_stage_set_completed (stage, FALSE);
+      IDE_EXIT;
+    }
+
+  ide_build_stage_set_completed (stage, TRUE);
+
+  IDE_EXIT;
+}
+
+static gboolean
+gbp_meson_build_stage_cross_file_execute (IdeBuildStage     *stage,
+                                          IdeBuildPipeline  *pipeline,
+                                          GCancellable      *cancellable,
+                                          GError           **error)
+{
+  GbpMesonBuildStageCrossFile *self = (GbpMesonBuildStageCrossFile *)stage;
+  g_autoptr(GKeyFile) crossbuild_keyfile = NULL;
+  g_autoptr(IdeTriplet) triplet = NULL;
+  g_autoptr(IdeSubprocessLauncher) env_launcher = NULL;
+  g_autofree gchar *crossbuild_file = NULL;
+  const gchar *binary_path;
+  const gchar *flags;
+  GHashTable *compilers;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_MESON_BUILD_STAGE_CROSS_FILE (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+  g_assert (IDE_IS_TOOLCHAIN (self->toolchain));
+
+  ide_build_stage_set_active (stage, TRUE);
+
+  crossbuild_keyfile = g_key_file_new ();
+  triplet = ide_toolchain_get_host_triplet (self->toolchain);
+
+  compilers  = ide_toolchain_get_tools_for_id (self->toolchain,
+                                               IDE_TOOLCHAIN_TOOL_CC);
+  g_hash_table_foreach (compilers, (GHFunc)add_lang_executable, crossbuild_keyfile);
+
+  binary_path = ide_toolchain_get_tool_for_language (self->toolchain,
+                                                     IDE_TOOLCHAIN_LANGUAGE_ANY,
+                                                     IDE_TOOLCHAIN_TOOL_AR);
+  if (binary_path != NULL)
+    _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "ar", binary_path);
+
+  binary_path = ide_toolchain_get_tool_for_language (self->toolchain,
+                                                     IDE_TOOLCHAIN_LANGUAGE_ANY,
+                                                     IDE_TOOLCHAIN_TOOL_STRIP);
+  if (binary_path != NULL)
+    _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "strip", binary_path);
+
+  binary_path = ide_toolchain_get_tool_for_language (self->toolchain,
+                                                     IDE_TOOLCHAIN_LANGUAGE_ANY,
+                                                     IDE_TOOLCHAIN_TOOL_PKG_CONFIG);
+  if (binary_path != NULL)
+    _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "pkgconfig", binary_path);
+
+  binary_path = ide_toolchain_get_tool_for_language (self->toolchain,
+                                                     IDE_TOOLCHAIN_LANGUAGE_ANY,
+                                                     IDE_TOOLCHAIN_TOOL_EXEC);
+  if (binary_path != NULL)
+    _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "exe_wrapper", binary_path);
+
+  binary_path = ide_triplet_get_kernel (triplet);
+  _g_key_file_set_string_quoted (crossbuild_keyfile, "host_machine", "system", binary_path);
+
+  binary_path = ide_triplet_get_arch (triplet);
+  _g_key_file_set_string_quoted (crossbuild_keyfile, "host_machine", "cpu_family", binary_path);
+
+  _g_key_file_set_string_quoted (crossbuild_keyfile, "host_machine", "cpu", binary_path);
+  _g_key_file_set_string_quoted (crossbuild_keyfile, "host_machine", "endian", "little");
+
+  env_launcher = ide_build_pipeline_create_launcher (pipeline, error);
+  flags = ide_subprocess_launcher_getenv (env_launcher, "CFLAGS");
+  _g_key_file_set_string_array_quoted (crossbuild_keyfile, "properties", "c_args", flags);
+  flags = ide_subprocess_launcher_getenv (env_launcher, "LDFLAGS");
+  _g_key_file_set_string_array_quoted (crossbuild_keyfile, "properties", "c_link_args", flags);
+
+  crossbuild_file = gbp_meson_build_stage_cross_file_get_path (self, pipeline);
+  if (!g_key_file_save_to_file (crossbuild_keyfile, crossbuild_file, error))
+    IDE_RETURN (FALSE);
+
+  ide_build_stage_set_active (stage, FALSE);
+
+  IDE_RETURN (TRUE);
+}
+
+static void
+ide_build_stage_mkdirs_finalize (GObject *object)
+{
+  GbpMesonBuildStageCrossFile *self = (GbpMesonBuildStageCrossFile *)object;
+
+  g_clear_object (&self->toolchain);
+
+  G_OBJECT_CLASS (gbp_meson_build_stage_cross_file_parent_class)->finalize (object);
+}
+
+static void
+gbp_meson_build_stage_cross_file_class_init (GbpMesonBuildStageCrossFileClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  IdeBuildStageClass *stage_class = IDE_BUILD_STAGE_CLASS (klass);
+
+  object_class->finalize = ide_build_stage_mkdirs_finalize;
+
+  stage_class->execute = gbp_meson_build_stage_cross_file_execute;
+  stage_class->query = gbp_meson_build_stage_cross_file_query;
+}
+
+static void
+gbp_meson_build_stage_cross_file_init (GbpMesonBuildStageCrossFile *self)
+{
+  
+}
+
+GbpMesonBuildStageCrossFile *
+gbp_meson_build_stage_cross_file_new (IdeContext    *context,
+                                      IdeToolchain  *toolchain)
+{
+  GbpMesonBuildStageCrossFile *build_stage = g_object_new (GBP_TYPE_MESON_BUILD_STAGE_CROSS_FILE,
+                                                           "context", context,
+                                                           NULL);
+  build_stage->toolchain = g_object_ref (toolchain);
+  return build_stage;
+}
+
+gchar *
+gbp_meson_build_stage_cross_file_get_path (GbpMesonBuildStageCrossFile *stage,
+                                           IdeBuildPipeline            *pipeline)
+{
+  g_return_val_if_fail (GBP_IS_MESON_BUILD_STAGE_CROSS_FILE (stage), NULL);
+  g_return_val_if_fail (IDE_IS_BUILD_PIPELINE (pipeline), NULL);
+
+  return ide_build_pipeline_build_builddir_path (pipeline, "gnome-builder-meson.crossfile", NULL);
+}
diff --git a/src/plugins/meson/gbp-meson-build-stage-cross-file.h 
b/src/plugins/meson/gbp-meson-build-stage-cross-file.h
new file mode 100644
index 000000000..4f06968eb
--- /dev/null
+++ b/src/plugins/meson/gbp-meson-build-stage-cross-file.h
@@ -0,0 +1,35 @@
+/* gbp-meson-build-stage-cross-file.h
+ *
+ * Copyright 2018 Corentin Noël <corentin noel collabora com>
+ * Copyright 2018 Collabora Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <ide.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_MESON_BUILD_STAGE_CROSS_FILE (gbp_meson_build_stage_cross_file_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpMesonBuildStageCrossFile, gbp_meson_build_stage_cross_file, GBP, 
MESON_BUILD_STAGE_CROSS_FILE, IdeBuildStage)
+
+GbpMesonBuildStageCrossFile *gbp_meson_build_stage_cross_file_new      (IdeContext                  *context,
+                                                                        IdeToolchain                
*toolchain);
+gchar                       *gbp_meson_build_stage_cross_file_get_path (GbpMesonBuildStageCrossFile *stage,
+                                                                        IdeBuildPipeline            
*pipeline);
+
+G_END_DECLS
diff --git a/src/plugins/meson/gbp-meson-pipeline-addin.c b/src/plugins/meson/gbp-meson-pipeline-addin.c
index afd37dabb..5839ac1ed 100644
--- a/src/plugins/meson/gbp-meson-pipeline-addin.c
+++ b/src/plugins/meson/gbp-meson-pipeline-addin.c
@@ -21,6 +21,7 @@
 #include <glib/gi18n.h>
 
 #include "gbp-meson-toolchain.h"
+#include "gbp-meson-build-stage-cross-file.h"
 #include "gbp-meson-build-system.h"
 #include "gbp-meson-pipeline-addin.h"
 
@@ -44,31 +45,6 @@ on_stage_query (IdeBuildStage    *stage,
   ide_build_stage_set_completed (stage, FALSE);
 }
 
-static void
-_g_key_file_set_string_quoted (GKeyFile *keyfile,
-                               const gchar *group,
-                               const gchar *key,
-                               const gchar *unquoted_value)
-{
-  g_autofree gchar *quoted_value = NULL;
-
-  g_return_if_fail (keyfile != NULL);
-  g_return_if_fail (group != NULL);
-  g_return_if_fail (key != NULL);
-  g_return_if_fail (unquoted_value != NULL);
-
-  quoted_value = g_strdup_printf ("'%s'", unquoted_value);
-  g_key_file_set_string (keyfile, group, key, quoted_value);
-}
-
-static void
-add_lang_executable (gchar *lang,
-                     gchar *path,
-                     GKeyFile *keyfile)
-{
-    _g_key_file_set_string_quoted (keyfile, "binaries", lang, path);
-}
-
 static void
 gbp_meson_pipeline_addin_load (IdeBuildPipelineAddin *addin,
                                IdeBuildPipeline      *pipeline)
@@ -146,55 +122,17 @@ gbp_meson_pipeline_addin_load (IdeBuildPipelineAddin *addin,
   if (NULL == (meson = ide_configuration_getenv (config, "MESON")))
     meson = "meson";
 
-  /* Create the toolchain file is required */
+  /* Create the toolchain file if required */
   if (GBP_IS_MESON_TOOLCHAIN (toolchain))
     crossbuild_file = g_strdup (gbp_meson_toolchain_get_file_path (GBP_MESON_TOOLCHAIN (toolchain)));
   else if (g_strcmp0 (ide_toolchain_get_id (toolchain), "default") != 0)
     {
-      g_autoptr(GKeyFile) crossbuild_keyfile = NULL;
-      g_autoptr(IdeTriplet) triplet = NULL;
-      g_autofree gchar *crossfile_name = NULL;
-      const gchar *binary_path;
-      GHashTable *compilers;
+      GbpMesonBuildStageCrossFile *cross_file_stage;
+      cross_file_stage = gbp_meson_build_stage_cross_file_new (context, toolchain);
+      crossbuild_file = gbp_meson_build_stage_cross_file_get_path (cross_file_stage, pipeline);
 
-      crossfile_name = g_strdup_printf ("gnome-builder-%s.crossfile", ide_toolchain_get_id (toolchain));
-      crossbuild_file = ide_build_pipeline_build_builddir_path (pipeline, crossfile_name, NULL);
-
-      crossbuild_keyfile = g_key_file_new ();
-      triplet = ide_toolchain_get_host_triplet (toolchain);
-
-      compilers  = ide_toolchain_get_tools_for_id (toolchain,
-                                                   IDE_TOOLCHAIN_TOOL_CC);
-      g_hash_table_foreach (compilers, (GHFunc)add_lang_executable, crossbuild_keyfile);
-
-      binary_path = ide_toolchain_get_tool_for_language (toolchain,
-                                                         IDE_TOOLCHAIN_LANGUAGE_ANY,
-                                                         IDE_TOOLCHAIN_TOOL_AR);
-      _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "ar", binary_path);
-
-      binary_path = ide_toolchain_get_tool_for_language (toolchain,
-                                                         IDE_TOOLCHAIN_LANGUAGE_ANY,
-                                                         IDE_TOOLCHAIN_TOOL_STRIP);
-      _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "strip", binary_path);
-
-      binary_path = ide_toolchain_get_tool_for_language (toolchain,
-                                                         IDE_TOOLCHAIN_LANGUAGE_ANY,
-                                                         IDE_TOOLCHAIN_TOOL_PKG_CONFIG);
-      _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "pkgconfig", binary_path);
-
-      binary_path = ide_toolchain_get_tool_for_language (toolchain,
-                                                         IDE_TOOLCHAIN_LANGUAGE_ANY,
-                                                         IDE_TOOLCHAIN_TOOL_EXEC);
-      _g_key_file_set_string_quoted (crossbuild_keyfile, "binaries", "exe_wrapper", binary_path);
-
-      binary_path = ide_triplet_get_kernel (triplet);
-      _g_key_file_set_string_quoted (crossbuild_keyfile, "host_machine", "system", binary_path);
-
-      binary_path = ide_triplet_get_arch (triplet);
-      _g_key_file_set_string_quoted (crossbuild_keyfile, "host_machine", "cpu_family", binary_path);
-
-      if (!g_key_file_save_to_file (crossbuild_keyfile, crossbuild_file, &error))
-        IDE_GOTO (failure);
+      id = ide_build_pipeline_connect (pipeline, IDE_BUILD_PHASE_PREPARE, 0, IDE_BUILD_STAGE 
(cross_file_stage));
+      ide_build_pipeline_addin_track (addin, id);
     }
 
   /* Setup our meson configure stage. */
diff --git a/src/plugins/meson/meson.build b/src/plugins/meson/meson.build
index 53ca7cd71..8337b0b67 100644
--- a/src/plugins/meson/meson.build
+++ b/src/plugins/meson/meson.build
@@ -8,6 +8,8 @@ meson_resources = gnome.compile_resources(
 
 meson_sources = [
   'meson-plugin.c',
+  'gbp-meson-build-stage-cross-file.c',
+  'gbp-meson-build-stage-cross-file.h',
   'gbp-meson-build-system.c',
   'gbp-meson-build-system.h',
   'gbp-meson-build-target.c',


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