update FindSDL2.cmake

This commit is contained in:
Gered 2016-09-06 17:27:12 -04:00
parent 21b4be3556
commit f577e44fc3

View file

@ -1,249 +1,249 @@
# Locate SDL2 library # - Find SDL2
# This module defines # Find the SDL2 headers and libraries
# 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::SDL2 - Imported target to use for building a library
# SDL2_BUILDING_LIBRARY # SDL2::SDL2main - Imported interface target to use if you want SDL and SDLmain.
# If this is defined, then no SDL2_main will be linked in because # SDL2_FOUND - True if SDL2 was found.
# only applications need main(). # SDL2_DYNAMIC - If we found a DLL version of SDL (meaning you might want to copy a DLL from SDL2::SDL2)
# 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.
# #
# Don't forget to include SDL2main.h and SDL2main.m your project for the # Original Author:
# OS X framework based version. (Other versions link to -lSDL2main which # 2015 Ryan Pavlik <ryan.pavlik@gmail.com> <abiryan@ryand.net>
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
# #
# # Copyright Sensics, Inc. 2015.
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration # Distributed under the Boost Software License, Version 1.0.
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library # (See accompanying file LICENSE_1_0.txt or copy at
# (SDL2.dll, libsdl2.so, SDL2.framework, etc). # http://www.boost.org/LICENSE_1_0.txt)
# Set SDL2_LIBRARY_TEMP 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.
#
#
# $SDL2 is an environment variable that would
# correspond to the ./configure --prefix=$SDL2
# used in building SDL2.
# l.e.galup 9-20-02
#
# Modified by Eric Wing.
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
# Also corrected the header search path to follow "proper" SDL2 guidelines.
# Added a search for SDL2main which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
# People will have to manually change the cache values of
# SDL2_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
#
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
# This needed to change because "proper" SDL2 convention
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL2/ (see FreeBSD).
#
# Ported by Johnny Patterson. This is a literal port for SDL2 of the FindSDL.cmake
# module with the minor edit of changing "SDL" to "SDL2" where necessary. This
# was not created for redistribution, and exists temporarily pending official
# SDL2 CMake modules.
#
# Note that on windows this will only search for the 32bit libraries, to search
# for 64bit change x86/i686-w64 to x64/x86_64-w64
#============================================================================= # Set up architectures (for windows) and prefixes (for mingw builds)
# Copyright 2003-2009 Kitware, Inc. if(WIN32)
# if(MINGW)
# CMake - Cross Platform Makefile Generator include(MinGWSearchPathExtras OPTIONAL)
# Copyright 2000-2014 Kitware, Inc. if(MINGWSEARCH_TARGET_TRIPLE)
# Copyright 2000-2011 Insight Software Consortium set(SDL2_PREFIX ${MINGWSEARCH_TARGET_TRIPLE})
# All rights reserved. endif()
# endif()
# Redistribution and use in source and binary forms, with or without if(CMAKE_SIZEOF_VOID_P EQUAL 8)
# modification, are permitted provided that the following conditions set(SDL2_LIB_PATH_SUFFIX lib/x64)
# are met: if(NOT MSVC AND NOT SDL2_PREFIX)
# set(SDL2_PREFIX x86_64-w64-mingw32)
# * Redistributions of source code must retain the above copyright endif()
# notice, this list of conditions and the following disclaimer. else()
# set(SDL2_LIB_PATH_SUFFIX lib/x86)
# * Redistributions in binary form must reproduce the above copyright if(NOT MSVC AND NOT SDL2_PREFIX)
# notice, this list of conditions and the following disclaimer in the set(SDL2_PREFIX i686-w64-mingw32)
# documentation and/or other materials provided with the distribution. endif()
# endif()
# * Neither the names of Kitware, Inc., the Insight Software Consortium, endif()
# nor the names of their contributors may be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
FIND_PATH(SDL2_INCLUDE_DIR SDL.h if(SDL2_PREFIX)
set(SDL2_ORIGPREFIXPATH ${CMAKE_PREFIX_PATH})
if(SDL2_ROOT_DIR)
list(APPEND CMAKE_PREFIX_PATH "${SDL2_ROOT_DIR}")
endif()
if(CMAKE_PREFIX_PATH)
foreach(_prefix ${CMAKE_PREFIX_PATH})
list(APPEND CMAKE_PREFIX_PATH "${_prefix}/${SDL2_PREFIX}")
endforeach()
endif()
if(MINGWSEARCH_PREFIXES)
list(APPEND CMAKE_PREFIX_PATH ${MINGWSEARCH_PREFIXES})
endif()
endif()
# Invoke pkgconfig for hints
find_package(PkgConfig QUIET)
set(SDL2_INCLUDE_HINTS)
set(SDL2_LIB_HINTS)
if(PKG_CONFIG_FOUND)
pkg_search_module(SDL2PC QUIET sdl2)
if(SDL2PC_INCLUDE_DIRS)
set(SDL2_INCLUDE_HINTS ${SDL2PC_INCLUDE_DIRS})
endif()
if(SDL2PC_LIBRARY_DIRS)
set(SDL2_LIB_HINTS ${SDL2PC_LIBRARY_DIRS})
endif()
endif()
include(FindPackageHandleStandardArgs)
find_library(SDL2_LIBRARY
NAMES
SDL2
HINTS HINTS
$ENV{SDL2} ${SDL2_LIB_HINTS}
PATH_SUFFIXES include/SDL2 include SDL2
i686-w64-mingw32/include/SDL2
x86_64-w64-mingw32/include/SDL2
PATHS PATHS
~/Library/Frameworks ${SDL2_ROOT_DIR}
/Library/Frameworks ENV SDL2DIR
/usr/local/include/SDL2 PATH_SUFFIXES lib SDL2 ${SDL2_LIB_PATH_SUFFIX})
/usr/include/SDL2
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
)
# Lookup the 64 bit libs on x64 set(_sdl2_framework FALSE)
IF(CMAKE_SIZEOF_VOID_P EQUAL 8) # Some special-casing if we've found/been given a framework.
FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2 # Handles whether we're given the library inside the framework or the framework itself.
if(APPLE AND "${SDL2_LIBRARY}" MATCHES "(/[^/]+)*.framework(/.*)?$")
set(_sdl2_framework TRUE)
set(SDL2_FRAMEWORK "${SDL2_LIBRARY}")
# Move up in the directory tree as required to get the framework directory.
while("${SDL2_FRAMEWORK}" MATCHES "(/[^/]+)*.framework(/.*)$" AND NOT "${SDL2_FRAMEWORK}" MATCHES "(/[^/]+)*.framework$")
get_filename_component(SDL2_FRAMEWORK "${SDL2_FRAMEWORK}" DIRECTORY)
endwhile()
if("${SDL2_FRAMEWORK}" MATCHES "(/[^/]+)*.framework$")
set(SDL2_FRAMEWORK_NAME ${CMAKE_MATCH_1})
# If we found a framework, do a search for the header ahead of time that will be more likely to get the framework header.
find_path(SDL2_INCLUDE_DIR
NAMES
SDL_haptic.h # this file was introduced with SDL2
HINTS HINTS
$ENV{SDL2} "${SDL2_FRAMEWORK}/Headers/")
PATH_SUFFIXES lib64 lib else()
lib/x64 # For some reason we couldn't get the framework directory itself.
x86_64-w64-mingw32/lib # Shouldn't happen, but might if something is weird.
unset(SDL2_FRAMEWORK)
endif()
endif()
find_path(SDL2_INCLUDE_DIR
NAMES
SDL_haptic.h # this file was introduced with SDL2
HINTS
${SDL2_INCLUDE_HINTS}
PATHS PATHS
/sw ${SDL2_ROOT_DIR}
/opt/local ENV SDL2DIR
/opt/csw PATH_SUFFIXES include include/sdl2 include/SDL2 SDL2)
/opt
if(WIN32 AND SDL2_LIBRARY)
find_file(SDL2_RUNTIME_LIBRARY
NAMES
SDL2.dll
libSDL2.dll
HINTS
${SDL2_LIB_HINTS}
PATHS
${SDL2_ROOT_DIR}
ENV SDL2DIR
PATH_SUFFIXES bin lib ${SDL2_LIB_PATH_SUFFIX})
endif()
if(WIN32 OR ANDROID OR IOS OR (APPLE AND NOT _sdl2_framework))
set(SDL2_EXTRA_REQUIRED SDL2_SDLMAIN_LIBRARY)
find_library(SDL2_SDLMAIN_LIBRARY
NAMES
SDL2main
PATHS
${SDL2_ROOT_DIR}
ENV SDL2DIR
PATH_SUFFIXES lib ${SDL2_LIB_PATH_SUFFIX})
endif()
if(MINGW AND NOT SDL2PC_FOUND)
find_library(SDL2_MINGW_LIBRARY mingw32)
find_library(SDL2_MWINDOWS_LIBRARY mwindows)
endif()
if(SDL2_PREFIX)
# Restore things the way they used to be.
set(CMAKE_PREFIX_PATH ${SDL2_ORIGPREFIXPATH})
endif()
# handle the QUIETLY and REQUIRED arguments and set QUATLIB_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SDL2
DEFAULT_MSG
SDL2_LIBRARY
SDL2_INCLUDE_DIR
${SDL2_EXTRA_REQUIRED})
if(SDL2_FOUND)
if(NOT TARGET SDL2::SDL2)
# Create SDL2::SDL2
if(WIN32 AND SDL2_RUNTIME_LIBRARY)
set(SDL2_DYNAMIC TRUE)
add_library(SDL2::SDL2 SHARED IMPORTED)
set_target_properties(SDL2::SDL2
PROPERTIES
IMPORTED_IMPLIB "${SDL2_LIBRARY}"
IMPORTED_LOCATION "${SDL2_RUNTIME_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
) )
# On 32bit build find the 32bit libs else()
ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8) add_library(SDL2::SDL2 UNKNOWN IMPORTED)
FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2 if(SDL2_FRAMEWORK AND SDL2_FRAMEWORK_NAME)
HINTS # Handle the case that SDL2 is a framework and we were able to decompose it above.
$ENV{SDL2} set_target_properties(SDL2::SDL2 PROPERTIES
PATH_SUFFIXES lib IMPORTED_LOCATION "${SDL2_FRAMEWORK}/${SDL2_FRAMEWORK_NAME}")
lib/x86 elseif(_sdl2_framework AND SDL2_LIBRARY MATCHES "(/[^/]+)*.framework$")
i686-w64-mingw32/lib # Handle the case that SDL2 is a framework and SDL_LIBRARY is just the framework itself.
PATHS
/sw # This takes the basename of the framework, without the extension,
/opt/local # and sets it (as a child of the framework) as the imported location for the target.
/opt/csw # This is the library symlink inside of the framework.
/opt set_target_properties(SDL2::SDL2 PROPERTIES
IMPORTED_LOCATION "${SDL2_LIBRARY}/${CMAKE_MATCH_1}")
else()
# Handle non-frameworks (including non-Mac), as well as the case that we're given the library inside of the framework
set_target_properties(SDL2::SDL2 PROPERTIES
IMPORTED_LOCATION "${SDL2_LIBRARY}")
endif()
set_target_properties(SDL2::SDL2
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
) )
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8) endif()
IF(NOT SDL2_BUILDING_LIBRARY) if(APPLE)
IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") # Need Cocoa here, is always a framework
# Non-OS X framework versions expect you to also dynamically link to find_library(SDL2_COCOA_LIBRARY Cocoa)
# SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms list(APPEND SDL2_EXTRA_REQUIRED SDL2_COCOA_LIBRARY)
# seem to provide SDL2main for compatibility even though they don't if(SDL2_COCOA_LIBRARY)
# necessarily need it. set_target_properties(SDL2::SDL2 PROPERTIES
# Lookup the 64 bit libs on x64 IMPORTED_LINK_INTERFACE_LIBRARIES ${SDL2_COCOA_LIBRARY})
IF(CMAKE_SIZEOF_VOID_P EQUAL 8) endif()
FIND_LIBRARY(SDL2MAIN_LIBRARY endif()
NAMES SDL2main
HINTS
$ENV{SDL2}
PATH_SUFFIXES lib64 lib
lib/x64
x86_64-w64-mingw32/lib
PATHS
/sw
/opt/local
/opt/csw
/opt
)
# On 32bit build find the 32bit libs
ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8)
FIND_LIBRARY(SDL2MAIN_LIBRARY
NAMES SDL2main
HINTS
$ENV{SDL2}
PATH_SUFFIXES lib
lib/x86
i686-w64-mingw32/lib
PATHS
/sw
/opt/local
/opt/csw
/opt
)
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)
ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
ENDIF(NOT SDL2_BUILDING_LIBRARY)
# SDL2 may require threads on your system.
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
# But for non-OSX systems, I will use the CMake Threads package.
IF(NOT APPLE)
FIND_PACKAGE(Threads)
ENDIF(NOT APPLE)
# MinGW needs an additional library, mwindows # Compute what to do with SDL2main
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows set(SDL2MAIN_LIBRARIES SDL2::SDL2)
# (Actually on second look, I think it only needs one of the m* libraries.) add_library(SDL2::SDL2main INTERFACE IMPORTED)
IF(MINGW) if(SDL2_SDLMAIN_LIBRARY)
SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW") add_library(SDL2::SDL2main_real STATIC IMPORTED)
ENDIF(MINGW) set_target_properties(SDL2::SDL2main_real
PROPERTIES
IMPORTED_LOCATION "${SDL2_SDLMAIN_LIBRARY}")
set(SDL2MAIN_LIBRARIES SDL2::SDL2main_real ${SDL2MAIN_LIBRARIES})
endif()
if(MINGW)
# MinGW requires some additional libraries to appear earlier in the link line.
if(SDL2PC_LIBRARIES)
# Use pkgconfig-suggested extra libraries if available.
list(REMOVE_ITEM SDL2PC_LIBRARIES SDL2main SDL2)
set(SDL2MAIN_LIBRARIES ${SDL2PC_LIBRARIES} ${SDL2MAIN_LIBRARIES})
else()
# fall back to extra libraries specified in pkg-config in
# an official binary distro of SDL2 for MinGW I downloaded
if(SDL2_MINGW_LIBRARY)
set(SDL2MAIN_LIBRARIES ${SDL2_MINGW_LIBRARY} ${SDL2MAIN_LIBRARIES})
endif()
if(SDL2_MWINDOWS_LIBRARY)
set(SDL2MAIN_LIBRARIES ${SDL2_MWINDOWS_LIBRARY} ${SDL2MAIN_LIBRARIES})
endif()
endif()
set_target_properties(SDL2::SDL2main
PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "main=SDL_main")
endif()
set_target_properties(SDL2::SDL2main
PROPERTIES
INTERFACE_LINK_LIBRARIES "${SDL2MAIN_LIBRARIES}")
endif()
mark_as_advanced(SDL2_ROOT_DIR)
endif()
SET(SDL2_FOUND "NO") mark_as_advanced(SDL2_LIBRARY
IF(SDL2_LIBRARY_TEMP) SDL2_RUNTIME_LIBRARY
# For SDL2main SDL2_INCLUDE_DIR
IF(NOT SDL2_BUILDING_LIBRARY) SDL2_SDLMAIN_LIBRARY
IF(SDL2MAIN_LIBRARY) SDL2_COCOA_LIBRARY
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP}) SDL2_MINGW_LIBRARY
ENDIF(SDL2MAIN_LIBRARY) SDL2_MWINDOWS_LIBRARY)
ENDIF(NOT SDL2_BUILDING_LIBRARY)
# 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")
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)
# For MinGW library
IF(MINGW)
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
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 "")
SET(SDL2_FOUND "YES")
ENDIF(SDL2_LIBRARY_TEMP)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)