find-package-support-functions.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. include("${CMAKE_CURRENT_LIST_DIR}/component-definitions.cmake")
  4. include("${CMAKE_CURRENT_LIST_DIR}/thirdparty-dependency-definitions.cmake")
  5. #-------------------------------------------------------------------------
  6. # Function to get installed components.
  7. #-------------------------------------------------------------------------
  8. function(get_installed_components installed_components_out)
  9. set(result "")
  10. foreach(_COMPONENT IN LISTS OTEL_BUILT_COMPONENTS_LIST)
  11. set(_COMPONENT_TARGET_FILE "${CMAKE_CURRENT_LIST_DIR}/opentelemetry-cpp-${_COMPONENT}-target.cmake")
  12. if(EXISTS "${_COMPONENT_TARGET_FILE}")
  13. list(APPEND result ${_COMPONENT})
  14. message(DEBUG "get_installed_components: component = ${_COMPONENT}, installed = TRUE")
  15. else()
  16. message(DEBUG "get_installed_components: component = ${_COMPONENT}, installed = FALSE")
  17. endif()
  18. endforeach()
  19. set(${installed_components_out} ${result} PARENT_SCOPE)
  20. endfunction()
  21. #-------------------------------------------------------------------------
  22. # Function to get dependent components.
  23. #-------------------------------------------------------------------------
  24. function(get_dependent_components component_in dependent_components_out)
  25. set(result "")
  26. set(depends_var "COMPONENT_${component_in}_COMPONENT_DEPENDS")
  27. if(DEFINED ${depends_var})
  28. set(result ${${depends_var}})
  29. endif()
  30. set(${dependent_components_out} ${result} PARENT_SCOPE)
  31. endfunction()
  32. #-------------------------------------------------------------------------
  33. # Function to get requested components.
  34. #-------------------------------------------------------------------------
  35. function(get_requested_components installed_components_in requested_components_out)
  36. set(result "")
  37. if (NOT opentelemetry-cpp_FIND_COMPONENTS)
  38. set(result ${${installed_components_in}})
  39. message(DEBUG "get_requested_components: No components explicitly requested. Importing all installed components including: ${result}")
  40. set(${requested_components_out} ${result} PARENT_SCOPE)
  41. else()
  42. message(DEBUG "get_requested_components: Components requested: ${opentelemetry-cpp_FIND_COMPONENTS}")
  43. foreach(_COMPONENT IN LISTS opentelemetry-cpp_FIND_COMPONENTS)
  44. if(NOT ${_COMPONENT} IN_LIST OTEL_BUILT_COMPONENTS_LIST)
  45. message(ERROR " get_requested_components: Component `${_COMPONENT}` is not a built component of the opentelemetry-cpp package. Built components include: ${OTEL_BUILT_COMPONENTS_LIST}")
  46. return()
  47. endif()
  48. if(NOT ${_COMPONENT} IN_LIST ${installed_components_in})
  49. message(ERROR " get_requested_components: Component `${_COMPONENT}` is supported by opentelemetry-cpp but not installed. Installed components include: ${${installed_components_in}}")
  50. return()
  51. endif()
  52. get_dependent_components(${_COMPONENT} _DEPENDENT_COMPONENTS)
  53. list(APPEND result ${_DEPENDENT_COMPONENTS})
  54. list(APPEND result ${_COMPONENT})
  55. endforeach()
  56. list(REMOVE_DUPLICATES result)
  57. set(${requested_components_out} ${result} PARENT_SCOPE)
  58. endif()
  59. endfunction()
  60. #-------------------------------------------------------------------------
  61. # Function to get the targets for a component.
  62. #-------------------------------------------------------------------------
  63. function(get_component_targets component_in targets_out)
  64. set(result "")
  65. if(NOT ${component_in} IN_LIST OTEL_BUILT_COMPONENTS_LIST)
  66. message(ERROR " get_component_targets: Component `${component_in}` component is not a built component of the opentelemetry-cpp package.")
  67. else()
  68. set(targets_var "COMPONENT_${component_in}_TARGETS")
  69. if(DEFINED ${targets_var})
  70. set(result ${${targets_var}})
  71. endif()
  72. endif()
  73. set(${targets_out} ${result} PARENT_SCOPE)
  74. endfunction()
  75. #-------------------------------------------------------------------------
  76. # Get targets for a list of components.
  77. #-------------------------------------------------------------------------
  78. function(get_targets components_in targets_out)
  79. set(result "")
  80. foreach(_comp IN LISTS ${components_in})
  81. get_component_targets(${_comp} comp_targets)
  82. foreach(target IN LISTS comp_targets)
  83. list(APPEND result ${target})
  84. endforeach()
  85. endforeach()
  86. set(${targets_out} ${result} PARENT_SCOPE)
  87. endfunction()
  88. #-------------------------------------------------------------------------
  89. # Check if a target is imported for a list of targets.
  90. #-------------------------------------------------------------------------
  91. function(check_targets_imported targets_in)
  92. set(result TRUE)
  93. foreach(_target IN LISTS ${targets_in})
  94. if(TARGET ${_target})
  95. message(DEBUG "check_targets_imported: imported target `${_target}`")
  96. else()
  97. message(FATAL_ERROR " check_targets_imported: failed to import target `${_target}`")
  98. set(result FALSE)
  99. endif()
  100. endforeach()
  101. set(${result_bool_out} ${result} PARENT_SCOPE)
  102. endfunction()
  103. #-------------------------------------------------------------------------
  104. # Check if a dependency is expected and required
  105. #-------------------------------------------------------------------------
  106. function (is_dependency_required dependency_in components_in is_required_out)
  107. foreach(_component IN LISTS ${components_in})
  108. if(${dependency_in} IN_LIST COMPONENT_${_component}_THIRDPARTY_DEPENDS)
  109. set(${is_required_out} TRUE PARENT_SCOPE)
  110. return()
  111. endif()
  112. endforeach()
  113. endfunction()
  114. #-------------------------------------------------------------------------
  115. # Find all required and expected dependencies
  116. #-------------------------------------------------------------------------
  117. include(CMakeFindDependencyMacro)
  118. function(find_required_dependencies components_in)
  119. foreach(_dependency IN LISTS OTEL_THIRDPARTY_DEPENDENCIES_SUPPORTED)
  120. if(${_dependency}_FOUND)
  121. # The dependency is already found by another component. Continue.
  122. continue()
  123. endif()
  124. set(is_required FALSE)
  125. is_dependency_required(${_dependency} ${components_in} is_required)
  126. message(DEBUG "find_required_dependencies: dependency = ${_dependency}, is_required = ${is_required}")
  127. if(is_required)
  128. message(DEBUG "find_required_dependencies: calling find_dependency(${_dependency} ${OTEL_${_dependency}_SEARCH_MODE} )...")
  129. find_dependency(${_dependency} ${OTEL_${_dependency}_SEARCH_MODE})
  130. endif()
  131. endforeach()
  132. endfunction()