cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project(AvogadroLibsTest LANGUAGES CXX)

if(PROJECT_IS_TOP_LEVEL)
  # Enable build of standalone tests
  # testing against AvogadroLibs already installed on the system.
  include(CTest)
  enable_testing()

  # QT_VERSION not previously defined when building tests only
  set(QT_VERSION 6)

  # Nor is USE_QT, which gates the qtgui suite. Default it on so a standalone
  # test build behaves as it always has.
  option(USE_QT "Build the tests that require Qt 6" ON)

  find_package(AvogadroLibs REQUIRED)
  include_directories("${AvogadroLibs_INCLUDE_DIRS}/..")
endif()

include_directories("${AvogadroLibs_BINARY_DIR}/avogadro/core")

# find google test
find_package(GTest REQUIRED)
find_package(Eigen3 REQUIRED)
# Only the qtgui tests link Qt, so a Qt-free build (USE_QT=OFF) can still run
# the core, calc, io and quantumio suites -- which is what the MemorySanitizer
# workflow does, since instrumenting Qt is not practical.
if(USE_QT)
  find_package(Qt${QT_VERSION} COMPONENTS Widgets Network Concurrent REQUIRED)
endif()
# Add both as "system headers" to avoid warnings generated by them with
# compilers that support that notion.
include_directories(SYSTEM
  ${GTEST_INCLUDE_DIRS}
  ${EIGEN3_INCLUDE_DIR})

include(CheckIncludeFileCXX)
include(CheckCXXSymbolExists)
check_include_file_cxx("gtest/gtest.h" HAVE_GTEST_HPP)
if(HAVE_GTEST_HPP)
  check_cxx_symbol_exists(GTEST_HAS_PTHREAD "gtest/gtest.h" GTEST_HAS_PTHREAD)
  check_cxx_symbol_exists(GTEST_IS_THREADSAFE "gtest/gtest.h" GTEST_IS_THREADSAFE)
endif()

if(GTEST_HAS_PTHREAD)
  message(STATUS "GTest claims it has pthreads, we need to link to it.")
  find_package(Threads)
  set(EXTRA_LINK_LIB ${CMAKE_THREAD_LIBS_INIT})
else()
  set(EXTRA_LINK_LIB "")
endif()

find_path(AVOGADRO_DATA_ROOT .avogadro.data
  ${AvogadroLibs_SOURCE_DIR}/../avogadrodata
  $ENV{AVOGADRO_DATA_ROOT}
  DOC "The repository for data used for testing."
  )
mark_as_advanced(AVOGADRO_DATA_ROOT)

# Fatal, or the data-dependent test directories silently drop their tests.
if(NOT AVOGADRO_DATA_ROOT OR NOT EXISTS "${AVOGADRO_DATA_ROOT}/.avogadro.data")
  message(FATAL_ERROR "ENABLE_TESTING is ON but the avogadrodata test data "
    "was not found. Set AVOGADRO_DATA_ROOT (CMake variable or environment "
    "variable) to an avogadrodata checkout "
    "(https://github.com/OpenChemistry/avogadrodata), or turn ENABLE_TESTING "
    "off. Without it the calc, io, quantumio and qtopengl tests would not be "
    "registered.")
endif()

# Add the tests for each module.

add_subdirectory(calc)

add_subdirectory(core)
add_subdirectory(io)
add_subdirectory(quantumio)
if(USE_QT)
  add_subdirectory(qtgui)
  # The command-boundary tests link plugin targets directly, which only works
  # when they are static libraries built in this tree.
  if(BUILD_STATIC_PLUGINS AND NOT PROJECT_IS_TOP_LEVEL)
    add_subdirectory(qtplugins)
  else()
    message(STATUS "Skipping qtplugins tests: they need in-tree static plugins"
      " (BUILD_STATIC_PLUGINS=ON).")
  endif()
endif()
if(USE_OPENGL)
  add_subdirectory(rendering)
endif()
