Rework -Weverything
[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         endif()
57     endif()
58 endfunction ()
59
60 # This function creates a GoogleTest test executable for a module.  It
61 # hides all the complexity of how to treat different source files
62 # under different configuration conditions. It should be extended
63 # if we ever support another GPU compilation approach.
64 #
65 # It can be called with extra options and arguments:
66 #   MPI
67 #     To trigger the ctest runner to run this test with multiple ranks
68 #   HARDWARE_DETECTION
69 #     To trigger the test executable setup code to run hardware detection
70 #   CPP_SOURCE_FILES          file1.cpp file2.cpp ...
71 #     All the normal C++ .cpp source files
72 #   GPU_CPP_SOURCE_FILES  file1.cpp file2.cpp ...
73 #     All the C++ .cpp source files that are always needed, but must be
74 #     compiled in the way that suits GMX_GPU.
75 #   CUDA_CU_SOURCE_FILES      file1.cu  file2.cu  ...
76 #     All the normal CUDA .cu source files
77 #   OPENCL_CPP_SOURCE_FILES   file1.cpp file2.cpp ...
78 #     All the other C++ .cpp source files needed only with OpenCL
79 #   SYCL_CPP_SOURCE_FILES   file1.cpp file2.cpp ...
80 #     All the C++ .cpp source files needed only with SYCL
81 #   NON_GPU_CPP_SOURCE_FILES  file1.cpp file2.cpp ...
82 #     All the other C++ .cpp source files needed only with neither OpenCL nor CUDA nor SYCL
83 function (gmx_add_gtest_executable EXENAME)
84     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
85         set(_options MPI HARDWARE_DETECTION)
86         set(_multi_value_keywords
87             CPP_SOURCE_FILES
88             CUDA_CU_SOURCE_FILES
89             GPU_CPP_SOURCE_FILES
90             OPENCL_CPP_SOURCE_FILES
91             SYCL_CPP_SOURCE_FILES
92             NON_GPU_CPP_SOURCE_FILES
93             )
94         cmake_parse_arguments(ARG "${_options}" "" "${_multi_value_keywords}" ${ARGN})
95
96         file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
97         set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
98         file(MAKE_DIRECTORY ${_temporary_files_path})
99         # Note that the quotation marks in the next line form part of
100         # the defined symbol, so that the macro replacement in the
101         # source file is as a string.
102         # These are only needed for unittest_main.cpp, but for simplicity used
103         # for the whole target (since there may be multiple executables in the
104         # same directory, it is not straightforward to use a source file
105         # property).
106         set(EXTRA_COMPILE_DEFINITIONS
107             TEST_DATA_PATH="${_input_files_path}"
108             TEST_TEMP_PATH="${_temporary_files_path}")
109         if (ARG_MPI)
110             list(APPEND EXTRA_COMPILE_DEFINITIONS
111                  TEST_USES_MPI=true)
112         endif()
113         if (ARG_HARDWARE_DETECTION)
114             list(APPEND EXTRA_COMPILE_DEFINITIONS
115                  TEST_USES_HARDWARE_DETECTION=true)
116         endif()
117
118         if (GMX_GPU_CUDA AND NOT GMX_CLANG_CUDA)
119             # Work around FindCUDA that prevents using target_link_libraries()
120             # with keywords otherwise...
121             set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
122             cuda_add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
123                 ${ARG_CPP_SOURCE_FILES}
124                 ${ARG_CUDA_CU_SOURCE_FILES}
125                 ${ARG_GPU_CPP_SOURCE_FILES})
126         else()
127             add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
128                 ${ARG_CPP_SOURCE_FILES})
129         endif()
130
131         if (GMX_GPU_CUDA)
132             if (GMX_CLANG_CUDA)
133                 target_sources(${EXENAME} PRIVATE
134                     ${ARG_CUDA_CU_SOURCE_FILES}
135                     ${ARG_GPU_CPP_SOURCE_FILES})
136                 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
137                 gmx_compile_cuda_file_with_clang(${ARG_CUDA_CU_SOURCE_FILES})
138                 gmx_compile_cuda_file_with_clang(${ARG_GPU_CPP_SOURCE_FILES})
139                 if(ARG_CUDA_CU_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
140                     target_link_libraries(${EXENAME} PRIVATE ${GMX_EXTRA_LIBRARIES})
141                 endif()
142             endif()
143         elseif (GMX_GPU_OPENCL)
144             target_sources(${EXENAME} PRIVATE ${ARG_OPENCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
145             if(ARG_OPENCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
146                 target_link_libraries(${EXENAME} PRIVATE ${OpenCL_LIBRARIES})
147             endif()
148         elseif (GMX_GPU_SYCL)
149             target_sources(${EXENAME} PRIVATE ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
150             if(ARG_SYCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
151                 add_sycl_to_target(
152                     TARGET ${EXENAME}
153                     SOURCES ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES}
154                     )
155             endif()
156         else()
157             target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
158         endif()
159
160         gmx_target_compile_options(${EXENAME})
161         target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
162         target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
163         # Permit GROMACS code to include externally developed headers,
164         # such as the functionality from the nonstd project that we
165         # use for gmx::compat::optional. These are included as system
166         # headers so that no warnings are issued from them.
167         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
168         if(CYGWIN)
169             # Ensure GoogleTest headers can find POSIX things needed
170             target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
171         endif()
172
173         target_link_libraries(${EXENAME} PRIVATE
174             testutils common libgromacs gmock
175             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
176
177         if(GMX_CLANG_TIDY)
178             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
179                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
180         endif()
181         gmx_warn_on_everything(${EXENAME})
182         if (HAS_WARNING_EVERYTHING)
183             # Some false positives exist produced by GoogleTest implementation
184             gmx_target_warning_suppression(${EXENAME} "-Wno-zero-as-null-pointer-constant" HAS_WARNING_NO_ZERO_AS_NULL_POINTER_CONSTANT)
185             gmx_target_warning_suppression(${EXENAME} "-Wno-gnu-zero-variadic-macro-arguments" HAS_WARNING_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
186         endif()
187     endif()
188 endfunction()
189
190 # This function can be called with extra options and arguments:
191 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
192 #                           threads (when supported by the build configuration)
193 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
194 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
195 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
196 #                         increase the length of the ctest timeout.
197 #   IGNORE_LEAKS          Skip some memory safety checks.
198 #
199 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
200 # that ctest can run the test binary over a range of numbers of MPI
201 # ranks.
202 function (gmx_register_gtest_test NAME EXENAME)
203     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
204         set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
205         set(_one_value_args MPI_RANKS OPENMP_THREADS)
206         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
207         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
208         set(_labels GTest)
209         set(_timeout 30)
210         if (ARG_INTEGRATION_TEST)
211             list(APPEND _labels IntegrationTest)
212             # Slow build configurations should have longer timeouts.
213             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
214             # checks) can take signficantly more time than other
215             # configurations.
216             if (GMX_GPU_OPENCL OR GMX_GPU_SYCL)
217                 set(_timeout 240)
218             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
219                 set(_timeout 300)
220             else()
221                 set(_timeout 120)
222             endif()
223         elseif (ARG_SLOW_TEST)
224             list(APPEND _labels SlowTest)
225             set(_timeout 480)
226         else()
227             list(APPEND _labels UnitTest)
228             gmx_get_test_prefix_cmd(_prefix_cmd)
229         endif()
230         if (ARG_IGNORE_LEAKS)
231             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
232         endif ()
233         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
234         if (ARG_OPENMP_THREADS)
235             if (GMX_OPENMP)
236                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
237             endif()
238         endif()
239         if (ARG_MPI_RANKS)
240             if (NOT GMX_CAN_RUN_MPI_TESTS)
241                 return()
242             endif()
243             list(APPEND _labels MpiTest)
244             if (GMX_MPI)
245                 set(_cmd
246                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
247                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
248             elseif (GMX_THREAD_MPI)
249                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
250             endif()
251         endif()
252         add_test(NAME ${NAME}
253                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
254         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
255         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
256         add_dependencies(tests ${EXENAME})
257     endif()
258 endfunction ()
259
260 function (gmx_add_unit_test NAME EXENAME)
261     gmx_add_gtest_executable(${EXENAME} ${ARGN})
262     gmx_register_gtest_test(${NAME} ${EXENAME})
263 endfunction()
264
265 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
266     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
267         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
268         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
269     endif()
270 endfunction()