project.cmake 23 KB

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