component_get_requirements.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_register)
  51. set(options)
  52. set(single_value KCONFIG KCONFIG_PROJBUILD)
  53. set(multi_value SRCS SRC_DIRS EXCLUDE_SRCS
  54. INCLUDE_DIRS PRIV_INCLUDE_DIRS LDFRAGMENTS REQUIRES
  55. PRIV_REQUIRES REQUIRED_IDF_TARGETS EMBED_FILES EMBED_TXTFILES)
  56. cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" "${ARGN}")
  57. set(__component_priv_requires "${__PRIV_REQUIRES}")
  58. set(__component_requires "${__REQUIRES}")
  59. set(__component_kconfig "${__KCONFIG}")
  60. set(__component_kconfig_projbuild "${__KCONFIG_PROJBUILD}")
  61. set(__component_registered 1)
  62. return()
  63. endmacro()
  64. macro(register_component)
  65. set(__component_requires "${COMPONENT_REQUIRES}")
  66. set(__component_priv_requires "${COMPONENT_PRIV_REQUIRES}")
  67. set(__component_registered 1)
  68. return()
  69. endmacro()
  70. macro(register_config_only_component)
  71. register_component()
  72. endmacro()
  73. idf_build_get_property(__common_reqs __COMPONENT_REQUIRES_COMMON)
  74. idf_build_get_property(__component_targets __COMPONENT_TARGETS)
  75. function(__component_get_requirements)
  76. # This is in a function (separate variable context) so that variables declared
  77. # and set by the included CMakeLists.txt does not bleed into the next inclusion.
  78. # We are only interested in the public and private requirements of components
  79. __component_get_property(__component_dir ${__component_target} COMPONENT_DIR)
  80. __component_get_property(__component_name ${__component_target} COMPONENT_NAME)
  81. set(COMPONENT_NAME ${__component_name})
  82. set(COMPONENT_DIR ${__component_dir})
  83. set(COMPONENT_PATH ${__component_dir}) # for backward compatibility only, COMPONENT_DIR is preferred
  84. include(${__component_dir}/CMakeLists.txt OPTIONAL)
  85. spaces2list(__component_requires)
  86. spaces2list(__component_priv_requires)
  87. set(__component_requires "${__component_requires}" PARENT_SCOPE)
  88. set(__component_priv_requires "${__component_priv_requires}" PARENT_SCOPE)
  89. set(__component_kconfig "${__component_kconfig}" PARENT_SCOPE)
  90. set(__component_kconfig_projbuild "${__component_kconfig_projbuild}" PARENT_SCOPE)
  91. set(__component_registered ${__component_registered} PARENT_SCOPE)
  92. endfunction()
  93. set(CMAKE_BUILD_EARLY_EXPANSION 1)
  94. foreach(__component_target ${__component_targets})
  95. set(__component_requires "")
  96. set(__component_priv_requires "")
  97. set(__component_registered 0)
  98. __component_get_requirements()
  99. list(APPEND __component_requires "${__common_reqs}")
  100. # Remove duplicates and the component itself from its requirements
  101. __component_get_property(__component_alias ${__component_target} COMPONENT_ALIAS)
  102. __component_get_property(__component_name ${__component_target} COMPONENT_NAME)
  103. # Prevent component from linking to itself.
  104. if(__component_requires)
  105. list(REMOVE_DUPLICATES __component_requires)
  106. list(REMOVE_ITEM __component_requires ${__component_alias} ${__component_name})
  107. endif()
  108. if(__component_priv_requires)
  109. list(REMOVE_DUPLICATES __component_priv_requires)
  110. list(REMOVE_ITEM __component_priv_requires ${__component_alias} ${__component_name})
  111. endif()
  112. set(__contents
  113. "__component_set_property(${__component_target} REQUIRES \"${__component_requires}\")
  114. __component_set_property(${__component_target} PRIV_REQUIRES \"${__component_priv_requires}\")
  115. __component_set_property(${__component_target} __COMPONENT_REGISTERED ${__component_registered})"
  116. )
  117. if(__component_kconfig)
  118. get_filename_component(__component_kconfig "${__component_kconfig}" ABSOLUTE)
  119. set(__contents
  120. "${__contents}\n__component_set_property(${__component_target} KCONFIG \"${__component_kconfig}\")"
  121. )
  122. endif()
  123. if(__component_kconfig_projbuild)
  124. get_filename_component(__component_kconfig "${__component_kconfig}" ABSOLUTE)
  125. set(__contents
  126. "${__contents}\n__component_set_property(${__component_target} KCONFIG_PROJBUILD \"${__component_kconfig_projbuild}\")"
  127. )
  128. endif()
  129. set(__component_requires_contents "${__component_requires_contents}\n${__contents}")
  130. endforeach()
  131. file(WRITE ${COMPONENT_REQUIRES_FILE} "${__component_requires_contents}")