From f96b76e2bf9a2110706d3a95be271c2623266818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 31 Jan 2022 12:57:58 +0100 Subject: [PATCH] modules: explicitly link to pthread in FindVulkan. Originally I thought this was caused by the changes that happened in the previous commit, but apparently not, and it was due to an unrelated system upgrade I did -- the 1.2.202 Arch package had libvulkan.so link to libpthread.so, but the 1.2.203 build not (and I couldn't reproduce when rebuilding the 1.2.203 package from scratch, so I guess there was some weird hiccup on the machine that used to build the package?). And to make things worse, this caused a problem only when passing `--magnum-device cpu` to select SwiftShader, which (unlike other drivers, apparently) relies on pthread being linked in. Nevertheless, Magnum should be immune against such issues same as it's trying to be immune against Assimp regressions, so let's just link to pthread always in the Find module, and do it via -pthread instead of -lpthread so the same issue with the reference DCEd doesn't happen to us. --- modules/FindVulkan.cmake | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/FindVulkan.cmake b/modules/FindVulkan.cmake index 9a736a228..3bcc45c5e 100644 --- a/modules/FindVulkan.cmake +++ b/modules/FindVulkan.cmake @@ -78,10 +78,25 @@ find_library(Vulkan_LIBRARY include(FindPackageHandleStandardArgs) find_package_handle_standard_args(Vulkan DEFAULT_MSG Vulkan_LIBRARY) +# libvulkan.so *should* link against pthread on its own, but in some rare +# corner cases (like the 1.2.203 ArchLinux package) it doesn't -- most probably +# due to the vulkan loader wrongly using `-lpthread` instead of `-pthread` and +# then the linker DCEing out the reference to it based on some unfortunate +# combination of compiler/linker flags? +# +# In that case using drivers that rely on pthread (such as SwiftShader) would +# lead to a nasty std::system_error telling me to link to pthreads. As I can't +# have control over how the loader links to pthread or whether at all, and what +# random packaging issues happened in various distributions, I have to link to +# it explicitly to be immune against such issues. +set(THREADS_PREFER_PTHREAD_FLAG TRUE) +find_package(Threads REQUIRED) + if(NOT TARGET Vulkan::Vulkan) add_library(Vulkan::Vulkan UNKNOWN IMPORTED) - set_property(TARGET Vulkan::Vulkan PROPERTY - IMPORTED_LOCATION ${Vulkan_LIBRARY}) + set_target_properties(Vulkan::Vulkan PROPERTIES + IMPORTED_LOCATION ${Vulkan_LIBRARY} + INTERFACE_LINK_LIBRARIES Threads::Threads) endif() mark_as_advanced(Vulkan_LIBRARY)