From dcad860b70c7c8b8defb7d69ee105340d136416f Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Tue, 17 Sep 2019 15:16:14 +0100
Subject: [PATCH] build: Add steam-runtime-tools library

When doing the development build on a non-SteamRT OS (Debian buster),
this means we need to take steam-runtime-tools from git.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 .gitignore                           |  1 +
 THIRD-PARTY.md                       | 13 ++++++++--
 build-relocatable-install.py         | 38 +++++++++++++++++++++++++++-
 ci/Dockerfile.in                     |  4 +++
 debian/control                       |  4 +++
 debian/gitlab-ci.yml                 | 16 ++++++++----
 debian/tests/relocatable-install     |  6 +++++
 meson.build                          |  6 +++++
 src/wrap.c                           |  6 ++++-
 subprojects/steam-runtime-tools.wrap |  4 +++
 10 files changed, 89 insertions(+), 9 deletions(-)
 create mode 100644 subprojects/steam-runtime-tools.wrap

diff --git a/.gitignore b/.gitignore
index 329b876b0..54314a50d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@
 /libcapsule_*.tar.[gx]z
 /pressure-vessel-[0-9]*.tar.gz
 /relocatable-install/
+/subprojects/steam-runtime-tools/
diff --git a/THIRD-PARTY.md b/THIRD-PARTY.md
index aab21266a..e4c524bc0 100644
--- a/THIRD-PARTY.md
+++ b/THIRD-PARTY.md
@@ -10,11 +10,17 @@ same place as pressure-vessel, or from
 pressure-vessel incorporates code from GNOME libglnx, variously licensed
 under [LGPL-2.0-or-later][] and [LGPL-2.1-or-later][].
 Complete source code can be obtained from the same place as
-pressure-vessel.
+pressure-vessel, or from <http://repo.steampowered.com/steamrt/>.
 
 pressure-vessel incorporates code from Flatpak, licensed under
 [LGPL-2.1-or-later][]. Complete source code can be obtained from the
-same place as pressure-vessel.
+same place as pressure-vessel, or from
+<http://repo.steampowered.com/steamrt/>.
+
+pressure-vessel includes the steam-runtime-tools library and utilities,
+which are licensed under the [MIT][] license. See steam-runtime-tools.txt.
+Complete source code can be obtained from the same place as pressure-vessel,
+or from <http://repo.steampowered.com/steamrt/>.
 
 Binary releases of pressure-vessel include the following third-party
 programs and libraries:
@@ -47,6 +53,9 @@ programs and libraries:
 
 - libz.so.1 from zlib: [Zlib][] license. See zlib.txt.
 
+- libjson-glib-1.0.so.0 from json-glib: [LGPL-2.0-or-later][]. See
+  json-glib.txt.
+
 Complete source code can be obtained from the same place as
 pressure-vessel, or from <http://repo.steampowered.com/steamrt/>.
 
diff --git a/build-relocatable-install.py b/build-relocatable-install.py
index cf86be7c5..8bd52e776 100755
--- a/build-relocatable-install.py
+++ b/build-relocatable-install.py
@@ -78,11 +78,13 @@ ARCHS = [
 DEPENDENCIES = {
     'libcapsule-tools-relocatable': 'libcapsule',
     'libelf1': 'elfutils',
+    'libsteam-runtime-tools-0-helpers': 'steam-runtime-tools',
     'zlib1g': 'zlib',
 }
 # program to install => binary package
 WRAPPED_PROGRAMS = {
     'bwrap': 'bubblewrap',
+    'steam-runtime-system-info': 'steam-runtime-tools-bin',
 }
 PRIMARY_ARCH_DEPENDENCIES = {
     'bubblewrap': 'bubblewrap',
@@ -90,10 +92,12 @@ PRIMARY_ARCH_DEPENDENCIES = {
     'libcap2': 'libcap2',
     'libffi6': 'libffi',
     'libglib2.0-0': 'glib2.0',
+    'libjson-glib-1.0-0': 'json-glib',
     'libmount1': 'util-linux',
     'libpcre3': 'pcre3',
-    'libxau6': 'libxau',
     'libselinux1': 'libselinux',
+    'libsteam-runtime-tools-0-0': 'steam-runtime-tools',
+    'libxau6': 'libxau',
 }
 SCRIPTS = [
     'pressure-vessel-locale-gen',
@@ -233,6 +237,7 @@ def main():
             raise RuntimeError('--output directory must not already exist')
 
         os.makedirs(os.path.join(installation, 'bin'), exist_ok=True)
+        os.makedirs(os.path.join(installation, 'libexec'), exist_ok=True)
         os.makedirs(os.path.join(installation, 'metadata'), exist_ok=True)
 
         for arch in ARCHS:
@@ -291,6 +296,35 @@ def main():
                     os.path.join(installation, 'bin'),
                 )
 
+            path = '/usr/libexec/steam-runtime-tools-0'
+
+            if not os.path.exists(path):
+                package = 'libsteam-runtime-tools-0-helpers'
+                v_check_call([
+                    'apt-get',
+                    'download',
+                    package + ':' + arch.name,
+                ], cwd=tmpdir)
+                v_check_call(
+                    'dpkg-deb -X {}_*_{}.deb build-relocatable'.format(
+                        quote(package),
+                        quote(arch.name),
+                    ),
+                    cwd=tmpdir,
+                    shell=True,
+                )
+                path = '{}/build-relocatable/{}'.format(tmpdir, path)
+
+            for tool in glob.glob(os.path.join(path, arch.multiarch + '-*')):
+                install_exe(
+                    tool,
+                    os.path.join(
+                        installation,
+                        'libexec',
+                        'steam-runtime-tools-0',
+                    ),
+                )
+
         primary_architecture = subprocess.check_output([
             'dpkg', '--print-architecture',
         ]).decode('utf-8').strip()
@@ -329,8 +363,10 @@ def main():
                     'soname:libXau.so.6',
                     'soname:libcap.so.2',
                     'soname:libgio-2.0.so.0',
+                    'soname:libjson-glib-1.0.so.0',
                     'soname:libpcre.so.3',
                     'soname:libselinux.so.1',
+                    'soname:libsteam-runtime-tools-0.so.0',
                 ])
 
             for so in glob.glob(
diff --git a/ci/Dockerfile.in b/ci/Dockerfile.in
index 86e6adba2..29def092e 100644
--- a/ci/Dockerfile.in
+++ b/ci/Dockerfile.in
@@ -17,9 +17,13 @@ apt-get -y install \
     libmount1 \
     libpcre3 \
     libselinux1 \
+    libsteam-runtime-tools-0-dev \
+    libsteam-runtime-tools-0-helpers:amd64 \
+    libsteam-runtime-tools-0-helpers:i386 \
     libxau6 \
     locales \
     pandoc \
+    steam-runtime-tools-bin \
     zlib1g:amd64 \
     zlib1g:i386 \
     ${NULL+}
diff --git a/debian/control b/debian/control
index 8d88b32de..ca0653a5e 100644
--- a/debian/control
+++ b/debian/control
@@ -6,6 +6,7 @@ Standards-Version: 4.4.0
 Build-Depends:
  debhelper (>= 9),
  libglib2.0-dev,
+ libsteam-runtime-tools-0-dev,
  libxau-dev,
  locales <!nocheck> | locales-all <!nocheck>,
  meson,
@@ -22,6 +23,7 @@ Multi-Arch: foreign
 Depends:
  bubblewrap,
  libcapsule-tools-relocatable,
+ steam-runtime-tools-bin,
  ${misc:Depends},
  ${shlibs:Depends},
 Recommends:
@@ -37,6 +39,7 @@ Architecture: amd64
 Multi-Arch: foreign
 Depends:
  libcapsule-tools-relocatable,
+ libsteam-runtime-tools-0-helpers,
  ${misc:Depends},
  ${shlibs:Depends},
 Description: Steam container launcher - cross-architecture dependencies
@@ -49,6 +52,7 @@ Architecture: i386
 Multi-Arch: foreign
 Depends:
  libcapsule-tools-relocatable,
+ libsteam-runtime-tools-0-helpers,
  ${misc:Depends},
  ${shlibs:Depends},
 Description: Steam container launcher - cross-architecture dependencies
diff --git a/debian/gitlab-ci.yml b/debian/gitlab-ci.yml
index 430e49fcf..4ff147849 100644
--- a/debian/gitlab-ci.yml
+++ b/debian/gitlab-ci.yml
@@ -29,6 +29,9 @@ variables:
     # Set non-empty to allow
     CI_ALLOW_MISSING_SOURCES: ''
 
+    STEAM_RUNTIME_TOOLS_GIT_REPO: https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.steamos.cloud/steam/steam-runtime-tools.git
+    STEAM_RUNTIME_TOOLS_GIT_BRANCH: master
+
 build:
     variables:
         BUILD_IMAGE: docker.internal.steamos.cloud/steamos/package-builder:buster
@@ -40,11 +43,10 @@ build:devel:
         - |
             set -eux
 
-            apt-get -y --no-install-recommends install \
-            libglib2.0-dev \
-            libxau-dev \
-            meson \
-            ${NULL+}
+            git clone \
+            -b${STEAM_RUNTIME_TOOLS_GIT_BRANCH} \
+            ${STEAM_RUNTIME_TOOLS_GIT_REPO} \
+            subprojects/steam-runtime-tools
 
             meson _build
             ninja -C _build
@@ -81,9 +83,13 @@ build:scout:
             libcapsule0 \
             libcapsule-tools-relocatable:amd64 \
             libcapsule-tools-relocatable:i386 \
+            libsteam-runtime-tools-0-dev \
+            libsteam-runtime-tools-0-helpers:amd64 \
+            libsteam-runtime-tools-0-helpers:i386 \
             libglib2.0-dev \
             libxau-dev \
             meson \
+            steam-runtime-tools-bin
             ${NULL+}
 
             meson \
diff --git a/debian/tests/relocatable-install b/debian/tests/relocatable-install
index 5bd50727a..5a623a1df 100755
--- a/debian/tests/relocatable-install
+++ b/debian/tests/relocatable-install
@@ -4,9 +4,15 @@ set -eux
 
 dpkg --add-architecture amd64
 dpkg --add-architecture i386
+apt-get -y update
 apt-get -y install \
     libcapsule-tools-relocatable:amd64 \
     libcapsule-tools-relocatable:i386 \
+    libsteam-runtime-tools-0-0:amd64 \
+    libsteam-runtime-tools-0-0:i386 \
+    libsteam-runtime-tools-0-helpers:amd64 \
+    libsteam-runtime-tools-0-helpers:i386 \
+    steam-runtime-tools-bin \
     ${NULL+}
 
 /usr/lib/pressure-vessel/relocatable/bin/pressure-vessel-build-relocatable-install \
diff --git a/meson.build b/meson.build
index 295cae89a..30d2004f3 100644
--- a/meson.build
+++ b/meson.build
@@ -177,6 +177,12 @@ executable(
     '-Wno-unused-local-typedefs',
   ],
   dependencies : [
+    dependency(
+      'steam-runtime-tools-0',
+      required : true,
+      fallback : ['steam-runtime-tools', 'libsteamrt_dep'],
+      default_options : ['gtk_doc=false'],
+    ),
     dependency('gio-unix-2.0', required : true),
     dependency('xau', required : true),
     subproject('libglnx').get_variable('libglnx_dep'),
diff --git a/src/wrap.c b/src/wrap.c
index a621b7365..164733e43 100644
--- a/src/wrap.c
+++ b/src/wrap.c
@@ -33,10 +33,14 @@
 #include <stdlib.h>
 #include <sysexits.h>
 
+/* Include these before steam-runtime-tools.h so that their backport of
+ * G_DEFINE_AUTOPTR_CLEANUP_FUNC will be visible to it */
+#include "glib-backports.h"
 #include "libglnx.h"
 
+#include <steam-runtime-tools/steam-runtime-tools.h>
+
 #include "bwrap.h"
-#include "glib-backports.h"
 #include "flatpak-bwrap-private.h"
 #include "flatpak-run-private.h"
 #include "flatpak-utils-private.h"
diff --git a/subprojects/steam-runtime-tools.wrap b/subprojects/steam-runtime-tools.wrap
new file mode 100644
index 000000000..f30180ac0
--- /dev/null
+++ b/subprojects/steam-runtime-tools.wrap
@@ -0,0 +1,4 @@
+[wrap-git]
+directory = steam-runtime-tools
+url = https://gitlab.steamos.cloud/steam/steam-runtime-tools.git
+revision = head
-- 
GitLab