CMakeLists.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. cmake_minimum_required(VERSION 3.20)
  2. # 项目设置
  3. project(armfly_bootloader)
  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. set(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}/User/app/bootloader/bootloader.c
  28. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/global/var.c
  29. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/iap/iap.c
  30. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/queue/queue.c
  31. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/can/can.c
  32. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dwt/dwt.c
  33. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/led/led.c
  34. ${CMAKE_CURRENT_SOURCE_DIR}/Project/GCC/startup_stm32f40xx.s
  35. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/misc.c
  36. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_can.c
  37. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_flash.c
  38. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
  39. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
  40. )
  41. # C .h文件
  42. set(INC_C_DIRS
  43. ${CMAKE_CURRENT_SOURCE_DIR}/Project
  44. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/led
  45. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/dwt
  46. ${CMAKE_CURRENT_SOURCE_DIR}/User/bsp/can
  47. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/bootloader
  48. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/iap
  49. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/global
  50. ${CMAKE_CURRENT_SOURCE_DIR}/User/app/queue
  51. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/CMSIS/Include
  52. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/CMSIS/Device/ST/STM32F4xx/Include
  53. ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/STM32F4xx_StdPeriph_Driver/inc
  54. )
  55. set(INC_CXX_DIRS)
  56. set(INC_ASM_DIRS)
  57. # 链接定义
  58. set(C_SYMB
  59. "DEBUG"
  60. "STM32F40_41xxx"
  61. "USE_STDPERIPH_DRIVER"
  62. )
  63. set(CXX_SYMB)
  64. set(ASM_SYMB)
  65. # 链接静态库或者文件夹
  66. set(LINK_DIRS)
  67. set(LINK_LIBS)
  68. # 链接脚本
  69. set(LINK_SRC
  70. ${CMAKE_CURRENT_SOURCE_DIR}/Project/GCC/STM32F417IG_FLASH.ld
  71. )
  72. # 编译器参数
  73. set(compiler_OPTS)
  74. # 链接脚本参数
  75. set(LINK_OPTS)
  76. link_directories(${CMAKE_PROJECT_NAME} ${LINK_DIRS})
  77. add_executable(${CMAKE_PROJECT_NAME})
  78. target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${C_SRCS})
  79. # 添加 include 路径
  80. target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
  81. $<$<COMPILE_LANGUAGE:C>: ${INC_C_DIRS}>
  82. $<$<COMPILE_LANGUAGE:CXX>: ${INC_CXX_DIRS}>
  83. $<$<COMPILE_LANGUAGE:ASM>: ${INC_ASM_DIRS}>
  84. )
  85. # 添加 链接 文件
  86. target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
  87. $<$<COMPILE_LANGUAGE:C>: ${C_SYMB}>
  88. $<$<COMPILE_LANGUAGE:CXX>: ${CXX_SYMB}>
  89. $<$<COMPILE_LANGUAGE:ASM>: ${ASM_SYMB}>
  90. $<$<CONFIG:Debug>:DEBUG>
  91. $<$<CONFIG:Release>: >
  92. )
  93. target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIBS})
  94. # 编译器参数
  95. target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
  96. ${MCU_PARAMS}
  97. ${compiler_OPTS}
  98. -Wall
  99. -Wextra
  100. -Wpedantic
  101. $<$<COMPILE_LANGUAGE:C>: >
  102. $<$<COMPILE_LANGUAGE:CXX>:
  103. # 去除编译警告
  104. -Wno-unused-parameter
  105. -Wno-unused-variable
  106. -Wno-pointer-sign
  107. -Wno-unused-but-set-variable
  108. # -Wno-volatile
  109. # -Wold-style-cast
  110. # -Wuseless-cast
  111. # -Wsuggest-override
  112. >
  113. $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP>
  114. $<$<CONFIG:Debug>:-Og -g3 -ggdb>
  115. $<$<CONFIG:Release>:-Og -g0>
  116. )
  117. # 链接参数
  118. target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
  119. -T${LINK_SRC}
  120. ${MCU_PARAMS}
  121. ${LINK_OPTS}
  122. -Wl,-Map=${CMAKE_PROJECT_NAME}.map
  123. # -u _printf_float
  124. --specs=nosys.specs
  125. -Wl,--start-group
  126. -lc
  127. -lm
  128. -lstdc++
  129. -lsupc++
  130. -Wl,--end-group
  131. # Allow good software remapping across address space (with proper GCC section making)
  132. -Wl,-z,max-page-size=8
  133. -Wl,--print-memory-usage
  134. )
  135. # post-build
  136. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  137. COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
  138. )
  139. # 生成 hex 文件
  140. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  141. COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
  142. )
  143. # 生成 bin 文件
  144. add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
  145. COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
  146. )