From 86622e0c311a80bb26666688b88b8c5d853119e5 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Thu, 22 Jun 2023 01:48:02 +0200 Subject: Make cross-compilation scripts work from MSYS2 This is weird and runs very slowly. Meson can also find libraries outside the subroot, in particular the fast float plugin. --- README.adoc | 2 +- meson.build | 5 +- msys2-configure.sh | 132 +++++++++++++++++++++++++++++++++++++++++++++++ msys2-cross-configure.sh | 123 ------------------------------------------- msys2-cross-install.sh | 69 ------------------------- msys2-install.sh | 76 +++++++++++++++++++++++++++ 6 files changed, 212 insertions(+), 195 deletions(-) create mode 100755 msys2-configure.sh delete mode 100755 msys2-cross-configure.sh delete mode 100755 msys2-cross-install.sh create mode 100755 msys2-install.sh diff --git a/README.adoc b/README.adoc index 0e36c48..a06b97c 100644 --- a/README.adoc +++ b/README.adoc @@ -64,7 +64,7 @@ from a context menu. Windows ~~~~~~~ 'fiv' can be cross-compiled for Windows, provided that you install a bunch of -dependencies listed at the beginning of 'msys2-cross-configure.sh', +dependencies listed at the beginning of 'msys2-configure.sh', plus rsvg-convert from librsvg2, and icotool from icoutils. Beware that the build will take up about a gigabyte of disk space. diff --git a/meson.build b/meson.build index e1d9e5f..50daf3d 100644 --- a/meson.build +++ b/meson.build @@ -326,10 +326,11 @@ if not win32 meson.add_install_script(updater, skip_if_destdir : dynamic_desktops) endif elif meson.is_cross_build() + # Note that even compiling /from within MSYS2/ can still be a cross-build. msys2_root = meson.get_external_property('msys2_root') - meson.add_install_script('msys2-cross-install.sh', msys2_root) + meson.add_install_script('msys2-install.sh', msys2_root) - # This is the minimum to run targets from msys2-cross-configure.sh builds. + # This is the minimum to run targets from msys2-configure.sh builds. meson.add_devenv({ 'WINEPATH' : msys2_root / 'bin', 'XDG_DATA_DIRS' : msys2_root / 'share', diff --git a/msys2-configure.sh b/msys2-configure.sh new file mode 100755 index 0000000..53ccdc5 --- /dev/null +++ b/msys2-configure.sh @@ -0,0 +1,132 @@ +#!/bin/sh -e +# msys2-configure.sh: set up an MSYS2-based Meson build targeting x86-64. +# Dependencies: AWK, sed, sha256sum, cURL, bsdtar, +# wine64, Meson, mingw-w64-binutils, mingw-w64-gcc, pkg-config +repository=https://repo.msys2.org/mingw/mingw64/ + +# Support running directly from within MSYS2 on Windows. +if [ -n "$MSYSTEM" ] +then + wine64() { "$@"; } + awk() { command awk -v RS='\r?\n' "$@"; } +fi + +status() { + echo "$(tput bold)-- $*$(tput sgr0)" +} + +dbsync() { + status Fetching repository DB + [ -f db.tsv ] || curl -# "$repository/mingw64.db" | bsdtar -xOf- | awk ' + function flush() { print f["%NAME%"] f["%FILENAME%"] f["%DEPENDS%"] } + NR > 1 && $0 == "%FILENAME%" { flush(); for (i in f) delete f[i] } + !/^[^%]/ { field = $0; next } { f[field] = f[field] $0 "\t" } + field == "%SHA256SUM%" { path = "*packages/" f["%FILENAME%"] + sub(/\t$/, "", path); print $0, path > "db.sums" } END { flush() } + ' > db.tsv +} + +fetch() { + status Resolving "$@" + mkdir -p packages + awk -F'\t' 'function get(name, i, a) { + if (visited[name]++ || !(name in filenames)) return + print filenames[name]; split(deps[name], a); for (i in a) get(a[i]) + } BEGIN { while ((getline < "db.tsv") > 0) { + filenames[$1] = $2; deps[$1] = ""; for (i = 3; i <= NF; i++) { + gsub(/[<=>].*/, "", $i); deps[$1] = deps[$1] $i FS } + } for (i = 0; i < ARGC; i++) get(ARGV[i]) }' "$@" | while IFS= read -r name + do + status Fetching "$name" + [ -f "packages/$name" ] || curl -#o "packages/$name" "$repository/$name" + done + + version=$(curl -# https://exiftool.org/ver.txt) + name=exiftool-$version.tar.gz remotename=Image-ExifTool-$version.tar.gz + status Fetching "$remotename" + [ -f "$name" ] || curl -#o "$name" "https://exiftool.org/$remotename" + ln -sf "$name" exiftool.tar.gz +} + +verify() { + status Verifying checksums + sha256sum --ignore-missing --quiet -c db.sums +} + +extract() { + status Extracting packages + for subdir in * + do [ -d "$subdir" -a "$subdir" != packages ] && rm -rf -- "$subdir" + done + for i in packages/* + do bsdtar -xf "$i" --strip-components 1 mingw64 + done + + bsdtar -xf exiftool.tar.gz + mv Image-ExifTool-*/exiftool bin + mv Image-ExifTool-*/lib/* lib/perl5/site_perl + rm -rf Image-ExifTool-* +} + +configure() { + # Don't let GLib programs inherit wrong paths from the environment. + export XDG_DATA_DIRS=$msys2_root/share + + status Configuring packages + wine64 bin/update-mime-database.exe share/mime + wine64 bin/glib-compile-schemas.exe share/glib-2.0/schemas + wine64 bin/gdk-pixbuf-query-loaders.exe \ + > lib/gdk-pixbuf-2.0/2.10.0/loaders.cache +} + +setup() { + status Setting up Meson + [ -n "$MSYSTEM" ] && wrap=false || wrap=true + + cat >"$toolchain" <<-EOF + [binaries] + c = 'x86_64-w64-mingw32-gcc' + cpp = 'x86_64-w64-mingw32-g++' + ar = 'x86_64-w64-mingw32-gcc-ar' + ranlib = 'x86_64-w64-mingw32-gcc-ranlib' + strip = 'x86_64-w64-mingw32-strip' + windres = 'x86_64-w64-mingw32-windres' + pkgconfig = 'pkg-config' + + [properties] + sys_root = '$builddir' + msys2_root = '$msys2_root' + pkg_config_libdir = '$msys2_root/share/pkgconfig:$msys2_root/lib/pkgconfig' + needs_exe_wrapper = $wrap + + [host_machine] + system = 'windows' + cpu_family = 'x86_64' + cpu = 'x86_64' + endian = 'little' + EOF + + meson setup --buildtype=debugoptimized --prefix="$packagedir" \ + --bindir . --libdir . --cross-file="$toolchain" "$builddir" "$sourcedir" +} + +sourcedir=$(realpath "${2:-$(dirname "$0")}") +builddir=$(realpath "${1:-builddir}") +packagedir=$builddir/package +toolchain=$builddir/msys2-cross-toolchain.meson + +# This directory name matches the prefix in .pc files, so we don't need to +# modify them (pkgconf has --prefix-variable, but Meson can't pass that option). +msys2_root=$builddir/mingw64 + +mkdir -p "$msys2_root" +cd "$msys2_root" +dbsync +fetch mingw-w64-x86_64-gtk3 mingw-w64-x86_64-lcms2 \ + mingw-w64-x86_64-libraw mingw-w64-x86_64-libheif \ + mingw-w64-x86_64-perl mingw-w64-x86_64-perl-win32-api \ + mingw-w64-x86_64-libwinpthread-git # Because we don't do "provides"? +verify +extract +configure +setup diff --git a/msys2-cross-configure.sh b/msys2-cross-configure.sh deleted file mode 100755 index b2ea2d5..0000000 --- a/msys2-cross-configure.sh +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/sh -e -# msys2-cross-configure.sh: set up an MSYS2-based cross-compiled Meson build. -# Dependencies: AWK, sed, sha256sum, cURL, bsdtar, -# wine64, Meson, mingw-w64-binutils, mingw-w64-gcc, pkg-config -repository=https://repo.msys2.org/mingw/mingw64/ - -status() { - echo "$(tput bold)-- $*$(tput sgr0)" -} - -dbsync() { - status Fetching repository DB - [ -f db.tsv ] || curl -# "$repository/mingw64.db" | bsdtar -xOf- | awk ' - function flush() { print f["%NAME%"] f["%FILENAME%"] f["%DEPENDS%"] } - NR > 1 && $0 == "%FILENAME%" { flush(); for (i in f) delete f[i] } - !/^[^%]/ { field = $0; next } { f[field] = f[field] $0 "\t" } - field == "%SHA256SUM%" { path = "*packages/" f["%FILENAME%"] - sub(/\t$/, "", path); print $0, path > "db.sums" } END { flush() } - ' > db.tsv -} - -fetch() { - status Resolving "$@" - mkdir -p packages - awk -F'\t' 'function get(name, i, a) { - if (visited[name]++ || !(name in filenames)) return - print filenames[name]; split(deps[name], a); for (i in a) get(a[i]) - } BEGIN { while ((getline < "db.tsv") > 0) { - filenames[$1] = $2; deps[$1] = ""; for (i = 3; i <= NF; i++) { - gsub(/[<=>].*/, "", $i); deps[$1] = deps[$1] $i FS } - } for (i = 0; i < ARGC; i++) get(ARGV[i]) }' "$@" | while IFS= read -r name - do - status Fetching "$name" - [ -f "packages/$name" ] || curl -#o "packages/$name" "$repository/$name" - done - - version=$(curl -# https://exiftool.org/ver.txt) - name=exiftool-$version.tar.gz remotename=Image-ExifTool-$version.tar.gz - status Fetching "$remotename" - [ -f "$name" ] || curl -#o "$name" "https://exiftool.org/$remotename" - ln -sf "$name" exiftool.tar.gz -} - -verify() { - status Verifying checksums - sha256sum --ignore-missing --quiet -c db.sums -} - -extract() { - status Extracting packages - for subdir in * - do [ -d "$subdir" -a "$subdir" != packages ] && rm -rf -- "$subdir" - done - for i in packages/* - do bsdtar -xf "$i" --strip-components 1 mingw64 - done - - bsdtar -xf exiftool.tar.gz - mv Image-ExifTool-*/exiftool bin - mv Image-ExifTool-*/lib/* lib/perl5/site_perl - rm -rf Image-ExifTool-* -} - -configure() { - # Don't let GLib programs inherit wrong paths from the environment. - export XDG_DATA_DIRS=$msys2_root/share - - status Configuring packages - wine64 bin/update-mime-database.exe share/mime - wine64 bin/glib-compile-schemas.exe share/glib-2.0/schemas - wine64 bin/gdk-pixbuf-query-loaders.exe \ - > lib/gdk-pixbuf-2.0/2.10.0/loaders.cache -} - -setup() { - status Setting up Meson - cat >"$toolchain" <<-EOF - [binaries] - c = 'x86_64-w64-mingw32-gcc' - cpp = 'x86_64-w64-mingw32-g++' - ar = 'x86_64-w64-mingw32-gcc-ar' - ranlib = 'x86_64-w64-mingw32-gcc-ranlib' - strip = 'x86_64-w64-mingw32-strip' - windres = 'x86_64-w64-mingw32-windres' - pkgconfig = 'pkg-config' - - [properties] - sys_root = '$builddir' - msys2_root = '$msys2_root' - pkg_config_libdir = '$msys2_root/share/pkgconfig:$msys2_root/lib/pkgconfig' - needs_exe_wrapper = true - - [host_machine] - system = 'windows' - cpu_family = 'x86_64' - cpu = 'x86_64' - endian = 'little' - EOF - - meson setup --buildtype=debugoptimized --prefix="$packagedir" \ - --bindir . --libdir . --cross-file="$toolchain" "$builddir" "$sourcedir" -} - -sourcedir=$(realpath "${2:-$(dirname "$0")}") -builddir=$(realpath "${1:-builddir}") -packagedir=$builddir/package -toolchain=$builddir/msys2-cross-toolchain.meson - -# This directory name matches the prefix in .pc files, so we don't need to -# modify them (pkgconf has --prefix-variable, but Meson can't pass that option). -msys2_root=$builddir/mingw64 - -mkdir -p "$msys2_root" -cd "$msys2_root" -dbsync -fetch mingw-w64-x86_64-gtk3 mingw-w64-x86_64-lcms2 \ - mingw-w64-x86_64-libraw mingw-w64-x86_64-libheif \ - mingw-w64-x86_64-perl mingw-w64-x86_64-perl-win32-api \ - mingw-w64-x86_64-libwinpthread-git # Because we don't do "provides"? -verify -extract -configure -setup diff --git a/msys2-cross-install.sh b/msys2-cross-install.sh deleted file mode 100755 index 7cbc2cc..0000000 --- a/msys2-cross-install.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/sh -e -export LC_ALL=C -cd "$MESON_INSTALL_DESTDIR_PREFIX" -msys2_root=$1 - -# Copy binaries we directly or indirectly depend on. -cp -p "$msys2_root"/bin/*.dll . -cp -p "$msys2_root"/bin/wperl.exe . -cp -p "$msys2_root"/bin/exiftool . -# The console helper is only useful for debug builds. -cp -p "$msys2_root"/bin/gspawn-*-helper*.exe . -cp -pR "$msys2_root"/etc/ . - -mkdir -p lib -cp -pR "$msys2_root"/lib/gdk-pixbuf-2.0/ lib -cp -pR "$msys2_root"/lib/perl5/ lib -mkdir -p share/glib-2.0/schemas -cp -pR "$msys2_root"/share/glib-2.0/schemas/*.Settings.* share/glib-2.0/schemas -mkdir -p share -cp -pR "$msys2_root"/share/mime/ share -mkdir -p share/icons -cp -pR "$msys2_root"/share/icons/Adwaita/ share/icons -mkdir -p share/icons/hicolor -cp -p "$msys2_root"/share/icons/hicolor/index.theme share/icons/hicolor - -# Remove unreferenced libraries. -find lib -name '*.a' -exec rm -- {} + -awk 'function whitelist(binary) { - if (seen[binary]++) - return - - delete orphans[binary] - while (("strings -a \"" binary "\" 2>/dev/null" | getline string) > 0) - if (match(string, /[-.+_a-zA-Z0-9]+[.][Dd][Ll][Ll]$/)) - whitelist("./" substr(string, RSTART, RLENGTH)) -} BEGIN { - while (("find . -type f -path \"./*.[Dd][Ll][Ll]\"" | getline) > 0) - orphans[$0]++ - while (("find . -type f -path \"./*.[Ee][Xx][Ee]\"" | getline) > 0) - whitelist($0) - while (("find ./lib -type f -path \"./*.[Dd][Ll][Ll]\"" | getline) > 0) - whitelist($0) - for (library in orphans) - print library -}' | xargs rm -- - -# Removes unused icons from the Adwaita theme. It could be even more aggressive, -# since it keeps around lots of sizes and all the GTK+ stock icons. -find share/icons/Adwaita -type f | awk 'BEGIN { - while (("grep -aho \"[a-z][a-z-]*\" *.dll *.exe" | getline) > 0) - good[$0] = 1 -} /[.](png|svg|cur|ani)$/ { - # Cut out the basename without extensions. - match($0, /[^\/]+$/) - base = substr($0, RSTART) - sub(/[.].+$/, "", base) - - # Try matching while cutting off suffixes. - # Disregarding the not-much-used GTK_ICON_LOOKUP_GENERIC_FALLBACK. - while (!(keep = good[base]) && - sub(/-(ltr|rtl|symbolic)$/, "", base)) {} - if (!keep) - print -}' | xargs rm -- - -wine64 "$msys2_root"/bin/glib-compile-schemas.exe share/glib-2.0/schemas - -# This may speed up program start-up a little bit. -wine64 "$msys2_root"/bin/gtk-update-icon-cache-3.0.exe share/icons/Adwaita diff --git a/msys2-install.sh b/msys2-install.sh new file mode 100755 index 0000000..abb284f --- /dev/null +++ b/msys2-install.sh @@ -0,0 +1,76 @@ +#!/bin/sh -e +export LC_ALL=C +cd "$MESON_INSTALL_DESTDIR_PREFIX" +msys2_root=$1 + +# Support running directly from within MSYS2 on Windows. +if [ -n "$MSYSTEM" ] +then + wine64() { "$@"; } + awk() { command awk -v RS='\r?\n' "$@"; } +fi + +# Copy binaries we directly or indirectly depend on. +cp -p "$msys2_root"/bin/*.dll . +cp -p "$msys2_root"/bin/wperl.exe . +cp -p "$msys2_root"/bin/exiftool . +# The console helper is only useful for debug builds. +cp -p "$msys2_root"/bin/gspawn-*-helper*.exe . +cp -pR "$msys2_root"/etc/ . + +mkdir -p lib +cp -pR "$msys2_root"/lib/gdk-pixbuf-2.0/ lib +cp -pR "$msys2_root"/lib/perl5/ lib +mkdir -p share/glib-2.0/schemas +cp -pR "$msys2_root"/share/glib-2.0/schemas/*.Settings.* share/glib-2.0/schemas +mkdir -p share +cp -pR "$msys2_root"/share/mime/ share +mkdir -p share/icons +cp -pR "$msys2_root"/share/icons/Adwaita/ share/icons +mkdir -p share/icons/hicolor +cp -p "$msys2_root"/share/icons/hicolor/index.theme share/icons/hicolor + +# Remove unreferenced libraries. +find lib -name '*.a' -exec rm -- {} + +awk 'function whitelist(binary) { + if (seen[binary]++) + return + + delete orphans[binary] + while (("strings -a \"" binary "\" 2>/dev/null" | getline string) > 0) + if (match(string, /[-.+_a-zA-Z0-9]+[.][Dd][Ll][Ll]$/)) + whitelist("./" substr(string, RSTART, RLENGTH)) +} BEGIN { + while (("find . -type f -path \"./*.[Dd][Ll][Ll]\"" | getline) > 0) + orphans[$0]++ + while (("find . -type f -path \"./*.[Ee][Xx][Ee]\"" | getline) > 0) + whitelist($0) + while (("find ./lib -type f -path \"./*.[Dd][Ll][Ll]\"" | getline) > 0) + whitelist($0) + for (library in orphans) + print library +}' | xargs rm -- + +# Removes unused icons from the Adwaita theme. It could be even more aggressive, +# since it keeps around lots of sizes and all the GTK+ stock icons. +find share/icons/Adwaita -type f | awk 'BEGIN { + while (("grep -aho \"[a-z][a-z-]*\" *.dll *.exe" | getline) > 0) + good[$0] = 1 +} /[.](png|svg|cur|ani)$/ { + # Cut out the basename without extensions. + match($0, /[^\/]+$/) + base = substr($0, RSTART) + sub(/[.].+$/, "", base) + + # Try matching while cutting off suffixes. + # Disregarding the not-much-used GTK_ICON_LOOKUP_GENERIC_FALLBACK. + while (!(keep = good[base]) && + sub(/-(ltr|rtl|symbolic)$/, "", base)) {} + if (!keep) + print +}' | xargs rm -- + +wine64 "$msys2_root"/bin/glib-compile-schemas.exe share/glib-2.0/schemas + +# This may speed up program start-up a little bit. +wine64 "$msys2_root"/bin/gtk-update-icon-cache-3.0.exe share/icons/Adwaita -- cgit v1.2.3-70-g09d2