cmake_minimum_required(VERSION 3.20) # 项目设置 project(armfly) 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=hard ) # C 源文件 file(GLOB_RECURSE C_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/Project/main.c ${CMAKE_CURRENT_SOURCE_DIR}/Project/stm32f4xx_it.c ${CMAKE_CURRENT_SOURCE_DIR}/Project/system_stm32f4xx.c ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Project/GCC/startup_stm32f40xx.s ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4x7_ETH_Driver/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_core.c ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd_int.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/src/ff.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/src/option/cc936.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/api/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/apps/tftp/*.c # ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/apps/httpd/fs.c # ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/apps/httpd/fsdata.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/ipv4/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/snmp/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/netif/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/netif/ppp/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/cpu_core.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU/*.s ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_ascii.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_math.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_mem.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_str.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/Ports/ARM-Cortex-M4/GNU/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/Ports/ARM-Cortex-M4/GNU/*.s ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/UCOS-BSP/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Source/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.s ${CMAKE_CURRENT_SOURCE_DIR}/User/*.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 ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/usbh_msc_fatfs.c ) list(REMOVE_ITEM C_SRCS ${R_SRCS}) # C .h文件 set(INC_C_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/Project ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/fatfs ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip/port ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip/port/arch ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/ucos ${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}/Libraries/STM32F4x7_ETH_Driver/inc ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/inc ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/inc ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/inc ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-BSP ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Source ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include # ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/apps/httpd ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/lwip ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/netif ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/posix ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/posix/sys ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/src ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/cJSON ${CMAKE_CURRENT_SOURCE_DIR}/User ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/ad7606 ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/am2303 ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/can ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dm9k ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/eth ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/ext_sram ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dwt ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/interface ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/iwdg ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/lcd ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/nand_flash ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/nor_flash ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/sdio ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/spi ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/timer ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/uart ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/usbh ${CMAKE_CURRENT_SOURCE_DIR}/User/app ${CMAKE_CURRENT_SOURCE_DIR}/User/app/avltree ${CMAKE_CURRENT_SOURCE_DIR}/User/app/ftp ${CMAKE_CURRENT_SOURCE_DIR}/User/app/iec104 ${CMAKE_CURRENT_SOURCE_DIR}/User/app/key ${CMAKE_CURRENT_SOURCE_DIR}/User/app/queue ${CMAKE_CURRENT_SOURCE_DIR}/User/app/tftp ${CMAKE_CURRENT_SOURCE_DIR}/User/app/memory ${CMAKE_CURRENT_SOURCE_DIR}/User/app/modbus ${CMAKE_CURRENT_SOURCE_DIR}/User/app/net ) set(INC_CXX_DIRS) set(INC_ASM_DIRS) # 链接定义 set(C_SYMB "DEBUG" "STM32F40_41xxx" "USE_STDPERIPH_DRIVER" "USE_USB_OTG_HS" "USE_EMBEDDED_PHY" ) 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 )