First, to avoid misunderstandings: This is not about building SDL2 with CMake. This is about building applications/games that use SDL2 with CMake - with -dev packages from packages managers (of Linux distros, vcpkg, whatever) or the official Development Libraries.
Originally, SDL2 didn’t ship any cmake files, so people used custom FindSDL2.cmake
scripts based on the SDL1.2 FindSDL.cmake shipped by CMake itself (example - yes, this is older than SDL 2.0.0!).
Those usually provided ${SDL2_INCLUDE_DIR}
(for CMake’s include_directories()
) and ${SDL2_LIBRARY}
(for CMake’s target_link_libraries()
).
SDL 2.0.4 (Jan 2016) introduced sdl2-config.cmake
which provide ${SDL2_INCLUDE_DIRS}
and ${SDL2_LIBRARIES}
(notice the plurals) instead, which I think can be used much in the same way as the ones from the custom FindSDL2.cmake
files.
SDL 2.0.6 (Sep 2017) additionally introduced SDL2Config.cmake
which appears to work entirely differently. It does not provide ${SDL2_INCLUDE_DIRS}
and ${SDL2_INCLUDE_DIRS}
.
TBH, it’s hard to understand what it does at all, because SDL2Config.cmake itself does nothing more than include("${CMAKE_CURRENT_LIST_DIR}/SDL2Targets.cmake")
, and SDL2Targets.cmake
is generated by CMake, does a lot of magic and has no comments that’d describe how to actually use it.
Apparently it defines libraries called SDL2::SDL2
and SDL2::SDL2main
that you can then somehow use? Possibly by passing SDL2::SDL2
to CMake’s target_link_libraries()
and maybe the include path is set automagically then?
I don’t know and this gets me to the main problem here:
Some Linux distros (or package managers) ship sdl2-config.cmake
others ship the incompatible SDL2Config.cmake
.
Debian (and thus Ubuntu; I’m on XUbuntu) ships sdl2-config.cmake
, Arch Linux ships SDL2Config.cmake
, and apparently so does vcpkg (at least that was my impression based on bug reports I got in dhewm3 when I tried removing the custom FindSDL2.cmake).
SDL2-devel-2.0.*-VC.zip contains neither (which is unfortunate, especially with VC it might be handy).
SDL2-devel-2.0.*-mingw.tar.gz contains sdl2-config.cmake
, but in a broken state, as it hardcodes /opt/local/x86_64-w64-mingw32
. At least that could be relatively easily fixed by using ${CMAKE_CURRENT_LIST_DIR}
, which is the current directory sdl2-config.cmake is in, kinda like this.
Anyway, the core problem is that it is (or at least seems to me) very hard to make your own project that uses CMake and SDL build on all the current/relevant Linux distros (and other package managers), because for some reason two incompatible “solutions” exist.
Or am I missing something and it’s indeed feasible to be compatible with either without too much pain?
Wait - if you’re gonna tell me that sdl2-config.cmake
now also provides SDL2::SDL2
(like SDL2Config.cmake
), that’s nice and all, but not helpful, because that was only added in 2.0.12 (March 2020), and I at least need to support SDL2 back to 2.0.8 on Linux, because Ubuntu 18.04LTS ships that version (and Debian and Raspbian stable ship 2.0.9); even Ubuntu 20.04 LTS ships 2.0.10 (i.e. does not support SDL2::SDL2
).
Why are there two different mechanisms anyway?