CMakeLists.txt 10 KB

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