FindSFML.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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[ \t]+([0-9]+)" SFML_VERSION_MAJOR_MATCH "${SFML_CONFIG_HPP_CONTENTS}")
  87. STRING(REGEX MATCH "#define SFML_VERSION_MINOR[ \t]+([0-9]+)" SFML_VERSION_MINOR_MATCH "${SFML_CONFIG_HPP_CONTENTS}")
  88. STRING(REGEX REPLACE "#define SFML_VERSION_MAJOR[ \t]+([0-9]+)" "\\1" SFML_VERSION_MAJOR "${SFML_VERSION_MAJOR_MATCH}")
  89. STRING(REGEX REPLACE "#define SFML_VERSION_MINOR[ \t]+([0-9]+)" "\\1" SFML_VERSION_MINOR "${SFML_VERSION_MINOR_MATCH}")
  90. math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10 + ${SFML_FIND_VERSION_MINOR}")
  91. # if we could extract them, compare with the requested version number
  92. if (SFML_VERSION_MAJOR)
  93. # transform version numbers to an integer
  94. math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10 + ${SFML_VERSION_MINOR}")
  95. # compare them
  96. if(SFML_VERSION LESS SFML_REQUESTED_VERSION)
  97. set(SFML_VERSION_OK FALSE)
  98. endif()
  99. else()
  100. # SFML version is < 3.0
  101. if (SFML_REQUESTED_VERSION GREATER 29)
  102. set(SFML_VERSION_OK FALSE)
  103. if (SFML_REQUESTED_VERSION GREATER 19)
  104. set(SFML_VERSION_MAJOR 1)
  105. else()
  106. set(SFML_VERSION_MAJOR 2)
  107. endif()
  108. set(SFML_VERSION_MINOR x)
  109. endif()
  110. endif()
  111. endif()
  112. # find the requested modules
  113. set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found
  114. set(FIND_SFML_LIB_PATHS
  115. ${SFML_ROOT}
  116. $ENV{SFML_ROOT}
  117. ~/Library/Frameworks
  118. /Library/Frameworks
  119. /usr/local
  120. /usr
  121. /sw
  122. /opt/local
  123. /opt/csw
  124. /opt)
  125. foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
  126. string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
  127. string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
  128. set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER}${FIND_SFML_LIB_SUFFIX})
  129. # no suffix for sfml-main, it is always a static library
  130. if(FIND_SFML_COMPONENT_LOWER STREQUAL "main")
  131. set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER})
  132. endif()
  133. # debug library
  134. find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
  135. NAMES ${FIND_SFML_COMPONENT_NAME}-d
  136. PATH_SUFFIXES lib64 lib
  137. PATHS ${FIND_SFML_LIB_PATHS})
  138. # release library
  139. find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
  140. NAMES ${FIND_SFML_COMPONENT_NAME}
  141. PATH_SUFFIXES lib64 lib
  142. PATHS ${FIND_SFML_LIB_PATHS})
  143. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  144. # library found
  145. set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE)
  146. # if both are found, set SFML_XXX_LIBRARY to contain both
  147. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  148. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}
  149. optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  150. endif()
  151. # if only one debug/release variant is found, set the other to be equal to the found one
  152. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  153. # debug and not release
  154. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
  155. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
  156. endif()
  157. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
  158. # release and not debug
  159. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  160. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  161. endif()
  162. else()
  163. # library not found
  164. set(SFML_FOUND FALSE)
  165. set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE)
  166. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "")
  167. set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY")
  168. endif()
  169. # mark as advanced
  170. MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY
  171. SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
  172. SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
  173. # add to the global list of libraries
  174. set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}")
  175. endforeach()
  176. # handle errors
  177. if(NOT SFML_VERSION_OK)
  178. # SFML version not ok
  179. set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR})")
  180. set(SFML_FOUND FALSE)
  181. elseif(NOT SFML_FOUND)
  182. # include directory or library not found
  183. set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})")
  184. endif()
  185. if (NOT SFML_FOUND)
  186. if(SFML_FIND_REQUIRED)
  187. # fatal error
  188. message(FATAL_ERROR ${FIND_SFML_ERROR})
  189. elseif(NOT SFML_FIND_QUIETLY)
  190. # error but continue
  191. message(STATUS "${FIND_SFML_ERROR}")
  192. endif()
  193. endif()
  194. # handle success
  195. if(SFML_FOUND)
  196. message(STATUS "Found SFML ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR} in ${SFML_INCLUDE_DIR}")
  197. foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
  198. string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
  199. string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
  200. if(NOT TARGET sfml-${FIND_SFML_COMPONENT_LOWER})
  201. add_library(sfml-${FIND_SFML_COMPONENT_LOWER} UNKNOWN IMPORTED)
  202. set_target_properties(sfml-${FIND_SFML_COMPONENT_LOWER} PROPERTIES
  203. IMPORTED_LOCATION "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}"
  204. INTERFACE_INCLUDE_DIRECTORIES "${SFML_INCLUDE_DIR}"
  205. )
  206. if(NOT ${FIND_SFML_COMPONENT_LOWER} STREQUAL system)
  207. set_target_properties(sfml-${FIND_SFML_COMPONENT_LOWER} PROPERTIES
  208. INTERFACE_LINK_LIBRARIES sfml-system
  209. )
  210. endif()
  211. endif()
  212. endforeach()
  213. endif()