CMakeLists.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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(TARGET_FLAGS
  17. -mthumb
  18. -mcpu=cortex-m4
  19. -mfpu=fpv4-sp-d16
  20. -mfloat-abi=hard
  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}/drivers/can/*.c
  27. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash/*.c
  28. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio/*.c
  29. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c/*.c
  30. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm/*.c
  31. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick/*.c
  32. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart/*.c
  33. ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble/*.c
  34. ${CMAKE_CURRENT_SOURCE_DIR}/modules/can/*.c
  35. ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli/*.c
  36. ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom/*.c
  37. ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266/*.c
  38. ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio/*.c
  39. ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap/*.c
  40. ${CMAKE_CURRENT_SOURCE_DIR}/modules/task/*.c
  41. ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart/*.c
  42. ${CMAKE_CURRENT_SOURCE_DIR}/project/*.c
  43. ${CMAKE_CURRENT_SOURCE_DIR}/project/startup/gcc/startup_stm32f40_41xxx.s
  44. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/*.c
  45. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button/multi_button.c
  46. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source/*.c
  47. )
  48. file(GLOB_RECURSE R_SRCS
  49. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
  50. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
  51. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
  52. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
  53. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
  54. )
  55. list(REMOVE_ITEM C_SRCS ${R_SRCS})
  56. # C .h文件
  57. set(INC_C_DIRS
  58. ${CMAKE_CURRENT_SOURCE_DIR}/applications
  59. ${CMAKE_CURRENT_SOURCE_DIR}/applications/task
  60. ${CMAKE_CURRENT_SOURCE_DIR}/common/inc
  61. ${CMAKE_CURRENT_SOURCE_DIR}/dev
  62. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/can
  63. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash
  64. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio
  65. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c
  66. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm
  67. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick
  68. ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart
  69. ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble
  70. ${CMAKE_CURRENT_SOURCE_DIR}/modules/can
  71. ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli
  72. ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom
  73. ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266
  74. ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio
  75. ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap
  76. ${CMAKE_CURRENT_SOURCE_DIR}/modules/task
  77. ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart
  78. ${CMAKE_CURRENT_SOURCE_DIR}/project
  79. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Include
  80. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Device/ST/STM32F4xx/Include
  81. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/inc
  82. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button
  83. ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source
  84. )
  85. set(INC_CXX_DIRS)
  86. set(INC_ASM_DIRS)
  87. # 链接定义
  88. set(C_SYMB
  89. "DEBUG"
  90. "STM32F40_41xxx"
  91. "USE_STDPERIPH_DRIVER"
  92. )
  93. set(CXX_SYMB)
  94. set(ASM_SYMB)
  95. # 链接静态库或者文件夹
  96. set(LINK_DIRS)
  97. set(LINK_LIBS)
  98. # 链接脚本
  99. set(LINK_SRC
  100. ${CMAKE_CURRENT_SOURCE_DIR}/project/STM32F417IG_FLASH.ld
  101. )
  102. # 编译器参数
  103. set(COMPILER_OPTS)
  104. # 链接脚本参数
  105. set(LINK_OPTS
  106. --specs=nosys.specs
  107. )
  108. link_directories(${CMAKE_PROJECT_NAME} ${LINK_DIRS})
  109. add_executable(${CMAKE_PROJECT_NAME})
  110. target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${C_SRCS})
  111. # 添加 include 路径
  112. target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
  113. $<$<COMPILE_LANGUAGE:C>: ${INC_C_DIRS}>
  114. $<$<COMPILE_LANGUAGE:CXX>: ${INC_CXX_DIRS}>
  115. $<$<COMPILE_LANGUAGE:ASM>: ${INC_ASM_DIRS}>
  116. )
  117. # 添加 链接 文件
  118. target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
  119. $<$<COMPILE_LANGUAGE:C>: ${C_SYMB}>
  120. $<$<COMPILE_LANGUAGE:CXX>: ${CXX_SYMB}>
  121. $<$<COMPILE_LANGUAGE:ASM>: ${ASM_SYMB}>
  122. $<$<CONFIG:Debug>:DEBUG>
  123. $<$<CONFIG:Release>: >
  124. )
  125. target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIBS})
  126. # 编译器参数
  127. target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
  128. ${TARGET_FLAGS}
  129. ${COMPILER_OPTS}
  130. $<$<COMPILE_LANGUAGE:C>: >
  131. $<$<COMPILE_LANGUAGE:CXX>: >
  132. $<$<COMPILE_LANGUAGE:ASM>: >
  133. $<$<CONFIG:Debug>:-Og -g3 -ggdb>
  134. $<$<CONFIG:Release>:-Og -g0>
  135. )
  136. # 链接参数
  137. target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
  138. ${TARGET_FLAGS}
  139. -T${LINK_SRC}
  140. ${LINK_OPTS}
  141. -Wl,-Map=${CMAKE_PROJECT_NAME}.map
  142. -Wl,--gc-sections
  143. -Wl,--start-group
  144. -lc
  145. -lm
  146. -lstdc++
  147. -lsupc++
  148. -Wl,--end-group
  149. # Allow good software remapping across address space (with proper GCC section making)
  150. -Wl,-z,max-page-size=8
  151. -Wl,--print-memory-usage
  152. )
  153. # post-build
  154. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>)
  155. # 生成 hex 文件
  156. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex)
  157. # 生成 bin 文件
  158. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin)