1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindSDL
6-------
7
8Locate the SDL library
9
10
11Imported targets
12^^^^^^^^^^^^^^^^
13
14.. versionadded:: 3.19
15
16This module defines the following :prop_tgt:`IMPORTED` target:
17
18``SDL::SDL``
19  The SDL library, if found
20
21Result variables
22^^^^^^^^^^^^^^^^
23
24This module will set the following variables in your project:
25
26``SDL_INCLUDE_DIRS``
27  where to find SDL.h
28``SDL_LIBRARIES``
29  the name of the library to link against
30``SDL_FOUND``
31  if false, do not try to link to SDL
32``SDL_VERSION``
33  the human-readable string containing the version of SDL if found
34``SDL_VERSION_MAJOR``
35  SDL major version
36``SDL_VERSION_MINOR``
37  SDL minor version
38``SDL_VERSION_PATCH``
39  SDL patch version
40
41.. versionadded:: 3.19
42  Added the ``SDL_INCLUDE_DIRS``, ``SDL_LIBRARIES`` and ``SDL_VERSION[_<PART>]``
43  variables.
44
45Cache variables
46^^^^^^^^^^^^^^^
47
48These variables may optionally be set to help this module find the correct files:
49
50``SDL_INCLUDE_DIR``
51  where to find SDL.h
52``SDL_LIBRARY``
53  the name of the library to link against
54
55
56Variables for locating SDL
57^^^^^^^^^^^^^^^^^^^^^^^^^^
58
59This module responds to the flag:
60
61``SDL_BUILDING_LIBRARY``
62    If this is defined, then no SDL_main will be linked in because
63    only applications need main().
64    Otherwise, it is assumed you are building an application and this
65    module will attempt to locate and set the proper link flags
66    as part of the returned SDL_LIBRARY variable.
67
68
69Obsolete variables
70^^^^^^^^^^^^^^^^^^
71
72.. deprecated:: 3.19
73
74These variables are obsolete and provided for backwards compatibility:
75
76``SDL_VERSION_STRING``
77  the human-readable string containing the version of SDL if found.
78  Identical to SDL_VERSION
79
80
81Don't forget to include SDLmain.h and SDLmain.m your project for the
82OS X framework based version.  (Other versions link to -lSDLmain which
83this module will try to find on your behalf.) Also for OS X, this
84module will automatically add the -framework Cocoa on your behalf.
85
86
87
88Additional Note: If you see an empty SDL_LIBRARY_TEMP in your
89configuration and no SDL_LIBRARY, it means CMake did not find your SDL
90library (SDL.dll, libsdl.so, SDL.framework, etc).  Set
91SDL_LIBRARY_TEMP to point to your SDL library, and configure again.
92Similarly, if you see an empty SDLMAIN_LIBRARY, you should set this
93value as appropriate.  These values are used to generate the final
94SDL_LIBRARY variable, but when these values are unset, SDL_LIBRARY
95does not get created.
96
97
98
99$SDLDIR is an environment variable that would correspond to the
100./configure --prefix=$SDLDIR used in building SDL.  l.e.galup 9-20-02
101
102On OSX, this will prefer the Framework version (if found) over others.
103People will have to manually change the cache values of SDL_LIBRARY to
104override this selection or set the CMake environment
105CMAKE_INCLUDE_PATH to modify the search paths.
106
107Note that the header path has changed from SDL/SDL.h to just SDL.h
108This needed to change because "proper" SDL convention is #include
109"SDL.h", not <SDL/SDL.h>.  This is done for portability reasons
110because not all systems place things in SDL/ (see FreeBSD).
111#]=======================================================================]
112
113find_path(SDL_INCLUDE_DIR SDL.h
114  HINTS
115    ENV SDLDIR
116  PATH_SUFFIXES SDL SDL12 SDL11
117                # path suffixes to search inside ENV{SDLDIR}
118                include/SDL include/SDL12 include/SDL11 include
119)
120
121if(CMAKE_SIZEOF_VOID_P EQUAL 8)
122  set(VC_LIB_PATH_SUFFIX lib/x64)
123else()
124  set(VC_LIB_PATH_SUFFIX lib/x86)
125endif()
126
127# On FreeBSD SDL depends on libiconv and SDL_stdinc.h includes iconv.h, which is
128# located in ${LOCALBASE}/include. Append {LOCALBASE}/include to
129# the SDL_INCLUDE_DIR, thus allow to build SDL apps out of box.
130list(APPEND SDL_INCLUDE_DIR /usr/local/include)
131
132# SDL-1.1 is the name used by FreeBSD ports...
133# don't confuse it for the version number.
134find_library(SDL_LIBRARY_TEMP
135  NAMES SDL SDL-1.1
136  HINTS
137    ENV SDLDIR
138  PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
139)
140
141# Hide this cache variable from the user, it's an internal implementation
142# detail. The documented library variable for the user is SDL_LIBRARY
143# which is derived from SDL_LIBRARY_TEMP further below.
144set_property(CACHE SDL_LIBRARY_TEMP PROPERTY TYPE INTERNAL)
145
146if(NOT SDL_BUILDING_LIBRARY)
147  if(NOT SDL_INCLUDE_DIR MATCHES ".framework")
148    # Non-OS X framework versions expect you to also dynamically link to
149    # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
150    # seem to provide SDLmain for compatibility even though they don't
151    # necessarily need it.
152    find_library(SDLMAIN_LIBRARY
153      NAMES SDLmain SDLmain-1.1
154      HINTS
155        ENV SDLDIR
156      PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
157      PATHS
158      /opt
159    )
160  endif()
161endif()
162
163# SDL may require threads on your system.
164# The Apple build may not need an explicit flag because one of the
165# frameworks may already provide it.
166# But for non-OSX systems, I will use the CMake Threads package.
167if(NOT APPLE)
168  find_package(Threads)
169endif()
170
171# MinGW needs an additional link flag, -mwindows
172# It's total link flags should look like -lmingw32 -lSDLmain -lSDL -mwindows
173if(MINGW)
174  set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW")
175endif()
176
177if(SDL_LIBRARY_TEMP)
178  # For SDLmain
179  if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY)
180    list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
181    if(_SDL_MAIN_INDEX EQUAL -1)
182      set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP})
183    endif()
184    unset(_SDL_MAIN_INDEX)
185  endif()
186
187  # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
188  # CMake doesn't display the -framework Cocoa string in the UI even
189  # though it actually is there if I modify a pre-used variable.
190  # I think it has something to do with the CACHE STRING.
191  # So I use a temporary variable until the end so I can set the
192  # "real" variable in one-shot.
193  if(APPLE)
194    set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} "-framework Cocoa")
195  endif()
196
197  # For threads, as mentioned Apple doesn't need this.
198  # In fact, there seems to be a problem if I used the Threads package
199  # and try using this line, so I'm just skipping it entirely for OS X.
200  if(NOT APPLE)
201    set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
202  endif()
203
204  # For MinGW library
205  if(MINGW)
206    set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP})
207  endif()
208
209  # Set the final string here so the GUI reflects the final state.
210  set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
211endif()
212
213if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
214  file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
215  file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
216  file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
217  string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}")
218  string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}")
219  string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}")
220  unset(SDL_VERSION_MAJOR_LINE)
221  unset(SDL_VERSION_MINOR_LINE)
222  unset(SDL_VERSION_PATCH_LINE)
223  set(SDL_VERSION ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH})
224  set(SDL_VERSION_STRING ${SDL_VERSION})
225endif()
226
227include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
228
229FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
230                                  REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
231                                  VERSION_VAR SDL_VERSION_STRING)
232
233if(SDL_FOUND)
234  set(SDL_LIBRARIES ${SDL_LIBRARY})
235  set(SDL_INCLUDE_DIRS ${SDL_INCLUDE_DIR})
236  if(NOT TARGET SDL::SDL)
237    add_library(SDL::SDL INTERFACE IMPORTED)
238    set_target_properties(SDL::SDL PROPERTIES
239      INTERFACE_INCLUDE_DIRECTORIES "${SDL_INCLUDE_DIR}"
240      INTERFACE_LINK_LIBRARIES "${SDL_LIBRARY}")
241  endif()
242endif()
243