Update instructions in containers.rst
[gromacs.git] / src / testutils / TestMacros.cmake
blob5f29e65402c3fa165fbd912982ad04b8282f775b
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)
46         if(GMX_CLANG_TIDY)
47             set_target_properties(${NAME} PROPERTIES CXX_CLANG_TIDY
48                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
49         endif()
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>)
52         endif()
54     endif()
55 endfunction ()
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:
63 #   MPI
64 #     To trigger the ctest runner to run this test with multiple ranks
65 #   HARDWARE_DETECTION
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
84             CPP_SOURCE_FILES
85             CUDA_CU_SOURCE_FILES
86             GPU_CPP_SOURCE_FILES
87             OPENCL_CPP_SOURCE_FILES
88             SYCL_CPP_SOURCE_FILES
89             NON_GPU_CPP_SOURCE_FILES
90             )
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
102         # property).
103         set(EXTRA_COMPILE_DEFINITIONS
104             TEST_DATA_PATH="${_input_files_path}"
105             TEST_TEMP_PATH="${_temporary_files_path}")
106         if (ARG_MPI)
107             list(APPEND EXTRA_COMPILE_DEFINITIONS
108                  TEST_USES_MPI=true)
109         endif()
110         if (ARG_HARDWARE_DETECTION)
111             list(APPEND EXTRA_COMPILE_DEFINITIONS
112                  TEST_USES_HARDWARE_DETECTION=true)
113         endif()
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         else()
124             add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
125                 ${ARG_CPP_SOURCE_FILES})
126         endif()
128         if (GMX_GPU_CUDA)
129             if (GMX_CLANG_CUDA)
130                 target_sources(${EXENAME} PRIVATE
131                     ${ARG_CUDA_CU_SOURCE_FILES}
132                     ${ARG_GPU_CPP_SOURCE_FILES})
133                 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
134                 gmx_compile_cuda_file_with_clang(${ARG_CUDA_CU_SOURCE_FILES})
135                 gmx_compile_cuda_file_with_clang(${ARG_GPU_CPP_SOURCE_FILES})
136                 if(ARG_CUDA_CU_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
137                     target_link_libraries(${EXENAME} PRIVATE ${GMX_EXTRA_LIBRARIES})
138                 endif()
139             endif()
140         elseif (GMX_GPU_OPENCL)
141             target_sources(${EXENAME} PRIVATE ${ARG_OPENCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
142             if(ARG_OPENCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
143                 target_link_libraries(${EXENAME} PRIVATE ${OpenCL_LIBRARIES})
144             endif()
145         elseif (GMX_GPU_SYCL)
146             target_sources(${EXENAME} PRIVATE ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
147             set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES COMPILE_FLAGS "${SYCL_CXX_FLAGS}")
148             set_source_files_properties(${ARG_SYCL_CPP_SOURCE_FILES} PROPERTIES COMPILE_FLAGS "${SYCL_CXX_FLAGS}")
149             if(ARG_SYCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
150                 target_link_libraries(${EXENAME} PRIVATE ${SYCL_CXX_FLAGS})
151             endif()
152         else()
153             target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
154         endif()
156         gmx_target_compile_options(${EXENAME})
157         target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
158         target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
159         # Permit GROMACS code to include externally developed headers,
160         # such as the functionality from the nonstd project that we
161         # use for gmx::compat::optional. These are included as system
162         # headers so that no warnings are issued from them.
163         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
164         if(CYGWIN)
165             # Ensure GoogleTest headers can find POSIX things needed
166             target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
167         endif()
169         target_link_libraries(${EXENAME} PRIVATE
170             testutils common libgromacs gmock
171             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
173         if(GMX_CLANG_TIDY)
174             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
175                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
176         endif()
177         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7")
178             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>)
179         endif()
180         # clang-3.6 warns about a number of issues that are not reported by more modern compilers
181         # and we know they are not real issues. So we only check that it can compile without error
182         # but ignore all warnings.
183         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION MATCHES "^3\.6")
184             target_compile_options(${EXENAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-w>)
185         endif()
186     endif()
187 endfunction()
189 # This function can be called with extra options and arguments:
190 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
191 #                           threads (when supported by the build configuration)
192 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
193 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
194 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
195 #                         increase the length of the ctest timeout.
196 #   IGNORE_LEAKS          Skip some memory safety checks.
198 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
199 # that ctest can run the test binary over a range of numbers of MPI
200 # ranks.
201 function (gmx_register_gtest_test NAME EXENAME)
202     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
203         set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
204         set(_one_value_args MPI_RANKS OPENMP_THREADS)
205         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
206         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
207         set(_labels GTest)
208         set(_timeout 30)
209         if (ARG_INTEGRATION_TEST)
210             list(APPEND _labels IntegrationTest)
211             # Slow build configurations should have longer timeouts.
212             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
213             # checks) can take signficantly more time than other
214             # configurations.
215             if (GMX_GPU_OPENCL)
216                 set(_timeout 240)
217             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
218                 set(_timeout 300)
219             else()
220                 set(_timeout 120)
221             endif()
222         elseif (ARG_SLOW_TEST)
223             list(APPEND _labels SlowTest)
224             set(_timeout 480)
225         else()
226             list(APPEND _labels UnitTest)
227             gmx_get_test_prefix_cmd(_prefix_cmd)
228         endif()
229         if (ARG_IGNORE_LEAKS)
230             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
231         endif ()
232         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
233         if (ARG_OPENMP_THREADS)
234             if (GMX_OPENMP)
235                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
236             endif()
237         endif()
238         if (ARG_MPI_RANKS)
239             if (NOT GMX_CAN_RUN_MPI_TESTS)
240                 return()
241             endif()
242             list(APPEND _labels MpiTest)
243             if (GMX_MPI)
244                 set(_cmd
245                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
246                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
247             elseif (GMX_THREAD_MPI)
248                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
249             endif()
250         endif()
251         add_test(NAME ${NAME}
252                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
253         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
254         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
255         add_dependencies(tests ${EXENAME})
256     endif()
257 endfunction ()
259 function (gmx_add_unit_test NAME EXENAME)
260     gmx_add_gtest_executable(${EXENAME} ${ARGN})
261     gmx_register_gtest_test(${NAME} ${EXENAME})
262 endfunction()
264 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
265     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
266         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
267         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
268     endif()
269 endfunction()