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# SDL-1.1 is the name used by FreeBSD ports... 128# don't confuse it for the version number. 129find_library(SDL_LIBRARY_TEMP 130 NAMES SDL SDL-1.1 131 HINTS 132 ENV SDLDIR 133 PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 134) 135 136# Hide this cache variable from the user, it's an internal implementation 137# detail. The documented library variable for the user is SDL_LIBRARY 138# which is derived from SDL_LIBRARY_TEMP further below. 139set_property(CACHE SDL_LIBRARY_TEMP PROPERTY TYPE INTERNAL) 140 141if(NOT SDL_BUILDING_LIBRARY) 142 if(NOT SDL_INCLUDE_DIR MATCHES ".framework") 143 # Non-OS X framework versions expect you to also dynamically link to 144 # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms 145 # seem to provide SDLmain for compatibility even though they don't 146 # necessarily need it. 147 find_library(SDLMAIN_LIBRARY 148 NAMES SDLmain SDLmain-1.1 149 HINTS 150 ENV SDLDIR 151 PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 152 PATHS 153 /opt 154 ) 155 endif() 156endif() 157 158# SDL may require threads on your system. 159# The Apple build may not need an explicit flag because one of the 160# frameworks may already provide it. 161# But for non-OSX systems, I will use the CMake Threads package. 162if(NOT APPLE) 163 find_package(Threads) 164endif() 165 166# MinGW needs an additional link flag, -mwindows 167# It's total link flags should look like -lmingw32 -lSDLmain -lSDL -mwindows 168if(MINGW) 169 set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW") 170endif() 171 172if(SDL_LIBRARY_TEMP) 173 # For SDLmain 174 if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY) 175 list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX) 176 if(_SDL_MAIN_INDEX EQUAL -1) 177 set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP}) 178 endif() 179 unset(_SDL_MAIN_INDEX) 180 endif() 181 182 # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa. 183 # CMake doesn't display the -framework Cocoa string in the UI even 184 # though it actually is there if I modify a pre-used variable. 185 # I think it has something to do with the CACHE STRING. 186 # So I use a temporary variable until the end so I can set the 187 # "real" variable in one-shot. 188 if(APPLE) 189 set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} "-framework Cocoa") 190 endif() 191 192 # For threads, as mentioned Apple doesn't need this. 193 # In fact, there seems to be a problem if I used the Threads package 194 # and try using this line, so I'm just skipping it entirely for OS X. 195 if(NOT APPLE) 196 set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT}) 197 endif() 198 199 # For MinGW library 200 if(MINGW) 201 set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP}) 202 endif() 203 204 # Set the final string here so the GUI reflects the final state. 205 set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found") 206endif() 207 208if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h") 209 file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$") 210 file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$") 211 file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$") 212 string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}") 213 string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}") 214 string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}") 215 unset(SDL_VERSION_MAJOR_LINE) 216 unset(SDL_VERSION_MINOR_LINE) 217 unset(SDL_VERSION_PATCH_LINE) 218 set(SDL_VERSION ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH}) 219 set(SDL_VERSION_STRING ${SDL_VERSION}) 220endif() 221 222include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) 223 224FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL 225 REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR 226 VERSION_VAR SDL_VERSION_STRING) 227 228if(SDL_FOUND) 229 set(SDL_LIBRARIES ${SDL_LIBRARY}) 230 set(SDL_INCLUDE_DIRS ${SDL_INCLUDE_DIR}) 231 if(NOT TARGET SDL::SDL) 232 add_library(SDL::SDL INTERFACE IMPORTED) 233 set_target_properties(SDL::SDL PROPERTIES 234 INTERFACE_INCLUDE_DIRECTORIES "${SDL_INCLUDE_DIR}" 235 INTERFACE_LINK_LIBRARIES "${SDL_LIBRARY}") 236 endif() 237endif() 238