# 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.

# This file is created from PKGBUILD.sh.ep contained by the mentioned repository.
# Do not edit it manually! See README.md in the repository's root directory
# for more information.

# All patches are managed at https://github.com/Martchus/qtbase

_android_arch=aarch64
pkgname=android-aarch64-qt6-base
_qtver=6.10.1
pkgver=${_qtver/-/}
pkgrel=3
arch=(any)
url='https://www.qt.io'
license=(GPL-3.0-only
         LGPL-3.0-only
         LicenseRef-Qt-Commercial
         Qt-GPL-exception-1.0)
pkgdesc='A cross-platform application and UI framework (android)'
depends=('java-runtime-headless-openjdk=17' 'ant' 'android-ndk' 'android-sdk' 'android-sdk-build-tools'
         'android-sdk-platform-tools' 'android-platform-35' "android-${_android_arch}-libjpeg-turbo"
         "android-${_android_arch}-libpng" "android-${_android_arch}-openssl" "android-${_android_arch}-zlib"
         "android-${_android_arch}-sqlite")
makedepends=('android-cmake' 'android-pkg-config'
             'java-environment-openjdk=17' 'qt6-base' 'ninja' 'patchelf')
optdepends=('qt6-base: development tools')
# note: Using the java-environment-openjdk and android-platform-X versions that match what is documented
# on https://doc.qt.io/qt-6/supported-platforms.html#android because using unsupported versions is not a
# good idea (see e.g. https://bugreports.qt.io/browse/QTBUG-119223 for Java; using a newer version of
# android-platform also did not work in my tests with Qt 6.7.2 and android-platform 35).
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
groups=(android-${_android_arch}-qt6)
_pkgfqn="qtbase-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfqn}.tar.xz"
        '0001-Use-CMake-s-default-import-library-suffix.patch'
        '0002-Fix-finding-D-Bus.patch'
        '0003-Fix-using-static-PCRE2-and-DBus-1.patch'
        '0004-Fix-transitive-dependencies-of-static-libraries.patch'
        '0005-Fix-libjpeg-workaround-for-conflict-with-rpcndr.h.patch'
        '0006-Support-finding-static-MariaDB-client-library.patch'
        '0007-Allow-overriding-CMAKE_FIND_LIBRARY_SUFFIXES-to-pref.patch'
        '0008-Find-fontconfig-via-pkg-config-for-correct-handling-.patch'
        '0009-Fix-dependency-of-xcb-image-on-xcb-util.patch'
        '0010-Allow-using-properties-of-PkgConfig-targets-for-glib.patch'
        '0011-Allow-using-properties-of-PkgConfig-targets-for-Wayl.patch'
        '0012-Allow-overriding-preference-for-shared-libzstd-libra.patch'
        '0013-Workaround-Unknown-CMake-command-_qt_test_emscripten.patch'
        '0014-Fix-configuration-when-EMSCRIPTEN_ROOT-is-an-absolut.patch'
        '0015-Allow-keeping-Android-app-in-background-with-QtQuick.patch'
        '0016-Enable-minimal-and-offscreen-plugins-when-compiling-.patch'
        '0017-Allow-configuring-use-of-OpenSSL-in-QPasswordDigesto.patch'
        '0018-Drop-CMAKE_SYSTEM_VERSION-from-modules-json-if-Linux.patch'
        '0019-Workaround-linker-error-about-missing-symbol-__sync_.patch'
        '0020-Fix-Android-build-after-ec2e3e7ac92d000e0df0c693b9a6.patch'
        '0021-Fix-inclusion-of-OpenGL-header-after-Qt-6-header.patch'
        '0022-Use-a-more-reasonable-fallback-directory-for-fonts-o.patch'
        '0023-Undefine-mingw-stat.patch'
        '0024-Export-some-constexpr-variables.patch'
        '0025-Android-don-t-enable-focus-for-QtEditText-when-added.patch'
        '0026-Android-avoid-a11y-operations-on-Qt-thread-without-a.patch')
sha256sums=('5a6226f7e23db51fdc3223121eba53f3f5447cf0cc4d6cb82a3a2df7a65d265d'
            '3264e47e25cc9588956b8300d3205510f3b60054d4854f0351299a159172cba4'
            'c72dbf4ffe0f8e0de916bf403130f059d8c238d80e720f1bbe534a72c02a4c69'
            '5ace6c31738397fc4de457ebb1b50d409a48b5388cff654836e0d042d103dd1d'
            '4617285282ca092e70d57331046ce2f1c3970094acc2d57391ddb5054f7f80bb'
            'c56e70a8d5440fded4f1b06c64a30417ce76996f7d3bc6df76bcea163f8dfa7f'
            'bcbc694b233cd6de74a150230984c6a8dee368d32a979a97f04edac8efebc2ab'
            '70fc553321b18164189949dc76568e5d7dae1feffb530119fc705d7e5a41c84f'
            'bef3d0f528440797b344e0756b7789314002db3104e12da718e77c6d91128f1b'
            '246f117141accf28325156d01e9df40beb1bbc0362a908ed9487b9a79f31d4a1'
            '6a17d9c29c643feff8c908d0c03d6cd14a24c004b96c8eca7839494455c8c8f5'
            '9d495fb90f5721a8b621ccdc4c0a4880b4c8abd769d9be5b08d409b00c75d7e9'
            'db05a3a3f1be3901e359086ba8719b7f9f05b1a504ecedf4ac4e8c7e1ff895fd'
            'eb11f9ce19186079b4bb714350bf0578709fd85954e2a3f8b08e7e018f759df3'
            '51bf497e71586542aa8d39f1403e47f35ee14fa84f8103f64b7f3b6277972ed1'
            'bb3c8a496f28f63732e86c47d3df9a10bdff87af040504f4b01755d9f0def2f4'
            '0c055a2a83e4fcd21c19193d02af02f3c85fb848e6a1ef3877efd661920643bd'
            '7591896be74d5a2f76fda8244c89006440b52406545d1dd6bd8bcd316f95e6ec'
            '46b5d9313e2f3234bbebb078e82f7356845d9c14eaacdc2fb58bec96110263a8'
            '30ced1c0653f4b39ae334b29cd5db3e513865dce1e30ca38a41acd0dc7d33419'
            'a4bc8133527ce47a6ec051f5e51ebfaa9fc3c53cba26239ebadf450835a49a36'
            '486c52be82e9599c3c2daf2bfc93f8ce8f524ff405f8bebfe68d2646d0408c57'
            'da1dd3a446374017c0a27f66733343d20a8e84d3eff00f03601dbcc11ad49d88'
            'c7301c23b00c103e96a8f558c3fcb685f564f19555a3f20b8e2b4932bf6a651d'
            '7df84091ad46fd25a1cd5e2d365a91b285f72a755824e01ee01e32687b59ff84'
            'b2be0ed04393edef28c36346cac41ee2b44b624ec36725d71ce0e45b4d7c846a'
            '4964d4a72b156d988a0e0529fb32848ca932d2012779e3a49ef075c48c958cdb')

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() {
  export PATH=/usr/lib/jvm/java-17-openjdk/bin:$PATH
  source android-env ${_android_arch}
  android-${_android_arch}-cmake -G Ninja -B build-$_android_arch -S $_pkgfqn \
      -DCMAKE_FIND_ROOT_PATH="${ANDROID_PREFIX}" \
      -DQT_BINARY_DIR=${ANDROID_PREFIX_BIN} \
      -DQT_INCLUDE_DIRS_NO_SYSTEM=ON \
      -DQT_HOST_PATH=/usr \
      -DQT_NO_PACKAGE_VERSION_CHECK:BOOL=TRUE \
      -DANDROID_SDK_ROOT=${ANDROID_HOME} \
      -DANDROID_NDK_ROOT=${ANDROID_NDK_HOME} \
      -DANDROID_STL="c++_shared" \
      -DFEATURE_pkg_config=ON \
      -DFEATURE_system_pcre2=OFF \
      -DFEATURE_system_freetype=OFF \
      -DFEATURE_system_harfbuzz=OFF \
      -DFEATURE_system_sqlite=ON \
      -DFEATURE_system_libjpeg=ON \
      -DFEATURE_system_libpng=ON \
      -DFEATURE_system_zlib=ON \
      -DINSTALL_DOCDIR=share/doc/qt6 \
      -DINSTALL_EXAMPLESDIR=share/doc/qt6/examples \
      -DINPUT_openssl=runtime
  VERBOSE=1 cmake --build build-$_android_arch

  # note: Avoid using `-DINSTALL_` options like in the regular package to avoid any problems with androiddeployqt.
}

package() {
  source android-env ${_android_arch}
  DESTDIR="$pkgdir" cmake --install build-$_android_arch

  # fix linking against SQLite
  patchelf \
    --replace-needed "${ANDROID_PREFIX_LIB}"/libsqlite3.so libsqlite3.so \
    "$pkgdir/${ANDROID_PREFIX}"/plugins/sqldrivers/libplugins_sqldrivers_qsqlite*.so

  install -Dm644 $_pkgfqn/LICENSES/* -t "$pkgdir"/usr/share/licenses/$pkgname

  # Drop QMAKE_PRL_BUILD_DIR because reference the build dir
  find "$pkgdir/${ANDROID_PREFIX}/" -type f -name '*.prl' \
    -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

  find ${pkgdir}/${ANDROID_PREFIX} -type f -name 'lib*.so' -exec ${ANDROID_STRIP} -g --strip-unneeded {} \;
  find ${pkgdir}/${ANDROID_PREFIX} -type f -name 'lib*.a' -exec ${ANDROID_STRIP} -g {} \;
  [[ -d "${pkgdir}/${ANDROID_PREFIX}/share/doc" ]] && rm -r "${pkgdir}/${ANDROID_PREFIX}/share/doc" || true
}
