From e129029921253818638637a188d75586e267b4fa Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 23 Aug 2019 13:11:43 +0100
Subject: [PATCH] wrap: Add an option to find the runtime relative to a base
 path

In the SteamLinuxRuntime environment, this could be set to the depot's
base path (the directory containing 'scout' and 'pressure-vessel').

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel-test-ui         |  8 ++++++++
 pressure-vessel-unruntime-scout |  3 ++-
 src/wrap.c                      | 20 ++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/pressure-vessel-test-ui b/pressure-vessel-test-ui
index 3b6ca1534..bfe70b22b 100755
--- a/pressure-vessel-test-ui
+++ b/pressure-vessel-test-ui
@@ -56,13 +56,21 @@ class Gui:
         self.runtimes = {}      # type: typing.Dict[str, str]
 
         for search in (
+            os.getenv('PRESSURE_VESSEL_RUNTIME_BASE'),
             '..',
             '../..',
         ):
+            if search is None:
+                continue
+
             source_of_runtimes = os.path.join(
                 os.path.dirname(sys.argv[0]),
                 search,
             )
+
+            if not os.path.isdir(source_of_runtimes):
+                continue
+
             for member in os.listdir(source_of_runtimes):
                 path = os.path.realpath(
                     os.path.join(source_of_runtimes, member)
diff --git a/pressure-vessel-unruntime-scout b/pressure-vessel-unruntime-scout
index 6aed386ae..45435502a 100755
--- a/pressure-vessel-unruntime-scout
+++ b/pressure-vessel-unruntime-scout
@@ -34,7 +34,8 @@ me="$(readlink -f "$0")"
 here="${me%/*}"
 me="${me##*/}"
 
-export PRESSURE_VESSEL_RUNTIME="${here}/../../scout/files"
+export PRESSURE_VESSEL_RUNTIME_BASE="${here}/../.."
+export PRESSURE_VESSEL_RUNTIME="scout/files"
 exec "$here/pressure-vessel-unruntime" "$@"
 
 # vim:set sw=4 sts=4 et:
diff --git a/src/wrap.c b/src/wrap.c
index 46347268a..159b744a9 100644
--- a/src/wrap.c
+++ b/src/wrap.c
@@ -1597,6 +1597,7 @@ static gboolean opt_shell_after = FALSE;
 static gboolean opt_shell_fail = FALSE;
 static gboolean opt_shell_instead = FALSE;
 static GPtrArray *opt_ld_preload = NULL;
+static char *opt_runtime_base = NULL;
 static char *opt_runtime = NULL;
 static gboolean opt_share_home = TRUE;
 static gboolean opt_verbose = FALSE;
@@ -1644,6 +1645,10 @@ static GOptionEntry options[] =
     "it with the host system's graphics stack. The empty string "
     "means don't use a runtime. [Default: $PRESSURE_VESSEL_RUNTIME or '']",
     "RUNTIME" },
+  { "runtime-base", 0, 0, G_OPTION_ARG_FILENAME, &opt_runtime_base,
+    "If a --runtime is a relative path, look for it relative to BASE. "
+    "[Default: $PRESSURE_VESSEL_RUNTIME_BASE or '.']",
+    "BASE" },
   { "share-home", 0, 0, G_OPTION_ARG_NONE, &opt_share_home,
     "Use the real home directory. [Default]", NULL },
   { "unshare-home", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_share_home,
@@ -1746,6 +1751,20 @@ main (int argc,
   if (opt_runtime == NULL)
     opt_runtime = g_strdup (g_getenv ("PRESSURE_VESSEL_RUNTIME"));
 
+  if (opt_runtime_base == NULL)
+    opt_runtime_base = g_strdup (g_getenv ("PRESSURE_VESSEL_RUNTIME_BASE"));
+
+  if (opt_runtime != NULL
+      && opt_runtime[0] != '\0'
+      && !g_path_is_absolute (opt_runtime)
+      && opt_runtime_base != NULL
+      && opt_runtime_base[0] != '\0')
+    {
+      g_autofree gchar *tmp = g_steal_pointer (&opt_runtime);
+
+      opt_runtime = g_build_filename (opt_runtime_base, tmp, NULL);
+    }
+
   if (opt_version)
     {
       g_print ("pressure-vessel version %s\n", VERSION);
@@ -2303,6 +2322,7 @@ out:
   g_clear_pointer (&opt_steam_app_id, g_free);
   g_clear_pointer (&opt_home, g_free);
   g_clear_pointer (&opt_fake_home, g_free);
+  g_clear_pointer (&opt_runtime_base, g_free);
   g_clear_pointer (&opt_runtime, g_free);
 
   return ret;
-- 
GitLab