cmake_minimum_required(VERSION 3.22)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
# STM32CubeMX generated symbols (macros)
set(MX_Defines_Syms 
	USE_HAL_DRIVER 
	STM32F103xE
    $<$<CONFIG:Debug>:DEBUG>
)

# STM32CubeMX generated include paths
set(MX_Include_Dirs
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Inc
    ${CMAKE_CURRENT_SOURCE_DIR}/../../RT-Thread
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Inc
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/CMSIS/Device/ST/STM32F1xx/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/CMSIS/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/components/finsh/inc/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/include/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/include/libc/include/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/include/libc/extension/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/include/libc/extension/fcntl/msvc/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/include/libc/extension/fcntl/octal/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/ST/ARM/DSP/Inc
    ${CMAKE_SOURCE_DIR}/segger_rtt/RTT/
    ${CMAKE_SOURCE_DIR}/segger_rtt/Config # cubemx_config/board.c build before user code, then if not include, will cause error
)

# STM32CubeMX generated application sources
set(MX_Application_Src
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/main.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/gpio.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/adc.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/i2c.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/spi.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/tim.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/dma.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/usart.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f1xx_it.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f1xx_hal_msp.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f1xx_hal_timebase_tim.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/sysmem.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/syscalls.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../startup_stm32f103xe.s
)

# STM32 HAL/LL Drivers
set(STM32_Drivers_Src
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/system_stm32f1xx.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c
)

# Drivers Midllewares


set(RT-Thread_Src
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/clock.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/components.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/cpu.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/idle.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/ipc.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/irq.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/kservice.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/mem.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/memheap.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/mempool.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/object.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/scheduler.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/signal.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/slab.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/thread.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/src/timer.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/bsp/_template/cubemx_config/board.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/libcpu/arm/cortex-m3/context_gcc.S
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/libcpu/arm/cortex-m3/cpuport.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/components/finsh/src/cmd.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/components/finsh/src/msh.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/components/finsh/src/msh_file.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/components/finsh/src/msh_parse.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/RealThread_RTOS_RT-Thread/components/finsh/src/shell.c
)

# Link directories setup
set(MX_LINK_DIRS

    ${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/ST/ARM/DSP/Lib
)
# Project static libraries
set(MX_LINK_LIBS  
	:libarm_cortexM3l_math.a
    STM32_Drivers
    ${TOOLCHAIN_LINK_LIBRARIES}
    RT-Thread
	
)
# Interface library for includes and symbols
add_library(stm32cubemx INTERFACE)
target_include_directories(stm32cubemx INTERFACE ${MX_Include_Dirs})
target_compile_definitions(stm32cubemx INTERFACE ${MX_Defines_Syms})

# Propagate suppression of a few warnings (for GCC) to all consumers of stm32cubemx
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
    target_compile_options(stm32cubemx INTERFACE
        "-Wno-pedantic"
        "-Wno-cast-function-type"
        "-Wno-implicit-fallthrough"
    )
endif()

# Create STM32_Drivers static library
add_library(STM32_Drivers OBJECT)
target_sources(STM32_Drivers PRIVATE ${STM32_Drivers_Src})
target_link_libraries(STM32_Drivers PUBLIC stm32cubemx)


# Create RT-Thread static library
add_library(RT-Thread OBJECT)
target_sources(RT-Thread PRIVATE ${RT-Thread_Src})
target_link_libraries(RT-Thread PUBLIC stm32cubemx)

# For GCC builds, silence a few third-party warnings coming from RT-Thread
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
    target_compile_options(RT-Thread PRIVATE
        "-Wno-pedantic"
        "-Wno-cast-function-type"
        "-Wno-implicit-fallthrough"
    )
endif()

# Add STM32CubeMX generated application sources to the project
target_sources(${CMAKE_PROJECT_NAME} PRIVATE ${MX_Application_Src})

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE ${MX_LINK_DIRS})

# Add libraries to the project
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE ${MX_LINK_LIBS})

# Add the map file to the list of files to be removed with 'clean' target
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map)

# Validate that STM32CubeMX code is compatible with C standard
if((CMAKE_C_STANDARD EQUAL 90) OR (CMAKE_C_STANDARD EQUAL 99))
    message(ERROR "Generated code requires C11 or higher")
endif()