bf2f3518dfd49225f28b0e9d3e8fa016e9f873e9
[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,2016,2017,2018,2019, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 include(CMakeParseArguments)
36
37 function (gmx_add_unit_test_object_library NAME)
38     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
39         include_directories(BEFORE SYSTEM ${GMOCK_INCLUDE_DIRS})
40         add_library(${NAME} OBJECT ${UNITTEST_TARGET_OPTIONS} ${ARGN})
41         set_property(TARGET ${NAME} APPEND PROPERTY COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}")
42         set_property(TARGET ${NAME} APPEND PROPERTY COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
43     endif()
44 endfunction ()
45
46 function (gmx_add_gtest_executable EXENAME)
47     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
48         set(_options MPI HARDWARE_DETECTION)
49         cmake_parse_arguments(ARG "${_options}" "" "" ${ARGN})
50         set(_source_files ${ARG_UNPARSED_ARGUMENTS})
51
52         file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
53         set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
54         file(MAKE_DIRECTORY ${_temporary_files_path})
55         # Note that the quotation marks in the next line form part of
56         # the defined symbol, so that the macro replacement in the
57         # source file is as a string.
58         # These are only needed for unittest_main.cpp, but for simplicity used
59         # for the whole target (since there may be multiple executables in the
60         # same directory, it is not straightforward to use a source file
61         # property).
62         set(EXTRA_COMPILE_DEFINITIONS
63             TEST_DATA_PATH="${_input_files_path}"
64             TEST_TEMP_PATH="${_temporary_files_path}")
65         if (ARG_MPI)
66             list(APPEND EXTRA_COMPILE_DEFINITIONS
67                  TEST_USES_MPI=true)
68         endif()
69         if (ARG_HARDWARE_DETECTION)
70             list(APPEND EXTRA_COMPILE_DEFINITIONS
71                  TEST_USES_HARDWARE_DETECTION=true)
72         endif()
73
74         include_directories(BEFORE SYSTEM ${GMOCK_INCLUDE_DIRS})
75         add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
76             ${_source_files} ${TESTUTILS_DIR}/unittest_main.cpp)
77         target_link_libraries(${EXENAME}
78             ${TESTUTILS_LIBS} libgromacs ${GMOCK_LIBRARIES}
79             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
80         set_property(TARGET ${EXENAME}
81             APPEND PROPERTY COMPILE_FLAGS "${GMOCK_COMPILE_FLAGS}")
82         set_property(TARGET ${EXENAME}
83             APPEND PROPERTY COMPILE_DEFINITIONS "${GMOCK_COMPILE_DEFINITIONS}")
84         set_property(TARGET ${EXENAME}
85             APPEND PROPERTY COMPILE_DEFINITIONS "${EXTRA_COMPILE_DEFINITIONS}")
86
87         # Permit GROMACS code to include externally developed headers,
88         # such as the functionality from the nonstd project that we
89         # use for gmx::compat::optional. These are included as system
90         # headers so that no warnings are issued from them.
91         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
92
93         if(GMX_CLANG_TIDY)
94             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
95                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
96         endif()
97         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION MATCHES "^6\.0")
98             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>)
99         endif()
100     endif()
101 endfunction()
102
103 # This function can be called with extra options and arguments:
104 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
105 #                           threads (when supported by the build configuration)
106 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
107 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
108 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
109 #                         increase the length of the ctest timeout.
110 #
111 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
112 # that ctest can run the test binary over a range of numbers of MPI
113 # ranks.
114 function (gmx_register_gtest_test NAME EXENAME)
115     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
116         set(_options INTEGRATION_TEST SLOW_TEST)
117         set(_one_value_args MPI_RANKS OPENMP_THREADS)
118         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
119         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
120         set(_labels GTest)
121         set(_timeout 30)
122         if (ARG_INTEGRATION_TEST)
123             list(APPEND _labels IntegrationTest)
124             # Slow build configurations should have longer timeouts.
125             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
126             # checks) can take signficantly more time than other
127             # configurations.
128             if (GMX_USE_OPENCL)
129                 set(_timeout 240)
130             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
131                 set(_timeout 300)
132             else()
133                 set(_timeout 120)
134             endif()
135             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
136         elseif (ARG_SLOW_TEST)
137             list(APPEND _labels SlowTest)
138             set(_timeout 480)
139             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
140         else()
141             list(APPEND _labels UnitTest)
142             gmx_get_test_prefix_cmd(_prefix_cmd)
143         endif()
144         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
145         if (ARG_OPENMP_THREADS)
146             if (GMX_OPENMP)
147                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
148             endif()
149         endif()
150         if (ARG_MPI_RANKS)
151             if (NOT GMX_CAN_RUN_MPI_TESTS)
152                 return()
153             endif()
154             list(APPEND _labels MpiTest)
155             if (GMX_MPI)
156                 set(_cmd
157                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
158                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
159             elseif (GMX_THREAD_MPI)
160                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
161             endif()
162         endif()
163         add_test(NAME ${NAME}
164                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
165         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
166         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
167         add_dependencies(tests ${EXENAME})
168     endif()
169 endfunction ()
170
171 function (gmx_add_unit_test NAME EXENAME)
172     gmx_add_gtest_executable(${EXENAME} ${ARGN})
173     gmx_register_gtest_test(${NAME} ${EXENAME})
174 endfunction()
175
176 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
177     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
178         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
179         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
180     endif()
181 endfunction()