cmake_minimum_required(VERSION 3.10)

project(fire_rt1052_whd)

enable_language(CXX)
enable_language(ASM)

# Different linker scripts
set(TARGET_LDSCRIPT_FLASH "${CMAKE_SOURCE_DIR}/app_flexspi.ld")
set(TARGET_LDSCRIPT_RAM   "${CMAKE_SOURCE_DIR}/app_ram.ld")

set(TARGET_SOURCES
    "SDK/components/lists/fsl_component_generic_list.c"
    "SDK/components/osa/fsl_os_abstraction_free_rtos.c"
    "SDK/components/serial_manager/fsl_component_serial_manager.c"
    "SDK/components/serial_manager/fsl_component_serial_port_uart.c"
    "SDK/components/uart/fsl_adapter_lpuart.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_adc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_adc_etc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_aipstz.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_aoi.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_bee.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_cache.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_clock.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_cmp.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_common.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_common_arm.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_csi.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_dcdc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_dcp.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_dmamux.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_elcdif.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_enc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_enet.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_ewm.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexcan.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_camera.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_camera_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_i2c_master.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_i2s.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_i2s_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_mculcd.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_mculcd_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_spi.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_spi_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_uart.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexio_uart_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexram.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexram_allocate.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexspi.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_flexspi_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_gpc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_gpio.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_gpt.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_kpp.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_lpi2c.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_lpi2c_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_lpspi.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_lpspi_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_lpuart.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_lpuart_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_ocotp.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_pit.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_pmu.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_pwm.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_pxp.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_qtmr.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_romapi.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_rtwdog.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_sai.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_sai_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_semc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_snvs_hp.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_snvs_lp.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_spdif.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_spdif_edma.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_src.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_tempmon.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_trng.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_tsc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_usdhc.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_wdog.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_xbara.c"
    "SDK/devices/MIMXRT1052/drivers/fsl_xbarb.c"
    "SDK/devices/MIMXRT1052/gcc/startup_MIMXRT1052.S"
    "SDK/devices/MIMXRT1052/system_MIMXRT1052.c"
    "SDK/devices/MIMXRT1052/utilities/debug_console/fsl_debug_console.c"
    "SDK/devices/MIMXRT1052/utilities/fsl_assert.c"
    "SDK/devices/MIMXRT1052/utilities/fsl_notifier.c"
    "SDK/devices/MIMXRT1052/utilities/fsl_sbrk.c"
    "SDK/devices/MIMXRT1052/utilities/str/fsl_str.c"
    "SDK/devices/MIMXRT1052/xip/fsl_flexspi_nor_boot.c"
    "SDK/middleware/sdmmc/common/fsl_sdmmc_common.c"
    "SDK/middleware/sdmmc/osa/fsl_sdmmc_osa.c"
    "SDK/middleware/sdmmc/sdio/fsl_sdio.c"
    "SDK/middleware/sdmmc/host/usdhc/non_blocking/fsl_sdmmc_host.c"
    "board/board.c"
    "board/clock_config.c"
    "board/dcd.c"
    "board/peripherals.c"
    "board/pin_mux.c"
    "src/freertos_support.c"
    "src/lwip_port/sys_arch.c"
    "src/main.c"
    "src/task_hello.c"
    "src/whd_port/cy_network_buffer.c"
    "src/whd_port/hal/cyhal_sdio.c"
    "src/whd_port/resources/whd_resources.c"
    "src/whd_port/resources/firmware/COMPONENT_43362/43362A2_bin.c"
    "src/whd_port/resources/nvram/cyw43362A2_nvram_image.c"
    "src/whd_port/rtos/cyabs_rtos.c"
    "xip/fire_rt1052_pro_flexspi_nor_config.c"
)

set(TARGET_C_DEFINES
    "CPU_MIMXRT1052DVL6B"
    "MCUXPRESSO_SDK"
    "PRINTF_ADVANCED_ENABLE=1"
    "PRINTF_FLOAT_ENABLE=1"
    "SDK_OS_FREE_RTOS"
    "SERIAL_PORT_TYPE_UART"
    "USE_RTOS=1"
    "__STARTUP_CLEAR_BSS"
    "__STARTUP_INITIALIZE_NONCACHEDATA"
)

set(TARGET_C_DEFINES_XIP
    "SKIP_SYSCLK_INIT"
    "XIP_BOOT_HEADER_ENABLE=1"
    "XIP_BOOT_HEADER_DCD_ENABLE=1"
    "XIP_EXTERNAL_FLASH=1"
)

set(TARGET_C_INCLUDES
    "SDK/CMSIS/Core/Include"
    "SDK/components/lists"
    "SDK/components/osa"
    "SDK/components/serial_manager"
    "SDK/components/uart"
    "SDK/devices/MIMXRT1052"
    "SDK/devices/MIMXRT1052/drivers"
    "SDK/devices/MIMXRT1052/utilities"
    "SDK/devices/MIMXRT1052/utilities/debug_console"
    "SDK/devices/MIMXRT1052/utilities/str"
    "SDK/middleware/sdmmc/common"
    "SDK/middleware/sdmmc/host/usdhc"
    "SDK/middleware/sdmmc/osa"
    "SDK/middleware/sdmmc/sdio"
    "board"
    "include"
)

# Shared libraries linked with application
set(TARGET_LIBS
    "freertos_kernel"
    "lwip"
    "m"
    "whd"
)

# Shared library and linker script search paths
set(TARGET_LIB_DIRECTORIES
    "SDK/devices/MIMXRT1052/gcc"
)

# Conditional flags
# DEBUG
set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG -O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG -O0 -g")
set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG -O0 -g")

# RELEASE
set(CMAKE_C_FLAGS_RELEASE        "-DNDEBUG -O2 -flto")
set(CMAKE_CXX_FLAGS_RELEASE      "-DNDEBUG -O2 -flto")
set(CMAKE_ASM_FLAGS_RELEASE      "-DNDEBUG -O2 -flto")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")

# Final compiler flags
set(CMAKE_C_FLAGS          "${CMAKE_C_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS        "${CMAKE_CXX_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS        "${CMAKE_C_FLAGS} -x assembler-with-cpp")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")


set(WHD_EXTERNAL_PATH "${CMAKE_SOURCE_DIR}/include/whd_port" CACHE STRING "")
add_subdirectory(lib/whd)

set(FREERTOS_HEAP "4" CACHE STRING "" FORCE)
set(FREERTOS_PORT "GCC_ARM_CM7" CACHE STRING "" FORCE)
set(FREERTOS_CONFIG_FILE_DIRECTORY "${CMAKE_SOURCE_DIR}/include" CACHE STRING "" FORCE)
add_subdirectory(lib/freertos)

set(LWIP_ADDITIONAL_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include/lwip_port" CACHE STRING "")
add_subdirectory(lib/lwip)

# Shared sources, includes and definitions
add_compile_definitions(${TARGET_C_DEFINES})
include_directories(${TARGET_C_INCLUDES})
link_directories(${TARGET_LIB_DIRECTORIES})
link_libraries(${TARGET_LIBS})

# Main targets are added here

# Create ELF
add_executable("${CMAKE_PROJECT_NAME}_FLASH.elf" ${TARGET_SOURCES})
target_compile_definitions("${CMAKE_PROJECT_NAME}_FLASH.elf"
    PRIVATE ${TARGET_C_DEFINES_XIP}
)
target_link_options("${CMAKE_PROJECT_NAME}_FLASH.elf"
    PRIVATE "-T${TARGET_LDSCRIPT_FLASH}"
    PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}_FLASH.map"
)
set_property(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" APPEND
    PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_PROJECT_NAME}_FLASH.map"
)
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_FLASH.hex"
    COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_FLASH.elf" "${CMAKE_PROJECT_NAME}_FLASH.hex"
    DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.elf"
)
add_custom_target("${CMAKE_PROJECT_NAME}_FLASH_HEX" DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.hex")
if(DEFINED TARGET_TOOLCHAIN_SIZE)
add_custom_command(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" POST_BUILD
    COMMAND ${TARGET_TOOLCHAIN_SIZE} "${CMAKE_PROJECT_NAME}_FLASH.elf"
)
endif()

# Create ELF
add_executable("${CMAKE_PROJECT_NAME}_RAM.elf" ${TARGET_SOURCES})
target_link_options("${CMAKE_PROJECT_NAME}_RAM.elf"
    PRIVATE "-T${TARGET_LDSCRIPT_RAM}"
    PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}_RAM.map"
)
set_property(TARGET "${CMAKE_PROJECT_NAME}_RAM.elf" APPEND
    PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_PROJECT_NAME}_RAM.map"
)
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_RAM.hex"
    COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_RAM.elf" "${CMAKE_PROJECT_NAME}_RAM.hex"
    DEPENDS "${CMAKE_PROJECT_NAME}_RAM.elf"
)
add_custom_target("${CMAKE_PROJECT_NAME}_RAM_HEX" DEPENDS "${CMAKE_PROJECT_NAME}_RAM.hex")
if(DEFINED TARGET_TOOLCHAIN_SIZE)
add_custom_command(TARGET "${CMAKE_PROJECT_NAME}_RAM.elf" POST_BUILD
    COMMAND ${TARGET_TOOLCHAIN_SIZE} "${CMAKE_PROJECT_NAME}_RAM.elf"
)
endif()
