From d27246de654c883adf42cc1d35f3ccbb619c09d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 24 Jan 2025 00:21:05 +0100 Subject: [PATCH] modules: make the FindSDL2 config delegation work with CMake < 3.18. SDL 2.24 started adding a SDL2::SDL2 alias, which would avoid some of the extra branching I had to do in the Find module. Unfortunately, on CMake before 3.18 it causes static SDL to be marked as not found because the alias isn't an alias but rather an INTERFACE that links to SDL2::SDL2-static, which means it doesn't have the INTERFACE_LINK_LIBRARIES property, which it fails on. So I'm detecting that and undoing it in order to not fail. --- modules/FindSDL2.cmake | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/modules/FindSDL2.cmake b/modules/FindSDL2.cmake index f61b917aa..24386741a 100644 --- a/modules/FindSDL2.cmake +++ b/modules/FindSDL2.cmake @@ -78,6 +78,25 @@ if(TARGET SDL2::SDL2 OR TARGET SDL2::SDL2-static OR TARGET SDL2 OR TARGET SDL2-s set(_SDL2_TARGET SDL2-static) endif() + # Well, in 2.24, SDL's CMake config started adding SDL2::SDL2 as an alias + # to SDL2::SDL2-static. Which is kind of nice, however we still need to + # support the older versions, and we use the name of the target to know + # whether it's a static or a dynamic build. Additionally, the problem with + # the alias is that we can't get INTERFACE_INCLUDE_DIRECTORIES from it on + # CMake before 3.18 because there it's not an ALIAS but an INTERFACE with + # INTERFACE_LINK_LIBRARIES pointing to SDL2::SDL2-static. In that case, and + # in case it's an alias, switch to the static target instead. + # + # https://github.com/libsdl-org/SDL/commit/6d1dfc8322f752a02e876a99bb5e2e355319389d + if(TARGET SDL2::SDL2 AND TARGET SDL2::SDL2-static) + get_target_property(_SDL2_ALIASED_TARGET SDL2::SDL2 ALIASED_TARGET) + get_target_property(_SDL2_INTERFACE_LINK_LIBRARIES SDL2::SDL2 INTERFACE_LINK_LIBRARIES) + if(_SDL2_ALIASED_TARGET STREQUAL "SDL2::SDL2-static" OR _SDL2_INTERFACE_LINK_LIBRARIES STREQUAL "SDL2::SDL2-static") + set(_SDL2_TARGET SDL2::SDL2-static) + unset(_SDL2_DYNAMIC) + endif() + endif() + # In case we don't have https://github.com/libsdl-org/SDL/pull/4074 yet, # do the alias ourselves. if(NOT TARGET SDL2::SDL2)