cmake_minimum_required(VERSION 3.18...3.31)

project(qore-xml-module)

set (VERSION_MAJOR 2)
set (VERSION_MINOR 3)
set (VERSION_PATCH 0)

find_package(Qore 3.0 REQUIRED)

if (DEFINED ENV{LIBXML2_LIBRARY})
  set(LIBXML2_LIBRARY $ENV{LIBXML2_LIBRARY})
endif()

include(cmake/QoreXmlLibXml2.cmake)
find_package(OpenSSL REQUIRED)

set(CMAKE_REQUIRED_LIBRARIES ${QORE_XML_CHECK_LIBRARIES})
set(CMAKE_REQUIRED_INCLUDES ${QORE_XML_CHECK_INCLUDES})

include_directories( ${CMAKE_SOURCE_DIR}/src )
include_directories( ${OPENSSL_INCLUDE_DIR} )
include_directories( ${QORE_INCLUDE_DIR} )

# Match Qore's public headers and let CMake select the compiler-specific flag.
set(_xml_cxx_minimum 11)
if (Qore_VERSION VERSION_GREATER_EQUAL "3.0")
    set(_xml_cxx_minimum 17)
endif()
if (NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS _xml_cxx_minimum)
    set(CMAKE_CXX_STANDARD ${_xml_cxx_minimum})
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLIBXML_STATIC=1")
endif()

set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)

#include(CheckSymbolExists)
include(CheckFunctionExists)
include(CheckSymbolExists)

function(qore_check_symbol _symbol _define)
    # Do not reuse results when switching dependency providers in the same build.
    unset(${_define} CACHE)
    if(QORE_XML_BUNDLED_LIBXML2)
        # FetchContent targets have not been built during configure. Check the
        # selected headers without linking; the pinned build supplies these APIs.
        set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
        check_symbol_exists(${_symbol} "libxml/xmlreader.h" ${_define})
    else()
        check_function_exists(${_symbol} ${_define})
    endif()
    if (${_define})
#        message(STATUS "    adding define: -D${_define}")
        add_definitions(-D${_define})
    endif(${_define})
endfunction()

qore_check_symbol(xmlTextReaderSetSchema HAVE_XMLTEXTREADERSETSCHEMA)
qore_check_symbol(xmlTextReaderRelaxNGSetSchema HAVE_XMLTEXTREADERRELAXNGSETSCHEMA)
qore_check_symbol(xmlChildElementCount HAVE_XMLCHILDELEMENTCOUNT)
qore_check_symbol(xmlFirstElementChild HAVE_XMLFIRSTELEMENTCHILD)
qore_check_symbol(xmlGetLastChild HAVE_XMLGETLASTCHILD)
qore_check_symbol(xmlLastElementChild HAVE_XMLLASTELEMENTCHILD)
qore_check_symbol(xmlNextElementSibling HAVE_XMLNEXTLEMENTSIBLING)
qore_check_symbol(xmlPreviousElementSibling HAVE_XMLPREVIOUSELEMENTSIBLING)
qore_check_symbol(xmlTextReaderIsNamespaceDecl HAVE_XMLTEXTREADERISNAMESPACEDECL)
qore_check_symbol(xmlTextReaderByteConsumed HAVE_XMLTEXTREADERBYTECONSUMED)
qore_check_symbol(xmlTextReaderGetParserColumnNumber HAVE_XMLTEXTREADERGETPARSERCOLUMNNUMBER)
qore_check_symbol(xmlTextReaderGetParserLineNumber HAVE_XMLTEXTREADERGETPARSERLINENUMBER)

set(QPP_SRC
    src/QC_SaxIterator.qpp
    src/QC_FileSaxIterator.qpp
    src/QC_InputStreamSaxIterator.qpp
    src/QC_XmlDoc.qpp
    src/QC_XmlNode.qpp
    src/QC_XmlReader.qpp
    src/QC_XmlRpcClient.qpp
    src/ql_xml.qpp
    src/qc_option.qpp
    src/MakeXmlOpts.qpp
    src/QC_AbstractXmlIoInputCallback.qpp
)

set(CPP_SRC
    src/xml-module.cpp
    src/XsdFloat.cpp
    src/QoreXmlRpcReader.cpp
    src/QoreXmlReader.cpp
)

set(QMOD
    qlib/SalesforceSoapClient.qm
    qlib/SoapClient.qm
    qlib/SoapHandler.qm
    qlib/WSDL.qm
    qlib/XmlRpcHandler.qm
    qlib/XmlRpcConnection.qm
    qlib/XmlRpcClientIo
    qlib/SoapClientIo
    qlib/CdaDataProvider
    qlib/SaxDataProvider
    qlib/SoapDataProvider
    qlib/WebDavClient.qm
    qlib/WebDavClientDataProvider
    qlib/WebDavHandler
    qlib/WebContentUtil
)

set(SCRIPTS
    bin/soaputil
    bin/webdav-server
)

qore_wrap_qpp_value(QPP_SOURCES METALIST QPP_META_SOURCES ${QPP_SRC})

# Doxygen consumes generated public declarations, not the C++ implementation
# files that share the build directory with them.
set(QORE_DOX_TMPL_SRC
  docs/mainpage.doxygen.tmpl
  docs/xml-getting-started.doxygen.tmpl
  docs/xml-serialization.doxygen.tmpl
  docs/xml-schema-validation.doxygen.tmpl
  docs/xml-streaming.doxygen.tmpl
  docs/xml-rpc.doxygen.tmpl
  docs/xml-soap-guide.doxygen.tmpl
  docs/xml-webdav-guide.doxygen.tmpl
  docs/xml-user-modules.doxygen.tmpl
  docs/xml-cookbook.doxygen.tmpl
  docs/xml-api-overview.doxygen.tmpl
  docs/release-notes.doxygen.tmpl
)
set(MODULE_DOX_INPUT)
foreach(_xml_dox_tmpl IN LISTS QORE_DOX_TMPL_SRC)
    get_filename_component(_xml_dox_name "${_xml_dox_tmpl}" NAME_WE)
    list(APPEND MODULE_DOX_INPUT "${CMAKE_BINARY_DIR}/${_xml_dox_name}.dox")
endforeach()
foreach(_xml_qpp_source IN LISTS QPP_SRC)
    get_filename_component(_xml_qpp_name "${_xml_qpp_source}" NAME_WE)
    list(APPEND MODULE_DOX_INPUT "${CMAKE_BINARY_DIR}/${_xml_qpp_name}.dox.h")
endforeach()

SET (module_name "xml")

add_library(${module_name} MODULE ${QPP_SOURCES} ${CPP_SRC})

if (DEFINED ENV{DOXYGEN_EXECUTABLE})
    set(DOXYGEN_EXECUTABLE $ENV{DOXYGEN_EXECUTABLE})
endif()

# Preserve caller-supplied core/dependency documentation tags across module macros.
set(_xml_external_doc_tags "${TAGFILES}")

qore_external_binary_module(${module_name} "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}" ${QORE_XML_LIBXML2_TARGET})
add_executable(qore-xml-namespace-probe EXCLUDE_FROM_ALL cmake/libxml2-namespace-probe.c)
target_link_libraries(qore-xml-namespace-probe PRIVATE ${QORE_XML_LIBXML2_TARGET})
add_executable(qore-xml-uri-allocation EXCLUDE_FROM_ALL test/cmake/libxml2_uri_allocation.c)
target_link_libraries(qore-xml-uri-allocation PRIVATE ${QORE_XML_LIBXML2_TARGET})
add_executable(qore-xml-entity-allocation EXCLUDE_FROM_ALL test/cmake/libxml2_entity_allocation.c)
target_link_libraries(qore-xml-entity-allocation PRIVATE ${QORE_XML_LIBXML2_TARGET})
add_executable(qore-xml-float-test EXCLUDE_FROM_ALL test/xsd-float.cpp src/XsdFloat.cpp)
target_link_libraries(qore-xml-float-test PRIVATE ${QORE_LIBRARY} ${QORE_XML_LIBXML2_TARGET})
qore_user_modules("${QMOD}")
install(PROGRAMS ${SCRIPTS} DESTINATION bin)

qore_external_user_module("qlib/WSDL.qm" "")
qore_external_user_module("qlib/SoapClient.qm" "WSDL")
qore_external_user_module("qlib/SoapHandler.qm" "WSDL")
qore_external_user_module("qlib/SalesforceSoapClient.qm" "WSDL;SoapClient")
qore_external_user_module("qlib/SoapClientIo" "WSDL")
qore_external_user_module("qlib/SoapDataProvider" "WSDL;SoapClient")
qore_external_user_module("qlib/CargoXmlDataProvider" "WSDL")
qore_external_user_module("qlib/WebDavClient.qm" "")
qore_external_user_module("qlib/WebDavClientIo" "WebDavClient")
qore_external_user_module("qlib/WebDavClientDataProvider" "WebDavClient;WebDavClientIo")
qore_external_user_module("qlib/WebDavHandler" "")
qore_external_user_module("qlib/XmlRpcConnection.qm" "XmlRpcClientIo")
qore_external_user_module("qlib/XmlRpcHandler.qm" "")
qore_external_user_module("qlib/XmlRpcClientIo" "")
qore_external_user_module("qlib/CdaDataProvider" "")
qore_external_user_module("qlib/SaxDataProvider" "")
qore_external_user_module("qlib/WebContentUtil" "")

# Reconcile the installed i18n catalog root against the fragments these modules
# currently own.  Installing catalogs is otherwise additive: a fragment installed by
# an earlier generation of these sources survives every later install, is still
# discovered, and makes catalog composition fail as soon as the current generation
# describes the same app differently.
#
# This must come after every call above that installs catalogs, both because those
# calls record the fragment set and because install() rules run in declaration order,
# which is what publishes the manifest after the fragments it describes.  Removals are
# scoped to this project, so fragments Qore and other products install into the same
# shared catalog root are untouched.  Guarded because the helper arrived in Qore 2.3.
if (COMMAND qore_finalize_catalog_install)
    qore_finalize_catalog_install()
endif()

# install qpp metadata files
if (COMMAND qore_install_qpp_metadata)
    qore_install_qpp_metadata(${module_name} ${QPP_META_SOURCES})
else()
    install(FILES ${QPP_META_SOURCES}
            DESTINATION ${CMAKE_INSTALL_DATADIR}/qore/metadata)
endif()

qore_dist("${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")

qore_config_info()

if (DOXYGEN_FOUND)
  qore_wrap_dox(QORE_DOX_SRC ${QORE_DOX_TMPL_SRC})
  add_custom_target(QORE_MOD_DOX_FILES DEPENDS ${QORE_DOX_SRC})
  add_dependencies(docs-module QORE_MOD_DOX_FILES)
  # The shared Qore template defaults to a doxygen/ image directory. XML keeps
  # its documentation assets in docs/ instead.
  file(APPEND "${CMAKE_BINARY_DIR}/Doxyfile" "\nIMAGE_PATH = ${CMAKE_SOURCE_DIR}/docs\n")
  string(REPLACE ";" " " _xml_external_doc_tags "${_xml_external_doc_tags}")
  file(GLOB _xml_user_doxyfiles "${CMAKE_BINARY_DIR}/doxygen/Doxyfile.*")
  foreach(_xml_user_doxyfile IN LISTS _xml_user_doxyfiles)
    file(APPEND "${_xml_user_doxyfile}"
      "\nIMAGE_PATH = ${CMAKE_SOURCE_DIR}/docs\nTAGFILES += ${_xml_external_doc_tags}\n")
  endforeach()
  if (COMMAND qore_binary_module_two_phase_docs)
    # every user module, so that the xml documentation can refer to their classes and pages with @ref
    set(_xml_doc_user_modules WSDL SoapClient SoapClientIo SoapHandler SoapDataProvider SalesforceSoapClient
        CargoXmlDataProvider CdaDataProvider SaxDataProvider WebDavHandler WebDavClient WebDavClientIo
        WebDavClientDataProvider XmlRpcClientIo XmlRpcConnection XmlRpcHandler WebContentUtil)
    qore_binary_module_two_phase_docs(xml "${_xml_doc_user_modules}")
    file(APPEND "${CMAKE_BINARY_DIR}/Doxyfile.final" "\nTAGFILES += ${_xml_external_doc_tags}\n")
  endif()
endif()
