Split lines with many copyright years
[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
38 function (gmx_add_unit_test_library NAME)
39     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
40         add_library(${NAME} STATIC ${UNITTEST_TARGET_OPTIONS} ${ARGN})
41         gmx_target_compile_options(${NAME})
42         target_compile_definitions(${NAME} PRIVATE HAVE_CONFIG_H)
43         target_include_directories(${NAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
44         target_link_libraries(${NAME} PRIVATE testutils gmock)
45     endif()
46 endfunction ()
47
48 function (gmx_add_gtest_executable EXENAME)
49     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
50         set(_options MPI HARDWARE_DETECTION)
51         cmake_parse_arguments(ARG "${_options}" "" "" ${ARGN})
52         set(_source_files ${ARG_UNPARSED_ARGUMENTS})
53
54         file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
55         set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
56         file(MAKE_DIRECTORY ${_temporary_files_path})
57         # Note that the quotation marks in the next line form part of
58         # the defined symbol, so that the macro replacement in the
59         # source file is as a string.
60         # These are only needed for unittest_main.cpp, but for simplicity used
61         # for the whole target (since there may be multiple executables in the
62         # same directory, it is not straightforward to use a source file
63         # property).
64         set(EXTRA_COMPILE_DEFINITIONS
65             TEST_DATA_PATH="${_input_files_path}"
66             TEST_TEMP_PATH="${_temporary_files_path}")
67         if (ARG_MPI)
68             list(APPEND EXTRA_COMPILE_DEFINITIONS
69                  TEST_USES_MPI=true)
70         endif()
71         if (ARG_HARDWARE_DETECTION)
72             list(APPEND EXTRA_COMPILE_DEFINITIONS
73                  TEST_USES_HARDWARE_DETECTION=true)
74         endif()
75
76         add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
77             ${_source_files} ${TESTUTILS_DIR}/unittest_main.cpp)
78         gmx_target_compile_options(${EXENAME})
79         target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
80         target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
81         # Permit GROMACS code to include externally developed headers,
82         # such as the functionality from the nonstd project that we
83         # use for gmx::compat::optional. These are included as system
84         # headers so that no warnings are issued from them.
85         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
86
87         target_link_libraries(${EXENAME} PRIVATE
88             testutils libgromacs gmock
89             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
90
91         if(GMX_CLANG_TIDY)
92             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
93                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
94         endif()
95         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION MATCHES "^6\.0")
96             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>)
97         endif()
98     endif()
99 endfunction()
100
101 # This function can be called with extra options and arguments:
102 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
103 #                           threads (when supported by the build configuration)
104 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
105 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
106 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
107 #                         increase the length of the ctest timeout.
108 #
109 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
110 # that ctest can run the test binary over a range of numbers of MPI
111 # ranks.
112 function (gmx_register_gtest_test NAME EXENAME)
113     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
114         set(_options INTEGRATION_TEST SLOW_TEST)
115         set(_one_value_args MPI_RANKS OPENMP_THREADS)
116         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
117         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
118         set(_labels GTest)
119         set(_timeout 30)
120         if (ARG_INTEGRATION_TEST)
121             list(APPEND _labels IntegrationTest)
122             # Slow build configurations should have longer timeouts.
123             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
124             # checks) can take signficantly more time than other
125             # configurations.
126             if (GMX_USE_OPENCL)
127                 set(_timeout 240)
128             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
129                 set(_timeout 300)
130             else()
131                 set(_timeout 120)
132             endif()
133             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
134         elseif (ARG_SLOW_TEST)
135             list(APPEND _labels SlowTest)
136             set(_timeout 480)
137             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
138         else()
139             list(APPEND _labels UnitTest)
140             gmx_get_test_prefix_cmd(_prefix_cmd)
141         endif()
142         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
143         if (ARG_OPENMP_THREADS)
144             if (GMX_OPENMP)
145                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
146             endif()
147         endif()
148         if (ARG_MPI_RANKS)
149             if (NOT GMX_CAN_RUN_MPI_TESTS)
150                 return()
151             endif()
152             list(APPEND _labels MpiTest)
153             if (GMX_MPI)
154                 set(_cmd
155                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
156                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
157             elseif (GMX_THREAD_MPI)
158                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
159             endif()
160         endif()
161         add_test(NAME ${NAME}
162                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
163         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
164         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
165         add_dependencies(tests ${EXENAME})
166     endif()
167 endfunction ()
168
169 function (gmx_add_unit_test NAME EXENAME)
170     gmx_add_gtest_executable(${EXENAME} ${ARGN})
171     gmx_register_gtest_test(${NAME} ${EXENAME})
172 endfunction()
173
174 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
175     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
176         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
177         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
178     endif()
179 endfunction()