static {
    message("QML cache generation ahead of time is not supported in static builds")
    return()
}

android {
    message("QML cache generation ahead of time is not supported on Android")
    return()
}

qtPrepareTool(QML_CACHEGEN, qmlcachegen, _ARCH_CHECK)

isEmpty(TARGETPATH): error("Must set TARGETPATH (QML import name) for ahead-of-time QML cache generation")

!isEmpty(QT_TARGET_ARCH):QML_CACHEGEN_ARCH=$$QT_TARGET_ARCH
else:QML_CACHEGEN_ARCH=$$QT_ARCH

!isEmpty(QT_TARGET_BUILDABI):QML_CACHEGEN_ABI=$$QT_TARGET_BUILDABI
else:QML_CACHEGEN_ABI=$$QT_BUILDABI

QML_CACHEGEN_ARGS=--target-architecture=$$QML_CACHEGEN_ARCH --target-abi=$$QML_CACHEGEN_ABI

!system($$QML_CACHEGEN_ARCH_CHECK $$QML_CACHEGEN_ARGS --check-if-supported) {
    message("QML cache generation requested but target architecture $$QML_CACHEGEN_ARCH is not supported.")
    return()
}

load(qt_build_paths)

prefix_build: QMLCACHE_DESTDIR = $$MODULE_BASE_OUTDIR/qml/$$TARGETPATH
else: QMLCACHE_DESTDIR = $$[QT_INSTALL_QML]/$$TARGETPATH

CACHEGEN_FILES=
qmlcacheinst.files =
for(qmlf, QML_FILES) {
    contains(qmlf,.*\\.js$)|contains(qmlf,.*\\.qml$) {
        CACHEGEN_FILES += $$absolute_path($$qmlf, $$_PRO_FILE_PWD_)
        qmlcacheinst.files += $$QMLCACHE_DESTDIR/$$relative_path($$qmlf, $$_PRO_FILE_PWD_)c
    }
}

defineReplace(qmlCacheOutputFileName) {
    return($$relative_path($$QMLCACHE_DESTDIR/$$relative_path($$1, $$_PRO_FILE_PWD_)c, $$OUT_PWD))
}

qmlcacheinst.base = $$QMLCACHE_DESTDIR
qmlcacheinst.path = $$[QT_INSTALL_QML]/$$TARGETPATH
qmlcacheinst.CONFIG = no_check_exist

qmlcachegen.input = CACHEGEN_FILES
qmlcachegen.output = ${QMAKE_FUNC_FILE_IN_qmlCacheOutputFileName}
qmlcachegen.CONFIG = no_link target_predeps
qmlcachegen.commands = $$QML_CACHEGEN $$QML_CACHEGEN_ARGS -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_IN}
qmlcachegen.name = Generate QML Cache ${QMAKE_FILE_IN}
qmlcachegen.variable_out = GENERATED_FILES

!debug_and_release|!build_all|CONFIG(release, debug|release) {
    QMAKE_EXTRA_COMPILERS += qmlcachegen
    INSTALLS += qmlcacheinst
}
