Commit 0f3db1d2 for tesseract

commit 0f3db1d29474e4d19b38d8196e868fe4464ee7ad
Author: Stefan Weil <sw@weilnetz.de>
Date:   Thu Nov 26 19:35:03 2026 +0100

    ci: Build Windows installer with llvm-mingw and MSYS2 CLANG64 or UCRT64

    MSYS2 deprecated its MINGW64 environment on 2026-03-15 and removes
    MINGW64 builds of leaf packages since 2026-10-24, so the cross build
    of the installer can no longer use the mingw-w64-x86_64 packages.

    The generic part of the build is now done by the action
    https://github.com/stweil/setup-msys2-cross (release v1), which can
    also be used by other projects, or run as a script without GitHub
    Actions. It has a test workflow for all MSYS2 environments on x86_64
    and ARM64 runners:

    - llvm-mingw provides the cross compiler, the linker and the other
      cross tools. No Ubuntu LTS release has a GCC cross compiler for the
      Universal C Runtime. The release archive is checked with SHA-256.
    - pacman installs the libraries from the MSYS2 environment CLANG64
      (default), UCRT64 or CLANGARM64.
    - The MSYS2 directory (for example /clang64) is the sysroot of the
      compiler, so the build uses the same headers, import libraries and
      DLL files as a native build in that environment. For UCRT64 the
      compiler uses libstdc++ and libgcc from the GCC package of MSYS2,
      which is needed for C++ libraries like ICU.
    - Wrapper scripts for the compiler and for pkg-config are added to
      PATH, so that configure only needs --host.
    - libtool before 2.5 omits the builtins library of compiler-rt when it
      links a shared C++ library (undefined symbol ___chkstk_ms).
      The wrapper scripts always pass that library to the linker.

    nsis/build.sh only does the Tesseract part now and gets the host
    triplet and the MSYS2 directory as arguments:

    - The DLL files of the compiler (libc++.dll, or libstdc++-6.dll,
      libgcc_s_seh-1.dll and libwinpthread-1.dll) come from MSYS2 like all
      other DLL files and are no longer replaced by those of an Ubuntu
      compiler.
    - Installed files are stripped with the cross tool. The strip program
      of an ARM64 build machine does not support x86_64 executables.
    - The i686 argument is removed. It could not work any more because the
      list of packages was fixed to x86_64.

    nsis/Makefile.am uses $(CXX) and $(STRIP) for winpath.exe and passes
    the installation prefix to makensis.

    The workflow has a new input for the MSYS2 environment (CLANG64 or
    UCRT64).

    Tested by running the steps of the workflow in an Ubuntu 24.04 root
    file system (aarch64 build machine) for CLANG64 and UCRT64: both give
    an installer, all DLL imports of the installed files are satisfied, and
    tesseract.exe (OCR, PDF output) and the training tools run with Wine.
    The workflow was run on GitHub for CLANG64. The installer program was
    not run.

    Assisted-by: Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/.github/workflows/installer-for-windows.yml b/.github/workflows/installer-for-windows.yml
index fd79d932..1b7b0972 100644
--- a/.github/workflows/installer-for-windows.yml
+++ b/.github/workflows/installer-for-windows.yml
@@ -13,14 +13,33 @@ on:
         type: choice
         options:
           - 'Windows (64 bit)'
+      msystem:
+        description: 'MSYS2 environment which provides the libraries'
+        required: true
+        default: 'CLANG64'
+        type: choice
+        options:
+          - 'CLANG64'
+          - 'UCRT64'

 jobs:
   build64:
     runs-on: [ubuntu-24.04]
     steps:
     - uses: actions/checkout@v7
+    - name: Install llvm-mingw and MSYS2 packages
+      id: cross
+      uses: stweil/setup-msys2-cross@v1
+      with:
+        msystem: ${{ inputs.msystem }}
+        packages: >-
+          curl-winssl giflib icu leptonica libarchive libidn2 openjpeg2
+          openssl pango libpng libtiff libwebp
     - name: Build Tesseract installer (64 bit)
-      run: nsis/build.sh x86_64
+      run: nsis/build.sh "$HOST" "$PREFIX"
+      env:
+        HOST: ${{ steps.cross.outputs.host }}
+        PREFIX: ${{ steps.cross.outputs.prefix }}
     - uses: actions/upload-artifact@v7
       with:
         name: Tesseract Installer for Windows (64 bit)
diff --git a/nsis/Makefile.am b/nsis/Makefile.am
index 917f1431..41e28d85 100644
--- a/nsis/Makefile.am
+++ b/nsis/Makefile.am
@@ -13,10 +13,10 @@ Plugins/x86-unicode/INetC.dll:
 	unzip Inetc.zip $@

 winpath.exe: winpath.cpp
-	x86_64-w64-mingw32-g++ -Os -o $@ $<
-	x86_64-w64-mingw32-strip --strip-unneeded $@
+	$(CXX) -Os -o $@ $<
+	$(STRIP) --strip-unneeded $@

 winsetup: Plugins/x86-unicode/INetC.dll winpath.exe
-	makensis -DCROSSBUILD -DSHARED -DSIGNCODE=$(SIGNCODE) -DSRCDIR=$(top_srcdir) -DVERSION=${gitrev} $(shell test "$(host_cpu)" = x86_64 && echo "-DW64") -NOCD $(top_srcdir)/nsis/tesseract.nsi
+	makensis -DCROSSBUILD -DSHARED -DSIGNCODE=$(SIGNCODE) -DSRCDIR=$(top_srcdir) -DPREFIX=$(prefix) -DVERSION=${gitrev} $(shell test "$(host_cpu)" = x86_64 && echo "-DW64") -NOCD $(top_srcdir)/nsis/tesseract.nsi

 endif
diff --git a/nsis/build.sh b/nsis/build.sh
index 0060aaf7..8fd2071a 100755
--- a/nsis/build.sh
+++ b/nsis/build.sh
@@ -4,76 +4,46 @@

 # Author: Stefan Weil (2010-2026)

+# Usage: nsis/build.sh [HOST [PREFIX]]
+#
+# The cross compiler for HOST and the libraries for Windows in PREFIX
+# must be installed before this script is run. The GitHub workflow uses
+# the action https://github.com/stweil/setup-msys2-cross for that. Without
+# GitHub Actions, the script setup.sh from that repository does the same.
+# For the MSYS2 environment CLANG64 it is run like this:
+#
+#     setup.sh CLANG64 \
+#       curl-winssl giflib icu leptonica libarchive libidn2 openjpeg2 \
+#       openssl pango libpng libtiff libwebp
+#
+# It also shows the directories which must be added to PATH.
+
 set -e
 set -x

 LANG=C.UTF-8

-ARCH=$1
-
-if [ "$ARCH" = "i686" ]; then
-  MINGW=/mingw32
-else
-  ARCH=x86_64
-  MINGW=/mingw64
-fi
+# Host triplet of the cross compiler.
+HOST=${1:-x86_64-w64-mingw32}
+# Installation directory of the MSYS2 packages.
+MINGW=${2:-/clang64}

 ROOTDIR=$PWD
-HOST=$ARCH-w64-mingw32
 TAG=$(cat VERSION).$(date +%Y%m%d)
 BUILDDIR=bin/ndebug/$HOST
-PKG_ARCH=mingw-w64-${ARCH/_/-}
+
+if [ "$(id -u)" -eq 0 ]; then
+  SUDO=
+else
+  SUDO=sudo
+fi

 # Install packages.
-sudo apt-get update --quiet
-sudo apt-get install --assume-yes --no-install-recommends --quiet \
+$SUDO apt-get update --quiet
+$SUDO apt-get install --assume-yes --no-install-recommends --quiet \
   asciidoctor ruby-asciidoctor-pdf curl \
-  automake dpkg-dev libtool pkg-config default-jdk-headless \
-  mingw-w64-tools nsis g++-"$PKG_ARCH" \
-  makepkg pacman-package-manager python3-venv unzip
-
-# Configure pacman.
-
-# Enable mirrorlist.
-sudo sed -Ei 's/^#.*(Include.*mirrorlist)/\1/' /etc/pacman.conf
-(
-# Add msys key for pacman.
-cd /usr/share/keyrings
-sudo curl -OsS https://raw.githubusercontent.com/msys2/MSYS2-keyring/master/msys2.gpg
-sudo curl -OsS https://raw.githubusercontent.com/msys2/MSYS2-keyring/master/msys2-revoked
-sudo curl -OsS https://raw.githubusercontent.com/msys2/MSYS2-keyring/master/msys2-trusted
-)
-(
-# Add active environments for pacman.
-# See https://www.msys2.org/docs/repos-mirrors/.
-sudo mkdir -p /etc/pacman.d
-cd /etc/pacman.d
-cat <<eod | sudo tee mirrorlist >/dev/null
-[mingw64]
-Include = /etc/pacman.d/mirrorlist.mingw
-eod
-sudo curl -OsS https://raw.githubusercontent.com/msys2/MSYS2-packages/master/pacman-mirrors/mirrorlist.mingw
-# sudo curl -OsS https://raw.githubusercontent.com/msys2/MSYS2-packages/master/pacman-mirrors/mirrorlist.msys
-)
-
-sudo pacman-key --init
-sudo pacman-key --populate msys2
-sudo pacman -Syu --noconfirm
-
-# Install required pacman packages.
-sudo pacman -S --noconfirm \
- mingw-w64-x86_64-curl-winssl \
- mingw-w64-x86_64-giflib \
- mingw-w64-x86_64-icu \
- mingw-w64-x86_64-leptonica \
- mingw-w64-x86_64-libarchive \
- mingw-w64-x86_64-libidn2 \
- mingw-w64-x86_64-openjpeg2 \
- mingw-w64-x86_64-openssl \
- mingw-w64-x86_64-pango \
- mingw-w64-x86_64-libpng \
- mingw-w64-x86_64-libtiff \
- mingw-w64-x86_64-libwebp
+  automake libtool make pkg-config default-jdk-headless \
+  nsis python3-venv unzip

 git config --global user.email "sw@weilnetz.de"
 git config --global user.name "Stefan Weil"
@@ -86,24 +56,27 @@ git tag -a "v$TAG" -m "Tesseract $TAG"
 mkdir -p "$BUILDDIR" && cd "$BUILDDIR"

 # Run configure.
-PKG_CONFIG_PATH=$MINGW/lib/pkgconfig
-export PKG_CONFIG_PATH
+# The compiler uses $MINGW as sysroot, and $HOST-pkg-config only finds
+# the packages which are installed there, so no search paths are needed.
 # Disable OpenMP (see https://github.com/tesseract-ocr/tesseract/issues/1662).
 ../../../configure --disable-openmp --host="$HOST" --prefix="/usr/$HOST" \
-  CXX="$HOST-g++-posix" \
-  CXXFLAGS="-fno-math-errno -Wall -Wextra -Wpedantic -g -O2 -isystem $MINGW/include" \
-  LDFLAGS="-L$MINGW/lib"
+  CXX="$HOST-clang++" \
+  CXXFLAGS="-fno-math-errno -Wall -Wextra -Wpedantic -g -O2"

 make all -j$(nproc)
 make training -j$(nproc)

 MINGW_INSTALL=${PWD}${MINGW}
-make install-jars install training-install html prefix="$MINGW_INSTALL" INSTALL_STRIP_FLAG=-s
+# Strip the installed files with the cross tool. The strip program of the
+# build machine does not always support Windows executables.
+make install-jars install training-install html prefix="$MINGW_INSTALL" \
+  INSTALL_STRIP_FLAG="-s --strip-program=$HOST-strip"
 test -d venv || python3 -m venv venv
 source venv/bin/activate
 pip install pefile
+# Get the DLL files which are needed for the installer. This includes the
+# DLL files of the compiler (C++ standard library, unwinder, threads).
+# They are part of the MSYS2 environment which was used as sysroot.
 mkdir -p dll
-ln -sv $("$ROOTDIR/nsis/find_deps.py" "$MINGW_INSTALL"/bin/*.exe "$MINGW_INSTALL"/bin/*.dll) dll/
-ln -svf /usr/lib/gcc/x86_64-w64-mingw32/*-win32/libstdc++-6.dll dll/
-ln -svf /usr/lib/gcc/x86_64-w64-mingw32/*-win32/libgcc_s_seh-1.dll dll/
+ln -svf $("$ROOTDIR/nsis/find_deps.py" --dlldir "$MINGW/bin" "$MINGW_INSTALL"/bin/*.exe "$MINGW_INSTALL"/bin/*.dll) dll/
 make winsetup prefix="$MINGW_INSTALL"
diff --git a/nsis/find_deps.py b/nsis/find_deps.py
index 0aa12a91..296b67cc 100755
--- a/nsis/find_deps.py
+++ b/nsis/find_deps.py
@@ -45,7 +45,7 @@ def main():

     parser = argparse.ArgumentParser(description='Find and copy DLL dependencies')
     parser.add_argument('files', nargs='+', help='Paths to executable or library files')
-    parser.add_argument('--dlldir', dest='dlldir', default='/mingw64/bin/',
+    parser.add_argument('--dlldir', dest='dlldir', default='/clang64/bin/',
                         help='path to dll files')

     args = parser.parse_args()
diff --git a/nsis/tesseract.nsi b/nsis/tesseract.nsi
index af28a9b5..2eec37ba 100644
--- a/nsis/tesseract.nsi
+++ b/nsis/tesseract.nsi
@@ -72,7 +72,7 @@ OutFile ${OUTFILE}
 !endif

 !ifndef PREFIX
-!define PREFIX "../mingw64"
+!define PREFIX "../clang64"
 !endif
 !define BINDIR "${PREFIX}/bin"