/cmake/Modules/FindSFML.cmake

https://github.com/deadalnix/SFML · CMake · 175 lines · 108 code · 17 blank · 50 comment · 15 complexity · 005bcbe3046d0fcf65749c420702ab64 MD5 · raw file

  1. # Locate the SFML library
  2. #
  3. # This module defines the following variables:
  4. # - For each module XXX (SYSTEM, WINDOW, GRAPHICS, NETWORK, AUDIO, MAIN):
  5. # - 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)
  6. # - 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)
  7. # - SFML_XXX_LIBRARY, the name of the library to link to for the xxx module (includes both debug and optimized names if necessary)
  8. # - SFML_XXX_FOUND, true if either the debug or release library of the xxx module is found
  9. # - SFML_LIBRARIES, the list of all libraries corresponding to the required modules
  10. # - SFML_FOUND, true if all the required modules are found
  11. # - SFML_INCLUDE_DIR, the path where SFML headers are located (the directory containing the SFML/Config.hpp file)
  12. #
  13. # By default, the dynamic libraries of SFML will be found. To find the static ones instead,
  14. # you must set the SFML_STATIC_LIBRARIES variable to TRUE before calling find_package(SFML ...).
  15. # In case of static linking, the SFML_STATIC macro will also be defined by this script.
  16. #
  17. # On Mac OS X if SFML_STATIC_LIBRARIES is not set to TRUE then by default CMake will search for frameworks unless
  18. # CMAKE_FIND_FRAMEWORK is set to "NEVER" for example. Please refer to CMake documentation for more details.
  19. # Moreover, keep in mind that SFML frameworks are only available as release libraries unlike dylibs which
  20. # are available for both release and debug modes.
  21. #
  22. # If SFML is not installed in a standard path, you can use the SFMLDIR CMake variable or environment variable
  23. # to tell CMake where SFML is.
  24. # define the SFML_STATIC macro if static build was chosen
  25. if(SFML_STATIC_LIBRARIES)
  26. add_definitions(-DSFML_STATIC)
  27. endif()
  28. # deduce the libraries suffix from the options
  29. set(FIND_SFML_LIB_SUFFIX "")
  30. if(SFML_STATIC_LIBRARIES)
  31. set(FIND_SFML_LIB_SUFFIX "${FIND_SFML_LIB_SUFFIX}-s")
  32. endif()
  33. # find the SFML include directory
  34. find_path(SFML_INCLUDE_DIR SFML/Config.hpp
  35. PATH_SUFFIXES include
  36. PATHS
  37. ${SFMLDIR}
  38. $ENV{SFMLDIR}
  39. ~/Library/Frameworks
  40. /Library/Frameworks
  41. /usr/local/
  42. /usr/
  43. /sw # Fink
  44. /opt/local/ # DarwinPorts
  45. /opt/csw/ # Blastwave
  46. /opt/)
  47. # check the version number
  48. set(SFML_VERSION_OK TRUE)
  49. if(SFML_FIND_VERSION AND SFML_INCLUDE_DIR)
  50. # extract the major and minor version numbers from SFML/Config.hpp
  51. FILE(READ "${SFML_INCLUDE_DIR}/SFML/Config.hpp" SFML_CONFIG_HPP_CONTENTS)
  52. STRING(REGEX MATCH ".*#define SFML_VERSION_MAJOR ([0-9]+).*#define SFML_VERSION_MINOR ([0-9]+).*" SFML_CONFIG_HPP_CONTENTS "${SFML_CONFIG_HPP_CONTENTS}")
  53. STRING(REGEX REPLACE ".*#define SFML_VERSION_MAJOR ([0-9]+).*" "\\1" SFML_VERSION_MAJOR "${SFML_CONFIG_HPP_CONTENTS}")
  54. STRING(REGEX REPLACE ".*#define SFML_VERSION_MINOR ([0-9]+).*" "\\1" SFML_VERSION_MINOR "${SFML_CONFIG_HPP_CONTENTS}")
  55. math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10 + ${SFML_FIND_VERSION_MINOR}")
  56. # if we could extract them, compare with the requested version number
  57. if (SFML_VERSION_MAJOR)
  58. # transform version numbers to an integer
  59. math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10 + ${SFML_VERSION_MINOR}")
  60. # compare them
  61. if(SFML_VERSION LESS SFML_REQUESTED_VERSION)
  62. set(SFML_VERSION_OK FALSE)
  63. endif()
  64. else()
  65. # SFML version is < 2.0
  66. if (SFML_REQUESTED_VERSION GREATER 19)
  67. set(SFML_VERSION_OK FALSE)
  68. set(SFML_VERSION_MAJOR 1)
  69. set(SFML_VERSION_MINOR x)
  70. endif()
  71. endif()
  72. endif()
  73. # find the requested modules
  74. set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found
  75. set(FIND_SFML_LIB_PATHS
  76. ${SFMLDIR}
  77. $ENV{SFMLDIR}
  78. ~/Library/Frameworks
  79. /Library/Frameworks
  80. /usr/local
  81. /usr
  82. /sw
  83. /opt/local
  84. /opt/csw
  85. /opt)
  86. foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
  87. string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
  88. string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
  89. set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER}${FIND_SFML_LIB_SUFFIX})
  90. # no suffix for sfml-main, it is always a static library
  91. if(FIND_SFML_COMPONENT_LOWER STREQUAL "main")
  92. set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER})
  93. endif()
  94. # debug library
  95. find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
  96. NAMES ${FIND_SFML_COMPONENT_NAME}-d
  97. PATH_SUFFIXES lib64 lib
  98. PATHS ${FIND_SFML_LIB_PATHS})
  99. # release library
  100. find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
  101. NAMES ${FIND_SFML_COMPONENT_NAME}
  102. PATH_SUFFIXES lib64 lib
  103. PATHS ${FIND_SFML_LIB_PATHS})
  104. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  105. # library found
  106. set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE)
  107. # if both are found, set SFML_XXX_LIBRARY to contain both
  108. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  109. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}
  110. optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  111. endif()
  112. # if only one debug/release variant is found, set the other to be equal to the found one
  113. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
  114. # debug and not release
  115. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
  116. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
  117. endif()
  118. if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
  119. # release and not debug
  120. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  121. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
  122. endif()
  123. else()
  124. # library not found
  125. set(SFML_FOUND FALSE)
  126. set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE)
  127. set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "")
  128. set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY")
  129. endif()
  130. # mark as advanced
  131. MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY
  132. SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
  133. SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
  134. # add to the global list of libraries
  135. set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}")
  136. endforeach()
  137. # handle errors
  138. if(NOT SFML_VERSION_OK)
  139. # SFML version not ok
  140. set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR})")
  141. set(SFML_FOUND FALSE)
  142. elseif(NOT SFML_FOUND)
  143. # include directory or library not found
  144. set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})")
  145. endif()
  146. if (NOT SFML_FOUND)
  147. if(SFML_FIND_REQUIRED)
  148. # fatal error
  149. message(FATAL_ERROR ${FIND_SFML_ERROR})
  150. elseif(NOT SFML_FIND_QUIETLY)
  151. # error but continue
  152. message("${FIND_SFML_ERROR}")
  153. endif()
  154. endif()
  155. # handle success
  156. if(SFML_FOUND)
  157. message("Found SFML: ${SFML_INCLUDE_DIR}")
  158. endif()