opentelemetry-build-external-component.cmake 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. function(get_directory_name_in_path PATH_VAR RESULT_VAR)
  4. # get_filename_component does not work with paths ending in / or \, so remove it.
  5. string(REGEX REPLACE "[/\\]$" "" PATH_TRIMMED "${PATH_VAR}")
  6. get_filename_component(DIR_NAME ${PATH_TRIMMED} NAME)
  7. set(${RESULT_VAR} "${DIR_NAME}" PARENT_SCOPE)
  8. endfunction()
  9. # Enable building external components through otel-cpp build
  10. # The config options are
  11. # - OPENTELEMETRY_EXTERNAL_COMPONENT_PATH - Setting local paths of the external
  12. # component as env variable. Multiple paths can be set by separating them with.
  13. # - OPENTELEMETRY_EXTERNAL_COMPONENT_URL Setting github-repo of external component
  14. # as env variable
  15. # Add custom vendor component from local path, or GitHub repo
  16. # Prefer CMake option, then env variable, then URL.
  17. if(OPENTELEMETRY_EXTERNAL_COMPONENT_PATH)
  18. # Add custom component path to build tree and consolidate binary artifacts in
  19. # current project binary output directory.
  20. foreach(DIR IN LISTS OPENTELEMETRY_EXTERNAL_COMPONENT_PATH)
  21. get_directory_name_in_path(${DIR} EXTERNAL_EXPORTER_DIR_NAME)
  22. add_subdirectory(${DIR} ${PROJECT_BINARY_DIR}/external/${EXTERNAL_EXPORTER_DIR_NAME})
  23. endforeach()
  24. elseif(DEFINED ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_PATH})
  25. # Add custom component path to build tree and consolidate binary artifacts in
  26. # current project binary output directory.
  27. set(OPENTELEMETRY_EXTERNAL_COMPONENT_PATH_VAR $ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_PATH})
  28. foreach(DIR IN LISTS OPENTELEMETRY_EXTERNAL_COMPONENT_PATH_VAR)
  29. get_directory_name_in_path(${DIR} EXTERNAL_EXPORTER_DIR_NAME)
  30. add_subdirectory(${DIR} ${PROJECT_BINARY_DIR}/external/${EXTERNAL_EXPORTER_DIR_NAME})
  31. endforeach()
  32. elseif(DEFINED $ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_URL})
  33. # This option requires CMake 3.11+: add standard remote repo to build tree.
  34. include(FetchContent)
  35. FetchContent_Declare(
  36. external
  37. GIT_REPOSITORY $ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_URL}
  38. GIT_TAG "main")
  39. FetchContent_GetProperties(external)
  40. if(NOT external_POPULATED)
  41. FetchContent_Populate(external)
  42. add_subdirectory(${external_SOURCE_DIR})
  43. endif()
  44. endif()