2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2011,2012,2013,2014,2015,2016, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
35 include(CMakeParseArguments)
37 function (gmx_add_unit_test_object_library NAME)
38 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
39 include_directories(BEFORE SYSTEM ${GMOCK_INCLUDE_DIRS})
40 add_library(${NAME} OBJECT ${UNITTEST_TARGET_OPTIONS} ${ARGN})
41 set_property(TARGET ${NAME} APPEND PROPERTY COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}")
42 set_property(TARGET ${NAME} APPEND PROPERTY COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
46 function (gmx_add_gtest_executable EXENAME)
47 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
49 cmake_parse_arguments(ARG "${_options}" "" "" ${ARGN})
50 set(_source_files ${ARG_UNPARSED_ARGUMENTS})
52 file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
53 set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
54 file(MAKE_DIRECTORY ${_temporary_files_path})
55 # Note that the quotation marks in the next line form part of
56 # the defined symbol, so that the macro replacement in the
57 # source file is as a string.
58 # These are only needed for unittest_main.cpp, but for simplicity used
59 # for the whole target (since there may be multiple executables in the
60 # same directory, it is not straightforward to use a source file
62 set(EXTRA_COMPILE_DEFINITIONS
63 TEST_DATA_PATH="${_input_files_path}"
64 TEST_TEMP_PATH="${_temporary_files_path}")
66 list(APPEND EXTRA_COMPILE_DEFINITIONS
70 include_directories(BEFORE SYSTEM ${GMOCK_INCLUDE_DIRS})
71 add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
72 ${_source_files} ${TESTUTILS_DIR}/unittest_main.cpp)
73 target_link_libraries(${EXENAME}
74 ${TESTUTILS_LIBS} libgromacs ${GMOCK_LIBRARIES} ${GMX_EXE_LINKER_FLAGS} ${GMX_STDLIB_LIBRARIES})
75 set_property(TARGET ${EXENAME}
76 APPEND PROPERTY COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
77 set_property(TARGET ${EXENAME}
78 APPEND PROPERTY COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}")
79 set_property(TARGET ${EXENAME}
80 APPEND PROPERTY COMPILE_DEFINITIONS "${EXTRA_COMPILE_DEFINITIONS}")
84 function (gmx_register_unit_test NAME EXENAME)
85 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
87 COMMAND ${EXENAME} --gtest_output=xml:${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
88 set_tests_properties(${NAME} PROPERTIES LABELS "GTest;UnitTest")
89 add_dependencies(tests ${EXENAME})
93 # Use this function to register a test binary as an integration test
94 function (gmx_register_integration_test NAME EXENAME)
95 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
97 COMMAND ${EXENAME} --gtest_output=xml:${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
98 set_tests_properties(${testname} PROPERTIES LABELS "IntegrationTest")
99 add_dependencies(tests ${EXENAME})
101 # GMX_EXTRA_LIBRARIES might be needed for mdrun integration tests at
103 # target_link_libraries(${EXENAME} ${GMX_EXTRA_LIBRARIES} ${GMX_STDLIB_LIBRARIES})
107 # Use this function to register a test binary as an integration test
108 # that requires MPI. The intended number of MPI ranks is also passed
110 # TODO When a test case needs it, generalize the NUMPROC mechanism so
111 # that ctest can run the test binary over a range of numbers of MPI
113 function (gmx_register_mpi_integration_test NAME EXENAME NUMPROC)
114 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
116 foreach(VARNAME MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS)
117 # These variables need a valid value for the test to run
118 # and pass, but conceivably any of them might be valid
119 # with arbitrary (including empty) content. They can't be
120 # valid if they've been populated with the CMake
121 # find_package magic suffix/value "NOTFOUND", though.
122 if (${VARNAME} MATCHES ".*NOTFOUND")
123 message(STATUS "CMake variable ${VARNAME} was not detected to be a valid value. To test GROMACS correctly, check the advice in the install guide.")
124 set(_cannot_run_mpi_tests 1)
126 if (NOT VARNAME STREQUAL MPIEXEC AND ${VARNAME})
127 set(_an_mpi_variable_had_content 1)
130 if(_an_mpi_variable_had_content AND NOT MPIEXEC)
131 message(STATUS "CMake variable MPIEXEC must have a valid value if one of the other related MPIEXEC variables does. To test GROMACS correctly, check the advice in the install guide.")
132 set(_cannot_run_mpi_tests 1)
134 if(NOT _cannot_run_mpi_tests)
135 add_test(NAME ${NAME}
137 ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${NUMPROC}
138 ${MPIEXEC_PREFLAGS} $<TARGET_FILE:${EXENAME}> ${MPIEXEC_POSTFLAGS}
139 --gtest_output=xml:${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml
141 set_tests_properties(${testname} PROPERTIES LABELS "MpiIntegrationTest")
142 add_dependencies(tests ${EXENAME})
145 # GMX_EXTRA_LIBRARIES might be needed for mdrun integration tests at
147 # target_link_libraries(${EXENAME} ${GMX_EXTRA_LIBRARIES} ${GMX_STDLIB_LIBRARIES})
148 elseif(GMX_THREAD_MPI)
149 add_test(NAME ${NAME}
151 $<TARGET_FILE:${EXENAME}> -nt ${NUMPROC}
152 --gtest_output=xml:${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml
154 set_tests_properties(${testname} PROPERTIES LABELS "MpiIntegrationTest")
155 add_dependencies(tests ${EXENAME})
157 # GMX_EXTRA_LIBRARIES might be needed for mdrun integration tests at
159 # target_link_libraries(${EXENAME} ${GMX_EXTRA_LIBRARIES} ${GMX_STDLIB_LIBRARIES})
164 function (gmx_add_unit_test NAME EXENAME)
165 gmx_add_gtest_executable(${EXENAME} ${ARGN})
166 gmx_register_unit_test(${NAME} ${EXENAME})