# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=qt5-static
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.15.17
pkgrel=1
pkgdesc="A cross-platform application and UI framework (static) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io/'
msys2_references=(
  'archlinux: qt5-base'
  'aur: mingw-w64-qt5-base-static'
  "cpe: cpe:/a:qt:qt"
)
msys2_repository_url='https://github.com/qt/qt5'
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}-vulkan")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fxc2"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-dbus"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-z3"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd")
_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
        0009-add-missing-metatypes-configs.patch
        0010-qmltyperegistrar-search-in-correct-directory.patch
        0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch
        0012-mingw-add-extra-flags.patch
        0013-qtbase-use-system-libs.patch
        0014-qtimageformats-use-system-libs.patch
        0015-fix-build-with-cygwin-tools.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
        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
        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
        0401-disable-warning-of-non-existent-files.patch
        0402-deprecated-ucrt-functions.patch
        0403-qtactiveqt-tools-avoid-race-condition.patch
        0411-javascriptcore-disable-deprecated-warnings.patch
        0412-jitstubs-underscore-symbol-name-for-x86.patch)
sha256sums=('85eb566333d6ba59be3a97c9445a6e52f2af1b52fc3c54b8a2e7f9ea040a7de4'
            '884b32f0e2e1bb110330a921b69443379a7be98c42f13754f9c5f56c040ba3b7'
            '617e6fa85a92353d0073425d37cd5d90d92cb7f906d66dd2d0df576122d091a4'
            '27ce2161b5dbea2fcce6b15948ab722a71036d056420854f6554969f683583bb'
            '15c9fdf4d54628e64a9b7e456a5308aecf0f1c8e33a5aff0ab0bcac0300a9568'
            'e04033364f5ad77906b4100c34572df57bd793a55bc33b7d8a753a2cc60af259'
            '3be09af4b54ee0792a9e91fe7a1cf07d10f65f68564b93c86684599c31530c91'
            '18fd2fa42215ac47b3b314261ab98cbe65f8231429e4f29a152288a3ca93daf1'
            '4e154fbc9059a096c351d019da6b18c907b1d8b06e028f48c7365f62bcd0edc9'
            'e2ec5e67bdcfd162a82c49181a2d480f4b193acdfa6b0e22f4a8448286162630'
            '37e1d7f222718d6551442ab3fb2d979089dfd7b19690709318e4e1edc44444cd'
            'b5066718a1a393bda5e928434e8f987c3353ed8be2b0e103eb5fa6832a5e395a'
            '465d3897c4494695b66ac121944b3bd370db91bfea71dd2c9170dbf268cc99ea'
            '6e57b29ef71476f0eeb0fde9ac6635a1e4dcf66e0b83a34baf38c9df4f4429d2'
            'b4135a287752a983183b966785c04c46c018055d43b8a84ef9f6e72c3406a766'
            '91d65c71a1c809b23dc651981eee64a8d1fe9c8738f4c4756e5dca87e1c3ad4a'
            '9e81d5a387b4a436c6728c2dde9d9102cf18d89dfd1b14f64cb39e798621a568'
            'b31929c78f9906756a5e1bdf9a796541865c474aaa1b3eefea1a3f1d7c8d94ca'
            'd8a476be7e55e8bb9362868073dc9e7937431e68b9578c0a0ed103cf1ebc8c01'
            '97fcd6bd58ff8b8d735a6381b607bb3b4b351d1d07be1bbc18b97155dcbb501d'
            'f80ce415f25f0aa4ead2efe369b41efe35d9848d4d36d8dbf3831e48dbd6e506'
            '19566c38bd50581acaa6a9f46f0e25c41fdfc8b0b42269126e4d3a9b83e2f224'
            'ddaa067d144f5788ab7a3ed0dfc426d702c13de9c35f04eaaf38d642be4934d4'
            'bfb9ff161442683c88e137ca4ac4f9aabe44742d8204be565ccdaac6c59fcc0d'
            'f008164ef2a224b8c52736bab53f1aeec72d610928f03cce57f632824dfdd615'
            '42724bd154ed98c612d19a7daee2b5028270ef6dbfe7f35d5f97c8d0605f9fe6'
            '3dc4d7c4fed8fedd726211edbfe13f4d78247d3ab2f789f3ce2a8e43e59a7c5b'
            'c84cb5ba4243413232bc693fb0f54c0283bf0e496e6790a2698f226f80275a61'
            '69e1d6f8fc59ee2da0abe0a6965d0d7730bd015c5b1f1c5b4a69435328b21768'
            '00e32052ac1a9a8391275dfe69901d4ae146ac1441c00bf46f3ef1cfe459a78d'
            '8a74fab12c9d3faac81d75ea1c0662624b83059631db8d9f2570b414d62dc692'
            'b878680172290ad2dcb5508827ef5a9230cbfd8e97d2672d2e96b5addbd55833'
            '928a8eaca8840ff353cdff8fd11140724cb89094e6115fdd7b8cf534830adcca'
            'f07ff4f72626b91aa67ce7ffc78ce7c7c5765bf5431a65b7d867b5f66fa1506b'
            'f930338562119f542b550e3cdb802f1f7ebd476f54dd7297db424271167394b0'
            '6fc871c9a3697666c19d5c21a8f038b80c9c825b418435b0723bf35a9e532d91'
            'cf8b243128bd327971373fe9869350198c5822ecd797d2908bd1a32b62c6907e'
            '28ad823dc8b6665de7b552e82411a8b7b25bf716c42597d8dc8644bded63199c'
            '9ea7aeb486023f3aebf1af44603ebd9266009aaeaf91e52c37c8c5154ea3900c'
            '7e57ce20b6707bc3b4a727da3287cadda13a74317d86515358b2535bc15244c7'
            '052c7035ad9a4fc0321975ba7658fb755bbc0841c7e1d9e88bb0a15e6a90b770'
            '157b97b3e3031d93d93e45368a1c54ebfbf9578116f75f5126e977e5dd518191'
            '1d25b493ebe8bdb6e332db0c3b149626cf4ed5b51f67fe45fafd9b28c87088e8'
            '3dc92472a3f7ac0927546ae6f8ac3dc3b88158fa523917ecaac5aaae95cbe25e'
            '1e90e103bd96f07452d648286a3289306dbd5e101123798206410fb78283fff8'
            '614b027c94784fcfd7979cea3326b75d8415da2dbd0ab7d5181ad04c21341644'
            '62616c2b8aa19afbcb1bd6d05427ddfc87fa3bc6ed0bc1a688f80d8e95742bca')

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}
  touch qtbase/.gitignore

  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 \
    0009-add-missing-metatypes-configs.patch \
    0010-qmltyperegistrar-search-in-correct-directory.patch \
    0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch \
    0012-mingw-add-extra-flags.patch \
    0013-qtbase-use-system-libs.patch \
    0014-qtimageformats-use-system-libs.patch \
    0015-fix-build-with-cygwin-tools.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 \
    0302-ugly-hack-disable-qdoc-build.patch \
    0304-qtdeclarative-disable-dx12.patch \
    0310-fix-assimp-not-found.patch

  apply_patch_with_msg \
    0401-disable-warning-of-non-existent-files.patch \
    0402-deprecated-ucrt-functions.patch \
    0403-qtactiveqt-tools-avoid-race-condition.patch \
    0411-javascriptcore-disable-deprecated-warnings.patch \
    0412-jitstubs-underscore-symbol-name-for-x86.patch

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

  # 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() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

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

  # 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}/build-${MSYSTEM}/qtbase/bin:${srcdir}/build-${MSYSTEM}/qtbase/lib:${PATH}"
  export VULKAN_SDK=${MINGW_PREFIX}
  export QDOC_SKIP_BUILD=1

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

  ../${MSYSTEM}/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 \
    -silent \
    -dbus-linked \
    -opengl desktop \
    -openssl-linked \
    -sql-sqlite \
    -qt-assimp \
    -qt-doubleconversion \
    -qt-freetype \
    -qt-harfbuzz \
    -qt-libjpeg \
    -qt-libpng \
    -qt-pcre \
    -qt-sqlite \
    -qt-tiff \
    -qt-webp \
    -no-fontconfig \
    -no-glib \
    -no-gstreamer \
    -no-iconv \
    -no-icu \
    -no-jasper \
    -no-mng \
    -no-sql-ibase \
    -no-sql-mysql \
    -no-sql-odbc \
    -no-sql-psql \
    -no-sql-sqlite2 \
    -no-wmf

  make
}

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

package() {
  cd build-${MSYSTEM}

  make install

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

  # 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}"/build-${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})
  local BUILDDIR_PREFIX=$(cygpath -m "${srcdir}"/build-${MSYSTEM})

  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" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig" -type f -name '*.pc' \
    -exec sed -i -e "s|${BUILDDIR_PREFIX}/qtbase/lib/lib|-l|g" -e "s|.a | |g" \
                 -e "s|${BUILDDIR_PREFIX}/qt3d/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtactiveqt/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtdeclarative/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtlocation/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtmultimedia/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qttools/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtquick3d/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtquickcontrols2/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtscript/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtserialport/lib/lib|-l|g" \
                 -e "s|${BUILDDIR_PREFIX}/qtgamepad/lib/lib|-l|g" \
                 -e "s|-L${BUILDDIR_PREFIX}/qtlocation/lib ||g" \
                 -e "s|-l\:libz.a |-lz |g" \
                 -e "s|-l\:libzstd.a |-lzstd |g" \
                 -e "s|.a | |g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/share/qt5" -type f -name '*.prl' \
    -exec sed -i -e "s|-l\:libz.a\;|-lz\;|g" \
                 -e "s|-l\:libzstd.a\;|-lzstd\;|g" \
                 -e "s|-l\:libz.a |-lz |g" \
                 -e "s|-l\:libzstd.a |-lzstd |g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f -name '*.prl' \
    -exec sed -i -e "s|-l\:libz.a\;|-lz\;|g" \
                 -e "s|-l\:libzstd.a\;|-lzstd\;|g" \
                 -e "s|-l\:libz.a |-lz |g" \
                 -e "s|-l\:libzstd.a |-lzstd |g" {} \;
}
