CMakeLists.txt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_DEFAULT)
  19. list(APPEND compile_options "-Og")
  20. elseif(CONFIG_COMPILER_OPTIMIZATION_NONE)
  21. list(APPEND compile_options "-O0")
  22. elseif(CONFIG_COMPILER_OPTIMIZATION_PERF)
  23. list(APPEND compile_options "-O2")
  24. endif()
  25. else() # BOOTLOADER_BUILD
  26. if(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE)
  27. list(APPEND compile_options "-Os")
  28. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  29. list(APPEND compile_options "-freorder-blocks")
  30. endif()
  31. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG)
  32. list(APPEND compile_options "-Og")
  33. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE)
  34. list(APPEND compile_options "-O0")
  35. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF)
  36. list(APPEND compile_options "-O2")
  37. endif()
  38. endif()
  39. if(CONFIG_COMPILER_CXX_EXCEPTIONS)
  40. list(APPEND cxx_compile_options "-fexceptions")
  41. else()
  42. list(APPEND cxx_compile_options "-fno-exceptions")
  43. endif()
  44. if(CONFIG_COMPILER_CXX_RTTI)
  45. list(APPEND cxx_compile_options "-frtti")
  46. else()
  47. list(APPEND cxx_compile_options "-fno-rtti")
  48. list(APPEND link_options "-fno-rtti") # used to invoke correct multilib variant (no-rtti) during linking
  49. endif()
  50. if(CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS)
  51. list(APPEND compile_options "-msave-restore")
  52. endif()
  53. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  54. list(APPEND c_compile_options "-Wno-old-style-declaration")
  55. endif()
  56. # Clang finds some warnings in IDF code which GCC doesn't.
  57. # All these warnings should be fixed before Clang is presented
  58. # as a toolchain choice for users.
  59. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  60. # Clang checks Doxygen comments for being in sync with function prototype.
  61. # There are some inconsistencies, especially in ROM headers.
  62. list(APPEND compile_options "-Wno-documentation")
  63. # GCC allows repeated typedefs when the source and target types are the same.
  64. # Clang doesn't allow this. This occurs in many components due to forward
  65. # declarations.
  66. list(APPEND compile_options "-Wno-typedef-redefinition")
  67. # This issue is seemingly related to newlib's char type functions.
  68. # Fix is not clear yet.
  69. list(APPEND compile_options "-Wno-char-subscripts")
  70. # Clang seems to notice format string issues which GCC doesn't.
  71. list(APPEND compile_options "-Wno-format-security")
  72. # Logic bug in essl component
  73. list(APPEND compile_options "-Wno-tautological-overlap-compare")
  74. # Some pointer checks in mDNS component check addresses which can't be NULL
  75. list(APPEND compile_options "-Wno-tautological-pointer-compare")
  76. # Similar to the above, in tcp_transport
  77. list(APPEND compile_options "-Wno-pointer-bool-conversion")
  78. # mbedTLS md5.c triggers this warning in md5_test_buf (false positive)
  79. list(APPEND compile_options "-Wno-string-concatenation")
  80. # multiple cases of implict convertions between unrelated enum types
  81. list(APPEND compile_options "-Wno-enum-conversion")
  82. # When IRAM_ATTR is specified both in function declaration and definition,
  83. # it produces different section names, since section names include __COUNTER__.
  84. # Occurs in multiple places.
  85. list(APPEND compile_options "-Wno-section")
  86. # Multiple cases of attributes unknown to clang, for example
  87. # __attribute__((optimize("-O3")))
  88. list(APPEND compile_options "-Wno-unknown-attributes")
  89. # Disable Clang warnings for atomic operations with access size
  90. # more then 4 bytes
  91. list(APPEND compile_options "-Wno-atomic-alignment")
  92. # Clang also produces many -Wunused-function warnings which GCC doesn't.
  93. # However these aren't treated as errors.
  94. endif()
  95. # More warnings may exist in unit tests and example projects.
  96. if(CONFIG_COMPILER_WARN_WRITE_STRINGS)
  97. list(APPEND compile_options "-Wwrite-strings")
  98. endif()
  99. if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
  100. list(APPEND compile_definitions "-DNDEBUG")
  101. endif()
  102. if(CONFIG_COMPILER_STACK_CHECK_MODE_NORM)
  103. list(APPEND compile_options "-fstack-protector")
  104. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_STRONG)
  105. list(APPEND compile_options "-fstack-protector-strong")
  106. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_ALL)
  107. list(APPEND compile_options "-fstack-protector-all")
  108. endif()
  109. if(CONFIG_COMPILER_DUMP_RTL_FILES)
  110. list(APPEND compile_options "-fdump-rtl-expand")
  111. endif()
  112. if(NOT ${CMAKE_C_COMPILER_VERSION} VERSION_LESS 8.0.0)
  113. if(CONFIG_COMPILER_HIDE_PATHS_MACROS)
  114. list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.")
  115. list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=/IDF")
  116. endif()
  117. if(CONFIG_APP_REPRODUCIBLE_BUILD)
  118. idf_build_set_property(DEBUG_PREFIX_MAP_GDBINIT "${BUILD_DIR}/prefix_map_gdbinit")
  119. list(APPEND compile_options "-fdebug-prefix-map=${IDF_PATH}=/IDF")
  120. list(APPEND compile_options "-fdebug-prefix-map=${PROJECT_DIR}=/IDF_PROJECT")
  121. list(APPEND compile_options "-fdebug-prefix-map=${BUILD_DIR}=/IDF_BUILD")
  122. # component dirs
  123. idf_build_get_property(python PYTHON)
  124. idf_build_get_property(idf_path IDF_PATH)
  125. idf_build_get_property(component_dirs BUILD_COMPONENT_DIRS)
  126. execute_process(
  127. COMMAND ${python}
  128. "${idf_path}/tools/generate_debug_prefix_map.py"
  129. "${BUILD_DIR}"
  130. "${component_dirs}"
  131. OUTPUT_VARIABLE result
  132. RESULT_VARIABLE ret
  133. )
  134. if(NOT ret EQUAL 0)
  135. message(FATAL_ERROR "This is a bug. Please report to https://github.com/espressif/esp-idf/issues")
  136. endif()
  137. spaces2list(result)
  138. list(LENGTH component_dirs length)
  139. math(EXPR max_index "${length} - 1")
  140. foreach(index RANGE ${max_index})
  141. list(GET component_dirs ${index} folder)
  142. list(GET result ${index} after)
  143. list(APPEND compile_options "-fdebug-prefix-map=${folder}=${after}")
  144. endforeach()
  145. endif()
  146. endif()
  147. # GCC-specific options
  148. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  149. list(APPEND compile_options "-fstrict-volatile-bitfields"
  150. "-Wno-error=unused-but-set-variable"
  151. )
  152. endif()
  153. if(CONFIG_ESP_SYSTEM_USE_EH_FRAME)
  154. list(APPEND compile_options "-fasynchronous-unwind-tables")
  155. list(APPEND link_options "-Wl,--eh-frame-hdr")
  156. endif()
  157. list(APPEND link_options "-fno-lto")
  158. if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
  159. list(APPEND link_options "-Wl,-dead_strip")
  160. list(APPEND link_options "-Wl,-warn_commons")
  161. else()
  162. list(APPEND link_options "-Wl,--gc-sections")
  163. list(APPEND link_options "-Wl,--warn-common")
  164. endif()
  165. # SMP FreeRTOS user provided minimal idle hook. This allows the user to provide
  166. # their own copy of vApplicationMinimalIdleHook()
  167. if(CONFIG_FREERTOS_USE_MINIMAL_IDLE_HOOK)
  168. list(APPEND link_options "-Wl,--wrap=vApplicationMinimalIdleHook")
  169. endif()
  170. # Placing jump tables in flash would cause issues with code that required
  171. # to be placed in IRAM
  172. list(APPEND compile_options "-fno-jump-tables")
  173. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  174. # This flag is GCC-specific.
  175. # Not clear yet if some other flag should be used for Clang.
  176. list(APPEND compile_options "-fno-tree-switch-conversion")
  177. endif()
  178. if(CMAKE_C_COMPILER_ID MATCHES "LLVM")
  179. list(APPEND compile_options "-fno-use-cxa-atexit")
  180. endif()
  181. # For the transition period from 32-bit time_t to 64-bit time_t,
  182. # auto-detect the size of this type and set corresponding variable.
  183. include(CheckTypeSize)
  184. check_type_size("time_t" TIME_T_SIZE)
  185. if(TIME_T_SIZE)
  186. idf_build_set_property(TIME_T_SIZE ${TIME_T_SIZE})
  187. else()
  188. message(FATAL_ERROR "Failed to determine sizeof(time_t)")
  189. endif()
  190. idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND)
  191. idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND)
  192. idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND)
  193. idf_build_set_property(ASM_COMPILE_OPTIONS "${asm_compile_options}" APPEND)
  194. idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
  195. idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND)
  196. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  197. # Add each component as a subdirectory, processing each component's CMakeLists.txt
  198. foreach(component_target ${build_component_targets})
  199. __component_get_property(dir ${component_target} COMPONENT_DIR)
  200. __component_get_property(_name ${component_target} COMPONENT_NAME)
  201. __component_get_property(prefix ${component_target} __PREFIX)
  202. __component_get_property(alias ${component_target} COMPONENT_ALIAS)
  203. set(COMPONENT_NAME ${_name})
  204. set(COMPONENT_DIR ${dir})
  205. set(COMPONENT_ALIAS ${alias})
  206. set(COMPONENT_PATH ${dir}) # for backward compatibility only, COMPONENT_DIR is preferred
  207. idf_build_get_property(build_prefix __PREFIX)
  208. set(__idf_component_context 1)
  209. if(NOT prefix STREQUAL build_prefix)
  210. add_subdirectory(${dir} ${prefix}_${_name})
  211. else()
  212. add_subdirectory(${dir} ${_name})
  213. endif()
  214. set(__idf_component_context 0)
  215. endforeach()