project.cmake 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. # Designed to be included from an IDF app's CMakeLists.txt file
  2. cmake_minimum_required(VERSION 3.5)
  3. # The mere inclusion of this CMake file sets up some interal build properties.
  4. # These properties can be modified in between this inclusion the the idf_build_process
  5. # call.
  6. include(${CMAKE_CURRENT_LIST_DIR}/idf.cmake)
  7. set(IDFTOOL ${PYTHON} "${IDF_PATH}/tools/idf.py")
  8. # Internally, the Python interpreter is already set to 'python'. Re-set here
  9. # to be absolutely sure.
  10. set_default(PYTHON "python")
  11. idf_build_set_property(PYTHON ${PYTHON})
  12. # On processing, checking Python required modules can be turned off if it was
  13. # already checked externally.
  14. if(PYTHON_DEPS_CHECKED)
  15. idf_build_set_property(__CHECK_PYTHON 0)
  16. endif()
  17. # Initialize build target for this build using the environment variable or
  18. # value passed externally.
  19. __target_init()
  20. #
  21. # Get the project version from either a version file or the Git revision. This is passed
  22. # to the idf_build_process call. Dependencies are also set here for when the version file
  23. # changes (if it is used).
  24. #
  25. function(__project_get_revision var)
  26. set(_project_path "${CMAKE_CURRENT_LIST_DIR}")
  27. if(NOT DEFINED PROJECT_VER)
  28. if(EXISTS "${_project_path}/version.txt")
  29. file(STRINGS "${_project_path}/version.txt" PROJECT_VER)
  30. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${_project_path}/version.txt")
  31. else()
  32. git_describe(PROJECT_VER_GIT "${_project_path}")
  33. if(PROJECT_VER_GIT)
  34. set(PROJECT_VER ${PROJECT_VER_GIT})
  35. else()
  36. message(STATUS "Project is not inside a git repository, \
  37. will not use 'git describe' to determine PROJECT_VER.")
  38. set(PROJECT_VER "1")
  39. endif()
  40. endif()
  41. endif()
  42. message(STATUS "Project version: ${PROJECT_VER}")
  43. set(${var} "${PROJECT_VER}" PARENT_SCOPE)
  44. endfunction()
  45. #
  46. # Output the built components to the user. Generates files for invoking idf_monitor.py
  47. # that doubles as an overview of some of the more important build properties.
  48. #
  49. function(__project_info test_components)
  50. idf_build_get_property(prefix __PREFIX)
  51. idf_build_get_property(_build_components BUILD_COMPONENTS)
  52. idf_build_get_property(build_dir BUILD_DIR)
  53. idf_build_get_property(idf_path IDF_PATH)
  54. list(SORT _build_components)
  55. unset(build_components)
  56. unset(build_component_paths)
  57. foreach(build_component ${_build_components})
  58. __component_get_target(component_target "${build_component}")
  59. __component_get_property(_name ${component_target} COMPONENT_NAME)
  60. __component_get_property(_prefix ${component_target} __PREFIX)
  61. __component_get_property(_alias ${component_target} COMPONENT_ALIAS)
  62. __component_get_property(_dir ${component_target} COMPONENT_DIR)
  63. if(_alias IN_LIST test_components)
  64. list(APPEND test_component_paths ${_dir})
  65. else()
  66. if(_prefix STREQUAL prefix)
  67. set(component ${_name})
  68. else()
  69. set(component ${_alias})
  70. endif()
  71. list(APPEND build_components ${component})
  72. list(APPEND build_component_paths ${_dir})
  73. endif()
  74. endforeach()
  75. set(PROJECT_NAME ${CMAKE_PROJECT_NAME})
  76. idf_build_get_property(PROJECT_PATH PROJECT_DIR)
  77. idf_build_get_property(BUILD_DIR BUILD_DIR)
  78. idf_build_get_property(SDKCONFIG SDKCONFIG)
  79. idf_build_get_property(SDKCONFIG_DEFAULTS SDKCONFIG_DEFAULTS)
  80. idf_build_get_property(PROJECT_EXECUTABLE EXECUTABLE)
  81. set(PROJECT_BIN ${CMAKE_PROJECT_NAME}.bin)
  82. idf_build_get_property(IDF_VER IDF_VER)
  83. idf_build_get_property(sdkconfig_cmake SDKCONFIG_CMAKE)
  84. include(${sdkconfig_cmake})
  85. idf_build_get_property(COMPONENT_KCONFIGS KCONFIGS)
  86. idf_build_get_property(COMPONENT_KCONFIGS_PROJBUILD KCONFIG_PROJBUILDS)
  87. # Write project description JSON file
  88. idf_build_get_property(build_dir BUILD_DIR)
  89. make_json_list("${build_components};${test_components}" build_components_json)
  90. make_json_list("${build_component_paths};${test_component_paths}" build_component_paths_json)
  91. configure_file("${idf_path}/tools/cmake/project_description.json.in"
  92. "${build_dir}/project_description.json")
  93. # We now have the following component-related variables:
  94. #
  95. # build_components is the list of components to include in the build.
  96. # build_component_paths is the paths to all of these components, obtained from the component dependencies file.
  97. #
  98. # Print the list of found components and test components
  99. string(REPLACE ";" " " build_components "${build_components}")
  100. string(REPLACE ";" " " build_component_paths "${build_component_paths}")
  101. message(STATUS "Components: ${build_components}")
  102. message(STATUS "Component paths: ${build_component_paths}")
  103. if(test_components)
  104. string(REPLACE ";" " " test_components "${test_components}")
  105. string(REPLACE ";" " " test_component_paths "${test_component_paths}")
  106. message(STATUS "Test components: ${test_components}")
  107. message(STATUS "Test component paths: ${test_component_paths}")
  108. endif()
  109. endfunction()
  110. function(__project_init components_var test_components_var)
  111. # Use EXTRA_CFLAGS, EXTRA_CXXFLAGS and EXTRA_CPPFLAGS to add more priority options to the compiler
  112. # EXTRA_CPPFLAGS is used for both C and C++
  113. # Unlike environments' CFLAGS/CXXFLAGS/CPPFLAGS which work for both host and target build,
  114. # these works only for target build
  115. set(extra_cflags "$ENV{EXTRA_CFLAGS}")
  116. set(extra_cxxflags "$ENV{EXTRA_CXXFLAGS}")
  117. set(extra_cppflags "$ENV{EXTRA_CPPFLAGS}")
  118. spaces2list(extra_cflags)
  119. spaces2list(extra_cxxflags)
  120. spaces2list(extra_cppflags)
  121. idf_build_set_property(C_COMPILE_OPTIONS "${extra_cflags}" APPEND)
  122. idf_build_set_property(CXX_COMPILE_OPTIONS "${extra_cxxflags}" APPEND)
  123. idf_build_set_property(COMPILE_OPTIONS "${extra_cppflags}" APPEND)
  124. function(__project_component_dir component_dir)
  125. get_filename_component(component_dir "${component_dir}" ABSOLUTE)
  126. if(EXISTS ${component_dir}/CMakeLists.txt)
  127. idf_build_component(${component_dir})
  128. else()
  129. file(GLOB component_dirs ${component_dir}/*)
  130. foreach(component_dir ${component_dirs})
  131. if(EXISTS ${component_dir}/CMakeLists.txt)
  132. get_filename_component(base_dir ${component_dir} NAME)
  133. __component_dir_quick_check(is_component ${component_dir})
  134. if(is_component)
  135. idf_build_component(${component_dir})
  136. endif()
  137. endif()
  138. endforeach()
  139. endif()
  140. endfunction()
  141. # Add component directories to the build, given the component filters, exclusions
  142. # extra directories, etc. passed from the root CMakeLists.txt.
  143. if(COMPONENT_DIRS)
  144. # User wants to fully override where components are pulled from.
  145. spaces2list(COMPONENT_DIRS)
  146. idf_build_set_property(__COMPONENT_TARGETS "")
  147. foreach(component_dir ${COMPONENT_DIRS})
  148. __project_component_dir(${component_dir})
  149. endforeach()
  150. else()
  151. # Look for components in the usual places: CMAKE_CURRENT_LIST_DIR/main,
  152. # CMAKE_CURRENT_LIST_DIR/components, and the extra component dirs
  153. if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/main")
  154. __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/main")
  155. endif()
  156. __project_component_dir("${CMAKE_CURRENT_LIST_DIR}/components")
  157. spaces2list(EXTRA_COMPONENT_DIRS)
  158. foreach(component_dir ${EXTRA_COMPONENT_DIRS})
  159. __project_component_dir("${component_dir}")
  160. endforeach()
  161. endif()
  162. spaces2list(COMPONENTS)
  163. spaces2list(EXCLUDE_COMPONENTS)
  164. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  165. foreach(component_target ${component_targets})
  166. __component_get_property(component_name ${component_target} COMPONENT_NAME)
  167. set(include 1)
  168. if(COMPONENTS AND NOT component_name IN_LIST COMPONENTS)
  169. set(include 0)
  170. endif()
  171. if(EXCLUDE_COMPONENTS AND component_name IN_LIST EXCLUDE_COMPONENTS)
  172. set(include 0)
  173. endif()
  174. if(include)
  175. list(APPEND components ${component_name})
  176. endif()
  177. endforeach()
  178. if(TESTS_ALL OR BUILD_TESTS OR TEST_COMPONENTS OR TEST_EXCLUDE_COMPONENTS)
  179. spaces2list(TEST_COMPONENTS)
  180. spaces2list(TEST_EXCLUDE_COMPONENTS)
  181. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  182. foreach(component_target ${component_targets})
  183. __component_get_property(component_dir ${component_target} COMPONENT_DIR)
  184. __component_get_property(component_name ${component_target} COMPONENT_NAME)
  185. if(component_name IN_LIST components)
  186. set(include 1)
  187. if(TEST_COMPONENTS AND NOT component_name IN_LIST TEST_COMPONENTS)
  188. set(include 0)
  189. endif()
  190. if(TEST_EXCLUDE_COMPONENTS AND component_name IN_LIST TEST_EXCLUDE_COMPONENTS)
  191. set(include 0)
  192. endif()
  193. if(include AND EXISTS ${component_dir}/test)
  194. __component_add(${component_dir}/test ${component_name})
  195. list(APPEND test_components ${component_name}::test)
  196. endif()
  197. endif()
  198. endforeach()
  199. endif()
  200. set(${components_var} "${components}" PARENT_SCOPE)
  201. set(${test_components_var} "${test_components}" PARENT_SCOPE)
  202. endfunction()
  203. # Trick to temporarily redefine project(). When functions are overriden in CMake, the originals can still be accessed
  204. # using an underscore prefixed function of the same name. The following lines make sure that __project calls
  205. # the original project(). See https://cmake.org/pipermail/cmake/2015-October/061751.html.
  206. function(project)
  207. endfunction()
  208. function(_project)
  209. endfunction()
  210. macro(project project_name)
  211. # Initialize project, preparing COMPONENTS argument for idf_build_process()
  212. # call later using external COMPONENT_DIRS, COMPONENTS_DIRS, EXTRA_COMPONENTS_DIR,
  213. # EXTRA_COMPONENTS_DIRS, COMPONENTS, EXLUDE_COMPONENTS, TEST_COMPONENTS,
  214. # TEST_EXLUDE_COMPONENTS, TESTS_ALL, BUILD_TESTS
  215. __project_init(components test_components)
  216. __target_set_toolchain()
  217. if(CCACHE_ENABLE)
  218. find_program(CCACHE_FOUND ccache)
  219. if(CCACHE_FOUND)
  220. message(STATUS "ccache will be used for faster builds")
  221. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
  222. else()
  223. message(WARNING "enabled ccache in build but ccache program not found")
  224. endif()
  225. endif()
  226. # The actual call to project()
  227. __project(${project_name} C CXX ASM)
  228. # Generate compile_commands.json (needs to come after project call).
  229. set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
  230. # Since components can import third-party libraries, the original definition of project() should be restored
  231. # before the call to add components to the build.
  232. function(project)
  233. set(project_ARGV ARGV)
  234. __project(${${project_ARGV}})
  235. # Set the variables that project() normally sets, documented in the
  236. # command's docs.
  237. #
  238. # https://cmake.org/cmake/help/v3.5/command/project.html
  239. #
  240. # There is some nuance when it comes to setting version variables in terms of whether
  241. # CMP0048 is set to OLD or NEW. However, the proper behavior should have bee already handled by the original
  242. # project call, and we're just echoing the values those variables were set to.
  243. set(PROJECT_NAME "${PROJECT_NAME}" PARENT_SCOPE)
  244. set(PROJECT_BINARY_DIR "${PROJECT_BINARY_DIR}" PARENT_SCOPE)
  245. set(PROJECT_SOURCE_DIR "${PROJECT_SOURCE_DIR}" PARENT_SCOPE)
  246. set(PROJECT_VERSION "${PROJECT_VERSION}" PARENT_SCOPE)
  247. set(PROJECT_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}" PARENT_SCOPE)
  248. set(PROJECT_VERSION_MINOR "${PROJECT_VERSION_MINOR}" PARENT_SCOPE)
  249. set(PROJECT_VERSION_PATCH "${PROJECT_VERSION_PATCH}" PARENT_SCOPE)
  250. set(PROJECT_VERSION_TWEAK "${PROJECT_VERSION_TWEAK}" PARENT_SCOPE)
  251. set(${PROJECT_NAME}_BINARY_DIR "${${PROJECT_NAME}_BINARY_DIR}" PARENT_SCOPE)
  252. set(${PROJECT_NAME}_SOURCE_DIR "${${PROJECT_NAME}_SOURCE_DIR}" PARENT_SCOPE)
  253. set(${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION}" PARENT_SCOPE)
  254. set(${PROJECT_NAME}_VERSION_MAJOR "${${PROJECT_NAME}_VERSION_MAJOR}" PARENT_SCOPE)
  255. set(${PROJECT_NAME}_VERSION_MINOR "${${PROJECT_NAME}_VERSION_MINOR}" PARENT_SCOPE)
  256. set(${PROJECT_NAME}_VERSION_PATCH "${${PROJECT_NAME}_VERSION_PATCH}" PARENT_SCOPE)
  257. set(${PROJECT_NAME}_VERSION_TWEAK "${${PROJECT_NAME}_VERSION_TWEAK}" PARENT_SCOPE)
  258. endfunction()
  259. # Prepare the following arguments for the idf_build_process() call using external
  260. # user values:
  261. #
  262. # SDKCONFIG_DEFAULTS is from external SDKCONFIG_DEFAULTS
  263. # SDKCONFIG is from external SDKCONFIG
  264. # BUILD_DIR is set to project binary dir
  265. #
  266. # PROJECT_NAME is taken from the passed name from project() call
  267. # PROJECT_DIR is set to the current directory
  268. # PROJECT_VER is from the version text or git revision of the current repo
  269. if(SDKCONFIG_DEFAULTS)
  270. get_filename_component(sdkconfig_defaults "${SDKCONFIG_DEFAULTS}" ABSOLUTE)
  271. if(NOT EXISTS "${sdkconfig_defaults}")
  272. message(FATAL_ERROR "SDKCONFIG_DEFAULTS '${sdkconfig_defaults}' does not exist.")
  273. endif()
  274. else()
  275. if(EXISTS "${CMAKE_SOURCE_DIR}/sdkconfig.defaults")
  276. set(sdkconfig_defaults "${CMAKE_SOURCE_DIR}/sdkconfig.defaults")
  277. else()
  278. set(sdkconfig_defaults "")
  279. endif()
  280. endif()
  281. if(SDKCONFIG)
  282. get_filename_component(sdkconfig "${SDKCONFIG}" ABSOLUTE)
  283. if(NOT EXISTS "${sdkconfig}")
  284. message(FATAL_ERROR "SDKCONFIG '${sdkconfig}' does not exist.")
  285. endif()
  286. set(sdkconfig ${SDKCONFIG})
  287. else()
  288. set(sdkconfig "${CMAKE_CURRENT_LIST_DIR}/sdkconfig")
  289. endif()
  290. if(BUILD_DIR)
  291. get_filename_component(build_dir "${BUILD_DIR}" ABSOLUTE)
  292. if(NOT EXISTS "${build_dir}")
  293. message(FATAL_ERROR "BUILD_DIR '${build_dir}' does not exist.")
  294. endif()
  295. else()
  296. set(build_dir ${CMAKE_BINARY_DIR})
  297. endif()
  298. __project_get_revision(project_ver)
  299. message(STATUS "Building ESP-IDF components for target ${IDF_TARGET}")
  300. idf_build_process(${IDF_TARGET}
  301. SDKCONFIG_DEFAULTS "${sdkconfig_defaults}"
  302. SDKCONFIG ${sdkconfig}
  303. BUILD_DIR ${build_dir}
  304. PROJECT_NAME ${CMAKE_PROJECT_NAME}
  305. PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}
  306. PROJECT_VER "${project_ver}"
  307. COMPONENTS "${components};${test_components}")
  308. # Special treatment for 'main' component for standard projects (not part of core build system).
  309. # Have it depend on every other component in the build. This is
  310. # a convenience behavior for the standard project; thus is done outside of the core build system
  311. # so that it treats components equally.
  312. #
  313. # This behavior should only be when user did not set REQUIRES/PRIV_REQUIRES manually.
  314. idf_build_get_property(build_components BUILD_COMPONENTS)
  315. if(idf::main IN_LIST build_components)
  316. __component_get_target(main_target idf::main)
  317. __component_get_property(reqs ${main_target} REQUIRES)
  318. __component_get_property(priv_reqs ${main_target} PRIV_REQUIRES)
  319. idf_build_get_property(common_reqs __COMPONENT_REQUIRES_COMMON)
  320. if(reqs STREQUAL common_reqs AND NOT priv_reqs) #if user has not set any requirements
  321. list(REMOVE_ITEM build_components idf::main)
  322. __component_get_property(lib ${main_target} COMPONENT_LIB)
  323. set_property(TARGET ${lib} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "${build_components}")
  324. get_property(type TARGET ${lib} PROPERTY TYPE)
  325. if(type STREQUAL STATIC_LIBRARY)
  326. set_property(TARGET ${lib} APPEND PROPERTY LINK_LIBRARIES "${build_components}")
  327. endif()
  328. endif()
  329. endif()
  330. set(project_elf ${CMAKE_PROJECT_NAME}.elf)
  331. # Create a dummy file to work around CMake requirement of having a source
  332. # file while adding an executable
  333. set(project_elf_src ${CMAKE_BINARY_DIR}/project_elf_src.c)
  334. add_custom_command(OUTPUT ${project_elf_src}
  335. COMMAND ${CMAKE_COMMAND} -E touch ${project_elf_src}
  336. VERBATIM)
  337. add_custom_target(_project_elf_src DEPENDS "${project_elf_src}")
  338. add_executable(${project_elf} "${project_elf_src}")
  339. add_dependencies(${project_elf} _project_elf_src)
  340. if(test_components)
  341. target_link_libraries(${project_elf} "-Wl,--whole-archive")
  342. foreach(test_component ${test_components})
  343. if(TARGET ${test_component})
  344. target_link_libraries(${project_elf} ${test_component})
  345. endif()
  346. endforeach()
  347. target_link_libraries(${project_elf} "-Wl,--no-whole-archive")
  348. endif()
  349. idf_build_get_property(build_components BUILD_COMPONENTS)
  350. if(test_components)
  351. list(REMOVE_ITEM build_components ${test_components})
  352. endif()
  353. target_link_libraries(${project_elf} ${build_components})
  354. set(mapfile "${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.map")
  355. target_link_libraries(${project_elf} "-Wl,--cref -Wl,--Map=${mapfile}")
  356. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  357. ADDITIONAL_MAKE_CLEAN_FILES
  358. "${mapfile}" "${project_elf_src}")
  359. idf_build_get_property(idf_path IDF_PATH)
  360. idf_build_get_property(python PYTHON)
  361. set(idf_size ${python} ${idf_path}/tools/idf_size.py)
  362. if(DEFINED OUTPUT_JSON AND OUTPUT_JSON)
  363. list(APPEND idf_size "--json")
  364. endif()
  365. # Add size targets, depend on map file, run idf_size.py
  366. add_custom_target(size
  367. DEPENDS ${project_elf}
  368. COMMAND ${idf_size} ${mapfile}
  369. )
  370. add_custom_target(size-files
  371. DEPENDS ${project_elf}
  372. COMMAND ${idf_size} --files ${mapfile}
  373. )
  374. add_custom_target(size-components
  375. DEPENDS ${project_elf}
  376. COMMAND ${idf_size} --archives ${mapfile}
  377. )
  378. unset(idf_size)
  379. idf_build_executable(${project_elf})
  380. __project_info("${test_components}")
  381. endmacro()