cmake_minimum_required(VERSION 3.17)

if(GTEST)

    add_definitions(-DGTEST)
    include(FetchContent)
    FetchContent_Declare(
            googletest
            # Specify the commit you depend on and update it regularly.
            URL https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip
    )
    # For Windows: Prevent overriding the parent project's compiler/linker settings
    set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(googletest)
    project(gtest C CXX ASM)
    add_subdirectory(common_functions)
    include_directories(gtests)
    add_executable(example gtests/unit-test.cpp)
    target_link_libraries(example gtest_main common_functions_lib sim800_lib)
    add_test(NAME example_test COMMAND example)
    # Now simply link against gtest or gtest_main as needed. Eg



else()
    set (CRC_ADDRESS 0)
    set (CRC32_USER_PROG_AND_USER_LIB_ADDRESS 0)
    add_subdirectory(cmake_func)
    set(MEM_MAP_FILE "memory_map.inc")
    if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" AND NOT BOOT)
        set(MEM_MAP_FILE_DEBUG "memory_map_debug.inc")
        FUNC_CREATE_MEMORY_MAP("${MEM_MAP_FILE_DEBUG}")
    else()
        set(MEM_MAP_FILE "memory_map.inc")
        FUNC_CREATE_MEMORY_MAP("${MEM_MAP_FILE}" )
    endif()
    add_compile_definitions("PAGE_SIZE=0x0800")
    set(CMAKE_SYSTEM_NAME Generic)
    set(CMAKE_SYSTEM_VERSION 1)
    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(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)
    add_compile_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork )
    add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
    #PAGE SIZE FOR F0 Series with 256KB Flash 0x0800 For 128Kb Flash 0x0400

    # uncomment to mitigate c++17 absolute addresses warnings
    #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register")
    if(BOOT)
        set (LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/bootloader/STM32G070CBTX_FLASH.ld)
    else()
        set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32G070CBTX_FLASH.ld )
    endif()
    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 ()

    # project settings
    if (BOOT)
        project(reader-boot C CXX ASM)
        add_compile_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork)
        add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
        file(GLOB_RECURSE SOURCES "bootloader/App/*.*" "bootloader/Core/*.*" "bootloader/Drivers/*.*" "bootloader/ARMCM0_STM32F0/*.*")

        include_directories(Core/Inc Drivers/STM32G0xx_HAL_Driver/Inc Drivers/STM32G0xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32G0xx/Include Drivers/CMSIS/Include Core/Interfaces Core/Interfaces/Templates App)
        include_directories(loader)
        include_directories(ARMCM0_STM32G0)
        add_definitions(-DDEBUG -DUSE_FULL_LL_DRIVER -DUSE_HAL_DRIVER -DSTM32G070xx)

        file(GLOB_RECURSE SOURCES "Boot/Core/*.*" "Drivers/*.*" "Boot/App/*.*" "Boot/loader/*.*" "Boot/ARMCM0_STM32G0/*.*")
        list(FILTER SOURCES EXCLUDE REGEX "IAR\\/.*.[c|h]$")
        list(FILTER SOURCES EXCLUDE REGEX "Keil\\/.*.[c|h]$")
        set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Boot/STM32G070CBTX_FLASH.ld)

        add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
        add_link_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork)
        add_link_options(-T ${LINKER_SCRIPT})

        add_subdirectory(common_functions)
        add_executable(${PROJECT_NAME}.elf ${SOURCES} )
        set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
        set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
        add_custom_command(TARGET ${PROJECT_NAME}.elf PRE_BUILD
                COMMAND  ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/${MEM_MAP_FILE}" ${PROJECT_BINARY_DIR}/${MEM_MAP_FILE}
                )
        add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
                COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
                COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
                COMMENT "Building ${HEX_FILE}
    Building ${BIN_FILE}")

    else()

        project(reader-main-prog C CXX ASM)

include_directories(Core/Inc Drivers/STM32G0xx_HAL_Driver/Inc Drivers/STM32G0xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32G0xx/Include Drivers/CMSIS/Include Core/Interfaces Core/Interfaces/Templates App)

add_definitions(-DDEBUG -DUSE_HAL_DRIVER -DSTM32G070xx)

file(GLOB_RECURSE SOURCES "Core/*.*" "Drivers/*.*" "App/*.*")

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32G070CBTX_FLASH.ld)



        add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
        add_link_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork )
        add_link_options(-T ${LINKER_SCRIPT})

        add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})
        target_link_libraries(${PROJECT_NAME}.elf common_functions_lib fs_lib_sim800 sim800_lib)
        set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
        set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
        set(HEX_FILE_SRECORD ${PROJECT_BINARY_DIR}/${PROJECT_NAME}_SRECORD.hex)
        set(HEX_FILE_SRECORD2 ${PROJECT_BINARY_DIR}/${PROJECT_NAME}_SRECORD2.hex)

        if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
            add_custom_command(TARGET ${PROJECT_NAME}.elf PRE_BUILD
                    COMMAND  ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/${MEM_MAP_FILE_DEBUG}" ${PROJECT_BINARY_DIR}/${MEM_MAP_FILE}
                    )
        else()
            add_custom_command(TARGET ${PROJECT_NAME}.elf PRE_BUILD
                    COMMAND  ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/${MEM_MAP_FILE}" ${PROJECT_BINARY_DIR}/${MEM_MAP_FILE}
                    )
        endif()
        math(EXPR CRC_USER_END_ADDRESS "${CRC32_USER_PROG_AND_USER_LIB_ADDRESS} + 4")
        add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
                COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
                COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
                COMMENT "Building ${HEX_FILE} Building ${BIN_FILE}"
                COMMAND srec_cat.exe ${HEX_FILE} -intel  -crop 0x08002000 ${CRC32_USER_PROG_AND_USER_LIB_ADDRESS} -fill 0xff 0x08002000 ${CRC32_USER_PROG_AND_USER_LIB_ADDRESS} -CRC32LE  ${CRC32_USER_PROG_AND_USER_LIB_ADDRESS} -o ${HEX_FILE_SRECORD} -intel
                COMMAND srec_cat.exe ${HEX_FILE_SRECORD} -intel  -crop 0x08002000 ${CRC_USER_END_ADDRESS}   ${HEX_FILE} -intel  -crop ${CRC_USER_END_ADDRESS}  ${CRC_ADDRESS}  -o ${HEX_FILE_SRECORD2} -intel
                COMMAND srec_cat.exe  ${HEX_FILE_SRECORD2} -intel  -crop 0x08002000 ${CRC_ADDRESS} -fill 0xff 0x08002000 ${CRC_ADDRESS} -CRC32LE   ${CRC_ADDRESS} -o ${HEX_FILE_SRECORD} -intel
                COMMAND ${CMAKE_OBJCOPY} -Obinary -Iihex ${HEX_FILE_SRECORD} ${BIN_FILE}
                )
    endif()
endif()