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(TARGET_FLAGS
    -mthumb
    -mcpu=cortex-m4
    -mfpu=fpv4-sp-d16
    -mfloat-abi=hard
    )

# C 源文件
file(GLOB_RECURSE C_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/applications/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/common/src/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/can/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/can/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/task/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/project/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/project/startup/gcc/startup_stm32f40_41xxx.s
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button/multi_button.c
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source/*.c
    )
file(GLOB_RECURSE R_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
    )

list(REMOVE_ITEM C_SRCS ${R_SRCS})

# C .h文件
set(INC_C_DIRS
    ${CMAKE_CURRENT_SOURCE_DIR}/applications
    ${CMAKE_CURRENT_SOURCE_DIR}/applications/task
    ${CMAKE_CURRENT_SOURCE_DIR}/common/inc
    ${CMAKE_CURRENT_SOURCE_DIR}/dev
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/can
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick
    ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/can
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/task
    ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart
    ${CMAKE_CURRENT_SOURCE_DIR}/project
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Device/ST/STM32F4xx/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/inc
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button
    ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source
    )

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/STM32F417IG_FLASH.ld
    )

# 编译器参数
set(COMPILER_OPTS)

# 链接脚本参数
set(LINK_OPTS
    --specs=nosys.specs
    )

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
    ${TARGET_FLAGS}
    ${COMPILER_OPTS}
    $<$<COMPILE_LANGUAGE:C>: >
    $<$<COMPILE_LANGUAGE:CXX>: >
    $<$<COMPILE_LANGUAGE:ASM>: >
    $<$<CONFIG:Debug>:-Og -g3 -ggdb>
    $<$<CONFIG:Release>:-Og -g0>
    )

# 链接参数
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
    ${TARGET_FLAGS}
    -T${LINK_SRC}
    ${LINK_OPTS}
    -Wl,-Map=${CMAKE_PROJECT_NAME}.map
    -Wl,--gc-sections
    -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)
