SET(SDK_MINIMUM_REQUIRED_VERSION "2.10.1")

if (NOT DEFINED FREERTOS_DIR)
    message(FATAL_ERROR "Please set FREERTOS_DIR cache entry")
endif()

if(EXISTS ${QUL_BOARD_SDK_DIR}/SW-Content-Register.txt)
    FILE(STRINGS ${QUL_BOARD_SDK_DIR}/SW-Content-Register.txt SDK_VERSION REGEX "Release Version: .*")
    STRING(REGEX MATCH "[0-9.]+" SDK_VERSION ${SDK_VERSION})
    if(SDK_VERSION)
        message(STATUS "SDK VERSION: " ${SDK_VERSION})
        if(SDK_VERSION VERSION_LESS ${SDK_MINIMUM_REQUIRED_VERSION})
            message(FATAL_ERROR "Qt for MCUs is supported on SDK version " ${SDK_MINIMUM_REQUIRED_VERSION} " or later.")
        endif()
    else()
        message(WARNING "Could not check SDK version. Qt for MCUs is supported on version " ${SDK_MINIMUM_REQUIRED_VERSION} " or later.")
    endif()
else()
    message(WARNING "Could not find SDK version information. Qt for MCUs is supported on SDK version " ${SDK_MINIMUM_REQUIRED_VERSION} " or later.")
endif()

set(MIDDLEWARE_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/middleware)

if(ARMGCC)
    target_sources(Platform
        PRIVATE
        ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/gcc/startup_MIMXRT1176_cm7.S
    )
elseif(IAR)
    target_sources(Platform
        PRIVATE
        ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/iar/startup_MIMXRT1176_cm7.s
        ${FREERTOS_DIR}/portable/${FREERTOS_PORT_DIR}/ARM_CM4F/portasm.s
    )
endif()

target_sources(Platform PRIVATE
    ${EA_COMMON_DIR}/freertos/nxp_os.c
    ${PLATFORM_COMMON_SRC_DIR}/platform.cpp
    ${PLATFORM_COMMON_SRC_DIR}/freertos/mem-freertos-default.cpp
    ${PLATFORM_COMMON_SRC_DIR}/freertos/freertosqueue.cpp
    ${PLATFORM_COMMON_SRC_DIR}/singlepointtoucheventdispatcher.cpp
    ${PLATFORM_COMMON_SRC_DIR}/freertos/mem_info.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/noncacheableallocator.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/lcdifv2layerengine.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/lcdifv2layer.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/platform_eaimxrt1176.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../common/vglitesupport.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../common/vglitedrawingengine.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../common/vgliterotationcache.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/embeddedartists.c
    ${CMAKE_CURRENT_SOURCE_DIR}/embeddedartists_qul.c
    ${CMAKE_CURRENT_SOURCE_DIR}/power.c
    ${MIDDLEWARE_SRC_DIR}/display_support.c
    ${MIDDLEWARE_SRC_DIR}/vglite_support.c
    #${MIDDLEWARE_SRC_DIR}/lcdifv2_support.c
    ${MIDDLEWARE_SRC_DIR}/pin_mux.c
    ${MIDDLEWARE_SRC_DIR}/board.c
    ${MIDDLEWARE_SRC_DIR}/clock_config.c
    ${MIDDLEWARE_SRC_DIR}/dcd.c
    ${MIDDLEWARE_SRC_DIR}/eaimxrt1176.c
    ${MIDDLEWARE_SRC_DIR}/pca6416.c
    ${MIDDLEWARE_SRC_DIR}/pca9530.c
    ${MIDDLEWARE_SRC_DIR}/app.c
    ${MIDDLEWARE_SRC_DIR}/host_hid_generic.c
    ${MIDDLEWARE_SRC_DIR}/fsl_os_abstraction_free_rtos.c
)

target_sources(PlatformBSP PRIVATE
    # General SDK
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/system_MIMXRT1176_cm7.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/xip/fsl_flexspi_nor_boot.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_pmu.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_dcdc.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_anatop_ai.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_gpio.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_clock.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_common.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_common_arm.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_lpuart.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_elcdif.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_soc_src.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_lcdifv2.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_mipi_dsi.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/drivers/fsl_lpi2c.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/utilities/fsl_sbrk.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/utilities/fsl_assert.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/utilities/str/fsl_str.c
    ${QUL_BOARD_SDK_DIR}/devices/MIMXRT1176/utilities/debug_console/fsl_debug_console.c
    ${QUL_BOARD_SDK_DIR}/components/gt911/fsl_gt911.c
    ${QUL_BOARD_SDK_DIR}/components/uart/fsl_adapter_lpuart.c
    ${QUL_BOARD_SDK_DIR}/components/lists/fsl_component_generic_list.c
    ${QUL_BOARD_SDK_DIR}/components/serial_manager/fsl_component_serial_manager.c
    ${QUL_BOARD_SDK_DIR}/components/serial_manager/fsl_component_serial_port_uart.c
    ${QUL_BOARD_SDK_DIR}/components/video/fsl_video_common.c
    ${QUL_BOARD_SDK_DIR}/components/video/display/rm68191/fsl_rm68191.c
    ${QUL_BOARD_SDK_DIR}/components/video/display/mipi_dsi_cmd/fsl_mipi_dsi_cmd.c
    ${QUL_BOARD_SDK_DIR}/components/video/display/rm68200/fsl_rm68200.c
    ${QUL_BOARD_SDK_DIR}/components/video/display/dc/lcdifv2/fsl_dc_fb_lcdifv2.c
    ${QUL_BOARD_SDK_DIR}/components/video/display/dc/elcdif/fsl_dc_fb_elcdif.c
    ${QUL_BOARD_SDK_DIR}/components/video/display/fbdev/fsl_fbdev.c
    ${QUL_BOARD_SDK_DIR}/components/video/display/adv7535/fsl_adv7535.c
    ${QUL_BOARD_SDK_DIR}/boards/evkmimxrt1170/xip/evkmimxrt1170_flexspi_nor_config.c

    # FreeRTOS
    ${FREERTOS_DIR}/event_groups.c
    ${FREERTOS_DIR}/croutine.c
    ${FREERTOS_DIR}/list.c
    ${FREERTOS_DIR}/portable/${FREERTOS_PORT_DIR}/ARM_CM4F/port.c
    ${FREERTOS_DIR}/queue.c
    ${FREERTOS_DIR}/stream_buffer.c
    ${FREERTOS_DIR}/tasks.c
    ${FREERTOS_DIR}/timers.c
    ${FREERTOS_DIR}/queue.c

    # VGLite
    ${QUL_BOARD_SDK_DIR}/middleware/vglite/VGLite/vg_lite.c
    ${QUL_BOARD_SDK_DIR}/middleware/vglite/VGLite/rtos/vg_lite_os.c
    ${QUL_BOARD_SDK_DIR}/middleware/vglite/VGLite/vg_lite_image.c
    ${QUL_BOARD_SDK_DIR}/middleware/vglite/VGLite/vg_lite_matrix.c
    ${QUL_BOARD_SDK_DIR}/middleware/vglite/VGLite/vg_lite_path.c
    ${QUL_BOARD_SDK_DIR}/middleware/vglite/VGLiteKernel/vg_lite_kernel.c
    ${QUL_BOARD_SDK_DIR}/middleware/vglite/VGLiteKernel/rtos/vg_lite_hal.c

    # USB Host (for touch events)
    ${QUL_BOARD_SDK_DIR}/middleware/usb/host/usb_host_devices.c
    ${QUL_BOARD_SDK_DIR}/middleware/usb/host/usb_host_ehci.c
    ${QUL_BOARD_SDK_DIR}/middleware/usb/host/usb_host_framework.c
    ${QUL_BOARD_SDK_DIR}/middleware/usb/host/usb_host_hci.c
    ${QUL_BOARD_SDK_DIR}/middleware/usb/host/class/usb_host_hid.c
    ${QUL_BOARD_SDK_DIR}/middleware/usb/host/class/usb_host_hub.c
    ${QUL_BOARD_SDK_DIR}/middleware/usb/host/class/usb_host_hub_app.c
    ${QUL_BOARD_SDK_DIR}/middleware/usb/phy/usb_phy.c
    #${QUL_BOARD_SDK_DIR}/components/osa/fsl_os_abstraction_free_rtos.c
    ${QUL_BOARD_SDK_DIR}/components/lists/fsl_component_generic_list.c
)

include(${PLATFORM_COMMON_SRC_DIR}/freertos/freertos.cmake)

target_include_directories(Platform PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/../common
    ${MIDDLEWARE_SRC_DIR}
)

target_include_directories(PlatformBSP PRIVATE
    ${MIDDLEWARE_SRC_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
)

if(NOT IAR)
    target_compile_options(
        # The DelayLoop() function in NXP SDK code (fsl_common_arm.c) uses a non-local label in
        # inline assembly which is incompatible with GCC LTO when that function gets inlined
        # Thus, we need to disable function inlining. See UL-4412.
        PlatformBSP
        PRIVATE
        -fno-inline-functions
    )

    target_compile_options(
        Platform
        PRIVATE
        -Wno-unused-parameter
        -Wno-sign-compare
        -Wno-missing-field-initializers
        -Wno-implicit-fallthrough
    )
endif()

if (QUL_PREPROCESS_CACHE_SIZE)
    if (IAR)
        target_link_options(QuickUltralitePlatform PUBLIC --config_def __qul_preprocess_cache_size__=${QUL_PREPROCESS_CACHE_SIZE})
    else()
        target_link_options(QuickUltralitePlatform PUBLIC -Xlinker --defsym=__qul_preprocess_cache_size__=${QUL_PREPROCESS_CACHE_SIZE})
    endif()
endif()

install_board_platform_packages()
