CMakeLists.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. cmake_minimum_required(VERSION 3.20)
  2. # 项目设置
  3. project(alientek)
  4. enable_language(C CXX ASM)
  5. message("Build type: " ${CMAKE_BUILD_TYPE})
  6. # 编译脚本设置
  7. set(CMAKE_C_STANDARD 99)
  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=soft
  21. )
  22. # C 源文件
  23. file(GLOB_RECURSE C_SRCS
  24. ${CMAKE_CURRENT_SOURCE_DIR}/applications/*.c
  25. ${CMAKE_CURRENT_SOURCE_DIR}/common/src/*.c
  26. ${CMAKE_CURRENT_SOURCE_DIR}/dev/*.c
  27. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/can/*.c
  28. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash/*.c
  29. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio/*.c
  30. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c/*.c
  31. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm/*.c
  32. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick/*.c
  33. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart/*.c
  34. ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble/*.c
  35. ${CMAKE_CURRENT_SOURCE_DIR}/modules/can/*.c
  36. ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli/*.c
  37. ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom/*.c
  38. ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266/*.c
  39. ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio/*.c
  40. ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap/*.c
  41. ${CMAKE_CURRENT_SOURCE_DIR}/modules/task/*.c
  42. ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart/*.c
  43. ${CMAKE_CURRENT_SOURCE_DIR}/project/*.c
  44. ${CMAKE_CURRENT_SOURCE_DIR}/project/startup/gcc/startup_stm32f40_41xxx.s
  45. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/*.c
  46. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button/multi_button.c
  47. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source/*.c
  48. )
  49. file(GLOB_RECURSE R_SRCS
  50. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
  51. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
  52. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
  53. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
  54. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
  55. )
  56. list(REMOVE_ITEM C_SRCS ${R_SRCS})
  57. # C .h文件
  58. set(INC_C_DIRS
  59. ${CMAKE_CURRENT_SOURCE_DIR}/applications
  60. ${CMAKE_CURRENT_SOURCE_DIR}/applications/task
  61. ${CMAKE_CURRENT_SOURCE_DIR}/common/inc
  62. ${CMAKE_CURRENT_SOURCE_DIR}/dev
  63. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/can
  64. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash
  65. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio
  66. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c
  67. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm
  68. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick
  69. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart
  70. ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble
  71. ${CMAKE_CURRENT_SOURCE_DIR}/modules/can
  72. ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli
  73. ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom
  74. ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266
  75. ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio
  76. ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap
  77. ${CMAKE_CURRENT_SOURCE_DIR}/modules/task
  78. ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart
  79. ${CMAKE_CURRENT_SOURCE_DIR}/project
  80. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Include
  81. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Device/ST/STM32F4xx/Include
  82. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/inc
  83. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button
  84. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source
  85. )
  86. set(INC_CXX_DIRS)
  87. set(INC_ASM_DIRS)
  88. # 链接定义
  89. set(C_SYMB
  90. "DEBUG"
  91. "STM32F40_41xxx"
  92. "USE_STDPERIPH_DRIVER"
  93. )
  94. set(CXX_SYMB)
  95. set(ASM_SYMB)
  96. # 链接静态库或者文件夹
  97. set(LINK_DIRS)
  98. set(LINK_LIBS)
  99. # 链接脚本
  100. set(LINK_SRC
  101. ${CMAKE_CURRENT_SOURCE_DIR}/project/STM32F417IG_FLASH.ld
  102. )
  103. # 编译器参数
  104. set(compiler_OPTS)
  105. # 链接脚本参数
  106. set(LINK_OPTS)
  107. link_directories(${CMAKE_PROJECT_NAME} ${LINK_DIRS})
  108. add_executable(${CMAKE_PROJECT_NAME})
  109. target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${C_SRCS})
  110. # 添加 include 路径
  111. target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
  112. $<$<COMPILE_LANGUAGE:C>: ${INC_C_DIRS}>
  113. $<$<COMPILE_LANGUAGE:CXX>: ${INC_CXX_DIRS}>
  114. $<$<COMPILE_LANGUAGE:ASM>: ${INC_ASM_DIRS}>
  115. )
  116. # 添加 链接 文件
  117. target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
  118. $<$<COMPILE_LANGUAGE:C>: ${C_SYMB}>
  119. $<$<COMPILE_LANGUAGE:CXX>: ${CXX_SYMB}>
  120. $<$<COMPILE_LANGUAGE:ASM>: ${ASM_SYMB}>
  121. $<$<CONFIG:Debug>:DEBUG>
  122. $<$<CONFIG:Release>: >
  123. )
  124. target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIBS})
  125. # 编译器参数
  126. target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
  127. ${MCU_PARAMS}
  128. ${compiler_OPTS}
  129. -Wall
  130. -Wextra
  131. -Wpedantic
  132. $<$<COMPILE_LANGUAGE:C>: >
  133. $<$<COMPILE_LANGUAGE:CXX>:
  134. # 去除编译警告
  135. -Wno-unused-variable
  136. -Wno-pointer-sign
  137. -Wno-unused-but-set-variable
  138. -Wno-unused-function
  139. -Wno-unused-parameter
  140. -Wno-format=
  141. # -Wno-volatile
  142. # -Wold-style-cast
  143. # -Wuseless-cast
  144. # -Wsuggest-override
  145. >
  146. $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP>
  147. $<$<CONFIG:Debug>:-Og -g3 -ggdb>
  148. $<$<CONFIG:Release>:-Og -g0>
  149. )
  150. # 链接参数
  151. target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
  152. -T${LINK_SRC}
  153. ${MCU_PARAMS}
  154. ${LINK_OPTS}
  155. -Wl,-Map=${CMAKE_PROJECT_NAME}.map
  156. # -u _printf_float
  157. --specs=nosys.specs
  158. -Wl,--start-group
  159. -lc
  160. -lm
  161. -lstdc++
  162. -lsupc++
  163. -Wl,--end-group
  164. # Allow good software remapping across address space (with proper GCC section making)
  165. -Wl,-z,max-page-size=8
  166. -Wl,--print-memory-usage
  167. )
  168. # post-build
  169. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  170. COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
  171. )
  172. # 生成 hex 文件
  173. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  174. COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
  175. )
  176. # 生成 bin 文件
  177. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  178. COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
  179. )