#
# qmake configuration for all symbian mkspecs
#

TEMPLATE		= app
CONFIG			+= prepend_includepath qt warn_on release incremental link_prl sis_targets run_on_phone
QT			+= core gui
QMAKE_INCREMENTAL_STYLE = sublib

DEFINES			+= UNICODE QT_KEYPAD_NAVIGATION QT_SOFTKEYS_ENABLED QT_USE_MATH_H_FLOATS

QMAKE_COMPILER_DEFINES  += SYMBIAN

QMAKE_EXT_OBJ           = .o
QMAKE_EXT_RES           = _res.o
QMAKE_EXTENSION_SHLIB   = dll
QMAKE_EXTENSION_PLUGIN  = dll
QMAKE_EXTENSION_STATICLIB = lib

QMAKE_LEX		= flex
QMAKE_LEXFLAGS		=
QMAKE_YACC		= byacc
QMAKE_YACCFLAGS		= -d
QMAKE_CFLAGS		=
QMAKE_CFLAGS_DEPS	=
QMAKE_CFLAGS_WARN_ON	=
QMAKE_CFLAGS_WARN_OFF	=
QMAKE_CFLAGS_RELEASE	=
QMAKE_CFLAGS_DEBUG	=
QMAKE_CFLAGS_YACC	= -Wno-unused -Wno-parentheses

MMP_OPTION_KEYWORDS = CW ARMCC GCCE
VERSION_FLAGS.ARMCC = ARMCC_4_0
QMAKE_CXXFLAGS		= $$QMAKE_CFLAGS
# Symbian build system applies -cwd source on the MWCC command line.
# this causes problems with include paths, -cwd include uses the same
# rules for include paths as ARMCC
# This should really be fixed in raptor, as using CXXFLAGS means we pass
# both on the command line and rely on the compiler using the last specified
QMAKE_CXXFLAGS.CW	= -cwd include
QMAKE_CXXFLAGS.ARMCC      = --visibility_inlines_hidden
QMAKE_CXXFLAGS.ARMCC_4_0  = --import_all_vtbl
QMAKE_CXXFLAGS.GCCE	= -fvisibility-inlines-hidden
QMAKE_CXXFLAGS_DEPS	= $$QMAKE_CFLAGS_DEPS
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_YACC	= $$QMAKE_CFLAGS_YACC
QMAKE_CXXFLAGS_THREAD	= $$QMAKE_CFLAGS_THREAD
QMAKE_CXXFLAGS_RTTI_ON	=
QMAKE_CXXFLAGS_RTTI_OFF	=
QMAKE_CXXFLAGS_EXCEPTIONS_ON =
QMAKE_CXXFLAGS_EXCEPTIONS_OFF =

QMAKE_INCDIR		=
QMAKE_INCDIR_QT		= $$[QT_INSTALL_HEADERS]

QMAKE_LFLAGS            =
QMAKE_LFLAGS.ARMCC      =
QMAKE_LFLAGS.GCCE       =
QMAKE_LFLAGS_EXCEPTIONS_ON =
QMAKE_LFLAGS_EXCEPTIONS_OFF =
QMAKE_LFLAGS_RELEASE    =
QMAKE_LFLAGS_DEBUG      =
QMAKE_LFLAGS_CONSOLE    =
QMAKE_LFLAGS_WINDOWS    =
QMAKE_LFLAGS_DLL        =
QMAKE_LINK_OBJECT_MAX   =
QMAKE_LINK_OBJECT_SCRIPT=

QMAKE_LIBS		= -llibc -llibm -leuser -llibdl
QMAKE_LIBS_CORE         = $$QMAKE_LIBS -lefsrv -lhal -lbafl -lapparc
QMAKE_LIBS_CFBSBITMAP   = -lfbscli -lbitgdi -lgdi
QMAKE_LIBS_GUI          = $$QMAKE_LIBS_CORE $$QMAKE_LIBS_CFBSBITMAP -lws32 -lapgrfx -lcone -leikcore -lmediaclientaudio -lcentralrepository
QMAKE_LIBS_NETWORK      = 
QMAKE_LIBS_EGL          = -llibEGL
QMAKE_LIBS_OPENGL       = -llibGLESv2
QMAKE_LIBS_OPENGL_ES1   = -llibGLESv1_CM
QMAKE_LIBS_OPENGL_ES2   = -llibGLESv2
QMAKE_LIBS_OPENGL_QT     = $$QMAKE_LIBS_OPENGL $$QMAKE_LIBS_CFBSBITMAP -lcone -lws32
QMAKE_LIBS_OPENGL_ES1_QT = $$QMAKE_LIBS_OPENGL_ES1 $$QMAKE_LIBS_CFBSBITMAP -lcone -lws32
QMAKE_LIBS_OPENGL_ES2_QT = $$QMAKE_LIBS_OPENGL_ES2 $$QMAKE_LIBS_CFBSBITMAP -lcone -lws32
QMAKE_LIBS_OPENVG       = $$QMAKE_LIBS_CFBSBITMAP -llibOpenVG
QMAKE_LIBS_THREAD       = -llibpthread
QMAKE_LIBS_COMPAT       = 
QMAKE_LIBS_S60          = -lavkon -leikcoctl

exists($${EPOCROOT}epoc32/include/platform/mw/akntranseffect.h) {
    QMAKE_LIBS_S60 += -lgfxtrans
}

exists($${EPOCROOT}epoc32/include/platform/sgresource/sgimage.h) {
    QMAKE_LIBS_OPENVG += -lsgresource
    QMAKE_LIBS_OPENGL_QT += -lsgresource
    QMAKE_LIBS_OPENGL_ES1_QT += -lsgresource
    QMAKE_LIBS_OPENGL_ES2_QT += -lsgresource
}

contains(QMAKE_HOST.os,Windows) {
    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_MOC            = $$[QT_INSTALL_BINS]\\moc.exe
    QMAKE_UIC            = $$[QT_INSTALL_BINS]\\uic.exe
    QMAKE_IDC            = $$[QT_INSTALL_BINS]\\idc.exe
} else {
    QMAKE_COPY           = cp
    QMAKE_COPY_DIR       = cp -r
    QMAKE_MOVE           = mv
    QMAKE_DEL_FILE       = rm -f
    QMAKE_MKDIR          = mkdir -p
    QMAKE_DEL_DIR        = rmdir
    QMAKE_DEL_TREE       = rm -rf
    QMAKE_CHK_DIR_EXISTS = test -d

    QMAKE_MOC            = $$[QT_INSTALL_BINS]/moc
    QMAKE_UIC            = $$[QT_INSTALL_BINS]/uic
    QMAKE_IDC            = $$[QT_INSTALL_BINS]/idc
}

QMAKE_IDL		  = midl
QMAKE_ZIP		  = zip -r -9
QMAKE_UNZIP               = unzip -o

QMAKE_WRITE_DEFAULT_RC    = 1

QMAKE_TAR                 = tar -cf
QMAKE_GZIP                = gzip -9f

QT_ARCH               = symbian

load(qt_config)

QMAKE_LIBS_QT_ENTRY     = -lqtmain$${QT_LIBINFIX}.lib
QMAKE_LIBS_NO_QT_ENTRY  = -llibcrt0.lib
QMAKE_LIBS_NO_QT_ENTRY_GCCE  = -llibcrt0_gcce.lib

# List of languages that have a Symbian language code mapping provided in localize_deployment.prf
SYMBIAN_SUPPORTED_LANGUAGES = \
    af sq am ar hy bn bg my be ca \
    hr cs da nl en et fi fr gd ka \
    de el gu he hi hu is id ga it \
    ja kn kk ko lo lv lt mk ms ml \
    mr mo mn nb pl pt pa ro ru sr \
    si sk sl so es sw sv tl ta te \
    th bo ti tr tk uk ur vi cy zu \
    nn eu zh gl fa st en_US fr_BE \
    pt_BR en_CA fr_CA el_CY tr_CY \
    en_TW en_HK en_CN en_JP en_TH \
    sv_FI zh_HK es_419 en_ZA fr_CH \
    de_CH it_CH zh_TW

# These directories must match what configure uses for QT_INSTALL_PLUGINS and QT_INSTALL_IMPORTS
QT_PLUGINS_BASE_DIR = /resource/qt$${QT_LIBINFIX}/plugins
QT_IMPORTS_BASE_DIR = /resource/qt$${QT_LIBINFIX}/imports

load(symbian/platform_paths)

# If environment.prf is provided by platform, load that.
# It is used for platform specific variable settings, such as SYMBIAN_VERSION.
exists($${EPOCROOT}epoc32/tools/qt/mkspecs/features/environment.prf) {
    load($${EPOCROOT}epoc32/tools/qt/mkspecs/features/environment.prf)
}

# Try to detect SDK version if it wasn't set by environment.prf
isEmpty(SYMBIAN_VERSION)|isEmpty(S60_VERSION) {
    exists($${EPOCROOT}epoc32/release/winscw/udeb/z/system/install/series60v5.4.sis)|exists($${EPOCROOT}epoc32/data/z/system/install/series60v5.4.sis) {
        isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = 5.4
        isEmpty(S60_VERSION): S60_VERSION = 5.4
    } else {
        exists($${EPOCROOT}epoc32/release/winscw/udeb/z/system/install/series60v5.3.sis)|exists($${EPOCROOT}epoc32/data/z/system/install/series60v5.3.sis) {
            isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = 5.3
            isEmpty(S60_VERSION): S60_VERSION = 5.3
        } else {
            # The Symbian^3 PDK does not necessarily contain the required sis files.
            # However, libstdcppv5 first appeared in Symbian^3 (S60 5.2), so check for that too.
            exists($${EPOCROOT}epoc32/release/winscw/udeb/z/system/install/series60v5.2.sis)|exists($${EPOCROOT}epoc32/data/z/system/install/series60v5.2.sis)|exists($${EPOCROOT}epoc32/release/armv5/lib/libstdcppv5.dso) {
                isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = Symbian3
                isEmpty(S60_VERSION): S60_VERSION = 5.2
            } else {
                exists($${EPOCROOT}epoc32/release/winscw/udeb/z/system/install/series60v5.1.sis)|exists($${EPOCROOT}epoc32/data/z/system/install/series60v5.1.sis) {
                    isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = Symbian2
                    isEmpty(S60_VERSION): S60_VERSION = 5.1
                } else {
                    exists($${EPOCROOT}epoc32/release/winscw/udeb/z/system/install/series60v5.0.sis)|exists($${EPOCROOT}epoc32/data/z/system/install/series60v5.0.sis) {
                        isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = 9.4
                        isEmpty(S60_VERSION): S60_VERSION = 5.0
                    } else {
                        exists($${EPOCROOT}epoc32/release/winscw/udeb/z/system/install/series60v3.2.sis)|exists($${EPOCROOT}epoc32/data/z/system/install/series60v3.2.sis) {
                            isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = 9.3
                            isEmpty(S60_VERSION): S60_VERSION = 3.2
                        } else {
                            exists($${EPOCROOT}epoc32/release/winscw/udeb/z/system/install/series60v3.1.sis)|exists($${EPOCROOT}epoc32/data/z/system/install/series60v3.1.sis) {
                                isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = 9.2
                                isEmpty(S60_VERSION): S60_VERSION = 3.1
                            } else {
                                isEmpty(SYMBIAN_VERSION): SYMBIAN_VERSION = Unknown
                                isEmpty(S60_VERSION): S60_VERSION = Unknown
                            }
                        }
                    }
                }
            }
        }
    }
}

# pkg_depends_webkit, pkg_depends_core, and pkg_platform_dependencies can be removed by developer
# if multiple languages need to be supported by pkg file. In that case the developer should declare
# multiple language compatible dependency statements him/herself.

default_deployment.pkg_prerules += pkg_depends_webkit pkg_depends_qt pkg_platform_dependencies
DEPLOYMENT += default_deployment default_bin_deployment default_resource_deployment default_reg_deployment

defineReplace(symbianRemoveSpecialCharacters) {
    # Produce identical string to what SymbianCommonGenerator::removeSpecialCharacters

    fixedStr = $$1

    fixedStr = $$replace(fixedStr, /,_)
    fixedStr = $$replace(fixedStr, \\\\,_)
    fixedStr = $$replace(fixedStr, " ",_)
    fixedStr = $$replace(fixedStr, :,_)

    return ($$fixedStr)
}

# Determines translations that are Symbian supported
defineTest(matchSymbianLanguages) {
    # Qt language codes for which we need to parse .ts file
    SYMBIAN_MATCHED_LANGUAGES =
    # List of translation files for matched languages
    SYMBIAN_MATCHED_TRANSLATIONS =
    # List of Qt language codes for which we have no mapped Symbian code but we have a fallback code
    # and therefore need to generate a mapping for in localize_deployment.prf.
    # The fallback code means plain language code for languages that have both language and country codes.
    # E.g. the fallback code for language "zh_CN" would be "zh".
    SYMBIAN_UNMAPPED_LANGUAGES =
    # List of handled Qt language codes to avoid duplicate Symbian language codes in case both
    # unmapped language+country combination and its fallback code, or multiple unmapped language+country
    # combinations that have same fallback code are included.
    HANDLED_LANGUAGES =

    # Cannot parse .ts file for language here, so detect it from filename.
    # Allow two and three character language and country codes.
    for(translation, TRANSLATIONS) {
        language = $$replace(translation, "^(.*/)?[^/]+_(([^_]{2,3}_)?[^_]{2,3})\\.ts$", \\2)
        !contains(HANDLED_LANGUAGES, $$language) {
            HANDLED_LANGUAGES += $$language
            # Make sure translation path is absolute or shadow builds will not work
            !contains(translation, "(^/|^\\\\|^.:).*"): translation = $$_PRO_FILE_PWD_/$$translation

            contains(SYMBIAN_SUPPORTED_LANGUAGES, $$language) {
                SYMBIAN_MATCHED_LANGUAGES += $$language
                SYMBIAN_MATCHED_TRANSLATIONS += $$translation
            } else {
                # No direct mapping for specified language found. Check if a fallback language code can be used.
                strippedLanguage = $$replace(language, "_.*$",)
                contains(SYMBIAN_SUPPORTED_LANGUAGES, $$strippedLanguage):!contains(HANDLED_LANGUAGES, $$strippedLanguage) {
                    HANDLED_LANGUAGES += $$strippedLanguage
                    SYMBIAN_UNMAPPED_LANGUAGES += $$language
                    SYMBIAN_MATCHED_LANGUAGES += $$language
                    SYMBIAN_MATCHED_TRANSLATIONS += $$translation
                    SYMBIAN_LANGUAGE_FALLBACK.$$language = $$strippedLanguage
                    export(SYMBIAN_LANGUAGE_FALLBACK.$$language)
                }
            }
        }
    }

    isEmpty(SYMBIAN_MATCHED_LANGUAGES): SYMBIAN_MATCHED_LANGUAGES = en

    export(SYMBIAN_MATCHED_LANGUAGES)
    export(SYMBIAN_MATCHED_TRANSLATIONS)
    export(SYMBIAN_UNMAPPED_LANGUAGES)
}

# Symbian pkg files that define multiple languages require a language specific string to be
# defined for various items, even though the string often needs to be same for all languages.
# This replacement method will generate correct string for such cases based on TRANSLATIONS
# variable.
defineReplace(addLanguageDependentPkgItem) {
    localize_deployment:!isEmpty(TRANSLATIONS):isEmpty(SYMBIAN_MATCHED_LANGUAGES) {
        matchSymbianLanguages()
    }

    pkgItem = $$eval($$1)
    pkgLanguageList =

    for(dummyItem, SYMBIAN_MATCHED_LANGUAGES) {
        pkgLanguageList += "\"$$pkgItem\""
    }

    isEmpty(pkgLanguageList): pkgLanguageList = "\"$$pkgItem\""

    return($$join(pkgLanguageList,",",,))
}
