
message (STATUS "Preparing unit test support. Build the tests with 'make tests' and run them with 'ctest'.")

find_package(Threads REQUIRED)

include(ExternalProject)

# Download and install GoogleTest
ExternalProject_Add(
    gtest
    URL file://${PROJECT_SOURCE_DIR}/Tests/GTest
    PREFIX ${PROJECT_BINARY_DIR}/gtest
    # Disable install step
    INSTALL_COMMAND ""
)
ExternalProject_Get_Property(gtest source_dir binary_dir)


add_library(ThirdParty::GTest IMPORTED STATIC GLOBAL)
add_dependencies(ThirdParty::GTest gtest)

if(CMAKE_GENERATOR STREQUAL Xcode)
  set_target_properties(ThirdParty::GTest PROPERTIES
      "IMPORTED_LOCATION_DEBUG" "${binary_dir}/Debug/libgtest.a"
      "IMPORTED_LOCATION_RELEASE" "${binary_dir}/Release/libgtest.a"
      "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
  )
else()
  set_target_properties(ThirdParty::GTest PROPERTIES
      "IMPORTED_LOCATION" "${binary_dir}/libgtest.a"
      "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
  )
endif()

add_library(ThirdParty::GTestMain IMPORTED STATIC GLOBAL)
add_dependencies(ThirdParty::GTestMain gtest)

if(CMAKE_GENERATOR STREQUAL Xcode)
  set_target_properties(ThirdParty::GTestMain PROPERTIES
      "IMPORTED_LOCATION_DEBUG" "${binary_dir}/Debug/libgtest_main.a"
      "IMPORTED_LOCATION_RELEASE" "${binary_dir}/Release/libgtest_main.a"
      "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
  )
else()
  set_target_properties(ThirdParty::GTestMain PROPERTIES
      "IMPORTED_LOCATION" "${binary_dir}/libgtest_main.a"
      "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
  )
endif()


function (new_test name libs)
  get_filename_component(namedir ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  add_executable(Test${name}
                 Test${name}.cc)
  target_include_directories(Test${name} PRIVATE ${PROJECT_SOURCE_DIR}/include
                                              ${PROJECT_BINARY_DIR}/include)
  target_include_directories(Test${name} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/Tests/GTest/include)
  target_compile_options(Test${name} PRIVATE ${Hammer_CompileOptions})
  if(VERBOSE_DEBUG)
    target_compile_options(Test${name} PRIVATE ${Hammer_VerboseOptions})
    if(VERY_VERBOSE_DEBUG)
      target_compile_options(Test${name} PRIVATE ${Hammer_SanitizeOptions})
    endif()
  endif()
  if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
    target_compile_options(Test${name} PRIVATE "-Wno-global-constructors")
  endif()
  set_target_properties(Test${name} PROPERTIES
                        POSITION_INDEPENDENT_CODE ${Hammer_PIC})
  target_link_libraries(Test${name} PRIVATE ${libs} ThirdParty::GTest ThirdParty::GTestMain
                                    INTERFACE "-Wl,-rpath,${PROJECT_BINARY_DIR}/lib")
  if(${ARGC} GREATER 2)
    SET(ARGN_COPY ${ARGN})
    foreach(MY_FILE ${ARGN_COPY})
      file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${MY_FILE}
           DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    endforeach()
  endif()

  if(SANITIZE)
	  add_sanitizers(Test${name})
  endif()

  add_test(NAME Test${name}
           WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
           COMMAND Test${name} --gtest_color=yes)
  # set_tests_properties(Test${name} PROPERTIES
  #   ENVIRONMENT "")
  set_tests_properties(Test${name} PROPERTIES
    LABELS "${namedir}")

endfunction ()


# add tests in subdirectories
add_subdirectory (Core)
add_subdirectory (Math)
add_subdirectory (Amplitudes)
add_subdirectory (FormFactors)
add_subdirectory (Rates)
add_subdirectory (Tools)
