build.cmake 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. # idf_build_get_property
  2. #
  3. # @brief Retrieve the value of the specified property related to ESP-IDF build.
  4. #
  5. # @param[out] var the variable to store the value in
  6. # @param[in] property the property to get the value of
  7. #
  8. # @param[in, optional] GENERATOR_EXPRESSION (option) retrieve the generator expression for the property
  9. # instead of actual value
  10. function(idf_build_get_property var property)
  11. cmake_parse_arguments(_ "GENERATOR_EXPRESSION" "" "" ${ARGN})
  12. if(__GENERATOR_EXPRESSION)
  13. set(val "$<TARGET_PROPERTY:__idf_build_target,${property}>")
  14. else()
  15. get_property(val TARGET __idf_build_target PROPERTY ${property})
  16. endif()
  17. set(${var} ${val} PARENT_SCOPE)
  18. endfunction()
  19. # idf_build_set_property
  20. #
  21. # @brief Set the value of the specified property related to ESP-IDF build. The property is
  22. # also added to the internal list of build properties if it isn't there already.
  23. #
  24. # @param[in] property the property to set the value of
  25. # @param[out] value value of the property
  26. #
  27. # @param[in, optional] APPEND (option) append the value to the current value of the
  28. # property instead of replacing it
  29. function(idf_build_set_property property value)
  30. cmake_parse_arguments(_ "APPEND" "" "" ${ARGN})
  31. if(__APPEND)
  32. set_property(TARGET __idf_build_target APPEND PROPERTY ${property} ${value})
  33. else()
  34. set_property(TARGET __idf_build_target PROPERTY ${property} ${value})
  35. endif()
  36. # Keep track of set build properties so that they can be exported to a file that
  37. # will be included in early expansion script.
  38. idf_build_get_property(build_properties __BUILD_PROPERTIES)
  39. if(NOT property IN_LIST build_properties)
  40. idf_build_set_property(__BUILD_PROPERTIES "${property}" APPEND)
  41. endif()
  42. endfunction()
  43. # idf_build_unset_property
  44. #
  45. # @brief Unset the value of the specified property related to ESP-IDF build. Equivalent
  46. # to setting the property to an empty string; though it also removes the property
  47. # from the internal list of build properties.
  48. #
  49. # @param[in] property the property to unset the value of
  50. function(idf_build_unset_property property)
  51. idf_build_set_property(${property} "") # set to an empty value
  52. idf_build_get_property(build_properties __BUILD_PROPERTIES) # remove from tracked properties
  53. list(REMOVE_ITEM build_properties ${property})
  54. idf_build_set_property(__BUILD_PROPERTIES "${build_properties}")
  55. endfunction()
  56. #
  57. # Retrieve the IDF_PATH repository's version, either using a version
  58. # file or Git revision. Sets the IDF_VER build property.
  59. #
  60. function(__build_get_idf_git_revision)
  61. idf_build_get_property(idf_path IDF_PATH)
  62. git_describe(idf_ver_git "${idf_path}" "--match=v*.*")
  63. if(EXISTS "${idf_path}/version.txt")
  64. file(STRINGS "${idf_path}/version.txt" idf_ver_t)
  65. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${idf_path}/version.txt")
  66. else()
  67. set(idf_ver_t ${idf_ver_git})
  68. endif()
  69. # cut IDF_VER to required 32 characters.
  70. string(SUBSTRING "${idf_ver_t}" 0 31 idf_ver)
  71. idf_build_set_property(COMPILE_DEFINITIONS "-DIDF_VER=\"${idf_ver}\"" APPEND)
  72. git_submodule_check("${idf_path}")
  73. idf_build_set_property(IDF_VER ${idf_ver})
  74. endfunction()
  75. #
  76. # Sets initial list of build specifications (compile options, definitions, etc.) common across
  77. # all library targets built under the ESP-IDF build system. These build specifications are added
  78. # privately using the directory-level CMake commands (add_compile_options, include_directories, etc.)
  79. # during component registration.
  80. #
  81. function(__build_set_default_build_specifications)
  82. unset(compile_definitions)
  83. unset(compile_options)
  84. unset(c_compile_options)
  85. unset(cxx_compile_options)
  86. list(APPEND compile_definitions "-D_GNU_SOURCE")
  87. list(APPEND compile_options "-ffunction-sections"
  88. "-fdata-sections"
  89. # warning-related flags
  90. "-Wall"
  91. "-Werror=all"
  92. "-Wno-error=unused-function"
  93. "-Wno-error=unused-variable"
  94. "-Wno-error=deprecated-declarations"
  95. "-Wextra"
  96. "-Wno-unused-parameter"
  97. "-Wno-sign-compare"
  98. # ignore multiple enum conversion warnings since gcc 11
  99. # TODO: IDF-5163
  100. "-Wno-enum-conversion"
  101. # Default is dwarf-5 since GCC 11, fallback to dwarf-4 because of binary size
  102. # TODO: IDF-5160
  103. "-gdwarf-4"
  104. # always generate debug symbols (even in release mode, these don't
  105. # go into the final binary so have no impact on size
  106. "-ggdb")
  107. idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
  108. idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND)
  109. idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND)
  110. idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND)
  111. endfunction()
  112. function(__build_set_lang_version)
  113. if(NOT IDF_TARGET STREQUAL "linux")
  114. # Building for chip targets: we use a known version of the toolchain.
  115. # Use latest supported versions.
  116. set(c_std gnu17)
  117. set(cxx_std gnu++20)
  118. else()
  119. enable_language(C CXX)
  120. # Building for Linux target, fall back to an older version of the standard
  121. # if the preferred one is not supported by the compiler.
  122. set(preferred_c_versions gnu17 gnu11 gnu99)
  123. set(ver_found FALSE)
  124. foreach(c_version ${preferred_c_versions})
  125. check_c_compiler_flag("-std=${c_version}" ver_${c_version}_supported)
  126. if(ver_${c_version}_supported)
  127. set(c_std ${c_version})
  128. set(ver_found TRUE)
  129. break()
  130. endif()
  131. endforeach()
  132. if(NOT ver_found)
  133. message(FATAL_ERROR "Failed to set C language standard to one of the supported versions: "
  134. "${preferred_c_versions}. Please upgrade the host compiler.")
  135. endif()
  136. set(preferred_cxx_versions gnu++20 gnu++2a gnu++17 gnu++14)
  137. set(ver_found FALSE)
  138. foreach(cxx_version ${preferred_cxx_versions})
  139. check_cxx_compiler_flag("-std=${cxx_version}" ver_${cxx_version}_supported)
  140. if(ver_${cxx_version}_supported)
  141. set(cxx_std ${cxx_version})
  142. set(ver_found TRUE)
  143. break()
  144. endif()
  145. endforeach()
  146. if(NOT ver_found)
  147. message(FATAL_ERROR "Failed to set C++ language standard to one of the supported versions: "
  148. "${preferred_cxx_versions}. Please upgrade the host compiler.")
  149. endif()
  150. endif()
  151. idf_build_set_property(C_COMPILE_OPTIONS "-std=${c_std}" APPEND)
  152. idf_build_set_property(CXX_COMPILE_OPTIONS "-std=${cxx_std}" APPEND)
  153. endfunction()
  154. #
  155. # Initialize the build. This gets called upon inclusion of idf.cmake to set internal
  156. # properties used for the processing phase of the build.
  157. #
  158. function(__build_init idf_path)
  159. set(target ${IDF_TARGET})
  160. # Create the build target, to which the ESP-IDF build properties, dependencies are attached to.
  161. # Must be global so as to be accessible from any subdirectory in custom projects.
  162. add_library(__idf_build_target STATIC IMPORTED GLOBAL)
  163. # Set the Python path (which may be passed in via -DPYTHON=) and store in a build property
  164. set_default(PYTHON "python")
  165. file(TO_CMAKE_PATH ${PYTHON} PYTHON)
  166. idf_build_set_property(PYTHON ${PYTHON})
  167. idf_build_set_property(IDF_PATH ${idf_path})
  168. idf_build_set_property(__PREFIX idf)
  169. idf_build_set_property(__CHECK_PYTHON 1)
  170. idf_build_set_property(IDF_COMPONENT_MANAGER 0)
  171. __build_set_default_build_specifications()
  172. __build_set_lang_version()
  173. # Add internal components to the build
  174. idf_build_get_property(idf_path IDF_PATH)
  175. idf_build_get_property(prefix __PREFIX)
  176. file(GLOB component_dirs ${idf_path}/components/*)
  177. list(SORT component_dirs)
  178. foreach(component_dir ${component_dirs})
  179. # A potential component must be a directory
  180. if(IS_DIRECTORY ${component_dir})
  181. __component_dir_quick_check(is_component ${component_dir})
  182. if(is_component)
  183. __component_add(${component_dir} ${prefix})
  184. endif()
  185. endif()
  186. endforeach()
  187. if("${target}" STREQUAL "linux")
  188. set(requires_common freertos log esp_rom esp_common linux)
  189. idf_build_set_property(__COMPONENT_REQUIRES_COMMON "${requires_common}")
  190. else()
  191. # Set components required by all other components in the build
  192. #
  193. # - esp_hw_support is here for backward compatibility
  194. set(requires_common cxx newlib freertos esp_hw_support heap log soc hal esp_rom esp_common esp_system)
  195. idf_build_set_property(__COMPONENT_REQUIRES_COMMON "${requires_common}")
  196. endif()
  197. __build_get_idf_git_revision()
  198. __kconfig_init()
  199. endfunction()
  200. # idf_build_component
  201. #
  202. # @brief Present a directory that contains a component to the build system.
  203. # Relative paths are converted to absolute paths with respect to current directory.
  204. # All calls to this command must be performed before idf_build_process.
  205. #
  206. # @note This command does not guarantee that the component will be processed
  207. # during build (see the COMPONENTS argument description for command idf_build_process)
  208. #
  209. # @param[in] component_dir directory of the component
  210. function(idf_build_component component_dir)
  211. idf_build_get_property(prefix __PREFIX)
  212. __component_add(${component_dir} ${prefix} 0)
  213. endfunction()
  214. #
  215. # Resolve the requirement component to the component target created for that component.
  216. #
  217. function(__build_resolve_and_add_req var component_target req type)
  218. __component_get_target(_component_target ${req})
  219. __component_get_property(_component_registered ${component_target} __COMPONENT_REGISTERED)
  220. if(NOT _component_target OR NOT _component_registered)
  221. message(FATAL_ERROR "Failed to resolve component '${req}'.")
  222. endif()
  223. __component_set_property(${component_target} ${type} ${_component_target} APPEND)
  224. set(${var} ${_component_target} PARENT_SCOPE)
  225. endfunction()
  226. #
  227. # Build a list of components (in the form of component targets) to be added to the build
  228. # based on public and private requirements. This list is saved in an internal property,
  229. # __BUILD_COMPONENT_TARGETS.
  230. #
  231. function(__build_expand_requirements component_target)
  232. # Since there are circular dependencies, make sure that we do not infinitely
  233. # expand requirements for each component.
  234. idf_build_get_property(component_targets_seen __COMPONENT_TARGETS_SEEN)
  235. __component_get_property(component_registered ${component_target} __COMPONENT_REGISTERED)
  236. if(component_target IN_LIST component_targets_seen OR NOT component_registered)
  237. return()
  238. endif()
  239. idf_build_set_property(__COMPONENT_TARGETS_SEEN ${component_target} APPEND)
  240. get_property(reqs TARGET ${component_target} PROPERTY REQUIRES)
  241. get_property(priv_reqs TARGET ${component_target} PROPERTY PRIV_REQUIRES)
  242. __component_get_property(component_name ${component_target} COMPONENT_NAME)
  243. __component_get_property(component_alias ${component_target} COMPONENT_ALIAS)
  244. idf_build_get_property(common_reqs __COMPONENT_REQUIRES_COMMON)
  245. list(APPEND reqs ${common_reqs})
  246. if(reqs)
  247. list(REMOVE_DUPLICATES reqs)
  248. list(REMOVE_ITEM reqs ${component_alias} ${component_name})
  249. endif()
  250. foreach(req ${reqs})
  251. depgraph_add_edge(${component_name} ${req} REQUIRES)
  252. __build_resolve_and_add_req(_component_target ${component_target} ${req} __REQUIRES)
  253. __build_expand_requirements(${_component_target})
  254. endforeach()
  255. foreach(req ${priv_reqs})
  256. depgraph_add_edge(${component_name} ${req} PRIV_REQUIRES)
  257. __build_resolve_and_add_req(_component_target ${component_target} ${req} __PRIV_REQUIRES)
  258. __build_expand_requirements(${_component_target})
  259. endforeach()
  260. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  261. if(NOT component_target IN_LIST build_component_targets)
  262. idf_build_set_property(__BUILD_COMPONENT_TARGETS ${component_target} APPEND)
  263. __component_get_property(component_lib ${component_target} COMPONENT_LIB)
  264. idf_build_set_property(__BUILD_COMPONENTS ${component_lib} APPEND)
  265. idf_build_get_property(prefix __PREFIX)
  266. __component_get_property(component_prefix ${component_target} __PREFIX)
  267. __component_get_property(component_alias ${component_target} COMPONENT_ALIAS)
  268. idf_build_set_property(BUILD_COMPONENT_ALIASES ${component_alias} APPEND)
  269. # Only put in the prefix in the name if it is not the default one
  270. if(component_prefix STREQUAL prefix)
  271. __component_get_property(component_name ${component_target} COMPONENT_NAME)
  272. idf_build_set_property(BUILD_COMPONENTS ${component_name} APPEND)
  273. else()
  274. idf_build_set_property(BUILD_COMPONENTS ${component_alias} APPEND)
  275. endif()
  276. endif()
  277. endfunction()
  278. #
  279. # Write a CMake file containing set build properties, owing to the fact that an internal
  280. # list of properties is maintained in idf_build_set_property() call. This is used to convert
  281. # those set properties to variables in the scope the output file is included in.
  282. #
  283. function(__build_write_properties output_file)
  284. idf_build_get_property(build_properties __BUILD_PROPERTIES)
  285. foreach(property ${build_properties})
  286. idf_build_get_property(val ${property})
  287. set(build_properties_text "${build_properties_text}\nset(${property} \"${val}\")")
  288. endforeach()
  289. file(WRITE ${output_file} "${build_properties_text}")
  290. endfunction()
  291. #
  292. # Check if the Python interpreter used for the build has all the required modules.
  293. #
  294. function(__build_check_python)
  295. idf_build_get_property(check __CHECK_PYTHON)
  296. if(check)
  297. idf_build_get_property(python PYTHON)
  298. idf_build_get_property(idf_path IDF_PATH)
  299. message(STATUS "Checking Python dependencies...")
  300. execute_process(COMMAND "${python}" "${idf_path}/tools/idf_tools.py" "check-python-dependencies"
  301. RESULT_VARIABLE result)
  302. if(result EQUAL 1)
  303. # check_python_dependencies returns error code 1 on failure
  304. message(FATAL_ERROR "Some Python dependencies must be installed. Check above message for details.")
  305. elseif(NOT result EQUAL 0)
  306. # means check_python_dependencies.py failed to run at all, result should be an error message
  307. message(FATAL_ERROR "Failed to run Python dependency check. Python: ${python}, Error: ${result}")
  308. endif()
  309. endif()
  310. endfunction()
  311. #
  312. # Prepare for component processing expanding each component's project include
  313. #
  314. macro(__build_process_project_includes)
  315. # Include the sdkconfig cmake file, since the following operations require
  316. # knowledge of config values.
  317. idf_build_get_property(sdkconfig_cmake SDKCONFIG_CMAKE)
  318. include(${sdkconfig_cmake})
  319. # Make each build property available as a read-only variable
  320. idf_build_get_property(build_properties __BUILD_PROPERTIES)
  321. foreach(build_property ${build_properties})
  322. idf_build_get_property(val ${build_property})
  323. set(${build_property} "${val}")
  324. endforeach()
  325. # Check that the CMake target value matches the Kconfig target value.
  326. __target_check()
  327. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  328. # Include each component's project_include.cmake
  329. foreach(component_target ${build_component_targets})
  330. __component_get_property(dir ${component_target} COMPONENT_DIR)
  331. __component_get_property(_name ${component_target} COMPONENT_NAME)
  332. set(COMPONENT_NAME ${_name})
  333. set(COMPONENT_DIR ${dir})
  334. set(COMPONENT_PATH ${dir}) # this is deprecated, users are encouraged to use COMPONENT_DIR;
  335. # retained for compatibility
  336. if(EXISTS ${COMPONENT_DIR}/project_include.cmake)
  337. include(${COMPONENT_DIR}/project_include.cmake)
  338. endif()
  339. endforeach()
  340. endmacro()
  341. #
  342. # Utility macro for setting default property value if argument is not specified
  343. # for idf_build_process().
  344. #
  345. macro(__build_set_default var default)
  346. set(_var __${var})
  347. if(NOT "${${_var}}" STREQUAL "")
  348. idf_build_set_property(${var} "${${_var}}")
  349. else()
  350. idf_build_set_property(${var} "${default}")
  351. endif()
  352. unset(_var)
  353. endmacro()
  354. #
  355. # Import configs as build instance properties so that they are accessible
  356. # using idf_build_get_config(). Config has to have been generated before calling
  357. # this command.
  358. #
  359. function(__build_import_configs)
  360. # Include the sdkconfig cmake file, since the following operations require
  361. # knowledge of config values.
  362. idf_build_get_property(sdkconfig_cmake SDKCONFIG_CMAKE)
  363. include(${sdkconfig_cmake})
  364. idf_build_set_property(__CONFIG_VARIABLES "${CONFIGS_LIST}")
  365. foreach(config ${CONFIGS_LIST})
  366. set_property(TARGET __idf_build_target PROPERTY ${config} "${${config}}")
  367. endforeach()
  368. endfunction()
  369. # idf_build_process
  370. #
  371. # @brief Main processing step for ESP-IDF build: config generation, adding components to the build,
  372. # dependency resolution, etc.
  373. #
  374. # @param[in] target ESP-IDF target
  375. #
  376. # @param[in, optional] PROJECT_DIR (single value) directory of the main project the buildsystem
  377. # is processed for; defaults to CMAKE_SOURCE_DIR
  378. # @param[in, optional] PROJECT_VER (single value) version string of the main project; defaults
  379. # to 1
  380. # @param[in, optional] PROJECT_NAME (single value) main project name, defaults to CMAKE_PROJECT_NAME
  381. # @param[in, optional] SDKCONFIG (single value) sdkconfig output path, defaults to PROJECT_DIR/sdkconfig
  382. # if PROJECT_DIR is set and CMAKE_SOURCE_DIR/sdkconfig if not
  383. # @param[in, optional] SDKCONFIG_DEFAULTS (single value) config defaults file to use for the build; defaults
  384. # to none (Kconfig defaults or previously generated config are used)
  385. # @param[in, optional] BUILD_DIR (single value) directory for build artifacts; defautls to CMAKE_BINARY_DIR
  386. # @param[in, optional] COMPONENTS (multivalue) select components to process among the components
  387. # known by the build system
  388. # (added via `idf_build_component`). This argument is used to trim the build.
  389. # Other components are automatically added if they are required
  390. # in the dependency chain, i.e.
  391. # the public and private requirements of the components in this list
  392. # are automatically added, and in
  393. # turn the public and private requirements of those requirements,
  394. # so on and so forth. If not specified, all components known to the build system
  395. # are processed.
  396. macro(idf_build_process target)
  397. set(options)
  398. set(single_value PROJECT_DIR PROJECT_VER PROJECT_NAME BUILD_DIR SDKCONFIG)
  399. set(multi_value COMPONENTS SDKCONFIG_DEFAULTS)
  400. cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" ${ARGN})
  401. idf_build_set_property(BOOTLOADER_BUILD "${BOOTLOADER_BUILD}")
  402. # Check build target is specified. Since this target corresponds to a component
  403. # name, the target component is automatically added to the list of common component
  404. # requirements.
  405. if(target STREQUAL "")
  406. message(FATAL_ERROR "Build target not specified.")
  407. endif()
  408. idf_build_set_property(IDF_TARGET ${target})
  409. if("${target}" STREQUAL "esp32" OR "${target}" STREQUAL "esp32s2" OR "${target}" STREQUAL "esp32s3")
  410. idf_build_set_property(IDF_TARGET_ARCH "xtensa")
  411. elseif("${target}" STREQUAL "linux")
  412. # No arch specified for linux host builds at the moment
  413. idf_build_set_property(IDF_TARGET_ARCH "")
  414. else()
  415. idf_build_set_property(IDF_TARGET_ARCH "riscv")
  416. endif()
  417. __build_set_default(PROJECT_DIR ${CMAKE_SOURCE_DIR})
  418. __build_set_default(PROJECT_NAME ${CMAKE_PROJECT_NAME})
  419. __build_set_default(PROJECT_VER 1)
  420. __build_set_default(BUILD_DIR ${CMAKE_BINARY_DIR})
  421. idf_build_get_property(project_dir PROJECT_DIR)
  422. __build_set_default(SDKCONFIG "${project_dir}/sdkconfig")
  423. __build_set_default(SDKCONFIG_DEFAULTS "")
  424. # Check for required Python modules
  425. __build_check_python()
  426. idf_build_get_property(target IDF_TARGET)
  427. idf_build_get_property(arch IDF_TARGET_ARCH)
  428. if(NOT "${target}" STREQUAL "linux")
  429. idf_build_set_property(__COMPONENT_REQUIRES_COMMON ${arch} APPEND)
  430. endif()
  431. # Call for component manager to download dependencies for all components
  432. idf_build_get_property(idf_component_manager IDF_COMPONENT_MANAGER)
  433. if(idf_component_manager EQUAL 1)
  434. idf_build_get_property(build_dir BUILD_DIR)
  435. set(managed_components_list_file ${build_dir}/managed_components_list.temp.cmake)
  436. set(local_components_list_file ${build_dir}/local_components_list.temp.yml)
  437. set(__contents "components:\n")
  438. foreach(__component_name ${components})
  439. idf_component_get_property(__component_dir ${__component_name} COMPONENT_DIR)
  440. set(__contents "${__contents} - name: \"${__component_name}\"\n path: \"${__component_dir}\"\n")
  441. endforeach()
  442. file(WRITE ${local_components_list_file} "${__contents}")
  443. # Call for the component manager to prepare remote dependencies
  444. idf_build_get_property(python PYTHON)
  445. idf_build_get_property(component_manager_interface_version __COMPONENT_MANAGER_INTERFACE_VERSION)
  446. execute_process(COMMAND ${python}
  447. "-m"
  448. "idf_component_manager.prepare_components"
  449. "--project_dir=${project_dir}"
  450. "--interface_version=${component_manager_interface_version}"
  451. "prepare_dependencies"
  452. "--local_components_list_file=${local_components_list_file}"
  453. "--managed_components_list_file=${managed_components_list_file}"
  454. RESULT_VARIABLE result
  455. ERROR_VARIABLE error)
  456. if(NOT result EQUAL 0)
  457. message(FATAL_ERROR "${error}")
  458. endif()
  459. include(${managed_components_list_file})
  460. # Add managed components to list of all components
  461. # `managed_components` contains the list of components installed by the component manager
  462. # It is defined in the temporary managed_components_list_file file
  463. set(__COMPONENTS "${__COMPONENTS};${managed_components}")
  464. file(REMOVE ${managed_components_list_file})
  465. file(REMOVE ${local_components_list_file})
  466. else()
  467. message(VERBOSE "IDF Component manager was explicitly disabled by setting IDF_COMPONENT_MANAGER=0")
  468. idf_build_get_property(__component_targets __COMPONENT_TARGETS)
  469. set(__components_with_manifests "")
  470. foreach(__component_target ${__component_targets})
  471. __component_get_property(__component_dir ${__component_target} COMPONENT_DIR)
  472. if(EXISTS "${__component_dir}/idf_component.yml")
  473. set(__components_with_manifests "${__components_with_manifests}\t${__component_dir}\n")
  474. endif()
  475. endforeach()
  476. if(NOT "${__components_with_manifests}" STREQUAL "")
  477. message(WARNING "\"idf_component.yml\" file was found for components:\n${__components_with_manifests}"
  478. "However, the component manager is not enabled.")
  479. endif()
  480. endif()
  481. # Perform early expansion of component CMakeLists.txt in CMake scripting mode.
  482. # It is here we retrieve the public and private requirements of each component.
  483. # It is also here we add the common component requirements to each component's
  484. # own requirements.
  485. __component_get_requirements()
  486. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  487. # Finally, do component expansion. In this case it simply means getting a final list
  488. # of build component targets given the requirements set by each component.
  489. # Check if we need to trim the components first, and build initial components list
  490. # from that.
  491. if(__COMPONENTS)
  492. unset(component_targets)
  493. foreach(component ${__COMPONENTS})
  494. __component_get_target(component_target ${component})
  495. if(NOT component_target)
  496. message(FATAL_ERROR "Failed to resolve component '${component}'.")
  497. endif()
  498. list(APPEND component_targets ${component_target})
  499. endforeach()
  500. endif()
  501. foreach(component_target ${component_targets})
  502. __build_expand_requirements(${component_target})
  503. endforeach()
  504. idf_build_unset_property(__COMPONENT_TARGETS_SEEN)
  505. # Get a list of common component requirements in component targets form (previously
  506. # we just have a list of component names)
  507. idf_build_get_property(common_reqs __COMPONENT_REQUIRES_COMMON)
  508. foreach(common_req ${common_reqs})
  509. __component_get_target(component_target ${common_req})
  510. __component_get_property(lib ${component_target} COMPONENT_LIB)
  511. idf_build_set_property(___COMPONENT_REQUIRES_COMMON ${lib} APPEND)
  512. endforeach()
  513. # Generate config values in different formats
  514. idf_build_get_property(sdkconfig SDKCONFIG)
  515. idf_build_get_property(sdkconfig_defaults SDKCONFIG_DEFAULTS)
  516. __kconfig_generate_config("${sdkconfig}" "${sdkconfig_defaults}")
  517. __build_import_configs()
  518. # All targets built under this scope is with the ESP-IDF build system
  519. set(ESP_PLATFORM 1)
  520. idf_build_set_property(COMPILE_DEFINITIONS "-DESP_PLATFORM" APPEND)
  521. # Perform component processing (inclusion of project_include.cmake, adding component
  522. # subdirectories, creating library targets, linking libraries, etc.)
  523. __build_process_project_includes()
  524. idf_build_get_property(idf_path IDF_PATH)
  525. add_subdirectory(${idf_path} ${build_dir}/esp-idf)
  526. unset(ESP_PLATFORM)
  527. endmacro()
  528. # idf_build_executable
  529. #
  530. # @brief Specify the executable the build system can attach dependencies to (for generating
  531. # files used for linking, targets which should execute before creating the specified executable,
  532. # generating additional binary files, generating files related to flashing, etc.)
  533. function(idf_build_executable elf)
  534. # Set additional link flags for the executable
  535. idf_build_get_property(link_options LINK_OPTIONS)
  536. # Using LINK_LIBRARIES here instead of LINK_OPTIONS, as the latter is not in CMake 3.5.
  537. set_property(TARGET ${elf} APPEND PROPERTY LINK_LIBRARIES "${link_options}")
  538. # Propagate link dependencies from component library targets to the executable
  539. idf_build_get_property(link_depends __LINK_DEPENDS)
  540. set_property(TARGET ${elf} APPEND PROPERTY LINK_DEPENDS "${link_depends}")
  541. # Set the EXECUTABLE_NAME and EXECUTABLE properties since there are generator expression
  542. # from components that depend on it
  543. get_filename_component(elf_name ${elf} NAME_WE)
  544. get_target_property(elf_dir ${elf} BINARY_DIR)
  545. idf_build_set_property(EXECUTABLE_NAME ${elf_name})
  546. idf_build_set_property(EXECUTABLE ${elf})
  547. idf_build_set_property(EXECUTABLE_DIR "${elf_dir}")
  548. # Add dependency of the build target to the executable
  549. add_dependencies(${elf} __idf_build_target)
  550. endfunction()
  551. # idf_build_get_config
  552. #
  553. # @brief Get value of specified config variable
  554. function(idf_build_get_config var config)
  555. cmake_parse_arguments(_ "GENERATOR_EXPRESSION" "" "" ${ARGN})
  556. if(__GENERATOR_EXPRESSION)
  557. set(val "$<TARGET_PROPERTY:__idf_build_target,${config}>")
  558. else()
  559. get_property(val TARGET __idf_build_target PROPERTY ${config})
  560. endif()
  561. set(${var} ${val} PARENT_SCOPE)
  562. endfunction()