]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | # Copyright 2018, 2019 Peter Dimov |
2 | # Distributed under the Boost Software License, Version 1.0. | |
3 | # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt | |
4 | ||
5 | # Clear global variables on each `include(BoostTest)` | |
6 | ||
7 | set(BOOST_TEST_LINK_LIBRARIES "") | |
8 | set(BOOST_TEST_COMPILE_DEFINITIONS "") | |
9 | set(BOOST_TEST_COMPILE_OPTIONS "") | |
10 | set(BOOST_TEST_COMPILE_FEATURES "") | |
11 | ||
12 | # Include guard | |
13 | ||
14 | if(NOT CMAKE_VERSION VERSION_LESS 3.10) | |
15 | include_guard() | |
16 | endif() | |
17 | ||
18 | include(BoostMessage) | |
19 | ||
20 | # Private helper functions | |
21 | ||
22 | function(__boost_test_list_replace list what with) | |
23 | ||
24 | set(result "") | |
25 | ||
26 | foreach(x IN LISTS ${list}) | |
27 | ||
28 | if(x STREQUAL what) | |
29 | set(x ${with}) | |
30 | endif() | |
31 | ||
32 | list(APPEND result ${x}) | |
33 | ||
34 | endforeach() | |
35 | ||
36 | set(${list} ${result} PARENT_SCOPE) | |
37 | ||
38 | endfunction() | |
39 | ||
40 | # boost_test( [TYPE type] [PREFIX prefix] [NAME name] [IGNORE_TEST_GLOBALS] | |
41 | # SOURCES sources... | |
42 | # ARGUMENTS args... | |
43 | # LINK_LIBRARIES libs... | |
44 | # COMPILE_DEFINITIONS defs... | |
45 | # COMPILE_OPTIONS opts... | |
46 | # COMPILE_FEATURES features... | |
47 | # ) | |
48 | ||
49 | function(boost_test) | |
50 | ||
51 | cmake_parse_arguments(_ "IGNORE_TEST_GLOBALS" "TYPE;PREFIX;NAME" "SOURCES;ARGUMENTS;LIBRARIES;LINK_LIBRARIES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;COMPILE_FEATURES" ${ARGN}) | |
52 | ||
53 | if(NOT __TYPE) | |
54 | set(__TYPE run) | |
55 | endif() | |
56 | ||
57 | if(NOT __PREFIX) | |
58 | set(__PREFIX ${PROJECT_NAME}) | |
59 | endif() | |
60 | ||
61 | if(NOT __NAME) | |
62 | list(GET __SOURCES 0 __NAME) | |
63 | string(MAKE_C_IDENTIFIER ${__NAME} __NAME) | |
64 | endif() | |
65 | ||
66 | set(__NAME ${__PREFIX}-${__NAME}) | |
67 | ||
68 | if(__UNPARSED_ARGUMENTS) | |
69 | message(AUTHOR_WARNING "Extra arguments for test '${__NAME}' ignored: ${__UNPARSED_ARGUMENTS}") | |
70 | endif() | |
71 | ||
72 | if(__LIBRARIES) | |
73 | boost_message(VERBOSE "Test '${__NAME}' uses deprecated parameter LIBRARIES; use LINK_LIBRARIES") | |
74 | endif() | |
75 | ||
76 | if(DEFINED BUILD_TESTING AND NOT BUILD_TESTING) | |
77 | return() | |
78 | endif() | |
79 | ||
80 | if(__IGNORE_TEST_GLOBALS) | |
81 | ||
82 | set(BOOST_TEST_LINK_LIBRARIES "") | |
83 | set(BOOST_TEST_COMPILE_DEFINITIONS "") | |
84 | set(BOOST_TEST_COMPILE_OPTIONS "") | |
85 | set(BOOST_TEST_COMPILE_FEATURES "") | |
86 | ||
87 | endif() | |
88 | ||
89 | list(APPEND BOOST_TEST_LINK_LIBRARIES ${__LIBRARIES} ${__LINK_LIBRARIES}) | |
90 | list(APPEND BOOST_TEST_COMPILE_DEFINITIONS ${__COMPILE_DEFINITIONS}) | |
91 | list(APPEND BOOST_TEST_COMPILE_OPTIONS ${__COMPILE_OPTIONS}) | |
92 | list(APPEND BOOST_TEST_COMPILE_FEATURES ${__COMPILE_FEATURES}) | |
93 | ||
94 | if(MSVC) | |
95 | ||
96 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-fexceptions" "/GX") | |
97 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-fno-exceptions" "/GX-") | |
98 | ||
99 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-frtti" "/GR") | |
100 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-fno-rtti" "/GR-") | |
101 | ||
102 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-w" "/W0") | |
103 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-Wall" "/W4") | |
104 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-Wextra" "") | |
105 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-pedantic" "") | |
106 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-Wpedantic" "") | |
107 | ||
108 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-Werror" "/WX") | |
109 | __boost_test_list_replace(BOOST_TEST_COMPILE_OPTIONS "-Wno-error" "/WX-") | |
110 | ||
111 | endif() | |
112 | ||
113 | foreach(feature IN LISTS BOOST_TEST_COMPILE_FEATURES) | |
114 | if(NOT feature IN_LIST CMAKE_CXX_COMPILE_FEATURES) | |
115 | ||
116 | boost_message(VERBOSE "Test '${__NAME}' skipped, '${feature}' is not supported") | |
117 | return() | |
118 | ||
119 | endif() | |
120 | endforeach() | |
121 | ||
122 | foreach(library IN LISTS BOOST_TEST_LINK_LIBRARIES) | |
123 | ||
124 | if(TARGET ${library}) | |
125 | get_target_property(features ${library} INTERFACE_COMPILE_FEATURES) | |
126 | ||
127 | if(features) # need to check because features-NOTFOUND is a valid list | |
128 | foreach(feature IN LISTS features) | |
129 | if(NOT feature IN_LIST CMAKE_CXX_COMPILE_FEATURES) | |
130 | ||
131 | boost_message(VERBOSE "Test '${__NAME}' skipped, '${feature}' required by '${library}' is not supported") | |
132 | return() | |
133 | ||
134 | endif() | |
135 | endforeach() | |
136 | endif() | |
137 | endif() | |
138 | endforeach() | |
139 | ||
1e59de90 TL |
140 | if(NOT TARGET tests) |
141 | add_custom_target(tests) | |
142 | endif() | |
143 | ||
144 | if(__TYPE STREQUAL "compile") | |
f67539c2 TL |
145 | |
146 | add_library(${__NAME} STATIC EXCLUDE_FROM_ALL ${__SOURCES}) | |
147 | target_link_libraries(${__NAME} ${BOOST_TEST_LINK_LIBRARIES}) | |
148 | target_compile_definitions(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_DEFINITIONS}) | |
149 | target_compile_options(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_OPTIONS}) | |
150 | target_compile_features(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_FEATURES}) | |
151 | ||
1e59de90 | 152 | add_dependencies(tests ${__NAME}) |
f67539c2 | 153 | |
1e59de90 TL |
154 | elseif(__TYPE STREQUAL "compile-fail") |
155 | ||
156 | add_library(${__NAME} STATIC EXCLUDE_FROM_ALL ${__SOURCES}) | |
157 | target_link_libraries(${__NAME} ${BOOST_TEST_LINK_LIBRARIES}) | |
158 | target_compile_definitions(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_DEFINITIONS}) | |
159 | target_compile_options(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_OPTIONS}) | |
160 | target_compile_features(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_FEATURES}) | |
161 | ||
162 | add_test(NAME ${__TYPE}-${__NAME} COMMAND "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${__NAME} --config $<CONFIG>) | |
163 | ||
164 | set_tests_properties(${__TYPE}-${__NAME} PROPERTIES WILL_FAIL TRUE RUN_SERIAL TRUE) | |
f67539c2 TL |
165 | |
166 | elseif(__TYPE STREQUAL "link") | |
167 | ||
168 | add_executable(${__NAME} EXCLUDE_FROM_ALL ${__SOURCES}) | |
169 | target_link_libraries(${__NAME} ${BOOST_TEST_LINK_LIBRARIES}) | |
170 | target_compile_definitions(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_DEFINITIONS}) | |
171 | target_compile_options(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_OPTIONS}) | |
172 | target_compile_features(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_FEATURES}) | |
173 | ||
1e59de90 | 174 | add_dependencies(tests ${__NAME}) |
f67539c2 TL |
175 | |
176 | elseif(__TYPE STREQUAL "link-fail") | |
177 | ||
178 | add_library(compile-${__NAME} OBJECT EXCLUDE_FROM_ALL ${__SOURCES}) | |
179 | target_link_libraries(compile-${__NAME} ${BOOST_TEST_LINK_LIBRARIES}) | |
180 | target_compile_definitions(compile-${__NAME} PRIVATE ${BOOST_TEST_COMPILE_DEFINITIONS}) | |
181 | target_compile_options(compile-${__NAME} PRIVATE ${BOOST_TEST_COMPILE_OPTIONS}) | |
182 | target_compile_features(compile-${__NAME} PRIVATE ${BOOST_TEST_COMPILE_FEATURES}) | |
183 | ||
1e59de90 | 184 | add_dependencies(tests compile-${__NAME}) |
f67539c2 TL |
185 | |
186 | add_executable(${__NAME} EXCLUDE_FROM_ALL $<TARGET_OBJECTS:compile-${__NAME}>) | |
187 | target_link_libraries(${__NAME} ${BOOST_TEST_LINK_LIBRARIES}) | |
188 | target_compile_definitions(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_DEFINITIONS}) | |
189 | target_compile_options(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_OPTIONS}) | |
190 | target_compile_features(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_FEATURES}) | |
191 | ||
1e59de90 TL |
192 | add_test(NAME ${__TYPE}-${__NAME} COMMAND "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${__NAME} --config $<CONFIG>) |
193 | set_tests_properties(${__TYPE}-${__NAME} PROPERTIES WILL_FAIL TRUE RUN_SERIAL TRUE) | |
f67539c2 TL |
194 | |
195 | elseif(__TYPE STREQUAL "run" OR __TYPE STREQUAL "run-fail") | |
196 | ||
197 | add_executable(${__NAME} EXCLUDE_FROM_ALL ${__SOURCES}) | |
198 | target_link_libraries(${__NAME} ${BOOST_TEST_LINK_LIBRARIES}) | |
199 | target_compile_definitions(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_DEFINITIONS}) | |
200 | target_compile_options(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_OPTIONS}) | |
201 | target_compile_features(${__NAME} PRIVATE ${BOOST_TEST_COMPILE_FEATURES}) | |
202 | ||
1e59de90 | 203 | add_dependencies(tests ${__NAME}) |
f67539c2 | 204 | |
1e59de90 | 205 | add_test(NAME ${__TYPE}-${__NAME} COMMAND ${__NAME} ${__ARGUMENTS}) |
f67539c2 TL |
206 | |
207 | if(__TYPE STREQUAL "run-fail") | |
1e59de90 | 208 | set_tests_properties(${__TYPE}-${__NAME} PROPERTIES WILL_FAIL TRUE) |
f67539c2 TL |
209 | endif() |
210 | ||
211 | else() | |
212 | ||
213 | message(AUTHOR_WARNING "Unknown test type '${__TYPE}' for test '${__NAME}'") | |
214 | ||
215 | endif() | |
216 | ||
217 | endfunction(boost_test) |