# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

# This test uses CMake's FetchContent module to build opentelemetry-cpp from src
# and make its targets available within an external project.

# CMake 3.18+ is required to build curl 8.21.0+
cmake_minimum_required(VERSION 3.18)

project(opentelemetry-cpp-fetch-content-test LANGUAGES CXX)

set(BUILD_TESTING
    ON
    CACHE BOOL "Build tests" FORCE)

# Exercise the opentelemetry-cpp subproject default independently from the
# parent project's standard BUILD_TESTING option.
unset(OTELCPP_BUILD_TESTING CACHE)

if(NOT DEFINED OPENTELEMETRY_CPP_SRC_DIR)
  message(
    FATAL_ERROR
      "OPENTELEMETRY_CPP_SRC_DIR must be defined when running cmake on this test project"
  )
endif()

message(
  STATUS
    "Adding opentelemetry-cpp as a subdirectory with FetchContent from ${OPENTELEMETRY_CPP_SRC_DIR}"
)

include(FetchContent)
FetchContent_Declare(
  googletest SOURCE_DIR "${OPENTELEMETRY_CPP_SRC_DIR}/third_party/googletest")
FetchContent_Declare(opentelemetry-cpp SOURCE_DIR ${OPENTELEMETRY_CPP_SRC_DIR})
FetchContent_MakeAvailable(googletest opentelemetry-cpp)

if(NOT BUILD_TESTING)
  message(FATAL_ERROR "The parent project BUILD_TESTING option was disabled")
endif()

if(NOT DEFINED OTELCPP_BUILD_TESTING)
  message(FATAL_ERROR "OTELCPP_BUILD_TESTING was not defined")
endif()

if(OTELCPP_BUILD_TESTING)
  message(
    FATAL_ERROR
      "OTELCPP_BUILD_TESTING must default to OFF when opentelemetry-cpp is a subproject"
  )
endif()

if(NOT DEFINED OPENTELEMETRY_VERSION OR OPENTELEMETRY_VERSION
                                        VERSION_LESS_EQUAL 0.0.0)
  message(
    FATAL_ERROR
      "OpenTelemetry C++ version not set after FetchContent_MakeAvailable(opentelemetry-cpp)."
  )
endif()

if(NOT DEFINED OPENTELEMETRY_ABI_VERSION_NO OR OPENTELEMETRY_ABI_VERSION_NO
                                               LESS 1)
  message(
    FATAL_ERROR
      "OpenTelemetry C++ ABI version number not defined in the source directory"
  )
endif()

message(
  STATUS
    "OpenTelemetry C++ version: ${OPENTELEMETRY_VERSION}, ABI version: ${OPENTELEMETRY_ABI_VERSION_NO}"
)

add_executable(
  fetch_content_src_test
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_api.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_sdk.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_configuration.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_resource_detectors.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_ext_common.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_ext_http.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_ext_http_curl.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_ostream.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_in_memory.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_otlp_common.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_otlp_grpc.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_otlp_http.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_otlp_file.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_prometheus.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_zipkin.cc
  ${OPENTELEMETRY_CPP_SRC_DIR}/install/test/src/test_exporters_elasticsearch.cc)

target_link_libraries(
  fetch_content_src_test
  PRIVATE opentelemetry-cpp::api
          opentelemetry-cpp::version
          opentelemetry-cpp::metrics
          opentelemetry-cpp::trace
          opentelemetry-cpp::logs
          opentelemetry-cpp::configuration_core
          opentelemetry-cpp::configuration_trace_builders
          opentelemetry-cpp::configuration_metrics_builders
          opentelemetry-cpp::configuration_logs_builders
          opentelemetry-cpp::configuration_registry_factory
          opentelemetry-cpp::configuration_yaml
          opentelemetry-cpp::configuration
          opentelemetry-cpp::resource_detectors
          opentelemetry-cpp::resource_detectors_builders
          opentelemetry-cpp::container_resource_detector
          opentelemetry-cpp::container_resource_detector_builder
          opentelemetry-cpp::env_entity_resource_detector
          opentelemetry-cpp::host_resource_detector
          opentelemetry-cpp::host_resource_detector_builder
          opentelemetry-cpp::process_resource_detector
          opentelemetry-cpp::process_resource_detector_builder
          opentelemetry-cpp::service_resource_detector
          opentelemetry-cpp::service_resource_detector_builder
          opentelemetry-cpp::http_client
          opentelemetry-cpp::http_client_curl
          opentelemetry-cpp::in_memory_span_exporter
          opentelemetry-cpp::in_memory_metric_exporter
          opentelemetry-cpp::ostream_log_record_exporter
          opentelemetry-cpp::ostream_log_record_exporter_builder
          opentelemetry-cpp::ostream_metrics_exporter
          opentelemetry-cpp::ostream_metrics_exporter_builder
          opentelemetry-cpp::ostream_span_exporter
          opentelemetry-cpp::ostream_span_exporter_builder
          opentelemetry-cpp::otlp_common
          opentelemetry-cpp::otlp_recordable
          opentelemetry-cpp::otlp_file_exporter
          opentelemetry-cpp::otlp_file_exporter_builder
          opentelemetry-cpp::otlp_file_log_record_exporter
          opentelemetry-cpp::otlp_file_log_record_exporter_builder
          opentelemetry-cpp::otlp_file_metric_exporter
          opentelemetry-cpp::otlp_file_metric_exporter_builder
          opentelemetry-cpp::otlp_grpc_exporter
          opentelemetry-cpp::otlp_grpc_exporter_builder
          opentelemetry-cpp::otlp_grpc_log_record_exporter
          opentelemetry-cpp::otlp_grpc_log_record_exporter_builder
          opentelemetry-cpp::otlp_grpc_metrics_exporter
          opentelemetry-cpp::otlp_grpc_metric_exporter_builder
          opentelemetry-cpp::otlp_http_exporter
          opentelemetry-cpp::otlp_http_exporter_builder
          opentelemetry-cpp::otlp_http_log_record_exporter
          opentelemetry-cpp::otlp_http_log_record_exporter_builder
          opentelemetry-cpp::otlp_http_metric_exporter
          opentelemetry-cpp::otlp_http_metric_exporter_builder
          opentelemetry-cpp::prometheus_exporter
          opentelemetry-cpp::prometheus_exporter_builder
          opentelemetry-cpp::zipkin_trace_exporter
          opentelemetry-cpp::elasticsearch_log_record_exporter
          GTest::gtest
          GTest::gtest_main)

include(CTest)
include(GoogleTest)

gtest_add_tests(
  TARGET fetch_content_src_test
  TEST_PREFIX fetch_content.
  TEST_LIST fetch_content_src_test)
