3cc6a2dbad0e3835225fe998aca8c9001fbb56fd
[alexxy/gromacs.git] / python_packaging / sample_restraint / tests / CMakeLists.txt
1 if(DOWNLOAD_GOOGLETEST)
2     # Download and unpack googletest at configure time
3     configure_file(CMakeLists.gtest.in googletest-download/CMakeLists.txt)
4     execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
5                     RESULT_VARIABLE result
6                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
7     if(result)
8         message(WARNING "CMake step for googletest failed: ${result}")
9         #    message(FATAL_ERROR "CMake step for googletest failed: ${result}")
10     endif()
11     execute_process(COMMAND ${CMAKE_COMMAND} --build .
12                     RESULT_VARIABLE result
13                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
14     if(result)
15         message(WARNING "Build step for googletest failed: ${result}")
16         #    message(FATAL_ERROR "Build step for googletest failed: ${result}")
17     endif()
18
19     # Add googletest directly to our build. This defines
20     # the gtest and gtest_main targets.
21     add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
22                      ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
23                      EXCLUDE_FROM_ALL)
24     add_library(GTest::GTest ALIAS gtest)
25     add_library(GTest::Main ALIAS gtest_main)
26 else()
27     if(GMXAPI_EXTENSION_MASTER_PROJECT)
28         find_package(GTest REQUIRED)
29     else() # building as part of a GROMACS build...
30         if(TARGET gtest)
31             add_library(GTest::GTest ALIAS gtest)
32             add_library(GTest::Main ALIAS gtest_main)
33         else()
34             find_package(GTest)
35         endif()
36         if(NOT TARGET GTest::GTest)
37             message(FATAL_ERROR "GTest::GTest target should have been supplied by the parent project.")
38         endif()
39         if(NOT TARGET GTest::Main)
40             message(FATAL_ERROR "GTest::Main target should have been supplied by the parent project.")
41         endif()
42     endif()
43 endif() # DOWNLOAD_GOOGLETEST
44
45 #
46 # Set up our tests
47 #
48
49
50 # Copy test files
51 if (GMXAPI_EXTENSION_MASTER_PROJECT)
52     # TODO: Deduplicate and leverage CMake dependencies.
53     # We also build a test file in the spc_water_box pytest test fixture, but we can
54     # presumably extract both of those from the GROMACS installation, or at least
55     # through the gmxapi Python package resources.
56     # Ref: https://gitlab.com/gromacs/gromacs/-/issues/2961
57     file(DOWNLOAD
58          https://github.com/kassonlab/sample_restraint/raw/master/tests/data/topol.tpr
59          ${CMAKE_CURRENT_BINARY_DIR}/topol.tpr
60          STATUS _download_status
61          LOG _download_log)
62     list(GET _download_status 0 _status)
63     if(${_status} GREATER 0)
64         message(WARNING "Could not download test data: ${_download_log}")
65     endif()
66     unset(_status)
67     add_custom_target(gmxapi_extension_spc2_water_box
68                       COMMAND cmake -E echo
69                       SOURCES ${CMAKE_CURRENT_BINARY_DIR}/topol.tpr
70                       )
71 else()
72     # We are in the GROMACS build tree.
73     # We just need a simple TPR input file. The 6 atom spc water box will suffice.
74     if (NOT TARGET gmx)
75         message(FATAL_ERROR "Trying to use gmx wrapper binary, but gmx target not defined.")
76     endif()
77     set(_mdp ${CMAKE_CURRENT_BINARY_DIR}/grompp.mdp)
78     file(WRITE ${_mdp} "integrator = md\n")
79     file(APPEND ${_mdp} "nsteps = 6")
80     set(_gro ${CMAKE_SOURCE_DIR}/src/testutils/simulationdatabase/spc2.gro)
81     set(_top ${CMAKE_SOURCE_DIR}/src/testutils/simulationdatabase/spc2.top)
82     add_custom_target(gmxapi_extension_spc2_water_box
83                       BYPRODUCTS topol.tpr
84                       COMMAND gmx -quiet grompp -f ${_mdp} -c ${_gro} -p ${_top}
85                       DEPENDS gmx ${_mdp}
86                       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
87                       COMMENT "Generating input file for sample_restraint tests.")
88     unset(_mdp)
89     unset(_gro)
90     unset(_top)
91 endif ()
92
93 # Note: Expects topol.tpr to be in CURRENT_BINARY_DUR
94 configure_file(testingconfiguration.in.h testingconfiguration.h)
95
96 # Test that the library can access its dependencies and build.
97 add_executable(gmxapi_extension_library-test test_binding.cpp)
98 add_dependencies(gmxapi_extension_library-test gmxapi_extension_spc2_water_box)
99 target_include_directories(gmxapi_extension_library-test PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
100 target_link_libraries(gmxapi_extension_library-test Gromacs::gmxapi GTest::Main)
101 gtest_add_tests(TARGET gmxapi_extension_library-test
102                 TEST_LIST BasicPlugin)
103
104 # Test the C++ force evaluation for the restrained-ensemble biasing potential.
105 add_executable(gmxapi_extension_histogram-test test_histogram.cpp)
106 add_dependencies(gmxapi_extension_histogram-test gmxapi_extension_spc2_water_box)
107 target_include_directories(gmxapi_extension_histogram-test PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
108 set_target_properties(gmxapi_extension_histogram-test PROPERTIES SKIP_BUILD_RPATH FALSE)
109 target_link_libraries(gmxapi_extension_histogram-test gmxapi_extension_ensemblepotential Gromacs::gmxapi
110                       GTest::Main)
111 gtest_add_tests(TARGET gmxapi_extension_histogram-test
112                 TEST_LIST EnsembleHistogramPotentialPlugin)
113
114 # Test the flat-bottom bounding potential built in to the ensemble restraint.
115 add_executable(gmxapi_extension_bounding-test test_bounding_restraint.cpp)
116 add_dependencies(gmxapi_extension_bounding-test gmxapi_extension_spc2_water_box)
117 target_include_directories(gmxapi_extension_bounding-test PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
118 set_target_properties(gmxapi_extension_bounding-test PROPERTIES SKIP_BUILD_RPATH FALSE)
119 target_link_libraries(gmxapi_extension_bounding-test gmxapi_extension_ensemblepotential Gromacs::gmxapi
120                       GTest::Main)
121 gtest_add_tests(TARGET gmxapi_extension_bounding-test
122                 TEST_LIST EnsembleBoundingPotentialPlugin)
123
124 if (NOT GMXAPI_EXTENSION_MASTER_PROJECT)
125     include(CMakeGROMACS.txt)
126 endif ()