cmake_minimum_required(VERSION 3.22)

#
# This file is generated only once,
# and is not re-generated if converter is called multiple times.
#
# User is free to modify the file as much as necessary
#

# Setup compiler settings
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 the project name
set(CMAKE_PROJECT_NAME gebrabit_adv04_dsp)

# Include toolchain file
include("cmake/gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Enable CMake support for ASM and C languages
enable_language(C ASM)

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})

# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined library search paths
)

# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user sources here
    Core/Src/dbg_trace.c
    Core/Src/dsp_app.c
)

# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined include paths
)

# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined symbols
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
    stm32cubemx

    # Add user defined libraries
)

# ---- CMSIS-DSP v1.16.2 (Apache-2.0): only the files this project calls ----
set(CMSISDSP_DIR ${CMAKE_SOURCE_DIR}/Middlewares/CMSIS-DSP)
add_library(cmsisdsp STATIC
    ${CMSISDSP_DIR}/Source/FilteringFunctions/arm_fir_f32.c
    ${CMSISDSP_DIR}/Source/FilteringFunctions/arm_fir_init_f32.c
    ${CMSISDSP_DIR}/Source/TransformFunctions/arm_rfft_fast_f32.c
    ${CMSISDSP_DIR}/Source/TransformFunctions/arm_rfft_fast_init_f32.c
    ${CMSISDSP_DIR}/Source/TransformFunctions/arm_cfft_f32.c
    ${CMSISDSP_DIR}/Source/TransformFunctions/arm_cfft_init_f32.c
    ${CMSISDSP_DIR}/Source/TransformFunctions/arm_cfft_radix8_f32.c
    ${CMSISDSP_DIR}/Source/TransformFunctions/arm_bitreversal2.c
    ${CMSISDSP_DIR}/Source/CommonTables/arm_common_tables.c
    ${CMSISDSP_DIR}/Source/CommonTables/arm_const_structs.c
    ${CMSISDSP_DIR}/Source/ComplexMathFunctions/arm_cmplx_mag_f32.c
    ${CMSISDSP_DIR}/Source/StatisticsFunctions/arm_max_f32.c
    ${CMSISDSP_DIR}/Source/SupportFunctions/arm_copy_f32.c
    ${CMSISDSP_DIR}/Source/SupportFunctions/arm_fill_f32.c
)
# SYSTEM: warnings inside the library headers are not reported in our files.
target_include_directories(cmsisdsp
    SYSTEM PUBLIC ${CMSISDSP_DIR}/Include ${CMAKE_SOURCE_DIR}/Drivers/CMSIS/Include
    PRIVATE ${CMSISDSP_DIR}/PrivateInclude)
target_compile_definitions(cmsisdsp PUBLIC DISABLEFLOAT16 PRIVATE ARM_MATH_LOOPUNROLL)
# The library is always optimised, also in Debug (the last -O option wins).
target_compile_options(cmsisdsp PRIVATE -O2)
target_link_libraries(${CMAKE_PROJECT_NAME} cmsisdsp)
