1# ~~~
2# Copyright (c) 2014-2023 Valve Corporation
3# Copyright (c) 2014-2023 LunarG, Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16# ~~~
17enable_language(CXX) # Tests use C++
18
19set(CMAKE_CXX_STANDARD 17)
20set(CMAKE_CXX_STANDARD_REQUIRED ON)
21set(CMAKE_CXX_EXTENSIONS OFF)
22
23# Make sure tests uses the dynamic runtime instead
24set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
25
26# For MSVC/Windows, replace /GR with an empty string, this prevents warnings of /GR being overriden by /GR-
27# Newer CMake versions (3.20) have better solutions for this through policy - using the old
28# way while waiting for when updating can occur
29string(REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
30
31if (IS_DIRECTORY "${GOOGLETEST_INSTALL_DIR}/googletest")
32    set(BUILD_GTEST ON)
33    set(BUILD_GMOCK OFF)
34    set(gtest_force_shared_crt ON)
35    set(BUILD_SHARED_LIBS ON)
36    set(INSTALL_GTEST OFF)
37    add_subdirectory("${GOOGLETEST_INSTALL_DIR}" ${CMAKE_CURRENT_BINARY_DIR}/gtest)
38else()
39    message(FATAL_ERROR "Could not find googletest directory. See BUILD.md")
40endif()
41
42if (WIN32)
43    if(NOT IS_DIRECTORY ${DETOURS_INSTALL_DIR})
44        message(FATAL_ERROR "Could not find detours! See BUILD.md")
45    endif()
46    add_library(detours STATIC
47        ${DETOURS_INSTALL_DIR}/src/creatwth.cpp
48        ${DETOURS_INSTALL_DIR}/src/detours.cpp
49        ${DETOURS_INSTALL_DIR}/src/detours.h
50        ${DETOURS_INSTALL_DIR}/src/detver.h
51        ${DETOURS_INSTALL_DIR}/src/disasm.cpp
52        ${DETOURS_INSTALL_DIR}/src/disolarm.cpp
53        ${DETOURS_INSTALL_DIR}/src/disolarm64.cpp
54        ${DETOURS_INSTALL_DIR}/src/disolia64.cpp
55        ${DETOURS_INSTALL_DIR}/src/disolx64.cpp
56        ${DETOURS_INSTALL_DIR}/src/disolx86.cpp
57        ${DETOURS_INSTALL_DIR}/src/image.cpp
58        ${DETOURS_INSTALL_DIR}/src/modules.cpp
59    )
60    target_include_directories(detours PUBLIC ${DETOURS_INSTALL_DIR}/src)
61
62    target_compile_definitions(detours PUBLIC WIN32_LEAN_AND_MEAN)
63
64    if(MSVC)
65        target_compile_definitions(detours PUBLIC  "_CRT_SECURE_NO_WARNINGS=1")
66        set_target_properties(detours PROPERTIES COMPILE_FLAGS /EHsc)
67    endif()
68
69    # Silence errors found in clang-cl
70    if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND "${CMAKE_C_SIMULATE_ID}" MATCHES "MSVC")
71        target_compile_options(detours PRIVATE -Wno-sizeof-pointer-memaccess -Wno-microsoft-goto -Wno-microsoft-cast)
72    endif()
73endif()
74
75option(ENABLE_LIVE_VERIFICATION_TESTS "Enable tests which expect to run on live drivers. Meant for manual verification only" OFF)
76
77include(GoogleTest)
78add_subdirectory(framework)
79
80add_executable(
81    test_regression
82        loader_testing_main.cpp
83        loader_alloc_callback_tests.cpp
84        loader_envvar_tests.cpp
85        loader_get_proc_addr_tests.cpp
86        loader_debug_ext_tests.cpp
87        loader_handle_validation_tests.cpp
88        loader_layer_tests.cpp
89        loader_regression_tests.cpp
90        loader_phys_dev_inst_ext_tests.cpp
91        loader_settings_tests.cpp
92        loader_version_tests.cpp
93        loader_unknown_ext_tests.cpp
94        loader_wsi_tests.cpp)
95target_link_libraries(test_regression PUBLIC testing_dependencies)
96target_compile_definitions(test_regression PUBLIC VK_NO_PROTOTYPES)
97
98# Threading tests live in separate executabe just for threading tests as it'll need support
99# in the test harness to enable in CI, as thread sanitizer doesn't work with address sanitizer enabled.
100add_executable(
101    test_threading
102        loader_testing_main.cpp
103        loader_threading_tests.cpp)
104target_link_libraries(test_threading PUBLIC testing_dependencies)
105target_compile_definitions(test_threading PUBLIC VK_NO_PROTOTYPES)
106
107# executables that are meant for testing against real drivers rather than the mocks
108if (ENABLE_LIVE_VERIFICATION_TESTS)
109    add_subdirectory(live_verification)
110endif()
111
112if(WIN32)
113    # Copy loader and googletest (gtest) libs to test dir so the test executable can find them.
114    add_custom_command(TARGET test_regression POST_BUILD
115                       COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:gtest> $<TARGET_FILE_DIR:test_regression>)
116    # Copy the loader shared lib (if built) to the test application directory so the test app finds it.
117    if(TARGET vulkan)
118        add_custom_command(TARGET test_regression POST_BUILD
119                           COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:vulkan> $<TARGET_FILE_DIR:test_regression>)
120    endif()
121
122    # Copy the gtest shared lib (if built) to the live verification tests directory so the tests finds it.
123    if(ENABLE_LIVE_VERIFICATION_TESTS)
124        add_custom_command(TARGET test_regression POST_BUILD
125                           COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:gtest> $<TARGET_FILE_DIR:dynamic_rendering_get_proc_addr>)
126    endif()
127endif()
128
129# https://discourse.cmake.org/t/googletest-crash-when-using-cmake-xcode-arm64/5766
130#
131# TLDR: On macOS arm64, all binaries have to be signed before running.
132# Delay test discovery until test time as a workaround.
133if (XCODE)
134    set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE "PRE_TEST")
135endif()
136
137# must happen after the dll's get copied over
138if(NOT CMAKE_CROSSCOMPILING)
139    gtest_discover_tests(test_regression PROPERTIES DISCOVERY_TIMEOUT 100)
140else()
141    gtest_add_tests(TARGET test_regression)
142endif()
143
144# When APPLE_STATIC_LOADER is ON installation is disabled
145if (APPLE_STATIC_LOADER)
146    return()
147endif()
148
149# Test installation
150set(test_install_dir "${CMAKE_CURRENT_BINARY_DIR}/install")
151add_test(NAME integration.install
152    COMMAND ${CMAKE_COMMAND} --install ${PROJECT_BINARY_DIR} --prefix ${test_install_dir} --config $<CONFIG>
153)
154
155# find_package testing currently doesn't work well under cross-compilation scenarios.
156if (CMAKE_CROSSCOMPILING OR
157    NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
158    return()
159endif()
160
161# Test find_package suppport
162add_test(NAME integration.find_package
163    COMMAND ${CMAKE_CTEST_COMMAND}
164        --build-and-test ${CMAKE_CURRENT_LIST_DIR}/integration
165                        ${CMAKE_CURRENT_BINARY_DIR}/find_package
166        --build-generator ${CMAKE_GENERATOR}
167        --build-options -DCMAKE_PREFIX_PATH=${test_install_dir}
168)
169
170# Installing comes before testing
171set_tests_properties(integration.find_package PROPERTIES DEPENDS integration.install)
172