#
#  W A R N I N G
#  -------------
#
# This file is not part of the Qt API.  It exists purely as an
# implementation detail.  It may change from version to version
# without notice, or even be removed.
#
# We mean it.
#

!exists($$QMAKE_DOCS): error("Cannot find documentation specification file $$QMAKE_DOCS")

qtver.name = QT_VERSION
qtver.value = $$VERSION
isEmpty(qtver.value): qtver.value = $$MODULE_VERSION
isEmpty(qtver.value): error("No version for documentation specified.")
qtmver.name = QT_VER
qtmver.value = $$replace(qtver.value, ^(\\d+\\.\\d+).*$, \\1)
qtvertag.name = QT_VERSION_TAG
qtvertag.value = $$replace(qtver.value, \\.,)
qtdocs.name = QT_INSTALL_DOCS
qtdocs.value = $$[QT_INSTALL_DOCS/src]
builddir.name = BUILDDIR
builddir.value = $$OUT_PWD
QT_TOOL_ENV = qtver qtmver qtvertag qtdocs builddir
qtPrepareTool(QDOC, qdoc)
QT_TOOL_ENV =

# On Windows, put the includes into a .inc file which QDoc will read, if the project
# has too many includes. We do this to overcome a command-line limit on Windows.
WIN_INCLUDETEMP=
INCLUDE_PATHS=$$INCPATH
win32:count(INCLUDE_PATHS, 30, >) {
    WIN_INCLUDETEMP = $$OUT_PWD/qdocincludepaths.inc
    WIN_INCLUDETEMP_CONTENT =
    for (inc, INCLUDE_PATHS): \
        WIN_INCLUDETEMP_CONTENT += -I$$inc
    write_file($$absolute_path($$WIN_INCLUDETEMP, $$OUT_PWD), WIN_INCLUDETEMP_CONTENT)|error()
}

isEmpty(WIN_INCLUDETEMP) {
    QDOC_INCLUDE_PATHS=$(INCPATH)
} else {
    QDOC_INCLUDE_PATHS=@$$shell_quote($$WIN_INCLUDETEMP)
}

macos: QDOC_INCLUDE_PATHS += $$join(QMAKE_DEFAULT_INCDIRS," -I","-I")

!build_online_docs: qtPrepareTool(QHELPGENERATOR, qhelpgenerator)

qtPrepareTool(QTATTRIBUTIONSSCANNER, qtattributionsscanner)

# qtPrepareTool() must be called outside a build pass, as it protects
# against concurrent wrapper creation by omitting it during build passes.
# However, creating the actual targets is reserved to the build passes.
debug_and_release:!build_pass: return()

load(qt_build_paths)
QMAKE_DOCS_BASE_OUTDIR = $$MODULE_BASE_OUTDIR/doc

QMAKE_DOCS_TARGET = $$replace(QMAKE_DOCS, ^(.*/)?(.*)\\.qdocconf$, \\2)
isEmpty(QMAKE_DOCS_TARGETDIR): QMAKE_DOCS_TARGETDIR = $$QMAKE_DOCS_TARGET
QMAKE_DOCS_OUTPUTDIR = $$QMAKE_DOCS_BASE_OUTDIR/$$QMAKE_DOCS_TARGETDIR

QDOC += -outputdir $$shell_quote($$QMAKE_DOCS_OUTPUTDIR)
!build_online_docs: \
    QDOC += -installdir $$shell_quote($$[QT_INSTALL_DOCS])
PREP_DOC_INDEXES =
DOC_INDEXES =
!isEmpty(QTREPOS) {
    prepare_docs {
        # This is not for linking, but for providing type information.
        mps =
        deps = $$replace(QT, -private$, )
        deps = $$resolve_depends(deps, "QT.")
        for (d, deps): \
            mps += $$dirname(QT.$${d}.libs)
        mps = $$unique(mps)
        for (mp, mps): \
            PREP_DOC_INDEXES += -indexdir $$shell_quote($$mp/doc)
    }
    for(qrep, QTREPOS): \
        DOC_INDEXES += -indexdir $$shell_quote($$qrep/doc)
} else {
    prepare_docs: \
        PREP_DOC_INDEXES += -indexdir $$shell_quote($$[QT_INSTALL_DOCS/get])
    DOC_INDEXES += -indexdir $$shell_quote($$[QT_INSTALL_DOCS/get])
}

qtattributionsscanner.target = qtattributionsscanner
qtattributionsscanner.commands = $$QTATTRIBUTIONSSCANNER $$shell_quote($$MODULE_BASE_INDIR) \
    --filter "QDocModule=$$QMAKE_DOCS_TARGET" -o $$shell_quote($$OUT_PWD/codeattributions.qdoc)
qtattributionsscanner.CONFIG += phony
QMAKE_EXTRA_TARGETS += qtattributionsscanner

doc_command = $$QDOC $$QMAKE_DOCS
prepare_docs {
    prepare_docs.commands += $$doc_command -prepare $$PREP_DOC_INDEXES -no-link-errors $$QDOC_INCLUDE_PATHS
    generate_docs.commands += $$doc_command -generate $$DOC_INDEXES $$QDOC_INCLUDE_PATHS
    prepare_docs.depends += qtattributionsscanner
} else {
    html_docs.commands += $$doc_command $$DOC_INDEXES $(QDOC_INCLUDE_PATHS)
    html_docs.depends += qtattributionsscanner
}

!build_online_docs {
    qch_docs.commands = $$QHELPGENERATOR $$shell_quote($$QMAKE_DOCS_OUTPUTDIR/$${QMAKE_DOCS_TARGET}.qhp) -o $$shell_quote($$QMAKE_DOCS_BASE_OUTDIR/$${QMAKE_DOCS_TARGET}.qch)

    inst_html_docs.files = $$QMAKE_DOCS_OUTPUTDIR
    inst_html_docs.path = $$[QT_INSTALL_DOCS]
    inst_html_docs.CONFIG += no_check_exist directory no_default_install no_build
    INSTALLS += inst_html_docs

    inst_qch_docs.files = $$QMAKE_DOCS_BASE_OUTDIR/$${QMAKE_DOCS_TARGET}.qch
    inst_qch_docs.path = $$[QT_INSTALL_DOCS]
    inst_qch_docs.CONFIG += no_check_exist no_default_install no_build
    INSTALLS += inst_qch_docs

    install_html_docs.depends = install_inst_html_docs
    uninstall_html_docs.depends = uninstall_inst_html_docs
    install_qch_docs.depends = install_inst_qch_docs
    uninstall_qch_docs.depends = uninstall_inst_qch_docs
    install_docs.depends = install_html_docs install_qch_docs
    uninstall_docs.depends = uninstall_html_docs uninstall_qch_docs
}
