From c08183c54d3d9fa722d737d0a16f03182a51067b Mon Sep 17 00:00:00 2001
From: Matt Borgerson <contact@mborgerson.com>
Date: Mon, 23 Jan 2023 03:24:21 -0700
Subject: [PATCH] xemu-win64-toolchain: Add libslirp v4.7.0

---
 ubuntu-win64-cross/Dockerfile       |  9 ++++++++
 ubuntu-win64-cross/libslirp-1.patch | 34 +++++++++++++++++++++++++++++
 ubuntu-win64-cross/libslirp.mk      | 19 ++++++++++++++++
 3 files changed, 62 insertions(+)
 create mode 100644 ubuntu-win64-cross/libslirp-1.patch
 create mode 100644 ubuntu-win64-cross/libslirp.mk

diff --git a/ubuntu-win64-cross/Dockerfile b/ubuntu-win64-cross/Dockerfile
index b579248930..bd26ee8170 100644
--- a/ubuntu-win64-cross/Dockerfile
+++ b/ubuntu-win64-cross/Dockerfile
@@ -71,6 +71,15 @@ RUN V=1 MXE_VERBOSE=1 make -C /opt/mxe \
         MXE_PLUGIN_DIRS=plugins/gcc10 \
             sdl2
 
+COPY libslirp.mk /opt/mxe/src/libslirp.mk
+COPY libslirp-1.patch /opt/mxe/src/libslirp-1.patch
+RUN V=1 MXE_VERBOSE=1 make -C /opt/mxe \
+        MXE_TARGETS=x86_64-w64-mingw32.static \
+        MXE_PLUGIN_DIRS=plugins/gcc10 \
+            libslirp
+
+RUN find /opt/mxe/usr -executable -type f -exec chmod a+x {} \;
+
 ENV CROSSPREFIX=x86_64-w64-mingw32.static-
 ENV CROSSAR=${CROSSPREFIX}gcc-ar
 ENV PATH="/opt/mxe/.ccache/bin:/opt/mxe/usr/x86_64-pc-linux-gnu/bin:/opt/mxe/usr/bin:${PATH}"
diff --git a/ubuntu-win64-cross/libslirp-1.patch b/ubuntu-win64-cross/libslirp-1.patch
new file mode 100644
index 0000000000..eb4b6a328c
--- /dev/null
+++ b/ubuntu-win64-cross/libslirp-1.patch
@@ -0,0 +1,34 @@
+From d1417aed0b335d03c67c998b17b72933764ccbe3 Mon Sep 17 00:00:00 2001
+From: Matt Borgerson <contact@mborgerson.com>
+Date: Mon, 23 Jan 2023 03:06:46 -0700
+Subject: [PATCH] Rename pingtest's slirp_inet_aton
+
+---
+ test/pingtest.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/test/pingtest.c b/test/pingtest.c
+index 3bb0488..04b63ce 100644
+--- a/test/pingtest.c
++++ b/test/pingtest.c
+@@ -24,7 +24,7 @@
+ #ifdef _WIN32
+ //#include <sys/select.h>
+ #include <winsock2.h>
+-int slirp_inet_aton(const char *cp, struct in_addr *ia)
++int pingtest_inet_aton(const char *cp, struct in_addr *ia)
+ {
+     uint32_t addr = inet_addr(cp);
+     if (addr == 0xffffffff) {
+@@ -33,7 +33,7 @@ int slirp_inet_aton(const char *cp, struct in_addr *ia)
+     ia->s_addr = addr;
+     return 1;
+ }
+-#define inet_aton slirp_inet_aton
++#define inet_aton pingtest_inet_aton
+ #else
+ #include <poll.h>
+ #endif
+-- 
+2.34.1
+
diff --git a/ubuntu-win64-cross/libslirp.mk b/ubuntu-win64-cross/libslirp.mk
new file mode 100644
index 0000000000..71449d14c6
--- /dev/null
+++ b/ubuntu-win64-cross/libslirp.mk
@@ -0,0 +1,19 @@
+# This file is part of MXE. See LICENSE.md for licensing information.
+
+PKG             := libslirp
+$(PKG)_WEBSITE  := https://gitlab.freedesktop.org/slirp/libslirp
+$(PKG)_IGNORE   :=
+$(PKG)_VERSION  := 4.7.0
+$(PKG)_SUBDIR   := libslirp-v$($(PKG)_VERSION)
+$(PKG)_FILE     := libslirp-v$($(PKG)_VERSION).tar.gz
+$(PKG)_CHECKSUM := 9398f0ec5a581d4e1cd6856b88ae83927e458d643788c3391a39e61b75db3d3b
+$(PKG)_URL      := https://gitlab.freedesktop.org/slirp/$(PKG)/-/archive/v$($(PKG)_VERSION)/$($(PKG)_FILE)
+$(PKG)_DEPS     := cc glib meson-wrapper
+
+define $(PKG)_BUILD
+    '$(MXE_MESON_WRAPPER)' $(MXE_MESON_OPTS) \
+        $(PKG_MESON_OPTS) \
+            --buildtype=plain \
+        '$(BUILD_DIR)' '$(SOURCE_DIR)'
+    '$(MXE_NINJA)' -C '$(BUILD_DIR)' -j '$(JOBS)' install
+endef