component_get_requirements.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # if(IN_LIST) is used, which requires CMP0057 set to NEW
  2. cmake_policy(SET CMP0057 NEW)
  3. include("${BUILD_PROPERTIES_FILE}")
  4. include("${COMPONENT_PROPERTIES_FILE}")
  5. function(idf_build_get_property var property)
  6. cmake_parse_arguments(_ "GENERATOR_EXPRESSION" "" "" ${ARGN})
  7. if(__GENERATOR_EXPRESSION)
  8. message(FATAL_ERROR "Getting build property generator expression not
  9. supported before idf_component_register().")
  10. endif()
  11. set(${var} ${${property}} PARENT_SCOPE)
  12. endfunction()
  13. idf_build_get_property(idf_path IDF_PATH)
  14. include(${idf_path}/tools/cmake/utilities.cmake)
  15. include(${idf_path}/tools/cmake/version.cmake)
  16. function(__component_get_property var component_target property)
  17. set(_property __component_${component_target}_${property})
  18. set(${var} ${${_property}} PARENT_SCOPE)
  19. endfunction()
  20. #
  21. # Given a component name or alias, get the corresponding component target.
  22. #
  23. function(__component_get_target var name_or_alias)
  24. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  25. # Assume first that the paramters is an alias.
  26. string(REPLACE "::" "_" name_or_alias "${name_or_alias}")
  27. set(component_target ___${name_or_alias})
  28. if(component_target IN_LIST component_targets)
  29. set(${var} ${component_target} PARENT_SCOPE)
  30. set(target ${component_target})
  31. else() # assumption is wrong, try to look for it manually
  32. unset(target)
  33. foreach(component_target ${component_targets})
  34. __component_get_property(_component_name ${component_target} COMPONENT_NAME)
  35. if(name_or_alias STREQUAL _component_name)
  36. set(target ${component_target})
  37. break()
  38. endif()
  39. endforeach()
  40. set(${var} ${target} PARENT_SCOPE)
  41. endif()
  42. endfunction()
  43. function(idf_component_get_property var component property)
  44. __component_get_target(component_target ${component})
  45. __component_get_property(_var ${component_target} ${property})
  46. set(${var} ${_var} PARENT_SCOPE)
  47. endfunction()
  48. macro(require_idf_targets)
  49. endmacro()
  50. macro(idf_component_mock)
  51. set(options)
  52. set(single_value)
  53. set(multi_value MOCK_HEADER_FILES INCLUDE_DIRS REQUIRES)
  54. cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" "${ARGN}")
  55. idf_component_register(REQUIRES ${__REQUIRES} cmock)
  56. return()
  57. endmacro()
  58. macro(idf_component_register)
  59. set(options WHOLE_ARCHIVE)
  60. set(single_value KCONFIG KCONFIG_PROJBUILD)
  61. set(multi_value SRCS SRC_DIRS EXCLUDE_SRCS
  62. INCLUDE_DIRS PRIV_INCLUDE_DIRS LDFRAGMENTS REQUIRES
  63. PRIV_REQUIRES REQUIRED_IDF_TARGETS EMBED_FILES EMBED_TXTFILES)
  64. cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" "${ARGN}")
  65. set(__component_priv_requires "${__PRIV_REQUIRES}")
  66. set(__component_requires "${__REQUIRES}")
  67. set(__component_kconfig "${__KCONFIG}")
  68. set(__component_kconfig_projbuild "${__KCONFIG_PROJBUILD}")
  69. set(__component_registered 1)
  70. return()
  71. endmacro()
  72. macro(register_component)
  73. set(__component_requires "${COMPONENT_REQUIRES}")
  74. set(__component_priv_requires "${COMPONENT_PRIV_REQUIRES}")
  75. set(__component_registered 1)
  76. return()
  77. endmacro()
  78. macro(register_config_only_component)
  79. register_component()
  80. endmacro()
  81. idf_build_get_property(__common_reqs __COMPONENT_REQUIRES_COMMON)
  82. idf_build_get_property(__component_targets __COMPONENT_TARGETS)
  83. function(__component_get_requirements)
  84. # This is in a function (separate variable context) so that variables declared
  85. # and set by the included CMakeLists.txt does not bleed into the next inclusion.
  86. # We are only interested in the public and private requirements of components
  87. __component_get_property(__component_dir ${__component_target} COMPONENT_DIR)
  88. __component_get_property(__component_name ${__component_target} COMPONENT_NAME)
  89. set(COMPONENT_NAME ${__component_name})
  90. set(COMPONENT_DIR ${__component_dir})
  91. set(COMPONENT_PATH ${__component_dir}) # for backward compatibility only, COMPONENT_DIR is preferred
  92. include(${__component_dir}/CMakeLists.txt OPTIONAL)
  93. spaces2list(__component_requires)
  94. spaces2list(__component_priv_requires)
  95. set(__component_requires "${__component_requires}" PARENT_SCOPE)
  96. set(__component_priv_requires "${__component_priv_requires}" PARENT_SCOPE)
  97. set(__component_kconfig "${__component_kconfig}" PARENT_SCOPE)
  98. set(__component_kconfig_projbuild "${__component_kconfig_projbuild}" PARENT_SCOPE)
  99. set(__component_registered ${__component_registered} PARENT_SCOPE)
  100. endfunction()
  101. set(CMAKE_BUILD_EARLY_EXPANSION 1)
  102. foreach(__component_target ${__component_targets})
  103. set(__component_requires "")
  104. set(__component_priv_requires "")
  105. set(__component_registered 0)
  106. __component_get_requirements()
  107. # Remove duplicates and the component itself from its requirements
  108. __component_get_property(__component_alias ${__component_target} COMPONENT_ALIAS)
  109. __component_get_property(__component_name ${__component_target} COMPONENT_NAME)
  110. # Prevent component from linking to itself.
  111. if(__component_requires)
  112. list(REMOVE_DUPLICATES __component_requires)
  113. list(REMOVE_ITEM __component_requires ${__component_alias} ${__component_name})
  114. endif()
  115. if(__component_priv_requires)
  116. list(REMOVE_DUPLICATES __component_priv_requires)
  117. list(REMOVE_ITEM __component_priv_requires ${__component_alias} ${__component_name})
  118. endif()
  119. set(__contents
  120. "__component_set_property(${__component_target} REQUIRES \"${__component_requires}\")
  121. __component_set_property(${__component_target} PRIV_REQUIRES \"${__component_priv_requires}\")
  122. __component_set_property(${__component_target} __COMPONENT_REGISTERED ${__component_registered})"
  123. )
  124. if(__component_kconfig)
  125. get_filename_component(__component_kconfig "${__component_kconfig}" ABSOLUTE)
  126. set(__contents
  127. "${__contents}\n__component_set_property(${__component_target} KCONFIG \"${__component_kconfig}\")"
  128. )
  129. endif()
  130. if(__component_kconfig_projbuild)
  131. get_filename_component(__component_kconfig "${__component_kconfig}" ABSOLUTE)
  132. set(__contents
  133. "${__contents}\n__component_set_property(${__component_target} KCONFIG_PROJBUILD \"${__component_kconfig_projbuild}\")"
  134. )
  135. endif()
  136. set(__component_requires_contents "${__component_requires_contents}\n${__contents}")
  137. endforeach()
  138. file(WRITE ${COMPONENT_REQUIRES_FILE} "${__component_requires_contents}")