project(LomiriOnlineAccountsQML LANGUAGES CXX)

pkg_check_modules(ACCOUNTSQT accounts-qt${QT_VERSION} REQUIRED)

set(LOA_QML_MODULE LomiriOnlineAccountsQML)
set(API_URI Lomiri.OnlineAccounts)
set(API_VERSION 2.0)

set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--no-undefined")

if(ENABLE_QT6)
    qt_add_qml_module(${LOA_QML_MODULE}
        URI ${API_URI}
        VERSION ${API_VERSION}
        SOURCES plugin.cpp plugin.h
            account.cpp account.h
            account_model.cpp account_model.h
            authentication_data.cpp authentication_data.h
            plugin.cpp plugin.h
        PLUGIN_TARGET ${LOA_QML_MODULE}
        CLASS_NAME Plugin
        NO_GENERATE_PLUGIN_SOURCE
        NO_PLUGIN_OPTIONAL)
    qt_query_qml_module(${LOA_QML_MODULE}
        PLUGIN_TARGET module_plugin_target
        TARGET_PATH module_target_path
        QMLDIR module_qmldir
        TYPEINFO module_typeinfo
    )
    target_compile_options(${LOA_QML_MODULE} PRIVATE
        ${ACCOUNTSQT_CFLAGS}
    )
    target_include_directories(${LOA_QML_MODULE} PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_SOURCE_DIR}/online-accounts/lib/qt
    )
    target_link_libraries(${LOA_QML_MODULE} PRIVATE
        Qt${QT_VERSION}::Core
        Qt${QT_VERSION}::Qml
        lomiri-online-accounts-qt${QT_VERSION}
        ${ACCOUNTSQT_LDFLAGS}
    )

    set(PLUGIN_IMPORTS_DIR "${QT_INSTALL_QML}/${module_target_path}")
    install(TARGETS ${module_plugin_target} DESTINATION "${PLUGIN_IMPORTS_DIR}")
    install(FILES ${module_qmldir} DESTINATION "${PLUGIN_IMPORTS_DIR}")
    # install(FILES ${module_typeinfo} DESTINATION "${PLUGIN_IMPORTS_DIR}")
else()
    include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_SOURCE_DIR}/online-accounts/lib/qt
    )

    if(ENABLE_UBUNTU_COMPAT AND NOT ENABLE_QT6)
        add_definitions(-DLOMIRI_ONLINEACCOUNTS_UBUNTU_COMPAT)
    endif()

    add_definitions(
        ${ACCOUNTSQT_CFLAGS}
    )

    add_library(${LOA_QML_MODULE} MODULE
        account.cpp
        account_model.cpp
        authentication_data.cpp
        plugin.cpp
    )

    target_link_libraries(${LOA_QML_MODULE}
        Qt${QT_VERSION}::Core
        Qt${QT_VERSION}::Qml
        lomiri-online-accounts-qt${QT_VERSION}
        ${ACCOUNTSQT_LDFLAGS}
    )


    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/qmldir.in ${CMAKE_CURRENT_BINARY_DIR}/qmldir @ONLY)

    # Module install

    # FIXME: do we really need this, it causes build failures from time to time (maybe related to
    # a missing dependency declaration?
    #add_custom_command(TARGET ${LOA_QML_MODULE} POST_BUILD
    #                   COMMAND qmlplugindump -noinstantiate ${API_URI} ${API_VERSION} > ${CMAKE_CURRENT_BINARY_DIR}/plugin.qmltypes)

    set(PLUGIN_IMPORTS_DIR "${QT_INSTALL_QML}/Lomiri/OnlineAccounts")
    install(TARGETS ${LOA_QML_MODULE} DESTINATION ${PLUGIN_IMPORTS_DIR})
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/qmldir DESTINATION ${PLUGIN_IMPORTS_DIR})
    # FIXME: see above (qmlplugindump command)
    #install(FILES ${CMAKE_CURRENT_BINARY_DIR}/plugin.qmltypes DESTINATION ${PLUGIN_IMPORTS_DIR})
endif()
