Browse Source

modules: make FindSDL2 delegate to SDL2's own config file, if possible.

Now that the Find module doesn't need to get copied to all downstream
repositories, I can finally work on it in peace.
pull/419/merge
Vladimír Vondruš 2 years ago
parent
commit
2a8e550b57
  1. 3
      doc/changelog.dox
  2. 85
      modules/FindSDL2.cmake

3
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

85
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
# $<BUILD_INTERFACE and looks ugly in the output. Funnily enough, the
# BUILD_INTERFACE thing works here without having to override it with
# custom-found paths like I do in FindAssimp and elsewhere. Needs further
# investigation.
get_target_property(_SDL2_SOURCE_DIR SDL2 SOURCE_DIR)
# using the INTERFACE_INCLUDE_DIRECTORIES for the CMake subproject case as
# that contains $<BUILD_INTERFACE and looks ugly in the output. Funnily
# enough, the BUILD_INTERFACE thing works here without having to override
# it with custom-found paths like I do in FindAssimp and elsewhere. Needs
# further investigation.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args("SDL2" DEFAULT_MSG _SDL2_SOURCE_DIR)
if(TARGET SDL2 OR TARGET SDL2-static)
get_target_property(_SDL2_SOURCE_DIR ${_SDL2_TARGET} SOURCE_DIR)
find_package_handle_standard_args("SDL2" DEFAULT_MSG _SDL2_SOURCE_DIR)
else()
get_target_property(_SDL2_INTERFACE_INCLUDE_DIRECTORIES ${_SDL2_TARGET} INTERFACE_INCLUDE_DIRECTORIES)
find_package_handle_standard_args("SDL2" DEFAULT_MSG _SDL2_INTERFACE_INCLUDE_DIRECTORIES)
endif()
if(CORRADE_TARGET_WINDOWS)
if(CORRADE_TARGET_WINDOWS AND _SDL2_DYNAMIC)
# .dll is in LOCATION, .lib is in IMPLIB. Yay, useful!
get_target_property(SDL2_DLL_DEBUG SDL2 IMPORTED_LOCATION_DEBUG)
get_target_property(SDL2_DLL_RELEASE SDL2 IMPORTED_LOCATION_RELEASE)
endif()
return()
# 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?!
elseif(TARGET SDL2-static)
# The target should not be defined by SDL itself. If it is, that's from us.
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-static)
endif()
# Just to make FPHSA print some meaningful location, nothing else
get_target_property(_SDL2_SOURCE_DIR SDL2-static SOURCE_DIR)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args("SDL2" DEFAULT_MSG _SDL2_SOURCE_DIR)
return()
endif()

Loading…
Cancel
Save