component_get_requirements.cmake 6.1 KB

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