all repos — mgba @ f6755a6e1b7b0cf2b944cd8ca842746f11d6bf82

mGBA Game Boy Advance Emulator

src/platform/cmake/FindSDL2.cmake (view raw)

  1# SDL2_LIBRARIES, the name of the library to link against
  2# SDL2_FOUND, if false, do not try to link to SDL2
  3# SDL2_INCLUDE_DIRS, where to find SDL.h
  4#
  5# This module responds to the the flag:
  6# SDL2_BUILDING_LIBRARY
  7# If this is defined, then no SDL2main will be linked in because
  8# only applications need main().
  9# Otherwise, it is assumed you are building an application and this
 10# module will attempt to locate and set the the proper link flags
 11# as part of the returned SDL2_LIBRARIES variable.
 12#
 13# Don't forget to include SDLmain.h and SDLmain.m your project for the
 14# OS X framework based version. (Other versions link to -lSDL2main which
 15# this module will try to find on your behalf.) Also for OS X, this
 16# module will automatically add the -framework Cocoa on your behalf.
 17#
 18#
 19# Additional Note: If you see an empty SDL2_LIBRARIES_TEMP in your configuration
 20# and no SDL2_LIBRARIES, it means CMake did not find your SDL2 library
 21# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
 22# Set SDL2_LIBRARIES_TEMP to point to your SDL2 library, and configure again.
 23# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
 24# as appropriate. These values are used to generate the final SDL2_LIBRARIES
 25# variable, but when these values are unset, SDL2_LIBRARIES does not get created.
 26#
 27#
 28# $SDL2DIR is an environment variable that would
 29# correspond to the ./configure --prefix=$SDL2DIR
 30# used in building SDL2.
 31# l.e.galup  9-20-02
 32#
 33# Modified by Eric Wing.
 34# Added code to assist with automated building by using environmental variables
 35# and providing a more controlled/consistent search behavior.
 36# Added new modifications to recognize OS X frameworks and
 37# additional Unix paths (FreeBSD, etc).
 38# Also corrected the header search path to follow "proper" SDL guidelines.
 39# Added a search for SDL2main which is needed by some platforms.
 40# Added a search for threads which is needed by some platforms.
 41# Added needed compile switches for MinGW.
 42#
 43# On OSX, this will prefer the Framework version (if found) over others.
 44# People will have to manually change the cache values of
 45# SDL2_LIBRARIES to override this selection or set the CMake environment
 46# CMAKE_INCLUDE_PATH to modify the search paths.
 47#
 48# Note that the header path has changed from SDL2/SDL.h to just SDL.h
 49# This needed to change because "proper" SDL convention
 50# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
 51# reasons because not all systems place things in SDL2/ (see FreeBSD).
 52#
 53#=========================================================================
 54#
 55# Copyright (c) 1993-2015 Ken Martin, Will Schroeder, Bill Lorensen
 56# All rights reserved.
 57#
 58# Redistribution and use in source and binary forms, with or without
 59# modification, are permitted provided that the following conditions are met:
 60#
 61# * Redistributions of source code must retain the above copyright notice,
 62#   this list of conditions and the following disclaimer.
 63#
 64# * Redistributions in binary form must reproduce the above copyright notice,
 65#   this list of conditions and the following disclaimer in the documentation
 66#   and/or other materials provided with the distribution.
 67#
 68# * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
 69#   of any contributors may be used to endorse or promote products derived
 70#   from this software without specific prior written permission.
 71#
 72# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 73# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 74# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 75# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
 76# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 77# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 78# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 79# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 80# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 81# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 82#
 83#=========================================================================
 84#
 85SET(SDL2_SEARCH_PATHS
 86  ~/Library/Frameworks
 87  /Library/Frameworks
 88  /usr/local
 89  /usr
 90  /sw # Fink
 91  /opt/local # DarwinPorts
 92  /opt/csw # Blastwave
 93  /opt
 94  ${SDL2_PATH}
 95)
 96
 97FIND_PATH(SDL2_INCLUDE_DIRS SDL.h
 98  HINTS
 99  $ENV{SDL2DIR}
100  PATH_SUFFIXES include/SDL2 include
101  PATHS ${SDL2_SEARCH_PATHS} ${SDL2_LIBRARIES_TEMP}/../..
102)
103
104FIND_LIBRARY(SDL2_LIBRARIES_TEMP
105  NAMES SDL2
106  HINTS
107  $ENV{SDL2DIR}
108  PATH_SUFFIXES lib64 lib
109  PATHS ${SDL2_SEARCH_PATHS} ${SDL2_INCLUDE_DIRS}/../..
110)
111
112IF(NOT SDL2_BUILDING_LIBRARY)
113  IF(NOT ${SDL2_INCLUDE_DIRS} MATCHES ".framework")
114    # Non-OS X framework versions expect you to also dynamically link to
115    # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
116    # seem to provide SDL2main for compatibility even though they don't
117    # necessarily need it.
118    FIND_LIBRARY(SDL2MAIN_LIBRARY
119      NAMES SDL2main
120      HINTS
121      $ENV{SDL2DIR}
122      PATH_SUFFIXES lib64 lib
123      PATHS ${SDL2_SEARCH_PATHS}
124    )
125  ENDIF(NOT ${SDL2_INCLUDE_DIRS} MATCHES ".framework")
126ENDIF(NOT SDL2_BUILDING_LIBRARY)
127
128# SDL2 may require threads on your system.
129# The Apple build may not need an explicit flag because one of the
130# frameworks may already provide it.
131# But for non-OSX systems, I will use the CMake Threads package.
132IF(NOT APPLE)
133  FIND_PACKAGE(Threads)
134ENDIF(NOT APPLE)
135
136# MinGW needs an additional library, mwindows
137# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
138# (Actually on second look, I think it only needs one of the m* libraries.)
139IF(MINGW)
140  SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
141ENDIF(MINGW)
142
143IF(SDL2_LIBRARIES_TEMP)
144  # For SDL2main
145  IF(NOT SDL2_BUILDING_LIBRARY)
146    IF(SDL2MAIN_LIBRARY)
147      SET(SDL2_LIBRARIES_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARIES_TEMP})
148    ENDIF(SDL2MAIN_LIBRARY)
149  ENDIF(NOT SDL2_BUILDING_LIBRARY)
150
151  # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
152  # CMake doesn't display the -framework Cocoa string in the UI even
153  # though it actually is there if I modify a pre-used variable.
154  # I think it has something to do with the CACHE STRING.
155  # So I use a temporary variable until the end so I can set the
156  # "real" variable in one-shot.
157  IF(APPLE)
158    SET(SDL2_LIBRARIES_TEMP ${SDL2_LIBRARIES_TEMP} "-framework Cocoa")
159  ENDIF(APPLE)
160
161  # For threads, as mentioned Apple doesn't need this.
162  # In fact, there seems to be a problem if I used the Threads package
163  # and try using this line, so I'm just skipping it entirely for OS X.
164  IF(NOT APPLE)
165    SET(SDL2_LIBRARIES_TEMP ${SDL2_LIBRARIES_TEMP} ${CMAKE_THREAD_LIBS_INIT})
166  ENDIF(NOT APPLE)
167
168  # For MinGW library
169  IF(MINGW)
170    SET(SDL2_LIBRARIES_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARIES_TEMP})
171  ENDIF(MINGW)
172
173  # Set the final string here so the GUI reflects the final state.
174  SET(SDL2_LIBRARIES ${SDL2_LIBRARIES_TEMP} CACHE STRING "Where the SDL2 Library can be found")
175  # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
176  SET(SDL2_LIBRARIES_TEMP "${SDL2_LIBRARIES_TEMP}" CACHE INTERNAL "")
177ENDIF(SDL2_LIBRARIES_TEMP)
178
179INCLUDE(FindPackageHandleStandardArgs)
180
181FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARIES SDL2_INCLUDE_DIRS)