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

@@ -20,7 +20,7 @@ build_script:
- cd build
- ps: $env:CMAKE_PLATFORM="$(if ($env:PLATFORM -eq 'x64') { 'x64' } else { '' })"
- ps: $env:QT_SUFFIX="$(if ($env:PLATFORM -eq 'x64') { '_64' } else { '' })"
- cmake -DUSE_COMPILE_CACHE=ON -DCACHE_TOOL=%APPVEYOR_BUILD_FOLDER%/clcache.4.1.0/clcache-4.1.0/clcache.exe -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_PREFIX_PATH=c:/Qt/5.12/msvc2017%QT_SUFFIX%;c:/tools/vcpkg/installed/%PLATFORM%-windows -DCMAKE_GENERATOR_PLATFORM="%CMAKE_PLATFORM%" ..
- cmake -DUSE_COMPILE_CACHE=ON -DCACHE_TOOL=%APPVEYOR_BUILD_FOLDER%/clcache.4.1.0/clcache-4.1.0/clcache.exe -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_PREFIX_PATH=c:/Qt/5.12/msvc2017%QT_SUFFIX% -DCMAKE_TOOLCHAIN_FILE=c:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_GENERATOR_PLATFORM="%CMAKE_PLATFORM%" ..
- cmake --build . -- /maxcpucount:4
- cmake --build . --target tests
- cmake --build . --target package

View File

@@ -295,16 +295,25 @@ ENDIF(WANT_CARLA)
# check for SDL2
IF(WANT_SDL)
# Don't look for SDL2main
SET(SDL2_BUILDING_LIBRARY TRUE)
FIND_PACKAGE(SDL2)
# Search for SDL2 using config mode first, then fall back to module mode.
# This allows us to use SDL2's own CMake configuration files if available.
FIND_PACKAGE(SDL2 CONFIG QUIET)
IF(NOT TARGET SDL2::SDL2)
UNSET(SDL2_FOUND)
FIND_PACKAGE(SDL2 MODULE)
ENDIF()
IF(SDL2_FOUND)
SET(LMMS_HAVE_SDL TRUE)
SET(LMMS_HAVE_SDL2 TRUE)
SET(STATUS_SDL "OK, using SDL2")
SET(SDL2_LIBRARY "SDL2::SDL2")
SET(SDL_INCLUDE_DIR "")
SET(SDL_LIBRARY "")
ELSE()
SET(SDL2_INCLUDE_DIR "")
SET(SDL2_LIBRARY "")
ENDIF()
ENDIF()

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>")

View File

@@ -61,9 +61,7 @@ INCLUDE_DIRECTORIES(
${FFTW3F_INCLUDE_DIRS}
)
IF(NOT ("${SDL2_INCLUDE_DIR}" STREQUAL ""))
INCLUDE_DIRECTORIES("${SDL2_INCLUDE_DIR}")
ELSEIF(NOT ("${SDL_INCLUDE_DIR}" STREQUAL ""))
IF(NOT LMMS_HAVE_SDL2 AND NOT ("${SDL_INCLUDE_DIR}" STREQUAL ""))
INCLUDE_DIRECTORIES("${SDL_INCLUDE_DIR}")
ENDIF()
@@ -208,6 +206,11 @@ IF(LMMS_BUILD_WIN32)
SET_TARGET_PROPERTIES(lmms PROPERTIES
ENABLE_EXPORTS ON
)
IF(NOT MSVC)
SET_PROPERTY(TARGET lmms
APPEND_STRING PROPERTY LINK_FLAGS " -mwindows"
)
ENDIF()
IF(LMMS_BUILD_MSYS)
# ENABLE_EXPORTS property has no effect in some MSYS2 configurations.
# Add the linker flag manually to create liblmms.dll.a import library