CMakeLists.txt 5.1 KB

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