# Copyright (C) 2018  INRIA
#
# Treerecs is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Treerecs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Build GTEST but not GMOCK, install neither
option(BUILD_GTEST "Builds the googletest subproject" ON)
option(BUILD_GMOCK "Builds the googlemock subproject" OFF)
option(INSTALL_GTEST "Disable installation of googletest" OFF)
option(INSTALL_GMOCK "Disable installation of googlemock" OFF)
add_subdirectory(gtest)

# Specify C++14
include(CheckCXXCompilerFlag)
if(CYGWIN)
  CHECK_CXX_COMPILER_FLAG(-std=gnu++14 CXX_STANDARD_14_SUPPORT)
  if(CXX_STANDARD_14_SUPPORT)
    add_definitions(-std=gnu++14)
  else(CXX_STANDARD_14_SUPPORT)
    message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} does not support the -std=gnu++14 flag. Please use a different C++ compiler.")
  endif(CXX_STANDARD_14_SUPPORT)
else(CYGWIN)
  CHECK_CXX_COMPILER_FLAG(-std=c++14 CXX_STANDARD_14_SUPPORT)
  if(CXX_STANDARD_14_SUPPORT)
    set(CMAKE_CXX_STANDARD 14)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_EXTENSIONS OFF)
  else(CXX_STANDARD_14_SUPPORT)
    message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} has no C++14 support. Please use a different C++ compiler.")
  endif(CXX_STANDARD_14_SUPPORT)
endif(CYGWIN)

# Set path to test data files
if (UNIX)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D TEST_DATA_PATH=\\\"${PROJECT_SOURCE_DIR}/tests/data/\\\"")
else()#WIN32
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D TEST_DATA_PATH=\\\"${PROJECT_SOURCE_DIR}\\tests\\data\\\\\"")
endif()

# Libraries to link unit tests against
set(test_libs
  gtest_main
  treerecs-core)

# List unit tests
set(TESTS
  test_test
  grid_test
  Timer_tests
  Utils_tests
  Random_tests
  PhyloTreeToolBox_tests
  treeMapping_test
  profilenj_test
  treeReconciliation_test
  bipartition_test
  PhyloXML_tests
  RecPhyloTreeToSVG_tests
  IO_tests
  LibpllEvaluation_tests
  TreerecsCli_tests
  SupportTools_test
  )

# Create a runner for each unit test. Generates TEST_RUNNERS
foreach (TEST IN LISTS TESTS)
  set(TEST_RUNNER  run_${TEST})
  set(TEST_RUNNERS ${TEST_RUNNERS} ${TEST_RUNNER})
  add_executable(${TEST} ${TEST}.cpp)
  set_target_properties(${TEST}
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
    )
  target_link_libraries(${TEST} ${test_libs})
  add_custom_target(${TEST_RUNNER}
    COMMAND ${CMAKE_BINARY_DIR}/tests/${TEST}
    DEPENDS ${TEST})
  set_target_properties(${TEST_RUNNER}
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
    )
endforeach(TEST)

# Set ad-hoc additional properties
target_sources(TreerecsCli_tests
  PUBLIC ${CMAKE_SOURCE_DIR}/src/TreerecsCliUtils.cpp)

# Add target check
add_custom_target(check DEPENDS utest stest)

# Add target utest (unit tests)
add_custom_target(utest DEPENDS ${TEST_RUNNERS})

# Add target stest (system testing tests)
add_custom_target(stest ${CMAKE_CTEST_COMMAND} DEPENDS treerecs)

include(ctest_tests.cmake)
