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