cmake_minimum_required(VERSION 3.10)

project(rt1050_freertos_hello)

enable_language(CXX)
enable_language(ASM)

# Device specific settings, goes to CFLAGS and LDFLAGS
set(CFLAGS_HARDWARE "-mcpu=cortex-m7 -mfpu=fpv5-d16 -mfloat-abi=hard -mthumb")

# Extra CFlags
set(CFLAGS_EXTRA "-Og")
set(LDFLAGS_EXTRA "-Wl,--print-memory-usage")

# Different linker scripts
set(LDSCRIPT_RAM "${CMAKE_SOURCE_DIR}/MIMXRT1052/gcc/MIMXRT1052xxxxx_ram.ld")
set(LDSCRIPT_FLEXSPI "${CMAKE_SOURCE_DIR}/MIMXRT1052/gcc/MIMXRT1052xxxxx_flexspi_nor.ld")
set(LDSCRIPT_SDRAM "${CMAKE_SOURCE_DIR}/MIMXRT1052/gcc/MIMXRT1052xxxxx_sdram.ld")

# Copy them from Makefile
set(C_SOURCES
    "drivers/fsl_common.c"
    "drivers/fsl_elcdif.c"
    "drivers/fsl_clock.c"
    "drivers/fsl_enet.c"
    "drivers/fsl_gpio.c"
    "drivers/fsl_semc.c"
    "drivers/fsl_lpuart.c"
    "drivers/fsl_usdhc.c"
    "drivers/fsl_cache.c"
    "freertos/freertos_kernel/tasks.c"
    "freertos/freertos_kernel/event_groups.c"
    "freertos/freertos_kernel/croutine.c"
    "freertos/freertos_kernel/list.c"
    "freertos/freertos_kernel/timers.c"
    "freertos/freertos_kernel/portable/GCC/ARM_CM4F/port.c"
    "freertos/freertos_kernel/portable/MemMang/heap_4.c"
    "freertos/freertos_kernel/stream_buffer.c"
    "freertos/freertos_kernel/queue.c"
    "device/system_MIMXRT1052.c"
    "xip/evkbimxrt1050_flexspi_nor_config.c"
    "xip/fsl_flexspi_nor_boot.c"
    "component/lists/fsl_component_generic_list.c"
    "component/serial_manager/fsl_component_serial_port_uart.c"
    "component/serial_manager/fsl_component_serial_manager.c"
    "component/osa/fsl_os_abstraction_free_rtos.c"
    "component/uart/fsl_adapter_lpuart.c"
    "component/phy/device/phyksz8081/fsl_phyksz8081.c"
    "component/phy/mdio/enet/fsl_enet_mdio.c"
    "source/freertos_hello.c"
    "source/user_irqhandlers.c"
    "source/sdmmc_config.c"
    "source/tasks/user_hello_task.c"
    "source/tasks/user_sdcard_task.c"
    "board/peripherals.c"
    "board/pin_mux.c"
    "board/board.c"
    "board/dcd.c"
    "board/clock_config.c"
    "utilities/fsl_debug_console.c"
    "utilities/fsl_sbrk.c"
    "utilities/fsl_assert.c"
    "utilities/fsl_str.c"
    "middleware/sdmmc/sd/fsl_sd.c"
    "middleware/sdmmc/common/fsl_sdmmc_common.c"
    "middleware/sdmmc/mmc/fsl_mmc.c"
    "middleware/sdmmc/osa/fsl_sdmmc_osa.c"
    "middleware/sdmmc/sdio/fsl_sdio.c"
    "middleware/sdmmc/host/usdhc/non_blocking/fsl_sdmmc_host.c"
    "middleware/fatfs/source/ffsystem.c"
    "middleware/fatfs/source/ffunicode.c"
    "middleware/fatfs/source/diskio.c"
    "middleware/fatfs/source/ff.c"
    "middleware/fatfs/source/fsl_sd_disk/fsl_sd_disk.c"
    "middleware/lwip/src/core/ipv4/ip4.c"
    "middleware/lwip/src/core/ipv4/ip4_frag.c"
    "middleware/lwip/src/core/ipv4/acd.c"
    "middleware/lwip/src/core/ipv4/dhcp.c"
    "middleware/lwip/src/core/ipv4/icmp.c"
    "middleware/lwip/src/core/ipv4/ip4_addr.c"
    "middleware/lwip/src/core/ipv4/igmp.c"
    "middleware/lwip/src/core/ipv4/autoip.c"
    "middleware/lwip/src/core/ipv4/etharp.c"
    "middleware/lwip/src/core/memp.c"
    "middleware/lwip/src/core/sys.c"
    "middleware/lwip/src/core/altcp.c"
    "middleware/lwip/src/core/ip.c"
    "middleware/lwip/src/core/udp.c"
    "middleware/lwip/src/core/tcp_in.c"
    "middleware/lwip/src/core/mem.c"
    "middleware/lwip/src/core/raw.c"
    "middleware/lwip/src/core/dns.c"
    "middleware/lwip/src/core/tcp.c"
    "middleware/lwip/src/core/inet_chksum.c"
    "middleware/lwip/src/core/ipv6/mld6.c"
    "middleware/lwip/src/core/ipv6/ethip6.c"
    "middleware/lwip/src/core/ipv6/ip6_frag.c"
    "middleware/lwip/src/core/ipv6/dhcp6.c"
    "middleware/lwip/src/core/ipv6/ip6.c"
    "middleware/lwip/src/core/ipv6/inet6.c"
    "middleware/lwip/src/core/ipv6/ip6_addr.c"
    "middleware/lwip/src/core/ipv6/nd6.c"
    "middleware/lwip/src/core/ipv6/icmp6.c"
    "middleware/lwip/src/core/netif.c"
    "middleware/lwip/src/core/def.c"
    "middleware/lwip/src/core/tcp_out.c"
    "middleware/lwip/src/core/init.c"
    "middleware/lwip/src/core/altcp_tcp.c"
    "middleware/lwip/src/core/pbuf.c"
    "middleware/lwip/src/core/altcp_alloc.c"
    "middleware/lwip/src/core/timeouts.c"
    "middleware/lwip/src/core/stats.c"
    "middleware/lwip/src/api/sockets.c"
    "middleware/lwip/src/api/api_msg.c"
    "middleware/lwip/src/api/netdb.c"
    "middleware/lwip/src/api/api_lib.c"
    "middleware/lwip/src/api/tcpip.c"
    "middleware/lwip/src/api/netbuf.c"
    "middleware/lwip/src/api/if_api.c"
    "middleware/lwip/src/api/err.c"
    "middleware/lwip/src/api/netifapi.c"
    "middleware/lwip/src/netif/ethernet.c"
    "middleware/lwip/src/netif/slipif.c"
    "middleware/lwip/src/netif/ppp/ccp.c"
    "middleware/lwip/src/netif/ppp/utils.c"
    "middleware/lwip/src/netif/ppp/chap-md5.c"
    "middleware/lwip/src/netif/ppp/upap.c"
    "middleware/lwip/src/netif/ppp/eui64.c"
    "middleware/lwip/src/netif/ppp/eap.c"
    "middleware/lwip/src/netif/ppp/lwip_ecp.c"
    "middleware/lwip/src/netif/ppp/mppe.c"
    "middleware/lwip/src/netif/ppp/ipcp.c"
    "middleware/lwip/src/netif/ppp/ipv6cp.c"
    "middleware/lwip/src/netif/ppp/multilink.c"
    "middleware/lwip/src/netif/ppp/pppcrypt.c"
    "middleware/lwip/src/netif/ppp/polarssl/md4.c"
    "middleware/lwip/src/netif/ppp/polarssl/des.c"
    "middleware/lwip/src/netif/ppp/polarssl/sha1.c"
    "middleware/lwip/src/netif/ppp/polarssl/md5.c"
    "middleware/lwip/src/netif/ppp/polarssl/arc4.c"
    "middleware/lwip/src/netif/ppp/pppol2tp.c"
    "middleware/lwip/src/netif/ppp/demand.c"
    "middleware/lwip/src/netif/ppp/pppapi.c"
    "middleware/lwip/src/netif/ppp/lcp.c"
    "middleware/lwip/src/netif/ppp/vj.c"
    "middleware/lwip/src/netif/ppp/ppp.c"
    "middleware/lwip/src/netif/ppp/chap-new.c"
    "middleware/lwip/src/netif/ppp/pppoe.c"
    "middleware/lwip/src/netif/ppp/fsm.c"
    "middleware/lwip/src/netif/ppp/pppos.c"
    "middleware/lwip/src/netif/ppp/auth.c"
    "middleware/lwip/src/netif/ppp/magic.c"
    "middleware/lwip/src/netif/ppp/chap_ms.c"
    "middleware/lwip/src/netif/zepif.c"
    "middleware/lwip/src/netif/lowpan6_ble.c"
    "middleware/lwip/src/netif/lowpan6_common.c"
    "middleware/lwip/src/netif/bridgeif.c"
    "middleware/lwip/src/netif/bridgeif_fdb.c"
    "middleware/lwip/src/netif/lowpan6.c"
    "middleware/lwip/port/sys_arch.c"
    "middleware/lwip/port/enet_ethernetif.c"
    "middleware/lwip/port/enet_ethernetif_kinetis.c"
)

# Copy them from Makefile
set(ASM_SOURCES
    "startup/startup_MIMXRT1052.S"
)

# Copy them from Makefile
add_definitions(
    "-DCPU_MIMXRT1052DVL6B"
    "-DFSL_RTOS_FREE_RTOS"
    "-DUSE_RTOS"
    "-DSERIAL_PORT_TYPE_UART=1"
    "-D__STARTUP_CLEAR_BSS"
    "-D__STARTUP_INITIALIZE_NONCACHEDATA"
    "-DSD_ENABLED"
)

# Copy them from Makefile
include_directories(
    "drivers"
    "CMSIS"
    "freertos/freertos_kernel/include"
    "freertos/freertos_kernel/portable/GCC/ARM_CM4F"
    "device"
    "xip"
    "component/lists"
    "component/serial_manager"
    "component/osa"
    "component/uart"
    "component/phy"
    "component/phy/mdio/enet"
    "component/phy/device/phyksz8081"
    "source"
    "board"
    "utilities"
    "middleware/sdmmc/sd"
    "middleware/sdmmc/common"
    "middleware/sdmmc/mmc"
    "middleware/sdmmc/osa"
    "middleware/sdmmc/sdio"
    "middleware/sdmmc/host/usdhc"
    "middleware/fatfs/source"
    "middleware/fatfs/source/fsl_sd_disk"
    "middleware/lwip/src/include"
    "middleware/lwip/port"
)

# Conditional compiler flags
set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG")
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG")

set(CMAKE_CXX_FLAGS_DEBUG "-g -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG")

set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")

# Final compiler flags
set(CMAKE_C_FLAGS "${CFLAGS_HARDWARE} ${CFLAGS_EXTRA} -Wall -fdata-sections -ffunction-sections -ffreestanding -fno-builtin")
set(CMAKE_CXX_FLAGS "${CFLAGS_HARDWARE} ${CFLAGS_EXTRA} -Wall -fdata-sections -ffunction-sections -ffreestanding  -fno-builtin")
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
set(CMAKE_EXE_LINKER_FLAGS "${CFLAGS_HARDWARE} -specs=nano.specs -specs=nosys.specs -Wl,--gc-sections -lc -lm -lnosys ${LDFLAGS_EXTRA}")

# Main targets are added here

# **** All In SRAM ****

# Create ELF
add_executable("${CMAKE_PROJECT_NAME}_RAM.elf" ${C_SOURCES} ${ASM_SOURCES})

# Linker script and Map files
target_link_options("${CMAKE_PROJECT_NAME}_RAM.elf"
    PRIVATE "-T${LDSCRIPT_RAM}"
    PRIVATE "-Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_RAM.map,--cref"
)

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"
)

# **** FLEXSPI ****

# Create ELF
add_executable("${CMAKE_PROJECT_NAME}_FLEXSPI.elf" ${C_SOURCES} ${ASM_SOURCES})

# Linker script and Map files
target_link_options("${CMAKE_PROJECT_NAME}_FLEXSPI.elf"
    PRIVATE "-T${LDSCRIPT_FLEXSPI}"
    PRIVATE "-Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_FLEXSPI.map,--cref"
)

target_compile_definitions("${CMAKE_PROJECT_NAME}_FLEXSPI.elf"
    PRIVATE "XIP_EXTERNAL_FLASH=1"
    PRIVATE "XIP_BOOT_HEADER_ENABLE=1"
)

add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_FLEXSPI.hex"
    COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_FLEXSPI.elf" "${CMAKE_PROJECT_NAME}_FLEXSPI.hex"
    DEPENDS "${CMAKE_PROJECT_NAME}_FLEXSPI.elf"
)

add_custom_target("${CMAKE_PROJECT_NAME}_FLEXSPI_HEX"
    DEPENDS "${CMAKE_PROJECT_NAME}_FLEXSPI.hex"
)

# **** SDRAM ****

# Create ELF
add_executable("${CMAKE_PROJECT_NAME}_SDRAM.elf" ${C_SOURCES} ${ASM_SOURCES})

# Linker script and Map files
target_link_options("${CMAKE_PROJECT_NAME}_SDRAM.elf"
    PRIVATE "-T${LDSCRIPT_SDRAM}"
    PRIVATE "-Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_SDRAM.map,--cref"
)

target_compile_definitions("${CMAKE_PROJECT_NAME}_SDRAM.elf"
    PRIVATE "DATA_SECTION_IS_CACHABLE=1"
    PRIVATE "SKIP_SYSCLK_INIT"
)

add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_SDRAM.hex"
    COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_SDRAM.elf" "${CMAKE_PROJECT_NAME}_SDRAM.hex"
    DEPENDS "${CMAKE_PROJECT_NAME}_SDRAM.elf"
)

add_custom_target("${CMAKE_PROJECT_NAME}_SDRAM_HEX"
    DEPENDS "${CMAKE_PROJECT_NAME}_SDRAM.hex"
)