cmake_minimum_required(VERSION 3.22)

if(NOT CMAKE_TOOLCHAIN_FILE)
  set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/CMake/gcc-arm-none-eabi.cmake)
endif()

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE release CACHE STRING "Build Type" FORCE)
endif()

project(stm32h743_player C CXX ASM)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# 全局包含路径
include_directories(
  "${CMAKE_CURRENT_LIST_DIR}/Core/Inc"
  "${CMAKE_CURRENT_LIST_DIR}/Drivers/CMSIS/Include"
  "${CMAKE_CURRENT_LIST_DIR}/Drivers/CMSIS/Device/ST/STM32H7xx/Include"
  "${CMAKE_CURRENT_LIST_DIR}/User/utils"
  "${CMAKE_CURRENT_LIST_DIR}/User/ports/lvgl"
  "${CMAKE_CURRENT_LIST_DIR}/User/ports/fatfs"
)

# 全局定义
add_compile_definitions(
  USE_HAL_DRIVER
  STM32H743xx
  STM32_THREAD_SAFE_STRATEGY=4
)

# 全局编译器选项
add_compile_options(
  -fdiagnostics-color=always
)

# 全局连接器选项
add_link_options(
  -Wl,-wrap,_malloc_r -Wl,-wrap,_free_r -Wl,-wrap,_realloc_r #将newlib所提供的内存管理函数替换为用户自定义的
  -T${CMAKE_CURRENT_LIST_DIR}/STM32H743IITX_FLASH.ld
)

add_subdirectory(Drivers)
add_subdirectory(Middlewares)

set(TARGET_SOURCE_DIRS
  "${CMAKE_SOURCE_DIR}/Core/Src/*.c"
  "${CMAKE_SOURCE_DIR}/Core/Startup/*.s"
  "${CMAKE_SOURCE_DIR}/User/*.c"
)
file(GLOB_RECURSE TARGET_SOURCES ${TARGET_SOURCE_DIRS})

add_executable(${CMAKE_PROJECT_NAME} ${TARGET_SOURCES})

target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
  "${CMAKE_SOURCE_DIR}/Core/ThreadSafe"
  "${CMAKE_CURRENT_LIST_DIR}/User/audio"
  "${CMAKE_CURRENT_LIST_DIR}/User/bsp"
  "${CMAKE_CURRENT_LIST_DIR}/User/ports/fatfs"
  "${CMAKE_CURRENT_LIST_DIR}/User/ports/lvgl"
  "${CMAKE_CURRENT_LIST_DIR}/User/tasks"
)

target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
  drivers
  freertos
  easylogger
  fatfs
  dr_libs
#  lvgl
#  lvgl_demos
  tlsf
)
