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 
	STM32F407xx
	$<$<CONFIG:Debug>:DEBUG>
)

# STM32CubeMX generated include paths
set(STM32_HAL_Include_Dirs
	${CMAKE_CURRENT_SOURCE_DIR}/../../Include/HAL
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Inc
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/CMSIS/Device/ST/STM32F4xx/Include
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/CMSIS/Include
)

set(FREERTOS_Include_Dirs
	${CMAKE_CURRENT_SOURCE_DIR}/../../Include/RTOSMacros
	${CMAKE_CURRENT_SOURCE_DIR}/../../Include/FreeRTOS
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/include
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F
)

#set(MX_Init_Src
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/gpio.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/freertos.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/adc.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/dma.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/i2c.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/tim.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/usart.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f4xx_it.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f4xx_hal_msp.c
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f4xx_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_stm32f407xx.s
#)

#set(MX_Main_Src
#	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/main.c
#)

set(STM32_HAL_Src
	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/system_stm32f4xx.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f4xx_hal_msp.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f4xx_hal_timebase_tim.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
)

set(FREERTOS_Src
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/croutine.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/event_groups.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/list.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/queue.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/stream_buffer.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/tasks.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/timers.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/cmsis_os2.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_4.c
	${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/port.c
)

# Project static libraries
#set(MX_LINK_LIBS 
#	STM32_HAL_Drivers
#	${TOOLCHAIN_LINK_LIBRARIES}
#	FreeRTOS
#)

add_library(STM32HAL OBJECT)
target_sources(STM32HAL PRIVATE ${STM32_HAL_Src})
target_include_directories(STM32HAL PRIVATE ${STM32_HAL_Include_Dirs} ${FREERTOS_Include_Dirs})
target_compile_definitions(STM32HAL PRIVATE ${MX_Defines_Syms})

add_library(FREERTOS OBJECT)
target_sources(FREERTOS PRIVATE ${FREERTOS_Src})
target_include_directories(FREERTOS PRIVATE ${STM32_HAL_Include_Dirs} ${FREERTOS_Include_Dirs})
target_compile_definitions(FREERTOS PRIVATE ${MX_Defines_Syms})

add_library(HAL_H INTERFACE)
target_include_directories(HAL_H INTERFACE ${STM32_HAL_Include_Dirs})
target_compile_definitions(HAL_H INTERFACE ${MX_Defines_Syms})

add_library(FREERTOS_H INTERFACE)
target_include_directories(FREERTOS_H INTERFACE ${FREERTOS_Include_Dirs})

# Interface library for includes and symbols
#target_include_directories(STM32HAL INTERFACE
#	${MX_Include_Dirs}
#)
#target_compile_definitions(stm32cubemx INTERFACE ${MX_Defines_Syms})

# 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 FreeRTOS static library
#add_library(FreeRTOS OBJECT)
#target_sources(FreeRTOS PRIVATE ${FreeRTOS_Src})
#target_link_libraries(FreeRTOS PUBLIC stm32cubemx)


#target_sources(${CMAKE_PROJECT_NAME} PRIVATE ${MX_Init_Src})


# Add libraries to the project
#target_link_libraries(${CMAKE_PROJECT_NAME} ${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()
