CMakeLists.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. unset(compile_options)
  9. unset(c_compile_options)
  10. unset(cxx_compile_options)
  11. unset(compile_definitions)
  12. unset(link_options)
  13. # Add the following build specifications here, since these seem to be dependent
  14. # on config values on the root Kconfig.
  15. if(NOT BOOTLOADER_BUILD)
  16. if(CONFIG_COMPILER_OPTIMIZATION_SIZE)
  17. list(APPEND compile_options "-Os")
  18. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  19. list(APPEND compile_options "-freorder-blocks")
  20. endif()
  21. elseif(CONFIG_COMPILER_OPTIMIZATION_DEFAULT)
  22. list(APPEND compile_options "-Og")
  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. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE)
  37. list(APPEND compile_options "-O0")
  38. elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF)
  39. list(APPEND compile_options "-O2")
  40. endif()
  41. endif()
  42. if(CONFIG_COMPILER_CXX_EXCEPTIONS)
  43. list(APPEND cxx_compile_options "-fexceptions")
  44. else()
  45. list(APPEND cxx_compile_options "-fno-exceptions")
  46. endif()
  47. if(CONFIG_COMPILER_CXX_RTTI)
  48. list(APPEND cxx_compile_options "-frtti")
  49. else()
  50. list(APPEND cxx_compile_options "-fno-rtti")
  51. list(APPEND link_options "-fno-rtti") # used to invoke correct multilib variant (no-rtti) during linking
  52. endif()
  53. if(CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS)
  54. list(APPEND compile_options "-msave-restore")
  55. endif()
  56. if(CONFIG_COMPILER_DISABLE_GCC8_WARNINGS)
  57. list(APPEND compile_options "-Wno-parentheses"
  58. "-Wno-sizeof-pointer-memaccess"
  59. "-Wno-clobbered")
  60. list(APPEND compile_options "-Wno-format-overflow"
  61. "-Wno-stringop-truncation"
  62. "-Wno-misleading-indentation"
  63. "-Wno-cast-function-type"
  64. "-Wno-implicit-fallthrough"
  65. "-Wno-unused-const-variable"
  66. "-Wno-switch-unreachable"
  67. "-Wno-format-truncation"
  68. "-Wno-memset-elt-size"
  69. "-Wno-int-in-bool-context")
  70. endif()
  71. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  72. list(APPEND c_compile_options "-Wno-old-style-declaration")
  73. endif()
  74. # Clang finds some warnings in IDF code which GCC doesn't.
  75. # All these warnings should be fixed before Clang is presented
  76. # as a toolchain choice for users.
  77. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  78. # Clang checks Doxygen comments for being in sync with function prototype.
  79. # There are some inconsistencies, especially in ROM headers.
  80. list(APPEND compile_options "-Wno-documentation")
  81. # GCC allows repeated typedefs when the source and target types are the same.
  82. # Clang doesn't allow this. This occurs in many components due to forward
  83. # declarations.
  84. list(APPEND compile_options "-Wno-typedef-redefinition")
  85. # This issue is seemingly related to newlib's char type functions.
  86. # Fix is not clear yet.
  87. list(APPEND compile_options "-Wno-char-subscripts")
  88. # Clang seems to notice format string issues which GCC doesn't.
  89. list(APPEND compile_options "-Wno-format-security")
  90. # Logic bug in essl component
  91. list(APPEND compile_options "-Wno-tautological-overlap-compare")
  92. # Some pointer checks in mDNS component check addresses which can't be NULL
  93. list(APPEND compile_options "-Wno-tautological-pointer-compare")
  94. # Similar to the above, in tcp_transport
  95. list(APPEND compile_options "-Wno-pointer-bool-conversion")
  96. # mbedTLS md5.c triggers this warning in md5_test_buf (false positive)
  97. list(APPEND compile_options "-Wno-string-concatenation")
  98. # multiple cases of implict convertions between unrelated enum types
  99. list(APPEND compile_options "-Wno-enum-conversion")
  100. # When IRAM_ATTR is specified both in function declaration and definition,
  101. # it produces different section names, since section names include __COUNTER__.
  102. # Occurs in multiple places.
  103. list(APPEND compile_options "-Wno-section")
  104. # Multiple cases of attributes unknown to clang, for example
  105. # __attribute__((optimize("-O3")))
  106. list(APPEND compile_options "-Wno-unknown-attributes")
  107. # Clang also produces many -Wunused-function warnings which GCC doesn't.
  108. # However these aren't treated as errors.
  109. endif()
  110. # More warnings may exist in unit tests and example projects.
  111. if(CONFIG_COMPILER_WARN_WRITE_STRINGS)
  112. list(APPEND compile_options "-Wwrite-strings")
  113. endif()
  114. if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
  115. list(APPEND compile_definitions "-DNDEBUG")
  116. endif()
  117. if(CONFIG_COMPILER_STACK_CHECK_MODE_NORM)
  118. list(APPEND compile_options "-fstack-protector")
  119. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_STRONG)
  120. list(APPEND compile_options "-fstack-protector-strong")
  121. elseif(CONFIG_COMPILER_STACK_CHECK_MODE_ALL)
  122. list(APPEND compile_options "-fstack-protector-all")
  123. endif()
  124. if(CONFIG_COMPILER_DUMP_RTL_FILES)
  125. list(APPEND compile_options "-fdump-rtl-expand")
  126. endif()
  127. if(NOT ${CMAKE_C_COMPILER_VERSION} VERSION_LESS 8.0.0)
  128. if(CONFIG_COMPILER_HIDE_PATHS_MACROS)
  129. list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.")
  130. list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=IDF")
  131. endif()
  132. endif()
  133. # GCC-specific options
  134. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  135. list(APPEND compile_options "-fstrict-volatile-bitfields"
  136. "-Wno-error=unused-but-set-variable"
  137. )
  138. endif()
  139. if(CONFIG_ESP_SYSTEM_USE_EH_FRAME)
  140. list(APPEND compile_options "-fasynchronous-unwind-tables")
  141. list(APPEND link_options "-Wl,--eh-frame-hdr")
  142. endif()
  143. list(APPEND link_options "-fno-lto")
  144. # Placing jump tables in flash would cause issues with code that required
  145. # to be placed in IRAM
  146. list(APPEND compile_options "-fno-jump-tables")
  147. if(CMAKE_C_COMPILER_ID MATCHES "GNU")
  148. # This flag is GCC-specific.
  149. # Not clear yet if some other flag should be used for Clang.
  150. list(APPEND compile_options "-fno-tree-switch-conversion")
  151. endif()
  152. if(CMAKE_C_COMPILER_ID MATCHES "LLVM")
  153. list(APPEND compile_options "-fno-use-cxa-atexit")
  154. endif()
  155. idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND)
  156. idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND)
  157. idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND)
  158. idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
  159. idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND)
  160. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  161. # Add each component as a subdirectory, processing each component's CMakeLists.txt
  162. foreach(component_target ${build_component_targets})
  163. __component_get_property(dir ${component_target} COMPONENT_DIR)
  164. __component_get_property(_name ${component_target} COMPONENT_NAME)
  165. __component_get_property(prefix ${component_target} __PREFIX)
  166. __component_get_property(alias ${component_target} COMPONENT_ALIAS)
  167. set(COMPONENT_NAME ${_name})
  168. set(COMPONENT_DIR ${dir})
  169. set(COMPONENT_ALIAS ${alias})
  170. set(COMPONENT_PATH ${dir}) # for backward compatibility only, COMPONENT_DIR is preferred
  171. idf_build_get_property(build_prefix __PREFIX)
  172. set(__idf_component_context 1)
  173. if(NOT prefix STREQUAL build_prefix)
  174. add_subdirectory(${dir} ${prefix}_${_name})
  175. else()
  176. add_subdirectory(${dir} ${_name})
  177. endif()
  178. set(__idf_component_context 0)
  179. endforeach()