component_get_requirements.cmake 6.1 KB

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