FindSDL.cmake 7.33 KB
Newer Older
Benjamin Eikel's avatar
Benjamin Eikel committed
1
# - Locate SDL library
Ken Martin's avatar
Ken Martin committed
2
# This module defines
Benjamin Eikel's avatar
Benjamin Eikel committed
3
4
5
#  SDL_LIBRARY, the name of the library to link against
#  SDL_FOUND, if false, do not try to link to SDL
#  SDL_INCLUDE_DIR, where to find SDL.h
Benjamin Eikel's avatar
Benjamin Eikel committed
6
#  SDL_VERSION_STRING, human-readable string containing the version of SDL
7
8
#
# This module responds to the the flag:
Benjamin Eikel's avatar
Benjamin Eikel committed
9
10
11
12
13
14
#  SDL_BUILDING_LIBRARY
#    If this is defined, then no SDL_main will be linked in because
#    only applications need main().
#    Otherwise, it is assumed you are building an application and this
#    module will attempt to locate and set the the proper link flags
#    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
24
25
# and no SDL_LIBRARY, it means CMake did not find your SDL library
# (SDL.dll, libsdl.so, SDL.framework, etc).
# Set SDL_LIBRARY_TEMP to point to your SDL library, and configure again.
26
27
28
29
30
# Similarly, if you see an empty SDLMAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL_LIBRARY
# variable, but when these values are unset, SDL_LIBRARY does not get created.
#
#
Ken Martin's avatar
Ken Martin committed
31
32
33
34
# $SDLDIR is an environment variable that would
# correspond to the ./configure --prefix=$SDLDIR
# used in building SDL.
# l.e.galup  9-20-02
35
#
36
# Modified by Eric Wing.
37
38
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
39
40
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
41
42
43
44
45
46
# Also corrected the header search path to follow "proper" SDL guidelines.
# Added a search for SDLmain which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
47
# People will have to manually change the cache values of
48
49
# SDL_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
50
51
52
53
54
#
# Note that the header path has changed from SDL/SDL.h to just SDL.h
# This needed to change because "proper" SDL convention
# is #include "SDL.h", not <SDL/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL/ (see FreeBSD).
Alexander Neundorf's avatar
   
Alexander Neundorf committed
55

56
57
#=============================================================================
# Copyright 2003-2009 Kitware, Inc.
58
# Copyright 2012 Benjamin Eikel
59
60
61
62
63
64
65
66
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
67
# (To distribute this file outside of CMake, substitute the full
68
69
#  License text for the above reference.)

70
find_path(SDL_INCLUDE_DIR SDL.h
71
  HINTS
72
    ENV SDLDIR
73
74
75
  PATH_SUFFIXES SDL SDL12 SDL11
                # path suffixes to search inside ENV{SDLDIR}
                include/SDL include/SDL12 include/SDL11 include
76
77
78
79
)

# SDL-1.1 is the name used by FreeBSD ports...
# don't confuse it for the version number.
80
find_library(SDL_LIBRARY_TEMP
81
  NAMES SDL SDL-1.1
82
  HINTS
83
    ENV SDLDIR
84
  PATH_SUFFIXES lib
85
)
Alexander Neundorf's avatar
   
Alexander Neundorf committed
86

87
88
if(NOT SDL_BUILDING_LIBRARY)
  if(NOT ${SDL_INCLUDE_DIR} MATCHES ".framework")
89
90
    # Non-OS X framework versions expect you to also dynamically link to
    # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
91
92
    # seem to provide SDLmain for compatibility even though they don't
    # necessarily need it.
93
    find_library(SDLMAIN_LIBRARY
94
      NAMES SDLmain SDLmain-1.1
95
      HINTS
96
        ENV SDLDIR
97
      PATH_SUFFIXES lib
98
99
100
101
102
      PATHS
      /sw
      /opt/local
      /opt/csw
      /opt
103
    )
104
105
  endif()
endif()
Ken Martin's avatar
Ken Martin committed
106

107
# SDL may require threads on your system.
108
109
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
110
# But for non-OSX systems, I will use the CMake Threads package.
111
112
if(NOT APPLE)
  find_package(Threads)
113
endif()
Ken Martin's avatar
Ken Martin committed
114

115
116
117
# MinGW needs an additional library, mwindows
# It's total link flags should look like -lmingw32 -lSDLmain -lSDL -lmwindows
# (Actually on second look, I think it only needs one of the m* libraries.)
118
119
if(MINGW)
  set(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
120
endif()
Ken Martin's avatar
Ken Martin committed
121

122
if(SDL_LIBRARY_TEMP)
123
  # For SDLmain
124
125
126
  if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY)
    list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
    if(_SDL_MAIN_INDEX EQUAL -1)
127
      set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP})
128
    endif()
129
    unset(_SDL_MAIN_INDEX)
130
  endif()
131
132

  # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
133
  # CMake doesn't display the -framework Cocoa string in the UI even
134
135
  # though it actually is there if I modify a pre-used variable.
  # I think it has something to do with the CACHE STRING.
136
  # So I use a temporary variable until the end so I can set the
137
  # "real" variable in one-shot.
138
139
  if(APPLE)
    set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} "-framework Cocoa")
140
  endif()
141

142
  # For threads, as mentioned Apple doesn't need this.
143
  # In fact, there seems to be a problem if I used the Threads package
144
  # and try using this line, so I'm just skipping it entirely for OS X.
145
146
  if(NOT APPLE)
    set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
147
  endif()
148
149

  # For MinGW library
150
151
  if(MINGW)
    set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP})
152
  endif()
153

154
  # Set the final string here so the GUI reflects the final state.
155
  set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
156
  # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
157
  set(SDL_LIBRARY_TEMP "${SDL_LIBRARY_TEMP}" CACHE INTERNAL "")
158
endif()
159

Benjamin Eikel's avatar
Benjamin Eikel committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
  file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
  file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
  file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
  string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}")
  string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}")
  string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}")
  set(SDL_VERSION_STRING ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH})
  unset(SDL_VERSION_MAJOR_LINE)
  unset(SDL_VERSION_MINOR_LINE)
  unset(SDL_VERSION_PATCH_LINE)
  unset(SDL_VERSION_MAJOR)
  unset(SDL_VERSION_MINOR)
  unset(SDL_VERSION_PATCH)
endif()

176
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
Alexander Neundorf's avatar
   
Alexander Neundorf committed
177

Rolf Eike Beer's avatar
Rolf Eike Beer committed
178
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
Benjamin Eikel's avatar
Benjamin Eikel committed
179
180
                                  REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
                                  VERSION_VAR SDL_VERSION_STRING)