cmake_minimum_required(VERSION 3.20) # 项目设置 project(alientek_bootloader) enable_language(C CXX ASM) message("Build type: " ${CMAKE_BUILD_TYPE}) # 编译脚本设置 set(CMAKE_C_STANDARD 11) 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}/applications/*.c ${CMAKE_CURRENT_SOURCE_DIR}/project/gcc/startup_stm32f40xx.s ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/board/*.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}/applications ${CMAKE_CURRENT_SOURCE_DIR}/applications/bootloader ${CMAKE_CURRENT_SOURCE_DIR}/applications/global ${CMAKE_CURRENT_SOURCE_DIR}/applications/iap ${CMAKE_CURRENT_SOURCE_DIR}/applications/queue ${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}/board ${CMAKE_CURRENT_SOURCE_DIR}/board/driver ${CMAKE_CURRENT_SOURCE_DIR}/board/driver/gpio ${CMAKE_CURRENT_SOURCE_DIR}/board/can ${CMAKE_CURRENT_SOURCE_DIR}/board/dwt ) 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 $<$: ${INC_C_DIRS}> $<$: ${INC_CXX_DIRS}> $<$: ${INC_ASM_DIRS}> ) # 添加 链接 文件 target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE $<$: ${C_SYMB}> $<$: ${CXX_SYMB}> $<$: ${ASM_SYMB}> $<$:DEBUG> $<$: > ) target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIBS}) # 编译器参数 target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE ${MCU_PARAMS} ${compiler_OPTS} -Wall -Wextra -Wpedantic $<$: > $<$: # 去除编译警告 -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 > $<$:-x assembler-with-cpp -MMD -MP> $<$:-Og -g3 -ggdb> $<$:-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} $ ) # 生成 hex 文件 add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O ihex $ ${CMAKE_PROJECT_NAME}.hex ) # 生成 bin 文件 add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O binary $ ${CMAKE_PROJECT_NAME}.bin )