]> icculus.org git repositories - taylor/freespace2.git/blob - cmake/FindSDL2.cmake
fix broken FindSDL2.cmake from post-rebase merge
[taylor/freespace2.git] / cmake / FindSDL2.cmake
1 # - Locate SDL2 library (modified from CMake's FindSDL.cmake)
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 SDL2
7 #
8 # This module responds to the the flag:
9 #  SDL2_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 SDL2_LIBRARY variable.
15 #
16 # Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
17 # and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
18 # (SDL2.dll, libsdl2.so, SDL2.framework, etc).
19 # Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
20 # Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
21 # as appropriate. These values are used to generate the final SDL2_LIBRARY
22 # variable, but when these values are unset, SDL2_LIBRARY does not get created.
23 #
24 #
25 # $SDL2DIR is an environment variable that would
26 # correspond to the ./configure --prefix=$SDL2DIR
27 # used in building SDL.
28 #
29
30 #=============================================================================
31 # Copyright 2014 Justin Jacobs
32 #
33 # Distributed under the OSI-approved BSD License (the "License");
34 # see accompanying file Copyright.txt for details.
35 #
36 # This software is distributed WITHOUT ANY WARRANTY; without even the
37 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
38 # See the License for more information.
39 #=============================================================================
40
41 if(EMSCRIPTEN)
42   set(SDL2_LIBRARY "nul" CACHE STRING "emscripten override" FORCE)
43   set(SDL2_INCLUDE_DIR "${CMAKE_SYSTEM_INCLUDE_PATH}/SDL2" CACHE STRING "emscripten override" FORCE)
44 else()
45   find_path(SDL2_INCLUDE_DIR SDL.h
46     HINTS
47       "${SDL2DIR}"
48         ENV SDL2DIR
49         PATH_SUFFIXES include/SDL2 include
50   )
51
52   if(CMAKE_SIZEOF_VOID_P EQUAL 8)
53     set(VC_LIB_PATH_SUFFIX lib/x64)
54   else()
55     set(VC_LIB_PATH_SUFFIX lib/x86)
56   endif()
57
58   find_library(SDL2_LIBRARY_TEMP
59     NAMES SDL2
60     HINTS
61       "${SDL2DIR}"
62           ENV SDL2DIR
63     PATH_SUFFIXES lib "${VC_LIB_PATH_SUFFIX}"
64   )
65 endif()
66
67 if(NOT SDL2_BUILDING_LIBRARY)
68   if(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
69         # Non-OS X framework versions expect you to also dynamically link to
70         # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
71         # seem to provide SDLmain for compatibility even though they don't
72         # necessarily need it.
73         find_library(SDL2MAIN_LIBRARY
74           NAMES SDL2main
75           HINTS
76             "${SDL2DIR}"
77                 ENV SDL2DIR
78           PATH_SUFFIXES lib "${VC_LIB_PATH_SUFFIX}"
79           PATHS
80           /sw
81           /opt/local
82           /opt/csw
83           /opt
84         )
85   endif()
86 endif()
87
88 # SDL2 may require threads on your system.
89 # The Apple build may not need an explicit flag because one of the
90 # frameworks may already provide it.
91 # But for non-OSX systems, I will use the CMake Threads package.
92 if(NOT APPLE)
93   find_package(Threads)
94 endif()
95
96 # MinGW needs an additional library, mwindows
97 # It's total link flags should look like -lmingw32 -lSDLmain -lSDL -lmwindows
98 # (Actually on second look, I think it only needs one of the m* libraries.)
99 if(MINGW)
100   set(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
101 endif()
102
103 if(SDL2_LIBRARY_TEMP)
104   # For SDLmain
105   if(SDL2MAIN_LIBRARY AND NOT SDL2_BUILDING_LIBRARY)
106         list(FIND SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" _SDL2_MAIN_INDEX)
107         if(_SDL2_MAIN_INDEX EQUAL -1)
108           set(SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" ${SDL2_LIBRARY_TEMP})
109         endif()
110         unset(_SDL2_MAIN_INDEX)
111   endif()
112
113   # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
114   # CMake doesn't display the -framework Cocoa string in the UI even
115   # though it actually is there if I modify a pre-used variable.
116   # I think it has something to do with the CACHE STRING.
117   # So I use a temporary variable until the end so I can set the
118   # "real" variable in one-shot.
119   if(APPLE)
120         set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
121   endif()
122
123   # For threads, as mentioned Apple doesn't need this.
124   # In fact, there seems to be a problem if I used the Threads package
125   # and try using this line, so I'm just skipping it entirely for OS X.
126   if(NOT APPLE)
127         set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
128   endif()
129
130   # For MinGW library
131   if(MINGW)
132         set(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
133   endif()
134
135   # Set the final string here so the GUI reflects the final state.
136   set(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
137   # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
138   set(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
139 endif()
140
141 if(SDL2_INCLUDE_DIR AND EXISTS "${SDL2_INCLUDE_DIR}/SDL_version.h")
142   file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
143   file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
144   file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
145   string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MAJOR "${SDL2_VERSION_MAJOR_LINE}")
146   string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MINOR "${SDL2_VERSION_MINOR_LINE}")
147   string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_PATCH "${SDL2_VERSION_PATCH_LINE}")
148   set(SDL2_VERSION_STRING ${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH})
149   unset(SDL2_VERSION_MAJOR_LINE)
150   unset(SDL2_VERSION_MINOR_LINE)
151   unset(SDL2_VERSION_PATCH_LINE)
152   unset(SDL2_VERSION_MAJOR)
153   unset(SDL2_VERSION_MINOR)
154   unset(SDL2_VERSION_PATCH)
155 endif()
156
157 include(FindPackageHandleStandardArgs)
158
159 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2
160                                                                   REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
161                                                                   VERSION_VAR SDL2_VERSION_STRING)
162