ANDROID_BUILD_TOP=$$(ANDROID_BUILD_TOP)
isEmpty(ANDROID_BUILD_TOP) : error("$ANDROID_BUILD_TOP is empty. Forgot to run 'lunch'?")

ANDROID_PRODUCT_OUT=$$(ANDROID_PRODUCT_OUT)
isEmpty(ANDROID_PRODUCT_OUT) : error("$ANDROID_PRODUCT_OUT is empty. forgot to run 'lunch'?")

defineReplace(getAndroidBuildVar) {
    thevar = $$1
    r=$$system(cd $$ANDROID_BUILD_TOP && CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core make --no-print-directory -f build/core/config.mk dumpvar-$${thevar})
    return($$r)
}

# .qmake.cache is not available at mkspec loading time.
# not caching those is horribly slow though

!exists($$PWD/android_build_vars) {
    store_ANDROID_TOOLCHAIN_PREFIX = "ANDROID_TOOLCHAIN_PREFIX=$$getAndroidBuildVar(abs-TARGET_TOOLS_PREFIX)"
    store_ANDROID_TARGET_CFLAGS    = "ANDROID_TARGET_CFLAGS=$$getAndroidBuildVar(TARGET_GLOBAL_CFLAGS)"
    store_ANDROID_TARGET_LDFLAGS   = "ANDROID_TARGET_LDFLAGS=$$getAndroidBuildVar(TARGET_GLOBAL_LDFLAGS)"
    store_ANDROID_TARGET_ARCH      = "ANDROID_TARGET_ARCH=$$getAndroidBuildVar(TARGET_ARCH)"
    store_ANDROID_VERSION          = "ANDROID_VERSION=$$getAndroidBuildVar(PLATFORM_VERSION)"

    write_file(android_build_vars, store_ANDROID_TOOLCHAIN_PREFIX)
    write_file(android_build_vars, store_ANDROID_TARGET_CFLAGS, append)
    write_file(android_build_vars, store_ANDROID_TARGET_LDFLAGS, append)
    write_file(android_build_vars, store_ANDROID_TARGET_ARCH, append)
    write_file(android_build_vars, store_ANDROID_VERSION, append)
}

info(using android build env from cache in $$PWD/android_build_vars . delete this file if you changed your build env )
exists($$PWD/android_build_vars) {
    include($$PWD/android_build_vars)
}

ANDROID_VERSION_SPLIT = $$split(ANDROID_VERSION, ".")
ANDROID_VERSION_MAJOR = $$member(ANDROID_VERSION_SPLIT, 0)
ANDROID_VERSION_MINOR = $$member(ANDROID_VERSION_SPLIT, 1)

MAKEFILE_GENERATOR      = UNIX
QMAKE_COMPILER          = gcc
TARGET_PLATFORM         = unix
TEMPLATE                = app
QMAKE_INCREMENTAL_STYLE = sublib

include(../../common/linux.conf)
include(../../common/gcc-base-unix.conf)

CONFIG                  = qt warn_on release link_prl
QT                      = core gui
DEFINES                += HAVE_ANDROID_OS Q_OS_ANDROID_NO_SDK
DEFINES                += Q_ANDROID_VERSION_MAJOR=$$ANDROID_VERSION_MAJOR
DEFINES                += Q_ANDROID_VERSION_MINOR=$$ANDROID_VERSION_MINOR
DEFINES                += QT_NO_PRINTER QT_NO_PRINTDIALOG QT_NO_EXCEPTIONS

#note: -DANDROID results in weird behaviour of math.h
DEFINES                += ANDROID

QT_QPA_DEFAULT_PLATFORM = eglfs
EGLFS_PLATFORM_HOOKS_SOURCES  = $$PWD/qeglfshooks_surfaceflinger.cpp
EGLFS_PLATFORM_HOOKS_LIBS    += -lui -lgui -lutils -lcutils


QMAKE_CC                = $${ANDROID_TOOLCHAIN_PREFIX}gcc

QMAKE_CFLAGS            = $${ANDROID_TARGET_CFLAGS}
QMAKE_CFLAGS           -= -Werror=non-virtual-dtor
QMAKE_CFLAGS           -= -DNDEBUG

QMAKE_CFLAGS_WARN_ON    = -Wall -Wextra
QMAKE_CFLAGS_WARN_OFF   = -Wno-psabi

QMAKE_CFLAGS_SHLIB      = -fPIC
QMAKE_CFLAGS_YACC       = -Wno-unused -Wno-parentheses
QMAKE_CFLAGS_THREAD     = -D_REENTRANT
QMAKE_CFLAGS_HIDESYMS   =

QMAKE_CXX               = $${ANDROID_TOOLCHAIN_PREFIX}g++
QMAKE_CXXFLAGS          = $$QMAKE_CFLAGS
QMAKE_CXXFLAGS_WARN_ON  = $$QMAKE_CFLAGS_WARN_ON
QMAKE_CXXFLAGS_WARN_OFF = $$QMAKE_CFLAGS_WARN_OFF
QMAKE_CXXFLAGS_RELEASE += $$QMAKE_CFLAGS_RELEASE
QMAKE_CXXFLAGS_DEBUG   += $$QMAKE_CFLAGS_DEBUG
QMAKE_CXXFLAGS_SHLIB    = $$QMAKE_CFLAGS_SHLIB
QMAKE_CXXFLAGS_YACC     = $$QMAKE_CFLAGS_YACC
QMAKE_CXXFLAGS_THREAD   = $$QMAKE_CFLAGS_THREAD
QMAKE_CXXFLAGS_HIDESYMS = $$QMAKE_CFLAGS_HIDESYMS -fvisibility-inlines-hidden

QMAKE_LINK              = $$QMAKE_CXX
QMAKE_LINK_SHLIB        = $$QMAKE_LINK

QMAKE_AR                = $${ANDROID_TOOLCHAIN_PREFIX}ar cqs
QMAKE_OBJCOPY           = $${ANDROID_TOOLCHAIN_PREFIX}objcopy
QMAKE_NM                = $${ANDROID_TOOLCHAIN_PREFIX}nm -P
QMAKE_STRIP             = $${ANDROID_TOOLCHAIN_PREFIX}strip
QMAKE_RANLIB            = $${ANDROID_TOOLCHAIN_PREFIX}ranlib

QMAKE_INCDIR            = $${ANDROID_BUILD_TOP} \
                          $${ANDROID_BUILD_TOP}/dalvik/libnativehelper/include/nativehelper \
                          $${ANDROID_BUILD_TOP}/bionic \
                          $${ANDROID_BUILD_TOP}/bionic/libc/include \
                          $${ANDROID_BUILD_TOP}/bionic/libc/kernel/arch-arm \
                          $${ANDROID_BUILD_TOP}/bionic/libc/kernel/common \
                          $${ANDROID_BUILD_TOP}/bionic/libc/private \
                          $${ANDROID_BUILD_TOP}/bionic/libm/include \
                          $${ANDROID_BUILD_TOP}/bionic/libm/include/arm \
                          $${ANDROID_BUILD_TOP}/bionic/libstdc++/include \
                          $${ANDROID_BUILD_TOP}/bionic/libthread_db/include \
                          $${ANDROID_BUILD_TOP}/dalvik/libnativehelper/include \
                          $${ANDROID_BUILD_TOP}/external/icu4c/common/ \
                          $${ANDROID_BUILD_TOP}/external/icu4c/i18n/ \
                          $${ANDROID_BUILD_TOP}/external/skia/include \
                          $${ANDROID_BUILD_TOP}/external/stlport/stlport \
                          $${ANDROID_BUILD_TOP}/frameworks/include \
                          $${ANDROID_BUILD_TOP}/frameworks/native/include \
                          $${ANDROID_BUILD_TOP}/frameworks/native/include/gui \
                          $${ANDROID_BUILD_TOP}/frameworks/native/opengl/include \
                          $${ANDROID_BUILD_TOP}/frameworks/base/include \
                          $${ANDROID_BUILD_TOP}/frameworks/base/native/include \
                          $${ANDROID_BUILD_TOP}/frameworks/base/opengl/include \
                          $${ANDROID_BUILD_TOP}/hardware/libhardware/include \
                          $${ANDROID_BUILD_TOP}/hardware/libhardware_legacy/include \
                          $${ANDROID_BUILD_TOP}/hardware/ril/include \
                          $${ANDROID_BUILD_TOP}/system/core/include \
                          $${ANDROID_BUILD_TOP}/system/core/include/arch/linux-arm/ \
                          $${ANDROID_PRODUCT_OUT}/obj/include

QMAKE_INCDIR           += $${ANDROID_BUILD_TOP}/bionic/libc/arch-$${ANDROID_TARGET_ARCH}/include

QMAKE_LIBDIR            = ${ANDROID_PRODUCT_OUT}/obj/lib
QMAKE_INCDIR_X11        =
QMAKE_LIBDIR_X11        =
QMAKE_INCDIR_OPENGL     =
QMAKE_INCDIR_OPENGL_ES2 =
QMAKE_LIBDIR_OPENGL_ES2 =


QMAKE_LINK              = $$QMAKE_CXX
QMAKE_LINK_SHLIB        = $$QMAKE_CXX
QMAKE_LFLAGS            = $${ANDROID_TARGET_LDFLAGS} \
                          -nostdlib  \
                          -L${ANDROID_PRODUCT_OUT}/obj/lib \
                          -Wl,-rpath-link,${ANDROID_PRODUCT_OUT}/obj/lib

QMAKE_LFLAGS_PLUGIN     = -Wl,-T,${ANDROID_BUILD_TOP}/build/core/armelf.xsc \
                          -Wl,-shared,-Bsymbolic

#apparantly this file is missing from some builds.
#it does work without, but this may change in future.
#QMAKE_LFLAGS_APP        =   -Wl,-T,${ANDROID_BUILD_TOP}/build/core/armelf.xs \
QMAKE_LFLAGS_APP        = ${ANDROID_PRODUCT_OUT}/obj/lib/crtbegin_dynamic.o \
                          ${ANDROID_PRODUCT_OUT}/obj/lib/crtend_android.o

QMAKE_LFLAGS_SHLIB      = -Wl,-T,${ANDROID_BUILD_TOP}/build/core/armelf.xsc \
                          -Wl,-shared,-Bsymbolic \
                          ${ANDROID_PRODUCT_OUT}/obj/lib/crtbegin_so.o \
                          ${ANDROID_PRODUCT_OUT}/obj/lib/crtend_so.o

QMAKE_LFLAGS_SONAME     =
QMAKE_LFLAGS_NOUNDEF    = -Wl,--no-undefined
QMAKE_LFLAGS_RPATH      = -Wl,-rpath=


QMAKE_LIBS              = -lstlport -lstdc++ -llog -lz -lm -ldl -lc -lgcc


QMAKE_LIBS_X11          =
QMAKE_LIBS_X11SM        =
QMAKE_LIBS_QT_THREAD    =
QMAKE_LIBS_QT_OPENGL    =
QMAKE_LIBS_QTOPIA       =
QMAKE_LIBS_THREAD       =
QMAKE_LIBS_OPENGL       =
QMAKE_LIBS_OPENGL_ES2   = -lEGL -lGLESv2 $$QMAKE_LIBS

CONFIG                 += linux-android-9 android-9 linux-android android android-no-sdk android_app

load(qt_config)

# we don't support exceptions, but right now this has no effect
# you need to manually delete mkspecs/features/exceptions.prf
CONFIG                 -= exceptions
CONFIG                 += exceptions_off

