Skip to content

Instantly share code, notes, and snippets.

@pojntfx
Last active February 9, 2024 07:07
Show Gist options
  • Save pojntfx/36a1f41ffd34af7d2f3abe43646142e1 to your computer and use it in GitHub Desktop.
Save pojntfx/36a1f41ffd34af7d2f3abe43646142e1 to your computer and use it in GitHub Desktop.
Cross-compile and package GTK4/libadwaita apps for Windows from Linux with MSYS2 and WINE
#!/bin/bash
# Install native dependencies
sudo dnf update -y
sudo dnf install -y curl wine
# Install MSYS2
curl -L -o /tmp/msys2.exe 'https://github.com/msys2/msys2-installer/releases/download/2021-11-30/msys2-base-x86_64-20211130.sfx.exe'
wine64 /tmp/msys2.exe x -y -oC:/
# Fix MSYS2
sed -i ~/.wine/drive_c/msys64/etc/pacman.conf -e 's/SigLevel = Required/SigLevel = Never/g'
cat /etc/pki/tls/certs/ca-bundle.crt >~/.wine/drive_c/msys64/usr/ssl/certs/ca-bundle.crt
cat /etc/pki/tls/certs/ca-bundle.trust.crt >~/.wine/drive_c/msys64/usr/ssl/certs/ca-bundle.trust.crt
export WINEPATH="C:\\msys64\\usr\\bin"
# Install GCC, Go, GTK4 and libadwaita (plus any other packages you want to be included in the image)
rm -f ~/.wine/drive_c/msys64/var/lib/pacman/db.lck
wine64 bash.exe -c 'pacman --verbose --debug --noconfirm --ignore pacman -Syu' || true # It won't upgrade due to locks, but we only need it to refresh it's caches
rm -f ~/.wine/drive_c/msys64/var/lib/pacman/db.lck
wine64 bash.exe -c 'pacman --verbose --debug --noconfirm --ignore pacman --needed -S git mingw-w64-x86_64-gcc mingw-w64-x86_64-go mingw-w64-x86_64-pkg-config mingw-w64-x86_64-gtk4 mingw-w64-x86_64-gobject-introspection mingw-w64-x86_64-gobject-introspection-runtime mingw-w64-x86_64-glib2 mingw-w64-x86_64-libadwaita'
sed -i ~/.wine/drive_c/msys64/mingw64/lib/pkgconfig/* -e 's/-Wl,-luuid/-luuid/g' # See https://github.com/gotk3/gotk3/wiki/Installing-on-Windows#chocolatey, fails with invalid flag in pkg-config --libs: -Wl,-luuid otherwise
mkdir -p ~/.wine/drive_c/go ~/.wine/drive_c/tmp
# Copy source code to directory on C drive
rm -rf ~/.wine/drive_c/users/$(whoami)/Documents/gotk4-workspace
mkdir -p ~/.wine/drive_c/users/$(whoami)/Documents/gotk4-workspace
rm -rf out
cp -r . ~/.wine/drive_c/users/$(whoami)/Documents/gotk4-workspace
# Build
wine64 bash.exe -c "export PATH=$PATH:/mingw64/bin GOPATH=/c/go GOROOT=/mingw64/lib/go TMP=/c/tmp TEMP=/c/tmp GOARCH=amd64 && cd /c/users/$(whoami)/Documents/gotk4-workspace && go build -buildvcs=false -ldflags='-linkmode=external' -x -v -o out/vintangle-gui.exe ./cmd/vintangle-gui"
# Copy binaries to staging directory
rm -rf out
mkdir -p out/dist/bin
yes | cp -f ~/.wine/drive_c/users/$(whoami)/Documents/gotk4-workspace/out/* out/dist/bin
# Package (you can distribute the resulting .zip)
# To include everything:
cp -r ~/.wine/drive_c/msys64/mingw64/* out/dist
# To include only DLLs:
cp ~/.wine/drive_c/msys64/mingw64/bin/*.dll out/dist/bin
# If you know which libraries you need (use https://www.dependencywalker.com/ to find them):
cp ~/.wine/drive_c/msys64/mingw64/bin/{libadwaita-1-0.dll,libbrotlicommon.dll,libbrotlidec.dll,libbz2-1.dll,libcairo-2.dll,libcairo-gobject-2.dll,libcairo-script-interpreter-2.dll,libdatrie-1.dll,libdeflate.dll,libepoxy-0.dll,libexpat-1.dll,libffi-7.dll,libfontconfig-1.dll,libfreetype-6.dll,libfribidi-0.dll,libgcc_s_seh-1.dll,libgdk_pixbuf-2.0-0.dll,libgio-2.0-0.dll,libglib-2.0-0.dll,libgmodule-2.0-0.dll,libgobject-2.0-0.dll,libgraphene-1.0-0.dll,libgraphite2.dll,libgtk-4-1.dll,libharfbuzz-0.dll,libiconv-2.dll,libintl-8.dll,libjbig-0.dll,libjpeg-8.dll,libLerc.dll,liblzma-5.dll,liblzo2-2.dll,libpango-1.0-0.dll,libpangocairo-1.0-0.dll,libpangoft2-1.0-0.dll,libpangowin32-1.0-0.dll,libpcre-1.dll,libpixman-1-0.dll,libpng16-16.dll,libstdc++-6.dll,libthai-0.dll,libtiff-5.dll,libwebp-7.dll,libwinpthread-1.dll,libzstd.dll,zlib1.dll} out/dist/bin
cd out/dist || exit
zip -FSr ../vintangle-gui.exe.zip .
# Run
WINEPATH="${PWD}" wine64 /bin/vintangle-gui.exe # Or try on a real Windows machine if it doesn't work here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment