#-- Service --------------------------------------------------------------------
SET(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/MODULES/CmakeConfig_GCC_CortexM4/gcc_cm4f.cmake)
ENABLE_LANGUAGE(ASM)
CMAKE_MINIMUM_REQUIRED(VERSION 3.8.0)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")

IF (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
    MESSAGE(
            FATAL_ERROR
            "In-source builds not allowed.
            Please make a new directory (called a build directory) and run CMake from there.
            You may need to remove CMakeCache.txt."
    )
ENDIF ()

SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#-- Project config -------------------------------------------------------------
PROJECT(Charger)                    # Project name
SET(HARDWARE_REVISION 1)    # HW in servisProg
SET(VERSION \"1.0.0\")             #
SET(HARDWARE_USER_NAME "WeActARTRY")

#SET(VECT_TAB_OFFSET "0x100000")
SET(VECT_TAB_OFFSET "0x000000")

SET(HEXT_VALUE "8000000")
SET(PLL_NS "144")
#-- Defines --------------------------------------------------------------------
ADD_DEFINITIONS(-DUART_DMA_SEND)
ADD_DEFINITIONS(-DDEBUG -DAT32F403ACGT7)
#ADD_DEFINITIONS(-DDEBUG -DAT32F403AVG7 -DAT_START_F403_V1)
#ADD_DEFINITIONS(-DDEBUG -DAT32F437ZMT7 -DAT_START_F435_V1)
ADD_DEFINITIONS(-DFIRMWARE_VERSION=${VERSION})
ADD_DEFINITIONS(-DHARDWARE_REVISION=\"${HARDWARE_REVISION}\")
ADD_DEFINITIONS(-DVECT_TAB_OFFSET=${VECT_TAB_OFFSET})
ADD_DEFINITIONS(-DHEXT_VALUE=${HEXT_VALUE})
ADD_DEFINITIONS(-DPLL_NS=${PLL_NS})
ADD_DEFINITIONS(-DCMSIS_device_header="at32f403a_407.h")
ADD_DEFINITIONS(-DFLASH_PAGE_SIZE=2048)

#-- Project paths, Include dirs, Sources list  ---------------------------------
#ADD_FILES(SOURCES "MODULES/DeviceStartup_ARTERY_AT32F437ZMT7/ld/startup_at32f403a_407.s")
include(modular.cmake)


#-- Options --------------------------------------------------------------------
#IF (PRINTF_FLOAT STREQUAL "1")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -u_printf_float")
#ENDIF ()
IF (SCANF_FLOAT STREQUAL "1")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -u_scanf_float")
ENDIF ()

#-- Linker script --------------------------------------------------------------
SET(LDSCRIPT ${CMAKE_SOURCE_DIR}/APP/AT32F403AxG_FLASH.ld)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${LDSCRIPT} -Wl,-Map=${CMAKE_BINARY_DIR}/${PROJECT_NAME}.map -Wl,--print-memory-usage")


#-- Random BuildId Generation ------------------------------------------------------------
SET(RANDOM_BUILD_ID_GEN_FILE ${CMAKE_SOURCE_DIR}/MODULES/CmakeConfig_RandomBuildIdGenerator/version.cmake)
add_custom_target(GEN_RANDOM_BUILD_ID)
ADD_CUSTOM_COMMAND(TARGET GEN_RANDOM_BUILD_ID POST_BUILD
        COMMAND ${CMAKE_COMMAND} -P ${RANDOM_BUILD_ID_GEN_FILE})


#-- Project linking ------------------------------------------------------------
ADD_EXECUTABLE(${PROJECT_NAME}.elf ${SOURCES})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}.elf)
add_dependencies(${PROJECT_NAME}.elf GEN_RANDOM_BUILD_ID)

#-- Custom commands ------------------------------------------------------------
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} "-Oihex" ${PROJECT_NAME}.elf ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.hex
        COMMAND ${CMAKE_OBJCOPY} "-Obinary" ${PROJECT_NAME}.elf ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.bin
        #        COMMAND ${CMAKE_OBJDUMP} "-DS" ${PROJECT_NAME}.elf > ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.dasm
        COMMAND ${CMAKE_SIZE} ${PROJECT_NAME}.elf)
