isEmpty(DTK_BUILD): error("Please load(dtk_build) first!")

# -----------------------
# Config Qt module
MODULE_ID=$$DTK_MODULE_NAME
mod_inst_pfx=$$_PRO_FILE_PWD_
MODULE_PRI = $$mod_inst_pfx/qt_lib_$${MODULE_ID}.pri
module_libs = $$LIB_INSTALL_DIR
module_tools = $$TOOL_INSTALL_DIR
MODULE_INCLUDES = $$INCLUDE_INSTALL_DIR
MODULE_DEPENDS=$$find(QT, dtk*)

message("config qt module: $$MODULE_ID")
message("write $$MODULE_PRI to: $$mod_inst_pfx")

internal_module: \
    MODULE_DEPENDS = $$replace(QT, -private$, _private)
else: \
    MODULE_DEPENDS = $$replace(QT, -private$, )
MODULE_DEPENDS = $$unique(MODULE_DEPENDS)
contains(MODULE_DEPENDS, $$MODULE): \
    error("$$MODULE_ID depends on itself.")

# Create a module .pri file
module_libs=$$LIB_INSTALL_DIR
isEmpty(module_libs) {
host_build: \
    module_libs = "\$\$QT_MODULE_HOST_LIB_BASE"
else: \
    module_libs = "\$\$QT_MODULE_LIB_BASE"
}

# In addition to the library's private deps, the private module's deps
# are logically runtime deps of the public module.
runtime_deps = $$QT_PRIVATE $$QT_FOR_PRIVATE
!isEmpty(runtime_deps): \
    module_rundep = "QT.$${MODULE_ID}.run_depends = $$replace(runtime_deps, -private$, _private)"
else: \
    module_rundep =
module_build_type = v2
static: \
    module_build_type += staticlib
lib_bundle {
    module_build_type += lib_bundle
    MODULE_FRAMEWORKS = " \$\$QT_MODULE_LIB_BASE"
}
internal_module: \
    module_build_type += internal_module
ltcg: \
    module_build_type += ltcg
module_module =
!equals(TEMPLATE, aux) {
    module_module = $$MODULE_ID$$QT_LIBINFIX
    !lib_bundle: module_module ~= s,^Qt,Qt$$QT_MAJOR_VERSION,
}
!isEmpty(MODULE_CONFIG): \
    module_config = "QT.$${MODULE_ID}.CONFIG = $$MODULE_CONFIG"
else: \
    module_config =
!isEmpty(MODULE_PLUGIN_TYPES): \
    module_plugtypes = "QT.$${MODULE_ID}.plugin_types = $$replace(MODULE_PLUGIN_TYPES, /.*$, )"
else: \
    module_plugtypes =
!isEmpty(MODULE_MASTER_HEADER): \
    module_master = "QT.$${MODULE_ID}.master_header = $$MODULE_MASTER_HEADER"
else: \
    module_master =

MODULE_PRI_CONT = \
    "QT.$${MODULE_ID}.VERSION = $${VERSION}" \
    "QT.$${MODULE_ID}.MAJOR_VERSION = $${VER_MAJ}" \
    "QT.$${MODULE_ID}.MINOR_VERSION = $${VER_MIN}" \
    "QT.$${MODULE_ID}.PATCH_VERSION = $${VER_PAT}" \
    "" \
    "QT.$${MODULE_ID}.name = $${MODULE_ID}" \
    "QT.$${MODULE_ID}.module = $$module_module" \
    "QT.$${MODULE_ID}.tools = $$module_tools" \
    "QT.$${MODULE_ID}.libs = $$module_libs" \
    $$module_master \
    "QT.$${MODULE_ID}.includes = $$MODULE_INCLUDES" \
    "QT.$${MODULE_ID}.frameworks =$$MODULE_FRAMEWORKS"

MODULE_PRI_CONT += \
    "QT.$${MODULE_ID}.depends =$$join(MODULE_DEPENDS, " ", " ")" \
    $$module_rundep \
    "QT.$${MODULE_ID}.module_config =$$join(module_build_type, " ", " ")" \
    $$module_config \
    "QT.$${MODULE_ID}.DEFINES = $$val_escape(MODULE_DEFINES)" \
    "" \
    "QT_MODULES += $$MODULE"

write_file($$MODULE_PRI, MODULE_PRI_CONT)|error("Aborting.")

qt_module.files = $$MODULE_PRI

isEmpty(MKSPECS_INSTALL_DIR) {
    MKSPECS_INSTALL_DIR=$$[QT_INSTALL_ARCHDATA]/mkspecs
}
qt_module.path = $${MKSPECS_INSTALL_DIR}/modules

INSTALLS += qt_module

PKG_MODULE_DEPENDS=$$find(QT, dtk*)

#不能添加Qt包的依赖，无法单纯的从Qt模块名称获取其pkgconfig的pc文件名
#for (qt_module, QT) {
#    #DBus模块需要特殊处理
#    isEqual(qt_module, "dbus") {
#        PKG_MODULE_DEPENDS+=Qt5DBus
#    } else {
#        suffix = $$str_member($$qt_module, -7, -1)
#        #排除以private结尾的模块
#        !isEqual(suffix, "private"): PKG_MODULE_DEPENDS+=Qt5$$capitalizingString($$qt_module)
#    }
#}

PKG_MODULE_DEPENDS = $$unique(PKG_MODULE_DEPENDS)

# -----------------------
# Config pkg-config
QMAKE_PKGCONFIG_VERSION = $$VERSION
QMAKE_PKGCONFIG_DESTDIR = pkgconfig
QMAKE_PKGCONFIG_NAME = DTK_$$upper($$MODULE_ID)
QMAKE_PKGCONFIG_DESCRIPTION = Deepin Tool Kit $$MODULE_ID header files
QMAKE_PKGCONFIG_REQUIRES += $$PKG_MODULE_DEPENDS
QMAKE_PKGCONFIG_INCDIR = $$includes.path
QMAKE_PKGCONFIG_LIBDIR = $$target.path
