Use modern CMake targets for SDL2 (#6132)

This commit is contained in:
Dominic Clark
2021-10-05 16:38:11 +01:00
committed by GitHub
parent ca790def76
commit 3d7ef9fa4f
4 changed files with 56 additions and 40 deletions

View File

@@ -1,16 +1,17 @@
# This module defines
# SDL2::SDL2, a target providing SDL2 itself
# SDL2::SDL2main, a target providing an entry point for applications
# SDL2_LIBRARY, the name of the library to link against
# SDL2_FOUND, if false, do not try to link to SDL2
# SDL2_INCLUDE_DIR, where to find SDL.h
#
# This module responds to the the flag:
# SDL2_BUILDING_LIBRARY
# If this is defined, then no SDL2main will be linked in because
# If this is defined, then no SDL2::SDL2main target will be created because
# only applications need main().
# Otherwise, it is assumed you are building an application and this
# module will attempt to locate and set the the proper link flags
# as part of the returned SDL2_LIBRARY variable.
# as part of the SDL2::SDL2main target.
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
@@ -18,15 +19,17 @@
# module will automatically add the -framework Cocoa on your behalf.
#
#
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
# Additional Note: If you see an empty SDL2_LIBRARY in your configuration, it
# means CMake did not find your SDL2 library (SDL2.dll, libsdl2.so,
# SDL2.framework, etc).
# Set SDL2_LIBRARY to point to your SDL2 library, and configure again.
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL2_LIBRARY
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
# as appropriate.
#
#
# Modified by Dominic Clark.
# Added modern CMake targets to match those SDL2 itself uses.
#
# $SDL2DIR is an environment variable that would
# correspond to the ./configure --prefix=$SDL2DIR
# used in building SDL2.
@@ -92,7 +95,7 @@ else()
set(PATH_SUFFIXES lib/x86 lib)
endif()
FIND_LIBRARY(SDL2_LIBRARY_TEMP
FIND_LIBRARY(SDL2_LIBRARY
NAMES SDL2
HINTS
$ENV{SDL2DIR}
@@ -130,41 +133,42 @@ IF(MINGW)
SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW")
ENDIF(MINGW)
IF(SDL2_LIBRARY_TEMP)
# For SDL2main
IF(NOT SDL2_BUILDING_LIBRARY)
IF(SDL2MAIN_LIBRARY)
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
ENDIF(SDL2MAIN_LIBRARY)
ENDIF(NOT SDL2_BUILDING_LIBRARY)
IF(SDL2_LIBRARY)
ADD_LIBRARY(SDL2::SDL2 UNKNOWN IMPORTED)
SET_TARGET_PROPERTIES(SDL2::SDL2 PROPERTIES
IMPORTED_LOCATION "${SDL2_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
)
# For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
# CMake doesn't display the -framework Cocoa string in the UI even
# though it actually is there if I modify a pre-used variable.
# I think it has something to do with the CACHE STRING.
# So I use a temporary variable until the end so I can set the
# "real" variable in one-shot.
IF(APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
SET_PROPERTY(TARGET SDL2::SDL2 APPEND PROPERTY
INTERFACE_LINK_OPTIONS "-framework Cocoa"
)
ENDIF(APPLE)
# For threads, as mentioned Apple doesn't need this.
# In fact, there seems to be a problem if I used the Threads package
# and try using this line, so I'm just skipping it entirely for OS X.
IF(NOT APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
ENDIF(NOT APPLE)
IF(NOT APPLE AND Threads_FOUND)
SET_PROPERTY(TARGET SDL2::SDL2 APPEND PROPERTY
INTERFACE_LINK_LIBRARIES "Threads::Threads"
)
ENDIF(NOT APPLE AND Threads_FOUND)
ENDIF(SDL2_LIBRARY)
# For MinGW library
IF(NOT SDL2_BUILDING_LIBRARY AND SDL2MAIN_LIBRARY)
ADD_LIBRARY(SDL2::SDL2main STATIC IMPORTED)
SET_TARGET_PROPERTIES(SDL2::SDL2main PROPERTIES
IMPORTED_LOCATION "${SDL2MAIN_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
)
IF(MINGW)
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
SET_PROPERTY(TARGET SDL2::SDL2main APPEND PROPERTY
INTERFACE_LINK_OPTIONS "${MINGW32_LIBRARY}"
)
ENDIF(MINGW)
# Set the final string here so the GUI reflects the final state.
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
ENDIF(SDL2_LIBRARY_TEMP)
ENDIF(NOT SDL2_BUILDING_LIBRARY AND SDL2MAIN_LIBRARY)
# message("</FindSDL2.cmake>")