# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

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

_realname=qt5-static
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.15.12
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
msys2_references=(
  'archlinux: qt5-base'
)
pkgdesc="A cross-platform application and UI framework (static) (mingw-w64)"
url='https://www.qt.io/'
license=('spdx:GPL-2.0-only WITH Qt-GPL-exception-1.0 AND GPL-3.0-or-later AND LGPL-2.1-or-later AND LGPL-3.0-only AND GFDL-1.3-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-vulkan"
         "${MINGW_PACKAGE_PREFIX}-z3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-fxc2"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-dbus"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
_pkgfile="qt-everywhere-opensource-src-${pkgver}"
_pkgfqn="qt-everywhere-src-${pkgver}"
noextract=(${_pkgfile}.tar.xz)
source=(https://download.qt.io/official_releases/qt/${pkgver%.*}/${pkgver}/single/${_pkgfile}.tar.xz
        0000-adjust-qmake-conf-mingw.patch
        0001-qt-5.8.0-fix-sql-libraries-mingw.patch
        0002-qt-5.8.0-configure-gcc-before-clang.patch
        0003-enable-avx-support.patch
        0004-fix-linking-again-different-static-libs.patch
        0005-qtconnectivity-add-callback-attribute.patch
        0006-qt-5.3.0-win_flex-replace.patch
        0007-qt-5.3.0-win32-g-Enable-static-builds.patch
        0008-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch
        0016-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch
        0017-qt-5.3.0-fix-examples-building.patch
        0024-qt-5.3.0-icu-add-U_LIB_SUFFIX_C_NAME.patch
        0025-qt-5.8.0-force-using-make-on-msys.patch
        0028-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch
        0029-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch
        0030-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch
        0035-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch
        0036-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch
        0046-qt-5.4.1-Revert-Revert-fix-NTFS-mount-points.patch
        0048-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch
        0049-qt-5.8.0-win32-do-not-use-fontconfig.patch
        0051-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch
        0052-qt-5.11-mingw-fix-link-qdoc-with-clang.patch
        0054-win32-dont-need-flatpack-theme.patch
        0056-qt-5.11-static_icu.patch
        0057-fix-and-enable-iconv-codec.patch
        0060-activeqt-handle-win64-in-dumpcpp.patch
        0062-fix-static-build-big-resources.patch
        0063-enable-mingw-schannel-alpn.patch
        0064-fix-mingw-win32-winnt-detection.patch
        0100-fix-relocatable-prefix-staticbuild-v2.patch
        0125-qt5-windeployqt-fixes.patch
        0300-qt-5.8.0-cast-errors.patch
        0302-ugly-hack-disable-qdoc-build.patch
        0304-qtdeclarative-disable-dx12.patch
        0310-fix-assimp-not-found.patch)
sha256sums=('93f2c0889ee2e9cdf30c170d353c3f829de5f29ba21c119167dee5995e48ccce'
            '884b32f0e2e1bb110330a921b69443379a7be98c42f13754f9c5f56c040ba3b7'
            '617e6fa85a92353d0073425d37cd5d90d92cb7f906d66dd2d0df576122d091a4'
            '27ce2161b5dbea2fcce6b15948ab722a71036d056420854f6554969f683583bb'
            '15c9fdf4d54628e64a9b7e456a5308aecf0f1c8e33a5aff0ab0bcac0300a9568'
            'e04033364f5ad77906b4100c34572df57bd793a55bc33b7d8a753a2cc60af259'
            '3be09af4b54ee0792a9e91fe7a1cf07d10f65f68564b93c86684599c31530c91'
            '18fd2fa42215ac47b3b314261ab98cbe65f8231429e4f29a152288a3ca93daf1'
            '4e154fbc9059a096c351d019da6b18c907b1d8b06e028f48c7365f62bcd0edc9'
            'e2ec5e67bdcfd162a82c49181a2d480f4b193acdfa6b0e22f4a8448286162630'
            '465d3897c4494695b66ac121944b3bd370db91bfea71dd2c9170dbf268cc99ea'
            'b31929c78f9906756a5e1bdf9a796541865c474aaa1b3eefea1a3f1d7c8d94ca'
            'd8a476be7e55e8bb9362868073dc9e7937431e68b9578c0a0ed103cf1ebc8c01'
            'd7c228df3cc680793a6c858e1fac01092c0877071c075804b7e93f6b3a481eae'
            '97fcd6bd58ff8b8d735a6381b607bb3b4b351d1d07be1bbc18b97155dcbb501d'
            'f80ce415f25f0aa4ead2efe369b41efe35d9848d4d36d8dbf3831e48dbd6e506'
            '19566c38bd50581acaa6a9f46f0e25c41fdfc8b0b42269126e4d3a9b83e2f224'
            'ddaa067d144f5788ab7a3ed0dfc426d702c13de9c35f04eaaf38d642be4934d4'
            '8beeb610df3aff14bae43c56e5781cda3d49f1ad23c6e3fc2aa2405ce002de46'
            'f008164ef2a224b8c52736bab53f1aeec72d610928f03cce57f632824dfdd615'
            'a09ba083957a2b9020b5e17e57db98c3f41390db26d85b310798597fc2a4d34f'
            '42724bd154ed98c612d19a7daee2b5028270ef6dbfe7f35d5f97c8d0605f9fe6'
            '3dc4d7c4fed8fedd726211edbfe13f4d78247d3ab2f789f3ce2a8e43e59a7c5b'
            'c84cb5ba4243413232bc693fb0f54c0283bf0e496e6790a2698f226f80275a61'
            '69e1d6f8fc59ee2da0abe0a6965d0d7730bd015c5b1f1c5b4a69435328b21768'
            '00e32052ac1a9a8391275dfe69901d4ae146ac1441c00bf46f3ef1cfe459a78d'
            '8a74fab12c9d3faac81d75ea1c0662624b83059631db8d9f2570b414d62dc692'
            'b878680172290ad2dcb5508827ef5a9230cbfd8e97d2672d2e96b5addbd55833'
            '928a8eaca8840ff353cdff8fd11140724cb89094e6115fdd7b8cf534830adcca'
            'f07ff4f72626b91aa67ce7ffc78ce7c7c5765bf5431a65b7d867b5f66fa1506b'
            'f930338562119f542b550e3cdb802f1f7ebd476f54dd7297db424271167394b0'
            '6fc871c9a3697666c19d5c21a8f038b80c9c825b418435b0723bf35a9e532d91'
            'cf8b243128bd327971373fe9869350198c5822ecd797d2908bd1a32b62c6907e'
            '28ad823dc8b6665de7b552e82411a8b7b25bf716c42597d8dc8644bded63199c'
            '9ea7aeb486023f3aebf1af44603ebd9266009aaeaf91e52c37c8c5154ea3900c'
            '7e57ce20b6707bc3b4a727da3287cadda13a74317d86515358b2535bc15244c7'
            '052c7035ad9a4fc0321975ba7658fb755bbc0841c7e1d9e88bb0a15e6a90b770'
            '157b97b3e3031d93d93e45368a1c54ebfbf9578116f75f5126e977e5dd518191')

# 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() {
  [[ -d ${srcdir}/${_pkgfqn} ]] && rm -rf ${srcdir}/${_pkgfqn}
  msg2 "Extracting source tarball..."
  tar -xJf ${srcdir}/${_pkgfile}.tar.xz -C ${srcdir} --exclude=${_pkgfqn}/{qtandroidextras,qtmacextras,qtwayland,qtwebengine,qtx11extras} || true

  cd ${srcdir}/${_pkgfqn}

  # MSYS2 gperf cannot handle \r\n.
  msg2 "Converting line endings of .gperf files..."
  find . -name "*.gperf" -exec dos2unix "{}" \;

  apply_patch_with_msg \
    0000-adjust-qmake-conf-mingw.patch \
    0001-qt-5.8.0-fix-sql-libraries-mingw.patch \
    0002-qt-5.8.0-configure-gcc-before-clang.patch \
    0003-enable-avx-support.patch \
    0004-fix-linking-again-different-static-libs.patch \
    0005-qtconnectivity-add-callback-attribute.patch \
    0006-qt-5.3.0-win_flex-replace.patch \
    0007-qt-5.3.0-win32-g-Enable-static-builds.patch \
    0008-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch \
    0016-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch \
    0017-qt-5.3.0-fix-examples-building.patch \
    0025-qt-5.8.0-force-using-make-on-msys.patch

  # Ossi says we should fix this in qwebkit with a $$(shell_path), but the ' quotes get in the way
  # due to qtPrepareTool adding them with $$shell_quote
  apply_patch_with_msg \
    0028-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch \
    0029-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch \
    0030-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch \
    0035-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch

  # Patches so that qt5-static can be used with cmake.
  apply_patch_with_msg \
    0036-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch

  apply_patch_with_msg \
    0048-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch \
    0049-qt-5.8.0-win32-do-not-use-fontconfig.patch \
    0051-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch \
    0052-qt-5.11-mingw-fix-link-qdoc-with-clang.patch \
    0054-win32-dont-need-flatpack-theme.patch \
    0056-qt-5.11-static_icu.patch \
    0057-fix-and-enable-iconv-codec.patch \
    0060-activeqt-handle-win64-in-dumpcpp.patch \
    0062-fix-static-build-big-resources.patch \
    0063-enable-mingw-schannel-alpn.patch \
    0064-fix-mingw-win32-winnt-detection.patch \
    0100-fix-relocatable-prefix-staticbuild-v2.patch \
    0125-qt5-windeployqt-fixes.patch \
    0300-qt-5.8.0-cast-errors.patch \
    0304-qtdeclarative-disable-dx12.patch \
    0310-fix-assimp-not-found.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -i "s| -mthreads||g" qtbase/mkspecs/common/g++-win32.conf
  fi

  local _ARCH_TUNE=
  if [[ ${CARCH} == x86_64 ]]; then
    _ARCH_TUNE="-march=nocona -msahf -mtune=generic"
  elif [[ ${CARCH} == i686 ]]; then
    _ARCH_TUNE="-march=pentium4 -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

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -i "s| -mthreads||g" qtbase/mkspecs/common/g++-win32.conf
  fi
  # But overwrite this one.
  #sed -i "s|^QMAKE_LFLAGS_RELEASE .*= \(.*\)$|QMAKE_LFLAGS_RELEASE    = ${QMAKE_LFLAGS_RELEASE}|g" qtbase/mkspecs/common/g++-win32.conf

  # To keep the build folder name quite small (PATH_MAX limit)
  # the source folder (long) is renamed to $MSYSTEM (MINGW32 or MINGW64)
  cd ${srcdir}
  [[ -d ${MSYSTEM} ]] && rm -rf ${MSYSTEM}
  sleep 5
  mv ${_pkgfqn} ${MSYSTEM}
}

build() {
  local _buildpkgdir=${pkgdirbase}/${pkgname}/${MINGW_PREFIX}/${_realname}
  mkdir -p ${_buildpkgdir}
  local QTDIR_WIN=$(cygpath -am ${_buildpkgdir})

  cd ${MSYSTEM}
  touch qtbase/.gitignore

  # https://github.com/msys2/MSYS2-packages/issues/2282
  export MSYS2_ARG_CONV_EXCL='--foreign-types='

  # Qt manages the compiler flags for release / debug configs separately, so having our own values (-O2) is harmful here ..
  unset CFLAGS
  unset CXXFLAGS
  unset LDFLAGS
  unset QMAKESPEC
  unset XQMAKESPEC
  unset QMAKEPATH
  unset QMAKEFEATURES

  export PATH="${srcdir}/${MSYSTEM}/qtbase/bin:${srcdir}/${MSYSTEM}/qtbase/lib:${PATH}"
  export LLVM_INSTALL_DIR=${MINGW_PREFIX}
  export VULKAN_SDK=${MINGW_PREFIX}

  export QDOC_USE_STATIC_LIBCLANG=1
  #export FORCE_MINGW_QDOC_BUILD=1
  # Hack to disable building qdoc
  patch -p1 -i ${srcdir}/0302-ugly-hack-disable-qdoc-build.patch
  export QDOC_SKIP_BUILD=1

  ./configure \
    -prefix ${QTDIR_WIN} \
    -datadir ${QTDIR_WIN}/share/qt5 \
    -archdatadir ${QTDIR_WIN}/share/qt5 \
    -nomake examples \
    -nomake tests \
    -opensource \
    -confirm-license \
    -platform ${_platform} \
    -release \
    -feature-relocatable \
    -static \
    -static-runtime \
    -DJAS_DLL=0 \
    -opengl desktop \
    -schannel \
    -no-fontconfig \
    -no-glib \
    -no-gstreamer \
    -no-iconv \
    -no-icu \
    -no-jasper \
    -no-mng \
    -no-wmf \
    -qt-assimp \
    -qt-doubleconversion \
    -qt-freetype \
    -qt-harfbuzz \
    -qt-libjpeg \
    -qt-libpng \
    -qt-pcre \
    -qt-tiff \
    -qt-webp \
    -dbus-linked \
    -no-sql-ibase \
    -no-sql-mysql \
    -no-sql-odbc \
    -no-sql-psql \
    -no-sql-sqlite2 \
    -sql-sqlite \
    -qt-sqlite

  make || make
}

check() {
  cd ${MSYSTEM}
  make check -j1 -k
}

package() {
  cd ${MSYSTEM}

  make install

  install -d "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5
  install -Dm644 LICENSE* -t "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/${_realname}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}/${_realname}" -type f -name "*.orig" -exec rm -f {} \;

  # Workaround for installing empty .pc files
  plain "---> Fix pkgconfig files..."
  local _pc_files=( $(find ${srcdir}/${MSYSTEM} -type f -name Qt5*.pc) )
  cp -f ${_pc_files[@]} ${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig/

  # Fix paths in qconfig.pri and qmodule.pri:
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local QT_PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX}/${_realname})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdir}${MINGW_PREFIX}/${_realname})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"
  local MINGW_PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f -name '*.cmake' \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${MINGW_PREFIX}/${_realname}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}/${_realname}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f -name '*.cmake' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}/..|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}/..|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}/..|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig" -type f -name '*.pc' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}
