CMakeLists.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. cmake_minimum_required(VERSION 3.20)
  2. # 项目设置
  3. project(armfly)
  4. enable_language(C CXX ASM)
  5. message("Build type: " ${CMAKE_BUILD_TYPE})
  6. # 编译脚本设置
  7. set(CMAKE_C_STANDARD 11)
  8. set(CMAKE_C_STANDARD_REQUIRED ON)
  9. set(CMAKE_C_EXTENSIONS ON)
  10. set(CMAKE_CXX_STANDARD 20)
  11. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  12. set(CMAKE_CXX_EXTENSIONS ON)
  13. #添加的部分,关闭警告
  14. add_definitions(-w)
  15. # MCU 参数设置
  16. set(MCU_PARAMS
  17. -mthumb
  18. -mcpu=cortex-m4
  19. # -mfpu=fpv5-d16
  20. # -mfloat-abi=hard
  21. )
  22. # C 源文件
  23. file(GLOB_RECURSE C_SRCS
  24. ${CMAKE_CURRENT_SOURCE_DIR}/Project/main.c
  25. ${CMAKE_CURRENT_SOURCE_DIR}/Project/stm32f4xx_it.c
  26. ${CMAKE_CURRENT_SOURCE_DIR}/Project/system_stm32f4xx.c
  27. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/*.c
  28. ${CMAKE_CURRENT_SOURCE_DIR}/Project/GCC/startup_stm32f40xx.s
  29. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/*.c
  30. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4x7_ETH_Driver/src/*.c
  31. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/*.c
  32. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/src/*.c
  33. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_core.c
  34. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c
  35. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd_int.c
  36. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/ff.c
  37. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/option/cc936.c
  38. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/api/*.c
  39. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/*.c
  40. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/ipv4/*.c
  41. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/snmp/*.c
  42. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/netif/*.c
  43. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/netif/ppp/*.c
  44. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/cpu_core.c
  45. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU/*.c
  46. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU/*.s
  47. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_ascii.c
  48. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_math.c
  49. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_mem.c
  50. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_str.c
  51. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/Ports/ARM-Cortex-M4/GNU/*.c
  52. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/Ports/ARM-Cortex-M4/GNU/*.s
  53. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/UCOS-BSP/*.c
  54. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Source/*.c
  55. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.c
  56. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.s
  57. ${CMAKE_CURRENT_SOURCE_DIR}/User/*.c
  58. )
  59. file(GLOB_RECURSE R_SRCS
  60. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
  61. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
  62. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
  63. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
  64. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
  65. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/usbh_msc_fatfs.c
  66. )
  67. list(REMOVE_ITEM C_SRCS ${R_SRCS})
  68. # C .h文件
  69. set(INC_C_DIRS
  70. ${CMAKE_CURRENT_SOURCE_DIR}/Project
  71. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/fatfs
  72. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip
  73. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip/port
  74. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip/port/arch
  75. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/ucos
  76. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/CMSIS/Include
  77. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/CMSIS/Device/ST/STM32F4xx/Include
  78. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/inc
  79. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4x7_ETH_Driver/inc
  80. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/inc
  81. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/inc
  82. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/inc
  83. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU
  84. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU
  85. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB
  86. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-BSP
  87. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Source
  88. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU
  89. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include
  90. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/lwip
  91. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/netif
  92. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/posix
  93. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/posix/sys
  94. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/src
  95. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/cJSON
  96. ${CMAKE_CURRENT_SOURCE_DIR}/User
  97. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp
  98. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/ad7606
  99. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/am2303
  100. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/can
  101. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dm9k
  102. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/eth
  103. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/ext_sram
  104. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dwt
  105. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/interface
  106. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/iwdg
  107. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/lcd
  108. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/nand_flash
  109. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/nor_flash
  110. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/sdio
  111. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/spi
  112. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/timer
  113. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/uart
  114. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/usbh
  115. ${CMAKE_CURRENT_SOURCE_DIR}/User/app
  116. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/iec104
  117. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/key
  118. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/queue
  119. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/memory
  120. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/modbus
  121. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/net
  122. )
  123. set(INC_CXX_DIRS)
  124. set(INC_ASM_DIRS)
  125. # 链接定义
  126. set(C_SYMB
  127. "DEBUG"
  128. "STM32F40_41xxx"
  129. "USE_STDPERIPH_DRIVER"
  130. "USE_USB_OTG_HS"
  131. "USE_EMBEDDED_PHY"
  132. )
  133. set(CXX_SYMB)
  134. set(ASM_SYMB)
  135. # 链接静态库或者文件夹
  136. set(LINK_DIRS)
  137. set(LINK_LIBS)
  138. # 链接脚本
  139. set(LINK_SRC
  140. ${CMAKE_CURRENT_SOURCE_DIR}/Project/GCC/STM32F417IG_FLASH.ld
  141. )
  142. # 编译器参数
  143. set(compiler_OPTS)
  144. # 链接脚本参数
  145. set(LINK_OPTS)
  146. link_directories(${CMAKE_PROJECT_NAME} ${LINK_DIRS})
  147. add_executable(${CMAKE_PROJECT_NAME})
  148. target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${C_SRCS})
  149. # 添加 include 路径
  150. target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
  151. $<$<COMPILE_LANGUAGE:C>: ${INC_C_DIRS}>
  152. $<$<COMPILE_LANGUAGE:CXX>: ${INC_CXX_DIRS}>
  153. $<$<COMPILE_LANGUAGE:ASM>: ${INC_ASM_DIRS}>
  154. )
  155. # 添加 链接 文件
  156. target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
  157. $<$<COMPILE_LANGUAGE:C>: ${C_SYMB}>
  158. $<$<COMPILE_LANGUAGE:CXX>: ${CXX_SYMB}>
  159. $<$<COMPILE_LANGUAGE:ASM>: ${ASM_SYMB}>
  160. $<$<CONFIG:Debug>:DEBUG>
  161. $<$<CONFIG:Release>: >
  162. )
  163. target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIBS})
  164. # 编译器参数
  165. target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
  166. ${MCU_PARAMS}
  167. ${compiler_OPTS}
  168. -Wall
  169. -Wextra
  170. -Wpedantic
  171. $<$<COMPILE_LANGUAGE:C>: >
  172. $<$<COMPILE_LANGUAGE:CXX>:
  173. # 去除编译警告
  174. -Wno-unused-variable
  175. -Wno-pointer-sign
  176. -Wno-unused-but-set-variable
  177. -Wno-unused-function
  178. -Wno-unused-parameter
  179. -Wno-format=
  180. # -Wno-volatile
  181. # -Wold-style-cast
  182. # -Wuseless-cast
  183. # -Wsuggest-override
  184. >
  185. $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP>
  186. $<$<CONFIG:Debug>:-Og -g3 -ggdb>
  187. $<$<CONFIG:Release>:-Og -g0>
  188. )
  189. # 链接参数
  190. target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
  191. -T${LINK_SRC}
  192. ${MCU_PARAMS}
  193. ${LINK_OPTS}
  194. -Wl,-Map=${CMAKE_PROJECT_NAME}.map
  195. # -u _printf_float
  196. --specs=nosys.specs
  197. -Wl,--start-group
  198. -lc
  199. -lm
  200. -lstdc++
  201. -lsupc++
  202. -Wl,--end-group
  203. # Allow good software remapping across address space (with proper GCC section making)
  204. -Wl,-z,max-page-size=8
  205. -Wl,--print-memory-usage
  206. )
  207. # post-build
  208. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  209. COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
  210. )
  211. # 生成 hex 文件
  212. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  213. COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
  214. )
  215. # 生成 bin 文件
  216. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  217. COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
  218. )