]> git.mxchange.org Git - simgear.git/blob - CMakeModules/BoostTestTargets.cmake
Let ENABLE_TESTS enable/disable also the new Boost.Tests.
[simgear.git] / CMakeModules / BoostTestTargets.cmake
1 # - Add tests using boost::test
2 #
3 # Add this line to your test files in place of including a basic boost test header:
4 #  #include <BoostTestTargetConfig.h>
5 #
6 # If you cannot do that and must use the included form for a given test,
7 # include the line
8 #  // OVERRIDE_BOOST_TEST_INCLUDED_WARNING
9 # in the same file with the boost test include.
10 #
11 #  include(BoostTestTargets)
12 #  add_boost_test(<testdriver_name> SOURCES <source1> [<more sources...>]
13 #   [FAIL_REGULAR_EXPRESSION <additional fail regex>]
14 #   [LAUNCHER <generic launcher script>]
15 #   [LIBRARIES <library> [<library>...]]
16 #   [RESOURCES <resource> [<resource>...]]
17 #   [TESTS <testcasename> [<testcasename>...]])
18 #
19 #  If for some reason you need access to the executable target created,
20 #  it can be found in ${${testdriver_name}_TARGET_NAME} as specified when
21 #  you called add_boost_test
22 #
23 # Requires CMake 2.6 or newer (uses the 'function' command)
24 #
25 # Requires:
26 #       GetForceIncludeDefinitions
27 #       CopyResourcesToBuildTree
28 #
29 # Original Author:
30 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
31 # http://academic.cleardefinition.com
32 # Iowa State University HCI Graduate Program/VRAC
33 #
34 # Copyright Iowa State University 2009-2010.
35 # Distributed under the Boost Software License, Version 1.0.
36 # (See accompanying file LICENSE_1_0.txt or copy at
37 # http://www.boost.org/LICENSE_1_0.txt)
38
39 if(__add_boost_test)
40         return()
41 endif()
42 set(__add_boost_test YES)
43
44 set(BOOST_TEST_TARGET_PREFIX "test")
45
46 if(NOT Boost_FOUND)
47         find_package(Boost 1.34.0 QUIET)
48 endif()
49 if("${Boost_VERSION}0" LESS "1034000")
50         set(_shared_msg
51                 "NOTE: boost::test-based targets and tests cannot "
52                 "be added: boost >= 1.34.0 required but not found. "
53                 "(found: '${Boost_VERSION}'; want >=103400) ")
54         if(ENABLE_TESTS)
55                 message(FATAL_ERROR
56                         ${_shared_msg}
57                         "You may disable ENABLE_TESTS to continue without the "
58                         "tests.")
59         else()
60                 message(STATUS
61                         ${_shared_msg}
62                         "ENABLE_TESTS disabled, so continuing anyway.")
63         endif()
64 endif()
65
66 include(GetForceIncludeDefinitions)
67 include(CopyResourcesToBuildTree)
68
69 if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000")
70         set(_boosttesttargets_libs)
71         set(_boostConfig "BoostTestTargetsIncluded.h")
72         if(NOT Boost_UNIT_TEST_FRAMEWORK_LIBRARY)
73                 find_package(Boost 1.34.0 QUIET COMPONENTS unit_test_framework)
74         endif()
75         if(Boost_UNIT_TEST_FRAMEWORK_LIBRARY)
76                 set(_boosttesttargets_libs "${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}")
77                 if(Boost_USE_STATIC_LIBS)
78                         set(_boostConfig "BoostTestTargetsStatic.h")
79                 else()
80                         set(_boostConfig "BoostTestTargetsDynamic.h")
81                 endif()
82         endif()
83         get_filename_component(_moddir ${CMAKE_CURRENT_LIST_FILE} PATH)
84         configure_file("${_moddir}/${_boostConfig}"
85                 "${CMAKE_CURRENT_BINARY_DIR}/BoostTestTargetConfig.h"
86                 COPYONLY)
87         include_directories("${CMAKE_CURRENT_BINARY_DIR}")
88 endif()
89
90 function(add_boost_test _name)
91         if(NOT ENABLE_TESTS)
92                 return()
93         endif()
94
95         # parse arguments
96         set(_nowhere)
97         set(_curdest _nowhere)
98         set(_val_args
99                 SOURCES
100                 FAIL_REGULAR_EXPRESSION
101                 LAUNCHER
102                 LIBRARIES
103                 RESOURCES
104                 TESTS)
105         set(_bool_args
106                 USE_COMPILED_LIBRARY)
107         foreach(_arg ${_val_args} ${_bool_args})
108                 set(${_arg})
109         endforeach()
110         foreach(_element ${ARGN})
111                 list(FIND _val_args "${_element}" _val_arg_find)
112                 list(FIND _bool_args "${_element}" _bool_arg_find)
113                 if("${_val_arg_find}" GREATER "-1")
114                         set(_curdest "${_element}")
115                 elseif("${_bool_arg_find}" GREATER "-1")
116                         set("${_element}" ON)
117                         set(_curdest _nowhere)
118                 else()
119                         list(APPEND ${_curdest} "${_element}")
120                 endif()
121         endforeach()
122
123         if(_nowhere)
124                 message(FATAL_ERROR "Syntax error in use of add_boost_test!")
125         endif()
126
127         if(NOT SOURCES)
128                 message(FATAL_ERROR
129                         "Syntax error in use of add_boost_test: at least one source file required!")
130         endif()
131
132         if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000")
133
134                 include_directories(${Boost_INCLUDE_DIRS})
135
136                 set(includeType)
137                 foreach(src ${SOURCES})
138                         file(READ ${src} thefile)
139                         if("${thefile}" MATCHES ".*BoostTestTargetConfig.h.*")
140                                 set(includeType CONFIGURED)
141                                 set(includeFileLoc ${src})
142                                 break()
143                         elseif("${thefile}" MATCHES ".*boost/test/included/unit_test.hpp.*")
144                                 set(includeType INCLUDED)
145                                 set(includeFileLoc ${src})
146                                 set(_boosttesttargets_libs)     # clear this out - linking would be a bad idea
147                                 if(NOT
148                                         "${thefile}"
149                                         MATCHES
150                                         ".*OVERRIDE_BOOST_TEST_INCLUDED_WARNING.*")
151                                         message("Please replace the include line in ${src} with this alternate include line instead:")
152                                         message("  \#include <BoostTestTargetConfig.h>")
153                                         message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)")
154                                 endif()
155                                 break()
156                         endif()
157                 endforeach()
158
159                 if(NOT _boostTestTargetsNagged${_name} STREQUAL "${includeType}")
160                         if("includeType" STREQUAL "CONFIGURED")
161                                 message(STATUS
162                                         "Test '${_name}' uses the CMake-configurable form of the boost test framework - congrats! (Including File: ${includeFileLoc})")
163                         elseif("${includeType}" STREQUAL "INCLUDED")
164                                 message("In test '${_name}': ${includeFileLoc} uses the 'included' form of the boost unit test framework.")
165                         else()
166                                 message("In test '${_name}': Didn't detect the CMake-configurable boost test include.")
167                                 message("Please replace your existing boost test include in that test with the following:")
168                                 message("  \#include <BoostTestTargetConfig.h>")
169                                 message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)")
170                         endif()
171                 endif()
172                 set(_boostTestTargetsNagged${_name}
173                         "${includeType}"
174                         CACHE
175                         INTERNAL
176                         ""
177                         FORCE)
178
179
180                 if(RESOURCES)
181                         list(APPEND SOURCES ${RESOURCES})
182                 endif()
183
184                 # Generate a unique target name, using the relative binary dir
185                 # and provided name. (transform all / into _ and remove all other
186                 # non-alphabet characters)
187                 file(RELATIVE_PATH
188                         targetpath
189                         "${CMAKE_BINARY_DIR}"
190                         "${CMAKE_CURRENT_BINARY_DIR}")
191                 string(REGEX REPLACE "[^A-Za-z/_]" "" targetpath "${targetpath}")
192                 string(REPLACE "/" "_" targetpath "${targetpath}")
193
194                 set(_target_name ${BOOST_TEST_TARGET_PREFIX}-${targetpath}-${_name})
195                 set(${_name}_TARGET_NAME "${_target_name}" PARENT_SCOPE)
196
197                 # Build the test.
198                 add_executable(${_target_name} ${SOURCES})
199
200                 list(APPEND LIBRARIES ${_boosttesttargets_libs})
201
202                 if(LIBRARIES)
203                         target_link_libraries(${_target_name} ${LIBRARIES})
204                 endif()
205
206                 if(RESOURCES)
207                         set_property(TARGET ${_target_name} PROPERTY RESOURCE ${RESOURCES})
208                         copy_resources_to_build_tree(${_target_name})
209                 endif()
210
211                 if(NOT Boost_TEST_FLAGS)
212 #                       set(Boost_TEST_FLAGS --catch_system_error=yes --output_format=XML)
213                         set(Boost_TEST_FLAGS --catch_system_error=yes)
214                 endif()
215
216                 # TODO: Figure out why only recent boost handles individual test running properly
217
218                 if(LAUNCHER)
219                         set(_test_command ${LAUNCHER} "\$<TARGET_FILE:${_target_name}>")
220                 else()
221                         set(_test_command ${_target_name})
222                 endif()
223
224                 if(TESTS AND ( "${Boost_VERSION}" VERSION_GREATER "103799" ))
225                         foreach(_test ${TESTS})
226                                 add_test(
227                                         ${_name}-${_test}
228                                         ${_test_command} --run_test=${_test} ${Boost_TEST_FLAGS}
229                                 )
230                                 if(FAIL_REGULAR_EXPRESSION)
231                                         set_tests_properties(${_name}-${_test}
232                                                 PROPERTIES
233                                                 FAIL_REGULAR_EXPRESSION
234                                                 "${FAIL_REGULAR_EXPRESSION}")
235                                 endif()
236                         endforeach()
237                 else()
238                         add_test(
239                                 ${_name}-boost_test
240                                 ${_test_command} ${Boost_TEST_FLAGS}
241                         )
242                         if(FAIL_REGULAR_EXPRESSION)
243                                 set_tests_properties(${_name}-boost_test
244                                         PROPERTIES
245                                         FAIL_REGULAR_EXPRESSION
246                                         "${FAIL_REGULAR_EXPRESSION}")
247                         endif()
248                 endif()
249
250                 # CppCheck the test if we can.
251                 if(COMMAND add_cppcheck)
252                         add_cppcheck(${_target_name} STYLE UNUSED_FUNCTIONS)
253                 endif()
254
255         endif()
256 endfunction()