Browse Source

modules: make it possible to use SDL2 as a CMake subproject.

euler-xxx
Vladimír Vondruš 5 years ago
parent
commit
62628beac9
  1. 3
      doc/changelog.dox
  2. 57
      modules/FindSDL2.cmake
  3. 42
      src/Magnum/Platform/Sdl2Application.h

3
doc/changelog.dox

@ -315,6 +315,9 @@ See also:
[mosra/homebrew-magnum#6](https://github.com/mosra/homebrew-magnum/pull/6)) [mosra/homebrew-magnum#6](https://github.com/mosra/homebrew-magnum/pull/6))
- Various changes to Vcpkg packages to account for newly added libraries and - Various changes to Vcpkg packages to account for newly added libraries and
plugin interfaces ([mosra/magnum#485](https://github.com/mosra/magnum/issues/485)) 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 @subsection changelog-latest-bugfixes Bug fixes

57
modules/FindSDL2.cmake

@ -42,6 +42,63 @@
# DEALINGS IN THE SOFTWARE. # 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
# $<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)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args("SDL2" DEFAULT_MSG _SDL2_SOURCE_DIR)
if(CORRADE_TARGET_WINDOWS)
# .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()
# In Emscripten SDL is linked automatically, thus no need to find the library. # In Emscripten SDL is linked automatically, thus no need to find the library.
# Also the includes are in SDL subdirectory, not SDL2. # Also the includes are in SDL subdirectory, not SDL2.
if(CORRADE_TARGET_EMSCRIPTEN) if(CORRADE_TARGET_EMSCRIPTEN)

42
src/Magnum/Platform/Sdl2Application.h

@ -217,17 +217,51 @@ find_package(Magnum REQUIRED Sdl2Application)
target_link_libraries(your-app PRIVATE Magnum::Sdl2Application) target_link_libraries(your-app PRIVATE Magnum::Sdl2Application)
@endcode @endcode
Additionally, if you're using Magnum as a CMake subproject, do the following Additionally, if you're using Magnum as a CMake subproject, bundle the
[SDL repository](https://github.com/libsdl-org/SDL) and do the following
* *before* calling @cmake find_package() @ce to ensure it's enabled, as the * *before* calling @cmake find_package() @ce to ensure it's enabled, as the
library is not built by default. Using SDL2 itself as a CMake subproject isn't library is not built by default. If you want to use system-installed SDL2,
tested at the moment, so you need to provide it as a system dependency and omit the first part and point `CMAKE_PREFIX_PATH` to its installation dir if
point `CMAKE_PREFIX_PATH` to its installation dir if necessary. necessary.
@code{.cmake} @code{.cmake}
# This is the most minimal set of features which still make Sdl2Application
# work. If you need something from these, remove the setting. The SDL_AUDIO
# option should not be needed either as Magnum doesn't use it, but if it's
# disabled it causes linker errors. Either SDL_DLOPEN or SDL_LOADSO needs to be
# enabled depending on the system to allow linking dependencies at runtime, so
# it's better to just leave them both on.
set(SDL_ATOMIC OFF CACHE BOOL "" FORCE)
set(SDL_CPUINFO OFF CACHE BOOL "" FORCE)
set(SDL_EVENTS OFF CACHE BOOL "" FORCE)
set(SDL_FILE OFF CACHE BOOL "" FORCE)
set(SDL_FILESYSTEM OFF CACHE BOOL "" FORCE)
set(SDL_HAPTIC OFF CACHE BOOL "" FORCE)
set(SDL_LOCALE OFF CACHE BOOL "" FORCE)
set(SDL_POWER OFF CACHE BOOL "" FORCE)
set(SDL_RENDER OFF CACHE BOOL "" FORCE)
set(SDL_SENSOR OFF CACHE BOOL "" FORCE)
set(SDL_THREADS OFF CACHE BOOL "" FORCE)
set(SDL_TIMERS OFF CACHE BOOL "" FORCE)
# This assumes you want to have SDL as a static library. If not, set SDL_STATIC
# to OFF instead.
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
add_subdirectory(SDL EXCLUDE_FROM_ALL)
set(WITH_SDL2APPLICATION ON CACHE BOOL "" FORCE) set(WITH_SDL2APPLICATION ON CACHE BOOL "" FORCE)
add_subdirectory(magnum EXCLUDE_FROM_ALL) add_subdirectory(magnum EXCLUDE_FROM_ALL)
@endcode @endcode
<b></b>
@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 If no other application is requested, you can also use the generic
`Magnum::Application` alias to simplify porting. Again, see @ref building and `Magnum::Application` alias to simplify porting. Again, see @ref building and
@ref cmake for more information. @ref cmake for more information.

Loading…
Cancel
Save