cmake_minimum_required(VERSION 3.20)


# 项目设置
project(alientek)
enable_language(C CXX ASM)
message("Build type: " ${CMAKE_BUILD_TYPE})

# 编译脚本设置
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

#添加的部分，关闭警告
add_definitions(-w)

# MCU 参数设置
set(MCU_PARAMS
        -mthumb
        -mcpu=cortex-m4
#        -mfpu=fpv5-d16
        # -mfloat-abi=soft
        )

# C 源文件
file(GLOB_RECURSE C_SRCS
        ${CMAKE_CURRENT_SOURCE_DIR}/user/*.c
        ${CMAKE_CURRENT_SOURCE_DIR}/cmsis/*.c
        ${CMAKE_CURRENT_SOURCE_DIR}/startup/gcc/startup_stm32f40_41xxx.s
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/*.c
        ${CMAKE_CURRENT_SOURCE_DIR}/packages/multi_button/multi_button.c
        ${CMAKE_CURRENT_SOURCE_DIR}/app/*.c
        ${CMAKE_CURRENT_SOURCE_DIR}/cli/*.c
        ${CMAKE_CURRENT_SOURCE_DIR}/dev/*.c
        ${CMAKE_CURRENT_SOURCE_DIR}/hal/*.c
        )
file(GLOB_RECURSE R_SRCS
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
        )

list(REMOVE_ITEM C_SRCS ${R_SRCS})

# C .h文件
set(INC_C_DIRS
        ${CMAKE_CURRENT_SOURCE_DIR}/user
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/CMSIS/Include
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/CMSIS/Device/ST/STM32F4xx/Include
        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/inc
        ${CMAKE_CURRENT_SOURCE_DIR}/packages/multi_button
        ${CMAKE_CURRENT_SOURCE_DIR}/app
        ${CMAKE_CURRENT_SOURCE_DIR}/cli
        ${CMAKE_CURRENT_SOURCE_DIR}/dev
        ${CMAKE_CURRENT_SOURCE_DIR}/hal
        )

set(INC_CXX_DIRS)
set(INC_ASM_DIRS)

# 链接定义
set(C_SYMB
        "DEBUG"
        "STM32F40_41xxx"
        "USE_STDPERIPH_DRIVER"
        )
set(CXX_SYMB)
set(ASM_SYMB)

# 链接静态库或者文件夹
set(LINK_DIRS)
set(LINK_LIBS)

# 链接脚本
set(LINK_SRC
        ${CMAKE_CURRENT_SOURCE_DIR}/project/gcc/STM32F417IG_FLASH.ld
        )

# 编译器参数
set(compiler_OPTS)

# 链接脚本参数
set(LINK_OPTS)


link_directories(${CMAKE_PROJECT_NAME} ${LINK_DIRS})
add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${C_SRCS})

# 添加 include 路径
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        $<$<COMPILE_LANGUAGE:C>: ${INC_C_DIRS}>
        $<$<COMPILE_LANGUAGE:CXX>: ${INC_CXX_DIRS}>
        $<$<COMPILE_LANGUAGE:ASM>: ${INC_ASM_DIRS}>
        )

# 添加 链接 文件
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
        $<$<COMPILE_LANGUAGE:C>: ${C_SYMB}>
        $<$<COMPILE_LANGUAGE:CXX>: ${CXX_SYMB}>
        $<$<COMPILE_LANGUAGE:ASM>: ${ASM_SYMB}>

        $<$<CONFIG:Debug>:DEBUG>
        $<$<CONFIG:Release>: >
        )

target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIBS})

# 编译器参数
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
        ${MCU_PARAMS}
        ${compiler_OPTS}
        -Wall
        -Wextra
        -Wpedantic
        $<$<COMPILE_LANGUAGE:C>: >
        $<$<COMPILE_LANGUAGE:CXX>:

        # 去除编译警告
        -Wno-unused-variable
        -Wno-pointer-sign
        -Wno-unused-but-set-variable
        -Wno-unused-function
        -Wno-unused-parameter
        -Wno-format=
        # -Wno-volatile
        # -Wold-style-cast
        # -Wuseless-cast
        # -Wsuggest-override
        >
        $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP>
        $<$<CONFIG:Debug>:-Og -g3 -ggdb>
        $<$<CONFIG:Release>:-Og -g0>
        )

# 链接参数
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
        -T${LINK_SRC}
        ${MCU_PARAMS}
        ${LINK_OPTS}
        -Wl,-Map=${CMAKE_PROJECT_NAME}.map
        # -u _printf_float
        --specs=nosys.specs
        -Wl,--start-group
        -lc
        -lm
        -lstdc++
        -lsupc++
        -Wl,--end-group
        # Allow good software remapping across address space (with proper GCC section making)
        -Wl,-z,max-page-size=8
        -Wl,--print-memory-usage
        )

# post-build
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
        )

# 生成 hex 文件
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
        )

# 生成 bin 文件
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
        )
