FindGLFW3.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #.rst:
  2. # FindGLFW3
  3. # ------------
  4. #
  5. # Locate glfw3 library
  6. #
  7. # This module defines
  8. #
  9. # ::
  10. #
  11. # GLFW3_LIBRARIES, the library to link against
  12. # GLFW3_FOUND, if false, do not try to link to FREETYPE
  13. # GLFW3_INCLUDE_DIRS, where to find headers.
  14. # This is the concatenation of the paths:
  15. # GLFW3_INCLUDE_DIR
  16. #
  17. #=============================================================================
  18. # Copyright 2014-2014 Martell Malone
  19. #
  20. # Distributed under the OSI-approved BSD License (the "License");
  21. # see accompanying file Copyright.txt for details.
  22. #
  23. # This software is distributed WITHOUT ANY WARRANTY; without even the
  24. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  25. # See the License for more information.
  26. #=============================================================================
  27. # (To distribute this file outside of CMake, substitute the full
  28. # License text for the above reference.)
  29. # glfw has special requirements for linking (from docs: http://www.glfw.org/docs/latest/build.html)
  30. # MINGW or MSVC + static "glfw3" -> link: opengl32, gdi32 (plus glu32 if use GLU)
  31. # MINGW or MSVC + dynamic "glfw3dll" (but this not true ;) -> -DGLFW_DLL link: no
  32. # UNIX + static -> pkg-config --static --libs
  33. # UNIX + dynamic -> pkg-config --libs
  34. # So... if we find dynamic version, no problems, but if we find static, we need to determine deps
  35. # but cmake can't simply say to us what kind of library it found. So we try to find static version
  36. # first, and then if nothing found, we repeat search for dynamic
  37. find_package(PkgConfig)
  38. if(PKG_CONFIG_FOUND)
  39. message(STATUS "PkgConfig found")
  40. else()
  41. message(STATUS "PkgConfig not found, if you have only static glfw library, you build can fail")
  42. endif()
  43. if(PKG_CONFIG_FOUND)
  44. # Save some global stuff that we change, to revert after work has been done
  45. set(_saved_PKG_CONFIG_PATH "$ENV{PKG_CONFIG_PATH}")
  46. set(_saved_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
  47. # add /usr/local/lib/pkgconfig to pkg-config search path (some linuxes do not do that, but glfw installs to taht prefix by default)
  48. file(TO_CMAKE_PATH "$ENV{PKG_CONFIG_PATH}" PKG_CONFIG_PATH)
  49. list(APPEND PKG_CONFIG_PATH "/usr/local/lib/pkgconfig")
  50. file(TO_NATIVE_PATH "${PKG_CONFIG_PATH}" new_pkg_config_path)
  51. set(ENV{PKG_CONFIG_PATH} "${new_pkg_config_path}")
  52. # now try to find glfw with pkg-config
  53. pkg_check_modules(PC_GLFW3 glfw3)
  54. if(PC_GLFW3_FOUND)
  55. # try to find static library
  56. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
  57. find_library(GLFW3_STATIC_LIBRARY NAMES glfw3 libglfw3 PATHS ${PC_GLFW3_LIBRARY_DIRS} NO_DEFAULT_PATH)
  58. find_library(GLFW3_STATIC_LIBRARY NAMES glfw3 libglfw3 PATHS ${PC_GLFW3_LIBRARY_DIRS})
  59. # also we include glfw3.h header, not GLFW/glfw3.h :(
  60. find_path(GLFW3_INCLUDE_DIRS glfw3.h PATH_SUFFIXES GLFW PATHS ${PC_GLFW3_INCLUDE_DIRS} NO_DEFAULT_PATH)
  61. find_path(GLFW3_INCLUDE_DIRS glfw3.h PATH_SUFFIXES GLFW PATHS ${PC_GLFW3_INCLUDE_DIRS})
  62. if(GLFW3_STATIC_LIBRARY)
  63. # glfw3 is static
  64. set(GLFW3_LIBRARIES ${PC_GLFW3_STATIC_LIBRARIES})
  65. set(GLFW3_LIBRARY_DIRS ${PC_GLFW3_STATIC_LIBRARY_DIRS})
  66. # We also need to add any other LDFLAGS, but with additional fixup for Apple frameworks :(
  67. if(APPLE)
  68. unset(_is_framework)
  69. foreach(_arg ${PC_GLFW3_STATIC_LDFLAGS_OTHER})
  70. if(_is_framework)
  71. set(var FRAMEWORK_${_arg}_LIBRARY)
  72. find_library(${var} ${_arg})
  73. if(${var})
  74. list(APPEND GLFW3_LIBRARIES ${${var}})
  75. endif()
  76. unset(var)
  77. unset(_is_framework)
  78. else()
  79. if(_arg STREQUAL "-framework")
  80. set(_is_framework 1)
  81. else()
  82. list(APPEND GLFW3_LIBRARIES ${_arg})
  83. endif()
  84. endif()
  85. endforeach()
  86. else(APPLE)
  87. list(APPEND GLFW3_LIBRARIES ${PC_GLFW3_STATIC_LDFLAGS_OTHER})
  88. endif(APPLE)
  89. else()
  90. # glfw3 is dynamic
  91. set(GLFW3_DEFINITIONS -DGLFW_DLL)
  92. set(GLFW3_LIBRARIES ${PC_GLFW3_LIBRARIES})
  93. set(GLFW3_LIBRARY_DIRS ${PC_GLFW3_LIBRARY_DIRS})
  94. endif()
  95. set(GLFW3_FOUND 1)
  96. endif()
  97. # Restore global stuff
  98. set(CMAKE_FIND_LIBRARY_SUFFIXES "${_saved_CMAKE_FIND_LIBRARY_SUFFIXES}")
  99. set(ENV{PKG_CONFIG_PATH} "${_saved_PKG_CONFIG_PATH}")
  100. endif(PKG_CONFIG_FOUND)
  101. # fallback if pkg-config method not work
  102. if(NOT GLFW3_FOUND)
  103. find_path(GLFW3_INCLUDE_DIR glfw3.h
  104. HINTS
  105. ENV GLFW3_DIR
  106. PATH_SUFFIXES include/GLFW include
  107. PATHS
  108. ~/Library/Frameworks
  109. /Library/Frameworks
  110. /usr/local
  111. /usr
  112. /sw # Fink
  113. /opt/local # DarwinPorts
  114. /opt/csw # Blastwave
  115. /opt
  116. )
  117. find_library(GLFW3_LIBRARY
  118. NAMES glfw3 libglfw3 glfw
  119. HINTS
  120. ENV GLFW3_DIR
  121. PATH_SUFFIXES lib
  122. PATHS
  123. ~/Library/Frameworks
  124. /Library/Frameworks
  125. /usr/local
  126. /usr
  127. /sw
  128. /opt/local
  129. /opt/csw
  130. /opt
  131. )
  132. set(GLFW3_INCLUDE_DIRS "${GLFW3_INCLUDE_DIR}")
  133. set(GLFW3_LIBRARIES "${GLFW3_LIBRARY}")
  134. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  135. find_package_handle_standard_args(GLFW3 DEFAULT_MSG GLFW3_LIBRARIES GLFW3_INCLUDE_DIR)
  136. endif()
  137. mark_as_advanced(GLFW3_INCLUDE_DIR GLFW3_LIBRARIES GLFW3_LIBRARY)