project.cmake 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. # Designed to be included from an IDF app's CMakeLists.txt file
  2. cmake_minimum_required(VERSION 3.16)
  3. # Get the currently selected sdkconfig file early, so this doesn't
  4. # have to be done multiple times on different places.
  5. if(SDKCONFIG)
  6. get_filename_component(sdkconfig "${SDKCONFIG}" ABSOLUTE)
  7. else()
  8. set(sdkconfig "${CMAKE_SOURCE_DIR}/sdkconfig")
  9. endif()
  10. # Check if the cmake was started as part of the set-target action.
  11. # If so, check for existing sdkconfig file and rename it.
  12. # This is done before __target_init, so the existing IDF_TARGET from sdkconfig
  13. # is not considered for consistence checking.
  14. if("$ENV{_IDF_PY_SET_TARGET_ACTION}" EQUAL "1" AND EXISTS "${sdkconfig}")
  15. file(RENAME "${sdkconfig}" "${sdkconfig}.old")
  16. message(STATUS "Existing sdkconfig '${sdkconfig}' renamed to '${sdkconfig}.old'.")
  17. endif()
  18. include(${CMAKE_CURRENT_LIST_DIR}/targets.cmake)
  19. # Initialize build target for this build using the environment variable or
  20. # value passed externally.
  21. __target_init("${sdkconfig}")
  22. # The mere inclusion of this CMake file sets up some internal build properties.
  23. # These properties can be modified in between this inclusion the the idf_build_process
  24. # call.
  25. include(${CMAKE_CURRENT_LIST_DIR}/idf.cmake)
  26. # setting PYTHON variable here for compatibility only, new code should use
  27. # idf_build_get_property(variable PYTHON)
  28. idf_build_get_property(PYTHON PYTHON)
  29. if(NOT PYTHON)
  30. message(FATAL_ERROR "Internal error, PYTHON build property not set correctly.")
  31. endif()
  32. # legacy variable for compatibility
  33. set(IDFTOOL ${PYTHON} "${IDF_PATH}/tools/idf.py")
  34. # On processing, checking Python required modules can be turned off if it was
  35. # already checked externally.
  36. if(PYTHON_DEPS_CHECKED)
  37. idf_build_set_property(__CHECK_PYTHON 0)
  38. endif()
  39. # Store CMake arguments that need to be passed into all CMake sub-projects as well
  40. # (bootloader, ULP, etc)
  41. #
  42. # It's not possible to tell if CMake was called with --warn-uninitialized, so to also
  43. # have these warnings in sub-projects we set a cache variable as well and then check that.
  44. if(WARN_UNINITIALIZED)
  45. idf_build_set_property(EXTRA_CMAKE_ARGS --warn-uninitialized)
  46. else()
  47. idf_build_set_property(EXTRA_CMAKE_ARGS "")
  48. endif()
  49. # Enable the component manager for regular projects if not explicitly disabled.
  50. if(NOT "$ENV{IDF_COMPONENT_MANAGER}" EQUAL "0")
  51. idf_build_set_property(IDF_COMPONENT_MANAGER 1)
  52. endif()
  53. # Set component manager interface version
  54. idf_build_set_property(__COMPONENT_MANAGER_INTERFACE_VERSION 2)
  55. #
  56. # Get the project version from either a version file or the Git revision. This is passed
  57. # to the idf_build_process call. Dependencies are also set here for when the version file
  58. # changes (if it is used).
  59. #
  60. function(__project_get_revision var)
  61. set(_project_path "${CMAKE_CURRENT_LIST_DIR}")
  62. if(NOT DEFINED PROJECT_VER)
  63. if(EXISTS "${_project_path}/version.txt")
  64. file(STRINGS "${_project_path}/version.txt" PROJECT_VER)
  65. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${_project_path}/version.txt")
  66. else()
  67. git_describe(PROJECT_VER_GIT "${_project_path}")
  68. if(PROJECT_VER_GIT)
  69. set(PROJECT_VER ${PROJECT_VER_GIT})
  70. else()
  71. message(STATUS "Project is not inside a git repository, or git repository has no commits;"
  72. " will not use 'git describe' to determine PROJECT_VER.")
  73. set(PROJECT_VER 1)
  74. endif()
  75. endif()
  76. endif()
  77. set(${var} "${PROJECT_VER}" PARENT_SCOPE)
  78. endfunction()
  79. # paths_with_spaces_to_list
  80. #
  81. # Replacement for spaces2list in cases where it was previously used on
  82. # directory lists.
  83. #
  84. # If the variable doesn't contain spaces, (e.g. is already a CMake list)
  85. # then the variable is unchanged. Otherwise an external Python script is called
  86. # to try to split the paths, and the variable is updated with the result.
  87. #
  88. # This feature is added only for compatibility. Please do not introduce new
  89. # space separated path lists.
  90. #
  91. function(paths_with_spaces_to_list variable_name)
  92. if("${${variable_name}}" MATCHES "[ \t]")
  93. idf_build_get_property(python PYTHON)
  94. idf_build_get_property(idf_path IDF_PATH)
  95. execute_process(
  96. COMMAND ${python}
  97. "${idf_path}/tools/split_paths_by_spaces.py"
  98. "--var-name=${variable_name}"
  99. "${${variable_name}}"
  100. WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
  101. OUTPUT_VARIABLE result
  102. RESULT_VARIABLE ret)
  103. if(NOT ret EQUAL 0)
  104. message(FATAL_ERROR "Failed to parse ${variable_name}, see diagnostics above")
  105. endif()
  106. set("${variable_name}" "${result}" PARENT_SCOPE)
  107. endif()
  108. endfunction()
  109. #
  110. # Output the built components to the user. Generates files for invoking esp_idf_monitor
  111. # that doubles as an overview of some of the more important build properties.
  112. #
  113. function(__project_info test_components)
  114. idf_build_get_property(prefix __PREFIX)
  115. idf_build_get_property(_build_components BUILD_COMPONENTS)
  116. idf_build_get_property(build_dir BUILD_DIR)
  117. idf_build_get_property(idf_path IDF_PATH)
  118. list(SORT _build_components)
  119. unset(build_components)
  120. unset(build_component_paths)
  121. foreach(build_component ${_build_components})
  122. __component_get_target(component_target "${build_component}")
  123. __component_get_property(_name ${component_target} COMPONENT_NAME)
  124. __component_get_property(_prefix ${component_target} __PREFIX)
  125. __component_get_property(_alias ${component_target} COMPONENT_ALIAS)
  126. __component_get_property(_dir ${component_target} COMPONENT_DIR)
  127. if(_alias IN_LIST test_components)
  128. list(APPEND test_component_paths ${_dir})
  129. else()
  130. if(_prefix STREQUAL prefix)
  131. set(component ${_name})
  132. else()
  133. set(component ${_alias})
  134. endif()
  135. list(APPEND build_components ${component})
  136. list(APPEND build_component_paths ${_dir})
  137. endif()
  138. endforeach()
  139. set(PROJECT_NAME ${CMAKE_PROJECT_NAME})
  140. idf_build_get_property(PROJECT_PATH PROJECT_DIR)
  141. idf_build_get_property(BUILD_DIR BUILD_DIR)
  142. idf_build_get_property(SDKCONFIG SDKCONFIG)
  143. idf_build_get_property(SDKCONFIG_DEFAULTS SDKCONFIG_DEFAULTS)
  144. idf_build_get_property(PROJECT_EXECUTABLE EXECUTABLE)
  145. set(PROJECT_BIN ${CMAKE_PROJECT_NAME}.bin)
  146. idf_build_get_property(IDF_VER IDF_VER)
  147. idf_build_get_property(sdkconfig_cmake SDKCONFIG_CMAKE)
  148. include(${sdkconfig_cmake})
  149. idf_build_get_property(COMPONENT_KCONFIGS KCONFIGS)
  150. idf_build_get_property(COMPONENT_KCONFIGS_PROJBUILD KCONFIG_PROJBUILDS)
  151. idf_build_get_property(debug_prefix_map_gdbinit DEBUG_PREFIX_MAP_GDBINIT)
  152. if(CONFIG_APP_BUILD_TYPE_RAM)
  153. set(PROJECT_BUILD_TYPE ram_app)
  154. else()
  155. set(PROJECT_BUILD_TYPE flash_app)
  156. endif()
  157. # Write project description JSON file
  158. idf_build_get_property(build_dir BUILD_DIR)
  159. make_json_list("${build_components};${test_components}" build_components_json)
  160. make_json_list("${build_component_paths};${test_component_paths}" build_component_paths_json)
  161. configure_file("${idf_path}/tools/cmake/project_description.json.in"
  162. "${build_dir}/project_description.json")
  163. # Generate component dependency graph
  164. depgraph_generate("${build_dir}/component_deps.dot")
  165. # We now have the following component-related variables:
  166. #
  167. # build_components is the list of components to include in the build.
  168. # build_component_paths is the paths to all of these components, obtained from the component dependencies file.
  169. #
  170. # Print the list of found components and test components
  171. string(REPLACE ";" " " build_components "${build_components}")
  172. string(REPLACE ";" " " build_component_paths "${build_component_paths}")
  173. message(STATUS "Components: ${build_components}")
  174. message(STATUS "Component paths: ${build_component_paths}")
  175. if(test_components)
  176. string(REPLACE ";" " " test_components "${test_components}")
  177. string(REPLACE ";" " " test_component_paths "${test_component_paths}")
  178. message(STATUS "Test components: ${test_components}")
  179. message(STATUS "Test component paths: ${test_component_paths}")
  180. endif()
  181. endfunction()
  182. function(__project_init components_var test_components_var)
  183. # Use EXTRA_CFLAGS, EXTRA_CXXFLAGS and EXTRA_CPPFLAGS to add more priority options to the compiler
  184. # EXTRA_CPPFLAGS is used for both C and C++
  185. # Unlike environments' CFLAGS/CXXFLAGS/CPPFLAGS which work for both host and target build,
  186. # these works only for target build
  187. set(extra_cflags "$ENV{EXTRA_CFLAGS}")
  188. set(extra_cxxflags "$ENV{EXTRA_CXXFLAGS}")
  189. set(extra_cppflags "$ENV{EXTRA_CPPFLAGS}")
  190. spaces2list(extra_cflags)
  191. spaces2list(extra_cxxflags)
  192. spaces2list(extra_cppflags)
  193. idf_build_set_property(C_COMPILE_OPTIONS "${extra_cflags}" APPEND)
  194. idf_build_set_property(CXX_COMPILE_OPTIONS "${extra_cxxflags}" APPEND)
  195. idf_build_set_property(COMPILE_OPTIONS "${extra_cppflags}" APPEND)
  196. function(__project_component_dir component_dir)
  197. get_filename_component(component_dir "${component_dir}" ABSOLUTE)
  198. # The directory itself is a valid idf component
  199. if(EXISTS ${component_dir}/CMakeLists.txt)
  200. idf_build_component(${component_dir})
  201. else()
  202. # otherwise, check whether the subfolders are potential idf components
  203. file(GLOB component_dirs ${component_dir}/*)
  204. foreach(component_dir ${component_dirs})
  205. if(IS_DIRECTORY ${component_dir})
  206. __component_dir_quick_check(is_component ${component_dir})
  207. if(is_component)
  208. idf_build_component(${component_dir})
  209. endif()
  210. endif()
  211. endforeach()
  212. endif()
  213. endfunction()
  214. # Add component directories to the build, given the component filters, exclusions
  215. # extra directories, etc. passed from the root CMakeLists.txt.
  216. if(COMPONENT_DIRS)
  217. # User wants to fully override where components are pulled from.
  218. paths_with_spaces_to_list(COMPONENT_DIRS)
  219. idf_build_set_property(__COMPONENT_TARGETS "")
  220. foreach(component_dir ${COMPONENT_DIRS})
  221. get_filename_component(component_abs_path ${component_dir} ABSOLUTE)
  222. if(NOT EXISTS ${component_abs_path})
  223. message(FATAL_ERROR "Directory specified in COMPONENT_DIRS doesn't exist: ${component_abs_path}")
  224. endif()
  225. __project_component_dir(${component_dir})
  226. endforeach()
  227. else()
  228. if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/main")
  229. __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/main")
  230. endif()
  231. paths_with_spaces_to_list(EXTRA_COMPONENT_DIRS)
  232. foreach(component_dir ${EXTRA_COMPONENT_DIRS})
  233. get_filename_component(component_abs_path ${component_dir} ABSOLUTE)
  234. if(NOT EXISTS ${component_abs_path})
  235. message(FATAL_ERROR "Directory specified in EXTRA_COMPONENT_DIRS doesn't exist: ${component_abs_path}")
  236. endif()
  237. __project_component_dir("${component_dir}")
  238. endforeach()
  239. # Look for components in the usual places: CMAKE_CURRENT_LIST_DIR/main,
  240. # extra component dirs, and CMAKE_CURRENT_LIST_DIR/components
  241. __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/components")
  242. endif()
  243. # For bootloader components, we only need to set-up the Kconfig files.
  244. # Indeed, bootloader is currently compiled as a subproject, thus,
  245. # its components are not part of the main project.
  246. # However, in order to be able to configure these bootloader components
  247. # using menuconfig, we need to look for their Kconfig-related files now.
  248. file(GLOB bootloader_component_dirs "${CMAKE_CURRENT_LIST_DIR}/bootloader_components/*")
  249. list(SORT bootloader_component_dirs)
  250. foreach(bootloader_component_dir ${bootloader_component_dirs})
  251. if(IS_DIRECTORY ${bootloader_component_dir})
  252. __component_dir_quick_check(is_component ${bootloader_component_dir})
  253. if(is_component)
  254. __kconfig_bootloader_component_add("${bootloader_component_dir}")
  255. endif()
  256. endif()
  257. endforeach()
  258. spaces2list(COMPONENTS)
  259. spaces2list(EXCLUDE_COMPONENTS)
  260. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  261. foreach(component_target ${component_targets})
  262. __component_get_property(component_name ${component_target} COMPONENT_NAME)
  263. set(include 1)
  264. if(COMPONENTS AND NOT component_name IN_LIST COMPONENTS)
  265. set(include 0)
  266. endif()
  267. if(EXCLUDE_COMPONENTS AND component_name IN_LIST EXCLUDE_COMPONENTS)
  268. set(include 0)
  269. endif()
  270. if(include)
  271. list(APPEND components ${component_name})
  272. endif()
  273. endforeach()
  274. if(TESTS_ALL OR BUILD_TESTS OR TEST_COMPONENTS OR TEST_EXCLUDE_COMPONENTS)
  275. spaces2list(TEST_COMPONENTS)
  276. spaces2list(TEST_EXCLUDE_COMPONENTS)
  277. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  278. foreach(component_target ${component_targets})
  279. __component_get_property(component_dir ${component_target} COMPONENT_DIR)
  280. __component_get_property(component_name ${component_target} COMPONENT_NAME)
  281. if(component_name IN_LIST components)
  282. set(include 1)
  283. if(TEST_COMPONENTS AND NOT component_name IN_LIST TEST_COMPONENTS)
  284. set(include 0)
  285. endif()
  286. if(TEST_EXCLUDE_COMPONENTS AND component_name IN_LIST TEST_EXCLUDE_COMPONENTS)
  287. set(include 0)
  288. endif()
  289. if(include AND EXISTS ${component_dir}/test)
  290. __component_add(${component_dir}/test ${component_name})
  291. list(APPEND test_components ${component_name}::test)
  292. endif()
  293. endif()
  294. endforeach()
  295. endif()
  296. set(${components_var} "${components}" PARENT_SCOPE)
  297. set(${test_components_var} "${test_components}" PARENT_SCOPE)
  298. endfunction()
  299. # Trick to temporarily redefine project(). When functions are overridden in CMake, the originals can still be accessed
  300. # using an underscore prefixed function of the same name. The following lines make sure that __project calls
  301. # the original project(). See https://cmake.org/pipermail/cmake/2015-October/061751.html.
  302. function(project)
  303. endfunction()
  304. function(_project)
  305. endfunction()
  306. macro(project project_name)
  307. # Initialize project, preparing COMPONENTS argument for idf_build_process()
  308. # call later using external COMPONENT_DIRS, COMPONENTS_DIRS, EXTRA_COMPONENTS_DIR,
  309. # EXTRA_COMPONENTS_DIRS, COMPONENTS, EXLUDE_COMPONENTS, TEST_COMPONENTS,
  310. # TEST_EXLUDE_COMPONENTS, TESTS_ALL, BUILD_TESTS
  311. __project_init(components test_components)
  312. __target_set_toolchain()
  313. if(CCACHE_ENABLE)
  314. find_program(CCACHE_FOUND ccache)
  315. if(CCACHE_FOUND)
  316. message(STATUS "ccache will be used for faster recompilation")
  317. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
  318. else()
  319. message(WARNING "enabled ccache in build but ccache program not found")
  320. endif()
  321. endif()
  322. # The actual call to project()
  323. __project(${project_name} C CXX ASM)
  324. # Generate compile_commands.json (needs to come after project call).
  325. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  326. # If CMAKE_COLOR_DIAGNOSTICS not set in project CMakeLists.txt or in the environment,
  327. # enable it by default.
  328. if(NOT DEFINED CMAKE_COLOR_DIAGNOSTICS AND NOT DEFINED ENV{CMAKE_COLOR_DIAGNOSTICS})
  329. set(CMAKE_COLOR_DIAGNOSTICS ON)
  330. endif()
  331. # Since components can import third-party libraries, the original definition of project() should be restored
  332. # before the call to add components to the build.
  333. function(project)
  334. set(project_ARGV ARGV)
  335. __project(${${project_ARGV}})
  336. # Set the variables that project() normally sets, documented in the
  337. # command's docs.
  338. #
  339. # https://cmake.org/cmake/help/v3.16/command/project.html
  340. #
  341. # There is some nuance when it comes to setting version variables in terms of whether
  342. # CMP0048 is set to OLD or NEW. However, the proper behavior should have bee already handled by the original
  343. # project call, and we're just echoing the values those variables were set to.
  344. set(PROJECT_NAME "${PROJECT_NAME}" PARENT_SCOPE)
  345. set(PROJECT_BINARY_DIR "${PROJECT_BINARY_DIR}" PARENT_SCOPE)
  346. set(PROJECT_SOURCE_DIR "${PROJECT_SOURCE_DIR}" PARENT_SCOPE)
  347. set(PROJECT_VERSION "${PROJECT_VERSION}" PARENT_SCOPE)
  348. set(PROJECT_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}" PARENT_SCOPE)
  349. set(PROJECT_VERSION_MINOR "${PROJECT_VERSION_MINOR}" PARENT_SCOPE)
  350. set(PROJECT_VERSION_PATCH "${PROJECT_VERSION_PATCH}" PARENT_SCOPE)
  351. set(PROJECT_VERSION_TWEAK "${PROJECT_VERSION_TWEAK}" PARENT_SCOPE)
  352. set(PROJECT_DESCRIPTION "${PROJECT_DESCRIPTION}" PARENT_SCOPE)
  353. set(PROJECT_HOMEPAGE_URL "${PROJECT_HOMEPAGE_URL}" PARENT_SCOPE)
  354. set(${PROJECT_NAME}_BINARY_DIR "${${PROJECT_NAME}_BINARY_DIR}" PARENT_SCOPE)
  355. set(${PROJECT_NAME}_SOURCE_DIR "${${PROJECT_NAME}_SOURCE_DIR}" PARENT_SCOPE)
  356. set(${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION}" PARENT_SCOPE)
  357. set(${PROJECT_NAME}_VERSION_MAJOR "${${PROJECT_NAME}_VERSION_MAJOR}" PARENT_SCOPE)
  358. set(${PROJECT_NAME}_VERSION_MINOR "${${PROJECT_NAME}_VERSION_MINOR}" PARENT_SCOPE)
  359. set(${PROJECT_NAME}_VERSION_PATCH "${${PROJECT_NAME}_VERSION_PATCH}" PARENT_SCOPE)
  360. set(${PROJECT_NAME}_VERSION_TWEAK "${${PROJECT_NAME}_VERSION_TWEAK}" PARENT_SCOPE)
  361. set(${PROJECT_NAME}_DESCRIPTION "${${PROJECT_NAME}_DESCRIPTION}" PARENT_SCOPE)
  362. set(${PROJECT_NAME}_HOMEPAGE_URL "${${PROJECT_NAME}_HOMEPAGE_URL}" PARENT_SCOPE)
  363. endfunction()
  364. # Prepare the following arguments for the idf_build_process() call using external
  365. # user values:
  366. #
  367. # SDKCONFIG_DEFAULTS is from external SDKCONFIG_DEFAULTS
  368. # SDKCONFIG is from external SDKCONFIG
  369. # BUILD_DIR is set to project binary dir
  370. #
  371. # PROJECT_NAME is taken from the passed name from project() call
  372. # PROJECT_DIR is set to the current directory
  373. # PROJECT_VER is from the version text or git revision of the current repo
  374. # SDKCONFIG_DEFAULTS environment variable may specify a file name relative to the root of the project.
  375. # When building the bootloader, ignore this variable, since:
  376. # 1. The bootloader project uses an existing SDKCONFIG file from the top-level project
  377. # 2. File specified by SDKCONFIG_DEFAULTS will not be found relative to the root of the bootloader project
  378. if(NOT BOOTLOADER_BUILD)
  379. set(_sdkconfig_defaults "$ENV{SDKCONFIG_DEFAULTS}")
  380. endif()
  381. if(NOT _sdkconfig_defaults)
  382. if(EXISTS "${CMAKE_SOURCE_DIR}/sdkconfig.defaults")
  383. set(_sdkconfig_defaults "${CMAKE_SOURCE_DIR}/sdkconfig.defaults")
  384. else()
  385. set(_sdkconfig_defaults "")
  386. endif()
  387. endif()
  388. if(SDKCONFIG_DEFAULTS)
  389. set(_sdkconfig_defaults "${SDKCONFIG_DEFAULTS}")
  390. endif()
  391. foreach(sdkconfig_default ${_sdkconfig_defaults})
  392. get_filename_component(sdkconfig_default "${sdkconfig_default}" ABSOLUTE)
  393. if(NOT EXISTS "${sdkconfig_default}")
  394. message(FATAL_ERROR "SDKCONFIG_DEFAULTS '${sdkconfig_default}' does not exist.")
  395. endif()
  396. list(APPEND sdkconfig_defaults ${sdkconfig_default})
  397. endforeach()
  398. if(BUILD_DIR)
  399. get_filename_component(build_dir "${BUILD_DIR}" ABSOLUTE)
  400. if(NOT EXISTS "${build_dir}")
  401. message(FATAL_ERROR "BUILD_DIR '${build_dir}' does not exist.")
  402. endif()
  403. else()
  404. set(build_dir ${CMAKE_BINARY_DIR})
  405. endif()
  406. __project_get_revision(project_ver)
  407. message(STATUS "Building ESP-IDF components for target ${IDF_TARGET}")
  408. idf_build_process(${IDF_TARGET}
  409. SDKCONFIG_DEFAULTS "${sdkconfig_defaults}"
  410. SDKCONFIG ${sdkconfig}
  411. BUILD_DIR ${build_dir}
  412. PROJECT_NAME ${CMAKE_PROJECT_NAME}
  413. PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}
  414. PROJECT_VER "${project_ver}"
  415. COMPONENTS "${components};${test_components}")
  416. # Special treatment for 'main' component for standard projects (not part of core build system).
  417. # Have it depend on every other component in the build. This is
  418. # a convenience behavior for the standard project; thus is done outside of the core build system
  419. # so that it treats components equally.
  420. #
  421. # This behavior should only be when user did not set REQUIRES/PRIV_REQUIRES manually.
  422. idf_build_get_property(build_components BUILD_COMPONENT_ALIASES)
  423. if(idf::main IN_LIST build_components)
  424. __component_get_target(main_target idf::main)
  425. __component_get_property(reqs ${main_target} REQUIRES)
  426. __component_get_property(priv_reqs ${main_target} PRIV_REQUIRES)
  427. __component_get_property(managed_reqs ${main_target} MANAGED_REQUIRES)
  428. __component_get_property(managed_priv_reqs ${main_target} MANAGED_PRIV_REQUIRES)
  429. #if user has not set any requirements, except ones added with the component manager
  430. if((NOT reqs OR reqs STREQUAL managed_reqs) AND (NOT priv_reqs OR priv_reqs STREQUAL managed_priv_reqs))
  431. if(test_components)
  432. list(REMOVE_ITEM build_components ${test_components})
  433. endif()
  434. list(REMOVE_ITEM build_components idf::main)
  435. __component_get_property(lib ${main_target} COMPONENT_LIB)
  436. set_property(TARGET ${lib} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "${build_components}")
  437. get_property(type TARGET ${lib} PROPERTY TYPE)
  438. if(type STREQUAL STATIC_LIBRARY)
  439. set_property(TARGET ${lib} APPEND PROPERTY LINK_LIBRARIES "${build_components}")
  440. endif()
  441. endif()
  442. endif()
  443. set(project_elf ${CMAKE_PROJECT_NAME}.elf)
  444. # Create a dummy file to work around CMake requirement of having a source file while adding an
  445. # executable. This is also used by esp_idf_size to detect the target
  446. set(project_elf_src ${CMAKE_BINARY_DIR}/project_elf_src_${IDF_TARGET}.c)
  447. add_custom_command(OUTPUT ${project_elf_src}
  448. COMMAND ${CMAKE_COMMAND} -E touch ${project_elf_src}
  449. VERBATIM)
  450. add_custom_target(_project_elf_src DEPENDS "${project_elf_src}")
  451. add_executable(${project_elf} "${project_elf_src}")
  452. add_dependencies(${project_elf} _project_elf_src)
  453. if(__PROJECT_GROUP_LINK_COMPONENTS)
  454. target_link_libraries(${project_elf} PRIVATE "-Wl,--start-group")
  455. endif()
  456. if(test_components)
  457. target_link_libraries(${project_elf} PRIVATE "-Wl,--whole-archive")
  458. foreach(test_component ${test_components})
  459. if(TARGET ${test_component})
  460. target_link_libraries(${project_elf} PRIVATE ${test_component})
  461. endif()
  462. endforeach()
  463. target_link_libraries(${project_elf} PRIVATE "-Wl,--no-whole-archive")
  464. endif()
  465. idf_build_get_property(build_components BUILD_COMPONENT_ALIASES)
  466. if(test_components)
  467. list(REMOVE_ITEM build_components ${test_components})
  468. endif()
  469. if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
  470. # Compiling for the host, and the host is macOS, so the linker is Darwin LD.
  471. # Note, when adding support for Clang and LLD based toolchain this check will
  472. # need to be modified.
  473. set(linker_type "Darwin")
  474. else()
  475. set(linker_type "GNU")
  476. endif()
  477. foreach(build_component ${build_components})
  478. __component_get_target(build_component_target ${build_component})
  479. __component_get_property(whole_archive ${build_component_target} WHOLE_ARCHIVE)
  480. if(whole_archive)
  481. if(linker_type STREQUAL "GNU")
  482. message(STATUS "Component ${build_component} will be linked with -Wl,--whole-archive")
  483. target_link_libraries(${project_elf} PRIVATE
  484. "-Wl,--whole-archive"
  485. ${build_component}
  486. "-Wl,--no-whole-archive")
  487. elseif(linker_type STREQUAL "Darwin")
  488. message(STATUS "Component ${build_component} will be linked with -Wl,-force_load")
  489. target_link_libraries(${project_elf} PRIVATE
  490. "-Wl,-force_load"
  491. ${build_component})
  492. endif()
  493. else()
  494. target_link_libraries(${project_elf} PRIVATE ${build_component})
  495. endif()
  496. endforeach()
  497. if(linker_type STREQUAL "GNU")
  498. set(mapfile "${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.map")
  499. set(idf_target "${IDF_TARGET}")
  500. string(TOUPPER ${idf_target} idf_target)
  501. # Add cross-reference table to the map file
  502. target_link_options(${project_elf} PRIVATE "-Wl,--cref")
  503. # Add this symbol as a hint for esp_idf_size to guess the target name
  504. target_link_options(${project_elf} PRIVATE "-Wl,--defsym=IDF_TARGET_${idf_target}=0")
  505. # Enable map file output
  506. target_link_options(${project_elf} PRIVATE "-Wl,--Map=${mapfile}")
  507. # Check if linker supports --no-warn-rwx-segments
  508. execute_process(COMMAND ${CMAKE_LINKER} "--no-warn-rwx-segments" "--version"
  509. RESULT_VARIABLE result
  510. OUTPUT_QUIET
  511. ERROR_QUIET)
  512. if(${result} EQUAL 0)
  513. # Do not print RWX segment warnings
  514. target_link_options(${project_elf} PRIVATE "-Wl,--no-warn-rwx-segments")
  515. endif()
  516. unset(idf_target)
  517. endif()
  518. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  519. ADDITIONAL_MAKE_CLEAN_FILES
  520. "${mapfile}" "${project_elf_src}")
  521. idf_build_get_property(idf_path IDF_PATH)
  522. idf_build_get_property(python PYTHON)
  523. set(idf_size ${python} -m esp_idf_size)
  524. # Add size targets, depend on map file, run esp_idf_size
  525. # OUTPUT_JSON is passed for compatibility reasons, SIZE_OUTPUT_FORMAT
  526. # environment variable is recommended and has higher priority
  527. add_custom_target(size
  528. COMMAND ${CMAKE_COMMAND}
  529. -D "IDF_SIZE_TOOL=${idf_size}"
  530. -D "MAP_FILE=${mapfile}"
  531. -D "OUTPUT_JSON=${OUTPUT_JSON}"
  532. -P "${idf_path}/tools/cmake/run_size_tool.cmake"
  533. DEPENDS ${mapfile}
  534. USES_TERMINAL
  535. VERBATIM
  536. )
  537. add_custom_target(size-files
  538. COMMAND ${CMAKE_COMMAND}
  539. -D "IDF_SIZE_TOOL=${idf_size}"
  540. -D "IDF_SIZE_MODE=--files"
  541. -D "MAP_FILE=${mapfile}"
  542. -D "OUTPUT_JSON=${OUTPUT_JSON}"
  543. -P "${idf_path}/tools/cmake/run_size_tool.cmake"
  544. DEPENDS ${mapfile}
  545. USES_TERMINAL
  546. VERBATIM
  547. )
  548. add_custom_target(size-components
  549. COMMAND ${CMAKE_COMMAND}
  550. -D "IDF_SIZE_TOOL=${idf_size}"
  551. -D "IDF_SIZE_MODE=--archives"
  552. -D "MAP_FILE=${mapfile}"
  553. -D "OUTPUT_JSON=${OUTPUT_JSON}"
  554. -P "${idf_path}/tools/cmake/run_size_tool.cmake"
  555. DEPENDS ${mapfile}
  556. USES_TERMINAL
  557. VERBATIM
  558. )
  559. unset(idf_size)
  560. # Add DFU build and flash targets
  561. __add_dfu_targets()
  562. # Add UF2 build targets
  563. __add_uf2_targets()
  564. idf_build_executable(${project_elf})
  565. __project_info("${test_components}")
  566. endmacro()