CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. cmake_minimum_required(VERSION 3.16)
  2. project(esp-idf C CXX ASM)
  3. if(CMAKE_CURRENT_LIST_DIR STREQUAL CMAKE_SOURCE_DIR)
  4. message(FATAL_ERROR "Current directory '${CMAKE_CURRENT_LIST_DIR}' is not buildable. "
  5. "Change directories to one of the example projects in '${CMAKE_CURRENT_LIST_DIR}/examples' and try "
  6. "again.")
  7. endif()
  8. # Variables compile_options, c_compile_options, cxx_compile_options, compile_definitions, link_options shall
  9. # not be unset as they may already contain flags, set by toolchain-TARGET.cmake files.
  10. # Add the following build specifications here, since these seem to be dependent
  11. # on config values on the root Kconfig.
  12. if(NOT BOOTLOADER_BUILD)
  13. if(CONFIG_COMPILER_OPTIMIZATION_SIZE)
  14. list(APPEND compile_options "-Os")
  15. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  16. list(APPEND compile_options "-freorder-blocks")
  17. endif()
  18. elseif(CONFIG_COMPILER_OPTIMIZATION_DEBUG)
  19. list(APPEND compile_options "-Og")
  20. if(CMAKE_C_COMPILER_ID MATCHES "GNU" AND NOT CONFIG_IDF_TARGET_LINUX)
  21. list(APPEND compile_options "-fno-shrink-wrap") # Disable shrink-wrapping to reduce binary size
  22. endif()
  23. elseif(CONFIG_COMPILER_OPTIMIZATION_NONE)
  24. list(APPEND compile_options "-O0")
  25. elseif(CONFIG_COMPILER_OPTIMIZATION_PERF)
  26. list(APPEND compile_options "-O2")
  27. endif()
  28. else() # BOOTLOADER_BUILD
  29. if(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE)
  30. list(APPEND compile_options "-Os")
  31. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  32. list(APPEND compile_options "-freorder-blocks")
  33. endif()
  34. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG)
  35. list(APPEND compile_options "-Og")
  36. if(CMAKE_C_COMPILER_ID MATCHES "GNU" AND NOT CONFIG_IDF_TARGET_LINUX)
  37. list(APPEND compile_options "-fno-shrink-wrap") # Disable shrink-wrapping to reduce binary size
  38. endif()
  39. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE)
  40. list(APPEND compile_options "-O0")
  41. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF)
  42. list(APPEND compile_options "-O2")
  43. endif()
  44. endif()
  45. if(CONFIG_COMPILER_CXX_EXCEPTIONS)
  46. list(APPEND cxx_compile_options "-fexceptions")
  47. else()
  48. list(APPEND cxx_compile_options "-fno-exceptions")
  49. endif()
  50. if(CONFIG_COMPILER_CXX_RTTI)
  51. list(APPEND cxx_compile_options "-frtti")
  52. else()
  53. list(APPEND cxx_compile_options "-fno-rtti")
  54. list(APPEND link_options "-fno-rtti") # used to invoke correct multilib variant (no-rtti) during linking
  55. endif()
  56. if(CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS)
  57. list(APPEND compile_options "-msave-restore")
  58. endif()
  59. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  60. list(APPEND c_compile_options "-Wno-old-style-declaration")
  61. endif()
  62. # Clang finds some warnings in IDF code which GCC doesn't.
  63. # All these warnings should be fixed before Clang is presented
  64. # as a toolchain choice for users.
  65. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  66. # Clang checks Doxygen comments for being in sync with function prototype.
  67. # There are some inconsistencies, especially in ROM headers.
  68. list(APPEND compile_options "-Wno-documentation")
  69. # GCC allows repeated typedefs when the source and target types are the same.
  70. # Clang doesn't allow this. This occurs in many components due to forward
  71. # declarations.
  72. list(APPEND compile_options "-Wno-typedef-redefinition")
  73. # This issue is seemingly related to newlib's char type functions.
  74. # Fix is not clear yet.
  75. list(APPEND compile_options "-Wno-char-subscripts")
  76. # Clang seems to notice format string issues which GCC doesn't.
  77. list(APPEND compile_options "-Wno-format-security")
  78. # Logic bug in essl component
  79. list(APPEND compile_options "-Wno-tautological-overlap-compare")
  80. # Some pointer checks in mDNS component check addresses which can't be NULL
  81. list(APPEND compile_options "-Wno-tautological-pointer-compare")
  82. # Similar to the above, in tcp_transport
  83. list(APPEND compile_options "-Wno-pointer-bool-conversion")
  84. # mbedTLS md5.c triggers this warning in md5_test_buf (false positive)
  85. list(APPEND compile_options "-Wno-string-concatenation")
  86. # multiple cases of implict convertions between unrelated enum types
  87. list(APPEND compile_options "-Wno-enum-conversion")
  88. # When IRAM_ATTR is specified both in function declaration and definition,
  89. # it produces different section names, since section names include __COUNTER__.
  90. # Occurs in multiple places.
  91. list(APPEND compile_options "-Wno-section")
  92. # Multiple cases of attributes unknown to clang, for example
  93. # __attribute__((optimize("-O3")))
  94. list(APPEND compile_options "-Wno-unknown-attributes")
  95. # Disable Clang warnings for atomic operations with access size
  96. # more then 4 bytes
  97. list(APPEND compile_options "-Wno-atomic-alignment")
  98. # several warnings in wpa_supplicant component
  99. list(APPEND compile_options "-Wno-unused-but-set-variable")
  100. # Clang also produces many -Wunused-function warnings which GCC doesn't.
  101. list(APPEND compile_options "-Wno-unused-function")
  102. # many warnings in bluedroid code
  103. # warning: field 'hdr' with variable sized type 'BT_HDR' not at the end of a struct or class is a GNU extension
  104. list(APPEND compile_options "-Wno-gnu-variable-sized-type-not-at-end")
  105. # several warnings in bluedroid code
  106. list(APPEND compile_options "-Wno-constant-logical-operand")
  107. # warning: '_Static_assert' with no message is a C2x extension
  108. list(APPEND compile_options "-Wno-c2x-extensions")
  109. # warning on xMPU_SETTINGS for esp32s2 has size 0 for C and 1 for C++
  110. list(APPEND compile_options "-Wno-extern-c-compat")
  111. # warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1
  112. list(APPEND compile_options "-Wno-single-bit-bitfield-constant-conversion")
  113. endif()
  114. # More warnings may exist in unit tests and example projects.
  115. if(CONFIG_COMPILER_WARN_WRITE_STRINGS)
  116. list(APPEND compile_options "-Wwrite-strings")
  117. endif()
  118. if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
  119. list(APPEND compile_definitions "-DNDEBUG")
  120. endif()
  121. if(CONFIG_COMPILER_STACK_CHECK_MODE_NORM)
  122. list(APPEND compile_options "-fstack-protector")
  123. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_STRONG)
  124. list(APPEND compile_options "-fstack-protector-strong")
  125. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_ALL)
  126. list(APPEND compile_options "-fstack-protector-all")
  127. endif()
  128. if(CONFIG_COMPILER_DUMP_RTL_FILES)
  129. list(APPEND compile_options "-fdump-rtl-expand")
  130. endif()
  131. if(NOT ${CMAKE_C_COMPILER_VERSION} VERSION_LESS 8.0.0)
  132. if(CONFIG_COMPILER_HIDE_PATHS_MACROS)
  133. list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.")
  134. list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=/IDF")
  135. endif()
  136. if(CONFIG_APP_REPRODUCIBLE_BUILD)
  137. idf_build_set_property(DEBUG_PREFIX_MAP_GDBINIT "${BUILD_DIR}/prefix_map_gdbinit")
  138. list(APPEND compile_options "-fdebug-prefix-map=${IDF_PATH}=/IDF")
  139. list(APPEND compile_options "-fdebug-prefix-map=${PROJECT_DIR}=/IDF_PROJECT")
  140. list(APPEND compile_options "-fdebug-prefix-map=${BUILD_DIR}=/IDF_BUILD")
  141. # component dirs
  142. idf_build_get_property(python PYTHON)
  143. idf_build_get_property(idf_path IDF_PATH)
  144. idf_build_get_property(component_dirs BUILD_COMPONENT_DIRS)
  145. execute_process(
  146. COMMAND ${python}
  147. "${idf_path}/tools/generate_debug_prefix_map.py"
  148. "${BUILD_DIR}"
  149. "${component_dirs}"
  150. OUTPUT_VARIABLE result
  151. RESULT_VARIABLE ret
  152. )
  153. if(NOT ret EQUAL 0)
  154. message(FATAL_ERROR "This is a bug. Please report to https://github.com/espressif/esp-idf/issues")
  155. endif()
  156. spaces2list(result)
  157. list(LENGTH component_dirs length)
  158. math(EXPR max_index "${length} - 1")
  159. foreach(index RANGE ${max_index})
  160. list(GET component_dirs ${index} folder)
  161. list(GET result ${index} after)
  162. list(APPEND compile_options "-fdebug-prefix-map=${folder}=${after}")
  163. endforeach()
  164. endif()
  165. endif()
  166. if(CONFIG_COMPILER_DISABLE_GCC12_WARNINGS)
  167. list(APPEND compile_options "-Wno-address"
  168. "-Wno-use-after-free")
  169. endif()
  170. if(CONFIG_COMPILER_DISABLE_GCC13_WARNINGS)
  171. list(APPEND compile_options "-Wno-xor-used-as-pow")
  172. list(APPEND c_compile_options "-Wno-enum-int-mismatch")
  173. list(APPEND cxx_compile_options "-Wno-self-move"
  174. "-Wno-dangling-reference")
  175. endif()
  176. # GCC-specific options
  177. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  178. list(APPEND compile_options "-fstrict-volatile-bitfields"
  179. )
  180. endif()
  181. if(CONFIG_ESP_SYSTEM_USE_EH_FRAME)
  182. list(APPEND compile_options "-fasynchronous-unwind-tables")
  183. list(APPEND link_options "-Wl,--eh-frame-hdr")
  184. endif()
  185. list(APPEND link_options "-fno-lto")
  186. if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
  187. list(APPEND link_options "-Wl,-dead_strip")
  188. list(APPEND link_options "-Wl,-warn_commons")
  189. else()
  190. list(APPEND link_options "-Wl,--gc-sections")
  191. list(APPEND link_options "-Wl,--warn-common")
  192. endif()
  193. # SMP FreeRTOS user provided minimal idle hook. This allows the user to provide
  194. # their own copy of vApplicationMinimalIdleHook()
  195. if(CONFIG_FREERTOS_USE_MINIMAL_IDLE_HOOK)
  196. list(APPEND link_options "-Wl,--wrap=vApplicationMinimalIdleHook")
  197. endif()
  198. # Placing jump tables in flash would cause issues with code that required
  199. # to be placed in IRAM
  200. list(APPEND compile_options "-fno-jump-tables")
  201. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  202. # This flag is GCC-specific.
  203. # Not clear yet if some other flag should be used for Clang.
  204. list(APPEND compile_options "-fno-tree-switch-conversion")
  205. endif()
  206. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  207. list(APPEND compile_options "-fno-use-cxa-atexit")
  208. endif()
  209. if(COMPILER_RT_LIB_NAME)
  210. list(APPEND link_options "-rtlib=${CONFIG_COMPILER_RT_LIB_NAME}")
  211. endif()
  212. # For the transition period from 32-bit time_t to 64-bit time_t,
  213. # auto-detect the size of this type and set corresponding variable.
  214. include(CheckTypeSize)
  215. check_type_size("time_t" TIME_T_SIZE)
  216. if(TIME_T_SIZE)
  217. idf_build_set_property(TIME_T_SIZE ${TIME_T_SIZE})
  218. else()
  219. message(FATAL_ERROR "Failed to determine sizeof(time_t)")
  220. endif()
  221. idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND)
  222. idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND)
  223. idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND)
  224. idf_build_set_property(ASM_COMPILE_OPTIONS "${asm_compile_options}" APPEND)
  225. idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
  226. idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND)
  227. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  228. # Add each component as a subdirectory, processing each component's CMakeLists.txt
  229. foreach(component_target ${build_component_targets})
  230. __component_get_property(dir ${component_target} COMPONENT_DIR)
  231. __component_get_property(_name ${component_target} COMPONENT_NAME)
  232. __component_get_property(prefix ${component_target} __PREFIX)
  233. __component_get_property(alias ${component_target} COMPONENT_ALIAS)
  234. set(COMPONENT_NAME ${_name})
  235. set(COMPONENT_DIR ${dir})
  236. set(COMPONENT_ALIAS ${alias})
  237. set(COMPONENT_PATH ${dir}) # for backward compatibility only, COMPONENT_DIR is preferred
  238. idf_build_get_property(build_prefix __PREFIX)
  239. set(__idf_component_context 1)
  240. if(NOT prefix STREQUAL build_prefix)
  241. add_subdirectory(${dir} ${prefix}_${_name})
  242. else()
  243. add_subdirectory(${dir} ${_name})
  244. endif()
  245. set(__idf_component_context 0)
  246. endforeach()