FindSFML.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. # define the SFML_STATIC macro if static build was chosen
  53. if(SFML_STATIC_LIBRARIES)
  54. add_definitions(-DSFML_STATIC)
  55. endif()
  56. # deduce the libraries suffix from the options
  57. set(FIND_SFML_LIB_SUFFIX "")
  58. if(SFML_STATIC_LIBRARIES)
  59. set(FIND_SFML_LIB_SUFFIX "${FIND_SFML_LIB_SUFFIX}-s")
  60. endif()
  61. # find the SFML include directory
  62. find_path(SFML_INCLUDE_DIR SFML/Config.hpp
  63. PATH_SUFFIXES include
  64. PATHS
  65. ${SFML_ROOT}
  66. $ENV{SFML_ROOT}
  67. ~/Library/Frameworks
  68. /Library/Frameworks
  69. /usr/local/
  70. /usr/
  71. /sw # Fink
  72. /opt/local/ # DarwinPorts
  73. /opt/csw/ # Blastwave
  74. /opt/)
  75. # check the version number
  76. set(SFML_VERSION_OK TRUE)
  77. if(SFML_FIND_VERSION AND SFML_INCLUDE_DIR)
  78. # extract the major and minor version numbers from SFML/Config.hpp
  79. # we have to handle framework a little bit differently :
  80. if("${SFML_INCLUDE_DIR}" MATCHES "SFML.framework")
  81. set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/Headers/Config.hpp")
  82. else()
  83. set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/SFML/Config.hpp")
  84. endif()
  85. FILE(READ "${SFML_CONFIG_HPP_INPUT}" SFML_CONFIG_HPP_CONTENTS)
  86. STRING(REGEX MATCH ".*#define SFML_VERSION_MAJOR ([0-9]+).*#define SFML_VERSION_MINOR ([0-9]+).*" SFML_CONFIG_HPP_CONTENTS "${SFML_CONFIG_HPP_CONTENTS}")
  87. STRING(REGEX REPLACE ".*#define SFML_VERSION_MAJOR ([0-9]+).*" "\\1" SFML_VERSION_MAJOR "${SFML_CONFIG_HPP_CONTENTS}")
  88. STRING(REGEX REPLACE ".*#define SFML_VERSION_MINOR ([0-9]+).*" "\\1" SFML_VERSION_MINOR "${SFML_CONFIG_HPP_CONTENTS}")
  89. math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10 + ${SFML_FIND_VERSION_MINOR}")
  90. # if we could extract them, compare with the requested version number
  91. if (SFML_VERSION_MAJOR)
  92. # transform version numbers to an integer
  93. math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10 + ${SFML_VERSION_MINOR}")
  94. # compare them
  95. if(SFML_VERSION LESS SFML_REQUESTED_VERSION)
  96. set(SFML_VERSION_OK FALSE)
  97. endif()
  98. else()
  99. # SFML version is < 2.0
  100. if (SFML_REQUESTED_VERSION GREATER 19)
  101. set(SFML_VERSION_OK FALSE)
  102. set(SFML_VERSION_MAJOR 1)
  103. set(SFML_VERSION_MINOR x)
  104. endif()
  105. endif()
  106. endif()
  107. # find the requested modules
  108. set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found
  109. set(FIND_SFML_LIB_PATHS
  110. ${SFML_ROOT}
  111. $ENV{SFML_ROOT}
  112. ~/Library/Frameworks
  113. /Library/Frameworks
  114. /usr/local
  115. /usr
  116. /sw
  117. /opt/local
  118. /opt/csw
  119. /opt)
  120. foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
  121. string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
  122. string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
  123. set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER}${FIND_SFML_LIB_SUFFIX})
  124. # no suffix for sfml-main, it is always a static library
  125. if(FIND_SFML_COMPONENT_LOWER STREQUAL "main")
  126. set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER})
  127. endif()
  128. # debug library
  129. find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
  130. NAMES ${FIND_SFML_COMPONENT_NAME}-d
  131. PATH_SUFFIXES lib64 lib
  132. PATHS ${FIND_SFML_LIB_PATHS})
  133. # release library
  134. find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
  135. NAMES ${FIND_SFML_COMPONENT_NAME}
  136. PATH_SUFFIXES lib64 lib
  137. PATHS ${FIND_SFML_LIB_PATHS})
  138. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  139. # library found
  140. set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE)
  141. # if both are found, set SFML_XXX_LIBRARY to contain both
  142. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  143. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}
  144. optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  145. endif()
  146. # if only one debug/release variant is found, set the other to be equal to the found one
  147. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  148. # debug and not release
  149. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
  150. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
  151. endif()
  152. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
  153. # release and not debug
  154. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  155. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  156. endif()
  157. else()
  158. # library not found
  159. set(SFML_FOUND FALSE)
  160. set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE)
  161. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "")
  162. set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY")
  163. endif()
  164. # mark as advanced
  165. MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY
  166. SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
  167. SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
  168. # add to the global list of libraries
  169. set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}")
  170. endforeach()
  171. # handle errors
  172. if(NOT SFML_VERSION_OK)
  173. # SFML version not ok
  174. set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR})")
  175. set(SFML_FOUND FALSE)
  176. elseif(NOT SFML_FOUND)
  177. # include directory or library not found
  178. set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})")
  179. endif()
  180. if (NOT SFML_FOUND)
  181. if(SFML_FIND_REQUIRED)
  182. # fatal error
  183. message(FATAL_ERROR ${FIND_SFML_ERROR})
  184. elseif(NOT SFML_FIND_QUIETLY)
  185. # error but continue
  186. message(STATUS "${FIND_SFML_ERROR}")
  187. endif()
  188. endif()
  189. # handle success
  190. if(SFML_FOUND)
  191. message(STATUS "Found SFML ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR} in ${SFML_INCLUDE_DIR}")
  192. foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
  193. string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
  194. string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
  195. if(NOT TARGET sfml-${FIND_SFML_COMPONENT_LOWER})
  196. add_library(sfml-${FIND_SFML_COMPONENT_LOWER} UNKNOWN IMPORTED)
  197. set_target_properties(sfml-${FIND_SFML_COMPONENT_LOWER} PROPERTIES
  198. IMPORTED_LOCATION "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}"
  199. INTERFACE_INCLUDE_DIRECTORIES "${SFML_INCLUDE_DIR}"
  200. )
  201. if(NOT ${FIND_SFML_COMPONENT_LOWER} STREQUAL system)
  202. set_target_properties(sfml-${FIND_SFML_COMPONENT_LOWER} PROPERTIES
  203. INTERFACE_LINK_LIBRARIES sfml-system
  204. )
  205. endif()
  206. endif()
  207. endforeach()
  208. endif()