# PKGCONFIG for android-qt5
# Maintainer in the AUR: Gonzalo Exequiel Pedone <hipersayan DOT x AT gmail DOT com>
# Contributor: Jiaxi Hu <sftrytry _AT_ gmail _DOT_ com>
# Contributor: jimmy00784 <jimmy00784@gmail.com>
# Contributor: Ricardo (XenGi) Band <email@ricardo.band>
# Contributor: Martchus <martchus@gmx.net>

_pkg_arch=armv7a-eabi
_android_arch=armeabi-v7a
_android_platform=21

pkgname=android-$_pkg_arch-qt5
pkgver=5.13.1
pkgrel=1
pkgdesc="Qt 5 for Android"
arch=('x86_64')
url='https://www.qt.io'
license=('GPL3' 'LGPL')
makedepends=('libgl'
             'sqlite'
             'zlib'
             'python2'
             'python' # Qt Qml requires Python 3
             'ruby'
             'gperf'
             'libxslt'
             'fontconfig'
             'java-environment-openjdk<=8'
             'android-environment'
             'android-pkg-config'
             "android-$_pkg_arch-openssl")
depends=('java-runtime-headless-openjdk<=8'
         'apache-ant'
         'android-ndk'
         "android-platform-$_android_platform"
         'android-sdk'
         'android-sdk-build-tools'
         'android-sdk-platform-tools'
         "android-$_pkg_arch-zlib"
         "android-$_pkg_arch-libpng"
         "android-$_pkg_arch-libjpeg-turbo")
groups=('android-qt5')
conflicts=("android-qt5-${_android_arch}")
replaces=("android-qt5-${_android_arch}")

case "$_pkg_arch" in
    aarch64)
        optdepends=('android-google-apis-aarch64: AVD support'
                    'android-aarch64-system-image: AVD support')
        ;;
    armv7a-eabi)
        optdepends=('android-google-apis-armv7a-eabi: AVD support'
                    'android-armv7a-eabi-system-image: AVD support')
        ;;
    x86)
        optdepends=('android-google-apis-x86: AVD support'
                    'android-x86-system-image: AVD support')
        ;;
    x86-64)
        optdepends=('android-google-apis-x86-64: AVD support'
                    'android-x86-64-system-image: AVD support')
        ;;
    *)
        ;;
esac
optdepends+=("android-$_pkg_arch-openssl: SSL support for Qt Network")

options=(!strip !buildflags staticlibs !emptydirs)
_pkgfqn="qt-everywhere-src-${pkgver}"
source=("http://download.qt-project.org/official_releases/qt/${pkgver:0:4}/${pkgver}/single/${_pkgfqn}.tar.xz"
        '0001-Fix-clang-libc-build-under-Android.patch'
        '0002-Fix-androiddeployqt-search-paths.patch'
        '0003-Support-pkg-config-under-Android.patch'
	'0004-Use-pkg-config-to-find-libjpeg.patch'
        '0001-Disable-mapboxgl.patch')
sha256sums=('adf00266dc38352a166a9739f1a24a1e36f1be9c04bf72e16e142a256436974e'
            '29bcb93f9c79ad4b0709efa42694ed917b6e4cfce69a805e79a7dc3144be7765'
            '7a5520311ba2edee2ffcd546cd6e08248c8f73deed33bd8075649577f8cb1446'
            '5c3f0725f3c27263661ff5ebb4b279e4483de8ed70dee4fe42a84d31b3f2e4a9'
            '4f3ba22616040a2979bfdf840b44bf105374ea2fac866c42977ad7ec6e87ddcc'
            '6151b784050f9d4ef3eec22dd33360d3f566d4834592f1171df91a10c95fab77')

prepare() {
    export ANDROID_MINIMUM_PLATFORM=${_android_platform}
    source android-env ${_pkg_arch}

    cd ${_pkgfqn}
    	
    pushd qtbase
    patch -Np1 -i "../../0001-Fix-clang-libc-build-under-Android.patch"
    #patch -Np1 -i "../../0002-Fix-androiddeployqt-search-paths.patch"
    patch -Np1 -i "../../0003-Support-pkg-config-under-Android.patch"
    patch -Np1 -i "../../0004-Use-pkg-config-to-find-libjpeg.patch"
    popd

    pushd qtlocation
    # skip building mapboxgl as it increases compile time significantly and
    # likely not a lot of people actually using it; if you need it, just remove the
    # following line:
    patch -Np1 -i "../../0001-Disable-mapboxgl.patch"
    popd

    sed -i "s/android-16/android-$ANDROID_MINIMUM_PLATFORM/g" qtbase/mkspecs/features/android/sdk.prf
    sed -i "s/android-16/android-$ANDROID_MINIMUM_PLATFORM/g" qtbase/src/android/jar/jar.pro
    sed -i "s/android-16/android-$ANDROID_MINIMUM_PLATFORM/g" qtgamepad/src/plugins/gamepads/android/jar/jar.pro
    sed -i "s/android-16/android-$ANDROID_MINIMUM_PLATFORM/g" qtbase/configure.pri
}

build() {
    export ANDROID_MINIMUM_PLATFORM=${_android_platform}
    source android-env ${_pkg_arch}

    unset CC CXX CFLAGS CXXFLAGS LDFLAGS CHOST QMAKESPEC QTDIR CARCH

    export PKG_CONFIG=$ANDROID_PKGCONFIG
    export PYTHON=/usr/bin/python2

    cd ${_pkgfqn}

    configue_opts="
        -confirm-license
        -opensource
        -prefix ${ANDROID_PREFIX}
        -examplesdir ${ANDROID_PREFIX_SHARE}/qt5/examples
        -testsdir ${ANDROID_PREFIX_SHARE}/qt5/tests
        -xplatform android-clang
        -nomake tests
        -nomake examples
        -android-ndk ${ANDROID_NDK_HOME}
        -android-sdk ${ANDROID_HOME}
        -android-ndk-host linux-x86_64
        -android-toolchain-version 4.9
        -skip qtserialport
        -no-warnings-are-errors
        -pkg-config
        -system-zlib
        -system-libpng
        -system-libjpeg
        -qt-freetype
        -openssl-runtime
        -android-arch ${ANDROID_ABI}
        -android-ndk-platform ${ANDROID_NDK_PLATFORM}
        -recheck-all
        -optimized-qmake
        -I${ANDROID_PREFIX}/include"

    [[ $ANDROID_DEBUG_BUILD ]] \
        && configue_opts+=' -debug-and-release' \
        || configue_opts+=' -release'

    # add architecture specific options
    case "$_pkg_arch" in
        x86*)
             configue_opts+="
                 -no-sql-mysql
                 -no-sql-psql"
            ;;
        *)
            ;;
    esac

    ./configure ${configue_opts}

    make $MAKEFLAGS
}

package() {
    export ANDROID_MINIMUM_PLATFORM=${_android_platform}
    source android-env ${_pkg_arch}

    cd ${_pkgfqn}

    make INSTALL_ROOT=${pkgdir} install

    find ${pkgdir}/${ANDROID_PREFIX_BIN} -type f ! -name '*.pl' -exec strip {} \;
    find ${pkgdir}/${ANDROID_PREFIX_LIB} -type f -name 'lib*.so' -exec ${ANDROID_STRIP} {} \;
    find ${pkgdir}/${ANDROID_PREFIX_LIB} -type f \( -name 'lib*.a' ! -name 'libQt5Bootstrap.a' ! -name 'libQt5QmlDevTools.a' \) -exec ${ANDROID_STRIP} {} \;
    find ${pkgdir}/${ANDROID_PREFIX}/plugins -type f -name 'lib*.so' -exec ${ANDROID_STRIP} {} \;
    find ${pkgdir}/${ANDROID_PREFIX}/qml -type f -name 'lib*.so' -exec ${ANDROID_STRIP} {} \;

    # fix plugin path in Android-specific dependency file
    sed -i 's/\"plugins\//\"lib\/qt\/plugins\//g' ${pkgdir}/${ANDROID_PREFIX_LIB}/*-android-dependencies.xml

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