# With DEF files enabled, removed exported symbols are treated as errors
# and there is binary compatibility between successive builds.

CONFIG -= def_files_disabled

# We need a target name without the INFIX'ed part, since DEF files are not infixed.
# If a non-Qt project uses QT_LIBINFIX, it may declare QT_LIBINFIX_USED
equals(QMAKE_TARGET_PRODUCT, Qt4)|equals(QMAKE_TARGET_PRODUCT, QTestLib)|!isEmpty(QT_LIBINFIX_USED):clean_TARGET = $$replace(TARGET, "$${QT_LIBINFIX}$", "")
else:clean_TARGET = $$TARGET

defineTest(qtTestIfDirExists) {
    contains(QMAKE_HOST.os,Windows) {
        dirToTest = $$1
        $$dirToTest ~= s,/,\\,
        # Windows trick. Test for existence of nul, which every directory has.
        retValue = $$system("if exist $$dirToTest\\nul echo true")
        contains(retValue, true):return(true)|return(false)
    } else {
        system("test -d $$1"):return(true)|return(false)
    }
}

symbian-abld|symbian-sbsv2 {
    # Firstly, if the MMP_RULES already contain a defBlock variable, don't generate another one
    # (this bit is slightly magic, because it depends upon everyone creating their DEFFILE statements
    # in a defBlock variable; but otherwise we have to expand MMP_RULES then scan for the DEFFILE keyword)
    # Similarly, explicit EXPORTUNFROZEN should block adding defBlock
    !contains(MMP_RULES, defBlock):!contains(MMP_RULES, EXPORTUNFROZEN) {
        # Apps are executables on Symbian, so don't have exports, and therefore don't have DEF files
        # Plugins use standard DEF files, which qmake generates, so shouldn't be using these DEFFILE
        # statements - they use the qmake generated statements instead
        # Static libraries obviously don't have DEF files, as they don't take part in dynamic linkage
        !contains(TEMPLATE, app):!contains(CONFIG, plugin):!contains(CONFIG, staticlib): {
            # Symbian DEFFILE statements do not like drive letters, so remove it from the pro path.
            FIXED_PRO_PATH = $$replace(_PRO_FILE_PWD_, "^.:", "")/
            !isEmpty(DEF_FILE) {
                contains(DEF_FILE,"^/.*"): FIXED_PRO_PATH =
                defBlock = \
                "$${LITERAL_HASH}ifdef WINSCW" \
                "DEFFILE $$FIXED_PRO_PATH$$DEF_FILE/bwins/$${clean_TARGET}.def" \
                "$${LITERAL_HASH}elif defined EABI" \
                "DEFFILE $$FIXED_PRO_PATH$$DEF_FILE/eabi/$${clean_TARGET}.def" \
                "$${LITERAL_HASH}endif"
            } else:!isEmpty(defFilePath) {
                contains(defFilePath,"^/.*"): FIXED_PRO_PATH =
                defBlock = \
                "$${LITERAL_HASH}ifdef WINSCW" \
                "DEFFILE $$FIXED_PRO_PATH$$defFilePath/bwins/$${clean_TARGET}.def" \
                "$${LITERAL_HASH}elif defined EABI" \
                "DEFFILE $$FIXED_PRO_PATH$$defFilePath/eabi/$${clean_TARGET}.def" \
                "$${LITERAL_HASH}endif"
            } else {
                # If defFilePath is not defined, then put the folders containing the DEF files at the
                # same level as the .pro (and generated MMP) file(s)
                defBlock = \
                "$${LITERAL_HASH}ifdef WINSCW" \
                "DEFFILE $${FIXED_PRO_PATH}bwins/$${clean_TARGET}.def" \
                "$${LITERAL_HASH}elif defined EABI" \
                "DEFFILE $${FIXED_PRO_PATH}eabi/$${clean_TARGET}.def" \
                "$${LITERAL_HASH}endif"
            }
            MMP_RULES += defBlock
        }
    }

} else:contains(TEMPLATE, lib):!contains(CONFIG, static):!contains(CONFIG, staticlib):!contains(CONFIG, plugin) {
    !isEmpty(DEF_FILE) {
        defFile = $$DEF_FILE
    } else {
        defFile = .
    }
    qtTestIfDirExists($$_PRO_FILE_PWD_/$$defFile) {
        !exists("$$_PRO_FILE_PWD_/$$defFile/eabi") {
            dirToCreate = $$_PRO_FILE_PWD_/$$defFile/eabi
            contains(QMAKE_HOST.os,Windows):dirToCreate ~= s,/,\\,
            system("$$QMAKE_MKDIR $$dirToCreate")
        }
        elf2e32FileToAdd = $$_PRO_FILE_PWD_/$$defFile/eabi/$$basename(clean_TARGET)u.def
    } else {
        elf2e32FileToAdd = $$_PRO_FILE_PWD_/$$defFile
    }
    QMAKE_ELF2E32_FLAGS += "--definput=$$elf2e32FileToAdd"

    symbianObjdir = $$OBJECTS_DIR
    isEmpty(symbianObjdir):symbianObjdir = .

    freeze_target.target = freeze
    freeze_target.depends = first
    # The perl part is to convert to unix line endings and remove comments, which the s60 tools refuse to do.
    freeze_target.commands = $$QMAKE_COPY $$symbianObjdir/$${TARGET}.def $$elf2e32FileToAdd
    QMAKE_EXTRA_TARGETS += freeze_target
} else:contains(TEMPLATE, subdirs) {
    freeze_target.target = freeze
    freeze_target.CONFIG = recursive
    freeze_target.recurse = $$SUBDIRS
    QMAKE_EXTRA_TARGETS += freeze_target
} else {
    freeze_target.target = freeze
    freeze_target.commands =
    QMAKE_EXTRA_TARGETS += freeze_target
}
