8d5cf3b733863c5e4d248a3d60fce86fb8e4833e
[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, 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.
9 #
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.
14 #
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.
19 #
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.
24 #
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.
32 #
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.
35
36 include(CMakeParseArguments)
37 include(gmxClangCudaUtils)
38
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()
53
54     endif()
55 endfunction ()
56
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.
61 #
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 #   NON_GPU_CPP_SOURCE_FILES  file1.cpp file2.cpp ...
77 #     All the other C++ .cpp source files needed only with neither OpenCL nor CUDA
78 function (gmx_add_gtest_executable EXENAME)
79     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
80         set(_options MPI HARDWARE_DETECTION)
81         set(_multi_value_keywords
82             CPP_SOURCE_FILES
83             CUDA_CU_SOURCE_FILES
84             GPU_CPP_SOURCE_FILES
85             OPENCL_CPP_SOURCE_FILES
86             NON_GPU_CPP_SOURCE_FILES
87             )
88         cmake_parse_arguments(ARG "${_options}" "" "${_multi_value_keywords}" ${ARGN})
89
90         file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
91         set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
92         file(MAKE_DIRECTORY ${_temporary_files_path})
93         # Note that the quotation marks in the next line form part of
94         # the defined symbol, so that the macro replacement in the
95         # source file is as a string.
96         # These are only needed for unittest_main.cpp, but for simplicity used
97         # for the whole target (since there may be multiple executables in the
98         # same directory, it is not straightforward to use a source file
99         # property).
100         set(EXTRA_COMPILE_DEFINITIONS
101             TEST_DATA_PATH="${_input_files_path}"
102             TEST_TEMP_PATH="${_temporary_files_path}")
103         if (ARG_MPI)
104             list(APPEND EXTRA_COMPILE_DEFINITIONS
105                  TEST_USES_MPI=true)
106         endif()
107         if (ARG_HARDWARE_DETECTION)
108             list(APPEND EXTRA_COMPILE_DEFINITIONS
109                  TEST_USES_HARDWARE_DETECTION=true)
110         endif()
111
112         if (GMX_USE_CUDA AND NOT GMX_CLANG_CUDA)
113             # Work around FindCUDA that prevents using target_link_libraries()
114             # with keywords otherwise...
115             set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
116             cuda_add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
117                 ${ARG_CPP_SOURCE_FILES}
118                 ${ARG_CUDA_CU_SOURCE_FILES}
119                 ${ARG_GPU_CPP_SOURCE_FILES}
120                 ${TESTUTILS_DIR}/unittest_main.cpp)
121         else()
122             add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
123                 ${ARG_CPP_SOURCE_FILES}
124                 ${TESTUTILS_DIR}/unittest_main.cpp)
125         endif()
126
127         if (GMX_USE_CUDA)
128             if (GMX_CLANG_CUDA)
129                 target_sources(${EXENAME} PRIVATE
130                     ${ARG_CUDA_CU_SOURCE_FILES}
131                     ${ARG_GPU_CPP_SOURCE_FILES})
132                 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
133                 gmx_compile_cuda_file_with_clang(${ARG_CUDA_CU_SOURCE_FILES})
134                 if(ARG_CUDA_CU_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
135                     target_link_libraries(${EXENAME} PRIVATE ${GMX_EXTRA_LIBRARIES})
136                 endif()
137             endif()
138         elseif (GMX_USE_OPENCL)
139             target_sources(${EXENAME} PRIVATE ${ARG_OPENCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
140             if(ARG_OPENCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
141                 target_link_libraries(${EXENAME} PRIVATE ${OpenCL_LIBRARIES})
142             endif()
143         else()
144             target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
145         endif()
146
147         gmx_target_compile_options(${EXENAME})
148         target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
149         target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
150         # Permit GROMACS code to include externally developed headers,
151         # such as the functionality from the nonstd project that we
152         # use for gmx::compat::optional. These are included as system
153         # headers so that no warnings are issued from them.
154         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
155         if(CYGWIN)
156             # Ensure GoogleTest headers can find POSIX things needed
157             target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
158         endif()
159
160         target_link_libraries(${EXENAME} PRIVATE
161             testutils libgromacs gmock
162             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
163
164         if(GMX_CLANG_TIDY)
165             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
166                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
167         endif()
168         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7")
169             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>)
170         endif()
171         # clang-3.6 warns about a number of issues that are not reported by more modern compilers
172         # and we know they are not real issues. So we only check that it can compile without error
173         # but ignore all warnings.
174         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION MATCHES "^3\.6")
175             target_compile_options(${EXENAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-w>)
176         endif()
177     endif()
178 endfunction()
179
180 # This function can be called with extra options and arguments:
181 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
182 #                           threads (when supported by the build configuration)
183 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
184 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
185 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
186 #                         increase the length of the ctest timeout.
187 #   IGNORE_LEAKS          Skip some memory safety checks.
188 #
189 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
190 # that ctest can run the test binary over a range of numbers of MPI
191 # ranks.
192 function (gmx_register_gtest_test NAME EXENAME)
193     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
194         set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
195         set(_one_value_args MPI_RANKS OPENMP_THREADS)
196         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
197         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
198         set(_labels GTest)
199         set(_timeout 30)
200         if (ARG_INTEGRATION_TEST)
201             list(APPEND _labels IntegrationTest)
202             # Slow build configurations should have longer timeouts.
203             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
204             # checks) can take signficantly more time than other
205             # configurations.
206             if (GMX_USE_OPENCL)
207                 set(_timeout 240)
208             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
209                 set(_timeout 300)
210             else()
211                 set(_timeout 120)
212             endif()
213         elseif (ARG_SLOW_TEST)
214             list(APPEND _labels SlowTest)
215             set(_timeout 480)
216         else()
217             list(APPEND _labels UnitTest)
218             gmx_get_test_prefix_cmd(_prefix_cmd)
219         endif()
220         if (ARG_IGNORE_LEAKS)
221             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
222         endif ()
223         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
224         if (ARG_OPENMP_THREADS)
225             if (GMX_OPENMP)
226                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
227             endif()
228         endif()
229         if (ARG_MPI_RANKS)
230             if (NOT GMX_CAN_RUN_MPI_TESTS)
231                 return()
232             endif()
233             list(APPEND _labels MpiTest)
234             if (GMX_MPI)
235                 set(_cmd
236                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
237                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
238             elseif (GMX_THREAD_MPI)
239                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
240             endif()
241         endif()
242         add_test(NAME ${NAME}
243                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
244         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
245         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
246         add_dependencies(tests ${EXENAME})
247     endif()
248 endfunction ()
249
250 function (gmx_add_unit_test NAME EXENAME)
251     gmx_add_gtest_executable(${EXENAME} ${ARGN})
252     gmx_register_gtest_test(${NAME} ${EXENAME})
253 endfunction()
254
255 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
256     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
257         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
258         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
259     endif()
260 endfunction()