CMakeLists.txt 4.9 KB

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