CMakeLists.txt 5.1 KB

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