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