# This file is part of the ACTS project.
#
# Copyright (C) 2016 CERN for the benefit of the ACTS project
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# Set the common C++ flags.
include(detray-compiler-options-cpp)

include_directories(
    SYSTEM
    $<TARGET_PROPERTY:covfie::core,INTERFACE_INCLUDE_DIRECTORIES>
)

# Detray unit and integration test framework (depends on google test)
if(DETRAY_BUILD_TESTING)
    message(STATUS "Building 'detray::test_framework' component")

    file(
        GLOB _detray_test_headers
        RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
        "include/detray/test/framework/*.hpp"
    )

    add_library(detray_test_framework INTERFACE "${_detray_test_headers}")
    add_library(detray::test_framework ALIAS detray_test_framework)

    target_include_directories(
        detray_test_framework
        INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include"
    )

    target_link_libraries(
        detray_test_framework
        INTERFACE GTest::gtest vecmem::core detray::core detray::detectors
    )

    unset(_detray_test_headers)
endif()

# Common test dependencies such as test detectors, B-fields and track generators
# (depends on covfie)
if(DETRAY_BUILD_TEST_UTILS OR DETRAY_BUILD_BENCHMARKS)
    message(STATUS "Building 'detray::test_common' component")

    file(
        GLOB _detray_test_common_headers
        RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
        "include/detray/test/common/*.hpp"
        "include/detray/test/common/factories/*.hpp"
        "include/detray/test/common/event_generator/*.hpp"
    )

    detray_add_library(detray_test_common test_common "${_detray_test_common_headers}")

    target_link_libraries(
        detray_test_common
        INTERFACE vecmem::core covfie::core detray::core detray::detectors
    )

    unset(_detray_test_common_headers)

    if(DETRAY_BUILD_CUDA)
        message(STATUS "Building 'detray::test_common_cuda' component")
        file(
            GLOB _detray_test_common_cuda_headers
            RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
            "include/detray/test/common/device/cuda*.hpp"
        )

        detray_add_library(detray_test_common_cuda test_common_cuda "${_detray_test_common_cuda_headers}")

        target_link_libraries(
            detray_test_common_cuda
            INTERFACE vecmem::cuda covfie::cuda detray::test_common
        )
    endif()
endif()

# Lightweight test utils library without external dependencies
if(DETRAY_BUILD_TEST_UTILS OR DETRAY_BUILD_VALIDATION_TOOLS)
    message(STATUS "Building 'detray::test_utils' component")

    file(
        GLOB _detray_test_utils_headers
        RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
        "include/detray/test/utils/*.hpp"
    )

    detray_add_library(detray_test_utils test_utils ${_detray_test_utils_headers})

    target_link_libraries(detray_test_utils INTERFACE vecmem::core detray::core)

    unset(_detray_test_utils_headers)

    # Compile all headers one-by-one.
    detray_compile_headers(
        test_utils
        detray::test_utils
        "include/detray/test/utils/*.hpp"
        "include/detray/test/utils/**/*.hpp"
    )
    if(TARGET detray_test_utils_HEADERS)
        # The test utils headers need an algebra plugin to compile. They also
        # reach detray/test/framework/types.hpp (and through it the detector
        # metadata headers), which detray::test_utils does not declare.
        target_link_libraries(
            detray_test_utils_HEADERS
            PRIVATE detray::core_array detray::detectors
        )
    endif()
endif()

# Build extended validation tools (actsvg and csv dependencies)
if(DETRAY_BUILD_VALIDATION_TOOLS)
    message(STATUS "Building 'detray::validation_utils' component")

    file(
        GLOB _detray_validation_headers
        RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
        "include/detray/test/validation/*.hpp"
    )

    detray_add_library(detray_validation_utils validation_utils ${_detray_validation_headers})

    target_link_libraries(
        detray_validation_utils
        INTERFACE
            vecmem::core
            detray::core
            detray::test_utils
            detray::csv_io
            detray::svgtools
    )

    unset(_detray_validation_headers)
endif()

# Include all of the code-holding sub-directories.

# Build the benchmarks
if(DETRAY_BUILD_BENCHMARKS)
    add_subdirectory(benchmarks)
endif()

# Build test executables
if(DETRAY_BUILD_TESTING)
    # Build host and device specific test code
    add_subdirectory(include/detray/test/cpu)

    if(DETRAY_BUILD_CUDA OR DETRAY_BUILD_HIP)
        add_subdirectory(include/detray/test/device)
    endif()

    # Build the unittests
    if(DETRAY_BUILD_UNITTESTS)
        add_subdirectory(unit_tests)
    endif()

    # Build the integration tests
    if(DETRAY_BUILD_INTEGRATIONTESTS)
        add_subdirectory(integration_tests)
    endif()
endif()

# Build the command line tools (also required for the host jacobian
# validation integration test)
if(
    (DETRAY_BUILD_INTEGRATIONTESTS AND DETRAY_BUILD_HOST)
    OR DETRAY_BUILD_CLI_TOOLS
)
    add_subdirectory(tools)
endif()
