cmake_minimum_required(VERSION 3.10)
set(PROJ_NAME "lark1fq")
set(BIN_FILE ${PROJ_NAME}.bin)
set(ASM_FILE ${PROJ_NAME}.asm)
project(${PROJ_NAME} VERSION 0.1)

# toolchain path
set(TOOLCHAIN "arm-none-eabi-")
set(CMAKE_C_COMPILER "${TOOLCHAIN}gcc")
set(CMAKE_ASM_COMPILER "${TOOLCHAIN}gcc")
set(CMAKE_OBJCOPY "${TOOLCHAIN}objcopy")
set(CMAKE_OBJDUMP "${TOOLCHAIN}objdump")
set(CMAKE_AR "${TOOLCHAIN}ar")
set(CMAKE_RANLIB "${TOOLCHAIN}ranlib")
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/flash.ld")

set(CMAKE_C_FLAGS "                            \
-mcpu=cortex-m4                                \
-mthumb                                        \
-g3                                            \
-O2                                            \
-Wall                                          \
-nostartfiles                                  \
-mfloat-abi=hard                               \
-mfpu=vfpv4-d16                                \
-ffunction-sections                            \
-fdata-sections                                \
")

set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS}")

set(CMAKE_EXE_LINKER_FLAGS "                   \
-ffunction-sections                            \
-fdata-sections                                \
-Wl,--gc-sections                              \
-T${LINKER_SCRIPT}                             \
-Wl,-Map=${PROJ_NAME}.map                      \
--specs=nano.specs                             \
-Wl,--print-memory-usage                       \
-Wl,--print-output-format                      \
")

add_definitions(-DSTM32F40_41xxx)
add_definitions(-DUSE_STDPERIPH_DRIVER)

enable_language(ASM)
add_executable(${PROJ_NAME}.elf main.c)
target_sources(${PROJ_NAME}.elf PUBLIC ${CMAKE_CURRENT_LIST_DIR}/start.S)

add_subdirectory(../driver driver)
target_link_libraries(${PROJ_NAME}.elf driver)
target_include_directories(${PROJ_NAME}.elf PUBLIC ../driver/stddriver/inc)
target_include_directories(${PROJ_NAME}.elf PUBLIC ../driver/cmsis/inc)

add_subdirectory(src src)
target_link_libraries(${PROJ_NAME}.elf src)
target_include_directories(${PROJ_NAME}.elf PUBLIC src)

target_link_libraries(${PROJ_NAME}.elf m) # math library

add_custom_command(TARGET ${PROJ_NAME}.elf POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJ_NAME}.elf> ${BIN_FILE}
    COMMAND ${CMAKE_OBJDUMP} -d -S $<TARGET_FILE:${PROJ_NAME}.elf> >${ASM_FILE}
    # COMMENT "Generate ${BIN_FILE}\r\n"
    COMMAND size ${PROJ_NAME}.elf
    COMMAND echo "Calculating SHA-256 checksum for ${BIN_FILE}:"
    COMMAND sha256sum ${BIN_FILE}
)

# get git tag
execute_process(
    COMMAND git describe --abbrev=40 --tags --dirty --always
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_TAG
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
)
if(GIT_TAG)
    set(PROJECT_SDK_VERSION ${GIT_TAG})
else()
    message(WARNING "No Valid version info found for SDK!")
    set(PROJECT_SDK_VERSION "version-unknown-panic")
endif()
message(STATUS "Project SDK Version: ${PROJECT_SDK_VERSION}")
