#
# qmake configuration for building with android-g++
#

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/qws.conf)
CONFIG      = qt warn_on release link_prl ashmem android android_install
QT          = android core gui

# Passing in -win32 to qmake (from NQTC) causes this condition to pass, however
# qmake complains that -win32 is deprecated; should find another way, Q_OS_WIN
# should really be all QMAKE_HOST.os needs to depend on?
contains(QMAKE_HOST.os,Windows) {
    # Not having sh.exe in your path causes this condition to pass
    # To build Android Qt on Windows, this block must not be evaluated.
    isEmpty(QMAKE_SH) {
        QMAKE_COPY           = copy /y
        QMAKE_COPY_DIR       = xcopy /s /q /y /i
        QMAKE_MOVE           = move
        QMAKE_DEL_FILE       = del /q 2> NUL
        QMAKE_MKDIR          = mkdir
        QMAKE_DEL_DIR        = rmdir
        QMAKE_DEL_TREE       = rmdir /s /q
        QMAKE_CHK_DIR_EXISTS = if not exist
        QMAKE_INSTALL_FILE    = copy /y
        QMAKE_INSTALL_PROGRAM = copy /y
        QMAKE_SYMBOLIC_LINK   = $$QMAKE_COPY
    }
}

NDK_ROOT = $$(ANDROID_NDK_ROOT)
isEmpty( NDK_ROOT ) : NDK_ROOT = $$(HOME)/necessitas/android-ndk

NDK_HOST = $$(ANDROID_NDK_HOST)
isEmpty( NDK_HOST ) : NDK_HOST = linux-x86

ANDROID_PLATFORM = $$(ANDROID_NDK_PLATFORM)
isEmpty( ANDROID_PLATFORM ) : ANDROID_PLATFORM = android-5

ANDROID_TARGET_ARCH = $$(ANDROID_TARGET_ARCH)
isEmpty( ANDROID_TARGET_ARCH ) : ANDROID_TARGET_ARCH = armeabi-v7a   # armeabi     - arm v5 cpu architecture
                                                                 # armeabi-v7a - arm v7 cpu architecture
                                                                 # x86         - i686 cpu architecture

NDK_TOOLCHAIN_PREFIX = $$(ANDROID_NDK_TOOLCHAIN_PREFIX)
isEmpty( NDK_TOOLCHAIN_PREFIX ) {
contains(ANDROID_TARGET_ARCH, x86) : NDK_TOOLCHAIN_PREFIX = x86
                              else : NDK_TOOLCHAIN_PREFIX = arm-linux-androideabi
}

NDK_TOOLS_PREFIX = $$(ANDROID_NDK_TOOLS_PREFIX)
isEmpty( NDK_TOOLS_PREFIX ) {
contains(ANDROID_TARGET_ARCH, x86) : NDK_TOOLS_PREFIX = i686-android-linux
                              else : NDK_TOOLS_PREFIX = arm-linux-androideabi
}


NDK_TOOLCHAIN_VERSION = $$(ANDROID_NDK_TOOLCHAIN_VERSION)
isEmpty( NDK_TOOLCHAIN_VERSION ) : NDK_TOOLCHAIN_VERSION = 4.4.3

contains(ANDROID_TARGET_ARCH, x86) : ANDROID_ARCHITECTURE = x86
                              else : ANDROID_ARCHITECTURE = arm

!contains(NDK_TOOLCHAIN_VERSION, 4.4.3) : ANDROID_CXXSTL_SUFFIX = -$$NDK_TOOLCHAIN_VERSION

NDK_TOOLCHAIN = $$NDK_TOOLCHAIN_PREFIX-$$NDK_TOOLCHAIN_VERSION
NDK_TOOLCHAIN_PATH=$$NDK_ROOT/toolchains/$$NDK_TOOLCHAIN/prebuilt/$$NDK_HOST

CONFIG += $$ANDROID_PLATFORM $$ANDROID_TARGET_ARCH
ANDROID_PLATFORM_ROOT_PATH  = $$NDK_ROOT/platforms/$$ANDROID_PLATFORM/arch-$$ANDROID_ARCHITECTURE
ANDROID_PLATFORM_PATH  = $$ANDROID_PLATFORM_ROOT_PATH/usr

# used to compile platform plugins for android-4 and android-5
QMAKE_ANDROID_PLATFORM_INCDIR = $$NDK_ROOT/platforms/$$ANDROID_PLATFORM/arch-$$ANDROID_ARCHITECTURE/usr/include
QMAKE_ANDROID_PLATFORM_LIBDIR = $$NDK_ROOT/platforms/$$ANDROID_PLATFORM/arch-$$ANDROID_ARCHITECTURE/usr/lib

ANDROID_SOURCES_CXX_STL_LIBDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/libs/$$ANDROID_TARGET_ARCH
ANDROID_SOURCES_CXX_STL_INCDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/include $$ANDROID_SOURCES_CXX_STL_LIBDIR/include

# modifications to g++.conf
QMAKE_CC                = $$NDK_TOOLCHAIN_PATH/bin/$$NDK_TOOLS_PREFIX-gcc

contains(ANDROID_TARGET_ARCH, armeabi-v7a): QMAKE_CFLAGS = -Wno-psabi -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -DANDROID -Wa,--noexecstack
contains(ANDROID_TARGET_ARCH, armeabi)    : QMAKE_CFLAGS = -Wno-psabi -march=armv5te -mtune=xscale -msoft-float   -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -DANDROID -Wa,--noexecstack
contains(ANDROID_TARGET_ARCH, x86)        : QMAKE_CFLAGS = -ffunction-sections -funwind-tables -O2 -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300 -DANDROID  -Wa,--noexecstack

QMAKE_CFLAGS_WARN_ON    = -Wall -Wno-psabi -W
QMAKE_CFLAGS_WARN_OFF   = -Wno-psabi
contains(ANDROID_TARGET_ARCH, x86) {
                                    QMAKE_CFLAGS_RELEASE    = -g -O2
                                    QMAKE_CFLAGS_DEBUG      = -g
}else {
                                    QMAKE_CFLAGS_RELEASE    = -g -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64
                                    QMAKE_CFLAGS_DEBUG      = -g -marm -O0 -fno-omit-frame-pointer
}

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

QMAKE_CXX               = $$NDK_TOOLCHAIN_PATH/bin/$$NDK_TOOLS_PREFIX-g++
QMAKE_CXXFLAGS          = $$QMAKE_CFLAGS -DQT_NO_QWS_TRANSFORMED
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

# modifications to linux.conf
QMAKE_AR                = $$NDK_TOOLCHAIN_PATH/bin/$$NDK_TOOLS_PREFIX-ar cqs
QMAKE_OBJCOPY           = $$NDK_TOOLCHAIN_PATH/bin/$$NDK_TOOLS_PREFIX-objcopy

QMAKE_STRIP=
QMAKE_STRIP             = $$NDK_TOOLCHAIN_PATH/bin/$$NDK_TOOLS_PREFIX-strip

QMAKE_RANLIB            = $$NDK_TOOLCHAIN_PATH/bin/$$NDK_TOOLS_PREFIX-ranlib

QMAKE_INCDIR            = $$ANDROID_SOURCES_CXX_STL_INCDIR $$ANDROID_PLATFORM_PATH/include
QMAKE_LIBDIR            = $$ANDROID_PLATFORM_PATH/lib
QMAKE_INCDIR_X11        =
QMAKE_LIBDIR_X11        =
QMAKE_INCDIR_QT         = $$[QT_INSTALL_HEADERS]
QMAKE_LIBDIR_QT         = $$[QT_INSTALL_LIBS]

# Using a response file for linking on Windows
# requires forward slashes (or doubling up backwards ones)
contains(QMAKE_HOST.os,Windows) {
    QMAKE_LIBDIR_QT         = $$replace(QMAKE_LIBDIR_QT,\\\\,/)
    QMAKE_INCDIR_QT         = $$replace(QMAKE_INCDIR_QT,\\\\,/)
}

QMAKE_INCDIR_OPENGL     = $$ANDROID_PLATFORM_PATH/include
QMAKE_LIBDIR_OPENGL     = $$ANDROID_PLATFORM_PATH/lib
QMAKE_INCDIR_QTOPIA     =
QMAKE_LIBDIR_QTOPIA     =

contains(ANDROID_TARGET_ARCH, x86): LIBGCC_PATH_FULL     = $$system($$QMAKE_CC -print-libgcc-file-name)
                             else : LIBGCC_PATH_FULL     = $$system($$QMAKE_CC -mthumb-interwork -print-libgcc-file-name)

LIBGCC_PATH          = $$dirname(LIBGCC_PATH_FULL)

QMAKE_LINK              = $$QMAKE_CXX
QMAKE_LINK_SHLIB        = $$QMAKE_CXX
QMAKE_LFLAGS            = --sysroot=$$ANDROID_PLATFORM_ROOT_PATH -Wl,-rpath-link=$$ANDROID_PLATFORM_PATH/lib
QMAKE_LFLAGS_APP        =
QMAKE_LFLAGS_SHLIB      = -Wl,--no-undefined -Wl,-z,noexecstack -shared

contains(NDK_ROOT, ".*r6")|contains(NDK_ROOT, ".*r5.*")|contains(NDK_ROOT, ".*r5") {
    !contains(ANDROID_PLATFORM, android-4): !contains(ANDROID_PLATFORM, android-5): !contains(ANDROID_PLATFORM, android-8) {
        message("Your NDK-version is out-dated. A work-around is enabled. Consider updating your NDK (workarounds are required until r6(a))")
        QMAKE_LFLAGS_SHLIB += $$ANDROID_PLATFORM_PATH/lib/crtbegin_so.o $$ANDROID_PLATFORM_PATH/lib/crtend_so.o
    }
}

QMAKE_LFLAGS_PLUGIN     = $$QMAKE_LFLAGS_SHLIB
QMAKE_LFLAGS_SONAME     =
#-Wl,-soname,
#QMAKE_LFLAGS_THREAD     =
QMAKE_LFLAGS_NOUNDEF    =  -Wl,--no-undefined
QMAKE_LFLAGS_RPATH      =  -Wl,-rpath=


QMAKE_LIBS              = -llog -lz -lm -ldl -lc
QMAKE_LIBS_X11          =
QMAKE_LIBS_X11SM        =
QMAKE_LIBS_QT_THREAD    =
QMAKE_LIBS_QT_OPENGL    =
QMAKE_LIBS_QTOPIA       =
QMAKE_LIBS_THREAD       =
QMAKE_LIBS_EGL          =
QMAKE_LIBS_OPENGL       =
QMAKE_LIBS_OPENGL_QT    =
QMAKE_LIBS_OPENGL_ES1   = -lGLESv1_CM
QMAKE_LIBS_OPENGL_ES2   = -lGLESv2 $$QMAKE_LIBS

load(qt_config)
