CMakeLists.txt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. # MCU 参数设置
  14. set(MCU_PARAMS
  15. -mthumb
  16. -mcpu=cortex-m4
  17. # -mfpu=fpv5-d16
  18. # -mfloat-abi=hard
  19. )
  20. # C 源文件
  21. file(GLOB_RECURSE C_SRCS
  22. ${CMAKE_CURRENT_SOURCE_DIR}/Project/main.c
  23. ${CMAKE_CURRENT_SOURCE_DIR}/Project/stm32f4xx_it.c
  24. ${CMAKE_CURRENT_SOURCE_DIR}/Project/system_stm32f4xx.c
  25. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/*.c
  26. ${CMAKE_CURRENT_SOURCE_DIR}/Project/GCC/startup_stm32f40xx.s
  27. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/*.c
  28. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4x7_ETH_Driver/src/*.c
  29. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/*.c
  30. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/src/*.c
  31. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_core.c
  32. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c
  33. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/src/usb_hcd_int.c
  34. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/ff.c
  35. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/option/cc936.c
  36. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/api/*.c
  37. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/*.c
  38. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/ipv4/*.c
  39. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/core/snmp/*.c
  40. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/netif/*.c
  41. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/netif/ppp/*.c
  42. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/cpu_core.c
  43. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU/*.c
  44. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU/*.s
  45. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_ascii.c
  46. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_math.c
  47. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_mem.c
  48. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/lib_str.c
  49. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/Ports/ARM-Cortex-M4/GNU/*.c
  50. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB/Ports/ARM-Cortex-M4/GNU/*.s
  51. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/UCOS-BSP/*.c
  52. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Source/*.c
  53. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.c
  54. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU/*.s
  55. ${CMAKE_CURRENT_SOURCE_DIR}/User/*.c
  56. )
  57. file(GLOB_RECURSE R_SRCS
  58. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
  59. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
  60. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
  61. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
  62. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
  63. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/src/usbh_msc_fatfs.c
  64. )
  65. list(REMOVE_ITEM C_SRCS ${R_SRCS})
  66. # C .h文件
  67. set(INC_C_DIRS
  68. ${CMAKE_CURRENT_SOURCE_DIR}/Project
  69. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/fatfs
  70. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip
  71. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip/port
  72. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/lwip/port/arch
  73. ${CMAKE_CURRENT_SOURCE_DIR}/Project/conf/ucos
  74. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/CMSIS/Include
  75. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/CMSIS/Device/ST/STM32F4xx/Include
  76. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/inc
  77. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4x7_ETH_Driver/inc
  78. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Core/inc
  79. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_HOST_Library/Class/MSC/inc
  80. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32_USB_OTG_Driver/inc
  81. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU
  82. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-CPU/ARM-Cortex-M4/GNU
  83. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uC-LIB
  84. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-BSP
  85. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Source
  86. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/GNU
  87. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include
  88. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/lwip
  89. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/netif
  90. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/posix
  91. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/lwip-2.0.2/src/include/posix/sys
  92. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/FATFS/src
  93. ${CMAKE_CURRENT_SOURCE_DIR}/Third_Party/cJSON
  94. ${CMAKE_CURRENT_SOURCE_DIR}/User
  95. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp
  96. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/ad7606
  97. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/am2303
  98. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/can
  99. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dm9k
  100. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/eth
  101. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/ext_sram
  102. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dwt
  103. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/esp8266
  104. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/interface
  105. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/iwdg
  106. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/lcd
  107. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/nand_flash
  108. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/nor_flash
  109. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/sdio
  110. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/spi
  111. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/timer
  112. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/uart
  113. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/usbh
  114. ${CMAKE_CURRENT_SOURCE_DIR}/User/app
  115. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/esp8266
  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-volatile
  180. # -Wold-style-cast
  181. # -Wuseless-cast
  182. # -Wsuggest-override
  183. >
  184. $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP>
  185. $<$<CONFIG:Debug>:-Og -g3 -ggdb>
  186. $<$<CONFIG:Release>:-Og -g0>
  187. )
  188. # 链接参数
  189. target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
  190. -T${LINK_SRC}
  191. ${MCU_PARAMS}
  192. ${LINK_OPTS}
  193. -Wl,-Map=${CMAKE_PROJECT_NAME}.map
  194. # -u _printf_float
  195. --specs=nosys.specs
  196. -Wl,--start-group
  197. -lc
  198. -lm
  199. -lstdc++
  200. -lsupc++
  201. -Wl,--end-group
  202. # Allow good software remapping across address space (with proper GCC section making)
  203. -Wl,-z,max-page-size=8
  204. -Wl,--print-memory-usage
  205. )
  206. # post-build
  207. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  208. COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
  209. )
  210. # 生成 hex 文件
  211. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  212. COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
  213. )
  214. # 生成 bin 文件
  215. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  216. COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
  217. )