1# - Locate SDL library
2# This module defines
3#  SDL2_LIBRARY, the name of the library to link against
4#  SDL2_FOUND, if false, do not try to link to SDL
5#  SDL2_INCLUDE_DIR, where to find SDL.h
6#  SDL2_VERSION_STRING, human-readable string containing the version of SDL
7#
8# This module responds to the flag:
9#  SDL2_BUILDING_LIBRARY
10#    If this is defined, then no SDL2_main will be linked in because
11#    only applications need main().
12#    Otherwise, it is assumed you are building an application and this
13#    module will attempt to locate and set the proper link flags
14#    as part of the returned SDL2_LIBRARY variable.
15#
16# Don't forget to include SDLmain.h and SDLmain.m your project for the
17# OS X framework based version. (Other versions link to -lSDLmain which
18# this module will try to find on your behalf.) Also for OS X, this
19# module will automatically add the -framework Cocoa on your behalf.
20#
21#
22# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
23# and no SDL2_LIBRARY, it means CMake did not find your SDL library
24# (SDL.dll, libsdl.so, SDL.framework, etc).
25# Set SDL2_LIBRARY_TEMP to point to your SDL library, and configure again.
26# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
27# as appropriate. These values are used to generate the final SDL2_LIBRARY
28# variable, but when these values are unset, SDL2_LIBRARY does not get created.
29#
30#
31# $SDLDIR is an environment variable that would
32# correspond to the ./configure --prefix=$SDLDIR
33# used in building SDL.
34# l.e.galup  9-20-02
35#
36# Modified by Eric Wing.
37# Added code to assist with automated building by using environmental variables
38# and providing a more controlled/consistent search behavior.
39# Added new modifications to recognize OS X frameworks and
40# additional Unix paths (FreeBSD, etc).
41# Also corrected the header search path to follow "proper" SDL guidelines.
42# Added a search for SDLmain which is needed by some platforms.
43# Added a search for threads which is needed by some platforms.
44# Added needed compile switches for MinGW.
45#
46# On OSX, this will prefer the Framework version (if found) over others.
47# People will have to manually change the cache values of
48# SDL2_LIBRARY to override this selection or set the CMake environment
49# CMAKE_INCLUDE_PATH to modify the search paths.
50#
51# Note that the header path has changed from SDL/SDL.h to just SDL.h
52# This needed to change because "proper" SDL convention
53# is #include "SDL.h", not <SDL/SDL.h>. This is done for portability
54# reasons because not all systems place things in SDL/ (see FreeBSD).
55
56#=============================================================================
57# Copyright 2003-2009 Kitware, Inc.
58# Copyright 2012 Benjamin Eikel
59#
60# Distributed under the OSI-approved BSD License (the "License");
61# see accompanying file Copyright.txt for details.
62#
63# This software is distributed WITHOUT ANY WARRANTY; without even the
64# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
65# See the License for more information.
66#=============================================================================
67# (To distribute this file outside of CMake, substitute the full
68#  License text for the above reference.)
69
70find_path(SDL2_INCLUDE_DIR SDL.h
71  HINTS
72    ENV SDL2DIR
73  PATH_SUFFIXES include/SDL2
74)
75
76# SDL-1.1 is the name used by FreeBSD ports...
77# don't confuse it for the version number.
78find_library(SDL2_LIBRARY_TEMP
79  NAMES SDL2
80  HINTS
81    ENV SDL2DIR
82  PATH_SUFFIXES lib
83)
84
85if(NOT SDL2_BUILDING_LIBRARY)
86  if(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
87    # Non-OS X framework versions expect you to also dynamically link to
88    # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
89    # seem to provide SDLmain for compatibility even though they don't
90    # necessarily need it.
91    find_library(SDL2MAIN_LIBRARY
92      NAMES SDL2main
93      HINTS
94        ENV SDL2DIR
95      PATH_SUFFIXES lib
96      PATHS
97      /sw
98      /opt/local
99      /opt/csw
100      /opt
101    )
102  endif()
103endif()
104
105# SDL may require threads on your system.
106# The Apple build may not need an explicit flag because one of the
107# frameworks may already provide it.
108# But for non-OSX systems, I will use the CMake Threads package.
109if(NOT APPLE)
110  find_package(Threads)
111endif()
112
113# MinGW needs an additional library, mwindows
114# It's total link flags should look like -lmingw32 -lSDLmain -lSDL -lmwindows
115# (Actually on second look, I think it only needs one of the m* libraries.)
116if(MINGW)
117  set(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
118endif()
119
120if(SDL2_LIBRARY_TEMP)
121  # For SDLmain
122  if(SDL2MAIN_LIBRARY AND NOT SDL2_BUILDING_LIBRARY)
123    list(FIND SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" _SDL2_MAIN_INDEX)
124    if(_SDL2_MAIN_INDEX EQUAL -1)
125      set(SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" ${SDL2_LIBRARY_TEMP})
126    endif()
127    unset(_SDL2_MAIN_INDEX)
128  endif()
129
130  # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
131  # CMake doesn't display the -framework Cocoa string in the UI even
132  # though it actually is there if I modify a pre-used variable.
133  # I think it has something to do with the CACHE STRING.
134  # So I use a temporary variable until the end so I can set the
135  # "real" variable in one-shot.
136  if(APPLE)
137    set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
138  endif()
139
140  # For threads, as mentioned Apple doesn't need this.
141  # In fact, there seems to be a problem if I used the Threads package
142  # and try using this line, so I'm just skipping it entirely for OS X.
143  if(NOT APPLE)
144    set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
145  endif()
146
147  # For MinGW library
148  if(MINGW)
149    set(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
150  endif()
151
152  # Set the final string here so the GUI reflects the final state.
153  set(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
154  # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
155  set(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
156endif()
157
158if(SDL2_INCLUDE_DIR AND EXISTS "${SDL2_INCLUDE_DIR}/SDL2_version.h")
159  file(STRINGS "${SDL2_INCLUDE_DIR}/SDL2_version.h" SDL2_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_MAJOR_VERSION[ \t]+[0-9]+$")
160  file(STRINGS "${SDL2_INCLUDE_DIR}/SDL2_version.h" SDL2_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_MINOR_VERSION[ \t]+[0-9]+$")
161  file(STRINGS "${SDL2_INCLUDE_DIR}/SDL2_version.h" SDL2_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_PATCHLEVEL[ \t]+[0-9]+$")
162  string(REGEX REPLACE "^#define[ \t]+SDL2_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MAJOR "${SDL2_VERSION_MAJOR_LINE}")
163  string(REGEX REPLACE "^#define[ \t]+SDL2_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MINOR "${SDL2_VERSION_MINOR_LINE}")
164  string(REGEX REPLACE "^#define[ \t]+SDL2_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_PATCH "${SDL2_VERSION_PATCH_LINE}")
165  set(SDL2_VERSION_STRING ${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH})
166  unset(SDL2_VERSION_MAJOR_LINE)
167  unset(SDL2_VERSION_MINOR_LINE)
168  unset(SDL2_VERSION_PATCH_LINE)
169  unset(SDL2_VERSION_MAJOR)
170  unset(SDL2_VERSION_MINOR)
171  unset(SDL2_VERSION_PATCH)
172endif()
173
174include(FindPackageHandleStandardArgs)
175
176FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2
177                                  REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
178                                  VERSION_VAR SDL2_VERSION_STRING)
179