cmake_minimum_required(VERSION 3.22)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gcc-arm-none-eabi.cmake)
project(blinky)
enable_language(C ASM)

set(CUBE_def
    USE_HAL_DRIVER 
	STM32F030x8
    $<$<CONFIG:Debug>:DEBUG>
)

set(CUBE_src
    ${CMAKE_CURRENT_SOURCE_DIR}/Src/main.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Src/stm32f0xx_it.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Src/stm32f0xx_hal_msp.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Src/sysmem.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Src/syscalls.c
    ${CMAKE_CURRENT_SOURCE_DIR}/startup_stm32f030x8.s
    ${CMAKE_CURRENT_SOURCE_DIR}/Src/system_stm32f0xx.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_gpio.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_gpio.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_dma.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_cortex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pwr.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_exti.c
)

set(CUBE_inc
    ${CMAKE_CURRENT_SOURCE_DIR}/Inc
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Inc/Legacy
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/STM32F0xx_HAL_Driver/Inc
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/CMSIS/Device/ST/STM32F0xx/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/STM32CubeF0/Drivers/CMSIS/Include
)

add_executable(blinky
    ${CUBE_src}
    ${CMAKE_CURRENT_SOURCE_DIR}/../src/app_main.c
)
target_include_directories(blinky PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/../src
    ${CUBE_inc}
)
target_compile_definitions(blinky PUBLIC
    ${CUBE_def}
)