diff --git a/doc/changelog.dox b/doc/changelog.dox index 603a609ed..24328e0e5 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -1100,6 +1100,9 @@ See also: dependencies in a way that doesn't clash with modules of the same name potentially installed by other software, meaning the user doesn't need to copy and carry them along anymore (see [mosra/magnum#436](https://github.com/mosra/magnum/issues/436)) +- The `FindSDL2.cmake` module now delegates to SDL's own CMake config file, + if available, especially to ensure all dependencies are correctly linked on + static builds (see [mosra/magnum#553](https://github.com/mosra/magnum/issues/553)) @subsection changelog-latest-bugfixes Bug fixes diff --git a/modules/FindSDL2.cmake b/modules/FindSDL2.cmake index 50df96aa0..30dc70572 100644 --- a/modules/FindSDL2.cmake +++ b/modules/FindSDL2.cmake @@ -43,11 +43,41 @@ # 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) +# SDL2 installs CMake package config files which handle dependencies in case +# it's built statically. Try to find first, quietly, so it doesn't print +# loud messages when it's not found, since that's okay. If the unprefixed SDL2 +# targets already exist, it means we're using it through a CMake subproject -- +# don't attempt to find the package in that case. +# +# Don't do this on Emscripten -- there the config file is broken if +# EMSCRIPTEN_SYSROOT isn't defined, adding /include/SDL2 as an include path. +# Plus, it wants to pass -sUSE_SDL=2, which we definitely *do not* want here. +if(NOT CORRADE_TARGET_EMSCRIPTEN AND NOT TARGET SDL2 AND NOT TARGET SDL2-static) + find_package(SDL2 CONFIG QUIET) +endif() + +# If either a SDL2 config file was found or we have a CMake subproject, use the +# targets directly. I'd prefer the static variant if there's a choice, 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::SDL2 OR TARGET SDL2::SDL2-static OR TARGET SDL2 OR TARGET SDL2-static) + # The static build is a separate target for some reason. I wonder HOW that + # makes more sense than just having a build-time option for static/shared + # and use the same name for both. Are all depending projects supposed to + # branch on it like this?! + if(TARGET SDL2::SDL2) + set(_SDL2_TARGET SDL2::SDL2) + set(_SDL2_DYNAMIC ON) + elseif(TARGET SDL2::SDL2-static) + set(_SDL2_TARGET SDL2::SDL2-static) + elseif(TARGET SDL2) + set(_SDL2_TARGET SDL2) + set(_SDL2_DYNAMIC ON) + elseif(TARGET SDL2-static) + set(_SDL2_TARGET SDL2-static) + 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) @@ -56,47 +86,30 @@ if(TARGET SDL2) # 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) + set_target_properties(SDL2::SDL2 PROPERTIES INTERFACE_LINK_LIBRARIES ${_SDL2_TARGET}) endif() # Just to make FPHSA print some meaningful location, nothing else. Not - # using the INTERFACE_INCLUDE_DIRECTORIES as that contains - # $