CMakeLists.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. cmake_minimum_required(VERSION 3.20)
  2. # 项目设置
  3. project(alientek_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=soft
  21. )
  22. # C 源文件
  23. file(GLOB_RECURSE C_SRCS
  24. ${CMAKE_CURRENT_SOURCE_DIR}/applications/*.c
  25. ${CMAKE_CURRENT_SOURCE_DIR}/project/gcc/startup_stm32f40xx.s
  26. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/*.c
  27. ${CMAKE_CURRENT_SOURCE_DIR}/board/*.c
  28. )
  29. file(GLOB_RECURSE R_SRCS
  30. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
  31. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
  32. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
  33. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
  34. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
  35. )
  36. list(REMOVE_ITEM C_SRCS ${R_SRCS})
  37. # C .h文件
  38. set(INC_C_DIRS
  39. ${CMAKE_CURRENT_SOURCE_DIR}/applications
  40. ${CMAKE_CURRENT_SOURCE_DIR}/applications/bootloader
  41. ${CMAKE_CURRENT_SOURCE_DIR}/applications/global
  42. ${CMAKE_CURRENT_SOURCE_DIR}/applications/iap
  43. ${CMAKE_CURRENT_SOURCE_DIR}/applications/queue
  44. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/CMSIS/Include
  45. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/CMSIS/Device/ST/STM32F4xx/Include
  46. ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/inc
  47. ${CMAKE_CURRENT_SOURCE_DIR}/board
  48. ${CMAKE_CURRENT_SOURCE_DIR}/board/driver
  49. ${CMAKE_CURRENT_SOURCE_DIR}/board/driver/gpio
  50. ${CMAKE_CURRENT_SOURCE_DIR}/board/can
  51. ${CMAKE_CURRENT_SOURCE_DIR}/board/dwt
  52. )
  53. set(INC_CXX_DIRS)
  54. set(INC_ASM_DIRS)
  55. # 链接定义
  56. set(C_SYMB
  57. "DEBUG"
  58. "STM32F40_41xxx"
  59. "USE_STDPERIPH_DRIVER"
  60. )
  61. set(CXX_SYMB)
  62. set(ASM_SYMB)
  63. # 链接静态库或者文件夹
  64. set(LINK_DIRS)
  65. set(LINK_LIBS)
  66. # 链接脚本
  67. set(LINK_SRC
  68. ${CMAKE_CURRENT_SOURCE_DIR}/project/gcc/STM32F417IG_FLASH.ld
  69. )
  70. # 编译器参数
  71. set(compiler_OPTS)
  72. # 链接脚本参数
  73. set(LINK_OPTS)
  74. link_directories(${CMAKE_PROJECT_NAME} ${LINK_DIRS})
  75. add_executable(${CMAKE_PROJECT_NAME})
  76. target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${C_SRCS})
  77. # 添加 include 路径
  78. target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
  79. $<$<COMPILE_LANGUAGE:C>: ${INC_C_DIRS}>
  80. $<$<COMPILE_LANGUAGE:CXX>: ${INC_CXX_DIRS}>
  81. $<$<COMPILE_LANGUAGE:ASM>: ${INC_ASM_DIRS}>
  82. )
  83. # 添加 链接 文件
  84. target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
  85. $<$<COMPILE_LANGUAGE:C>: ${C_SYMB}>
  86. $<$<COMPILE_LANGUAGE:CXX>: ${CXX_SYMB}>
  87. $<$<COMPILE_LANGUAGE:ASM>: ${ASM_SYMB}>
  88. $<$<CONFIG:Debug>:DEBUG>
  89. $<$<CONFIG:Release>: >
  90. )
  91. target_link_libraries(${CMAKE_PROJECT_NAME} ${LINK_LIBS})
  92. # 编译器参数
  93. target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
  94. ${MCU_PARAMS}
  95. ${compiler_OPTS}
  96. -Wall
  97. -Wextra
  98. -Wpedantic
  99. $<$<COMPILE_LANGUAGE:C>: >
  100. $<$<COMPILE_LANGUAGE:CXX>:
  101. # 去除编译警告
  102. -Wno-unused-variable
  103. -Wno-pointer-sign
  104. -Wno-unused-but-set-variable
  105. -Wno-unused-function
  106. -Wno-unused-parameter
  107. -Wno-format=
  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. )