From 8f9dad9dbb340d025a88c58c0f2d4624beadfd6a Mon Sep 17 00:00:00 2001 From: Eric Van Albert Date: Mon, 24 Nov 2025 00:32:48 -0500 Subject: [PATCH] add build script --- .github/scripts/build.sh | 104 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100755 .github/scripts/build.sh diff --git a/.github/scripts/build.sh b/.github/scripts/build.sh new file mode 100755 index 00000000..378c197a --- /dev/null +++ b/.github/scripts/build.sh @@ -0,0 +1,104 @@ +#!/bin/bash +set -euo pipefail + +# Usage: TARGET=x86_64-pc-windows-gnu ./generate-rustflags.sh + +# Set PKG_CONFIG_PATH based on target +export PKG_CONFIG_PATH="../libmpv-static-build/${TARGET}/output/lib/pkgconfig" + +# Get all the static library dependencies +LIBS=$(pkg-config --static --libs mpv) + +# Convert pkg-config output to RUSTFLAGS format +RUSTFLAGS="" + +# Add all library flags, excluding -lpthread and -lstdc++ (we'll handle these separately) +for flag in $LIBS; do + if [[ $flag == -l* ]] || [[ $flag == -L* ]]; then + # Skip -lpthread and -lstdc++ since we'll link them statically + if [[ $flag != "-lpthread" ]] && [[ $flag != "-lstdc++" ]]; then + RUSTFLAGS="$RUSTFLAGS -C link-arg=$flag" + fi + fi +done + +# Platform-specific linking +case "$TARGET" in + x86_64-pc-windows-gnu) + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,--allow-multiple-definition" + # Wrap all static libraries in a group to resolve circular dependencies + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,-Bstatic" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,--start-group" + # Force static linking of libgcc, libgcc_eh, libstdc++, libwinpthread, and libmingw32 + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lmingw32" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lwinpthread" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lgcc_eh" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lgcc" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lstdc++" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,--end-group" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,-Bdynamic" + # MinGW runtime libraries (mingwex static, msvcrt uses system default CRT) + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,--start-group" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,-Bstatic" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lmingwex" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,-Bdynamic" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lmsvcrt" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,--end-group" + # Windows system libraries (dynamically linked) + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lkernel32" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-ladvapi32" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lshell32" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lole32" + + export RUSTFLAGS + echo "Generated RUSTFLAGS: $RUSTFLAGS" + + cargo build --release --target "$TARGET" + x86_64-w64-mingw32-strip "target/${TARGET}/release/radiance.exe" + ;; + x86_64-unknown-linux-gnu) + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,-Bstatic" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-lstdc++" + RUSTFLAGS="$RUSTFLAGS -C link-arg=-Wl,-Bdynamic" + + # Populate /tmp/native-lib-pc with fake pkg-config files so we only dynamically link the things we want to + rm -rf /tmp/native-lib-pc/ + mkdir -p /tmp/native-lib-pc/ + + cat </tmp/native-lib-pc/mpv.pc +Name: mpv +Description: placeholder +Version: 999 +Cflags: +Libs: +EOF + + cat </tmp/native-lib-pc/x11.pc +Name: X11 +Description: placeholder +Version: 999 +Cflags: +Libs: -lX11 +EOF + + cat </tmp/native-lib-pc/alsa.pc +Name: alsa +Description: placeholder +Version: 999 +Cflags: +Libs: -lasound +EOF + + export RUSTFLAGS + echo "Generated RUSTFLAGS: $RUSTFLAGS" + + PKG_CONFIG_LIBDIR="/tmp/native-lib-pc/" \ + PKG_CONFIG_PATH="/tmp/native-lib-pc/" \ + cargo build --release --target "$TARGET" + strip "target/${TARGET}/release/radiance" + ;; + *) + echo "Unknown target: $TARGET" >&2 + exit 1 + ;; +esac