Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / FindSDL.cmake
1 # - Locate SDL library
2 # This module defines
3 #  SDL_LIBRARY, the name of the library to link against
4 #  SDL_FOUND, if false, do not try to link to SDL
5 #  SDL_INCLUDE_DIR, where to find SDL.h
6 #  SDL_VERSION_STRING, human-readable string containing the version of SDL
7 #
8 # This module responds to the the flag:
9 #  SDL_BUILDING_LIBRARY
10 #    If this is defined, then no SDL_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 the proper link flags
14 #    as part of the returned SDL_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 SDL_LIBRARY_TEMP in your configuration
23 # and no SDL_LIBRARY, it means CMake did not find your SDL library
24 # (SDL.dll, libsdl.so, SDL.framework, etc).
25 # Set SDL_LIBRARY_TEMP to point to your SDL library, and configure again.
26 # Similarly, if you see an empty SDLMAIN_LIBRARY, you should set this value
27 # as appropriate. These values are used to generate the final SDL_LIBRARY
28 # variable, but when these values are unset, SDL_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 # SDL_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
70 find_path(SDL_INCLUDE_DIR SDL.h
71   HINTS
72     ENV SDLDIR
73   PATH_SUFFIXES include/SDL include/SDL12 include/SDL11 include
74 )
75
76 # SDL-1.1 is the name used by FreeBSD ports...
77 # don't confuse it for the version number.
78 find_library(SDL_LIBRARY_TEMP
79   NAMES SDL SDL-1.1
80   HINTS
81     ENV SDLDIR
82   PATH_SUFFIXES lib
83 )
84
85 if(NOT SDL_BUILDING_LIBRARY)
86   if(NOT ${SDL_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(SDLMAIN_LIBRARY
92       NAMES SDLmain SDLmain-1.1
93       HINTS
94         ENV SDLDIR
95       PATH_SUFFIXES lib
96       PATHS
97       /sw
98       /opt/local
99       /opt/csw
100       /opt
101     )
102   endif()
103 endif()
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.
109 if(NOT APPLE)
110   find_package(Threads)
111 endif()
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.)
116 if(MINGW)
117   set(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
118 endif()
119
120 if(SDL_LIBRARY_TEMP)
121   # For SDLmain
122   if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY)
123     list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
124     if(_SDL_MAIN_INDEX EQUAL -1)
125       list(APPEND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}")
126     endif()
127     unset(_SDL_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(SDL_LIBRARY_TEMP ${SDL_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(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
145   endif()
146
147   # For MinGW library
148   if(MINGW)
149     set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP})
150   endif()
151
152   # Set the final string here so the GUI reflects the final state.
153   set(SDL_LIBRARY ${SDL_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(SDL_LIBRARY_TEMP "${SDL_LIBRARY_TEMP}" CACHE INTERNAL "")
156 endif()
157
158 if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
159   file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
160   file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
161   file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
162   string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}")
163   string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}")
164   string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}")
165   set(SDL_VERSION_STRING ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH})
166   unset(SDL_VERSION_MAJOR_LINE)
167   unset(SDL_VERSION_MINOR_LINE)
168   unset(SDL_VERSION_PATCH_LINE)
169   unset(SDL_VERSION_MAJOR)
170   unset(SDL_VERSION_MINOR)
171   unset(SDL_VERSION_PATCH)
172 endif()
173
174 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
175
176 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
177                                   REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
178                                   VERSION_VAR SDL_VERSION_STRING)