# Maintainer: Martchus <martchus@gmx.net>

# All my PKGBUILDs are managed at https://github.com/Martchus/PKGBUILDs where
# you also find the URL of a binary repository.

pkgname=mingw-w64-clang-aarch64-qt6-tools-static
_qtver=6.8.0
pkgver=${_qtver/-/}
pkgrel=1
arch=(any)
url='https://www.qt.io'
license=(GPL3 LGPL3 FDL custom)
pkgdesc='A cross-platform application and UI framework (tools, mingw-w64)'
depends=('mingw-w64-clang-aarch64-qt6-base-static')
makedepends=('mingw-w64-clang-aarch64-cmake-static' 'mingw-w64-clang-aarch64-vulkan-headers' 'mingw-w64-clang-aarch64-vulkan-icd-loader' 'mingw-w64-clang-aarch64-qt6-declarative-static' 'qt6-declarative' 'qt6-tools' 'ninja')
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
groups=(mingw-w64-clang-aarch64-qt6)
_pkgfqn="qttools-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfqn}.tar.xz"
        '0001-Enable-only-SQL-plugins-which-are-known-to-work.patch')
sha256sums=('403115d8268503c6cc6e43310c8ae28eb9e605072a5d04e4a2de8b6af39981f7'
            'dcc695dd98d53daa715f3a7a9bfded88ff33f7656ed3f52bdfe0d794c58f8b7d')

_architectures=${MINGW_W64_QT6_ARCHS:-aarch64-w64-mingw32}

prepare () {
  cd $_pkgfqn

  # apply patches; further descriptions can be found in patch files itself
  for patch in "$srcdir/"*.patch; do
    msg2 "Applying patch $patch"
    patch -p1 -i "$patch"
  done
}

build() {
  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    export PKG_CONFIG=/usr/bin/$_arch-pkg-config
    $_arch-cmake-static -G Ninja -B build-$_arch -S $_pkgfqn \
      -DCMAKE_INSTALL_PREFIX:PATH="/usr/$_arch/static" \
      -DCMAKE_DISABLE_FIND_PACKAGE_harfbuzz=TRUE \
      -DFEATURE_static_runtime=ON \
      -DFEATURE_pkg_config=ON \
      -DFEATURE_windeployqt=OFF
    cmake --build build-$_arch
  done
}

package() {
  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    DESTDIR="$pkgdir" cmake --install build-$_arch

    find "$pkgdir/usr/$_arch" -iname '*.exe' -exec $STRIP --strip-all {} \;
    find "$pkgdir/usr/$_arch" -iname '*.dll' -exec $STRIP --strip-unneeded {} \;
    find "$pkgdir/usr/$_arch" -iname '*.a'   -exec $STRIP -g {} \;
    [[ -d "$pkgdir/usr/$_arch/static/share/doc" ]] && rm -r "$pkgdir/usr/$_arch/static/share/doc"
  done

  install -d "$pkgdir"/usr/share/licenses
  ln -s /usr/share/licenses/mingw-w64-clang-aarch64-qt6-base-static "$pkgdir"/usr/share/licenses/$pkgname

}

