cmake_minimum_required(VERSION 3.22)

set(SRC_ROOT ${CMAKE_SOURCE_DIR}/../../../../../src/)
set(COMMON ${SRC_ROOT}/common)
set(UPDATE ${SRC_ROOT}/cyclone_boot)
set(THIRD_PARTY ${CMAKE_SOURCE_DIR}/../../../../../third_party)
set(HAL_DRIVERS ${THIRD_PARTY}/st/drivers/stm32f4xx_hal_driver/src)
set(BSP ${THIRD_PARTY}/st/boards/stm32f4xx_nucleo_144)
set(TOOLS ${CMAKE_SOURCE_DIR}/../../../../../tools)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Enable CMake support for ASM and C languages
enable_language(C ASM)

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})

target_sources(${CMAKE_PROJECT_NAME} PRIVATE
        ${COMMON}/cpu_endian.c
        ${COMMON}/os_port_none.c
        ${COMMON}/date_time.c
        ${COMMON}/str.c
        ${COMMON}/path.c
        ${COMMON}/resource_manager.c

        ${UPDATE}/core/mailbox.c

        ${THIRD_PARTY}/st/boards2/stm32h750b_discovery/stm32h750b_discovery.c
        ${THIRD_PARTY}/st/boards2/stm32h750b_discovery/stm32h750b_discovery_bus.c
        ${THIRD_PARTY}/st/boards2/stm32h750b_discovery/stm32h750b_discovery_lcd.c
        ${THIRD_PARTY}/st/boards2/stm32h750b_discovery/stm32h750b_discovery_sdram.c
        ${THIRD_PARTY}/st/boards2/utilities/lcd/stm32_lcd.c
        ${THIRD_PARTY}/st/boards2/components/ft5336/ft5336.c
        ${THIRD_PARTY}/st/boards2/components/ft5336/ft5336_reg.c
        ${THIRD_PARTY}/st/boards2/components/mt48lc4m32b2/mt48lc4m32b2.c

        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dma2d.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_ltdc.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_sdram.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_fmc.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_qspi.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_cortex.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_rcc.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_rcc_ex.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_flash.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_flash_ex.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_gpio.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_hsem.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dma.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dma_ex.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_mdma.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_pwr.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_pwr_ex.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_i2c.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_i2c_ex.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_exti.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_tim.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_tim_ex.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_uart.c
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_uart_ex.c

        ${CMAKE_SOURCE_DIR}/cmake/startup_stm32h750xbhx.s
        ${CMAKE_SOURCE_DIR}/cmake/syscalls.c
        ${CMAKE_SOURCE_DIR}/cmake/sysmem.c
        ${CMAKE_SOURCE_DIR}/src/system_stm32h7xx.c
        ${CMAKE_SOURCE_DIR}/src/stm32h7xx_hal_msp.c
        ${CMAKE_SOURCE_DIR}/src/stm32h7xx_it.c
        ${CMAKE_SOURCE_DIR}/src/main.c
        ${CMAKE_SOURCE_DIR}/src/debug.c
)

target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        ${CMAKE_SOURCE_DIR}/src
        ${THIRD_PARTY}/cmsis/include
        ${THIRD_PARTY}/st/devices/stm32h7xx
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/inc
        ${THIRD_PARTY}/st/drivers/stm32h7xx_hal_driver/inc/legacy
        ${THIRD_PARTY}/st/boards2/stm32h750b_discovery
        ${THIRD_PARTY}/st/boards2/utilities/
        ${THIRD_PARTY}/st/boards2/utilities/lcd
        ${THIRD_PARTY}/st/boards2/utilities/Fonts
        ${THIRD_PARTY}/st/boards2/components
        ${THIRD_PARTY}/st/boards2/components/common
        ${THIRD_PARTY}/st/boards2/components/ft5336
        ${THIRD_PARTY}/st/boards2/components/mt48lc4m32b2
        ${THIRD_PARTY}/st/boards2/components/rk043fn48h
        ${COMMON}
        ${UPDATE}
)

target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    STM32H750xx
    APPLICATION_ADDRESS=0x90000000U
    USE_HAL_DRIVER
    USE_STM32H750B_DISCO
    USE_PWR_LDO_SUPPLY
    __USE_C99_MATH

    $<$<CONFIG:Debug>:DEBUG>
)


# Add the map file to the list of files to be removed with 'clean' target
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map)

# Convert .elf to .bin
add_custom_command(TARGET app_blinky POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O binary
        ${CMAKE_CURRENT_BINARY_DIR}/app_blinky.elf
        ${CMAKE_CURRENT_BINARY_DIR}/app_blinky.bin
        COMMENT "Generating binary file from ELF"
)

# Copy the generated .bin file to ../
set(VERSION_HEADER "${CMAKE_SOURCE_DIR}/src/version.h")
# Read the contents of the header
file(READ "${VERSION_HEADER}" VERSION_HEADER_CONTENT)
# Extract the version string from e.g., #define APP_VERSION "1.0.0"
string(REGEX MATCH "#define APP_VERSION_STRING \"([0-9\\.]+)\"" _ UNUSED ${VERSION_HEADER_CONTENT})
set(APP_VERSION_RAW "${CMAKE_MATCH_1}")
string(REPLACE "." "_" APP_VERSION "${APP_VERSION_RAW}")

configure_file(${VERSION_HEADER} ${CMAKE_CURRENT_BINARY_DIR}/version.h COPYONLY)

message("Detected application version: ${APP_VERSION_RAW}")
add_custom_command(TARGET app_blinky POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${CMAKE_CURRENT_BINARY_DIR}/app_blinky.bin
        ${CMAKE_CURRENT_SOURCE_DIR}/../app_blinky_${APP_VERSION}.bin
        COMMENT "Copying iap_demo.bin to parent directory"
)

add_custom_command(TARGET app_blinky POST_BUILD
        COMMAND ${TOOLS}/ImageBuilder/bin/image_builder_linux
        --input ${CMAKE_CURRENT_SOURCE_DIR}/../app_blinky_${APP_VERSION}.bin
        --output ${CMAKE_CURRENT_SOURCE_DIR}/../app_blinky_${APP_VERSION}.img.bin
        --vtor-align

        COMMAND ${TOOLS}/ImageBuilder/bin/image_builder_linux
        --input ${CMAKE_CURRENT_SOURCE_DIR}/../app_blinky_${APP_VERSION}.bin
        --output ${CMAKE_CURRENT_SOURCE_DIR}/../app_blinky_${APP_VERSION}_update.img
        --integrity-algo sha256
        --vtor-align

        COMMENT "Generating app_blinky images using ImageBuilder"
)

