Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub action and Makefile target for Windows installer #4341

Merged
merged 5 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/installer-for-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# GitHub actions - Create Tesseract installer for Windows

name: Cross build for Windows

on:
# Trigger workflow in GitHub web frontend or from API.
workflow_dispatch:
inputs:
targets:
description: 'Target operating system'
required: true
default: 'Windows (64 bit)'
type: choice
options:
- 'Windows (64 bit)'

jobs:
build64:
runs-on: [ubuntu-24.04]
steps:
- uses: actions/checkout@v4
- name: Build Tesseract installer (64 bit)
run: nsis/build.sh x86_64
- uses: actions/upload-artifact@v4
with:
name: Tesseract Installer for Windows (64 bit)
path: dist
54 changes: 54 additions & 0 deletions .github/workflows/pkg-config-crosswrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#! /bin/sh
# pkg-config wrapper for cross-building
# Sets pkg-config search path to search multiarch and historical cross-compiling paths.

# If the user has already set PKG_CONFIG_LIBDIR, believe it (even if empty):
# it's documented to be an override
if [ x"${PKG_CONFIG_LIBDIR+set}" = x ]; then
# GNU triplet for the compiler, e.g. i486-linux-gnu for Debian i386,
# i686-linux-gnu for Ubuntu i386
basename="$(basename "$0")"
triplet="${basename%-pkg-config}"
# Normalized multiarch path if any, e.g. i386-linux-gnu for i386
dpkg-architecture >/dev/null 2>&1
if [ "$?" != 0 ]; then
# dpkg-architecture is missing.
echo "Please install dpkg-dev to use pkg-config when cross-building" >&2
exit 1
fi
multiarch="$(dpkg-architecture -t"${triplet}" -qDEB_HOST_MULTIARCH 2>/dev/null)"
# Native multiarch path
native_multiarch="$(cat /usr/lib/pkg-config.multiarch)"

# This can be used for native builds as well, in that case, just exec pkg-config "$@" directly.
if [ "$native_multiarch" = "$multiarch" ]; then
exec pkg-config "$@"
fi

PKG_CONFIG_LIBDIR="/usr/local/${triplet}/lib/pkgconfig"
# For a native build we would also want to append /usr/local/lib/pkgconfig
# at this point; but this is a cross-building script, so don't
PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/local/share/pkgconfig"

if [ -n "$multiarch" ]; then
PKG_CONFIG_LIBDIR="/usr/local/lib/${multiarch}/pkgconfig:$PKG_CONFIG_LIBDIR"
PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/lib/${multiarch}/pkgconfig"
fi

PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/${triplet}/lib/pkgconfig"
# For a native build we would also want to append /usr/lib/pkgconfig
# at this point; but this is a cross-building script, so don't
# If you want to allow use of un-multiarched -dev packages for crossing
# (at the risk of finding build-arch stuff you didn't want, if not in a clean chroot)
# Uncomment the next line:
# PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/lib/pkgconfig"
# ... but on Ubuntu we rely cross-building with non-multiarch libraries:
if dpkg-vendor --derives-from Ubuntu; then
PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/lib/pkgconfig"
fi
PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/share/pkgconfig"

export PKG_CONFIG_LIBDIR
fi

exec pkg-config "$@"
8 changes: 8 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ ACLOCAL_AMFLAGS = -I m4
CLEANFILES =

SUBDIRS = . tessdata
if MINGW
SUBDIRS += nsis
endif

EXTRA_DIST = README.md LICENSE
EXTRA_DIST += aclocal.m4 config configure.ac autogen.sh
Expand Down Expand Up @@ -50,6 +53,11 @@ doc-pack: doc
doc-clean:
rm -rf $(top_builddir)/doc/html/*

if MINGW
winsetup: training ScrollView.jar
@cd "$(top_builddir)/nsis" && $(MAKE) winsetup
endif

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = tesseract.pc

Expand Down
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ AC_CONFIG_HEADERS([include/config_auto.h:config/config.h.in])

# default conditional
AM_CONDITIONAL([T_WIN], false)
AM_CONDITIONAL([MINGW], false)
AM_CONDITIONAL([GRAPHICS_DISABLED], false)
AC_SUBST([AM_CPPFLAGS])

Expand All @@ -87,7 +88,9 @@ AM_SILENT_RULES([yes])
AC_CANONICAL_HOST
case "${host_os}" in
mingw*)
AC_DEFINE_UNQUOTED([MINGW], 1, [This is a MinGW system])
AM_CONDITIONAL([T_WIN], true)
AM_CONDITIONAL([MINGW], true)
AM_CONDITIONAL([ADD_RT], false)
AC_SUBST([AM_LDFLAGS], ['-no-undefined'])
;;
Expand Down Expand Up @@ -559,6 +562,7 @@ AC_CONFIG_FILES([java/com/google/Makefile])
AC_CONFIG_FILES([java/com/google/scrollview/Makefile])
AC_CONFIG_FILES([java/com/google/scrollview/events/Makefile])
AC_CONFIG_FILES([java/com/google/scrollview/ui/Makefile])
AC_CONFIG_FILES([nsis/Makefile])
AC_OUTPUT

# Final message
Expand Down
22 changes: 22 additions & 0 deletions nsis/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
AUTOMAKE_OPTIONS = subdir-objects

all:

if MINGW

gitrev="$(shell git --git-dir=${abs_top_srcdir}/.git --work-tree=${abs_top_srcdir} describe --always --tags | sed s/^v//)"

.PHONY: winsetup

Plugins/x86-unicode/INetC.dll:
curl -O https://nsis.sourceforge.io/mediawiki/images/c/c9/Inetc.zip
unzip Inetc.zip $@

winpath.exe: winpath.cpp
x86_64-w64-mingw32-g++ -Os -o $@ $<
x86_64-w64-mingw32-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

endif
118 changes: 118 additions & 0 deletions nsis/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash

# GitHub actions - Create Tesseract installer for Windows

# Author: Stefan Weil (2010-2024)

set -e
set -x

LANG=C.UTF-8

ARCH=$1

if [ "$ARCH" = "i686" ]; then
MINGW=/mingw32
else
ARCH=x86_64
MINGW=/mingw64
fi

ROOTDIR=$PWD
DISTDIR=$ROOTDIR/dist
HOST=$ARCH-w64-mingw32
TAG=$(cat VERSION).$(date +%Y%m%d)
BUILDDIR=bin/ndebug/$HOST-$TAG
PKG_ARCH=mingw-w64-${ARCH/_/-}

# Install packages.
sudo apt-get update --quiet
sudo apt-get install --assume-yes --no-install-recommends --quiet \
asciidoc xsltproc docbook-xml docbook-xsl \
automake dpkg-dev libtool pkg-config default-jdk-headless \
mingw-w64-tools nsis g++-"$PKG_ARCH" \
makepkg pacman-package-manager

# Install pacman-package-manager and its dependencies (from Ubuntu 22.10).
# sudo curl -Os http://de.archive.ubuntu.com/ubuntu/pool/universe/p/pacman-package-manager/pacman-package-manager_6.0.1-4_amd64.deb
# sudo curl -Os http://de.archive.ubuntu.com/ubuntu/pool/universe/p/pacman-package-manager/libalpm13_6.0.1-4_amd64.deb
# sudo curl -Os http://de.archive.ubuntu.com/ubuntu/pool/universe/p/pacman-package-manager/makepkg_6.0.1-4_amd64.deb
# sudo dpkg -i *.deb || true
# sudo apt-get install --fix-broken --assume-yes --no-install-recommends --quiet

# 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 -Os https://raw.githubusercontent.com/msys2/MSYS2-keyring/master/msys2.gpg
sudo curl -Os https://raw.githubusercontent.com/msys2/MSYS2-keyring/master/msys2-revoked
sudo curl -Os 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 -O https://raw.githubusercontent.com/msys2/MSYS2-packages/master/pacman-mirrors/mirrorlist.mingw
# sudo curl -O 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

sudo ln -sf "$PWD/.github/workflows/pkg-config-crosswrapper" "/usr/bin/$HOST-pkg-config"

git config --global user.email "[email protected]"
git config --global user.name "Stefan Weil"
git tag -a "v$TAG" -m "Tesseract $TAG"

# Run autogen.
./autogen.sh

# Build Tesseract installer.
mkdir -p "$BUILDDIR" && cd "$BUILDDIR"

# Run configure.
PKG_CONFIG_PATH=$MINGW/lib/pkgconfig
export PKG_CONFIG_PATH
# 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"

make all training
MINGW_INSTALL=${PWD}${MINGW}
make install-jars install training-install html prefix="$MINGW_INSTALL"
test -d venv || python3 -m venv venv
source venv/bin/activate
pip install pefile
mkdir -p dll
ln -sv $("$ROOTDIR/nsis/find_deps.py" "$MINGW_INSTALL"/bin/*.exe "$MINGW_INSTALL"/bin/*.dll) dll/
make winsetup prefix="$MINGW_INSTALL"

# Copy result for upload.
mkdir -p "$DISTDIR" && cp nsis/tesseract-ocr-w*-setup-*.exe "$DISTDIR"
64 changes: 64 additions & 0 deletions nsis/find_deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
#
# Copyright (C) 2024 Stefan Weil
#
# SPDX-License-Identifier: MIT
#
# Find the DLL files which are required for a given set of
# Windows executables and libraries.

import argparse
import os
import pefile

VERBOSE = False

def find_dependencies(binary, search_path, analyzed_deps):
pe = pefile.PE(binary)
pe.parse_data_directories()
if VERBOSE:
print(f'{binary}:')
# print(pe.dump_info())

for entry in pe.DIRECTORY_ENTRY_IMPORT:
name = entry.dll.decode('utf-8')
if name in analyzed_deps:
if VERBOSE:
print(f'skip {name} (already analyzed)')
continue
analyzed_deps.add(name)
fullpath = os.path.join(search_path, name)
if not os.path.exists(fullpath):
# Not found, maybe system DLL. Skip it.
if VERBOSE:
print(f'skip {name} (not found, maybe system DLL)')
continue
print(fullpath)
analyzed_deps = find_dependencies(fullpath, search_path, analyzed_deps)

return analyzed_deps

def main():
"""
Command-line interface for universal dependency scanner.
"""

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/',
help='path to dll files')

args = parser.parse_args()

# try:
# Find dependencies
analyzed_deps = set()
for binary in args.files:
if True:
analyzed_deps = find_dependencies(binary, args.dlldir, analyzed_deps)
# except:
# print(f'error: failed to find dependencies for {binary}')


if __name__ == '__main__':
main()
Loading
Loading