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.
6 # Copyright (c) 2021, by the GROMACS development team, led by
7 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 # and including many others, as listed in the AUTHORS file in the
9 # top-level source directory and at http://www.gromacs.org.
11 # GROMACS is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU Lesser General Public License
13 # as published by the Free Software Foundation; either version 2.1
14 # of the License, or (at your option) any later version.
16 # GROMACS is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 # Lesser General Public License for more details.
21 # You should have received a copy of the GNU Lesser General Public
22 # License along with GROMACS; if not, see
23 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 # If you want to redistribute modifications to GROMACS, please
27 # consider that scientific software is very special. Version
28 # control is crucial - bugs must be traceable. We will be happy to
29 # consider code for inclusion in the official distribution, but
30 # derived work must not be called official GROMACS. Details are found
31 # in the README & COPYING files - if they are missing, get the
32 # official version at http://www.gromacs.org.
34 # To help us fund GROMACS development, we humbly ask that you cite
35 # the research papers on the package. Check out http://www.gromacs.org.
37 include(CMakeParseArguments)
38 include(gmxClangCudaUtils)
40 function (gmx_add_unit_test_library NAME)
41 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
42 add_library(${NAME} STATIC ${UNITTEST_TARGET_OPTIONS} ${ARGN})
43 gmx_target_compile_options(${NAME})
44 target_compile_definitions(${NAME} PRIVATE HAVE_CONFIG_H)
45 target_include_directories(${NAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
46 target_link_libraries(${NAME} PRIVATE testutils gmock)
48 set_target_properties(${NAME} PROPERTIES CXX_CLANG_TIDY
49 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
51 gmx_warn_on_everything(${NAME})
52 if (HAS_WARNING_EVERYTHING)
53 # Some false positives exist produced by GoogleTest implementation
54 gmx_target_warning_suppression(${NAME} "-Wno-zero-as-null-pointer-constant" HAS_WARNING_NO_ZERO_AS_NULL_POINTER_CONSTANT)
55 gmx_target_warning_suppression(${NAME} "-Wno-gnu-zero-variadic-macro-arguments" HAS_WARNING_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
56 # Use of GoogleMock can generate mock member functions that are unused
57 gmx_target_warning_suppression(${NAME} "-Wno-unused-member-function" HAS_WARNING_NO_UNUSED_MEMBER_FUNCTION)
59 # CUDA headers target C, so use old-style casts that clang
60 # warns about when it is the host compiler
61 gmx_target_warning_suppression(${NAME} "-Wno-old-style-cast" HAS_NO_OLD_STYLE_CAST)
67 # This function creates a GoogleTest test executable for a module. It
68 # hides all the complexity of how to treat different source files
69 # under different configuration conditions. It should be extended
70 # if we ever support another GPU compilation approach.
72 # It can be called with extra options and arguments:
74 # To trigger the ctest runner to run this test with multiple ranks
76 # To trigger the test executable setup code to run hardware detection
77 # CPP_SOURCE_FILES file1.cpp file2.cpp ...
78 # All the normal C++ .cpp source files
79 # GPU_CPP_SOURCE_FILES file1.cpp file2.cpp ...
80 # All the C++ .cpp source files that are always needed, but must be
81 # compiled in the way that suits GMX_GPU.
82 # CUDA_CU_SOURCE_FILES file1.cu file2.cu ...
83 # All the normal CUDA .cu source files
84 # OPENCL_CPP_SOURCE_FILES file1.cpp file2.cpp ...
85 # All the other C++ .cpp source files needed only with OpenCL
86 # SYCL_CPP_SOURCE_FILES file1.cpp file2.cpp ...
87 # All the C++ .cpp source files needed only with SYCL
88 # NON_GPU_CPP_SOURCE_FILES file1.cpp file2.cpp ...
89 # All the other C++ .cpp source files needed only with neither OpenCL nor CUDA nor SYCL
90 function (gmx_add_gtest_executable EXENAME)
91 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
92 set(_options MPI HARDWARE_DETECTION)
93 set(_multi_value_keywords
97 OPENCL_CPP_SOURCE_FILES
99 NON_GPU_CPP_SOURCE_FILES
101 cmake_parse_arguments(ARG "${_options}" "" "${_multi_value_keywords}" ${ARGN})
103 file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
104 set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
105 file(MAKE_DIRECTORY ${_temporary_files_path})
106 # Note that the quotation marks in the next line form part of
107 # the defined symbol, so that the macro replacement in the
108 # source file is as a string.
109 # These are only needed for unittest_main.cpp, but for simplicity used
110 # for the whole target (since there may be multiple executables in the
111 # same directory, it is not straightforward to use a source file
113 set(EXTRA_COMPILE_DEFINITIONS
114 TEST_DATA_PATH="${_input_files_path}"
115 TEST_TEMP_PATH="${_temporary_files_path}")
117 list(APPEND EXTRA_COMPILE_DEFINITIONS
120 if (ARG_HARDWARE_DETECTION)
121 list(APPEND EXTRA_COMPILE_DEFINITIONS
122 TEST_USES_HARDWARE_DETECTION=true)
125 if (GMX_GPU_CUDA AND NOT GMX_CLANG_CUDA)
126 # Work around FindCUDA that prevents using target_link_libraries()
127 # with keywords otherwise...
128 set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
129 cuda_add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
130 ${ARG_CPP_SOURCE_FILES}
131 ${ARG_CUDA_CU_SOURCE_FILES}
132 ${ARG_GPU_CPP_SOURCE_FILES})
134 add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
135 ${ARG_CPP_SOURCE_FILES})
140 target_sources(${EXENAME} PRIVATE
141 ${ARG_CUDA_CU_SOURCE_FILES}
142 ${ARG_GPU_CPP_SOURCE_FILES})
143 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
144 gmx_compile_cuda_file_with_clang(${ARG_CUDA_CU_SOURCE_FILES})
145 gmx_compile_cuda_file_with_clang(${ARG_GPU_CPP_SOURCE_FILES})
146 if(ARG_CUDA_CU_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
147 target_link_libraries(${EXENAME} PRIVATE ${GMX_EXTRA_LIBRARIES})
150 elseif (GMX_GPU_OPENCL)
151 target_sources(${EXENAME} PRIVATE ${ARG_OPENCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
152 if(ARG_OPENCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
153 target_link_libraries(${EXENAME} PRIVATE ${OpenCL_LIBRARIES})
155 elseif (GMX_GPU_SYCL)
156 target_sources(${EXENAME} PRIVATE ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
157 if(ARG_SYCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
160 SOURCES ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES}
164 target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
167 gmx_target_compile_options(${EXENAME})
168 target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
169 target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
170 # Permit GROMACS code to include externally developed headers,
171 # such as the functionality from the nonstd project that we
172 # use for gmx::compat::optional. These are included as system
173 # headers so that no warnings are issued from them.
174 target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
175 target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external/muparser)
177 # Ensure GoogleTest headers can find POSIX things needed
178 target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
181 target_link_libraries(${EXENAME} PRIVATE
182 testutils common libgromacs gmock
183 ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
186 set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
187 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
189 gmx_warn_on_everything(${EXENAME})
190 if (HAS_WARNING_EVERYTHING)
191 # Some false positives exist produced by GoogleTest implementation
192 gmx_target_warning_suppression(${EXENAME} "-Wno-zero-as-null-pointer-constant" HAS_WARNING_NO_ZERO_AS_NULL_POINTER_CONSTANT)
193 gmx_target_warning_suppression(${EXENAME} "-Wno-gnu-zero-variadic-macro-arguments" HAS_WARNING_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
194 # Use of GoogleMock can generate mock member functions that are unused
195 gmx_target_warning_suppression(${EXENAME} "-Wno-unused-member-function" HAS_WARNING_NO_UNUSED_MEMBER_FUNCTION)
197 # CUDA headers target C, so use old-style casts that clang
198 # warns about when it is the host compiler
199 gmx_target_warning_suppression(${EXENAME} "-Wno-old-style-cast" HAS_NO_OLD_STYLE_CAST)
205 # This function can be called with extra options and arguments:
206 # OPENMP_THREADS <N> declares the requirement to run the test binary with N OpenMP
207 # threads (when supported by the build configuration)
208 # MPI_RANKS <N> declares the requirement to run the test binary with N ranks
209 # INTEGRATION_TEST requires the use of the IntegrationTest label in CTest
210 # SLOW_TEST requires the use of the SlowTest label in CTest, and
211 # increase the length of the ctest timeout.
212 # IGNORE_LEAKS Skip some memory safety checks.
214 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
215 # that ctest can run the test binary over a range of numbers of MPI
217 function (gmx_register_gtest_test NAME EXENAME)
218 if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
219 set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
220 set(_one_value_args MPI_RANKS OPENMP_THREADS)
221 cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
222 set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
225 if (ARG_INTEGRATION_TEST)
226 list(APPEND _labels IntegrationTest)
227 # Slow build configurations should have longer timeouts.
228 # Both OpenCL (from JIT) and ThreadSanitizer (from how it
229 # checks) can take signficantly more time than other
231 if (GMX_GPU_OPENCL OR GMX_GPU_SYCL)
233 elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
238 elseif (ARG_SLOW_TEST)
239 list(APPEND _labels SlowTest)
242 list(APPEND _labels UnitTest)
243 gmx_get_test_prefix_cmd(_prefix_cmd)
245 if (ARG_IGNORE_LEAKS)
246 gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
248 set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
249 if (ARG_OPENMP_THREADS)
251 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
255 if (NOT GMX_CAN_RUN_MPI_TESTS)
258 list(APPEND _labels MpiTest)
261 ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
262 ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
263 elseif (GMX_THREAD_MPI)
264 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
267 add_test(NAME ${NAME}
268 COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
269 set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
270 set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
271 add_dependencies(tests ${EXENAME})
275 function (gmx_add_unit_test NAME EXENAME)
276 gmx_add_gtest_executable(${EXENAME} ${ARGN})
277 gmx_register_gtest_test(${NAME} ${EXENAME})
280 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
281 if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
282 gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
283 gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})