[gnome-builder/wip/gtk4-port: 1597/1774] libide/foundry: use flatpak-spawn for host execution




commit 76570d580e268f9e48377797c7acc01bd61c9902
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jun 22 11:56:51 2022 -0700

    libide/foundry: use flatpak-spawn for host execution
    
    Rather than using our IdeSubprocessLauncher with a "run-on-host", we can
    use "flatpak-spawn" as another layer to the run context.
    
    There may be an opportunity to completely remove IdeSubprocessLauncher by
    converting API usage to IdeRunContext if we add a spawn API.

 src/libide/foundry/ide-run-context.c      | 97 +++++++++++++++++++++++++------
 src/libide/foundry/ide-run-context.h      |  5 +-
 src/plugins/flatpak/gbp-flatpak-runtime.c |  6 +-
 3 files changed, 83 insertions(+), 25 deletions(-)
---
diff --git a/src/libide/foundry/ide-run-context.c b/src/libide/foundry/ide-run-context.c
index 26e9b0228..5d52c20fd 100644
--- a/src/libide/foundry/ide-run-context.c
+++ b/src/libide/foundry/ide-run-context.c
@@ -48,7 +48,6 @@ struct _IdeRunContext
   GQueue             layers;
   IdeRunContextLayer root;
   guint              ended : 1;
-  guint              run_on_host : 1;
 };
 
 G_DEFINE_FINAL_TYPE (IdeRunContext, ide_run_context, G_TYPE_OBJECT)
@@ -169,6 +168,84 @@ ide_run_context_push (IdeRunContext        *self,
   g_queue_push_head_link (&self->layers, &layer->qlink);
 }
 
+static gboolean
+ide_run_context_host_handler (IdeRunContext       *self,
+                              const char * const  *argv,
+                              const char * const  *env,
+                              const char          *cwd,
+                              IdeUnixFDMap        *unix_fd_map,
+                              gpointer             user_data,
+                              GError             **error)
+{
+  guint length;
+
+  g_assert (IDE_IS_RUN_CONTEXT (self));
+  g_assert (argv != NULL);
+  g_assert (env != NULL);
+  g_assert (IDE_IS_UNIX_FD_MAP (unix_fd_map));
+  g_assert (ide_is_flatpak ());
+
+  ide_run_context_append_argv (self, "flatpak-spawn");
+  ide_run_context_append_argv (self, "--host");
+  ide_run_context_append_argv (self, "--clear-env");
+  ide_run_context_append_argv (self, "--share-pids");
+  ide_run_context_append_argv (self, "--watch-bus");
+
+  if (env != NULL)
+    {
+      for (guint i = 0; env[i]; i++)
+        ide_run_context_append_formatted (self, "--env=%s", env[i]);
+    }
+
+  if (cwd != NULL)
+    ide_run_context_append_formatted (self, "--directory=%s", cwd);
+
+  if ((length = ide_unix_fd_map_get_length (unix_fd_map)))
+    {
+      if (!ide_run_context_merge_unix_fd_map (self, unix_fd_map, error))
+        return FALSE;
+
+      for (guint i = 0; i < length; i++)
+        {
+          int source_fd;
+          int dest_fd;
+
+          source_fd = ide_unix_fd_map_peek (unix_fd_map, i, &dest_fd);
+
+          if (source_fd != -1 && dest_fd != -1)
+            ide_run_context_append_formatted (self, "--forward-fd=%d", dest_fd);
+        }
+    }
+
+  /* Now append the arguments */
+  ide_run_context_append_args (self, argv);
+
+  return TRUE;
+}
+
+/**
+ * ide_run_context_push_host:
+ * @self: a #IdeRunContext
+ *
+ * Pushes handler to transform command to run on host.
+ *
+ * If necessary, a layer is pushed to ensure the command is run on the
+ * host instead of the application container.
+ *
+ * If Builder is running on the host already, this function does nothing.
+ */
+void
+ide_run_context_push_host (IdeRunContext *self)
+{
+  g_return_if_fail (IDE_IS_RUN_CONTEXT (self));
+
+  if (ide_is_flatpak ())
+    ide_run_context_push (self,
+                          ide_run_context_host_handler,
+                          NULL,
+                          NULL);
+}
+
 const char * const *
 ide_run_context_get_argv (IdeRunContext *self)
 {
@@ -280,23 +357,6 @@ ide_run_context_add_environ (IdeRunContext      *self,
     }
 }
 
-gboolean
-ide_run_context_get_run_on_host (IdeRunContext *self)
-{
-  g_return_val_if_fail (IDE_IS_RUN_CONTEXT (self), FALSE);
-
-  return self->run_on_host;
-}
-
-void
-ide_run_context_set_run_on_host (IdeRunContext *self,
-                                 gboolean       run_on_host)
-{
-  g_return_if_fail (IDE_IS_RUN_CONTEXT (self));
-
-  self->run_on_host = !!run_on_host;
-}
-
 const char *
 ide_run_context_get_cwd (IdeRunContext *self)
 {
@@ -716,7 +776,6 @@ ide_run_context_end (IdeRunContext  *self,
   ide_subprocess_launcher_set_argv (launcher, ide_run_context_get_argv (self));
   ide_subprocess_launcher_set_environ (launcher, ide_run_context_get_environ (self));
   ide_subprocess_launcher_set_cwd (launcher, cwd);
-  ide_subprocess_launcher_set_run_on_host (launcher, self->run_on_host);
 
   return g_steal_pointer (&launcher);
 }
diff --git a/src/libide/foundry/ide-run-context.h b/src/libide/foundry/ide-run-context.h
index 8ad40a3d4..8695448c7 100644
--- a/src/libide/foundry/ide-run-context.h
+++ b/src/libide/foundry/ide-run-context.h
@@ -54,10 +54,7 @@ void                   ide_run_context_push               (IdeRunContext
                                                            gpointer               handler_data,
                                                            GDestroyNotify         handler_data_destroy);
 IDE_AVAILABLE_IN_ALL
-gboolean               ide_run_context_get_run_on_host    (IdeRunContext         *self);
-IDE_AVAILABLE_IN_ALL
-void                   ide_run_context_set_run_on_host    (IdeRunContext         *self,
-                                                           gboolean               run_on_host);
+void                   ide_run_context_push_host          (IdeRunContext         *self);
 IDE_AVAILABLE_IN_ALL
 const char * const    *ide_run_context_get_argv           (IdeRunContext         *self);
 IDE_AVAILABLE_IN_ALL
diff --git a/src/plugins/flatpak/gbp-flatpak-runtime.c b/src/plugins/flatpak/gbp-flatpak-runtime.c
index dcb5d28cf..d5065cfa8 100644
--- a/src/plugins/flatpak/gbp-flatpak-runtime.c
+++ b/src/plugins/flatpak/gbp-flatpak-runtime.c
@@ -360,8 +360,6 @@ gbp_flatpak_runtime_handle_run_context_cb (IdeRunContext       *run_context,
   g_assert (IDE_IS_RUN_CONTEXT (run_context));
   g_assert (IDE_IS_UNIX_FD_MAP (unix_fd_map));
 
-  ide_run_context_set_run_on_host (run_context, TRUE);
-
   /* Pass through the FD mappings */
   if (!ide_run_context_merge_unix_fd_map (run_context, unix_fd_map, error))
     return FALSE;
@@ -456,6 +454,10 @@ gbp_flatpak_runtime_prepare_run_context (IdeRuntime    *runtime,
   g_assert (GBP_IS_FLATPAK_RUNTIME (runtime));
   g_assert (IDE_IS_RUN_CONTEXT (run_context));
 
+  /* We have to run "flatpak build" from the host */
+  ide_run_context_push_host (run_context);
+
+  /* Handle the upper layer to rewrite the command using "flatpak build" */
   ide_run_context_push (run_context,
                         gbp_flatpak_runtime_handle_run_context_cb,
                         g_object_ref (runtime),


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