From 62628beac97726468351a1485b01caa5662a0d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 27 Feb 2021 14:48:45 +0100 Subject: [PATCH] modules: make it possible to use SDL2 as a CMake subproject. --- doc/changelog.dox | 3 ++ modules/FindSDL2.cmake | 57 +++++++++++++++++++++++++++ src/Magnum/Platform/Sdl2Application.h | 42 ++++++++++++++++++-- 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 059d56ddb..4b9ac12b6 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -315,6 +315,9 @@ See also: [mosra/homebrew-magnum#6](https://github.com/mosra/homebrew-magnum/pull/6)) - Various changes to Vcpkg packages to account for newly added libraries and plugin interfaces ([mosra/magnum#485](https://github.com/mosra/magnum/issues/485)) +- The `FindSDL2.cmake` module was updated to allow using SDL2 as a + subproject. See the @ref Platform-Sdl2Application-usage "Platform::Sdl2Application docs" + for more information. See also [mosra/magnum#496](https://github.com/mosra/magnum/issues/496). @subsection changelog-latest-bugfixes Bug fixes diff --git a/modules/FindSDL2.cmake b/modules/FindSDL2.cmake index 622258867..54844d696 100644 --- a/modules/FindSDL2.cmake +++ b/modules/FindSDL2.cmake @@ -42,6 +42,63 @@ # DEALINGS IN THE SOFTWARE. # +# If we have a CMake subproject, use the targets directly. I'd prefer the +# static variant, however SDL2 defines its own SDL2::SDL2 alias for only the +# dynamic variant since https://github.com/libsdl-org/SDL/pull/4074 and so I'm +# forced to use that, if available. +if(TARGET SDL2) + # In case we don't have https://github.com/libsdl-org/SDL/pull/4074 yet, + # do the alias ourselves. + if(NOT TARGET SDL2::SDL2) + # Aliases of (global) targets are only supported in CMake 3.11, so we + # work around it by this. This is easier than fetching all possible + # properties (which are impossible to track of) and then attempting to + # rebuild them into a new target. + add_library(SDL2::SDL2 INTERFACE IMPORTED) + set_target_properties(SDL2::SDL2 PROPERTIES INTERFACE_LINK_LIBRARIES SDL2) + endif() + + # Just to make FPHSA print some meaningful location, nothing else. Not + # using the INTERFACE_INCLUDE_DIRECTORIES as that contains + # $ + +@m_class{m-note m-warning} + +@par + While SDL itself, being a C project, builds quite fast, when using it as a + CMake subproject be prepared that it will *significantly* increase the + CMake configure time due to excessive platform checks, and pollute the + CMake option list with a lot of unprefixed SDL-specific options. + If no other application is requested, you can also use the generic `Magnum::Application` alias to simplify porting. Again, see @ref building and @ref cmake for more information.