cmake_minimum_required(VERSION 3.22)
project(iap_demo)
enable_language(C ASM)

# Setup compiler settings
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()

# Include toolchain file
include("gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
message("Build type: " ${CMAKE_BUILD_TYPE})

add_executable(iap_demo)

target_sources(iap_demo PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/cpu_endian.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/os_port_freertos.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/date_time.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/str.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/path.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/resource_manager.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/core/crc32.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/drivers/mcu_core/stm32h7xx_mcu_driver.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/drivers/flash/internal/stm32h7xx_flash_driver.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/image/image.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/image/image_process.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/image/image_utils.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/memory/memory.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/memory/memory_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/core/verify.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/security/verify_auth.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/security/verify_sign.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/security/cipher.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/update/update.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/update/update_misc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/update/update_fallback.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/md5.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha1.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha224.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha256.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha384.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha512.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mac/hmac.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/rc4.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/idea.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/des.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/des3.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/aes.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/camellia.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/seed.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/aria.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher_modes/cbc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/aead/ccm.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/aead/gcm.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/chacha.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mac/poly1305.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/aead/chacha20_poly1305.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/dh.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/rsa.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/dsa.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ec.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ec_curves.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ecdh.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ecdsa.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/eddsa.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/curve25519.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/curve448.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/x25519.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/x448.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ed25519.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ed448.c
           ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mpi/mpi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mpi/mpi_misc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/encoding/base64.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/encoding/asn1.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/encoding/oid.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_import.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_key_import.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_decrypt.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_common.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pkcs5_decrypt.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pkcs5_common.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pkcs8_key_parse.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_key_parse.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_cert_parse.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_cert_validate.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_cert_ext_parse.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_common.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_sign_parse.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_sign_verify.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/kdf/hkdf.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/rng/yarrow.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ec_misc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/rsa_misc.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/portable/gcc/arm_cm7/r0p1/port.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/croutine.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/list.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/queue.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/tasks.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/timers.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/portable/memmang/heap_3.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards/stm32h7xx_nucleo/stm32h7xx_nucleo.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_adc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_adc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_cec.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_comp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_cortex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_crc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_crc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_cryp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_cryp_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dac.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dac_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dcmi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dfsdm.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dma.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dma_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dma2d.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_dsi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_exti.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_fdcan.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_flash.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_flash_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_gpio.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_hash.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_hash_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_hcd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_hrtim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_hsem.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_i2c.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_i2c_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_i2s.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_i2s_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_irda.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_iwdg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_jpeg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_lptim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_ltdc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_ltdc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_mdios.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_mdma.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_mmc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_mmc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_nand.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_nor.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_opamp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_opamp_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_pcd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_pcd_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_pwr.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_pwr_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_qspi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_ramecc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_rcc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_rcc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_rng.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_rtc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_rtc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_sai.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_sai_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_sd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_sdram.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_sd_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_smartcard.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_smartcard_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_smbus.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_spdifrx.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_spi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_spi_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_sram.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_swpmi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_tim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_tim_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_uart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_uart_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_usart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_usart_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_hal_wwdg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_adc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_bdma.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_comp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_crc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_dac.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_delayblock.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_dma.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_dma2d.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_exti.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_fmc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_gpio.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_hrtim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_i2c.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_lptim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_lpuart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_mdma.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_opamp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_pwr.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_rcc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_rng.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_rtc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_sdmmc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_spi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_swpmi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_tim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_usart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/src/stm32h7xx_ll_usb.c

        ${CMAKE_CURRENT_SOURCE_DIR}/startup_stm32h7a3xxq.S
        ${CMAKE_CURRENT_SOURCE_DIR}/syscalls.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/system_stm32h7xx.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/stm32h7xx_it.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/main.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/uart_user.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/common.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/ymodem.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/debug.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
)

target_include_directories(iap_demo PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/../src
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/cmsis/include
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/devices/stm32h7xx
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h7xx_hal_driver/
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards/stm32h7xx_nucleo
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/include
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/freertos/portable/gcc/arm_cm7/r0p1
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules
)

target_compile_definitions(iap_demo PRIVATE
    STM32H7A3xxQ
    USE_HAL_DRIVER
    USE_NUCLEO_H7A3ZI_Q
    STM32H7A3xI
    FLASH_DB_MODE
    __USE_C99_MATH

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

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

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

# Pre-build step: Generate res.c using ResourceCompiler
if(WIN32)
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../tools/ResourceCompiler/bin/rc.exe
                ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
                ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
        COMMENT "Running Resource Compiler to generate res.c"
    )
elseif(LINUX)
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../tools/ResourceCompiler/bin/rc
                ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
                ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
        COMMENT "Running Resource Compiler to generate res.c"
    )
endif()

# Copy the generated .bin file to ../
add_custom_command(TARGET iap_demo POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_BINARY_DIR}/iap_demo.bin
            ${CMAKE_CURRENT_SOURCE_DIR}/../
    COMMENT "Copying iap_demo.bin to parent directory"
)
