CONFIG += host_$$QMAKE_HOST.arch
DEFINES += Q_HOST_NAME=\\\"$$QMAKE_HOST.arch\\\" Q_HOST_$$upper($$QMAKE_HOST.arch)

defineTest(buildQMAKEProject) {
    pro_dir = $$1
    pro_out_dir = $$shadowed($$pro_dir)
    isEmpty(QMAKE_CD): QMAKE_CD=cd
    pro_cmd_base = "$$QMAKE_CD $$system_quote($$system_path($$pro_out_dir)) &&"
    mkpath($$pro_out_dir)
    command = $$pro_cmd_base $$system_quote($$system_path($$QMAKE_QMAKE))
    CONFIG(debug, debug|release) {
        command += QMAKE_CFLAGS_DEBUG*=\"$$QMAKE_CFLAGS_DEBUG\" \
            QMAKE_CXXFLAGS_DEBUG*=\"$$QMAKE_CXXFLAGS_DEBUG\" \
            QMAKE_LFLAGS_DEBUG*=\"$$QMAKE_LFLAGS_DEBUG\" \
            CONFIG+=debug
    } else {
        command += QMAKE_CFLAGS_RELEASE*=\"$$QMAKE_CFLAGS_RELEASE\" \
            QMAKE_CXXFLAGS_RELEASE*=\"$$QMAKE_CXXFLAGS_RELEASE\" \
            QMAKE_LFLAGS_RELEASE*=\"$$QMAKE_LFLAGS_RELEASE\"
    }
    command += -spec $$QMAKESPEC $$shell_quote($$pro_dir)
    message($${command}:)
    # Qt >= 5.8
    greaterThan(QT_MINOR_VERSION, 7) {
        !equals(3, false): 1 = "( $$1 ) 2>&1"
        output = $$system("$$command", lines, result)
        !isEmpty(output): message($$output)
        !equals(result, 0): return(false)
    } else {
        system("$$command")|return(false)
    }
    load(configure)
    command = $$pro_cmd_base $$QMAKE_MAKE
    message($${command}:)
    # Qt >= 5.8
    greaterThan(QT_MINOR_VERSION, 7) {
        !equals(3, false): 1 = "( $$1 ) 2>&1"
        output = $$system("$$command", lines, result)
        !isEmpty(output): message($$output)
        !equals(result, 0): return(false)
    } else {
        system("$$command")|return(false)
    }
    return(true)
}

isEqual(TARGET, dtkcore) {
    DEEPIN_OS_RELEASE_TOOL=$$_PRO_FILE_PWD_/../bin/deepin-os-release

    !exists($$DEEPIN_OS_RELEASE_TOOL) {
        tools_dir = $$_PRO_FILE_PWD_/../tools/deepin-os-release
        !buildQMAKEProject($$tools_dir): error(Failed on build deepin-os-release)
    }
} else {
    isEmpty(QT.dtkcore.tools): error(QT += dtkcore first)
    DEEPIN_OS_RELEASE_TOOL=$${QT.dtkcore.tools}/deepin-os-release
}

!exists($$DEEPIN_OS_RELEASE_TOOL): error(\"$$DEEPIN_OS_RELEASE_TOOL\" is not exists. Install \"dtkcore-bin\" first)

defineReplace(formatString) {
    string = $$1
    string = $$replace(string, \\s+, _)
    string_count = $$size(string)

    greaterThan(string_count, 1) {
        for (item, string) {
            isEmpty(new_string): new_string = $$item
            else: new_string = $${new_string}_$${item}
        }

        return($$new_string)
    }

    return($$string)
}

DEEPIN_OS_TYPE = $$system($$DEEPIN_OS_RELEASE_TOOL --deepin-type)
DEEPIN_OS_VERSION = $$system($$DEEPIN_OS_RELEASE_TOOL --deepin-version)
DISTRIB_ID = $$system($$DEEPIN_OS_RELEASE_TOOL --product-type)
DISTRIB_RELEASE = $$system($$DEEPIN_OS_RELEASE_TOOL --product-version)

isEmpty(DISTRIB_ID): warning(No value of the "--product-type" in the process "$$DEEPIN_OS_RELEASE_TOOL")
else {
    DISTRIB_ID = $$formatString($$DISTRIB_ID)

    message("OS: $$DISTRIB_ID, Version: $$DISTRIB_RELEASE")

    QMAKE_PLATFORM += $$lower($$DISTRIB_ID)

    !isEmpty(DISTRIB_ID): DEFINES += Q_OS_$$upper($$DISTRIB_ID)
    CONFIG *= $$QMAKE_PLATFORM

    DISTRIB_RELEASE = $$formatString($$DISTRIB_RELEASE)
    DEFINES += Q_OS_VERSION=\\\"$$DISTRIB_RELEASE\\\"
}

isEmpty(DEEPIN_OS_TYPE): warning(No value of the "--deepin-type" in the process "$$DEEPIN_OS_RELEASE_TOOL")
else {
    DEEPIN_OS_TYPE = $$formatString($$DEEPIN_OS_TYPE)

    message(Deepin OS Type: $$DEEPIN_OS_TYPE)
    message(Deepin OS Version: $$DEEPIN_OS_VERSION)

    !isEmpty(DEEPIN_OS_TYPE) {
        QMAKE_PLATFORM += deepin_$$lower($$DEEPIN_OS_TYPE) deepin_dde
        DEFINES += Q_OS_DEEPIN_$$upper($$DEEPIN_OS_TYPE) DEEPIN_DDE
    }

    DEEPIN_OS_VERSION = $$formatString($$DEEPIN_OS_VERSION)
    DEFINES += Q_OS_DEEPIN_VERSION=\\\"$$DEEPIN_OS_VERSION\\\"

    CONFIG *= $$QMAKE_PLATFORM
}
