# Enable policy to run automoc on generated files.
if(POLICY CMP0071)
  cmake_policy(SET CMP0071 NEW)
endif()

# Get all relevant Qt include dirs, to pass them on to shiboken.
get_property(QT_CORE_INCLUDE_DIRS TARGET Qt6::Core PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_GUI_INCLUDE_DIRS TARGET Qt6::Gui PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_SQL_INCLUDE_DIRS TARGET Qt6::Sql PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_WIDGETS_INCLUDE_DIRS TARGET Qt6::Widgets PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_NETWORK_INCLUDE_DIRS TARGET Qt6::Network PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_WEBENGINECORE_INCLUDE_DIRS TARGET Qt6::WebEngineCore PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_WEBENGINEWIDGETS_INCLUDE_DIRS TARGET Qt6::WebEngineWidgets PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
set(QT_INCLUDE_DIRS
    ${QT_CORE_INCLUDE_DIRS}
    ${QT_GUI_INCLUDE_DIRS}
    ${QT_SQL_INCLUDE_DIRS}
    ${QT_WIDGETS_INCLUDE_DIRS}
    ${QT_NETWORK_INCLUDE_DIRS}
    ${QT_WEBENGINECORE_INCLUDE_DIRS}
    ${QT_WEBENGINEWIDGETS_INCLUDE_DIRS}
)
get_property(QT_PRINTSUPPORT_INCLUDE_DIRS TARGET Qt6::PrintSupport PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
list(APPEND QT_INCLUDE_DIRS ${QT_PRINTSUPPORT_INCLUDE_DIRS})
set(INCLUDES "")
foreach(INCLUDE_DIR ${QT_INCLUDE_DIRS})
    list(APPEND INCLUDES "-I${INCLUDE_DIR}")
endforeach()
get_property(FalkonPrivate_INCLUDE_DIRS TARGET FalkonPrivate PROPERTY INCLUDE_DIRECTORIES)
foreach(INCLUDE_DIR ${FalkonPrivate_INCLUDE_DIRS})
    list(APPEND INCLUDES "-I${INCLUDE_DIR}")
endforeach()

# Set up the options to pass to shiboken.
set(GLOBAL_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/pyfalkon_global.h)
set(TYPESYSTEM_FILE ${CMAKE_CURRENT_SOURCE_DIR}/typesystem_pyfalkon.xml)

set(API_VERSION "${Qt6_VERSION_MAJOR}.${Qt6_VERSION_MINOR}")

set(SHIBOKEN_OPTIONS --generator-set=shiboken --enable-parent-ctor-heuristic
    --enable-pyside-extensions --enable-return-value-heuristic --use-isnull-as-nb_nonzero
    --avoid-protected-hack
    ${INCLUDES}
    -T${PYSIDE_TYPESYSTEMS}
    --output-directory=${CMAKE_CURRENT_BINARY_DIR}
    --api-version="${API_VERSION}"
)

# Specify which sources will be generated by shiboken, and their dependencies.
set(GENERATED_SOURCES
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/pyfalkon_module_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/webview_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabbedwebview_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/webpage_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/webhittestresult_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/desktopfile_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/plugininterface_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/loadrequest_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/qz_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/desktopnotificationsfactory_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/externaljsobject_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/pluginproxy_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/plugins_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/plugins_plugin_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/pluginspec_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/qtsingleapplication_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/squeezelabelv1_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/squeezelabelv2_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/lineedit_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/sidewidget_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/webtab_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/mainapplication_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/datapaths_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/settings_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/autosaver_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/browserwindow_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/pageformdata_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/passwordentry_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/passwordbackend_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/autofill_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/passwordmanager_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/bookmarkitem_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/bookmarkstools_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/bookmarks_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/bookmarksmodel_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/bookmarksfoldersmenu_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/bookmarksfoldersbutton_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/cookiemanager_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/cookiejar_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/downloaditem_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/downloadmanager_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/downloadmanager_downloadinfo_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/history_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/history_historyentry_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/historyitem_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/historymodel_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/locationbar_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/locationbar_loadaction_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/navigationbar_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/networkmanager_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/searchenginesdialog_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/searchenginesmanager_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/searchenginesmanager_engine_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/sidebarmanager_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/sidebarinterface_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/webinspector_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/combotabbar_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabbar_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabcontextmenu_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabicon_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabicon_data_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabmodel_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabmrumodel_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabstackedwidget_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabtreemodel_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/tabwidget_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/searchtoolbar_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/checkboxdialog_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/qzsettings_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/statusbar_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/abstractbuttoninterface_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/abstractbuttoninterface_clickcontroller_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/clickablelabel_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/delayedfilewatcher_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/iconprovider_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/qztools_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/sqlqueryjob_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/sqldatabase_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/toolbutton_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/wheelhelper_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/menu_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/action_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/urlinterceptor_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/sitesettingsmanager_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/sitesettingsmanager_sitesettings_wrapper.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/extensionschemehandler_wrapper.cpp
)
set(GENERATED_SOURCES_DEPENDENCIES
    ${GLOBAL_HEADER}
    ${TYPESYSTEM_FILE}
)

set(PYSIDE_VERSION "6")

# Add custom target to run shiboken.
add_custom_command(OUTPUT ${GENERATED_SOURCES}
                    COMMAND Shiboken${PYSIDE_VERSION}::shiboken${PYSIDE_VERSION}
                    ${SHIBOKEN_OPTIONS} ${GLOBAL_HEADER} ${TYPESYSTEM_FILE}
                    DEPENDS ${GENERATED_SOURCES_DEPENDENCIES}
                    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                    COMMENT "Running generator for ${TYPESYSTEM_FILE}.")

# We need to include the headers for the module bindings that we use.
set(PYSIDE_ADDITIONAL_INCLUDES "")
get_target_property(PYSIDE_INCLUDE_DIRS PySide${PYSIDE_VERSION}::pyside${PYSIDE_VERSION} INTERFACE_INCLUDE_DIRECTORIES)
foreach(INCLUDE_DIR ${PYSIDE_INCLUDE_DIRS})
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtCore")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtGui")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtNetwork")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtPrintSupport")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtWidgets")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtWebChannel")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtWebEngineCore")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtWebEngineWidgets")
    list(APPEND PYSIDE_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtSql")
endforeach()

set(PyFalkon_SRCS
    pythonplugin.cpp
    ${GENERATED_SOURCES}
)

add_library(PyFalkon MODULE ${PyFalkon_SRCS})
install(TARGETS PyFalkon DESTINATION ${FALKON_INSTALL_PLUGINDIR})

target_include_directories(PyFalkon
    PRIVATE
    ${PYSIDE_ADDITIONAL_INCLUDES}
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon
)

target_link_libraries(PyFalkon
    PRIVATE
    FalkonPrivate
    Shiboken${PYSIDE_VERSION}::libshiboken
    PySide${PYSIDE_VERSION}::pyside${PYSIDE_VERSION}
    Python3::Python
)

# Same as CONFIG += no_keywords to avoid syntax errors in object.h due to the usage of the word Slot
target_compile_definitions(PyFalkon PRIVATE QT_NO_KEYWORDS)

if(BUILD_TESTING)
# Wrong, outdated implementation of PyFalkon API
#    add_subdirectory(autotests)
endif()
