include(ExternalProject)

# https://stackoverflow.com/questions/45414507/pass-a-list-of-prefix-paths-to-externalproject-add-in-cmake-args
string(REPLACE ";" "|" DJV_THIRD_PARTY_DEPS_TMP "${DJV_THIRD_PARTY_DEPS}")

set(tests_ARGS
    -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
    -DCMAKE_PREFIX_PATH=${CMAKE_INSTALL_PREFIX}
    -DCMAKE_POLICY_DEFAULT_CMP0091:STRING=NEW
    -DCMAKE_FIND_FRAMEWORK=${CMAKE_FIND_FRAMEWORK}
    -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
    -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
    -DCMAKE_MSVC_RUNTIME_LIBRARY=${CMAKE_MSVC_RUNTIME_LIBRARY}
    -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
    -DDJV_THIRD_PARTY_DEPS=${DJV_THIRD_PARTY_DEPS_TMP})
if(CMAKE_CXX_STANDARD)
    set(tests_ARGS ${tests_ARGS} -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD})
endif()

ExternalProject_Add(
    tests
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/tests
    DEPENDS ${DJV_THIRD_PARTY_DEPS}
    SOURCE_DIR ${CMAKE_SOURCE_DIR}/tests
    TEST_BEFORE_INSTALL TRUE
    INSTALL_COMMAND ""
    LIST_SEPARATOR |
    CMAKE_ARGS ${tests_ARGS})
