include(CMakeForceCompiler)

# CROSS COMPILER SETTING
set(CMAKE_SYSTEM_NAME Generic)
cmake_minimum_required(VERSION 3.19)

# THE VERSION NUMBER
SET (Tutorial_VERSION_MAJOR 1)
SET (Tutorial_VERSION_MINOR 0)

project(my_project)

# ENABLE ASM
enable_language(ASM)

set(CMAKE_STATIC_LIBRARY_PREFIX)
set(CMAKE_STATIC_LIBRARY_SUFFIX)

set(CMAKE_EXECUTABLE_LIBRARY_PREFIX)
set(CMAKE_EXECUTABLE_LIBRARY_SUFFIX)

set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -D__STARTUP_CLEAR_BSS")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -D__STARTUP_INITIALIZE_NONCACHEDATA")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -g")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -mcpu=cortex-m3")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -Wall")
# set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -mfloat-abi=hard")
# set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -mfpu=fpv4-sp-d16")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -mthumb")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -fno-common")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -ffunction-sections")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -fdata-sections")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -ffreestanding")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -fno-builtin")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -mapcs")
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -std=gnu99")

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mcpu=cortex-m3")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall")
# set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mfloat-abi=hard")
# set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mfpu=fpv4-sp-d16")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mthumb")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -MMD")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -MP")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-common")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ffunction-sections")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fdata-sections")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ffreestanding")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-builtin")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mapcs")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -std=gnu99")

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mcpu=cortex-m3")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mfloat-abi=hard")
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mfpu=fpv4-sp-d16")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mthumb")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -MMD")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -MP")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-common")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ffunction-sections")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fdata-sections")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ffreestanding")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-builtin")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mapcs")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-rtti")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-exceptions")

set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -mcpu=cortex-m3")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Wall")
# set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -mfloat-abi=hard")
# set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -mfpu=fpv4-sp-d16")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --specs=nano.specs")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --specs=nosys.specs")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fno-common")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -ffunction-sections")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fdata-sections")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -ffreestanding")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fno-builtin")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -mthumb")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -mapcs")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --gc-sections")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -static")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -z")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} muldefs")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Map=output.map")

# ignore warnings
set(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -Wno-unused-function")
set(CMAKE_C_FLAGS_DEBUG   "${CMAKE_C_FLAGS_DEBUG}   -Wno-unused-function")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wno-unused-function")

set(PrjDirPath ${CMAKE_CURRENT_SOURCE_DIR})

# USER_CONF_START
include_directories(
    ${PrjDirPath}/..
    ${PrjDirPath}/../../../../device/drivers
    ${PrjDirPath}/../../../../components/ff14b/source
    ${PrjDirPath}/../../../../device/CMSIS/Include
    ${PrjDirPath}/../../../../device
)

add_executable(${CMAKE_PROJECT_NAME}.elf
    "${PrjDirPath}/../../../../device/mm32f3273g.h"
    "${PrjDirPath}/../../../../device/system_mm32f3273g.h"
    "${PrjDirPath}/../../../../device/mm32f3273g_features.h"
    "${PrjDirPath}/../../../../device/system_mm32f3273g.c"
    "${PrjDirPath}/../../../../device/hal_device_registers.h"
    "${PrjDirPath}/../../../../device/armgcc/startup_mm32f3273g.S"
    "${PrjDirPath}/../../../../device/drivers/hal_rtc.c"
    "${PrjDirPath}/../../../../device/drivers/hal_wwdg.c"
    "${PrjDirPath}/../../../../device/drivers/hal_uart.h"
    "${PrjDirPath}/../../../../device/drivers/hal_sdio.h"
    "${PrjDirPath}/../../../../device/drivers/hal_pwr.h"
    "${PrjDirPath}/../../../../device/drivers/hal_spi.c"
    "${PrjDirPath}/../../../../device/drivers/hal_i2c.h"
    "${PrjDirPath}/../../../../device/drivers/hal_power.h"
    "${PrjDirPath}/../../../../device/drivers/hal_dma_request.h"
    "${PrjDirPath}/../../../../device/drivers/hal_exti.c"
    "${PrjDirPath}/../../../../device/drivers/hal_rtc.h"
    "${PrjDirPath}/../../../../device/drivers/hal_exti.h"
    "${PrjDirPath}/../../../../device/drivers/hal_fsmc.c"
    "${PrjDirPath}/../../../../device/drivers/hal_usb_bdt.h"
    "${PrjDirPath}/../../../../device/drivers/hal_tim.h"
    "${PrjDirPath}/../../../../device/drivers/hal_power.c"
    "${PrjDirPath}/../../../../device/drivers/hal_pwr.c"
    "${PrjDirPath}/../../../../device/drivers/hal_common.h"
    "${PrjDirPath}/../../../../device/drivers/hal_rcc.h"
    "${PrjDirPath}/../../../../device/drivers/hal_gpio.h"
    "${PrjDirPath}/../../../../device/drivers/hal_syscfg.c"
    "${PrjDirPath}/../../../../device/drivers/hal_i2c.c"
    "${PrjDirPath}/../../../../device/drivers/hal_tim.c"
    "${PrjDirPath}/../../../../device/drivers/hal_flash.c"
    "${PrjDirPath}/../../../../device/drivers/hal_usb.h"
    "${PrjDirPath}/../../../../device/drivers/hal_comp.c"
    "${PrjDirPath}/../../../../device/drivers/hal_fsmc.h"
    "${PrjDirPath}/../../../../device/drivers/hal_iwdg.c"
    "${PrjDirPath}/../../../../device/drivers/hal_spi.h"
    "${PrjDirPath}/../../../../device/drivers/hal_i2s.h"
    "${PrjDirPath}/../../../../device/drivers/hal_dac.h"
    "${PrjDirPath}/../../../../device/drivers/hal_i2s.c"
    "${PrjDirPath}/../../../../device/drivers/hal_uart.c"
    "${PrjDirPath}/../../../../device/drivers/hal_wwdg.h"
    "${PrjDirPath}/../../../../device/drivers/hal_rcc.c"
    "${PrjDirPath}/../../../../device/drivers/hal_sdio.c"
    "${PrjDirPath}/../../../../device/drivers/hal_dma.c"
    "${PrjDirPath}/../../../../device/drivers/hal_dma.h"
    "${PrjDirPath}/../../../../device/drivers/hal_syscfg.h"
    "${PrjDirPath}/../../../../device/drivers/hal_usb.c"
    "${PrjDirPath}/../../../../device/drivers/hal_dac.c"
    "${PrjDirPath}/../../../../device/drivers/hal_flash.h"
    "${PrjDirPath}/../../../../device/drivers/hal_adc.h"
    "${PrjDirPath}/../../../../device/drivers/hal_adc.c"
    "${PrjDirPath}/../../../../device/drivers/hal_comp.h"
    "${PrjDirPath}/../../../../device/drivers/hal_iwdg.h"
    "${PrjDirPath}/../../../../device/drivers/hal_gpio.c"
    "${PrjDirPath}/../diskio_sdcard_sdio.c"
    "${PrjDirPath}/../board_init.h"
    "${PrjDirPath}/../clock_init.h"
    "${PrjDirPath}/../pin_init.c"
    "${PrjDirPath}/../board_init.c"
    "${PrjDirPath}/../sdcard_sdio.h"
    "${PrjDirPath}/../sdcard_sdio.c"
    "${PrjDirPath}/../readme.md"
    "${PrjDirPath}/../pin_init.h"
    "${PrjDirPath}/../clock_init.c"
    "${PrjDirPath}/../main.c"
    "${PrjDirPath}/../ffconf.h"
    "${PrjDirPath}/../../../../components/ff14b/source/ff.c"
    "${PrjDirPath}/../../../../components/ff14b/source/ffsystem.c"
    "${PrjDirPath}/../../../../components/ff14b/source/ffunicode.c"
)

target_compile_definitions(${CMAKE_PROJECT_NAME}.elf
    PRIVATE APP_FATFS
    PRIVATE BRD_PLUS_F3270
)

set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -T${PrjDirPath}/../../../../device/armgcc/linker/mm32f3273g_flash.ld -static")

# USER_CONF_END

target_link_libraries(${CMAKE_PROJECT_NAME}.elf
    -Wl,--start-group
    debug m
    debug c
    debug gcc
    debug nosys
    optimized m
    optimized c
    optimized gcc
    optimized nosys
    -Wl,--end-group
)
