#!/usr/bin/env bash
# Build libuv + libsodium for x86_64-w64-mingw32 into hyperswarm-peer's prefix.
# Versions MATCH the host packages the Linux build links (libuv 1.48.0,
# libsodium 1.0.18) so a Windows-only fault cannot be a version difference.
set -euo pipefail
PREFIX=/storage/projects/code/godot/hyperswarm-peer/thirdparty/mingw-prefix
BUILD=/tmp/claude-1000/-storage-projects-code-hypercore-kb/7124d82d-783f-4b82-878a-bf9a7cb67294/scratchpad/mingw-build
HOSTTRIPLE=x86_64-w64-mingw32
command -v cmake >/dev/null || { echo "FATAL: cmake not installed"; exit 1; }
mkdir -p "$PREFIX" "$BUILD"
cd "$BUILD"
echo "=== fetching ==="
[ -f libuv.tar.gz ] || curl -sSL --max-time 300 -o libuv.tar.gz https://dist.libuv.org/dist/v1.48.0/libuv-v1.48.0.tar.gz
# From the GitHub release, not download.libsodium.org: that host now 404s 1.0.18
# (it serves only current releases) and returns an HTML error page, which tar
# reports as "does not look like a tar archive" rather than as a failed download.
[ -f libsodium-gh.tar.gz ] || curl -sSL --max-time 300 -o libsodium-gh.tar.gz \
https://github.com/jedisct1/libsodium/releases/download/1.0.18-RELEASE/libsodium-1.0.18.tar.gz
# Verify before extracting, so a redirect page fails HERE with a clear message.
for f in libuv.tar.gz libsodium-gh.tar.gz; do
tar tzf "$f" >/dev/null 2>&1 || { echo "FATAL: $f is not a gzip tarball ($(file -b "$f"))"; exit 1; }
done
[ -d libuv-v1.48.0 ] || tar xf libuv.tar.gz
[ -d libsodium-1.0.18 ] || tar xf libsodium-gh.tar.gz
echo "=== libsodium (autotools, static) ==="
cd "$BUILD/libsodium-1.0.18"
if [ ! -f .built ]; then
./configure --host="$HOSTTRIPLE" --prefix="$PREFIX" \
--disable-shared --enable-static --disable-ssp --without-pthreads >/dev/null
make -j"$(nproc)" >/dev/null
make install >/dev/null
touch .built
fi
echo "libsodium: $(ls "$PREFIX/lib/libsodium.a" 2>/dev/null || echo MISSING)"
echo "=== libuv (cmake, static) ==="
cd "$BUILD/libuv-v1.48.0"
if [ ! -f .built ]; then
cmake -S . -B build-win \
-DCMAKE_SYSTEM_NAME=Windows \
-DCMAKE_C_COMPILER="${HOSTTRIPLE}-gcc" \
-DCMAKE_CXX_COMPILER="${HOSTTRIPLE}-g++" \
-DCMAKE_RC_COMPILER="${HOSTTRIPLE}-windres" \
-DCMAKE_INSTALL_PREFIX="$PREFIX" \
-DBUILD_SHARED_LIBS=OFF \
-DLIBUV_BUILD_TESTS=OFF \
-DLIBUV_BUILD_BENCH=OFF >/dev/null
cmake --build build-win -j"$(nproc)" >/dev/null
cmake --install build-win >/dev/null
touch .built
fi
echo "libuv: $(ls "$PREFIX"/lib/libuv*.a 2>/dev/null || echo MISSING)"
echo "=== prefix contents ==="
find "$PREFIX" -maxdepth 2 -name "*.a" -o -maxdepth 2 -name "*.pc" | sort