Debian Codegraph @

CMake/FindSFML.cmake

raw
1 # This script locates the SFML library
2 # ------------------------------------
3 #
4 # Usage
5 # -----
6 #
7 # When you try to locate the SFML libraries, you must specify which modules you want to use (system, window, graphics, network, audio, main).
8 # If none is given, the SFML_LIBRARIES variable will be empty and you'll end up linking to nothing.
9 # example:
10 # find_package(SFML COMPONENTS graphics window system) // find the graphics, window and system modules
11 #
12 # You can enforce a specific version, either MAJOR.MINOR or only MAJOR.
13 # If nothing is specified, the version won't be checked (ie. any version will be accepted).
14 # example:
15 # find_package(SFML COMPONENTS ...) // no specific version required
16 # find_package(SFML 2 COMPONENTS ...) // any 2.x version
17 # find_package(SFML 2.4 COMPONENTS ...) // version 2.4 or greater
18 #
19 # By default, the dynamic libraries of SFML will be found. To find the static ones instead,
20 # you must set the SFML_STATIC_LIBRARIES variable to TRUE before calling find_package(SFML ...).
21 # In case of static linking, the SFML_STATIC macro will also be defined by this script.
22 # example:
23 # set(SFML_STATIC_LIBRARIES TRUE)
24 # find_package(SFML 2 COMPONENTS network system)
25 #
26 # On Mac OS X if SFML_STATIC_LIBRARIES is not set to TRUE then by default CMake will search for frameworks unless
27 # CMAKE_FIND_FRAMEWORK is set to "NEVER" for example. Please refer to CMake documentation for more details.
28 # Moreover, keep in mind that SFML frameworks are only available as release libraries unlike dylibs which
29 # are available for both release and debug modes.
30 #
31 # If SFML is not installed in a standard path, you can use the SFML_ROOT CMake (or environment) variable
32 # to tell CMake where SFML is.
33 #
34 # Output
35 # ------
36 #
37 # This script defines the following variables:
38 # - For each specified module XXX (system, window, graphics, network, audio, main):
39 # - SFML_XXX_LIBRARY_DEBUG: the name of the debug library of the xxx module (set to SFML_XXX_LIBRARY_RELEASE is no debug version is found)
40 # - SFML_XXX_LIBRARY_RELEASE: the name of the release library of the xxx module (set to SFML_XXX_LIBRARY_DEBUG is no release version is found)
41 # - SFML_XXX_LIBRARY: the name of the library to link to for the xxx module (includes both debug and optimized names if necessary)
42 # - SFML_XXX_FOUND: true if either the debug or release library of the xxx module is found
43 # - SFML_LIBRARIES: the list of all libraries corresponding to the required modules
44 # - SFML_FOUND: true if all the required modules are found
45 # - SFML_INCLUDE_DIR: the path where SFML headers are located (the directory containing the SFML/Config.hpp file)
46 #
47 # example:
48 # find_package(SFML 2 COMPONENTS system window graphics audio REQUIRED)
49 # include_directories(${SFML_INCLUDE_DIR})
50 # add_executable(myapp ...)
51 # target_link_libraries(myapp ${SFML_LIBRARIES})
52
53 # define the SFML_STATIC macro if static build was chosen
54 if(SFML_STATIC_LIBRARIES)
55 add_definitions(-DSFML_STATIC)
56 endif()
57
58 # deduce the libraries suffix from the options
59 set(FIND_SFML_LIB_SUFFIX "")
60 if(SFML_STATIC_LIBRARIES)
61 set(FIND_SFML_LIB_SUFFIX "${FIND_SFML_LIB_SUFFIX}-s")
62 endif()
63
64 # find the SFML include directory
65 find_path(SFML_INCLUDE_DIR SFML/Config.hpp
66 PATH_SUFFIXES include
67 PATHS
68 ${SFML_ROOT}
69 $ENV{SFML_ROOT}
70 ~/Library/Frameworks
71 /Library/Frameworks
72 /usr/local/
73 /usr/
74 /sw # Fink
75 /opt/local/ # DarwinPorts
76 /opt/csw/ # Blastwave
77 /opt/)
78
79 # check the version number
80 set(SFML_VERSION_OK TRUE)
81 if(SFML_FIND_VERSION AND SFML_INCLUDE_DIR)
82 # extract the major and minor version numbers from SFML/Config.hpp
83 # we have to handle framework a little bit differently :
84 if("${SFML_INCLUDE_DIR}" MATCHES "SFML.framework")
85 set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/Headers/Config.hpp")
86 else()
87 set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/SFML/Config.hpp")
88 endif()
89 FILE(READ "${SFML_CONFIG_HPP_INPUT}" SFML_CONFIG_HPP_CONTENTS)
90 STRING(REGEX MATCH "#define SFML_VERSION_MAJOR[ \t]+([0-9]+)" SFML_VERSION_MAJOR_MATCH "${SFML_CONFIG_HPP_CONTENTS}")
91 STRING(REGEX MATCH "#define SFML_VERSION_MINOR[ \t]+([0-9]+)" SFML_VERSION_MINOR_MATCH "${SFML_CONFIG_HPP_CONTENTS}")
92 STRING(REGEX REPLACE "#define SFML_VERSION_MAJOR[ \t]+([0-9]+)" "\\1" SFML_VERSION_MAJOR "${SFML_VERSION_MAJOR_MATCH}")
93 STRING(REGEX REPLACE "#define SFML_VERSION_MINOR[ \t]+([0-9]+)" "\\1" SFML_VERSION_MINOR "${SFML_VERSION_MINOR_MATCH}")
94 math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10 + ${SFML_FIND_VERSION_MINOR}")
95
96 # if we could extract them, compare with the requested version number
97 if (SFML_VERSION_MAJOR)
98 # transform version numbers to an integer
99 math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10 + ${SFML_VERSION_MINOR}")
100
101 # compare them
102 if(SFML_VERSION LESS SFML_REQUESTED_VERSION)
103 set(SFML_VERSION_OK FALSE)
104 endif()
105 else()
106 # SFML version is < 3.0
107 if (SFML_REQUESTED_VERSION GREATER 29)
108 set(SFML_VERSION_OK FALSE)
109 if (SFML_REQUESTED_VERSION GREATER 19)
110 set(SFML_VERSION_MAJOR 1)
111 else()
112 set(SFML_VERSION_MAJOR 2)
113 endif()
114 set(SFML_VERSION_MINOR x)
115 endif()
116 endif()
117 endif()
118
119 # find the requested modules
120 set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found
121 set(FIND_SFML_LIB_PATHS
122 ${SFML_ROOT}
123 $ENV{SFML_ROOT}
124 ~/Library/Frameworks
125 /Library/Frameworks
126 /usr/local
127 /usr
128 /sw
129 /opt/local
130 /opt/csw
131 /opt)
132 foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
133 string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
134 string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
135 set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER}${FIND_SFML_LIB_SUFFIX})
136
137 # no suffix for sfml-main, it is always a static library
138 if(FIND_SFML_COMPONENT_LOWER STREQUAL "main")
139 set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER})
140 endif()
141
142 # debug library
143 find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
144 NAMES ${FIND_SFML_COMPONENT_NAME}-d
145 PATH_SUFFIXES lib64 lib
146 PATHS ${FIND_SFML_LIB_PATHS})
147
148 # release library
149 find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
150 NAMES ${FIND_SFML_COMPONENT_NAME}
151 PATH_SUFFIXES lib64 lib
152 PATHS ${FIND_SFML_LIB_PATHS})
153
154 if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
155 # library found
156 set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE)
157
158 # if both are found, set SFML_XXX_LIBRARY to contain both
159 if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
160 set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}
161 optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
162 endif()
163
164 # if only one debug/release variant is found, set the other to be equal to the found one
165 if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
166 # debug and not release
167 set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
168 set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
169 endif()
170 if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
171 # release and not debug
172 set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
173 set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
174 endif()
175 else()
176 # library not found
177 set(SFML_FOUND FALSE)
178 set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE)
179 set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "")
180 set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY")
181 endif()
182
183 # mark as advanced
184 MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY
185 SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
186 SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
187
188 # add to the global list of libraries
189 set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}")
190 endforeach()
191
192 # handle errors
193 if(NOT SFML_VERSION_OK)
194 # SFML version not ok
195 set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR})")
196 set(SFML_FOUND FALSE)
197 elseif(NOT SFML_FOUND)
198 # include directory or library not found
199 set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})")
200 endif()
201 if (NOT SFML_FOUND)
202 if(SFML_FIND_REQUIRED)
203 # fatal error
204 message(FATAL_ERROR ${FIND_SFML_ERROR})
205 elseif(NOT SFML_FIND_QUIETLY)
206 # error but continue
207 message(STATUS "${FIND_SFML_ERROR}")
208 endif()
209 endif()
210
211 # handle success
212 if(SFML_FOUND)
213 message(STATUS "Found SFML ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR} in ${SFML_INCLUDE_DIR}")
214 foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
215 string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
216 string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
217 if(NOT TARGET sfml-${FIND_SFML_COMPONENT_LOWER})
218 add_library(sfml-${FIND_SFML_COMPONENT_LOWER} UNKNOWN IMPORTED)
219 set_target_properties(sfml-${FIND_SFML_COMPONENT_LOWER} PROPERTIES
220 IMPORTED_LOCATION "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}"
221 INTERFACE_INCLUDE_DIRECTORIES "${SFML_INCLUDE_DIR}"
222 )
223 if(NOT ${FIND_SFML_COMPONENT_LOWER} STREQUAL system)
224 set_target_properties(sfml-${FIND_SFML_COMPONENT_LOWER} PROPERTIES
225 INTERFACE_LINK_LIBRARIES sfml-system
226 )
227 endif()
228 endif()
229 endforeach()
230 endif()