# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6
pkgbase=mingw-w64-${_realname}-static
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-static")
_qtver=6.10.1
pkgver=${_qtver/-/}
pkgrel=1
pkgdesc="A cross-platform application and UI framework (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io'
msys2_references=(
  'archlinux: qt6-base'
  "cpe: cpe:/a:qt:qt"
)
license=(spdx:LGPL-3.0-only WITH spdx:Qt-GPL-exception-1.0 AND spdx:GPL-2.0-or-later AND spdx:GPL-3.0-or-later)
depends=("${MINGW_PACKAGE_PREFIX}-vulkan-loader")
optdepends=("${MINGW_PACKAGE_PREFIX}-clang-libs: for lupdate and qdoc")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-xmlstarlet"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-dbus"
             "${MINGW_PACKAGE_PREFIX}-brotli"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             #"${MINGW_PACKAGE_PREFIX}-graphite2"
             #"${MINGW_PACKAGE_PREFIX}-harfbuzz"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             #"${MINGW_PACKAGE_PREFIX}-libmng"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-libwebp"
             #"${MINGW_PACKAGE_PREFIX}-md4c"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-pcre2"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             "rsync")
_pkgfn="${_realname/6/}-everywhere-src-${_qtver}"
_install_prefix=${MINGW_PREFIX}/qt6-static
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/single/${_pkgfn}.tar.xz"
        001-adjust-qmake-conf-mingw.patch
        002-qt-6.2.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        003-qt-6.2.0-dont-add-resource-files-to-qmake-libs.patch
        004-Allow-overriding-CMAKE_FIND_LIBRARY_SUFFIXES-to-pref.patch
        005-qt-6.2.0-win32static-cmake-link-ws2_32-and--static.patch
        006-Fix-finding-D-Bus.patch
        007-Fix-using-static-PCRE2-and-DBus-1.patch
        008-Fix-libjpeg-workaround-for-conflict-with-rpcndr.h.patch
        009-Fix-transitive-dependencies-of-static-libraries.patch
        010-Support-finding-static-MariaDB-client-library.patch
        012-Handle-win64-in-dumpcpp-and-MetaObjectGenerator-read.patch
        013-disable-finding-webp-from-cmake-config-files.patch
        014-imageformats-transitive-dependencies.patch
        015-qt6-windeployqt-fixes.patch)
sha256sums=('0ed08b079719394303cd2054b66b2dc0c5895ceeb88fb6131c18991c980bf00f'
            '26f3bcc3729520a5ba648a9ce92a9293c8fb1cf16d81af612587ce10d501dcb6'
            '817ce7a15eafb945da5f03706cd7196198c9a903c0372cb7023d1e27634355a4'
            'af3f4c6491942ee6a7681eecef2d999897e38fcef7a5edcf3774589f137dac73'
            '9b64d5a03ba6155d16c8105b8dd4bdc7d687170df8e0d5404b4b10292549bde2'
            '554e979085f5deabd9f83637168c79f4929781434164d42dd14a8335a61d3cfc'
            '5b3d7371b26ebcfc2a3d46a36db5dab4f50bb41b234d237021c32d21842a2880'
            '92eae9347dac68c1dfdd79d97fa9ac257ebffcb3364cb7693342dce5a182823b'
            '4b33eb1cd4cf43acd794bc5400a7224c96d6d0936f5589616beea3c470dc2165'
            '394d723e894d08b0b8584c0c0e1e73965f518acc7213e979ffc2907ceaf4bf40'
            '98a04c6fc0e7999f62db44a264ac73919dec4f56f2991ec5596e730d30662e55'
            '39d57493b0edc1317729b88dad6a07b86936072f0fbbd2aae7b01e2211ce29ed'
            'ce0023e8b72fe4b5840ca892bd5ad52120b6b484161bf2ffe0b6da9404625641'
            '1d640ce887bbaaa847b8dd30a1a655f273c9907de06ad9d91580c02215e3e8c8'
            '8201d978f1bca6abf784cd9e0a206c53c6cef7028e3a95712024db075dd7ea18')

# Use the right mkspecs file
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
  _platform=win32-clang-g++
else
  _platform=win32-g++
fi

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"

  sleep 2
  mv ${_pkgfn} qt${pkgver}
  sleep 2

  cd qt${pkgver}

  apply_patch_with_msg \
    001-adjust-qmake-conf-mingw.patch \
    002-qt-6.2.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    003-qt-6.2.0-dont-add-resource-files-to-qmake-libs.patch \
    004-Allow-overriding-CMAKE_FIND_LIBRARY_SUFFIXES-to-pref.patch \
    005-qt-6.2.0-win32static-cmake-link-ws2_32-and--static.patch \
    006-Fix-finding-D-Bus.patch \
    007-Fix-using-static-PCRE2-and-DBus-1.patch \
    008-Fix-libjpeg-workaround-for-conflict-with-rpcndr.h.patch \
    009-Fix-transitive-dependencies-of-static-libraries.patch \
    010-Support-finding-static-MariaDB-client-library.patch \
    012-Handle-win64-in-dumpcpp-and-MetaObjectGenerator-read.patch \
    013-disable-finding-webp-from-cmake-config-files.patch \
    014-imageformats-transitive-dependencies.patch \
    015-qt6-windeployqt-fixes.patch

  local _ARCH_TUNE
  if [[ ${CARCH} == x86_64 ]]; then
    _ARCH_TUNE="-march=nocona -msahf -mtune=generic"
  fi

  BIGOBJ_FLAGS="-Wa,-mbig-obj"

  # Append these ones ..
  sed -i "s|^QMAKE_CFLAGS .*= \(.*\)$|QMAKE_CFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" qtbase/mkspecs/${_platform}/qmake.conf
  sed -i "s|^QMAKE_CXXFLAGS .*= \(.*\)$|QMAKE_CXXFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" qtbase/mkspecs/${_platform}/qmake.conf
}

build() {
  mkdir -p B${MSYSTEM} && cd B${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CC} == clang ]]; then
    CXXFLAGS+=" -Wno-character-conversion"
  fi

  CXXFLAGS+=" -Wno-invalid-constexpr" \
  PKG_CONFIG_ARGN="--static" \
  LDFLAGS+=" -static -static-libgcc -static-libstdc++" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DCMAKE_CONFIGURATION_TYPES=;-DCMAKE_FIND_LIBRARY_SUFFIXES=" \
  ${MINGW_PREFIX}/bin/cmake \
    -Wno-dev \
    --log-level=STATUS \
    -G "Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_FIND_LIBRARY_SUFFIXES_OVERRIDE=".a" \
    -DBUILD_SHARED_LIBS=OFF \
    -DQT_QMAKE_TARGET_MKSPEC=${_platform} \
    -DCMAKE_INSTALL_PREFIX=${_install_prefix} \
    -DINSTALL_BINDIR=bin \
    -DINSTALL_LIBDIR=lib \
    -DINSTALL_INCLUDEDIR=include/qt6 \
    -DINSTALL_ARCHDATADIR=share/qt6 \
    -DINSTALL_DOCDIR=share/doc/qt6 \
    -DINSTALL_DATADIR=share/qt6 \
    -DINSTALL_MKSPECSDIR=share/qt6/mkspecs \
    -DINSTALL_DESCRIPTIONSDIR=share/qt6/modules \
    -DINSTALL_TESTSDIR=share/qt6/tests \
    -DINSTALL_EXAMPLESDIR=share/doc/qt6/examples \
    -DFEATURE_static_runtime=ON \
    -DFEATURE_relocatable=ON \
    -DFEATURE_openssl_linked=ON \
    -DINPUT_openssl=linked \
    -DINPUT_dbus=linked \
    -DINPUT_mng=no \
    -DINPUT_libmd4c=qt \
    -DFEATURE_glib=OFF \
    -DINPUT_quick3d_assimp=qt \
    -DFEATURE_system_assimp=OFF \
    -DFEATURE_system_doubleconversion=OFF \
    -DFEATURE_system_freetype=ON \
    -DFEATURE_system_harfbuzz=ON \
    -DFEATURE_system_jpeg=ON \
    -DFEATURE_system_pcre2=ON \
    -DFEATURE_system_png=ON \
    -DFEATURE_system_sqlite=OFF \
    -DFEATURE_system_tiff=ON \
    -DFEATURE_system_webp=ON \
    -DFEATURE_system_zlib=ON \
    -DFEATURE_opengl=ON \
    -DFEATURE_opengl_desktop=ON \
    -DFEATURE_egl=OFF \
    -DFEATURE_gstreamer=OFF \
    -DFEATURE_icu=OFF \
    -DFEATURE_fontconfig=OFF \
    -DFEATURE_pkg_config=ON \
    -DFEATURE_vulkan=ON \
    -DFEATURE_sql_ibase=OFF \
    -DFEATURE_sql_psql=OFF \
    -DFEATURE_sql_mysql=OFF \
    -DFEATURE_sql_odbc=OFF \
    -DFEATURE_zstd=OFF \
    -DFEATURE_wmf=ON \
    -DQT_BUILD_TESTS=OFF \
    -DQT_BUILD_EXAMPLES=OFF \
    -DOPENSSL_DEPENDENCIES="-lws2_32;-lgdi32;-lcrypt32" \
    -DLIBPNG_DEPENDENCIES="-lz" \
    -DGLIB2_DEPENDENCIES="-lintl;-lws2_32;-lole32;-lwinmm;-lshlwapi;-lm" \
    -DFREETYPE_DEPENDENCIES="-lbz2;-lharfbuzz;-lfreetype;-lbrotlidec;-lbrotlicommon" \
    -DHARFBUZZ_DEPENDENCIES="-lglib-2.0;-lintl;-lws2_32;-lusp10;-lgdi32;-lole32;-lwinmm;-lshlwapi;-lintl;-lm;-lfreetype;-lgraphite2;-lrpcrt4" \
    -DDBUS1_DEPENDENCIES="-lws2_32;-liphlpapi;-ldbghelp" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DOPENSSL_USE_STATIC_LIBS=ON \
    -DZLIB_USE_STATIC_LIBS=ON \
    -DBUILD_qtwebengine=OFF \
    ../qt${pkgver}

  export PATH=$PWD/bin:$PATH

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install B${MSYSTEM}

  install -d "${pkgdir}"${_install_prefix}/share/licenses/${_realname}
  install -Dm644 qt${pkgver}/LICENSES/* -t "${pkgdir}"${_install_prefix}/share/licenses/${_realname}

  # Fix *.pri, *.bat and *.cmake files
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  find "${pkgdir}${_install_prefix}/share/qt6" -type f \( -name '*.pri' -o -name '*.bat' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${_install_prefix}/bin" -type f \( -name '*.bat' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${_install_prefix}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}
