|
| 1 | +# Helper script to add the SDL2 CMake target and version variable as introduced in SDL 2.0.12. |
| 2 | +# Also fixes a wrong include path provided by the SDL2 config script. |
| 3 | + |
| 4 | + |
| 5 | +# Proper CMake target support was added in SDL 2.0.12, create one |
| 6 | +# Need to search again to find the full path of libSDL2 |
| 7 | +if(NOT TARGET SDL2::SDL2) |
| 8 | + # Remove -lSDL2 as that is handled by CMake, note the space at the end so it does not replace e.g. -lSDL2main |
| 9 | + # This may require "libdir" being set (from above) |
| 10 | + string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS " -lSDL2 ") |
| 11 | + string(STRIP "${SDL2_EXTRA_LINK_FLAGS}" SDL2_EXTRA_LINK_FLAGS) |
| 12 | + string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS_STATIC " -Wl,--no-undefined -lm -ldl -lasound -lm -ldl -lpthread -lpulse-simple -lpulse -lX11 -lXext -lXcursor -lXinerama -lXi -lXrandr -lXss -lXxf86vm -lpthread -lrt ") |
| 13 | + string(STRIP "${SDL2_EXTRA_LINK_FLAGS_STATIC}" SDL2_EXTRA_LINK_FLAGS_STATIC) |
| 14 | + |
| 15 | + find_library(SDL2_LIBRARY SDL2) |
| 16 | + if(NOT SDL2_LIBRARY) |
| 17 | + message(FATAL_ERROR "Could not determine the location of the SDL2 library.") |
| 18 | + endif() |
| 19 | + |
| 20 | + add_library(SDL2::SDL2 SHARED IMPORTED) |
| 21 | + set_target_properties(SDL2::SDL2 PROPERTIES |
| 22 | + INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIRS}" |
| 23 | + IMPORTED_LINK_INTERFACE_LANGUAGES "C" |
| 24 | + IMPORTED_LOCATION "${SDL2_LIBRARY}" |
| 25 | + INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS}") |
| 26 | + |
| 27 | + find_library(SDL2MAIN_LIBRARY SDL2main) |
| 28 | + if(NOT SDL2MAIN_LIBRARY) |
| 29 | + message(FATAL_ERROR "Could not determine the location of the SDL2main library.") |
| 30 | + endif() |
| 31 | + |
| 32 | + add_library(SDL2::SDL2main STATIC IMPORTED) |
| 33 | + set_target_properties(SDL2::SDL2main PROPERTIES |
| 34 | + IMPORTED_LINK_INTERFACE_LANGUAGES "C" |
| 35 | + IMPORTED_LOCATION "${SDL2MAIN_LIBRARY}") |
| 36 | + |
| 37 | + # Retrieve the version from the SDL2_version.h header |
| 38 | + if(SDL2_INCLUDE_DIRS AND EXISTS "${SDL2_INCLUDE_DIRS}/SDL_version.h") |
| 39 | + file(STRINGS "${SDL2_INCLUDE_DIRS}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$") |
| 40 | + file(STRINGS "${SDL2_INCLUDE_DIRS}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$") |
| 41 | + file(STRINGS "${SDL2_INCLUDE_DIRS}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$") |
| 42 | + string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}") |
| 43 | + string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}") |
| 44 | + string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}") |
| 45 | + set(SDL2_VERSION ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH}) |
| 46 | + unset(SDL_VERSION_MAJOR_LINE) |
| 47 | + unset(SDL_VERSION_MINOR_LINE) |
| 48 | + unset(SDL_VERSION_PATCH_LINE) |
| 49 | + unset(SDL_VERSION_MAJOR) |
| 50 | + unset(SDL_VERSION_MINOR) |
| 51 | + unset(SDL_VERSION_PATCH) |
| 52 | + endif() |
| 53 | + |
| 54 | +endif() |
| 55 | + |
| 56 | +# Temporary fix to deal with wrong include dir set by SDL2's CMake configuration. |
| 57 | +get_target_property(_SDL2_INCLUDE_DIR SDL2::SDL2 INTERFACE_INCLUDE_DIRECTORIES) |
| 58 | +if(_SDL2_INCLUDE_DIR MATCHES "(.+)/SDL2\$") |
| 59 | + message(STATUS "SDL2 include dir contains \"SDL2\" subdir (SDL bug #4004) - fixing to \"${CMAKE_MATCH_1}\".") |
| 60 | + set_target_properties(SDL2::SDL2 PROPERTIES |
| 61 | + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_MATCH_1}" |
| 62 | + ) |
| 63 | +endif() |
| 64 | + |
| 65 | +if(SDL2_VERSION AND SDL2_VERSION VERSION_LESS "2.0.5") |
| 66 | + message(FATAL_ERROR "SDL2 libraries were found, but have version ${SDL2_VERSION}. At least version 2.0.5 is required.") |
| 67 | +endif() |
| 68 | + |
0 commit comments