project.cmake 24 KB

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