CONFIG += metatypes

qtPrepareLibExecTool(QML_TYPEREGISTRAR, qmltyperegistrar)

isEmpty(QML_IMPORT_VERSION): \
    QML_IMPORT_VERSION = $$IMPORT_VERSION

# from moc.prf
isEmpty(QML_IMPORT_MAJOR_VERSION):!isEmpty(QML_IMPORT_VERSION): \
    QML_IMPORT_MAJOR_VERSION = $$section(QML_IMPORT_VERSION, ., 0, 0)

isEmpty(QML_IMPORT_MINOR_VERSION):!isEmpty(QML_IMPORT_VERSION): \
    QML_IMPORT_MINOR_VERSION = $$section(QML_IMPORT_VERSION, ., 1, 1)

isEmpty(QML_IMPORT_NAME):!isEmpty(TARGETPATH) {
    QML_IMPORT_NAME = $$replace(TARGETPATH, "/", ".")
    QML_IMPORT_NAME = $$replace(QML_IMPORT_NAME, .$${QML_IMPORT_MAJOR_VERSION}$, '')
}

isEmpty(QML_IMPORT_NAME) {
    error("Need TARGET_PATH or QML_IMPORT_NAME in order to generate qml types.");
}

isEmpty(QML_IMPORT_MAJOR_VERSION) {
    error("Need IMPORT_VERSION, QML_IMPORT_VERSION, or QML_IMPORT_MAJOR_VERSION in order to generate qml types.");
}

isEmpty(QML_IMPORT_MINOR_VERSION) {
    QML_IMPORT_MINOR_VERSION = 0
}

TARGET_BASENAME = $$lower($$basename(TARGET))
TARGET_BASENAME ~= s/\s/_/g

isEmpty(QMLTYPES_FILENAME) {
    plugin: QMLTYPES_FILENAME = plugins.qmltypes
    else: QMLTYPES_FILENAME = $${TARGET_BASENAME}.qmltypes
}

qt_module_deps = $$replace(QT, -private$, '')
qt_module_deps += $$replace(QT_PRIVATE, -private$, '')
qt_module_deps = $$resolve_depends(qt_module_deps, "QT.", ".depends" ".run_depends")
qt_module_deps = $$replace(qt_module_deps, _private$, '')
qt_module_deps = $$unique(qt_module_deps)

# We know we need to prefer the CMake debug build type for qmake debug builds. Which of the CMake
# release build types should be preferred for a qmake release build is guesswork. We apply a
# heuristic here:
#
# If you've gone to the trouble of building a "minsizerel" Qt, you probably want to use it for your
# qmake "release" builds. Conversely, if you have both a "release" and a "relwithdebinfo" Qt, you
# probably want to use the "release" Qt build for qmake "release" builds and the "relwithdebinfo"
# one for qmake "debug" builds.
#
# If no fitting build type exists, we accept the others. On linux, for example, we typically
# have only a single "relwithdebinfo" Qt build for both debug and release builds of user projects.
build_types = minsizerel release relwithdebinfo debug
CONFIG(debug, debug|release): build_types = $$reverse(build_types)

for(dep, qt_module_deps) {
    android:ABI = _$${ANDROID_TARGET_ARCH}
    infixed_module_name = $$eval(QT.$${dep}.module)

    for(build_type, build_types) {
        isEmpty(QT_LIBINFIX) {
            metatypes_filename = $$lower($${infixed_module_name})$${ABI}_$${build_type}_metatypes.json
        } else {
            metatypes_filename = $$lower($$replace($${infixed_module_name}, $$QT_LIBINFIX, ''))$${ABI}_$${build_type}_metatypes.json
        }
        metatypes_filepath = $$[QT_INSTALL_ARCHDATA]/metatypes/$${metatypes_filename}
        exists($${metatypes_filepath}) {
            QML_FOREIGN_METATYPES += $${metatypes_filepath}
            break()
        }
    }
}


QML_TYPEREGISTRAR_FLAGS = \
    --generate-qmltypes=$$QMLTYPES_FILENAME \
    --import-name=$$QML_IMPORT_NAME \
    --major-version=$$QML_IMPORT_MAJOR_VERSION \
    --minor-version=$$QML_IMPORT_MINOR_VERSION \
    --foreign-types=$$join(QML_FOREIGN_METATYPES, ',')

!isEmpty(QML_PAST_MAJOR_VERSIONS) {
    for(past_major_version,QML_PAST_MAJOR_VERSIONS): QML_TYPEREGISTRAR_FLAGS += --past-major-version $$past_major_version
}

!isEmpty(MODULE_PRIVATE_INCLUDES): QML_TYPEREGISTRAR_FLAGS += --private-includes

METATYPES_JSON = $${TARGET_BASENAME}_metatypes.json

TYPEREGISTRATIONS = $${TARGET_BASENAME}_qmltyperegistrations$${first(QMAKE_EXT_CPP)}

qmltyperegistrar_compiler.CONFIG += combine
qmltyperegistrar_compiler.commands = \
    $$QML_TYPEREGISTRAR $$QML_TYPEREGISTRAR_FLAGS -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_IN}
qmltyperegistrar_compiler.input = METATYPES_JSON
qmltyperegistrar_compiler.output = $$TYPEREGISTRATIONS
qmltyperegistrar_compiler.variable_out = SOURCES
qmltyperegistrar_compiler.name = Automatic QML type registration
qmltyperegistrar_compiler.dependency_type = TYPE_C
QMAKE_EXTRA_COMPILERS += qmltyperegistrar_compiler

!contains(TEMPLATE, "vc.*") {  # work around QTBUG-91033
    # Create a fake extra compiler to announce that we generate $$QMLTYPES_FILENAME.
    # This allows us to use $$QMLTYPES_FILENAME as input in other extra compilers.
    qmltyperegistrar_qmltypes.input = METATYPES_JSON
    qmltyperegistrar_qmltypes.depends = $$TYPEREGISTRATIONS
    qmltyperegistrar_qmltypes.output = $$QMLTYPES_FILENAME
    qmltyperegistrar_qmltypes.CONFIG = no_link
    qmltyperegistrar_qmltypes.commands = $$escape_expand(\\n)  # force creation of rule
    QMAKE_EXTRA_COMPILERS += qmltyperegistrar_qmltypes
}

install_qmltypes {
    INSTALL_QML_FILES = false

    android {
        build_pass {
            isEmpty(ANDROID_ABIS): ANDROID_ABIS = $$ALL_ANDROID_ABIS
            ABI = $$first(ANDROID_ABIS)
            equals(ABI,  $$QT_ARCH): INSTALL_QML_FILES = true
        }
    } else: !debug_and_release|!build_all|CONFIG(release, debug|release): INSTALL_QML_FILES = true

    equals(INSTALL_QML_FILES, true) {
        isEmpty(QMLTYPES_INSTALL_DIR): \
            QMLTYPES_INSTALL_DIR = $$[QT_INSTALL_QML]/$$TARGETPATH
        do_install_qmltypes.files = $$OUT_PWD/$$QMLTYPES_FILENAME
        do_install_qmltypes.path = $$QMLTYPES_INSTALL_DIR
        do_install_qmltypes.CONFIG += no_check_exist
        prefix_build: INSTALLS += do_install_qmltypes
        else: COPIES += do_install_qmltypes
    }
}
