#THIS FILE IS AUTO GENERATED FROM THE TEMPLATE! DO NOT CHANGE!
cmake_minimum_required(VERSION 3.23)


#/////////////////////////////////////////////PLEASE CHOOSE THIS OPTIONS BY YOURSELF
set (HAL_DRIVER_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/main_prog/Core/Inc/stm32g0xx_hal_conf.h)
set(MCU "STM32G070xx")
if(BOOT)
    set (LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Boot/STM32G070CBTX_FLASH.ld)
else()
    set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/main_prog/STM32G070CBTX_FLASH.ld )
endif()
set(FREERTOS_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/main_prog/Core/Inc/FreeRTOSConfig.h")
#/////////////////////////////////////////////

string (SUBSTRING "${MCU}" 0 7 MCU_SERIES)
string(APPEND MCU_SERIES "XX")
if(${MCU_SERIES} MATCHES  "STM32F4|STM32F3|STM32WB|STM32WL|STM32L4|STM32WB|STM32WL|STM32G4")
    set(CORTEX-M "4")
    set(HARDWARE_FLOAT 1)
elseif(${MCU_SERIES}  MATCHES  "STM32F2|STM32F1")
    set(CORTEX-M "3")
    set(HARDWARE_FLOAT 0)
elseif(${MCU_SERIES}  MATCHES  "STM32F0|STM32C0|STM32G0|STM32L0" )
    set(CORTEX-M "0")
    set(HARDWARE_FLOAT 0)
else()
    message(FATAL_ERROR "${MCU_SERIES} is not supported")
endif()

set(MCU_SERIES ${MCU_SERIES})

add_subdirectory(Cmake_templates)
if(GTEST)
add_subdirectory(libs/Drivers/CMSIS)
add_subdirectory(libs/Drivers/${MCU_SERIES}_HAL_Driver)
add_subdirectory(gtests)
else()
    set(CMAKE_SYSTEM_NAME Generic)
    set(CMAKE_OBJECT_PATH_MAX 300)
    set(CMAKE_C_COMPILER arm-none-eabi-gcc)
    set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
    set(CMAKE_ASM_COMPILER  arm-none-eabi-gcc)
    set(LEN_OFFSET 0xBC)
    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
    set (CRC_ADDRESS 0)
    set (FLASH_MAIN_ADDRESS 0)
    set(MEM_MAP_FILE "memory_map.inc")
    add_subdirectory(cmake_func)
    add_compile_definitions("PAGE_SIZE=0x0800")


        set(CMAKE_AR arm-none-eabi-ar)
        set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
        set(CMAKE_OBJDUMP arm-none-eabi-objdump)
        set(SIZE arm-none-eabi-size)
        set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
        project(myProject )
        #//////////////////////////////////////////////////////THIS IS USER CODE PART


        #//////////////////////////////////////////////////////END USER CODE PART


        set(CMAKE_AR arm-none-eabi-ar)
        set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
        set(CMAKE_OBJDUMP arm-none-eabi-objdump)
        set(SIZE arm-none-eabi-size)
        set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)


        set(COMPILE_OPTIONS
                -mcpu=cortex-m${CORTEX-M}
                -mthumb -mthumb-interwork
                -ffunction-sections
                -fdata-sections
                -fno-common
                -fmessage-length=0
                $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
                $<$<COMPILE_LANGUAGE:CXX>:-fpermissive>

                )
        set(LINK_OPTIONS
                -Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map
                -mcpu=cortex-m${CORTEX-M}
                -mthumb
                -mthumb-interwork
                -T ${LINKER_SCRIPT}
                )

        set(COMPILE_DEF
                USE_HAL_DRIVER
                USE_FULL_LL_DRIVER
                ${ENV_MCU}
                #//////////////////////////USER DEFINITIONS
                MBEDTLS_CONFIG_FILE="mbedtls_config.h"
                #/////////////////////////////////////////
                )

        set(LINK_OPTIONS "${LINK_OPTIONS}")


        add_link_options(${LINK_OPTIONS})
        if (${CORTEX-M} EQUAL 4)
            if (${HARDWARE_FLOAT} EQUAL 1)
                add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
                add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
                add_compile_definitions(ARM_MATH_CM4 ARM_MATH_MATRIX_CHECK ARM_MATH_ROUNDING)
            elseif(${HARDWARE_FLOAT} EQUAL 0 AND $})
                string(PREPEND COMPILE_OPTIONS " -mfloat-abi=soft")
            endif ()
        endif()
        set(COMPILE_OPTIONS "${COMPILE_OPTIONS}")
        set(COMPILE_DEF "${COMPILE_DEF}")

    if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
        message(STATUS "Maximum optimization for speed")
        add_definitions(-DWATCHDOG_IWDG)
        if (BOOT)
            add_compile_options(-Os -g)
        else()
            add_compile_options(-Ofast -g)
        endif()

    elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
        message(STATUS "Maximum optimization for speed, debug info included")
        add_compile_options(-Ofast -g)

    elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
        message(STATUS "Maximum optimization for size")
        add_compile_options(-Os)
    else ()
        message(STATUS "Minimal optimization, debug info included")
        add_definitions(-DWATCHDOG_IWDG)
        add_compile_options(-Os -g)
    endif ()

    if (BOOT)
        set (HAL_DRIVER_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/Boot/Core/Inc/stm32g0xx_hal_conf.h)
        FUNC_CREATE_MEMORY_MAP("${MEM_MAP_FILE}" "128" "8" "4" )
        math(EXPR LEN_ADDRESS "${FLASH_MAIN_ADDRESS} + ${LEN_OFFSET}" OUTPUT_FORMAT HEXADECIMAL)
        add_compile_definitions("BOOT_FLASH_VECTOR_TABLE_CS_OFFSET=${LEN_OFFSET}" "LEN_ADDRESS=${LEN_ADDRESS}")
        add_compile_definitions("PAGE_SIZE=0x0800")
        add_subdirectory(libs)
        add_subdirectory(Boot)
    else()
        add_subdirectory(libs)
        add_subdirectory(main_prog)
    endif()

#//////////////////////////////////////////////////////THIS IS USER CODE PART

#//////////////////////////////////////////////////////END USER CODE PART
endif()