component_get_requirements.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. include("${BUILD_PROPERTIES_FILE}")
  2. include("${COMPONENT_PROPERTIES_FILE}")
  3. function(idf_build_get_property var property)
  4. cmake_parse_arguments(_ "GENERATOR_EXPRESSION" "" "" ${ARGN})
  5. if(__GENERATOR_EXPRESSION)
  6. message(FATAL_ERROR "Getting build property generator expression not
  7. supported before idf_component_register().")
  8. endif()
  9. set(${var} ${${property}} PARENT_SCOPE)
  10. endfunction()
  11. idf_build_get_property(idf_path IDF_PATH)
  12. include(${idf_path}/tools/cmake/utilities.cmake)
  13. function(__component_get_property var component_target property)
  14. set(_property __component_${component_target}_${property})
  15. set(${var} ${${_property}} PARENT_SCOPE)
  16. endfunction()
  17. macro(require_idf_targets)
  18. endmacro()
  19. macro(idf_component_register)
  20. set(options)
  21. set(single_value)
  22. set(multi_value SRCS SRC_DIRS EXCLUDE_SRCS
  23. INCLUDE_DIRS PRIV_INCLUDE_DIRS LDFRAGMENTS REQUIRES
  24. PRIV_REQUIRES REQUIRED_IDF_TARGETS EMBED_FILES EMBED_TXTFILES)
  25. cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" "${ARGN}")
  26. set(__component_requires "${__REQUIRES}")
  27. set(__component_priv_requires "${__PRIV_REQUIRES}")
  28. set(__component_registered 1)
  29. return()
  30. endmacro()
  31. macro(register_component)
  32. set(__component_requires "${COMPONENT_REQUIRES}")
  33. set(__component_priv_requires "${COMPONENT_PRIV_REQUIRES}")
  34. set(__component_registered 1)
  35. return()
  36. endmacro()
  37. macro(register_config_only_component)
  38. register_component()
  39. endmacro()
  40. idf_build_get_property(__common_reqs __COMPONENT_REQUIRES_COMMON)
  41. idf_build_get_property(__component_targets __COMPONENT_TARGETS)
  42. function(__component_get_requirements)
  43. # This is in a function (separate variable context) so that variables declared
  44. # and set by the included CMakeLists.txt does not bleed into the next inclusion.
  45. # We are only interested in the public and private requirements of components
  46. __component_get_property(__component_dir ${__component_target} COMPONENT_DIR)
  47. __component_get_property(__component_name ${__component_target} COMPONENT_NAME)
  48. set(COMPONENT_NAME ${__component_name})
  49. set(COMPONENT_DIR ${__component_dir})
  50. set(COMPONENT_PATH ${__component_dir}) # for backward compatibility only, COMPONENT_DIR is preferred
  51. include(${__component_dir}/CMakeLists.txt OPTIONAL)
  52. spaces2list(__component_requires)
  53. spaces2list(__component_priv_requires)
  54. set(__component_requires "${__component_requires}" PARENT_SCOPE)
  55. set(__component_priv_requires "${__component_priv_requires}" PARENT_SCOPE)
  56. set(__component_registered ${__component_registered} PARENT_SCOPE)
  57. endfunction()
  58. set(CMAKE_BUILD_EARLY_EXPANSION 1)
  59. foreach(__component_target ${__component_targets})
  60. set(__component_requires "")
  61. set(__component_priv_requires "")
  62. set(__component_registered 0)
  63. __component_get_requirements()
  64. list(APPEND __component_requires "${__common_reqs}")
  65. # Remove duplicates and the component itself from its requirements
  66. __component_get_property(__component_alias ${__component_target} COMPONENT_ALIAS)
  67. __component_get_property(__component_name ${__component_target} COMPONENT_NAME)
  68. # Prevent component from linking to itself.
  69. if(__component_requires)
  70. list(REMOVE_DUPLICATES __component_requires)
  71. list(REMOVE_ITEM __component_requires ${__component_alias} ${__component_name})
  72. endif()
  73. if(__component_priv_requires)
  74. list(REMOVE_DUPLICATES __component_priv_requires)
  75. list(REMOVE_ITEM __component_priv_requires ${__component_alias} ${__component_name})
  76. endif()
  77. set(__contents
  78. "__component_set_property(${__component_target} REQUIRES \"${__component_requires}\")
  79. __component_set_property(${__component_target} PRIV_REQUIRES \"${__component_priv_requires}\")
  80. __component_set_property(${__component_target} __COMPONENT_REGISTERED ${__component_registered})"
  81. )
  82. set(__component_requires_contents "${__component_requires_contents}\n${__contents}")
  83. endforeach()
  84. file(WRITE ${COMPONENT_REQUIRES_FILE} "${__component_requires_contents}")