if(SIRIUS_USE_VCSQNM)
  find_package (Eigen3 3.3 REQUIRED NO_MODULE)
endif()
add_executable(sirius.scf sirius.scf.cpp)
target_link_libraries(sirius.scf PRIVATE sirius_cxx)
if(SIRIUS_USE_VCSQNM)
  target_link_libraries (sirius.scf PRIVATE Eigen3::Eigen)
endif()
install(TARGETS sirius.scf RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")

# Register verification tests as well
set(SIRIUS_SCF_LABELS cpu_serial cpu_band_parallel)
if(SIRIUS_USE_CUDA OR SIRIUS_USE_ROCM)
    set(SIRIUS_SCF_LABELS ${SIRIUS_SCF_LABELS} gpu_serial gpu_band_parallel gpu_k_point_parallel)
endif()
set(SIRIUS_SCF_FLAGS_gpu_serial           --control.processing_unit=gpu --control.std_evp_solver_name=cusolver --control.gen_evp_solver_name=cusolver)
set(SIRIUS_SCF_FLAGS_gpu_band_parallel    --control.processing_unit=gpu --control.mpi_grid_dims=2:2 --control.std_evp_solver_name=scalapack --control.gen_evp_solver_name=scalapack)
set(SIRIUS_SCF_FLAGS_gpu_k_point_parallel --control.processing_unit=gpu --control.std_evp_solver_name=cusolver --control.gen_evp_solver_name=cusolver)
set(SIRIUS_SCF_FLAGS_cpu_serial           --control.processing_unit=cpu)
set(SIRIUS_SCF_FLAGS_cpu_band_parallel    --control.processing_unit=cpu --control.mpi_grid_dims=2:2 --control.std_evp_solver_name=scalapack --control.gen_evp_solver_name=scalapack)

# todo: Add OMP_NUM_THREADS + srun / mpiexec flags here too?

if(BUILD_TESTING)
    set(CTEST_OUTPUT_ON_FAILURE 1)
    file(GLOB dirs LIST_DIRECTORIES true "${CMAKE_SOURCE_DIR}/verification/test*")

    foreach(full_path ${dirs})
        get_filename_component(test_name "${full_path}" NAME)
        set(test_name "sirius.scf_${test_name}")

        foreach(label ${SIRIUS_SCF_LABELS})
            set(full_test_name "${test_name}_${label}")
            set(flags "${SIRIUS_SCF_FLAGS_${label}}")
            set(_TEST_COMMAND $<TARGET_FILE:sirius.scf> --test_against=output_ref.json ${flags}) 
            if(${label} MATCHES "parallel")
                if(MPIRUN)
                    string(REPLACE " " ";" _MPIRUN ${MPIRUN})
                else()
                        set(_MPIRUN "")
                endif()
                set(_TEST_COMMAND ${_MPIRUN} ${_TEST_COMMAND})
            endif()
            add_test(NAME "${full_test_name}" COMMAND ${_TEST_COMMAND} WORKING_DIRECTORY "${full_path}")
            set_tests_properties("${full_test_name}" PROPERTIES LABELS "integration_test ${label}")
        endforeach()
    endforeach()
endif()
