Slightly restructure some loops
[openal-soft.git] / cmake / FindSDL2.cmake
blobe808d006154fb5b159d51245ce9dcf8e2e8530b8
1 # Locate SDL2 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 SDL2
5 # SDL2_INCLUDE_DIR, where to find SDL.h
7 # This module responds to the the flag:
8 # SDL2_BUILDING_LIBRARY
9 # If this is defined, then no SDL2_main will be linked in because
10 # only applications need main().
11 # Otherwise, it is assumed you are building an application and this
12 # module will attempt to locate and set the the proper link flags
13 # as part of the returned SDL2_LIBRARY variable.
15 # Don't forget to include SDL2main.h and SDL2main.m your project for the
16 # OS X framework based version. (Other versions link to -lSDL2main which
17 # this module will try to find on your behalf.) Also for OS X, this
18 # module will automatically add the -framework Cocoa on your behalf.
21 # Additional Note: If you see an empty SDL2_CORE_LIBRARY in your configuration
22 # and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
23 # (SDL2.dll, libsdl2.so, SDL2.framework, etc).
24 # Set SDL2_CORE_LIBRARY to point to your SDL2 library, and configure again.
25 # Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
26 # as appropriate. These values are used to generate the final SDL2_LIBRARY
27 # variable, but when these values are unset, SDL2_LIBRARY does not get created.
30 # $SDL2DIR is an environment variable that would
31 # correspond to the ./configure --prefix=$SDL2DIR
32 # used in building SDL2.
33 # l.e.galup  9-20-02
35 # Modified by Eric Wing.
36 # Added code to assist with automated building by using environmental variables
37 # and providing a more controlled/consistent search behavior.
38 # Added new modifications to recognize OS X frameworks and
39 # additional Unix paths (FreeBSD, etc).
40 # Also corrected the header search path to follow "proper" SDL2 guidelines.
41 # Added a search for SDL2main which is needed by some platforms.
42 # Added a search for threads which is needed by some platforms.
43 # Added needed compile switches for MinGW.
45 # On OSX, this will prefer the Framework version (if found) over others.
46 # People will have to manually change the cache values of
47 # SDL2_LIBRARY to override this selection or set the CMake environment
48 # CMAKE_INCLUDE_PATH to modify the search paths.
50 # Note that the header path has changed from SDL2/SDL.h to just SDL.h
51 # This needed to change because "proper" SDL2 convention
52 # is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
53 # reasons because not all systems place things in SDL2/ (see FreeBSD).
55 # Ported by Johnny Patterson. This is a literal port for SDL2 of the FindSDL.cmake
56 # module with the minor edit of changing "SDL" to "SDL2" where necessary. This
57 # was not created for redistribution, and exists temporarily pending official
58 # SDL2 CMake modules.
60 #=============================================================================
61 # Copyright 2003-2009 Kitware, Inc.
63 # Distributed under the OSI-approved BSD License (the "License");
64 # see accompanying file Copyright.txt for details.
66 # This software is distributed WITHOUT ANY WARRANTY; without even the
67 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
68 # See the License for more information.
69 #=============================================================================
70 # (To distribute this file outside of CMake, substitute the full
71 #  License text for the above reference.)
74 FIND_PATH(SDL2_INCLUDE_DIR SDL.h
75   HINTS
76   $ENV{SDL2DIR}
77   PATH_SUFFIXES include/SDL2 include
78   PATHS
79   ~/Library/Frameworks
80   /Library/Frameworks
81   /usr/local/include/SDL2
82   /usr/include/SDL2
83   /sw # Fink
84   /opt/local # DarwinPorts
85   /opt/csw # Blastwave
86   /opt
88 #MESSAGE("SDL2_INCLUDE_DIR is ${SDL2_INCLUDE_DIR}")
90 FIND_LIBRARY(SDL2_CORE_LIBRARY
91   NAMES SDL2
92   HINTS
93   $ENV{SDL2DIR}
94   PATH_SUFFIXES lib64 lib
95   PATHS
96   /sw
97   /opt/local
98   /opt/csw
99   /opt
101 #MESSAGE("SDL2_CORE_LIBRARY is ${SDL2_CORE_LIBRARY}")
103 IF(NOT SDL2_BUILDING_LIBRARY)
104   IF(NOT ${SDL2_INCLUDE_DIR} 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
115       /sw
116       /opt/local
117       /opt/csw
118       /opt
119     )
120   ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
121 ENDIF(NOT SDL2_BUILDING_LIBRARY)
123 # SDL2 may require threads on your system.
124 # The Apple build may not need an explicit flag because one of the
125 # frameworks may already provide it.
126 # But for non-OSX systems, I will use the CMake Threads package.
127 IF(NOT APPLE)
128   FIND_PACKAGE(Threads)
129 ENDIF(NOT APPLE)
131 # MinGW needs an additional library, mwindows
132 # It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
133 # (Actually on second look, I think it only needs one of the m* libraries.)
134 IF(MINGW)
135   SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
136 ENDIF(MINGW)
138 SET(SDL2_FOUND "NO")
139 IF(SDL2_CORE_LIBRARY)
140   SET(SDL2_LIBRARY_TEMP ${SDL2_CORE_LIBRARY})
142   # For SDL2main
143   IF(NOT SDL2_BUILDING_LIBRARY)
144     IF(SDL2MAIN_LIBRARY)
145       SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
146     ENDIF(SDL2MAIN_LIBRARY)
147   ENDIF(NOT SDL2_BUILDING_LIBRARY)
149   # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
150   # CMake doesn't display the -framework Cocoa string in the UI even
151   # though it actually is there if I modify a pre-used variable.
152   # I think it has something to do with the CACHE STRING.
153   # So I use a temporary variable until the end so I can set the
154   # "real" variable in one-shot.
155   IF(APPLE)
156     SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
157   ENDIF(APPLE)
159   # For threads, as mentioned Apple doesn't need this.
160   # In fact, there seems to be a problem if I used the Threads package
161   # and try using this line, so I'm just skipping it entirely for OS X.
162   IF(NOT APPLE)
163     SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
164   ENDIF(NOT APPLE)
166   # For MinGW library
167   IF(MINGW)
168     SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
169   ENDIF(MINGW)
170   
171   IF(WIN32)
172     SET(SDL2_LIBRARY_TEMP winmm imm32 version msimg32 ${SDL2_LIBRARY_TEMP})
173   ENDIF(WIN32)
175   # Set the final string here so the GUI reflects the final state.
176   SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP})
178   SET(SDL2_FOUND "YES")
179 ENDIF(SDL2_CORE_LIBRARY)
181 INCLUDE(FindPackageHandleStandardArgs)
182 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2
183                                   REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
185 IF(SDL2_STATIC)
186   if (UNIX AND NOT APPLE)
187     EXECUTE_PROCESS(COMMAND sdl2-config --static-libs OUTPUT_VARIABLE SDL2_LINK_FLAGS)
188     STRING(REGEX REPLACE "(\r?\n)+$" "" SDL2_LINK_FLAGS "${SDL2_LINK_FLAGS}")
189     SET(SDL2_LIBRARY ${SDL2_LINK_FLAGS})
190   ENDIF()
191 ENDIF(SDL2_STATIC)