cmake_minimum_required(VERSION 3.2...3.12 FATAL_ERROR)

project("message_queue_c" LANGUAGES C CXX)

# Enable automoc and autorcc (needed for Qt)
set(CMAKE_AUTOMOC TRUE)
set(CMAKE_AUTORCC TRUE)

# Append WIN32 to executable setup, otherwise
# Windows will show command prompt when running
# GUI application
if (MSVC)
    set(SAMPLE_TARGET_DEFINITION_ADDENDUM WIN32)
endif ()

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Core Gui REQUIRED
        HINTS
        "${Qt6_DIR}"
        "${Qt5_DIR}")
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Core Gui REQUIRED)

# Create target executable
add_executable(${PROJECT_NAME} ${SAMPLE_TARGET_DEFINITION_ADDENDUM}
    main.cpp
    mainwindow.cpp
    mainwindow.h
    queue_worker.cpp
    queue_worker.h
    backend.c
    backend.h
    queue_model.cpp
    queue_model.h
    camera_dialog.cpp
    camera_dialog.h
    camera_model.cpp
    camera_model.h
    control_widget.cpp
    control_widget.h
    cameralist_updater.cpp
    cameralist_updater.h
)

# Command compiler to use C99 standard (required by ids_peak_comfort_c)
set_target_properties(${PROJECT_NAME}
    PROPERTIES
        C_STANDARD 99
        C_STANDARD_REQUIRED ON
        CXX_STANDARD 14
        CXX_STANDARD_REQUIRED ON
)

# Find std threads package
find_package(Threads REQUIRED)

# Find ids_peak_comfort_c
if (NOT TARGET ids_peak_comfort_c)
    find_package(ids_peak_comfort_c REQUIRED
        HINTS ../../../../../../../lib/
    )
endif ()

# Add postbuild step to copy ids_peak_comfort_c dependencies
ids_peak_comfort_c_deploy(${PROJECT_NAME})

# Setup include directories
target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_SOURCE_DIR}/include
)

# Configure Qt deprecation warnings
target_compile_definitions(${PROJECT_NAME}
    PRIVATE
        # Disable all the deprecated APIs in Qt <= 5.9.9,
        # resulting in errors instead of warnings
        QT_DISABLE_DEPRECATED_BEFORE=0x050909

        # Disable all (remaining) qt deprecation warnings
        QT_NO_DEPRECATED_WARNINGS
        )

# Compiler specifics
if (!MSVC)
    target_link_libraries(${PROJECT_NAME}
            m # Math library
    )
endif ()

if (UNIX)
    # Link against atomic library
    target_link_libraries(${PROJECT_NAME}
            atomic
    )
endif ()

# Link libraries
target_link_libraries(${PROJECT_NAME}
    ids_peak_comfort_c::ids_peak_comfort_c
    ${CMAKE_THREAD_LIBS_INIT}
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Gui
)

# Post build steps
ids_peak_comfort_c_deploy(${PROJECT_NAME})

if (WIN32)
    if(NOT WINDEPLOYQT_EXECUTABLE)
        get_target_property(_qmake_executable Qt${QT_VERSION_MAJOR}::qmake
                IMPORTED_LOCATION)
        get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
        find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}" REQUIRED)
    endif()

    set(winqt_deploy_arguments
            --no-translations
            --no-system-d3d-compiler
            --no-opengl-sw
            --no-svg
            --no-quick-import)

    if (QT_VERSION_MAJOR EQUAL 6)
        list(APPEND winqt_deploy_arguments
                --no-system-dxc-compiler
        )
    else()
        list(APPEND winqt_deploy_arguments
                --no-webkit2
                --no-angle
        )
    endif()

    # Copy Qt dll's to output folder
    message(STATUS "[${PROJECT_NAME}] Add PostBuildStep for copy of the Qt libs. ${WINDEPLOYQT_EXECUTABLE}")
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND "${WINDEPLOYQT_EXECUTABLE}"
            ${winqt_deploy_arguments}
            --dir $<TARGET_FILE_DIR:${PROJECT_NAME}>
            $<TARGET_FILE:${PROJECT_NAME}>
    )
endif ()

# For unix Build we need the environment variable GENICAM_GENTL32_PATH respectively GENICAM_GENTL64_PATH to find the GenTL producer libraries.
# To set these environment variables a shell script is used. This script can be automatically generated via ids_peak_comfort_c_generate_starter_script.
# The shell script will be saved at ${CMAKE_CURRENT_BINARY_DIR}/${targetName}.sh and automatically copied to the output directory during post-build.

# To run the samples run this script, not the binary.
if (UNIX)
    ids_peak_comfort_c_generate_starter_script(${PROJECT_NAME})
endif ()
