Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / testutils / TestMacros.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
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.
10 #
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.
15 #
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.
20 #
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.
25 #
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.
33 #
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.
36
37 include(CMakeParseArguments)
38 include(gmxClangCudaUtils)
39
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)
47         if(GMX_CLANG_TIDY)
48             set_target_properties(${NAME} PROPERTIES CXX_CLANG_TIDY
49                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
50         endif()
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)
58             if(GMX_GPU_CUDA)
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)
62             endif()
63         endif()
64     endif()
65 endfunction ()
66
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.
71 #
72 # It can be called with extra options and arguments:
73 #   MPI
74 #     To trigger the ctest runner to run this test with multiple ranks
75 #   HARDWARE_DETECTION
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
94             CPP_SOURCE_FILES
95             CUDA_CU_SOURCE_FILES
96             GPU_CPP_SOURCE_FILES
97             OPENCL_CPP_SOURCE_FILES
98             SYCL_CPP_SOURCE_FILES
99             NON_GPU_CPP_SOURCE_FILES
100             )
101         cmake_parse_arguments(ARG "${_options}" "" "${_multi_value_keywords}" ${ARGN})
102
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
112         # property).
113         set(EXTRA_COMPILE_DEFINITIONS
114             TEST_DATA_PATH="${_input_files_path}"
115             TEST_TEMP_PATH="${_temporary_files_path}")
116         if (ARG_MPI)
117             list(APPEND EXTRA_COMPILE_DEFINITIONS
118                  TEST_USES_MPI=true)
119         endif()
120         if (ARG_HARDWARE_DETECTION)
121             list(APPEND EXTRA_COMPILE_DEFINITIONS
122                  TEST_USES_HARDWARE_DETECTION=true)
123         endif()
124
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})
133         else()
134             add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
135                 ${ARG_CPP_SOURCE_FILES})
136         endif()
137
138         if (GMX_GPU_CUDA)
139             if (GMX_CLANG_CUDA)
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})
148                 endif()
149             endif()
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})
154             endif()
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)
158                 add_sycl_to_target(
159                     TARGET ${EXENAME}
160                     SOURCES ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES}
161                     )
162             endif()
163         else()
164             target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
165         endif()
166
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         if(CYGWIN)
176             # Ensure GoogleTest headers can find POSIX things needed
177             target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
178         endif()
179
180         target_link_libraries(${EXENAME} PRIVATE
181             testutils common libgromacs gmock
182             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
183
184         if(GMX_CLANG_TIDY)
185             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
186                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
187         endif()
188         gmx_warn_on_everything(${EXENAME})
189         if (HAS_WARNING_EVERYTHING)
190             # Some false positives exist produced by GoogleTest implementation
191             gmx_target_warning_suppression(${EXENAME} "-Wno-zero-as-null-pointer-constant" HAS_WARNING_NO_ZERO_AS_NULL_POINTER_CONSTANT)
192             gmx_target_warning_suppression(${EXENAME} "-Wno-gnu-zero-variadic-macro-arguments" HAS_WARNING_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
193             # Use of GoogleMock can generate mock member functions that are unused
194             gmx_target_warning_suppression(${EXENAME} "-Wno-unused-member-function" HAS_WARNING_NO_UNUSED_MEMBER_FUNCTION)
195             if(GMX_GPU_CUDA)
196                 # CUDA headers target C, so use old-style casts that clang
197                 # warns about when it is the host compiler
198                 gmx_target_warning_suppression(${EXENAME} "-Wno-old-style-cast" HAS_NO_OLD_STYLE_CAST)
199             endif()
200         endif()
201     endif()
202 endfunction()
203
204 # This function can be called with extra options and arguments:
205 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
206 #                           threads (when supported by the build configuration)
207 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
208 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
209 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
210 #                         increase the length of the ctest timeout.
211 #   IGNORE_LEAKS          Skip some memory safety checks.
212 #
213 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
214 # that ctest can run the test binary over a range of numbers of MPI
215 # ranks.
216 function (gmx_register_gtest_test NAME EXENAME)
217     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
218         set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
219         set(_one_value_args MPI_RANKS OPENMP_THREADS)
220         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
221         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
222         set(_labels GTest)
223         set(_timeout 30)
224         if (ARG_INTEGRATION_TEST)
225             list(APPEND _labels IntegrationTest)
226             # Slow build configurations should have longer timeouts.
227             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
228             # checks) can take signficantly more time than other
229             # configurations.
230             if (GMX_GPU_OPENCL OR GMX_GPU_SYCL)
231                 set(_timeout 240)
232             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
233                 set(_timeout 300)
234             else()
235                 set(_timeout 120)
236             endif()
237         elseif (ARG_SLOW_TEST)
238             list(APPEND _labels SlowTest)
239             set(_timeout 480)
240         else()
241             list(APPEND _labels UnitTest)
242             gmx_get_test_prefix_cmd(_prefix_cmd)
243         endif()
244         if (ARG_IGNORE_LEAKS)
245             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
246         endif ()
247         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
248         if (ARG_OPENMP_THREADS)
249             if (GMX_OPENMP)
250                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
251             endif()
252         endif()
253         if (ARG_MPI_RANKS)
254             if (NOT GMX_CAN_RUN_MPI_TESTS)
255                 return()
256             endif()
257             list(APPEND _labels MpiTest)
258             if (GMX_MPI)
259                 set(_cmd
260                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
261                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
262             elseif (GMX_THREAD_MPI)
263                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
264             endif()
265         endif()
266         add_test(NAME ${NAME}
267                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
268         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
269         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
270         add_dependencies(tests ${EXENAME})
271     endif()
272 endfunction ()
273
274 function (gmx_add_unit_test NAME EXENAME)
275     gmx_add_gtest_executable(${EXENAME} ${ARGN})
276     gmx_register_gtest_test(${NAME} ${EXENAME})
277 endfunction()
278
279 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
280     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
281         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
282         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
283     endif()
284 endfunction()