forked from cucumber/cucumber-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
384 lines (338 loc) · 14.9 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
cmake_minimum_required(VERSION 3.1)
if(NOT CMAKE_VERSION VERSION_LESS "3.3")
# Don't ignore visibility related properties for non-SHARED targets
cmake_policy(SET CMP0063 NEW)
endif()
if (NOT CMAKE_VERSION VERSION_LESS "3.13")
# CMP0077: option() honors normal variables
# https://cmake.org/cmake/help/latest/policy/CMP0077.html
cmake_policy(SET CMP0077 NEW)
endif()
project(Cucumber-Cpp)
option(BUILD_SHARED_LIBS "Generate shared libraries" OFF)
option(CUKE_USE_STATIC_BOOST "Statically link Boost (except boost::test)" ${WIN32})
option(CUKE_USE_STATIC_GTEST "Statically link Google Test" ON)
option(CUKE_ENABLE_BOOST_TEST "Enable Boost.Test framework" ON)
option(CUKE_ENABLE_EXAMPLES "Build examples" OFF)
option(CUKE_ENABLE_GTEST "Enable Google Test framework" ON)
option(CUKE_ENABLE_QT "Enable Qt framework" ON)
option(CUKE_TESTS_E2E "Enable end-to-end tests" ON)
option(CUKE_TESTS_UNIT "Enable unit tests" ON)
option(CUKE_TESTS_VALGRIND "Enable tests within Valgrind" OFF)
set(GMOCK_SRC_DIR "" CACHE STRING "Google Mock framework sources path (otherwise downloaded)")
set(GMOCK_VER "1.7.0" CACHE STRING "Google Mock framework version to be used")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
#
# Option deprecation: if deprecated option is defined
# then print a warning and use its value instead
#
function(option_depr_message old prefer)
message (DEPRECATION "${old} is deprecated in favor of ${prefer}")
endfunction()
function(option_depr old prefer)
if(DEFINED ${old})
option_depr_message(${old} ${prefer})
set (${prefer} ${${old}} CACHE BOOL "Set from deprecated ${old}" FORCE)
endif()
endfunction()
function(option_depr_invert old prefer)
if(DEFINED ${old})
option_depr_message(${old} ${prefer})
set (${prefer} $<NOT:${${old}}> CACHE BOOL "Set from deprecated ${old}" FORCE)
endif()
endfunction()
option_depr_invert (CUKE_DISABLE_BOOST_TEST CUKE_ENABLE_BOOST_TEST)
option_depr_invert (CUKE_DISABLE_GTEST CUKE_ENABLE_GTEST)
option_depr_invert (CUKE_DISABLE_QT CUKE_ENABLE_QT)
option_depr_invert (CUKE_DISABLE_E2E_TESTS CUKE_TESTS_E2E)
option_depr_invert (CUKE_DISABLE_UNIT_TESTS CUKE_TESTS_UNIT)
option_depr (VALGRIND_TESTS CUKE_TESTS_VALGRIND)
#
# Generic Compiler Flags
#
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_INIT} -Werror -Wall -Wextra ${CMAKE_CXX_FLAGS}")
# TODO: A better fix should handle ld's --as-needed flag
if(UNIX AND NOT APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker '--no-as-needed'")
endif()
elseif(MSVC)
set(CMAKE_CXX_FLAGS "-DNOMINMAX ${CMAKE_CXX_FLAGS}") # exclude M$ min/max macros
set(CMAKE_CXX_FLAGS "/wd4996 ${CMAKE_CXX_FLAGS}") # don't warn about use of plain C functions without (non-portable) "_s" suffix
#set(CMAKE_CXX_FLAGS_DEBUG "/analyze ${CMAKE_CXX_FLAGS_DEBUG}")
endif()
#
# Colored Terminal Output
#
if(UNIX AND (
(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))
AND CMAKE_GENERATOR STREQUAL "Ninja")
# These compilers generate coloured output, but by default only when their output channel is a
# terminal (TTY/PTY). Ninja however captures all output in a pipe (per-subprocess), disabling
# coloured compiler diagnostics. We forcefully enable it because Ninja, since 1.0.0
# (ninja-build/ninja#198) takes care to strip VT100 CSI control sequences from the output if Ninja
# itself is writing to a pipe instead of a terminal. As a result this should give us the best of
# both worlds: coloured output when we're running in a terminal, plain output for editors, IDEs,
# etc.
set(CMAKE_CXX_FLAGS "-fdiagnostics-color=always ${CMAKE_CXX_FLAGS}" CACHE STRING "" FORCE)
endif()
#
# Boost
#
if(MSVC11)
# Boost 1.51 fixed a bug with MSVC11
message(STATUS "Forcing Boost 1.51+ on MSVC11")
set(BOOST_MIN_VERSION "1.51")
else()
set(BOOST_MIN_VERSION "1.46")
endif()
set(Boost_USE_STATIC_RUNTIME OFF)
set(CUKE_CORE_BOOST_LIBS thread system regex date_time program_options filesystem)
if(CUKE_ENABLE_BOOST_TEST)
# "An external test runner utility is required to link with dynamic library" (Boost User's Guide)
set(Boost_USE_STATIC_LIBS OFF)
set(CMAKE_CXX_FLAGS "-DBOOST_TEST_DYN_LINK ${CMAKE_CXX_FLAGS}")
find_package(Boost ${BOOST_MIN_VERSION} COMPONENTS unit_test_framework)
endif()
if(CUKE_USE_STATIC_BOOST)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost ${BOOST_MIN_VERSION} COMPONENTS ${CUKE_CORE_BOOST_LIBS} REQUIRED)
else()
set(CMAKE_CXX_FLAGS "-DBOOST_ALL_DYN_LINK ${CMAKE_CXX_FLAGS}")
set(Boost_USE_STATIC_LIBS OFF)
find_package(Boost ${BOOST_MIN_VERSION} COMPONENTS ${CUKE_CORE_BOOST_LIBS} REQUIRED)
endif()
# Create import targets for CMake versions older than 3.5 (actually older FindBoost.cmake)
if(Boost_USE_STATIC_LIBS)
set(LIBRARY_TYPE STATIC)
else()
# Just because we don't ask for static doesn't mean we're not getting static
set(LIBRARY_TYPE UNKNOWN)
endif()
if(Boost_INCLUDE_DIRS AND NOT TARGET Boost::boost)
add_library(Boost::boost INTERFACE IMPORTED)
set_target_properties(Boost::boost PROPERTIES
"INTERFACE_INCLUDE_DIRECTORIES" "${Boost_INCLUDE_DIRS}")
endif()
if(Boost_THREAD_LIBRARY AND NOT TARGET Boost::thread)
find_package(Threads REQUIRED)
add_library(Boost::thread ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::thread PROPERTIES
"IMPORTED_LOCATION" "${Boost_THREAD_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Threads::Threads;Boost::boost"
)
endif()
if(Boost_SYSTEM_LIBRARY AND NOT TARGET Boost::system)
add_library(Boost::system ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::system PROPERTIES
"IMPORTED_LOCATION" "${Boost_SYSTEM_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Boost::boost"
)
if(Boost_USE_STATIC_LIBS)
set_target_properties(Boost::system PROPERTIES
"COMPILE_DEFINITIONS" BOOST_ERROR_CODE_HEADER_ONLY=1
)
endif()
endif()
if(Boost_FILESYSTEM_LIBRARY AND NOT TARGET Boost::filesystem)
add_library(Boost::filesystem ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::filesystem PROPERTIES
"IMPORTED_LOCATION" "${Boost_FILESYSTEM_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Boost::system;Boost::boost"
)
endif()
if(Boost_REGEX_LIBRARY AND NOT TARGET Boost::regex)
add_library(Boost::regex ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::regex PROPERTIES
"IMPORTED_LOCATION" "${Boost_REGEX_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Boost::boost"
)
endif()
if(Boost_DATE_TIME_LIBRARY AND NOT TARGET Boost::date_time)
add_library(Boost::date_time ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::date_time PROPERTIES
"IMPORTED_LOCATION" "${Boost_DATE_TIME_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Boost::boost"
)
endif()
if(Boost_PROGRAM_OPTIONS_LIBRARY AND NOT TARGET Boost::program_options)
add_library(Boost::program_options ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::program_options PROPERTIES
"IMPORTED_LOCATION" "${Boost_PROGRAM_OPTIONS_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Boost::boost"
)
endif()
if(Boost_UNIT_TEST_FRAMEWORK_LIBRARY AND NOT TARGET Boost::unit_test_framework)
add_library(Boost::unit_test_framework ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::unit_test_framework PROPERTIES
"IMPORTED_LOCATION" "${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Boost::boost"
)
endif()
#
# GTest
#
if(CUKE_ENABLE_GTEST)
set(GTEST_USE_STATIC_LIBS ${CUKE_USE_STATIC_GTEST})
if(NOT GMOCK_ROOT)
set(GMOCK_ROOT "${CMAKE_CURRENT_BINARY_DIR}/gmock")
endif()
find_package(GMock REQUIRED)
endif()
#
# Qt
#
if(CUKE_ENABLE_QT)
find_package(Qt5Core)
find_package(Qt5Gui)
find_package(Qt5Widgets)
find_package(Qt5Test)
if(Qt5Core_FOUND AND Qt5Gui_FOUND AND Qt5Widgets_FOUND AND Qt5Test_FOUND)
message(STATUS "Found Qt version: ${Qt5Core_VERSION_STRING}")
if(NOT Qt5Core_VERSION_STRING VERSION_LESS 5.7 AND (NOT DEFINED CMAKE_CXX_STANDARD OR NOT CMAKE_CXX_STANDARD STREQUAL 98))
message(STATUS "C++11 is needed from Qt version 5.7.0, building with c++11 enabled")
set(CMAKE_CXX_STANDARD 11)
endif()
add_library(Qt::Core INTERFACE IMPORTED)
add_library(Qt::Gui INTERFACE IMPORTED)
add_library(Qt::Widgets INTERFACE IMPORTED)
add_library(Qt::Test INTERFACE IMPORTED)
set_target_properties(Qt::Core PROPERTIES INTERFACE_LINK_LIBRARIES Qt5::Core )
set_target_properties(Qt::Gui PROPERTIES INTERFACE_LINK_LIBRARIES Qt5::Gui )
set_target_properties(Qt::Widgets PROPERTIES INTERFACE_LINK_LIBRARIES Qt5::Widgets)
set_target_properties(Qt::Test PROPERTIES INTERFACE_LINK_LIBRARIES Qt5::Test )
else()
find_package(Qt4 COMPONENTS QtCore QtGui QtTest)
if(QT4_FOUND)
add_library(Qt::Core INTERFACE IMPORTED)
add_library(Qt::Gui INTERFACE IMPORTED)
add_library(Qt::Widgets INTERFACE IMPORTED)
add_library(Qt::Test INTERFACE IMPORTED)
set_target_properties(Qt::Core PROPERTIES INTERFACE_LINK_LIBRARIES Qt4::QtCore)
set_target_properties(Qt::Gui PROPERTIES INTERFACE_LINK_LIBRARIES Qt4::QtGui )
set_target_properties(Qt::Widgets PROPERTIES INTERFACE_LINK_LIBRARIES Qt4::QtGui )
set_target_properties(Qt::Test PROPERTIES INTERFACE_LINK_LIBRARIES Qt4::QtTest)
include(${QT_USE_FILE})
endif()
endif()
endif()
#
# Valgrind
#
if(CUKE_TESTS_VALGRIND)
find_package(Valgrind REQUIRED)
set(VALGRIND_ARGS --error-exitcode=2 --leak-check=full --undef-value-errors=no)
if(NOT VALGRIND_VERSION_STRING VERSION_LESS 3.9)
# Valgrind 3.9 no longer shows all leaks unless asked to
list(APPEND VALGRIND_ARGS --show-leak-kinds=all)
endif()
function(add_test name)
if(NOT name STREQUAL "NAME")
_add_test(${VALGRIND_EXECUTABLE} ${VALGRIND_ARGS} ${ARGV})
return()
endif()
set(TEST_ARGS ${ARGV})
list(FIND TEST_ARGS COMMAND COMMAND_IDX)
if(COMMAND_IDX EQUAL -1)
message(AUTHOR_WARNING "Weird command-line given to add_test(), not injecting valgrind")
_add_test(${ARGV})
return()
endif()
# We want to operate on the COMMAND, not the 'COMMAND' keyword preceding it
math(EXPR COMMAND_IDX "${COMMAND_IDX} + 1")
# Keep add_test() behaviour of replacing COMMANDs, when executable targets, with their output files
list(GET TEST_ARGS ${COMMAND_IDX} COMMAND)
if(TARGET ${COMMAND})
get_target_property(COMMAND_TYPE ${COMMAND} TYPE)
if(COMMAND_TYPE STREQUAL "EXECUTABLE")
# Inserting first, removing the original only after that, because inserting to the end of the list doesn't work
math(EXPR ORIG_COMMAND_IDX "${COMMAND_IDX} + 1")
list(INSERT TEST_ARGS ${COMMAND_IDX} "$<TARGET_FILE:${COMMAND}>")
list(REMOVE_AT TEST_ARGS ${ORIG_COMMAND_IDX})
endif()
endif()
# Insert the valgrind command line, before the command to execute
list(INSERT TEST_ARGS ${COMMAND_IDX} ${VALGRIND_EXECUTABLE} ${VALGRIND_ARGS})
_add_test(${TEST_ARGS})
endfunction()
endif()
#
# Cucumber-Cpp
#
set(CUKE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
add_subdirectory(3rdparty/json_spirit)
add_subdirectory(src)
#
# Tests
#
if(CUKE_TESTS_UNIT)
enable_testing()
add_subdirectory(tests)
else()
message(STATUS "Skipping unit tests")
endif()
if(NOT CUKE_TESTS_E2E)
message(STATUS "Skipping end-to-end tests")
else()
find_program(CUCUMBER_RUBY cucumber)
if(CUCUMBER_RUBY)
message(STATUS "Found Cucumber")
set(CUKE_FEATURES_DIR "${CMAKE_SOURCE_DIR}/features")
set(CUKE_FEATURES_TMP "${CMAKE_BINARY_DIR}/tmp")
set(CUKE_TEST_FEATURES_DIR "${CUKE_FEATURES_TMP}/test_features")
set(CUKE_DYNAMIC_CPP_STEPS "${CUKE_TEST_FEATURES_DIR}/step_definitions/cpp_steps.cpp")
string(REPLACE "/tmp" "${CMAKE_FILES_DIRECTORY}/e2e-steps.dir/tmp" CUKE_DYNAMIC_CPP_STEPS_OBJ "${CUKE_DYNAMIC_CPP_STEPS}${CMAKE_CXX_OUTPUT_EXTENSION}")
add_executable(e2e-steps EXCLUDE_FROM_ALL ${CUKE_DYNAMIC_CPP_STEPS})
# Mark this file as generated so it isn't required at CMake generation time (it is necessary when the target gets built though)
set_source_files_properties(${CUKE_DYNAMIC_CPP_STEPS} PROPERTIES GENERATED TRUE)
target_link_libraries(e2e-steps PRIVATE cucumber-cpp)
#Boost test lib required for boost specific scenario "Predicate Message"
if(TARGET Boost::unit_test_framework)
target_link_libraries(e2e-steps PRIVATE Boost::unit_test_framework)
else()
set(CUKE_E2E_TAGS "--tags ~@boost")
endif()
set(CUKE_COMPILE_DYNAMIC_CPP_STEPS '"${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target e2e-steps')
function(add_feature_target TARGET_NAME)
# Ensure we get colored output from cucumber and give it direct terminal access if
# possible. Direct terminal access would cause the output to be displayed as it's being
# produced instead of when cucumber is finished.
if(CMAKE_GENERATOR STREQUAL "Ninja")
list(APPEND ARGN --color)
endif()
set(USES_TERMINAL)
if(NOT CMAKE_VERSION VERSION_LESS 3.2)
set(USES_TERMINAL USES_TERMINAL)
endif()
add_custom_target(${TARGET_NAME}
COMMAND ${CUCUMBER_RUBY}
TEST_FEATURES_DIR=${CUKE_TEST_FEATURES_DIR}
TMP_DIR=${CUKE_FEATURES_TMP}
DYNAMIC_CPP_STEPS_SRC=${CUKE_DYNAMIC_CPP_STEPS}
DYNAMIC_CPP_STEPS_EXE=${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/e2e-steps
DYNAMIC_CPP_STEPS_OBJ=${CUKE_DYNAMIC_CPP_STEPS_OBJ}
COMPILE_DYNAMIC_CPP_STEPS=${CUKE_COMPILE_DYNAMIC_CPP_STEPS}
CUCUMBER_RUBY=${CUCUMBER_RUBY}
--format=junit "--out=${CMAKE_BINARY_DIR}/features"
${CUKE_E2E_TAGS}
${ARGN}
${CUKE_FEATURES_DIR}
${USES_TERMINAL}
# Execute in same directory as where DLLs appear on Windows
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src
)
endfunction(add_feature_target)
add_feature_target(features --format progress)
add_feature_target(features-pretty --format pretty)
add_feature_target(features-wip --format pretty --tags @wip)
else()
message(WARNING "Could not find Cucumber: skipping end-to-end tests")
endif()
endif()
#
# Examples
#
if(CUKE_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()