symbian-armcc {
    QMAKE_CFLAGS += $$QMAKE_CFLAGS.ARMCC
    QMAKE_CXXFLAGS += $$QMAKE_CXXFLAGS.ARMCC
    QMAKE_LFLAGS += $$QMAKE_LFLAGS.ARMCC
    # This is to prevent inclusion of the shipped RVCT headers, which are often in the
    # environment variable RVCTxxINC by default. -J prevents the searching of that location,
    # but needs a path, so just specify somewhere guaranteed not to contain header files.
    QMAKE_CFLAGS += -J$${EPOCROOT}epoc32/ignore_this_path
    QMAKE_CXXFLAGS += -J$${EPOCROOT}epoc32/ignore_this_path
} else:symbian-gcce {
    QMAKE_CFLAGS += $$QMAKE_CFLAGS.GCCE
    QMAKE_CXXFLAGS += $$QMAKE_CXXFLAGS.GCCE
    QMAKE_LFLAGS += $$QMAKE_LFLAGS.GCCE
}

# We need a target name without the INFIX'ed part, since flags are not infixed.
equals(QMAKE_TARGET_PRODUCT, Qt4):clean_TARGET = $$replace(TARGET, "$${QT_LIBINFIX}$", "")
else:clean_TARGET = $$TARGET

# we have some module specific options (defined in qt.prf) lets add them
!contains(clean_TARGET, ".*[ -/].*"):eval(TMPVAR = \$\$QMAKE_$${clean_TARGET}_CXXFLAGS)
!isEmpty(TMPVAR):QMAKE_CXXFLAGS += $$TMPVAR
!contains(clean_TARGET, ".*[ -/].*"):eval(TMPVAR = \$\$QMAKE_$${clean_TARGET}_LFLAGS)
!isEmpty(TMPVAR) {
    QMAKE_LFLAGS += $$TMPVAR
} else :symbian-gcce {  # lets provide a simple default. Without elf2e32 complains
    QMAKE_LFLAGS += -Ttext 0x80000 -Tdata 0x400000
}

qtPrepareTool(QMAKE_ELF2E32_WRAPPER, elf2e32_qtwrapper)

isEmpty(TARGET.EPOCSTACKSIZE):TARGET.EPOCSTACKSIZE = 0x14000
isEmpty(TARGET.EPOCHEAPSIZE):TARGET.EPOCHEAPSIZE = 0x020000 0x800000
epoc_heap_size = $$split(TARGET.EPOCHEAPSIZE, " ")
epoc_heap_size = $$join(epoc_heap_size, ",")

symbianObjdir=$$OBJECTS_DIR
isEmpty(symbianObjdir) {
    symbianObjdir = .
}
symbianDestdir=$$DESTDIR
isEmpty(symbianDestdir) {
    symbianDestdir = .
}
baseTarget = $$symbianRemoveSpecialCharacters($$basename(TARGET))
!equals(TARGET, "$$baseTarget"):symbianDestdir = $$symbianDestdir/$$dirname(TARGET)

contains(QMAKE_CFLAGS, "--thumb")|contains(QMAKE_CXXFLAGS, "--thumb")|contains(QMAKE_CFLAGS, "-mthumb")|contains(QMAKE_CXXFLAGS, "-mthumb") {
    DEFINES += __MARM_THUMB__
}

defineReplace(processSymbianLibrary) {
    qt_library = $$replace(1, "\\.dll$", ".dso")
    qt_library = $$replace(qt_library, "^-l", "")
    isFullName = $$find(qt_library, \\.)
    isEmpty(isFullName):qt_library="$${qt_library}.dso"
    return($$qt_library)
}

# This part turn "-llibc" into "libc.dso", and moves -L entries to QMAKE_LIBDIR.
libsToProcess = LIBS LIBS_PRIVATE QMAKE_LIBS
for(libToProcess, libsToProcess) {
    qt_libraries = $$split($$libToProcess, " ")
    eval($$libToProcess =)
    for(qt_library, qt_libraries) {
        contains(qt_library, "^-L.*") {
            qt_library = $$replace(qt_library, "^-L", "")
            QMAKE_LIBDIR += $$qt_library
        } else {
            qt_newLib = $$processSymbianLibrary($$qt_library)
            contains(qt_newLib, ".*\\.dso$")|contains(qt_newLib, ".*\\.lib$"):PRE_TARGETDEPS += $$qt_newLib
            symbian-gcce:qt_newLib = "-l:$$qt_newLib"
            eval($$libToProcess *= \$\$qt_newLib)
        }
    }
}

elf2e32_LIBPATH =
for(libPath, QMAKE_LIBDIR) {
    elf2e32_LIBPATH += "--libpath=$$libPath"
}

isEmpty(VERSION) {
    VERSION = $$QT_VERSION
}

# Check for version validity.
!isEmpty(VERSION):!contains(VERSION, "[0-9]+"):!contains(VERSION, "[0-9]+\\.[0-9]+")!contains(VERSION, "[0-9]+(\\.[0-9]+){2}") {
    error("Invalid VERSION for Symbian: $$VERSION")
}

splitVersion = $$split(VERSION, ".")
count(splitVersion, 0) {
    # Default Symbian version if none is specified.
    hexVersion = "000a0000"
    decVersion = "10.0"
} else {
    count(splitVersion, 3) {
        hexVersion = $$system("perl -e \"printf (\\\"%02x\\\", $$member(splitVersion, 0))\"")
        hexPart2 = $$system("perl -e \"printf (\\\"%02x\\\", $$member(splitVersion, 1))\"")
        hexPart2 = $$hexPart2$$system("perl -e \"printf (\\\"%02x\\\", $$member(splitVersion, 2))\"")
        decVersion = $$system("perl -e \"printf (\\\"%1d\\\", 0x$$hexVersion)\"").
        hexVersion = $$hexVersion$$hexPart2
        decVersion = $$decVersion$$system("perl -e \"printf (\\\"%d\\\", 0x$$hexPart2)\"")

        !contains(hexVersion, "[0-9a-f]{8}"):hexVersion = "00$${hexVersion}"
    } else {  # app code may have different numbering...
        hexVersion = $$VERSION
        decVersion = $$VERSION
    }
}
#error ("hexVersion: $$hexVersion, decVersion: $$decVersion")

intUid3 = $$lower($$replace(TARGET.UID3, "^0x", ""))
isEmpty(TARGET.SID):TARGET.SID = $$TARGET.UID3
isEmpty(TARGET.UID2):TARGET.UID2 = 0x00000000

capability = $$replace(TARGET.CAPABILITY, " ", "+")
capability = $$join(capability, "+")
capability = $$replace(capability, "\\+-", "-")
isEmpty(capability): capability = "None"
capability = "--capability=$$capability"

contains(TEMPLATE, lib):!contains(CONFIG, static):!contains(CONFIG, staticlib) {
    contains(CONFIG, plugin):QMAKE_ELF2E32_FLAGS += --definput=plugin_commonu.def

    !isEmpty(QMAKE_POST_LINK):QMAKE_POST_LINK = $$escape_expand(\\n\\t)$$QMAKE_POST_LINK
    moveCmd = $$QMAKE_MOVE $$symbianDestdir/$${baseTarget}.dll $$symbianDestdir/$${baseTarget}.sym
    contains(QMAKE_HOST.os,Windows):moveCmd = $$replace(moveCmd, /, \\)
    QMAKE_POST_LINK = $$moveCmd \
                      && $$QMAKE_ELF2E32_WRAPPER --version=$$decVersion \
                      --sid=$$TARGET.SID \
                      --uid1=0x10000079 \
                      --uid2=$$TARGET.UID2 \
                      --uid3=$$TARGET.UID3 \
                      --targettype=DLL \
                      --elfinput=$${symbianDestdir}/$${baseTarget}.sym \
                      --output=$${symbianDestdir}/$${baseTarget}.dll \
                      --tmpdso=$${symbianObjdir}/$${baseTarget}.dso \
                      --dso=$${symbianDestdir}/$${baseTarget}.dso \
                      --defoutput=$$symbianObjdir/$${baseTarget}.def \
                      --linkas=\"$${baseTarget}{$${hexVersion}}[$${intUid3}].dll\" \
                      --heap=$$epoc_heap_size \
                      --stack=$$TARGET.EPOCSTACKSIZE \
                      $$elf2e32_LIBPATH \
                      $$capability \
                      $$QMAKE_ELF2E32_FLAGS \
                      $$QMAKE_POST_LINK
    silent:QMAKE_POST_LINK = @echo postlinking $@ && $$QMAKE_POST_LINK
    QMAKE_DISTCLEAN += $${symbianDestdir}/$${baseTarget}.sym
    QMAKE_DISTCLEAN += $${symbianDestdir}/$${baseTarget}.dso
    QMAKE_CLEAN += $${symbianObjdir}/$${baseTarget}.dso
    QMAKE_CLEAN += $${symbianObjdir}/$${baseTarget}.def

    symbian-armcc: {
        LIBS += usrt2_2.lib dfpaeabi.dso dfprvct2_2.dso drtaeabi.dso scppnwdl.dso drtrvct2_2.dso
        # Quotation unfortunately is different on Windows and unix.
        contains(QMAKE_HOST.os, Windows) {
            LIBS += \"h_t__uf.l(switch8.o)\" edllstub.lib \"edll.lib(uc_dll_.o)\"
        } else {
            LIBS += h_t__uf.l\\(switch8.o\\) edllstub.lib edll.lib\\(uc_dll_.o\\)
        }
    } else :symbian-gcce {
        LIBS += \
            -l:edllstub.lib \
            -l:edll.lib \
            -l:usrt2_2.lib \
            -l:dfpaeabi.dso \
            -l:drtaeabi.dso \
            -l:scppnwdl.dso \
            -lsupc++ \
            -lgcc
    }

    QMAKE_LFLAGS += --soname \"$${baseTarget}{$${hexVersion}}[$${intUid3}].dll\"
    DEFINES += __DLL__
}

contains(TEMPLATE, app):!contains(QMAKE_LINK, "^@:.*") {
    !isEmpty(QMAKE_POST_LINK):QMAKE_POST_LINK = $$escape_expand(\\n\\t)$$QMAKE_POST_LINK
    moveCmd = $$QMAKE_MOVE $$symbianDestdir/$${baseTarget} $$symbianDestdir/$${baseTarget}.sym
    contains(QMAKE_HOST.os,Windows):moveCmd = $$replace(moveCmd, /, \\)
    QMAKE_POST_LINK = $$moveCmd \
                      && $$QMAKE_ELF2E32_WRAPPER --version $$decVersion \
                      --sid=$$TARGET.SID \
                      --uid1=0x1000007a \
                      --uid2=$$TARGET.UID2 \
                      --uid3=$$TARGET.UID3 \
                      --targettype=EXE \
                      --elfinput=$${symbianDestdir}/$${baseTarget}.sym \
                      --output=$${symbianDestdir}/$${baseTarget}.exe \
                      --linkas=\"$${baseTarget}{$${hexVersion}}[$${intUid3}].exe\" \
                      --heap=$$epoc_heap_size \
                      --stack=$$TARGET.EPOCSTACKSIZE \
                      $$elf2e32_LIBPATH \
                      $$capability \
                      $$QMAKE_ELF2E32_FLAGS \
                      && $$QMAKE_COPY "$${symbianDestdir}/$${baseTarget}.exe" "$${symbianDestdir}/$${baseTarget}" \
                      $$QMAKE_POST_LINK
    silent:QMAKE_POST_LINK = @echo postlinking $@ && $$QMAKE_POST_LINK
    QMAKE_DISTCLEAN += $${symbianDestdir}/$${baseTarget}.sym
    QMAKE_DISTCLEAN += $${symbianDestdir}/$${baseTarget}.exe
    QMAKE_CLEAN += $${symbianDestdir}/$${baseTarget}

    symbian-armcc: {
        QMAKE_LIBS += usrt2_2.lib dfpaeabi.dso dfprvct2_2.dso drtaeabi.dso scppnwdl.dso drtrvct2_2.dso h_t__uf.l\\(switch8.o\\)
        QMAKE_LIBS += -leexe.lib\\(uc_exe_.o\\)
        contains(CONFIG, "qt") {
            QMAKE_LIBS -= $$QMAKE_LIBS_QT_ENTRY
            QMAKE_LIBS += $$QMAKE_LIBS_QT_ENTRY
        } else {
            QMAKE_LIBS -= $$QMAKE_LIBS_NO_QT_ENTRY
            QMAKE_LIBS += $$QMAKE_LIBS_NO_QT_ENTRY
        }
    } else :symbian-gcce {
        # notice that we can't merge these as ordering of arguments is important.
        QMAKE_LIBS += \
            -l:eexe.lib \
            -l:usrt2_2.lib
        contains(CONFIG, "qt") {
            modified_entry = $$replace(QMAKE_LIBS_QT_ENTRY, "^-l", "-l:")
            QMAKE_LIBS -= $$modified_entry
            QMAKE_LIBS += $$modified_entry
        } else {
            modified_entry = $$replace(QMAKE_LIBS_NO_QT_ENTRY, "^-l", "-l:")
            QMAKE_LIBS -= $$modified_entry
            modified_entry = $$replace(QMAKE_LIBS_NO_QT_ENTRY_GCCE, "^-l", "-l:")
            QMAKE_LIBS -= $$modified_entry
            QMAKE_LIBS += $$modified_entry
        }
        QMAKE_LIBS += \
            -l:dfpaeabi.dso \
            -l:drtaeabi.dso \
            -l:scppnwdl.dso \
            -lsupc++ \
            -lgcc
        QMAKE_LFLAGS += --shared
    }

    QMAKE_LFLAGS += --soname \"$${baseTarget}{$${hexVersion}}[$${intUid3}].exe\"
    DEFINES += __EXE__
}

# Symbian resource files
symbian_resources_INCLUDES = $$join(INCLUDEPATH, " -I", "-I")
symbian_resources_DEFINES = $$join(DEFINES, " -D", "-D")
symbian_resources_DEFINES += -D__QT_SYMBIAN_RESOURCE__
symbian_resources_RCC_DIR = $$replace(RCC_DIR, "/$", "")
symbian_resources_INCLUDES += "-I$$symbian_resources_RCC_DIR"

for(symbian_resource, SYMBIAN_RESOURCES) {
    symbian_resource = $$basename(symbian_resource)
    symbian_resource_clean = $$replace(symbian_resource, "\\.rss$", ".rsc")
    QMAKE_DISTCLEAN += $${symbianDestdir}/$${symbian_resource_clean}
    symbian_resource_clean = $$replace(symbian_resource, "\\.rss$", ".rpp")
    QMAKE_CLEAN += $${symbian_resources_RCC_DIR}/$${symbian_resource_clean}
}

symbianresources.input = SYMBIAN_RESOURCES
symbianresources.output = $$symbian_resources_RCC_DIR/${QMAKE_FILE_BASE}$${QT_LIBINFIX}.rsg
symbianresources.commands = cpp -nostdinc -undef \
                            -include $$QMAKE_SYMBIAN_INCLUDES \
                            $$symbian_resources_INCLUDES \
                            $$symbian_resources_DEFINES \
                            ${QMAKE_FILE_NAME} \
                            > $${symbian_resources_RCC_DIR}/${QMAKE_FILE_BASE}.rpp \
                            && rcomp -u -m045,046,047 \
                            -s$${symbian_resources_RCC_DIR}/${QMAKE_FILE_BASE}.rpp \
                            -o$${symbianDestdir}/${QMAKE_FILE_BASE}$${QT_LIBINFIX}.rsc \
                            -h$${symbian_resources_RCC_DIR}/${QMAKE_FILE_BASE}$${QT_LIBINFIX}.rsg \
                            -i${QMAKE_FILE_NAME}
silent:symbianresources.commands = @echo rcomp $< && $$symbianresources.commands
symbianresources.dependency_type = TYPE_C
symbianresources.CONFIG = no_link target_predeps

QMAKE_EXTRA_COMPILERS += symbianresources

# This section generates the rsg and rsc files for symbian.
contains(TEMPLATE, "app"):!contains(CONFIG, "no_icon") {
    # Look for extra languages for the resources, and then generate a target for each one.
    localize_deployment:symbianGenResourceLanguages = $$SYMBIAN_MATCHED_LANGUAGES default
    else:symbianGenResourceLanguages = default
    for(language, symbianGenResourceLanguages) {
        # Special languages get their language number appended to the filename.
        contains(language, default) {
            symbianGenResource_DEFINES = $$symbian_resources_DEFINES
            rpp = $${symbian_resources_RCC_DIR}/$${baseTarget}.rpp
            rsc = $${symbianDestdir}/$${baseTarget}.rsc
            rsg = $${symbian_resources_RCC_DIR}/$${baseTarget}.rsg
        } else {
            languageNo = $$eval(SYMBIAN_LANG.$$language)
            symbianGenResource_DEFINES = $$symbian_resources_DEFINES -DLANGUAGE_$${languageNo}
            rpp = $${symbian_resources_RCC_DIR}/$${baseTarget}_$${languageNo}.rpp
            rsc = $${symbianDestdir}/$${baseTarget}.r$${languageNo}
            rsg = $${symbian_resources_RCC_DIR}/$${baseTarget}_$${languageNo}.rsg
        }

        # Make our own extra target in order to get dependencies for generated
        # files right. This also avoids the warning about files not found.
        eval(symbianGenResource_$${language}.target = $$rsg)
        eval(symbianGenResource_$${language}.commands = cpp -nostdinc -undef \
                                      -include $$QMAKE_SYMBIAN_INCLUDES \
                                      $$symbian_resources_INCLUDES \
                                      $$symbianGenResource_DEFINES \
                                      $${baseTarget}.rss \
                                      > $$rpp \
                                      && rcomp -u -m045,046,047 \
                                      -s$$rpp \
                                      -o$$rsc \
                                      -h$$rsg \
                                      -i$${baseTarget}.rss)
        silent:eval(symbianGenResource_$${language}.commands = @echo rcomp $${baseTarget}.rss && $$eval(symbianGenResource_$${language}.commands))
        eval(symbianGenResource_$${language}.depends = $${baseTarget}.rss)
        PRE_TARGETDEPS += $$rsg
        QMAKE_CLEAN += $$rsg $$rpp
        QMAKE_DISTCLEAN += $$rsc

        QMAKE_EXTRA_TARGETS += symbianGenResource_$${language}

        # Note that we depend on the base rsg file, even if dealing with a specific language.
        # hence we don't use $$rsg on the next line.
        eval(symbianGenRegResource_$${language}.depends = $${baseTarget}_reg.rss $${symbian_resources_RCC_DIR}/$${baseTarget}.rsg)
        contains(language, default) {
            rpp = $${symbian_resources_RCC_DIR}/$${baseTarget}_reg.rpp
            rsc = $${symbianDestdir}/$${baseTarget}_reg.rsc
            rsg = $${symbian_resources_RCC_DIR}/$${baseTarget}_reg.rsg
        } else {
            rpp = $${symbian_resources_RCC_DIR}/$${baseTarget}_reg_$${languageNo}.rpp
            rsc = $${symbianDestdir}/$${baseTarget}_reg.r$${languageNo}
            rsg = $${symbian_resources_RCC_DIR}/$${baseTarget}_reg_$${languageNo}.rsg
        }
        eval(symbianGenRegResource_$${language}.target = $$rsg)
        eval(symbianGenRegResource_$${language}.commands = cpp -nostdinc -undef \
                                         -include $$QMAKE_SYMBIAN_INCLUDES \
                                         $$symbian_resources_INCLUDES \
                                         $$symbianGenResource_DEFINES \
                                         $${baseTarget}_reg.rss \
                                         > $$rpp \
                                         && rcomp -u -m045,046,047 \
                                         -s$$rpp \
                                         -o$$rsc \
                                         -h$$rsg \
                                         -i$${baseTarget}_reg.rss)
        silent:eval(symbianGenRegResource_$${language}.commands = @echo rcomp $${baseTarget}_reg.rss && $$eval(symbianGenRegResource_$${language}.commands))
        PRE_TARGETDEPS += $$rsg
        QMAKE_CLEAN += $$rsg $$rpp
        QMAKE_DISTCLEAN += $$rsc

        QMAKE_EXTRA_TARGETS += symbianGenRegResource_$${language}
    }

    # Trick to get qmake to create the RCC_DIR for us.
    symbianRccDirCreation.input = SOURCES
    symbianRccDirCreation.commands =
    symbianRccDirCreation.output = $${symbian_resources_RCC_DIR}/symbian_resource_dummy
    symbianRccDirCreation.CONFIG = no_link combine

    QMAKE_EXTRA_COMPILERS += symbianRccDirCreation

    QMAKE_DISTCLEAN += $${baseTarget}.rss \
                       $${baseTarget}_reg.rss \
                       $${baseTarget}.loc
}

# Generated pkg files

QMAKE_DISTCLEAN += $${baseTarget}_template.pkg \
                   $${baseTarget}_installer.pkg \
                   $${baseTarget}_stub.pkg
