• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2include(CMakeParseArguments)
3
4if("${WIN32}")
5    # This defaults to OFF on Windows, since we don't support PCHs there.
6    option(FRUIT_TESTS_USE_PRECOMPILED_HEADERS "Whether to use pre-compiled headers (PCHs) in Fruit tests." OFF)
7
8    if ("${FRUIT_TESTS_USE_PRECOMPILED_HEADERS}")
9        # TODO: consider adding support for PCHs on Windows (at least when using MinGW).
10        message(FATAL_ERROR "Please rerun CMake without -DFRUIT_TESTS_USE_PRECOMPILED_HEADERS, precompiled headers are not supported on Windows.")
11    endif()
12else()
13    option(FRUIT_TESTS_USE_PRECOMPILED_HEADERS "Whether to use pre-compiled headers (PCHs) in Fruit tests." ON)
14endif()
15
16if("${WIN32}")
17  # No timeout on windows, the `timeout' executable has a different syntax.
18  set(TIMEOUT_COMMAND_PREFIX "")
19  set(TIMEOUT_COMMAND_PREFIX_STR "")
20else()
21  set(TIMEOUT_COMMAND_PREFIX "timeout" "30")
22  set(TIMEOUT_COMMAND_PREFIX_STR "timeout 30")
23endif()
24
25set(VALGRIND_FLAGS
26    --leak-check=full --malloc-fill=AA --track-origins=yes --read-var-info=yes --num-callers=50 --error-exitcode=1 --gen-suppressions=all --suppressions=${CMAKE_SOURCE_DIR}/tests/valgrind_suppressions.supp)
27string(REPLACE ";" " " VALGRIND_FLAGS_STR "${VALGRIND_FLAGS}")
28
29if(NOT "${WIN32}")
30  function(check_all_python_tests_listed DIRNAME)
31    # Join the list with " ".
32    string(REPLACE ";" " " STR "${ARGN}")
33    add_test(NAME check-all-python-tests-listed-${DIRNAME}
34             WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
35             COMMAND bash -c -x "pwd; for f in \$(ls test_*.py); do echo \" ${STR} \" | fgrep -q \" \$f \" || { echo \"\$f not listed.\" && exit 1; }; done")
36  endfunction(check_all_python_tests_listed)
37endif()
38
39if ("${FRUIT_TESTS_USE_PRECOMPILED_HEADERS}")
40    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
41        add_custom_command(
42            OUTPUT test_common-precompiled.h.gch
43            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
44            DEPENDS test_common.h fruit
45            COMMAND bash -c "${CMAKE_CXX_COMPILER} -x c++-header ${FRUIT_COMPILE_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}/../include -I${CMAKE_CURRENT_SOURCE_DIR} -I${CMAKE_CURRENT_BINARY_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/test_common.h -o test_common-precompiled.h.gch")
46        add_custom_target(test-common-precompiled-header ALL DEPENDS test_common-precompiled.h.gch)
47        # Note that the "test_common-precompiled.h" header doesn't exist, but it's ok because GCC looks for
48        # test_common-precompiled.h.gch first. We don't call the precompiled header test_common.h.gch so that if GCC doesn't
49        # find it it reports an error instead of using the normal header.
50        set(FRUIT_TESTONLY_CXXFLAGS "-include${CMAKE_CURRENT_BINARY_DIR}/test_common-precompiled.h")
51
52    elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Clang|AppleClang)$")
53        add_custom_command(
54            OUTPUT test_common.pch
55            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
56            DEPENDS test_common.h fruit
57            COMMAND bash -c "${CMAKE_CXX_COMPILER} -x c++-header ${FRUIT_COMPILE_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}/../include -I${CMAKE_CURRENT_SOURCE_DIR} -I${CMAKE_CURRENT_BINARY_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/test_common.h -o test_common.pch")
58        add_custom_target(test-common-precompiled-header ALL DEPENDS test_common.pch)
59    set(FRUIT_TESTONLY_CXXFLAGS "-include-pch ${CMAKE_CURRENT_BINARY_DIR}/test_common.pch")
60  else()
61
62    message(ERROR "Using pre-compiled headers in tests is only supported with GCC and Clang. Please add -DFRUIT_TESTS_USE_PRECOMPILED_HEADERS=OFF to your cmake invocation and try again.")
63  endif()
64else()
65    set(FRUIT_TESTONLY_CXXFLAGS "")
66endif()
67
68if("${FRUIT_ENABLE_COVERAGE}")
69  set(FRUIT_ENABLE_COVERAGE_PYTHON_BOOL "True")
70else()
71  set(FRUIT_ENABLE_COVERAGE_PYTHON_BOOL "False")
72endif()
73
74if("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(MSVC)$")
75  # These warnings are disabled for tests only, since they can only be produced when using fruit as a client. Also, they cannot be disabled via pragma pushes/pops,
76  #   so we leave it up to clients to disable them if desired.
77  # The warning C4702 is disabled because if MSVC optimizes the call to InvokeLambdaWithInjectedArgVector::operator() when cPtr is null, it will inline
78  #   a FRUIT_UNREACHABLE statement, which makes all statements succeeding the operator() call unreachable.
79  # The warning C4503 is disabled because some of the test_class_destruction.py tests suchs as "test_injector_creation_and_injection"
80  #   produce extremely long decorator names. This has no effect on the actual results of the test.
81  set(FRUIT_TESTONLY_CXXFLAGS "${FRUIT_TESTONLY_CXXFLAGS} /wd4702 /wd4503")
82endif()
83
84add_library(test_headers_copy STATIC test_common.cpp)
85target_link_libraries(test_headers_copy fruit)
86
87# Escape the backslash which is a Windows path separator.
88string(REPLACE "\\" "\\\\" ADDITIONAL_INCLUDE_DIRS "${Boost_INCLUDE_DIRS}")
89
90file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/fruit_test_config.py"
91     CONTENT "
92CXX='${CMAKE_CXX_COMPILER}'
93CXX_COMPILER_NAME='${CMAKE_CXX_COMPILER_ID}'
94CXX_COMPILER_VERSION='${CMAKE_CXX_COMPILER_VERSION}'
95FRUIT_COMPILE_FLAGS='${FRUIT_COMPILE_FLAGS} ${FRUIT_TESTONLY_CXXFLAGS}'
96ADDITIONAL_INCLUDE_DIRS='${ADDITIONAL_INCLUDE_DIRS}'
97ADDITIONAL_LINKER_FLAGS='${CMAKE_EXE_LINKER_FLAGS}'
98RUN_TESTS_UNDER_VALGRIND='${RUN_TESTS_UNDER_VALGRIND_FLAG}'
99VALGRIND_FLAGS='${VALGRIND_FLAGS_STR}'
100CMAKE_BUILD_TYPE='${CMAKE_BUILD_TYPE}'
101
102PATH_TO_COMPILED_FRUIT='$<TARGET_FILE_DIR:fruit>'
103PATH_TO_COMPILED_FRUIT_LIB='$<TARGET_FILE:fruit>'
104PATH_TO_COMPILED_TEST_HEADERS='$<TARGET_FILE_DIR:test_headers_copy>'
105PATH_TO_COMPILED_TEST_HEADERS_LIB='$<TARGET_FILE:test_headers_copy>'
106PATH_TO_FRUIT_STATIC_HEADERS='${CMAKE_CURRENT_SOURCE_DIR}/../include'
107PATH_TO_FRUIT_GENERATED_HEADERS='${CMAKE_CURRENT_BINARY_DIR}/../include'
108PATH_TO_FRUIT_TEST_HEADERS='${CMAKE_CURRENT_SOURCE_DIR}'
109ENABLE_COVERAGE=${FRUIT_ENABLE_COVERAGE_PYTHON_BOOL}
110")
111
112file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
113     CONTENT "
114[pytest]
115testpaths = \"${CMAKE_CURRENT_SOURCE_DIR}\"
116addopts = -r a
117timeout = 300
118")
119