2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2011,2012,2013,2014,2015 by the GROMACS development team.
5 # Copyright (c) 2016,2017,2018,2019,2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
36 include(CMakeParseArguments)
37 include(gmxClangCudaUtils)
39 function (gmx_add_unit_test_library NAME)
40 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
41 add_library(${NAME} STATIC ${UNITTEST_TARGET_OPTIONS} ${ARGN})
42 gmx_target_compile_options(${NAME})
43 target_compile_definitions(${NAME} PRIVATE HAVE_CONFIG_H)
44 target_include_directories(${NAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
45 target_link_libraries(${NAME} PRIVATE testutils gmock)
47 set_target_properties(${NAME} PROPERTIES CXX_CLANG_TIDY
48 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
50 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7")
51 target_compile_options(${NAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Weverything ${IGNORED_CLANG_ALL_WARNINGS} -Wno-gnu-zero-variadic-macro-arguments -Wno-zero-as-null-pointer-constant -Wno-missing-variable-declarations>)
57 # This function creates a GoogleTest test executable for a module. It
58 # hides all the complexity of how to treat different source files
59 # under different configuration conditions. It should be extended
60 # if we ever support another GPU compilation approach.
62 # It can be called with extra options and arguments:
64 # To trigger the ctest runner to run this test with multiple ranks
66 # To trigger the test executable setup code to run hardware detection
67 # CPP_SOURCE_FILES file1.cpp file2.cpp ...
68 # All the normal C++ .cpp source files
69 # GPU_CPP_SOURCE_FILES file1.cpp file2.cpp ...
70 # All the C++ .cpp source files that are always needed, but must be
71 # compiled in the way that suits GMX_GPU.
72 # CUDA_CU_SOURCE_FILES file1.cu file2.cu ...
73 # All the normal CUDA .cu source files
74 # OPENCL_CPP_SOURCE_FILES file1.cpp file2.cpp ...
75 # All the other C++ .cpp source files needed only with OpenCL
76 # SYCL_CPP_SOURCE_FILES file1.cpp file2.cpp ...
77 # All the C++ .cpp source files needed only with SYCL
78 # NON_GPU_CPP_SOURCE_FILES file1.cpp file2.cpp ...
79 # All the other C++ .cpp source files needed only with neither OpenCL nor CUDA nor SYCL
80 function (gmx_add_gtest_executable EXENAME)
81 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
82 set(_options MPI HARDWARE_DETECTION)
83 set(_multi_value_keywords
87 OPENCL_CPP_SOURCE_FILES
89 NON_GPU_CPP_SOURCE_FILES
91 cmake_parse_arguments(ARG "${_options}" "" "${_multi_value_keywords}" ${ARGN})
93 file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
94 set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
95 file(MAKE_DIRECTORY ${_temporary_files_path})
96 # Note that the quotation marks in the next line form part of
97 # the defined symbol, so that the macro replacement in the
98 # source file is as a string.
99 # These are only needed for unittest_main.cpp, but for simplicity used
100 # for the whole target (since there may be multiple executables in the
101 # same directory, it is not straightforward to use a source file
103 set(EXTRA_COMPILE_DEFINITIONS
104 TEST_DATA_PATH="${_input_files_path}"
105 TEST_TEMP_PATH="${_temporary_files_path}")
107 list(APPEND EXTRA_COMPILE_DEFINITIONS
110 if (ARG_HARDWARE_DETECTION)
111 list(APPEND EXTRA_COMPILE_DEFINITIONS
112 TEST_USES_HARDWARE_DETECTION=true)
115 if (GMX_GPU_CUDA AND NOT GMX_CLANG_CUDA)
116 # Work around FindCUDA that prevents using target_link_libraries()
117 # with keywords otherwise...
118 set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
119 cuda_add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
120 ${ARG_CPP_SOURCE_FILES}
121 ${ARG_CUDA_CU_SOURCE_FILES}
122 ${ARG_GPU_CPP_SOURCE_FILES}
123 ${TESTUTILS_DIR}/unittest_main.cpp)
125 add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
126 ${ARG_CPP_SOURCE_FILES}
127 ${TESTUTILS_DIR}/unittest_main.cpp)
132 target_sources(${EXENAME} PRIVATE
133 ${ARG_CUDA_CU_SOURCE_FILES}
134 ${ARG_GPU_CPP_SOURCE_FILES})
135 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
136 gmx_compile_cuda_file_with_clang(${ARG_CUDA_CU_SOURCE_FILES})
137 gmx_compile_cuda_file_with_clang(${ARG_GPU_CPP_SOURCE_FILES})
138 if(ARG_CUDA_CU_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
139 target_link_libraries(${EXENAME} PRIVATE ${GMX_EXTRA_LIBRARIES})
142 elseif (GMX_GPU_OPENCL)
143 target_sources(${EXENAME} PRIVATE ${ARG_OPENCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
144 if(ARG_OPENCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
145 target_link_libraries(${EXENAME} PRIVATE ${OpenCL_LIBRARIES})
147 elseif (GMX_GPU_SYCL)
148 target_sources(${EXENAME} PRIVATE ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
149 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES COMPILE_FLAGS "${SYCL_CXX_FLAGS}")
150 set_source_files_properties(${ARG_SYCL_CPP_SOURCE_FILES} PROPERTIES COMPILE_FLAGS "${SYCL_CXX_FLAGS}")
151 if(ARG_SYCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
152 target_link_libraries(${EXENAME} PRIVATE ${SYCL_CXX_FLAGS})
155 target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
158 gmx_target_compile_options(${EXENAME})
159 target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
160 target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
161 # Permit GROMACS code to include externally developed headers,
162 # such as the functionality from the nonstd project that we
163 # use for gmx::compat::optional. These are included as system
164 # headers so that no warnings are issued from them.
165 target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
167 # Ensure GoogleTest headers can find POSIX things needed
168 target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
171 target_link_libraries(${EXENAME} PRIVATE
172 testutils common libgromacs gmock
173 ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
176 set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
177 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
179 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7")
180 target_compile_options(${EXENAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Weverything ${IGNORED_CLANG_ALL_WARNINGS} -Wno-gnu-zero-variadic-macro-arguments -Wno-zero-as-null-pointer-constant -Wno-missing-variable-declarations>)
182 # clang-3.6 warns about a number of issues that are not reported by more modern compilers
183 # and we know they are not real issues. So we only check that it can compile without error
184 # but ignore all warnings.
185 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION MATCHES "^3\.6")
186 target_compile_options(${EXENAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-w>)
191 # This function can be called with extra options and arguments:
192 # OPENMP_THREADS <N> declares the requirement to run the test binary with N OpenMP
193 # threads (when supported by the build configuration)
194 # MPI_RANKS <N> declares the requirement to run the test binary with N ranks
195 # INTEGRATION_TEST requires the use of the IntegrationTest label in CTest
196 # SLOW_TEST requires the use of the SlowTest label in CTest, and
197 # increase the length of the ctest timeout.
198 # IGNORE_LEAKS Skip some memory safety checks.
200 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
201 # that ctest can run the test binary over a range of numbers of MPI
203 function (gmx_register_gtest_test NAME EXENAME)
204 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
205 set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
206 set(_one_value_args MPI_RANKS OPENMP_THREADS)
207 cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
208 set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
211 if (ARG_INTEGRATION_TEST)
212 list(APPEND _labels IntegrationTest)
213 # Slow build configurations should have longer timeouts.
214 # Both OpenCL (from JIT) and ThreadSanitizer (from how it
215 # checks) can take signficantly more time than other
219 elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
224 elseif (ARG_SLOW_TEST)
225 list(APPEND _labels SlowTest)
228 list(APPEND _labels UnitTest)
229 gmx_get_test_prefix_cmd(_prefix_cmd)
231 if (ARG_IGNORE_LEAKS)
232 gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
234 set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
235 if (ARG_OPENMP_THREADS)
237 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
241 if (NOT GMX_CAN_RUN_MPI_TESTS)
244 list(APPEND _labels MpiTest)
247 ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
248 ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
249 elseif (GMX_THREAD_MPI)
250 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
253 add_test(NAME ${NAME}
254 COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
255 set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
256 set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
257 add_dependencies(tests ${EXENAME})
261 function (gmx_add_unit_test NAME EXENAME)
262 gmx_add_gtest_executable(${EXENAME} ${ARGN})
263 gmx_register_gtest_test(${NAME} ${EXENAME})
266 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
267 if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
268 gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
269 gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})