From 225a3a03b61b5a48d30e61db63a8b137d0416ce6 Mon Sep 17 00:00:00 2001
From: Marcin Kurczewski <dash@wind.garden>
Date: Tue, 19 Nov 2024 15:47:38 +0100
Subject: [PATCH] build: change uthash packaging

Resolves #1856.
---
 .dockerignore                             |  1 +
 .github/workflows/job_build_tr1_macos.yml |  7 ++++++-
 .gitignore                                |  3 ---
 src/libtrx/meson.build                    |  4 +---
 src/libtrx/subprojects/uthash.wrap        | 11 -----------
 tools/tr1/docker/game-linux/Dockerfile    | 18 ++++--------------
 tools/tr1/docker/game-win/Dockerfile      | 19 +++++++++++++++++--
 tools/tr2/docker/game-win/Dockerfile      | 19 +++++++++++++++++--
 8 files changed, 46 insertions(+), 36 deletions(-)
 delete mode 100644 src/libtrx/subprojects/uthash.wrap

diff --git a/.dockerignore b/.dockerignore
index 6ef407d53..c1005a191 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,2 +1,3 @@
 /data
 /test
+/build
diff --git a/.github/workflows/job_build_tr1_macos.yml b/.github/workflows/job_build_tr1_macos.yml
index b12c21489..0a121a0a0 100644
--- a/.github/workflows/job_build_tr1_macos.yml
+++ b/.github/workflows/job_build_tr1_macos.yml
@@ -20,6 +20,7 @@ env:
   FFMPEG_INSTALL_TMP_X86_64: /tmp/install_x86_64
   CACHE_TMP_DIR: /tmp/opt_local/
   CACHE_DST_DIR: /opt/local/
+  C_INCLUDE_PATH: /opt/local/include/uthash/:/opt/local/include/
 
 jobs:
   build:
@@ -62,7 +63,7 @@ jobs:
         id: restore-cache
         uses: actions/cache/restore@v4
         with:
-          key: ${{ runner.os }}-tooling-${{ hashFiles('.github/workflows/build_macos.yml') }}
+          key: ${{ runner.os }}-tooling-${{ hashFiles('.github/workflows/job_build_tr1_macos.yml') }}
           path: |
             ${{ env.CACHE_TMP_DIR }}
             ${{ env.FFMPEG_INSTALL_TMP_ARM64 }}
@@ -115,6 +116,10 @@ jobs:
         if: steps.restore-cache.outputs.cache-hit != 'true'
         run: sudo port -N install libsdl2 +universal
 
+      - name: "Build dependency: uthash (universal)"
+        if: steps.restore-cache.outputs.cache-hit != 'true'
+        run: sudo port -N install uthash +universal
+
       - name: "Build dependency: ffmpeg (universal)"
         if: steps.restore-cache.outputs.cache-hit != 'true'
         run: |
diff --git a/.gitignore b/.gitignore
index a69d0c2b1..ff2ce821f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,8 +33,5 @@ Release/
 # libtrx artefacts
 **/subprojects/packagecache/
 **/subprojects/dwarfstack-*/
-**/subprojects/uthash-*/
 src/tr1/subprojects/dwarfstack.wrap
-src/tr1/subprojects/uthash.wrap
 src/tr2/subprojects/dwarfstack.wrap
-src/tr2/subprojects/uthash.wrap
diff --git a/src/libtrx/meson.build b/src/libtrx/meson.build
index 8faa282fb..d6a5fe82d 100644
--- a/src/libtrx/meson.build
+++ b/src/libtrx/meson.build
@@ -32,8 +32,6 @@ if host_machine.system() == 'darwin'
   staticdeps = false
 endif
 
-uthash = subproject('uthash', default_options: ['warning_level=0'])
-
 null_dep = dependency('', required: false)
 dep_avcodec = dependency('libavcodec', static: staticdeps)
 dep_avformat = dependency('libavformat', static: staticdeps)
@@ -43,6 +41,7 @@ dep_pcre2 = dependency('libpcre2-8', static: staticdeps)
 dep_backtrace = c_compiler.find_library('backtrace', static: true, required: false)
 dep_swscale = dependency('libswscale', static: staticdeps)
 dep_swresample = dependency('libswresample', static: staticdeps)
+c_compiler.check_header('uthash.h', required: true)
 
 dep_zlib = null_dep
 if not staticdeps
@@ -151,7 +150,6 @@ dependencies = [
   dep_swscale,
   dep_zlib,
   dep_opengl,
-  uthash.get_variable('uthash_dep'),
 ]
 
 if dep_backtrace.found() and host_machine.system() == 'linux'
diff --git a/src/libtrx/subprojects/uthash.wrap b/src/libtrx/subprojects/uthash.wrap
deleted file mode 100644
index 68a51eaaf..000000000
--- a/src/libtrx/subprojects/uthash.wrap
+++ /dev/null
@@ -1,11 +0,0 @@
-[wrap-file]
-directory = uthash-2.3.0
-source_url = https://github.com/troydhanson/uthash/archive/v2.3.0.tar.gz
-source_filename = uthash-2.3.0.tar.gz
-source_hash = e10382ab75518bad8319eb922ad04f907cb20cccb451a3aa980c9d005e661acc
-patch_filename = uthash_2.3.0-1_patch.zip
-patch_url = https://wrapdb.mesonbuild.com/v2/uthash_2.3.0-1/get_patch
-patch_hash = d0b7cf9788c3735ee6a08bb649c58be1f5fad4b95e50e7681cbdf44882da9d7e
-
-[provide]
-uthash = uthash_dep
diff --git a/tools/tr1/docker/game-linux/Dockerfile b/tools/tr1/docker/game-linux/Dockerfile
index 98aed3c15..b851e024f 100644
--- a/tools/tr1/docker/game-linux/Dockerfile
+++ b/tools/tr1/docker/game-linux/Dockerfile
@@ -92,19 +92,6 @@ RUN cd SDL \
 
 
 
-# UPX
-FROM base AS upx
-
-RUN mkdir /ext/
-WORKDIR /ext/
-
-RUN apt-get install -y wget xz-utils
-RUN wget https://github.com/upx/upx/releases/download/v4.1.0/upx-4.1.0-amd64_linux.tar.xz
-RUN tar -xvf upx-*.tar.xz
-RUN ln -s upx-*/upx
-
-
-
 # TR1X
 FROM base
 
@@ -130,9 +117,12 @@ RUN apt-get install -y \
         pyjson5 \
         meson \
         ninja
+# Regular dependencies
+RUN apt-get install -y \
+        upx \
+        uthash-dev
 
 # manually built dependencies
-COPY --from=upx /ext/upx /usr/local/bin/upx
 COPY --from=libav /ext/ /ext/
 COPY --from=sdl /ext/ /ext/
 COPY --from=backtrace /ext/ /ext/
diff --git a/tools/tr1/docker/game-win/Dockerfile b/tools/tr1/docker/game-win/Dockerfile
index 65ad4c5b4..ae2a98f11 100644
--- a/tools/tr1/docker/game-win/Dockerfile
+++ b/tools/tr1/docker/game-win/Dockerfile
@@ -112,6 +112,19 @@ RUN cd SDL \
 
 
 
+# uthash
+FROM mingw AS uthash
+
+RUN mkdir /ext/
+WORKDIR /tmp/
+
+RUN apt-get install -y wget xz-utils
+RUN wget https://github.com/troydhanson/uthash/archive/v2.3.0.tar.gz
+RUN tar -xvf v2.3.0.tar.gz
+RUN cp -rL uthash-2.3.0/* /ext/
+
+
+
 # TR1X
 FROM mingw
 
@@ -123,12 +136,11 @@ COPY --from=pcre2 /ext/ /ext/
 COPY --from=zlib /ext/ /ext/
 COPY --from=libav /ext/ /ext/
 COPY --from=sdl /ext/ /ext/
+COPY --from=uthash /ext/ /ext/
 
 # system dependencies
 # configure pkgconfig manually
 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=967969
-ENV PKG_CONFIG_LIBDIR=/ext/lib/
-ENV PKG_CONFIG_PATH=/ext/lib/pkgconfig/
 RUN apt-get install -y \
         mingw-w64-tools \
         pkg-config \
@@ -139,5 +151,8 @@ RUN apt-get install -y \
         meson \
         ninja
 
+ENV PKG_CONFIG_LIBDIR=/ext/lib/
+ENV PKG_CONFIG_PATH=/ext/lib/pkgconfig/
+ENV C_INCLUDE_PATH=/ext/include/
 ENV PYTHONPATH=/app/tools/
 ENTRYPOINT ["/app/tools/tr1/docker/game-win/entrypoint.sh"]
diff --git a/tools/tr2/docker/game-win/Dockerfile b/tools/tr2/docker/game-win/Dockerfile
index 130f8e3dd..1f119f6be 100644
--- a/tools/tr2/docker/game-win/Dockerfile
+++ b/tools/tr2/docker/game-win/Dockerfile
@@ -97,6 +97,19 @@ RUN cd SDL \
 
 
 
+# uthash
+FROM mingw AS uthash
+
+RUN mkdir /ext/
+WORKDIR /tmp/
+
+RUN apt-get install -y wget xz-utils
+RUN wget https://github.com/troydhanson/uthash/archive/v2.3.0.tar.gz
+RUN tar -xvf v2.3.0.tar.gz
+RUN cp -rL uthash-2.3.0/* /ext/
+
+
+
 # TR2X
 FROM mingw
 
@@ -108,12 +121,11 @@ COPY --from=pcre2 /ext/ /ext/
 COPY --from=zlib /ext/ /ext/
 COPY --from=libav /ext/ /ext/
 COPY --from=sdl /ext/ /ext/
+COPY --from=uthash /ext/ /ext/
 
 # system dependencies
 # configure pkgconfig manually
 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=967969
-ENV PKG_CONFIG_LIBDIR=/ext/lib/
-ENV PKG_CONFIG_PATH=/ext/lib/pkgconfig/
 RUN apt-get install -y \
         mingw-w64-tools \
         pkg-config \
@@ -124,5 +136,8 @@ RUN apt-get install -y \
         meson \
         ninja
 
+ENV PKG_CONFIG_LIBDIR=/ext/lib/
+ENV PKG_CONFIG_PATH=/ext/lib/pkgconfig/
+ENV C_INCLUDE_PATH=/ext/include/
 ENV PYTHONPATH=/app/tools/
 ENTRYPOINT ["/app/tools/tr2/docker/game-win/entrypoint.sh"]