expand_requirements.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # expand_requirements.cmake is a utility cmake script to expand component requirements early in the build,
  2. # before the components are ready to be included.
  3. #
  4. # Parameters:
  5. # - COMPONENTS = Space-separated list of initial components to include in the build.
  6. # Can be empty, in which case all components are in the build.
  7. # - COMPONENT_REQUIRES_COMMON = Components to always include in the build, and treated as dependencies
  8. # of all other components.
  9. # - DEPENDENCIES_FILE = Path of generated cmake file which will contain the expanded dependencies for these
  10. # components.
  11. # - COMPONENT_DIRS = List of paths to search for all components.
  12. # - DEBUG = Set -DDEBUG=1 to debug component lists in the build.
  13. #
  14. # If successful, DEPENDENCIES_FILE can be expanded to set BUILD_COMPONENTS & BUILD_COMPONENT_PATHS with all
  15. # components required for the build, and the get_component_requirements() function to return each component's
  16. # recursively expanded requirements.
  17. #
  18. # BUILD_COMPONENTS & BUILD_COMPONENT_PATHS will be ordered in a best-effort way so that dependencies are listed first.
  19. # (Note that IDF supports cyclic dependencies, and dependencies in a cycle have ordering guarantees.)
  20. #
  21. # Determinism:
  22. #
  23. # Given the the same list of names in COMPONENTS (regardless of order), and an identical value of
  24. # COMPONENT_REQUIRES_COMMON, and all the same COMPONENT_REQUIRES & COMPONENT_PRIV_REQUIRES values in
  25. # each component, then the output of BUILD_COMPONENTS should always be in the same
  26. # order.
  27. #
  28. # BUILD_COMPONENT_PATHS will be in the same component order as BUILD_COMPONENTS, even if the
  29. # actual component paths are different due to different paths.
  30. #
  31. # TODO: Error out if a component requirement is missing
  32. cmake_minimum_required(VERSION 3.5)
  33. include("${IDF_PATH}/tools/cmake/utilities.cmake")
  34. include("${IDF_PATH}/tools/cmake/component_utils.cmake")
  35. set(ESP_PLATFORM 1)
  36. if(NOT DEPENDENCIES_FILE)
  37. message(FATAL_ERROR "DEPENDENCIES_FILE must be set.")
  38. endif()
  39. if(NOT COMPONENT_DIRS)
  40. message(FATAL_ERROR "COMPONENT_DIRS variable must be set")
  41. endif()
  42. spaces2list(COMPONENT_DIRS)
  43. spaces2list(COMPONENT_REQUIRES_COMMON)
  44. # Dummy register_component used to save requirements variables as global properties, for later expansion
  45. #
  46. # (expand_component_requirements() includes the component CMakeLists.txt, which then sets its component variables,
  47. # calls this dummy macro, and immediately exits again.)
  48. macro(register_component)
  49. if(COMPONENT STREQUAL main AND NOT COMPONENT_REQUIRES)
  50. set(main_component_requires ${COMPONENTS})
  51. list(REMOVE_ITEM main_component_requires "main")
  52. set_property(GLOBAL PROPERTY "${COMPONENT}_REQUIRES" "${main_component_requires}")
  53. else()
  54. spaces2list(COMPONENT_REQUIRES)
  55. set_property(GLOBAL PROPERTY "${COMPONENT}_REQUIRES" "${COMPONENT_REQUIRES}")
  56. endif()
  57. spaces2list(COMPONENT_PRIV_REQUIRES)
  58. set_property(GLOBAL PROPERTY "${COMPONENT}_PRIV_REQUIRES" "${COMPONENT_PRIV_REQUIRES}")
  59. # This is tricky: we override register_component() so it returns out of the component CMakeLists.txt
  60. # (as we're declaring it as a macro not a function, so it doesn't have its own scope.)
  61. #
  62. # This means no targets are defined, and the component expansion ends early.
  63. return()
  64. endmacro()
  65. macro(register_config_only_component)
  66. register_component()
  67. endmacro()
  68. function(require_idf_targets)
  69. if(NOT ${IDF_TARGET} IN_LIST ARGN)
  70. message(FATAL_ERROR "Component ${COMPONENT_NAME} only supports targets: ${ARGN}")
  71. endif()
  72. endfunction()
  73. # expand_component_requirements: Recursively expand a component's requirements,
  74. # setting global properties BUILD_COMPONENTS & BUILD_COMPONENT_PATHS and
  75. # also invoking the components to call register_component() above,
  76. # which will add per-component global properties with dependencies, etc.
  77. function(expand_component_requirements component)
  78. get_property(seen_components GLOBAL PROPERTY SEEN_COMPONENTS)
  79. if(component IN_LIST seen_components)
  80. return() # already added, or in process of adding, this component
  81. endif()
  82. set_property(GLOBAL APPEND PROPERTY SEEN_COMPONENTS ${component})
  83. find_component_path("${component}" "${ALL_COMPONENTS}" "${ALL_COMPONENT_PATHS}" component_path)
  84. debug("Expanding dependencies of ${component} @ ${component_path}")
  85. if(NOT component_path)
  86. set_property(GLOBAL APPEND PROPERTY COMPONENTS_NOT_FOUND ${component})
  87. return()
  88. endif()
  89. # include the component CMakeLists.txt to expand its properties
  90. # into the global cache (via register_component(), above)
  91. unset(COMPONENT_REQUIRES)
  92. unset(COMPONENT_PRIV_REQUIRES)
  93. set(COMPONENT ${component})
  94. include(${component_path}/CMakeLists.txt)
  95. get_property(requires GLOBAL PROPERTY "${component}_REQUIRES")
  96. get_property(requires_priv GLOBAL PROPERTY "${component}_PRIV_REQUIRES")
  97. # Recurse dependencies first, so that they appear first in the list (when possible)
  98. foreach(req ${COMPONENT_REQUIRES_COMMON} ${requires} ${requires_priv})
  99. expand_component_requirements(${req})
  100. endforeach()
  101. list(FIND TEST_COMPONENTS ${component} idx)
  102. if(NOT idx EQUAL -1)
  103. list(GET TEST_COMPONENTS ${idx} test_component)
  104. list(GET TEST_COMPONENT_PATHS ${idx} test_component_path)
  105. set_property(GLOBAL APPEND PROPERTY BUILD_TEST_COMPONENTS ${test_component})
  106. set_property(GLOBAL APPEND PROPERTY BUILD_TEST_COMPONENT_PATHS ${test_component_path})
  107. endif()
  108. # Now append this component to the full list (after its dependencies)
  109. set_property(GLOBAL APPEND PROPERTY BUILD_COMPONENT_PATHS ${component_path})
  110. set_property(GLOBAL APPEND PROPERTY BUILD_COMPONENTS ${component})
  111. endfunction()
  112. # filter_components_list: Filter the components included in the build
  113. # as specified by the user. Or, in the case of unit testing, filter out
  114. # the test components to be built.
  115. macro(filter_components_list)
  116. spaces2list(COMPONENTS)
  117. spaces2list(EXCLUDE_COMPONENTS)
  118. spaces2list(TEST_COMPONENTS)
  119. spaces2list(TEST_EXCLUDE_COMPONENTS)
  120. list(LENGTH ALL_COMPONENTS all_components_length)
  121. math(EXPR all_components_length "${all_components_length} - 1")
  122. foreach(component_idx RANGE 0 ${all_components_length})
  123. list(GET ALL_COMPONENTS ${component_idx} component)
  124. list(GET ALL_COMPONENT_PATHS ${component_idx} component_path)
  125. if(COMPONENTS)
  126. if(${component} IN_LIST COMPONENTS)
  127. set(add_component 1)
  128. else()
  129. set(add_component 0)
  130. endif()
  131. else()
  132. set(add_component 1)
  133. endif()
  134. if(NOT ${component} IN_LIST EXCLUDE_COMPONENTS AND add_component EQUAL 1)
  135. list(APPEND components ${component})
  136. list(APPEND component_paths ${component_path})
  137. if(BUILD_TESTS EQUAL 1)
  138. if(TEST_COMPONENTS)
  139. if(${component} IN_LIST TEST_COMPONENTS)
  140. set(add_test_component 1)
  141. else()
  142. set(add_test_component 0)
  143. endif()
  144. else()
  145. set(add_test_component 1)
  146. endif()
  147. if(${component} IN_LIST ALL_TEST_COMPONENTS)
  148. if(NOT ${component} IN_LIST TEST_EXCLUDE_COMPONENTS AND add_test_component EQUAL 1)
  149. list(APPEND test_components ${component}_test)
  150. list(APPEND test_component_paths ${component_path}/test)
  151. list(APPEND components ${component}_test)
  152. list(APPEND component_paths ${component_path}/test)
  153. endif()
  154. endif()
  155. endif()
  156. endif()
  157. endforeach()
  158. set(COMPONENTS ${components})
  159. set(TEST_COMPONENTS ${test_components})
  160. set(TEST_COMPONENT_PATHS ${test_component_paths})
  161. list(APPEND ALL_COMPONENTS "${TEST_COMPONENTS}")
  162. list(APPEND ALL_COMPONENT_PATHS "${TEST_COMPONENT_PATHS}")
  163. endmacro()
  164. # Main functionality goes here
  165. # Find every available component in COMPONENT_DIRS, save as ALL_COMPONENT_PATHS and ALL_COMPONENTS
  166. components_find_all("${COMPONENT_DIRS}" ALL_COMPONENT_PATHS ALL_COMPONENTS ALL_TEST_COMPONENTS)
  167. filter_components_list()
  168. debug("ALL_COMPONENT_PATHS ${ALL_COMPONENT_PATHS}")
  169. debug("ALL_COMPONENTS ${ALL_COMPONENTS}")
  170. debug("ALL_TEST_COMPONENTS ${ALL_TEST_COMPONENTS}")
  171. set_property(GLOBAL PROPERTY SEEN_COMPONENTS "") # anti-infinite-recursion
  172. set_property(GLOBAL PROPERTY BUILD_COMPONENTS "")
  173. set_property(GLOBAL PROPERTY BUILD_COMPONENT_PATHS "")
  174. set_property(GLOBAL PROPERTY BUILD_TEST_COMPONENTS "")
  175. set_property(GLOBAL PROPERTY BUILD_TEST_COMPONENT_PATHS "")
  176. set_property(GLOBAL PROPERTY COMPONENTS_NOT_FOUND "")
  177. # Indicate that the component CMakeLists.txt is being included in the early expansion phase of the build,
  178. # and might not want to execute particular operations.
  179. set(CMAKE_BUILD_EARLY_EXPANSION 1)
  180. foreach(component ${COMPONENTS})
  181. debug("Expanding initial component ${component}")
  182. expand_component_requirements(${component})
  183. endforeach()
  184. unset(CMAKE_BUILD_EARLY_EXPANSION)
  185. get_property(build_components GLOBAL PROPERTY BUILD_COMPONENTS)
  186. get_property(build_component_paths GLOBAL PROPERTY BUILD_COMPONENT_PATHS)
  187. get_property(build_test_components GLOBAL PROPERTY BUILD_TEST_COMPONENTS)
  188. get_property(build_test_component_paths GLOBAL PROPERTY BUILD_TEST_COMPONENT_PATHS)
  189. get_property(not_found GLOBAL PROPERTY COMPONENTS_NOT_FOUND)
  190. debug("components in build: ${build_components}")
  191. debug("components in build: ${build_component_paths}")
  192. debug("components not found: ${not_found}")
  193. function(line contents)
  194. file(APPEND "${DEPENDENCIES_FILE}.tmp" "${contents}\n")
  195. endfunction()
  196. file(WRITE "${DEPENDENCIES_FILE}.tmp" "# Component requirements generated by expand_requirements.cmake\n\n")
  197. line("set(BUILD_COMPONENTS ${build_components})")
  198. line("set(BUILD_COMPONENT_PATHS ${build_component_paths})")
  199. line("set(BUILD_TEST_COMPONENTS ${build_test_components})")
  200. line("set(BUILD_TEST_COMPONENT_PATHS ${build_test_component_paths})")
  201. line("")
  202. line("# get_component_requirements: Generated function to read the dependencies of a given component.")
  203. line("#")
  204. line("# Parameters:")
  205. line("# - component: Name of component")
  206. line("# - var_requires: output variable name. Set to recursively expanded COMPONENT_REQUIRES ")
  207. line("# for this component.")
  208. line("# - var_private_requires: output variable name. Set to recursively expanded COMPONENT_PRIV_REQUIRES ")
  209. line("# for this component.")
  210. line("#")
  211. line("# Throws a fatal error if 'componeont' is not found (indicates a build system problem).")
  212. line("#")
  213. line("function(get_component_requirements component var_requires var_private_requires)")
  214. foreach(build_component ${build_components})
  215. get_property(reqs GLOBAL PROPERTY "${build_component}_REQUIRES")
  216. get_property(private_reqs GLOBAL PROPERTY "${build_component}_PRIV_REQUIRES")
  217. line(" if(\"\$\{component}\" STREQUAL \"${build_component}\")")
  218. line(" set(\${var_requires} \"${reqs}\" PARENT_SCOPE)")
  219. line(" set(\${var_private_requires} \"${private_reqs}\" PARENT_SCOPE)")
  220. line(" return()")
  221. line(" endif()")
  222. endforeach()
  223. line(" message(FATAL_ERROR \"Component not found: \${component}\")")
  224. line("endfunction()")
  225. # only replace DEPENDENCIES_FILE if it has changed (prevents ninja/make build loops.)
  226. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${DEPENDENCIES_FILE}.tmp" "${DEPENDENCIES_FILE}")
  227. execute_process(COMMAND ${CMAKE_COMMAND} -E remove "${DEPENDENCIES_FILE}.tmp")