include(${CMAKE_SOURCE_DIR}/dep/build_tools/cmake/cpp_standart_setup.cmake)

if(MSVC)
    add_compile_options(/FS)
    add_compile_options($<$<CONFIG:Debug>:/Z7> $<$<CONFIG:RelWithDebInfo>:/Z7>)
endif()

if(WIN32)
    add_compile_definitions(UNICODE _UNICODE)
endif()

if(USE_DIE)
    add_definitions(-DUSE_DIE)
endif()
add_definitions(-DUSE_XSIMD)

option(WITH_YARA "Use Yara" ON)
if(WITH_YARA)
    add_definitions(-DUSE_YARA)
endif()

# Adds the xjs* sources to XPDF and defines USE_PDFJSEMUL (see xpdf.cmake)
option(WITH_PDFJSEMUL "Use PDF JavaScript emulator" ON)

# The widget sources live in diegui.cmake so ../../tests can build the very same
# list; only main() and the resources are added on top of it here.
include(${CMAKE_CURRENT_LIST_DIR}/diegui.cmake)

if(UNIX AND NOT APPLE)
    include(GNUInstallDirs)
endif()

set(DIE_GUI_TARGET DetectItEasy)
set(DIE_GUI_SOURCES
    ${DIEGUI_SOURCES}
    main_gui.cpp
    rsrc.qrc
    res.qrc
)

if(WIN32 AND EXISTS "${X_WINDOWS_RESOURCE_ICON_RC_IN}")
    # X_ORIGINAL_FILENAME ("die") and X_WINDOWS_ICON (icons/main.ico) already carry the GUI's
    # values from src/CMakeLists.txt, so unlike diec/diel nothing has to be overridden here.
    # The manifest is copied beside the generated .rc because RT_MANIFEST resolves relative to
    # the .rc file's own directory.
    configure_file("${X_WINDOWS_RESOURCE_ICON_RC_IN}"
                   "${CMAKE_CURRENT_BINARY_DIR}/resource_die.rc" @ONLY)
    configure_file("${X_RESOURCE_DIR}/windows/windows.manifest.xml"
                   "${CMAKE_CURRENT_BINARY_DIR}/windows.manifest.xml" COPYONLY)
    list(APPEND DIE_GUI_SOURCES "${CMAKE_CURRENT_BINARY_DIR}/resource_die.rc")
endif()

if(APPLE AND EXISTS "${X_MACOS_ICON}")
    set_source_files_properties("${X_MACOS_ICON}" PROPERTIES
        MACOSX_PACKAGE_LOCATION "Resources"
    )
    list(APPEND DIE_GUI_SOURCES "${X_MACOS_ICON}")
endif()

add_executable(${DIE_GUI_TARGET} WIN32
    ${DIE_GUI_SOURCES}
)

set_target_properties(${DIE_GUI_TARGET} PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE    TRUE
    MACOSX_BUNDLE_GUI_IDENTIFIER "${MACOSX_BUNDLE_GUI_IDENTIFIER}"
    MACOSX_BUNDLE_BUNDLE_NAME "Detect It Easy"
    MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION}"
    MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
    MACOSX_BUNDLE_ICON_FILE "main.icns"
    OUTPUT_NAME "die"
)

if(MSVC)
    # The .rc already supplies RT_MANIFEST; keep the linker from embedding a competing one.
    target_link_options(${DIE_GUI_TARGET} PRIVATE /MANIFEST:NO)
endif()

target_include_directories(${DIE_GUI_TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(${DIE_GUI_TARGET} PRIVATE
    die_updater_qt
    capstone
    cppfilt
    xsimd
    Qt${DIE_QT_MAJOR}::Widgets
    Qt${DIE_QT_MAJOR}::Concurrent
    Qt${DIE_QT_MAJOR}::Network
    Qt${DIE_QT_MAJOR}::PrintSupport
    Qt${DIE_QT_MAJOR}::OpenGL
    Qt${DIE_QT_MAJOR}::Svg
    Qt${DIE_QT_MAJOR}::Sql
)

if(TARGET die_update)
    add_dependencies(${DIE_GUI_TARGET} die_update)
    if(APPLE)
        add_custom_command(TARGET ${DIE_GUI_TARGET} POST_BUILD
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                "$<TARGET_FILE:die_update>"
                "$<TARGET_BUNDLE_CONTENT_DIR:${DIE_GUI_TARGET}>/MacOS/$<TARGET_FILE_NAME:die_update>"
            VERBATIM)
    else()
        add_custom_command(TARGET ${DIE_GUI_TARGET} POST_BUILD
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                "$<TARGET_FILE:die_update>"
                "$<TARGET_FILE_DIR:${DIE_GUI_TARGET}>/$<TARGET_FILE_NAME:die_update>"
            VERBATIM)
    endif()
endif()

if(DIE_QT_MAJOR EQUAL 5)
    target_link_libraries(${DIE_GUI_TARGET} PRIVATE
        Qt5::Script
        Qt5::ScriptTools
    )
elseif(DIE_QT_MAJOR GREATER_EQUAL 6)
    target_link_libraries(${DIE_GUI_TARGET} PRIVATE
        Qt${DIE_QT_MAJOR}::Qml
    )
endif()

if(WITH_YARA)
    target_link_libraries(${DIE_GUI_TARGET} PRIVATE yara)
endif()

if(WIN32)
    target_link_libraries(${DIE_GUI_TARGET} PRIVATE Wintrust Crypt32)
endif()

if(TARGET die_translations)
    add_dependencies(${DIE_GUI_TARGET} die_translations)
endif()

if(UNIX AND NOT APPLE)
    set(DIE_RUNTIME_DESTINATION "${CMAKE_INSTALL_BINDIR}")
    set(DIE_DOC_DESTINATION "${CMAKE_INSTALL_DOCDIR}/detect-it-easy")
    set(DIE_DESKTOP_TEMPLATE "${CMAKE_CURRENT_SOURCE_DIR}/../../res/linux/die.desktop.in")
    set(DIE_DESKTOP_FILE "${CMAKE_CURRENT_BINARY_DIR}/die.desktop")
    if(EXISTS "${DIE_DESKTOP_TEMPLATE}")
        configure_file("${DIE_DESKTOP_TEMPLATE}" "${DIE_DESKTOP_FILE}" @ONLY)
    endif()
else()
    set(DIE_RUNTIME_DESTINATION ".")
    set(DIE_DOC_DESTINATION ".")
endif()

install(TARGETS ${DIE_GUI_TARGET}
    RUNTIME DESTINATION ${DIE_RUNTIME_DESTINATION}
    BUNDLE  DESTINATION .
)

if(UNIX AND NOT APPLE)
    install(FILES
        "${CMAKE_CURRENT_SOURCE_DIR}/../../LICENSE"
        "${CMAKE_CURRENT_SOURCE_DIR}/../../README.md"
        DESTINATION ${DIE_DOC_DESTINATION}
        OPTIONAL
    )
    if(EXISTS "${DIE_DESKTOP_FILE}")
        install(FILES "${DIE_DESKTOP_FILE}"
            DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications")
    endif()
    install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../LINUX/hicolor/"
        DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor"
        OPTIONAL
    )
    install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../../LINUX/io.github.horsicq.detect-it-easy.metainfo.xml"
        DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/metainfo"
        OPTIONAL
    )
    # X_RESOURCES, never CMAKE_INSTALL_LIBDIR. With -DCMAKE_INSTALL_PREFIX=/usr on
    # Debian/Ubuntu the latter is the multiarch lib/x86_64-linux-gnu, and
    # XOptions::convertPathName("$data/...") searches only /usr/lib/die,
    # /usr/local/lib/die and /app/lib/die - so the database, styles, info and yara
    # rules shipped in a .deb were invisible to the application that packaged them.
    #
    # Four of these five are also installed to ${X_RESOURCES} by the _mylibs
    # fragments (XStyles/xstyles.cmake, XInfoDB/xinfodb.cmake,
    # Detect-It-Easy/detectiteasy.cmake, XYara/xyara.cmake), each guarded by
    # `if (DEFINED X_RESOURCES)` - which holds here, since cpp_standart_setup.cmake
    # is included at the top of this file. They are kept rather than deleted so the
    # install does not depend on which fragments a given option set pulls in; the
    # duplicate lands on the same path. Only "images" has no fragment.
    install(DIRECTORY "${CMAKE_SOURCE_DIR}/dep/XStyles/qss"
        DESTINATION "${X_RESOURCES}" OPTIONAL)
    install(DIRECTORY "${CMAKE_SOURCE_DIR}/dep/XInfoDB/info"
        DESTINATION "${X_RESOURCES}" OPTIONAL)
    install(DIRECTORY "${CMAKE_SOURCE_DIR}/dep/Detect-It-Easy/db"
        DESTINATION "${X_RESOURCES}" OPTIONAL)
    if(WITH_PEID)
        install(DIRECTORY "${CMAKE_SOURCE_DIR}/dep/XYara/yara_rules"
            DESTINATION "${X_RESOURCES}" OPTIONAL)
    else()
        install(DIRECTORY "${CMAKE_SOURCE_DIR}/dep/XYara/yara_rules"
            DESTINATION "${X_RESOURCES}" OPTIONAL
            PATTERN "peid.yar" EXCLUDE)
    endif()
    install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../images"
        DESTINATION "${X_RESOURCES}" OPTIONAL)
else()
    install(FILES
        "${CMAKE_CURRENT_SOURCE_DIR}/../../LICENSE"
        DESTINATION .
        RENAME LICENSE.txt
        OPTIONAL
    )
    install(FILES
        "${CMAKE_CURRENT_SOURCE_DIR}/../../README.md"
        "${X_WINDOWS_ICON}"
        DESTINATION .
        OPTIONAL
    )
    install(CODE [[file(WRITE "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/portable" "")]])
endif()

if(APPLE)
    include(${CMAKE_SOURCE_DIR}/dep/build_tools/cmake/macdeployqt.cmake)
endif()

if(WIN32)
    include(${CMAKE_SOURCE_DIR}/dep/build_tools/cmake/deploy_qt_windows.cmake)
    include(${CMAKE_SOURCE_DIR}/dep/build_tools/cmake/deploy_msvc.cmake)
endif()
