CMakeLists.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. cmake_minimum_required(VERSION 3.5)
  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(CONFIG_COMPILER_DISABLE_GCC8_WARNINGS)
  54. list(APPEND compile_options "-Wno-parentheses"
  55. "-Wno-sizeof-pointer-memaccess"
  56. "-Wno-clobbered")
  57. list(APPEND compile_options "-Wno-format-overflow"
  58. "-Wno-stringop-truncation"
  59. "-Wno-misleading-indentation"
  60. "-Wno-cast-function-type"
  61. "-Wno-implicit-fallthrough"
  62. "-Wno-unused-const-variable"
  63. "-Wno-switch-unreachable"
  64. "-Wno-format-truncation"
  65. "-Wno-memset-elt-size"
  66. "-Wno-int-in-bool-context")
  67. endif()
  68. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  69. list(APPEND c_compile_options "-Wno-old-style-declaration")
  70. endif()
  71. # Clang finds some warnings in IDF code which GCC doesn't.
  72. # All these warnings should be fixed before Clang is presented
  73. # as a toolchain choice for users.
  74. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  75. # Clang checks Doxygen comments for being in sync with function prototype.
  76. # There are some inconsistencies, especially in ROM headers.
  77. list(APPEND compile_options "-Wno-documentation")
  78. # GCC allows repeated typedefs when the source and target types are the same.
  79. # Clang doesn't allow this. This occurs in many components due to forward
  80. # declarations.
  81. list(APPEND compile_options "-Wno-typedef-redefinition")
  82. # This issue is seemingly related to newlib's char type functions.
  83. # Fix is not clear yet.
  84. list(APPEND compile_options "-Wno-char-subscripts")
  85. # Clang seems to notice format string issues which GCC doesn't.
  86. list(APPEND compile_options "-Wno-format-security")
  87. # Logic bug in essl component
  88. list(APPEND compile_options "-Wno-tautological-overlap-compare")
  89. # Some pointer checks in mDNS component check addresses which can't be NULL
  90. list(APPEND compile_options "-Wno-tautological-pointer-compare")
  91. # Similar to the above, in tcp_transport
  92. list(APPEND compile_options "-Wno-pointer-bool-conversion")
  93. # mbedTLS md5.c triggers this warning in md5_test_buf (false positive)
  94. list(APPEND compile_options "-Wno-string-concatenation")
  95. # multiple cases of implict convertions between unrelated enum types
  96. list(APPEND compile_options "-Wno-enum-conversion")
  97. # When IRAM_ATTR is specified both in function declaration and definition,
  98. # it produces different section names, since section names include __COUNTER__.
  99. # Occurs in multiple places.
  100. list(APPEND compile_options "-Wno-section")
  101. # Multiple cases of attributes unknown to clang, for example
  102. # __attribute__((optimize("-O3")))
  103. list(APPEND compile_options "-Wno-unknown-attributes")
  104. # Clang also produces many -Wunused-function warnings which GCC doesn't.
  105. # However these aren't treated as errors.
  106. endif()
  107. # More warnings may exist in unit tests and example projects.
  108. if(CONFIG_COMPILER_WARN_WRITE_STRINGS)
  109. list(APPEND compile_options "-Wwrite-strings")
  110. endif()
  111. if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
  112. list(APPEND compile_definitions "-DNDEBUG")
  113. endif()
  114. if(CONFIG_COMPILER_STACK_CHECK_MODE_NORM)
  115. list(APPEND compile_options "-fstack-protector")
  116. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_STRONG)
  117. list(APPEND compile_options "-fstack-protector-strong")
  118. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_ALL)
  119. list(APPEND compile_options "-fstack-protector-all")
  120. endif()
  121. if(CONFIG_COMPILER_DUMP_RTL_FILES)
  122. list(APPEND compile_options "-fdump-rtl-expand")
  123. endif()
  124. if(NOT ${CMAKE_C_COMPILER_VERSION} VERSION_LESS 8.0.0)
  125. if(CONFIG_COMPILER_HIDE_PATHS_MACROS)
  126. list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.")
  127. list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=/IDF")
  128. endif()
  129. if(CONFIG_APP_REPRODUCIBLE_BUILD)
  130. idf_build_set_property(DEBUG_PREFIX_MAP_GDBINIT "${BUILD_DIR}/prefix_map_gdbinit")
  131. list(APPEND compile_options "-fdebug-prefix-map=${IDF_PATH}=/IDF")
  132. list(APPEND compile_options "-fdebug-prefix-map=${PROJECT_DIR}=/IDF_PROJECT")
  133. list(APPEND compile_options "-fdebug-prefix-map=${BUILD_DIR}=/IDF_BUILD")
  134. # component dirs
  135. idf_build_get_property(python PYTHON)
  136. idf_build_get_property(idf_path IDF_PATH)
  137. idf_build_get_property(component_dirs BUILD_COMPONENT_DIRS)
  138. execute_process(
  139. COMMAND ${python}
  140. "${idf_path}/tools/generate_debug_prefix_map.py"
  141. "${BUILD_DIR}"
  142. "${component_dirs}"
  143. OUTPUT_VARIABLE result
  144. RESULT_VARIABLE ret
  145. )
  146. if(NOT ret EQUAL 0)
  147. message(FATAL_ERROR "This is a bug. Please report to https://github.com/espressif/esp-idf/issues")
  148. endif()
  149. spaces2list(result)
  150. list(LENGTH component_dirs length)
  151. math(EXPR max_index "${length} - 1")
  152. foreach(index RANGE ${max_index})
  153. list(GET component_dirs ${index} folder)
  154. list(GET result ${index} after)
  155. list(APPEND compile_options "-fdebug-prefix-map=${folder}=${after}")
  156. endforeach()
  157. endif()
  158. endif()
  159. # GCC-specific options
  160. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  161. list(APPEND compile_options "-fstrict-volatile-bitfields"
  162. "-Wno-error=unused-but-set-variable"
  163. )
  164. endif()
  165. if(CONFIG_ESP_SYSTEM_USE_EH_FRAME)
  166. list(APPEND compile_options "-fasynchronous-unwind-tables")
  167. list(APPEND link_options "-Wl,--eh-frame-hdr")
  168. endif()
  169. list(APPEND link_options "-fno-lto")
  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()