Skip to content

Commit

Permalink
Support tests on windows with clang & clang-cl
Browse files Browse the repository at this point in the history
Required adding a .def file to properly export the required symbols and
disabling pointer cast warnings that are coming from Catch2.
  • Loading branch information
charles-lunarg committed Dec 6, 2023
1 parent 767bd1b commit 348e8bf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
13 changes: 12 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ target_link_libraries(VulkanMock
vk-bootstrap-compiler-warnings
)
target_compile_features(VulkanMock PUBLIC cxx_std_17)
target_compile_definitions(VulkanMock PUBLIC VK_NO_PROTOTYPES)
target_compile_definitions(VulkanMock PUBLIC VK_NO_PROTOTYPES COMPILING_DLL)
if(WIN32)
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC" OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC")
target_link_options(VulkanMock PRIVATE /DEF:${CMAKE_CURRENT_SOURCE_DIR}/vulkan_mock.def)
elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "GNU")
target_link_options(VulkanMock PRIVATE -Wl,/DEF:${CMAKE_CURRENT_SOURCE_DIR}/vulkan_mock.def)
endif()
endif()

add_executable(vk-bootstrap-test
vulkan_library_loader.hpp
Expand All @@ -37,6 +44,10 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_compile_options(vk-bootstrap-test PRIVATE /wd4996)
endif()

if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(vk-bootstrap-compiler-warnings INTERFACE -Wno-microsoft-cast)
endif()

list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
include(Catch)
catch_discover_tests(vk-bootstrap-test)
Expand Down
14 changes: 7 additions & 7 deletions tests/bootstrap_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ VkExtensionProperties get_extension_properties(const char* extName) {
}

VulkanMock& get_and_setup_default() {
VulkanMock& mock = get_vulkan_mock();
VulkanMock& mock = *get_vulkan_mock();
mock.instance_extensions.push_back(get_extension_properties(VK_KHR_SURFACE_EXTENSION_NAME));
#if defined(_WIN32)
mock.instance_extensions.push_back(get_extension_properties("VK_KHR_win32_surface"));
Expand Down Expand Up @@ -512,7 +512,7 @@ TEST_CASE("SystemInfo Loading Vulkan Automatically", "[VkBootstrap.loading]") {
TEST_CASE("SystemInfo Loading Vulkan Manually", "[VkBootstrap.loading]") {
[[maybe_unused]] VulkanMock& mock = get_and_setup_default();
VulkanLibrary vk_lib;
REQUIRE(vk_lib.vkGetInstanceProcAddr != NULL);
REQUIRE(vk_lib.vkGetInstanceProcAddr);
auto info_ret = vkb::SystemInfo::get_system_info(vk_lib.vkGetInstanceProcAddr);
REQUIRE(info_ret);
vkb::InstanceBuilder builder;
Expand All @@ -531,7 +531,7 @@ TEST_CASE("InstanceBuilder Loading Vulkan Automatically", "[VkBootstrap.loading]
TEST_CASE("InstanceBuilder Loading Vulkan Manually", "[VkBootstrap.loading]") {
[[maybe_unused]] VulkanMock& mock = get_and_setup_default();
VulkanLibrary vk_lib;
REQUIRE(vk_lib.vkGetInstanceProcAddr != NULL);
REQUIRE(vk_lib.vkGetInstanceProcAddr);
vkb::InstanceBuilder builder{ vk_lib.vkGetInstanceProcAddr };
auto ret = builder.build();
vk_lib.close();
Expand All @@ -554,15 +554,15 @@ TEST_CASE("ReLoading Vulkan Manually", "[VkBootstrap.loading]") {
[[maybe_unused]] VulkanMock& mock = get_and_setup_default();
{
VulkanLibrary vk_lib;
REQUIRE(vk_lib.vkGetInstanceProcAddr != NULL);
REQUIRE(vk_lib.vkGetInstanceProcAddr);
vkb::InstanceBuilder builder{ vk_lib.vkGetInstanceProcAddr };
auto ret = builder.build();
REQUIRE(ret);
vk_lib.close();
}
{
VulkanLibrary vk_lib;
REQUIRE(vk_lib.vkGetInstanceProcAddr != NULL);
REQUIRE(vk_lib.vkGetInstanceProcAddr);
vkb::InstanceBuilder builder{ vk_lib.vkGetInstanceProcAddr };
auto ret = builder.build();
REQUIRE(ret);
Expand Down Expand Up @@ -643,7 +643,7 @@ TEST_CASE("Passing vkb classes to Vulkan handles", "[VkBootstrap.pass_class_to_h

// Check if we can get instance functions.
PFN_vkVoidFunction instanceFunction = instance.fp_vkGetInstanceProcAddr(instance, "vkEnumeratePhysicalDevices"); // validation layers should be provided.
REQUIRE(instanceFunction != NULL);
REQUIRE(instanceFunction);

vkb::PhysicalDeviceSelector physicalDeviceSelector(instance);
auto physicalDevice =
Expand All @@ -655,7 +655,7 @@ TEST_CASE("Passing vkb classes to Vulkan handles", "[VkBootstrap.pass_class_to_h

// Check if we can get a device function address, passing vkb::Device to the function.
PFN_vkVoidFunction deviceFunction = instance.fp_vkGetDeviceProcAddr(device.value(), "vkAcquireNextImageKHR");
REQUIRE(deviceFunction != NULL);
REQUIRE(deviceFunction);
}
}

Expand Down
12 changes: 5 additions & 7 deletions tests/vulkan_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

VulkanMock mock;

EXPORT_MACRO VulkanMock& get_vulkan_mock() {
extern "C" {
VulkanMock* get_vulkan_mock() {
mock = VulkanMock{};
return mock;
return &mock;
}
}

template <typename T> VkResult fill_out_count_pointer_pair(std::vector<T> const& data_vec, uint32_t* pCount, T* pData) {
Expand Down Expand Up @@ -368,13 +370,9 @@ PFN_vkVoidFunction shim_vkGetInstanceProcAddr([[maybe_unused]] VkInstance instan
}

extern "C" {
#if defined(WIN32)

EXPORT_MACRO VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* pName) {
#pragma comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* pName) {
return shim_vkGetInstanceProcAddr(instance, pName);
}
#endif

#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__)
#define DLSYM_FUNC_NAME dlsym
Expand Down
4 changes: 4 additions & 0 deletions tests/vulkan_mock.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LIBRARY vulkan-1
EXPORTS
get_vulkan_mock
vkGetInstanceProcAddr
12 changes: 3 additions & 9 deletions tests/vulkan_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ struct VulkanMock {
}
};

#if !defined(EXPORT_MACRO)
#if defined(WIN32)
#define EXPORT_MACRO __declspec(dllexport)
#else
#define EXPORT_MACRO
#endif
#endif

EXPORT_MACRO VulkanMock& get_vulkan_mock();
extern "C" {
VulkanMock* get_vulkan_mock();
}

0 comments on commit 348e8bf

Please sign in to comment.